Tool 系统

wesgine 的工具系统:六种来源、分类机制、执行流程与 JSON Schema 规范。


六种工具来源

来源归属层生命周期
Tier E 内置(read/write/exec/grep…)Layer 2 Kernel编译时固定
Tier C SDK 闭包(cell.Tools().Register)Layer 3 Cell随 Cell
MCP proxy(cell.MCPs().Upsert)Layer 3 Cell动态 Connect/Deregister
Adapter 派生(kb/email/cron/headless-browser/playwright/desktop)Layer 3 Cell随 CellSpec 对应字段
Sub-agent as ToolLayer 3 Cell 内部自引用随 Cell
Per-run 临时 Tool(RunParams.Tools)Layer 4 Actor/Request单次 Run

API

查询

方法路径说明
GET/tools工具列表(JSON Schema)
GET/tools/descriptions工具描述(纯文本)

SDK 注册

cell.Tools().Register(tool.Tool{
    Name:        "my_tool",
    Description: "自定义工具",
    Schema:      tool.Schema{...},
    Handler:     func(ctx tool.Context, args map[string]any) (string, error) { ... },
})

Tier E 内置工具

引擎二进制 embed 的核心工具,所有 Cell 默认可用:

工具说明
read读取文件内容
write写入文件
edit编辑文件(apply patch)
apply_patch应用补丁
exec执行命令
grep / search_files搜索文件内容
glob / find_files按模式查找文件
memory记忆读写搜索
plan计划管理
delegate_task委派子任务
ask_userHITL 交互
browser_navigate浏览器导航
web_search网络搜索

工具分类(CycleDetector 用)

分类说明典型工具
categoryExploration只读探索read、grep、search_files、plan
categoryAction写操作(默认)write、edit、exec

分类优先级(四级 fallback)

  1. 硬编码 map(wesgine 内置 ~20 个工具)
  2. ExtraCategories(消费方声明)
  3. ToolMeta 自动推断(ReadOnly=true → exploration)
  4. 默认 categoryAction

执行流程

Agent Loop 决定调用工具
  ↓
ToolRegistry 查找工具定义
  ↓
Governance 检查(Hardline → Sandbox → Zone)
  ↓
ToolContext 注入 Cell 边界信息
  ↓
Handler 执行
  ↓
结果返回 Agent Loop

ToolContext

工具执行时接收的上下文信息:

字段说明
CellID当前 Cell ID
Actor当前请求主体
RunID当前 Run ID
SessionID当前 Session ID
AgentID当前 Agent ID
WorkDir工作目录
AllowPaths允许访问的路径
DenyPaths禁止访问的路径

Agent 工具白名单

Agent 可配置 Tools 字段过滤可用工具:

AgentConfig{
    Tools: []string{"read", "write", "exec", "grep"},
}

与 Cell DisabledEngineSkills AND 生效。


MCP 工具

通过 MCP 协议代理的外部工具自动注册到 Cell ToolRegistry:

cell.MCPs().Upsert(ctx, mcp.ServerConfig{
    Name:      "my-mcp",
    Command:   []string{"./mcp-server"},
    Transport: "stdio",
})

MCP 工具对 Agent 透明——与内置工具无差别。


Plugin 工具

Plugin 子进程通过 JSON-RPC 暴露工具,由 Manager 桥接到 ToolRegistry:


Progress-Aware 工具

参数变化时 CycleDetector streak /= 2(减半):

消费方通过 ExtraProgressAware 声明额外的 progress-aware 工具。


JSON Schema 规范

工具定义使用 JSON Schema 描述参数:

{
  "name": "read",
  "description": "读取文件内容",
  "parameters": {
    "type": "object",
    "properties": {
      "path": {
        "type": "string",
        "description": "文件路径"
      },
      "offset": {
        "type": "integer",
        "description": "起始行号"
      },
      "limit": {
        "type": "integer",
        "description": "读取行数"
      }
    },
    "required": ["path"]
  }
}

相关文档