feat: 解决工具沙箱缓存机制问题,增加forceReinstall参数支持 (#114)

* 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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Sean
2025-07-05 08:00:02 +08:00
committed by GitHub
parent 8f592cb880
commit e414dc0d18
2 changed files with 21 additions and 22 deletions

View File

@ -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<Object>} 执行结果
*/
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()

View File

@ -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')
})
}
];