From 0953c1b72b30cdb604798a52f94483e118d13107 Mon Sep 17 00:00:00 2001 From: kicer Date: Wed, 23 Sep 2026 10:17:28 +0800 Subject: [PATCH] =?UTF-8?q?lab=E5=85=83=E5=B7=A5=E5=85=B7:tool+=E8=87=AA?= =?UTF-8?q?=E7=94=B1args=E8=BD=AC=E5=8F=91=E5=86=85=E7=BD=AE+MCP(args?= =?UTF-8?q?=E4=B8=BA=E8=87=AA=E7=94=B1=E5=AF=B9=E8=B1=A1=E2=80=94=E2=80=94?= =?UTF-8?q?=E7=9B=AE=E6=A0=87schema=E5=AE=9A=E4=B9=89=E6=9C=9F=E4=B8=8D?= =?UTF-8?q?=E5=8F=AF=E7=9F=A5,=E8=A7=84=E8=8C=83=C2=A7=E4=B8=89=E5=90=88?= =?UTF-8?q?=E6=B3=95=E4=BE=8B=E5=A4=96,=E6=9D=A1=E4=BB=B6=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E5=BD=92=E5=86=85=E5=B1=82handler);help=E4=BC=AA?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=8E=B0=E5=9C=BA=E5=8F=91=E7=8E=B0(?= =?UTF-8?q?=E6=97=A0=E5=8F=82=E5=88=97=E4=B8=80=E5=8F=A5=E8=AF=9D=E5=90=8D?= =?UTF-8?q?=E5=8D=95/=E5=B8=A6args.tool=E5=87=BA=E5=AE=8C=E6=95=B4?= =?UTF-8?q?=E5=AE=9A=E4=B9=89),=E6=9C=AA=E7=9F=A5=E5=90=8D=E4=B8=8E?= =?UTF-8?q?=E7=BC=BA=E5=8F=82=E9=94=99=E8=AF=AF=E5=9B=9E=E6=98=BE=E5=85=A8?= =?UTF-8?q?=E6=B8=85=E5=8D=95,=E7=A6=81=E5=B5=8C=E5=A5=97,=E5=86=85?= =?UTF-8?q?=E5=B1=82=E9=94=99=E8=AF=AF=E5=86=92=E6=B3=A1=E4=B8=8D=E5=8C=85?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/server/tools/definitions.json | 21 ++++++++++++++++ app/server/tools/executor.js | 42 +++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/app/server/tools/definitions.json b/app/server/tools/definitions.json index f3156c9..66269b5 100644 --- a/app/server/tools/definitions.json +++ b/app/server/tools/definitions.json @@ -213,5 +213,26 @@ } } } + }, + { + "type": "function", + "function": { + "name": "lab", + "description": "Unified entry point for all available tools (a meta tool; only present for profiles that opt in).\n\nCall a tool:\n lab(tool: \"\", args: {})\n\nThe target tool's arguments go inside `args` as a JSON object. Omit `args` for tools that take no arguments.\n\nDiscovering tools:\n lab(tool: \"help\") → list all tools with a one-line description each\n lab(tool: \"help\", args: {tool: \"\"}) → full description and parameter schema of \n\nAn unknown tool name also returns the list of available tools.\nNested lab calls are rejected; inner tool errors surface unchanged.\n\nPrefer calling a tool directly when its name and parameters are clear from the task. Use \"help\" when unsure — it costs one extra round-trip.", + "parameters": { + "type": "object", + "required": ["tool"], + "properties": { + "tool": { + "type": "string", + "description": "Name of the tool to call, or \"help\"." + }, + "args": { + "type": "object", + "description": "Arguments for the target tool, matching its own schema (look it up with lab(tool: \"help\", args: {tool: \"\"}) if unsure). Omit for tools that take no arguments." + } + } + } + } } ] diff --git a/app/server/tools/executor.js b/app/server/tools/executor.js index c7117c7..b69faf4 100644 --- a/app/server/tools/executor.js +++ b/app/server/tools/executor.js @@ -9,6 +9,8 @@ const skills = require('./skills'); const rexec = require('../remote-exec'); const publicFiles = require('../public-files'); const scheduler = require('../scheduler'); // 只依赖 log/cron-parser,不成环(engine→executor→scheduler 单向) +// lab 的 help 伪工具要读全量定义:与 engine 同路径 require → node 缓存同一对象,单一事实源。 +const BUILTIN_TOOL_DEFS = require('./definitions.json'); // ---------- 路径沙箱 ---------- function resolveInTask(taskDir, p) { @@ -824,6 +826,45 @@ async function toolSkill(ctx, args) { } } +// lab:全工具统一入口(元工具,tools 数组收缩到 1 个仍可达内置 + MCP 全部能力)。 +// args 为自由对象——目标 schema 定义期不可知(docs/tool-design.md §三 的合法例外), +// 条件校验归内层 handler;错误冒泡不包装(只有一层)。help 是伪工具:不进 HANDLERS, +// 只在本函数早退。默认被全局 tools_deny 屏蔽,仅 lab profile 显式清空 deny 后下发。 +async function toolLab(ctx, args) { + const name = String(args.tool || '').trim(); + if (!name) throw new Error('lab: 缺少参数 tool。可用:' + listCallableTools().join(', ')); + if (name === 'lab') throw new Error('lab: 不允许嵌套调用 lab。'); + if (name === 'help') return runLabHelp(args.args); + const inner = args.args; + if (inner != null && (typeof inner !== 'object' || Array.isArray(inner))) { + throw new Error('lab: args 必须是对象(目标工具的键值参数),收到:' + JSON.stringify(inner).slice(0, 80)); + } + // 先自查再转发:未知名要带全清单回显(§五 报错回显),executeTool 的兑底文案没这个待遇。 + if (!HANDLERS[name] && !mcp.hasTool(name)) { + throw new Error('未知工具 "' + name + '"。可用:' + listCallableTools().join(', ')); + } + return executeTool(ctx, name, inner || {}); +} + +function listCallableTools() { + // help 不在 HANDLERS(伪工具);lab 自身不列(嵌套已被拒,列了只会诱导模型绕圈)。 + // MCP 侧用 toolDefinitions() 现成取名——含冲突重命名的 server__ 前缀,与实际可调用名严格一致。 + return Object.keys(HANDLERS).filter(n => n !== 'lab') + .concat(mcp.toolDefinitions().map(t => t.function.name)); +} + +function runLabHelp(args) { + const all = BUILTIN_TOOL_DEFS.concat(mcp.toolDefinitions()) + .filter(t => t && t.function && t.function.name !== 'lab'); + const target = args && String(args.tool || '').trim(); + if (!target) { + return all.map(t => t.function.name + ' — ' + String(t.function.description || '').split('\n')[0].trim()).join('\n'); + } + const def = all.find(t => t.function.name === target); + if (!def) throw new Error('未知工具 "' + target + '"。可用:' + all.map(t => t.function.name).join(', ')); + return JSON.stringify(def.function, null, 2); +} + const HANDLERS = { read_file: toolReadFile, write_file: toolWriteFile, @@ -833,6 +874,7 @@ const HANDLERS = { fetch: toolFetch, schedule: toolScheduleTask, skill: toolSkill, + lab: toolLab, }; // ctx: {sessionId, taskDir, run, cfg, db, vision}