Elffuss BraidHub

kikocis/continue public

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