| 1 | import { 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 ToggleSwitch from "../../../../gui/Switch"; |
| 7 | import ToolPolicyItem from "./ToolPolicyItem"; |
| 8 | |
| 9 | export const ToolPoliciesSection = () => { |
| 10 | const availableTools = useAppSelector((state) => state.config.config.tools); |
| 11 | const toolGroupSettings = useAppSelector( |
| 12 | (store) => store.ui.toolGroupSettings, |
| 13 | ); |
| 14 | const dispatch = useAppDispatch(); |
| 15 | |
| 16 | const toolsByGroup = useMemo(() => { |
| 17 | const byGroup: Record<string, Tool[]> = {}; |
| 18 | for (const tool of availableTools) { |
| 19 | if (!byGroup[tool.group]) { |
| 20 | byGroup[tool.group] = []; |
| 21 | } |
| 22 | byGroup[tool.group].push(tool); |
| 23 | } |
| 24 | return Object.entries(byGroup); |
| 25 | }, [availableTools]); |
| 26 | |
| 27 | // Detect duplicate tool names |
| 28 | const duplicateDetection = useMemo(() => { |
| 29 | const counts: Record<string, number> = {}; |
| 30 | availableTools.forEach((tool) => { |
| 31 | if (counts[tool.function.name]) { |
| 32 | counts[tool.function.name] = counts[tool.function.name] + 1; |
| 33 | } else { |
| 34 | counts[tool.function.name] = 1; |
| 35 | } |
| 36 | }); |
| 37 | return Object.fromEntries( |
| 38 | Object.entries(counts).map(([k, v]) => [k, v > 1]), |
| 39 | ); |
| 40 | }, [availableTools]); |
| 41 | |
| 42 | return ( |
| 43 | <> |
| 44 | {toolsByGroup.map(([groupName, tools]) => ( |
| 45 | <div key={groupName} className="mt-2 flex flex-col pr-1"> |
| 46 | <div className="flex flex-row items-center justify-between px-1"> |
| 47 | <h3 |
| 48 | className="m-0 p-0 font-bold" |
| 49 | style={{ |
| 50 | fontSize: fontSize(-2), |
| 51 | }} |
| 52 | > |
| 53 | {groupName} |
| 54 | </h3> |
| 55 | <ToggleSwitch |
| 56 | isToggled={toolGroupSettings[groupName] !== "exclude"} |
| 57 | onToggle={() => dispatch(toggleToolGroupSetting(groupName))} |
| 58 | text="" |
| 59 | size={10} |
| 60 | /> |
| 61 | </div> |
| 62 | <div className={`relative flex flex-col p-1`}> |
| 63 | {tools.map((tool) => ( |
| 64 | <ToolPolicyItem |
| 65 | key={tool.uri + tool.function.name} |
| 66 | tool={tool} |
| 67 | duplicatesDetected={duplicateDetection[tool.function.name]} |
| 68 | excluded={toolGroupSettings[groupName] === "exclude"} |
| 69 | /> |
| 70 | ))} |
| 71 | </div> |
| 72 | </div> |
| 73 | ))} |
| 74 | </> |
| 75 | ); |
| 76 | }; |