From 07577fc94b129dbc1935fd77df369bc1cdafb8c4 Mon Sep 17 00:00:00 2001 From: kshern Date: Mon, 24 Feb 2025 02:36:52 +0800 Subject: [PATCH] feat:toolsbar add array support --- client/src/components/DynamicJsonForm.tsx | 55 ++++++++++++++++------- client/src/components/ToolsTab.tsx | 14 +++--- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/client/src/components/DynamicJsonForm.tsx b/client/src/components/DynamicJsonForm.tsx index ff26118..e760a13 100644 --- a/client/src/components/DynamicJsonForm.tsx +++ b/client/src/components/DynamicJsonForm.tsx @@ -215,23 +215,48 @@ const DynamicJsonForm = ({ return; } - const newValue = { - ...(typeof value === "object" && value !== null && !Array.isArray(value) - ? value - : {}), - } as JsonObject; - let current: JsonObject = newValue; - - for (let i = 0; i < path.length - 1; i++) { - const key = path[i]; - if (!(key in current)) { - current[key] = {}; + const updateArray = (array: JsonValue[], path: string[], value: JsonValue): JsonValue[] => { + const [index, ...restPath] = path; + const arrayIndex = Number(index); + const newArray = [...array]; + + if (restPath.length === 0) { + newArray[arrayIndex] = value; + } else { + newArray[arrayIndex] = updateValue(newArray[arrayIndex], restPath, value); } - current = current[key] as JsonObject; - } + return newArray; + }; - current[path[path.length - 1]] = fieldValue; - onChange(newValue); + const updateObject = (obj: JsonObject, path: string[], value: JsonValue): JsonObject => { + const [key, ...restPath] = path; + const newObj = { ...obj }; + + if (restPath.length === 0) { + newObj[key] = value; + } else { + newObj[key] = updateValue(newObj[key], restPath, value); + } + return newObj; + }; + + const updateValue = (current: JsonValue, path: string[], value: JsonValue): JsonValue => { + if (path.length === 0) return value; + + if (!current) { + current = !isNaN(Number(path[0])) ? [] : {}; + } + + if (Array.isArray(current)) { + return updateArray(current, path, value); + } else if (typeof current === 'object' && current !== null) { + return updateObject(current, path, value); + } + + return current; + }; + + onChange(updateValue(value, path, fieldValue)); }; return ( diff --git a/client/src/components/ToolsTab.tsx b/client/src/components/ToolsTab.tsx index 418e58e..23be41a 100644 --- a/client/src/components/ToolsTab.tsx +++ b/client/src/components/ToolsTab.tsx @@ -17,11 +17,6 @@ import ListPane from "./ListPane"; import { CompatibilityCallToolResult } from "@modelcontextprotocol/sdk/types.js"; -type SchemaProperty = { - type: string; - description?: string; - properties?: Record; -}; const ToolsTab = ({ tools, @@ -168,7 +163,7 @@ const ToolsTab = ({

{Object.entries(selectedTool.inputSchema.properties ?? []).map( ([key, value]) => { - const prop = value as SchemaProperty; + const prop = value as JsonSchemaType; return (