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