| 1 | import { |
| 2 | ChevronRightIcon, |
| 3 | InformationCircleIcon, |
| 4 | } from "@heroicons/react/24/outline"; |
| 5 | import { Tool } from "core"; |
| 6 | import { BUILT_IN_GROUP_NAME } from "core/tools/builtIn"; |
| 7 | import { useEffect, useMemo, useState } from "react"; |
| 8 | import { useDispatch } from "react-redux"; |
| 9 | import { Tooltip } from "react-tooltip"; |
| 10 | import { useAppSelector } from "../../../../../redux/hooks"; |
| 11 | import { |
| 12 | addTool, |
| 13 | toggleToolSetting, |
| 14 | } from "../../../../../redux/slices/uiSlice"; |
| 15 | import { ToolTip } from "../../../../gui/Tooltip"; |
| 16 | import { useFontSize } from "../../../../ui/font"; |
| 17 | |
| 18 | interface ToolDropdownItemProps { |
| 19 | tool: Tool; |
| 20 | duplicatesDetected: boolean; |
| 21 | isGroupEnabled: boolean; |
| 22 | } |
| 23 | |
| 24 | function ToolPolicyItem(props: ToolDropdownItemProps) { |
| 25 | const dispatch = useDispatch(); |
| 26 | const policy = useAppSelector( |
| 27 | (state) => state.ui.toolSettings[props.tool.function.name], |
| 28 | ); |
| 29 | const [isExpanded, setIsExpanded] = useState(false); |
| 30 | const mode = useAppSelector((state) => state.session.mode); |
| 31 | |
| 32 | useEffect(() => { |
| 33 | if (!policy) { |
| 34 | dispatch(addTool(props.tool)); |
| 35 | } |
| 36 | }, [props.tool.function.name, policy]); |
| 37 | |
| 38 | const parameters = useMemo(() => { |
| 39 | if (props.tool.function.parameters?.properties) { |
| 40 | return Object.entries(props.tool.function.parameters.properties).map( |
| 41 | ([name, schema]) => |
| 42 | [name, schema] as [string, { description: string; type: string }], |
| 43 | ); |
| 44 | } |
| 45 | return undefined; |
| 46 | }, [props.tool.function.parameters]); |
| 47 | |
| 48 | const fontSize = useFontSize(-2); |
| 49 | |
| 50 | const disabled = |
| 51 | !props.isGroupEnabled || |
| 52 | (mode === "plan" && |
| 53 | props.tool.group === BUILT_IN_GROUP_NAME && |
| 54 | !props.tool.readonly); |
| 55 | |
| 56 | if (!policy) { |
| 57 | return null; |
| 58 | } |
| 59 | const disabledTooltipId = `disabled-note-${props.tool.function.name}`; |
| 60 | |
| 61 | return ( |
| 62 | <div |
| 63 | className="flex flex-col" |
| 64 | style={{ |
| 65 | fontSize, |
| 66 | }} |
| 67 | > |
| 68 | <div className="flex flex-row items-center"> |
| 69 | <div |
| 70 | className={`hover:bg-badge hover:text-list-active-foreground xs:gap-1.5 flex flex-1 cursor-pointer flex-row items-center gap-1 rounded py-0.5 pl-1 pr-2`} |
| 71 | onClick={() => setIsExpanded((val) => !val)} |
| 72 | > |
| 73 | <ChevronRightIcon |
| 74 | className={`xs:flex hidden h-3 w-3 flex-shrink-0 transition-all duration-200 ${isExpanded ? "rotate-90" : ""}`} |
| 75 | /> |
| 76 | |
| 77 | <div |
| 78 | className={`flex items-center gap-1 rounded-md`} |
| 79 | style={{ |
| 80 | fontSize, |
| 81 | }} |
| 82 | > |
| 83 | {props.duplicatesDetected ? ( |
| 84 | <ToolTip |
| 85 | place="bottom" |
| 86 | className="flex flex-wrap items-center" |
| 87 | content={ |
| 88 | <p className="m-0 p-0"> |
| 89 | <span>Duplicate tool name</span>{" "} |
| 90 | <code>{props.tool.function.name}</code>{" "} |
| 91 | <span> |
| 92 | detected. Permissions will conflict and usage may be |
| 93 | unpredictable |
| 94 | </span> |
| 95 | </p> |
| 96 | } |
| 97 | > |
| 98 | <InformationCircleIcon className="h-3 w-3 flex-shrink-0 cursor-help text-yellow-500" /> |
| 99 | </ToolTip> |
| 100 | ) : null} |
| 101 | {props.tool.faviconUrl && ( |
| 102 | <img |
| 103 | src={props.tool.faviconUrl} |
| 104 | alt={props.tool.displayTitle} |
| 105 | className="h-3 w-3 flex-shrink-0" |
| 106 | /> |
| 107 | )} |
| 108 | <span className="line-clamp-1 break-all"> |
| 109 | {props.tool.originalFunctionName ?? props.tool.function.name} |
| 110 | </span> |
| 111 | </div> |
| 112 | </div> |
| 113 | |
| 114 | <div |
| 115 | className={`flex w-8 select-none flex-row items-center justify-end gap-2 px-2 py-0.5 sm:w-16 ${disabled ? "cursor-not-allowed" : "hover:text-list-active-foreground cursor-pointer hover:brightness-125"}`} |
| 116 | data-testid={`tool-policy-item-${props.tool.function.name}`} |
| 117 | data-tooltip-id={disabled ? disabledTooltipId : undefined} |
| 118 | onClick={ |
| 119 | disabled |
| 120 | ? undefined |
| 121 | : (e) => { |
| 122 | dispatch(toggleToolSetting(props.tool.function.name)); |
| 123 | e.stopPropagation(); |
| 124 | e.preventDefault(); |
| 125 | } |
| 126 | } |
| 127 | > |
| 128 | {disabled || policy === "disabled" ? ( |
| 129 | <> |
| 130 | <span className="text-lightgray sm:hidden">Off</span> |
| 131 | <span className="text-lightgray hidden sm:inline-block"> |
| 132 | Excluded |
| 133 | </span> |
| 134 | </> |
| 135 | ) : policy === "allowedWithoutPermission" ? ( |
| 136 | <> |
| 137 | <span className="text-success sm:hidden">Auto</span> |
| 138 | <span className="text-success hidden sm:inline-block"> |
| 139 | Automatic |
| 140 | </span> |
| 141 | </> |
| 142 | ) : ( |
| 143 | // allowedWithPermission |
| 144 | <> |
| 145 | <span className="text-warning sm:hidden">Ask</span> |
| 146 | <span className="text-warning hidden sm:inline-block"> |
| 147 | Ask First |
| 148 | </span> |
| 149 | </> |
| 150 | )} |
| 151 | </div> |
| 152 | <Tooltip id={disabledTooltipId}> |
| 153 | {mode === "chat" |
| 154 | ? "Tool disabled in chat mode" |
| 155 | : !props.isGroupEnabled |
| 156 | ? "Group is turned off" |
| 157 | : "Tool disabled in plan mode"} |
| 158 | </Tooltip> |
| 159 | </div> |
| 160 | <div |
| 161 | className={`flex flex-col overflow-hidden ${isExpanded ? "h-min" : "h-0 opacity-0"} gap-x-1 gap-y-2 pl-2 transition-all`} |
| 162 | > |
| 163 | <span className="mt-1.5 text-xs font-bold">Description:</span> |
| 164 | <span className="italic">{props.tool.function.description}</span> |
| 165 | {parameters ? ( |
| 166 | <> |
| 167 | <span className="text-xs font-bold">Arguments:</span> |
| 168 | {parameters.map((param, idx) => ( |
| 169 | <div key={idx} className="block"> |
| 170 | <code className="">{param[0]}</code> |
| 171 | <span className="ml-1">{`(${param[1].type}):`}</span> |
| 172 | <span className="ml-1 italic">{param[1].description}</span> |
| 173 | </div> |
| 174 | ))} |
| 175 | </> |
| 176 | ) : null} |
| 177 | <div className="h-1"></div> |
| 178 | </div> |
| 179 | </div> |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | export default ToolPolicyItem; |