feat:toolsbar add array support

This commit is contained in:
kshern
2025-02-24 02:36:52 +08:00
parent e28a64c932
commit 07577fc94b
2 changed files with 45 additions and 24 deletions

View File

@@ -215,23 +215,48 @@ const DynamicJsonForm = ({
return; return;
} }
const newValue = { const updateArray = (array: JsonValue[], path: string[], value: JsonValue): JsonValue[] => {
...(typeof value === "object" && value !== null && !Array.isArray(value) const [index, ...restPath] = path;
? value const arrayIndex = Number(index);
: {}), const newArray = [...array];
} as JsonObject;
let current: JsonObject = newValue; if (restPath.length === 0) {
newArray[arrayIndex] = value;
for (let i = 0; i < path.length - 1; i++) { } else {
const key = path[i]; newArray[arrayIndex] = updateValue(newArray[arrayIndex], restPath, value);
if (!(key in current)) {
current[key] = {};
} }
current = current[key] as JsonObject; return newArray;
} };
current[path[path.length - 1]] = fieldValue; const updateObject = (obj: JsonObject, path: string[], value: JsonValue): JsonObject => {
onChange(newValue); 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 ( return (

View File

@@ -17,11 +17,6 @@ import ListPane from "./ListPane";
import { CompatibilityCallToolResult } from "@modelcontextprotocol/sdk/types.js"; import { CompatibilityCallToolResult } from "@modelcontextprotocol/sdk/types.js";
type SchemaProperty = {
type: string;
description?: string;
properties?: Record<string, SchemaProperty>;
};
const ToolsTab = ({ const ToolsTab = ({
tools, tools,
@@ -168,7 +163,7 @@ const ToolsTab = ({
</p> </p>
{Object.entries(selectedTool.inputSchema.properties ?? []).map( {Object.entries(selectedTool.inputSchema.properties ?? []).map(
([key, value]) => { ([key, value]) => {
const prop = value as SchemaProperty; const prop = value as JsonSchemaType;
return ( return (
<div key={key}> <div key={key}>
<Label <Label
@@ -211,15 +206,16 @@ const ToolsTab = ({
} }
className="mt-1" className="mt-1"
/> />
) : prop.type === "object" ? ( ) : prop.type === "object"|| prop.type === "array" ? (
<div className="mt-1"> <div className="mt-1">
<DynamicJsonForm <DynamicJsonForm
schema={ schema={
{ {
type: "object", type: prop.type,
properties: prop.properties, properties: prop.properties,
description: prop.description, description: prop.description,
} as JsonSchemaType items:prop.items
}
} }
value={(params[key] as JsonValue) ?? {}} value={(params[key] as JsonValue) ?? {}}
onChange={(newValue: JsonValue) => { onChange={(newValue: JsonValue) => {