本章目标:帮助你理解工具注册机制、工具执行管道、以及 Shell Subprocess FS LSP Web / Skill 等能力插件的实现细节。阅读本章后,你应该能回答"一个工具是如何被模型调用的"以及"如何添加一个新的能力插件"。
6.1 工具注册机制
6.1.1 工具的三角色模式
每个工具能力遵循能力接缝模式:
预览
源码
graph TB
subgraph "Service Definition"
A["Service 定义<br/>接口声明"]
end
subgraph "Service Provider"
B["Provider 实现<br/>具体功能"]
end
subgraph "Consumer"
C["工具 Schema<br/>面向模型"]
end
A --> B
B --> C
graph LR
A["ctx.tools.register()"] --> B["工具注册表"]
B --> C["schemas()"]
C --> D["Prompt Assembly"]
D --> E["模型请求中的 tools 字段"]
6.2 工具执行管道
6.2.1 执行流程
预览
源码
sequenceDiagram
participant Model as LLM
participant Loop as Agent Loop
participant Pre as tools/pre-execute
participant Exec as tools/execute
participant Post as tools/post-execute
participant Tool as 实际工具
Model->>Loop: tool_call(name, args)
Loop->>Pre: waterfall('tools/pre-execute', input)
Note over Pre: 预处理、验证、拦截
Pre-->>Loop: modified input
Loop->>Exec: waterfall('tools/execute', input)
Exec->>Tool: 实际执行
Tool-->>Exec: result
Exec-->>Loop: result
Loop->>Post: waterfall('tools/post-execute', result)
Note over Post: 后处理、审计
Post-->>Loop: final result
Loop->>Model: tool_result
sequenceDiagram
participant Model as LLM
participant Loop as Agent Loop
participant Pre as tools/pre-execute
participant Exec as tools/execute
participant Post as tools/post-execute
participant Tool as 实际工具
Model->>Loop: tool_call(name, args)
Loop->>Pre: waterfall('tools/pre-execute', input)
Note over Pre: 预处理、验证、拦截
Pre-->>Loop: modified input
Loop->>Exec: waterfall('tools/execute', input)
Exec->>Tool: 实际执行
Tool-->>Exec: result
Exec-->>Loop: result
Loop->>Post: waterfall('tools/post-execute', result)
Note over Post: 后处理、审计
Post-->>Loop: final result
Loop->>Model: tool_result
graph TB
subgraph "Shell 能力"
A["shell/ (Service Definition)"]
B["shell-local/ (Provider)"]
C["shell-pwsh/ (Provider)"]
D["tool-shell/ (Consumer)"]
end
subgraph "依赖"
E["subprocess/"]
end
A --> B
A --> C
B --> E
C --> E
D --> A
// tool-shell 将 Shell 能力暴露为模型工具
ctx.tools.register({
name:'bash',
description:'Execute a bash command',
parameters:{
type:'object',
properties:{
command:{ type:'string', description:'The command to execute'}},
required:['command']},asyncexecute(input){const result =await ctx.shell.execute(input.command)return result
}})
6.4 Subprocess 能力
6.4.1 架构
预览
源码
graph TB
subgraph "Subprocess 能力"
A["subprocess/ (Service Definition)"]
B["subprocess-local/ (Provider)"]
C["消费者: shell, terminal, code-runtime"]
end
A --> B
A --> C
graph TB
subgraph "Subprocess 能力"
A["subprocess/ (Service Definition)"]
B["subprocess-local/ (Provider)"]
C["消费者: shell, terminal, code-runtime"]
end
A --> B
A --> C
graph TB
subgraph "FS 能力"
A["fs/ (Service Definition)"]
B["fs-local/ (Provider)"]
C["tool-fs-search/ (Consumer)"]
D["tool-fs-read/ (Consumer)"]
E["tool-fs-write/ (Consumer)"]
end
subgraph "策略"
F["fs-policy/"]
end
A --> B
A --> C
A --> D
A --> E
F --> A
graph TB
subgraph "FS 能力"
A["fs/ (Service Definition)"]
B["fs-local/ (Provider)"]
C["tool-fs-search/ (Consumer)"]
D["tool-fs-read/ (Consumer)"]
E["tool-fs-write/ (Consumer)"]
end
subgraph "策略"
F["fs-policy/"]
end
A --> B
A --> C
A --> D
A --> E
F --> A
graph TB
subgraph "Web 能力"
A["web/ (Service Definition)"]
B["web-search/ (Provider)"]
C["web-fetch/ (Provider)"]
D["tool-web/ (Consumer)"]
end
A --> B
A --> C
D --> A
graph TB
subgraph "Web 能力"
A["web/ (Service Definition)"]
B["web-search/ (Provider)"]
C["web-fetch/ (Provider)"]
D["tool-web/ (Consumer)"]
end
A --> B
A --> C
D --> A