From e414dc0d18f6ed94459c542e306a32bb07187874 Mon Sep 17 00:00:00 2001 From: Sean Date: Sat, 5 Jul 2025 08:00:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=A7=A3=E5=86=B3=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E6=B2=99=E7=AE=B1=E7=BC=93=E5=AD=98=E6=9C=BA=E5=88=B6=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E5=A2=9E=E5=8A=A0forceReinstall=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E6=94=AF=E6=8C=81=20(#114)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 为promptx_tool增加forceReinstall选项支持 解决工具沙箱缓存机制问题,允许强制重新安装工具依赖。 ## 修改内容 ### 1. 工具定义更新 (toolDefinitions.js) - 增加 context.options.forceReinstall 参数支持 - 增加 context.options.timeout 参数支持 - 完善参数描述,说明context用途 - 移除暂时不需要的role_id和session_id参数 ### 2. 执行逻辑优化 (ToolCommand.js) - 支持从context.options提取执行选项 - 将选项传递给ToolSandbox构造函数 - 增加调试日志输出沙箱选项 - 完善JSDoc注释 ## 解决的问题 - Issue #107: PromptX工具沙箱缓存机制不支持工具集更新 - 鲁班等AI角色在调试工具时遇到的缓存问题 - 工具依赖更新后无法自动重新安装的问题 ## 使用方式 ```javascript // 强制重新安装依赖 { tool_resource: "@tool://tool-name", parameters: { /* 工具参数 */ }, context: { options: { forceReinstall: true } } } ``` 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude * refactor: 简化context参数,移除暂时不需要的role_id和session_id 按照奥卡姆剃刀原则,移除当前没有实际用途的参数: - 移除 context.role_id - 移除 context.session_id - 保留 context.options 用于执行配置 - 简化API接口,降低复杂度 未来如需要可重新添加这些参数。 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude * refactor: 简化promptx_tool参数结构,移除context层级 ## 主要改进 ### 1. 扁平化参数结构 - 移除复杂的context嵌套,直接使用顶级参数 - forceReinstall 和 timeout 作为可选的顶级参数 - 遵循MCP协议惯例,降低AI理解成本 ### 2. 参数说明优化 - forceReinstall: 明确默认值为false - 详细说明使用场景:工具开发和调试中的缓存问题 - timeout: 明确默认值为30000ms ### 3. 防止盲目调用 - 增加重要提醒:要求AI先了解工具说明再调用 - 避免大模型在不了解工具的情况下盲目执行 ## 新的调用方式 ## 优势 - 符合MCP预训练共识,降低AI学习成本 - 参数结构简洁直观 - 保持向后兼容性 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --------- Co-authored-by: Claude --- src/lib/core/pouch/commands/ToolCommand.js | 11 +++++--- src/lib/mcp/toolDefinitions.js | 32 ++++++++++------------ 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/lib/core/pouch/commands/ToolCommand.js b/src/lib/core/pouch/commands/ToolCommand.js index 3406e8e..771ab25 100644 --- a/src/lib/core/pouch/commands/ToolCommand.js +++ b/src/lib/core/pouch/commands/ToolCommand.js @@ -97,7 +97,8 @@ ${JSON.stringify(result.result, null, 2)} * @param {Object} args - 命令参数 * @param {string} args.tool_resource - 工具资源引用,格式:@tool://tool-name * @param {Object} args.parameters - 传递给工具的参数 - * @param {Object} args.context - 执行上下文信息(可选) + * @param {boolean} args.forceReinstall - 是否强制重新安装工具依赖(默认false) + * @param {number} args.timeout - 工具执行超时时间(毫秒,默认30000ms) * @returns {Promise} 执行结果 */ async executeToolInternal(args) { @@ -108,12 +109,14 @@ ${JSON.stringify(result.result, null, 2)} // 1. 参数验证 this.validateArguments(args) - const { tool_resource, parameters, context = {} } = args + const { tool_resource, parameters, forceReinstall = false, timeout = 30000 } = args logger.debug(`[PromptXTool] 开始执行工具: ${tool_resource}`) - // 2. 创建ToolSandbox实例 - sandbox = new ToolSandbox(tool_resource) + // 2. 构建沙箱选项并创建ToolSandbox实例 + const sandboxOptions = { forceReinstall, timeout } + logger.debug(`[PromptXTool] 沙箱选项:`, sandboxOptions) + sandbox = new ToolSandbox(tool_resource, sandboxOptions) // 3. 设置ResourceManager const resourceManager = await this.getResourceManager() diff --git a/src/lib/mcp/toolDefinitions.js b/src/lib/mcp/toolDefinitions.js index 8f951db..8613b86 100644 --- a/src/lib/mcp/toolDefinitions.js +++ b/src/lib/mcp/toolDefinitions.js @@ -113,7 +113,7 @@ const TOOL_DEFINITIONS = [ }, { name: 'promptx_tool', - description: '🔧 [工具执行器] 执行通过@tool协议声明的JavaScript工具 - 支持角色配置中定义的专业工具能力,如@tool://calculator数学计算、@tool://send-email邮件发送等。提供安全沙箱执行、参数验证、错误处理和性能监控。', + description: '🔧 [工具执行器] 执行通过@tool协议声明的JavaScript工具 - 支持角色配置中定义的专业工具能力,如@tool://calculator数学计算、@tool://send-email邮件发送等。提供安全沙箱执行、参数验证、错误处理和性能监控。⚠️ **重要提醒**:请务必先查看具体工具的使用说明或通过welcome命令了解可用工具,不要在不了解工具功能和参数的情况下盲目调用。', inputSchema: { type: 'object', properties: { @@ -126,19 +126,15 @@ const TOOL_DEFINITIONS = [ type: 'object', description: '传递给工具的参数对象' }, - context: { - type: 'object', - description: '执行上下文信息(可选)', - properties: { - role_id: { - type: 'string', - description: '当前激活的角色ID' - }, - session_id: { - type: 'string', - description: '会话ID' - } - } + forceReinstall: { + type: 'boolean', + description: '是否强制重新安装工具依赖(默认false)。当工具代码更新但缓存未失效时设为true,用于解决工具开发和调试中的缓存问题', + default: false + }, + timeout: { + type: 'number', + description: '工具执行超时时间(毫秒),默认30000ms', + default: 30000 } }, required: ['tool_resource', 'parameters'] @@ -149,10 +145,10 @@ const TOOL_DEFINITIONS = [ .describe('工具资源引用,格式:@tool://tool-name'), parameters: z.object({}).passthrough() .describe('传递给工具的参数对象'), - context: z.object({ - role_id: z.string().optional().describe('当前激活的角色ID'), - session_id: z.string().optional().describe('会话ID') - }).optional().describe('执行上下文信息') + forceReinstall: z.boolean().optional().default(false) + .describe('是否强制重新安装工具依赖(默认false)'), + timeout: z.number().optional().default(30000) + .describe('工具执行超时时间(毫秒),默认30000ms') }) } ];