| 1 | import { MessageModes, Tool } from "core"; |
| 2 | import { useMemo } from "react"; |
| 3 | import { useAppDispatch, useAppSelector } from "../../../../../redux/hooks"; |
| 4 | import { toggleToolGroupSetting } from "../../../../../redux/slices/uiSlice"; |
| 5 | import { fontSize } from "../../../../../util"; |
| 6 | import Alert from "../../../../gui/Alert"; |
| 7 | import ToggleSwitch from "../../../../gui/Switch"; |
| 8 | import ToolPolicyItem from "./ToolPolicyItem"; |
| 9 | |
| 10 | export const ToolPoliciesSection = () => { |
| 11 | const mode = useAppSelector((state) => state.session.mode); |
| 12 | const availableTools = useAppSelector((state) => state.config.config.tools); |
| 13 | const toolGroupSettings = useAppSelector( |
| 14 | (store) => store.ui.toolGroupSettings, |
| 15 | ); |
| 16 | const dispatch = useAppDispatch(); |
| 17 | |
| 18 | const toolsByGroup = useMemo(() => { |
| 19 | const byGroup: Record<string, Tool[]> = {}; |
| 20 | for (const tool of availableTools) { |
| 21 | if (!byGroup[tool.group]) { |
| 22 | byGroup[tool.group] = []; |
| 23 | } |
| 24 | byGroup[tool.group].push(tool); |
| 25 | } |
| 26 | return Object.entries(byGroup); |
| 27 | }, [availableTools]); |
| 28 | |
| 29 | // Detect duplicate tool names |
| 30 | const duplicateDetection = useMemo(() => { |
| 31 | const counts: Record<string, number> = {}; |
| 32 | availableTools.forEach((tool) => { |
| 33 | if (counts[tool.function.name]) { |
| 34 | counts[tool.function.name] = counts[tool.function.name] + 1; |
| 35 | } else { |
| 36 | counts[tool.function.name] = 1; |
| 37 | } |
| 38 | }); |
| 39 | return Object.fromEntries( |
| 40 | Object.entries(counts).map(([k, v]) => [k, v > 1]), |
| 41 | ); |
| 42 | }, [availableTools]); |
| 43 | |
| 44 | const allToolsOff = mode === "chat"; |
| 45 | |
| 46 | const getMessage = (mode: MessageModes) => { |
| 47 | switch (mode) { |
| 48 | case "chat": |
| 49 | return "All tools disabled in Chat, switch to Plan or Agent mode to use tools"; |
| 50 | case "plan": |
| 51 | return "Read-only/MCP tools available in Plan mode"; |
| 52 | default: |
| 53 | return ""; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | const message = getMessage(mode); |
| 58 | |
| 59 | return ( |
| 60 | <> |
| 61 | {(mode === "chat" || mode === "plan") && ( |
| 62 | <div className="bg-background sticky top-0 z-10 my-1 mb-3"> |
| 63 | <Alert type="info" size="sm"> |
| 64 | <span className="text-2xs italic">{message}</span> |
| 65 | </Alert> |
| 66 | </div> |
| 67 | )} |
| 68 | {toolsByGroup.length === 0 && ( |
| 69 | <span className="text-description text-sm italic"> |
| 70 | No tools available |
| 71 | </span> |
| 72 | )} |
| 73 | {toolsByGroup.map(([groupName, tools]) => { |
| 74 | const isGroupEnabled = |
| 75 | !allToolsOff && toolGroupSettings[groupName] !== "exclude"; |
| 76 | return ( |
| 77 | <div key={groupName} className="mt-2 flex flex-col pr-1"> |
| 78 | <div className="flex flex-row items-center justify-between px-1"> |
| 79 | <h3 |
| 80 | className="m-0 p-0 font-bold" |
| 81 | style={{ |
| 82 | fontSize: fontSize(-2), |
| 83 | }} |
| 84 | > |
| 85 | {groupName} |
| 86 | </h3> |
| 87 | <ToggleSwitch |
| 88 | isToggled={isGroupEnabled} |
| 89 | onToggle={() => dispatch(toggleToolGroupSetting(groupName))} |
| 90 | text="" |
| 91 | size={10} |
| 92 | disabled={allToolsOff} |
| 93 | /> |
| 94 | </div> |
| 95 | <div className={`relative flex flex-col p-1`}> |
| 96 | {tools.map((tool) => ( |
| 97 | <ToolPolicyItem |
| 98 | key={tool.uri + tool.function.name} |
| 99 | tool={tool} |
| 100 | duplicatesDetected={duplicateDetection[tool.function.name]} |
| 101 | isGroupEnabled={isGroupEnabled} |
| 102 | /> |
| 103 | ))} |
| 104 | </div> |
| 105 | </div> |
| 106 | ); |
| 107 | })} |
| 108 | </> |
| 109 | ); |
| 110 | }; |