foliko 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +30 -0
- package/22.txt +10 -0
- package/README.md +218 -0
- package/SPEC.md +452 -0
- package/cli/bin/foliko.js +12 -0
- package/cli/src/commands/chat.js +75 -0
- package/cli/src/index.js +64 -0
- package/cli/src/ui/chat-ui.js +272 -0
- package/cli/src/utils/ansi.js +40 -0
- package/cli/src/utils/markdown.js +296 -0
- package/docs/quick-reference.md +131 -0
- package/docs/user-manual.md +1205 -0
- package/examples/basic.js +110 -0
- package/examples/bootstrap.js +93 -0
- package/examples/mcp-example.js +53 -0
- package/examples/skill-example.js +49 -0
- package/examples/workflow.js +158 -0
- package/package.json +36 -0
- package/plugins/ai-plugin.js +89 -0
- package/plugins/audit-plugin.js +187 -0
- package/plugins/default-plugins.js +412 -0
- package/plugins/file-system-plugin.js +344 -0
- package/plugins/install-plugin.js +93 -0
- package/plugins/python-executor-plugin.js +331 -0
- package/plugins/rules-plugin.js +292 -0
- package/plugins/scheduler-plugin.js +426 -0
- package/plugins/session-plugin.js +343 -0
- package/plugins/shell-executor-plugin.js +196 -0
- package/plugins/storage-plugin.js +237 -0
- package/plugins/subagent-plugin.js +395 -0
- package/plugins/think-plugin.js +329 -0
- package/plugins/tools-plugin.js +114 -0
- package/skills/mcp-usage/SKILL.md +198 -0
- package/skills/vb-agent-dev/AGENTS.md +162 -0
- package/skills/vb-agent-dev/SKILL.md +370 -0
- package/src/capabilities/index.js +11 -0
- package/src/capabilities/skill-manager.js +319 -0
- package/src/capabilities/workflow-engine.js +401 -0
- package/src/core/agent-chat.js +311 -0
- package/src/core/agent.js +573 -0
- package/src/core/framework.js +255 -0
- package/src/core/index.js +19 -0
- package/src/core/plugin-base.js +205 -0
- package/src/core/plugin-manager.js +392 -0
- package/src/core/provider.js +108 -0
- package/src/core/tool-registry.js +134 -0
- package/src/core/tool-router.js +216 -0
- package/src/executors/executor-base.js +58 -0
- package/src/executors/mcp-executor.js +728 -0
- package/src/index.js +37 -0
- package/src/utils/event-emitter.js +97 -0
- package/test-chat.js +129 -0
- package/test-mcp.js +79 -0
- package/test-reload.js +61 -0
|
@@ -0,0 +1,1205 @@
|
|
|
1
|
+
# VB-Agent Framework 用户手册
|
|
2
|
+
|
|
3
|
+
> 简约的插件化 Agent 框架
|
|
4
|
+
|
|
5
|
+
## 目录
|
|
6
|
+
|
|
7
|
+
1. [项目概述](#1-项目概述)
|
|
8
|
+
2. [快速开始](#2-快速开始)
|
|
9
|
+
3. [核心概念](#3-核心概念)
|
|
10
|
+
4. [内置插件](#4-内置插件)
|
|
11
|
+
5. [插件开发](#5-插件开发)
|
|
12
|
+
6. [技能系统](#6-技能系统)
|
|
13
|
+
7. [子 Agent 系统](#7-子-agent-系统)
|
|
14
|
+
8. [工具路由](#8-工具路由)
|
|
15
|
+
9. [配置说明](#9-配置说明)
|
|
16
|
+
10. [事件系统](#10-事件系统)
|
|
17
|
+
11. [API 参考](#11-api-参考)
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## 1. 项目概述
|
|
22
|
+
|
|
23
|
+
VB-Agent 是一个基于插件的 Agent 框架,具有以下特性:
|
|
24
|
+
|
|
25
|
+
- **纯 JS 实现** - 无 TypeScript,简单易懂
|
|
26
|
+
- **插件化架构** - 通过插件扩展功能
|
|
27
|
+
- **工具路由** - 智能选择相关工具,减少 token 消耗
|
|
28
|
+
- **多子 Agent 支持** - 支持子 Agent 分工处理
|
|
29
|
+
- **热重载** - 支持动态加载/重载插件
|
|
30
|
+
|
|
31
|
+
### 项目结构
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
vb-agent/
|
|
35
|
+
├── src/ # 核心源码
|
|
36
|
+
│ ├── core/ # 核心模块
|
|
37
|
+
│ │ ├── framework.js # 框架容器
|
|
38
|
+
│ │ ├── agent.js # Agent 类
|
|
39
|
+
│ │ ├── plugin-base.js # 插件基类
|
|
40
|
+
│ │ ├── plugin-manager.js # 插件管理器
|
|
41
|
+
│ │ ├── tool-registry.js # 工具注册表
|
|
42
|
+
│ │ ├── tool-router.js # 工具路由
|
|
43
|
+
│ │ └── agent-chat.js # 聊天处理器
|
|
44
|
+
│ ├── capabilities/ # 能力插件
|
|
45
|
+
│ │ └── skill-manager.js # 技能管理器
|
|
46
|
+
│ ├── executors/ # 执行器
|
|
47
|
+
│ │ └── mcp-executor.js # MCP 执行器
|
|
48
|
+
│ └── utils/ # 工具模块
|
|
49
|
+
├── plugins/ # 内置插件
|
|
50
|
+
├── skills/ # 技能目录
|
|
51
|
+
├── .agent/ # Agent 配置目录
|
|
52
|
+
│ ├── plugins/ # 用户插件目录
|
|
53
|
+
│ ├── skills/ # 用户技能目录
|
|
54
|
+
│ └── mcp_config.json # MCP 配置
|
|
55
|
+
├── examples/ # 示例代码
|
|
56
|
+
└── test-chat.js # 聊天测试
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## 2. 快速开始
|
|
62
|
+
|
|
63
|
+
### 2.1 安装
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
npm install
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 2.2 创建聊天机器人
|
|
70
|
+
|
|
71
|
+
```javascript
|
|
72
|
+
const { Framework } = require('./src')
|
|
73
|
+
|
|
74
|
+
async function main() {
|
|
75
|
+
// 创建框架
|
|
76
|
+
const framework = new Framework({ debug: false })
|
|
77
|
+
|
|
78
|
+
// Bootstrap(加载所有配置和插件)
|
|
79
|
+
await framework.bootstrap({
|
|
80
|
+
agentDir: './.agent',
|
|
81
|
+
aiConfig: {
|
|
82
|
+
provider: 'deepseek',
|
|
83
|
+
model: 'deepseek-chat',
|
|
84
|
+
apiKey: process.env.DEEPSEEK_API_KEY
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
// 创建 Agent
|
|
89
|
+
const agent = framework.createAgent({
|
|
90
|
+
name: 'MyAgent',
|
|
91
|
+
systemPrompt: '你是一个有帮助的助手。'
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
// 对话
|
|
95
|
+
for await (const chunk of agent.chatStream('你好')) {
|
|
96
|
+
if (chunk.type === 'text') {
|
|
97
|
+
process.stdout.write(chunk.text)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
main().catch(console.error)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### 2.3 运行测试聊天
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
node test-chat.js
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## 3. 核心概念
|
|
114
|
+
|
|
115
|
+
### 3.1 Framework(框架)
|
|
116
|
+
|
|
117
|
+
框架是整个系统的容器,管理插件和工具注册。
|
|
118
|
+
|
|
119
|
+
```javascript
|
|
120
|
+
const framework = new Framework({ debug: false })
|
|
121
|
+
|
|
122
|
+
// 加载插件
|
|
123
|
+
await framework.loadPlugin(myPlugin)
|
|
124
|
+
|
|
125
|
+
// 创建 Agent
|
|
126
|
+
const agent = framework.createAgent(config)
|
|
127
|
+
|
|
128
|
+
// 执行工具
|
|
129
|
+
const result = await framework.executeTool('tool_name', { arg: 'value' })
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### 3.2 Agent(智能体)
|
|
133
|
+
|
|
134
|
+
Agent 负责对话和推理,拥有独立的工具集和系统提示。
|
|
135
|
+
|
|
136
|
+
```javascript
|
|
137
|
+
const agent = framework.createAgent({
|
|
138
|
+
name: 'MyAgent', // Agent 名称
|
|
139
|
+
systemPrompt: '你是一个...', // 系统提示
|
|
140
|
+
sharedPrompt: '{{WORK_DIR}}', // 共享提示(支持占位符)
|
|
141
|
+
metadata: { // 元数据
|
|
142
|
+
projectName: 'MyProject'
|
|
143
|
+
},
|
|
144
|
+
enableToolRouting: true // 启用工具路由(默认 true)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
// 发送消息(非流式)
|
|
148
|
+
const result = await agent.chat('你好')
|
|
149
|
+
|
|
150
|
+
// 发送消息(流式)
|
|
151
|
+
for await (const chunk of agent.chatStream('你好')) {
|
|
152
|
+
if (chunk.type === 'text') process.stdout.write(chunk.text)
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### 3.3 Plugin(插件)
|
|
157
|
+
|
|
158
|
+
插件是功能扩展的基本单元。
|
|
159
|
+
|
|
160
|
+
```javascript
|
|
161
|
+
const { Plugin } = require('./src/core/plugin-base')
|
|
162
|
+
|
|
163
|
+
class MyPlugin extends Plugin {
|
|
164
|
+
constructor() {
|
|
165
|
+
super()
|
|
166
|
+
this.name = 'my-plugin'
|
|
167
|
+
this.version = '1.0.0'
|
|
168
|
+
this.description = '我的插件'
|
|
169
|
+
this.priority = 10 // 加载优先级(数字越小越先加载)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
install(framework) {
|
|
173
|
+
this._framework = framework
|
|
174
|
+
// 安装时调用
|
|
175
|
+
return this
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
start(framework) {
|
|
179
|
+
// 注册工具
|
|
180
|
+
framework.registerTool({
|
|
181
|
+
name: 'my_tool',
|
|
182
|
+
description: '我的工具',
|
|
183
|
+
inputSchema: { /* zod schema */ },
|
|
184
|
+
execute: async (args) => {
|
|
185
|
+
return { result: 'done' }
|
|
186
|
+
}
|
|
187
|
+
})
|
|
188
|
+
return this
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
reload(framework) {
|
|
192
|
+
// 热重载时调用
|
|
193
|
+
return this
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
uninstall(framework) {
|
|
197
|
+
// 卸载时调用
|
|
198
|
+
return this
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
module.exports = MyPlugin
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### 3.4 免引入写法
|
|
206
|
+
|
|
207
|
+
支持更简洁的插件写法:
|
|
208
|
+
|
|
209
|
+
```javascript
|
|
210
|
+
// 直接导出函数
|
|
211
|
+
module.exports = function(Plugin) {
|
|
212
|
+
return class extends Plugin {
|
|
213
|
+
constructor() {
|
|
214
|
+
super()
|
|
215
|
+
this.name = 'my-plugin'
|
|
216
|
+
this.description = '我的插件'
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
start(framework) {
|
|
220
|
+
framework.registerTool({
|
|
221
|
+
name: 'hello',
|
|
222
|
+
execute: async (args) => `Hello, ${args.name}!`
|
|
223
|
+
})
|
|
224
|
+
return this
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## 4. 内置插件
|
|
233
|
+
|
|
234
|
+
### 4.1 AI 插件 (ai-plugin)
|
|
235
|
+
|
|
236
|
+
提供 AI 对话能力。
|
|
237
|
+
|
|
238
|
+
```javascript
|
|
239
|
+
await framework.loadPlugin(new AIPlugin({
|
|
240
|
+
provider: 'deepseek', // AI 提供商
|
|
241
|
+
model: 'deepseek-chat', // 模型名称
|
|
242
|
+
apiKey: 'your-api-key', // API Key
|
|
243
|
+
baseURL: 'https://api.deepseek.com' // 可选:自定义 API 地址
|
|
244
|
+
}))
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### 4.2 存储插件 (storage-plugin)
|
|
248
|
+
|
|
249
|
+
键值对存储,支持 JSON 文件持久化。
|
|
250
|
+
|
|
251
|
+
```javascript
|
|
252
|
+
// 注册的工具
|
|
253
|
+
storage_set(key, value) // 存储值
|
|
254
|
+
storage_get(key) // 获取值
|
|
255
|
+
storage_delete(key) // 删除值
|
|
256
|
+
storage_list() // 列出所有键
|
|
257
|
+
storage_clear() // 清空存储
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
### 4.3 工具插件 (tools-plugin)
|
|
261
|
+
|
|
262
|
+
内置工具管理功能。
|
|
263
|
+
|
|
264
|
+
```javascript
|
|
265
|
+
// 注册的工具
|
|
266
|
+
reload_plugins(pluginName?) // 热重载插件
|
|
267
|
+
list_plugins() // 列出已加载插件
|
|
268
|
+
list_tools() // 列出所有工具
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
### 4.4 文件系统插件 (file-system-plugin)
|
|
272
|
+
|
|
273
|
+
文件系统操作。
|
|
274
|
+
|
|
275
|
+
```javascript
|
|
276
|
+
// 注册的工具
|
|
277
|
+
read_directory(path) // 读取目录
|
|
278
|
+
create_directory(path) // 创建目录
|
|
279
|
+
read_file(path) // 读取文件
|
|
280
|
+
write_file(path, content) // 写入文件
|
|
281
|
+
delete_file(path) // 删除文件
|
|
282
|
+
modify_file(path, oldText, newText) // 修改文件
|
|
283
|
+
search_file(pattern, path?) // 搜索文件
|
|
284
|
+
execute_command(cmd) // 执行命令
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
### 4.5 Shell 执行器 (shell-executor-plugin)
|
|
288
|
+
|
|
289
|
+
执行 Shell 命令。
|
|
290
|
+
|
|
291
|
+
```javascript
|
|
292
|
+
// 注册的工具
|
|
293
|
+
shell(command) // 执行 Shell 命令
|
|
294
|
+
powershell(command) // 执行 PowerShell(仅 Windows)
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### 4.6 Python 执行器 (python-executor-plugin)
|
|
298
|
+
|
|
299
|
+
执行 Python 代码。
|
|
300
|
+
|
|
301
|
+
```javascript
|
|
302
|
+
// 注册的工具
|
|
303
|
+
python(code) // 执行 Python 代码
|
|
304
|
+
python_script(scriptPath) // 执行 Python 脚本
|
|
305
|
+
pip_install(package) // 安装 Python 包
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
### 4.7 MCP 执行器 (mcp-executor)
|
|
309
|
+
|
|
310
|
+
连接 MCP 服务器。
|
|
311
|
+
|
|
312
|
+
```json
|
|
313
|
+
// .agent/mcp_config.json
|
|
314
|
+
{
|
|
315
|
+
"mcpServers": {
|
|
316
|
+
"fetch": {
|
|
317
|
+
"command": "uvx",
|
|
318
|
+
"args": ["mcp-server-fetch"]
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
```javascript
|
|
325
|
+
// 注册的工具
|
|
326
|
+
mcp_call(server, tool, args) // 调用 MCP 工具
|
|
327
|
+
mcp_list_servers() // 列出已连接的 MCP 服务器
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
### 4.8 会话插件 (session-plugin)
|
|
331
|
+
|
|
332
|
+
多会话管理。
|
|
333
|
+
|
|
334
|
+
```javascript
|
|
335
|
+
// 注册的工具
|
|
336
|
+
session_create(sessionId?) // 创建会话
|
|
337
|
+
session_get(sessionId) // 获取会话
|
|
338
|
+
session_list() // 列出所有会话
|
|
339
|
+
session_delete(sessionId) // 删除会话
|
|
340
|
+
session_history(sessionId) // 获取会话历史
|
|
341
|
+
session_stats() // 获取会话统计
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### 4.9 审计插件 (audit-plugin)
|
|
345
|
+
|
|
346
|
+
记录操作日志。
|
|
347
|
+
|
|
348
|
+
```javascript
|
|
349
|
+
// 注册的工具
|
|
350
|
+
audit_query(options) // 查询日志
|
|
351
|
+
audit_stats() // 获取统计
|
|
352
|
+
audit_export(format) // 导出日志
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
### 4.10 规则插件 (rules-plugin)
|
|
356
|
+
|
|
357
|
+
规则引擎,控制工具调用权限。
|
|
358
|
+
|
|
359
|
+
```javascript
|
|
360
|
+
// 注册的工具
|
|
361
|
+
rules_list() // 列出规则
|
|
362
|
+
rules_add(rule) // 添加规则
|
|
363
|
+
rules_remove(ruleId) // 移除规则
|
|
364
|
+
rules_test(input) // 测试规则匹配
|
|
365
|
+
rules_stats() // 获取统计
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
### 4.11 调度插件 (scheduler-plugin)
|
|
369
|
+
|
|
370
|
+
定时任务。
|
|
371
|
+
|
|
372
|
+
```javascript
|
|
373
|
+
// 注册的工具
|
|
374
|
+
schedule_cron(expression, task) // 创建 Cron 任务
|
|
375
|
+
schedule_once(datetime, task) // 创建一次性任务
|
|
376
|
+
schedule_list() // 列出所有任务
|
|
377
|
+
schedule_cancel(taskId) // 取消任务
|
|
378
|
+
schedule_now(taskId) // 立即执行任务
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
### 4.12 子 Agent 插件 (subagent-plugin)
|
|
382
|
+
|
|
383
|
+
多 Agent 分工,支持独立工具集。
|
|
384
|
+
|
|
385
|
+
**方式1:配置 `this.agents = []` 自动注册**
|
|
386
|
+
|
|
387
|
+
```javascript
|
|
388
|
+
const { Plugin } = require('../src/core/plugin-base')
|
|
389
|
+
const { tool } = require('ai')
|
|
390
|
+
const { z } = require('zod')
|
|
391
|
+
|
|
392
|
+
class MyPlugin extends Plugin {
|
|
393
|
+
name = 'my-plugin'
|
|
394
|
+
|
|
395
|
+
// 配置式注册子Agent,插件启动时自动创建
|
|
396
|
+
agents = [
|
|
397
|
+
{
|
|
398
|
+
name: 'code-agent',
|
|
399
|
+
role: '代码专家',
|
|
400
|
+
description: '处理代码开发任务',
|
|
401
|
+
tools: {
|
|
402
|
+
compile: tool({
|
|
403
|
+
description: '编译代码',
|
|
404
|
+
parameters: z.object({
|
|
405
|
+
language: z.string(),
|
|
406
|
+
code: z.string()
|
|
407
|
+
}),
|
|
408
|
+
execute: async (args) => ({ success: true })
|
|
409
|
+
})
|
|
410
|
+
},
|
|
411
|
+
parentTools: ['read_file', 'write_file'] // 从父Agent继承的工具
|
|
412
|
+
}
|
|
413
|
+
]
|
|
414
|
+
}
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
**方式2:手动调用 `this.registerSubAgent()`**
|
|
418
|
+
|
|
419
|
+
```javascript
|
|
420
|
+
const { Plugin } = require('../src/core/plugin-base')
|
|
421
|
+
const { tool } = require('ai')
|
|
422
|
+
const { z } = require('zod')
|
|
423
|
+
|
|
424
|
+
class MyPlugin extends Plugin {
|
|
425
|
+
start(framework) {
|
|
426
|
+
// 手动注册子Agent
|
|
427
|
+
this.registerSubAgent({
|
|
428
|
+
name: 'code-agent',
|
|
429
|
+
role: '代码专家',
|
|
430
|
+
tools: {
|
|
431
|
+
compile: tool({
|
|
432
|
+
description: '编译代码',
|
|
433
|
+
parameters: z.object({ language: z.string(), code: z.string() }),
|
|
434
|
+
execute: async (args) => ({ success: true })
|
|
435
|
+
})
|
|
436
|
+
},
|
|
437
|
+
parentTools: ['read_file']
|
|
438
|
+
})
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
**子Agent配置说明:**
|
|
444
|
+
|
|
445
|
+
| 字段 | 类型 | 说明 |
|
|
446
|
+
|------|------|------|
|
|
447
|
+
| `name` | string | 子Agent名称(唯一标识) |
|
|
448
|
+
| `role` | string | 角色描述 |
|
|
449
|
+
| `description` | string | 详细描述(供主Agent智能选择) |
|
|
450
|
+
| `tools` | object | 自定义工具 `{ name: toolDef }`,只属于此子Agent |
|
|
451
|
+
| `parentTools` | array | 从父Agent继承的工具名称列表 |
|
|
452
|
+
|
|
453
|
+
### 4.13 技能管理器 (skill-manager)
|
|
454
|
+
|
|
455
|
+
技能加载和管理。
|
|
456
|
+
|
|
457
|
+
```javascript
|
|
458
|
+
// 注册的工具
|
|
459
|
+
loadSkill(skill) // 加载指定技能
|
|
460
|
+
|
|
461
|
+
// 技能存放在 skills/ 或 .agent/skills/ 目录
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
---
|
|
465
|
+
|
|
466
|
+
## 5. 插件开发
|
|
467
|
+
|
|
468
|
+
### 5.1 插件存放位置
|
|
469
|
+
|
|
470
|
+
| 位置 | 说明 | 加载方式 |
|
|
471
|
+
|------|------|----------|
|
|
472
|
+
| `plugins/` | 内置插件 | 框架初始化时加载 |
|
|
473
|
+
| `.agent/plugins/` | 用户插件 | Bootstrap 时自动加载 |
|
|
474
|
+
|
|
475
|
+
### 5.2 开发步骤
|
|
476
|
+
|
|
477
|
+
1. 创建插件文件 `.agent/plugins/my-plugin.js`
|
|
478
|
+
2. 实现插件类
|
|
479
|
+
3. 使用 `reload_plugins` 重载插件
|
|
480
|
+
|
|
481
|
+
### 5.3 完整示例
|
|
482
|
+
|
|
483
|
+
```javascript
|
|
484
|
+
// .agent/plugins/system-info.js
|
|
485
|
+
const { Plugin } = require('../../src/core/plugin-base')
|
|
486
|
+
const os = require('os')
|
|
487
|
+
const { z } = require('zod')
|
|
488
|
+
|
|
489
|
+
class SystemInfoPlugin extends Plugin {
|
|
490
|
+
constructor() {
|
|
491
|
+
super()
|
|
492
|
+
this.name = 'system-info'
|
|
493
|
+
this.version = '1.0.0'
|
|
494
|
+
this.description = '系统信息查看插件'
|
|
495
|
+
this.priority = 5
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
start(framework) {
|
|
499
|
+
framework.registerTool({
|
|
500
|
+
name: 'get_system_info',
|
|
501
|
+
description: '获取系统基本信息',
|
|
502
|
+
inputSchema: z.object({}),
|
|
503
|
+
execute: async () => {
|
|
504
|
+
return {
|
|
505
|
+
platform: os.platform(),
|
|
506
|
+
hostname: os.hostname(),
|
|
507
|
+
arch: os.arch(),
|
|
508
|
+
homedir: os.homedir(),
|
|
509
|
+
cwd: process.cwd()
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
})
|
|
513
|
+
|
|
514
|
+
framework.registerTool({
|
|
515
|
+
name: 'get_memory_usage',
|
|
516
|
+
description: '获取内存使用情况',
|
|
517
|
+
inputSchema: z.object({}),
|
|
518
|
+
execute: async () => {
|
|
519
|
+
const total = os.totalmem()
|
|
520
|
+
const free = os.freemem()
|
|
521
|
+
const used = total - free
|
|
522
|
+
return {
|
|
523
|
+
total: `${(total / 1024 / 1024 / 1024).toFixed(2)} GB`,
|
|
524
|
+
used: `${(used / 1024 / 1024 / 1024).toFixed(2)} GB`,
|
|
525
|
+
free: `${(free / 1024 / 1024 / 1024).toFixed(2)} GB`,
|
|
526
|
+
percent: `${((used / total) * 100).toFixed(1)}%`
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
})
|
|
530
|
+
|
|
531
|
+
return this
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
module.exports = SystemInfoPlugin
|
|
536
|
+
```
|
|
537
|
+
|
|
538
|
+
### 5.4 免引入写法
|
|
539
|
+
|
|
540
|
+
```javascript
|
|
541
|
+
// .agent/plugins/my-plugin.js
|
|
542
|
+
const { z } = require('zod')
|
|
543
|
+
|
|
544
|
+
module.exports = function(Plugin) {
|
|
545
|
+
return class extends Plugin {
|
|
546
|
+
constructor() {
|
|
547
|
+
super()
|
|
548
|
+
this.name = 'my-plugin'
|
|
549
|
+
this.description = '我的插件'
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
start(framework) {
|
|
553
|
+
framework.registerTool({
|
|
554
|
+
name: 'hello',
|
|
555
|
+
description: '打招呼',
|
|
556
|
+
inputSchema: z.object({
|
|
557
|
+
name: z.string().describe('姓名')
|
|
558
|
+
}),
|
|
559
|
+
execute: async (args) => {
|
|
560
|
+
return `Hello, ${args.name}!`
|
|
561
|
+
}
|
|
562
|
+
})
|
|
563
|
+
return this
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
---
|
|
570
|
+
|
|
571
|
+
## 6. 技能系统
|
|
572
|
+
|
|
573
|
+
### 6.1 技能目录
|
|
574
|
+
|
|
575
|
+
| 位置 | 说明 |
|
|
576
|
+
|------|------|
|
|
577
|
+
| `skills/` | 项目级技能 |
|
|
578
|
+
| `.agent/skills/` | Agent 专属技能 |
|
|
579
|
+
| `~/.agents/skills/` | 全局技能(未来支持) |
|
|
580
|
+
|
|
581
|
+
### 6.2 技能格式
|
|
582
|
+
|
|
583
|
+
技能是 Markdown 文件,包含 frontmatter 元数据。
|
|
584
|
+
|
|
585
|
+
```markdown
|
|
586
|
+
---
|
|
587
|
+
name: my-skill
|
|
588
|
+
description: 我的技能描述
|
|
589
|
+
license: MIT
|
|
590
|
+
compatibility: vb-agent >= 1.0.0
|
|
591
|
+
metadata:
|
|
592
|
+
author: Your Name
|
|
593
|
+
version: 1.0.0
|
|
594
|
+
allowed-tools:
|
|
595
|
+
- read_file
|
|
596
|
+
- write_file
|
|
597
|
+
---
|
|
598
|
+
|
|
599
|
+
# 我的技能
|
|
600
|
+
|
|
601
|
+
这里是技能的详细说明和使用指南。
|
|
602
|
+
```
|
|
603
|
+
|
|
604
|
+
### 6.3 使用技能
|
|
605
|
+
|
|
606
|
+
Agent 可以通过 `loadSkill` 工具加载技能:
|
|
607
|
+
|
|
608
|
+
```
|
|
609
|
+
用户: 帮我创建一个系统信息插件
|
|
610
|
+
Agent: [调用 loadSkill 技能 vb-agent-dev]
|
|
611
|
+
Agent: [获取插件开发指南后,按照指南创建插件]
|
|
612
|
+
```
|
|
613
|
+
|
|
614
|
+
### 6.4 内置技能
|
|
615
|
+
|
|
616
|
+
- **vb-agent-dev** - VB-Agent 插件开发指南
|
|
617
|
+
- **api-patterns** - API 设计模式
|
|
618
|
+
- **app-builder** - 应用程序构建
|
|
619
|
+
- **architecture** - 架构设计
|
|
620
|
+
- ...(共 39+ 技能)
|
|
621
|
+
|
|
622
|
+
---
|
|
623
|
+
|
|
624
|
+
## 7. 子 Agent 系统
|
|
625
|
+
|
|
626
|
+
### 7.1 概念
|
|
627
|
+
|
|
628
|
+
子 Agent 是主 Agent 的专家分身,专门处理特定领域任务。
|
|
629
|
+
|
|
630
|
+
```
|
|
631
|
+
主 Agent
|
|
632
|
+
├── 代码专家 (code-agent)
|
|
633
|
+
│ ├── read_file
|
|
634
|
+
│ ├── write_file
|
|
635
|
+
│ └── execute_command
|
|
636
|
+
├── 数据专家 (data-agent)
|
|
637
|
+
│ ├── python
|
|
638
|
+
│ └── execute_command
|
|
639
|
+
└── ...
|
|
640
|
+
```
|
|
641
|
+
|
|
642
|
+
### 7.2 使用方式
|
|
643
|
+
|
|
644
|
+
```javascript
|
|
645
|
+
// 创建子 Agent
|
|
646
|
+
await framework.loadPlugin(new SubAgentPlugin({
|
|
647
|
+
name: 'code-agent',
|
|
648
|
+
role: '代码专家',
|
|
649
|
+
description: '负责代码开发、调试和重构',
|
|
650
|
+
tools: ['read_file', 'write_file', 'execute_command']
|
|
651
|
+
}))
|
|
652
|
+
|
|
653
|
+
// 主 Agent 自动获得 code-agent 工具
|
|
654
|
+
// 使用: "帮我写一个排序算法"
|
|
655
|
+
// Agent 自动调用 code-agent 工具
|
|
656
|
+
```
|
|
657
|
+
|
|
658
|
+
### 7.3 系统提示中的子 Agent
|
|
659
|
+
|
|
660
|
+
```markdown
|
|
661
|
+
【子 Agent 分配规则 - 必须遵守】
|
|
662
|
+
1. 每个子Agent有固定的专业领域,必须委托给对应的专家:
|
|
663
|
+
- code-agent: 代码专家
|
|
664
|
+
- data-agent: 数据专家
|
|
665
|
+
2. 你需要对任务进行拆分,传递给对应的子Agent执行。
|
|
666
|
+
|
|
667
|
+
【可委托的子 Agent】
|
|
668
|
+
- code-agent: 代码专家
|
|
669
|
+
- data-agent: 数据专家
|
|
670
|
+
|
|
671
|
+
使用相应子Agent名称的工具将任务委托给相应子代理。
|
|
672
|
+
```
|
|
673
|
+
|
|
674
|
+
---
|
|
675
|
+
|
|
676
|
+
## 8. 工具路由
|
|
677
|
+
|
|
678
|
+
### 8.1 问题
|
|
679
|
+
|
|
680
|
+
44 个工具全部传给 LLM 会:
|
|
681
|
+
- 消耗大量 token
|
|
682
|
+
- 降低推理速度
|
|
683
|
+
- 增加成本
|
|
684
|
+
|
|
685
|
+
### 8.2 解决方案
|
|
686
|
+
|
|
687
|
+
智能工具路由:根据用户输入选择相关工具。
|
|
688
|
+
|
|
689
|
+
```javascript
|
|
690
|
+
const agent = framework.createAgent({
|
|
691
|
+
name: 'MyAgent',
|
|
692
|
+
enableToolRouting: true // 默认启用
|
|
693
|
+
})
|
|
694
|
+
```
|
|
695
|
+
|
|
696
|
+
### 8.3 意图分类
|
|
697
|
+
|
|
698
|
+
| 意图 | 关键词 | 工具 |
|
|
699
|
+
|------|--------|------|
|
|
700
|
+
| file_operation | 文件、读取、写入 | read_file, write_file, ... |
|
|
701
|
+
| code_development | 代码、开发、插件 | loadSkill, read_file, ... |
|
|
702
|
+
| system_info | 系统、信息、内存 | get_system_info, ... |
|
|
703
|
+
| plugin_management | 插件、重载 | reload_plugins, list_plugins |
|
|
704
|
+
|
|
705
|
+
### 8.4 工作原理
|
|
706
|
+
|
|
707
|
+
```
|
|
708
|
+
用户: "查看系统信息"
|
|
709
|
+
↓
|
|
710
|
+
意图分析: system_info (匹配度 100%)
|
|
711
|
+
↓
|
|
712
|
+
工具选择: get_system_info, get_memory_usage, get_cpu_info, ...
|
|
713
|
+
↓
|
|
714
|
+
传给 LLM: 仅 7 个工具(而非全部 44 个)
|
|
715
|
+
```
|
|
716
|
+
|
|
717
|
+
---
|
|
718
|
+
|
|
719
|
+
## 9. 配置说明
|
|
720
|
+
|
|
721
|
+
### 9.1 .agent 目录结构
|
|
722
|
+
|
|
723
|
+
```
|
|
724
|
+
.agent/
|
|
725
|
+
├── config # 配置文件(key=value 格式)
|
|
726
|
+
├── ai.json # AI 配置
|
|
727
|
+
├── plugins.json # 插件配置
|
|
728
|
+
├── mcp_config.json # MCP 服务器配置
|
|
729
|
+
├── skills/ # Agent 专属技能
|
|
730
|
+
└── plugins/ # 用户插件
|
|
731
|
+
```
|
|
732
|
+
|
|
733
|
+
### 9.2 AI 配置
|
|
734
|
+
|
|
735
|
+
```json
|
|
736
|
+
// .agent/ai.json
|
|
737
|
+
{
|
|
738
|
+
"provider": "deepseek",
|
|
739
|
+
"model": "deepseek-chat",
|
|
740
|
+
"apiKey": "your-api-key",
|
|
741
|
+
"baseURL": "https://api.deepseek.com"
|
|
742
|
+
}
|
|
743
|
+
```
|
|
744
|
+
|
|
745
|
+
### 9.3 MCP 配置
|
|
746
|
+
|
|
747
|
+
```json
|
|
748
|
+
// .agent/mcp_config.json
|
|
749
|
+
{
|
|
750
|
+
"mcpServers": {
|
|
751
|
+
"fetch": {
|
|
752
|
+
"command": "uvx",
|
|
753
|
+
"args": ["mcp-server-fetch"]
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
```
|
|
758
|
+
|
|
759
|
+
---
|
|
760
|
+
|
|
761
|
+
## 10. 事件系统
|
|
762
|
+
|
|
763
|
+
VB-Agent 基于 EventEmitter,提供完整的事件系统。
|
|
764
|
+
|
|
765
|
+
### 10.1 事件基础
|
|
766
|
+
|
|
767
|
+
```javascript
|
|
768
|
+
// 监听事件
|
|
769
|
+
agent.on('eventName', (data) => {
|
|
770
|
+
console.log('事件触发:', data)
|
|
771
|
+
})
|
|
772
|
+
|
|
773
|
+
// 监听一次
|
|
774
|
+
agent.once('eventName', (data) => {
|
|
775
|
+
console.log('只触发一次')
|
|
776
|
+
})
|
|
777
|
+
|
|
778
|
+
// 移除监听
|
|
779
|
+
agent.off('eventName', handler)
|
|
780
|
+
|
|
781
|
+
// 触发事件
|
|
782
|
+
agent.emit('eventName', { key: 'value' })
|
|
783
|
+
```
|
|
784
|
+
|
|
785
|
+
### 10.2 Framework 事件
|
|
786
|
+
|
|
787
|
+
| 事件名 | 参数 | 说明 |
|
|
788
|
+
|--------|------|------|
|
|
789
|
+
| `framework:ready` | `framework` | 框架就绪 |
|
|
790
|
+
| `framework:destroyed` | - | 框架销毁 |
|
|
791
|
+
| `plugin:registered` | `plugin` | 插件注册 |
|
|
792
|
+
| `plugin:loaded` | `plugin` | 插件加载完成 |
|
|
793
|
+
| `plugin:unloaded` | `plugin` | 插件卸载 |
|
|
794
|
+
| `plugin:reloaded` | `plugin` | 插件重载 |
|
|
795
|
+
| `tool:registered` | `tool` | 工具注册 |
|
|
796
|
+
| `agent:created` | `agent` | Agent 创建 |
|
|
797
|
+
|
|
798
|
+
```javascript
|
|
799
|
+
framework.on('framework:ready', (fw) => {
|
|
800
|
+
console.log('框架就绪')
|
|
801
|
+
})
|
|
802
|
+
|
|
803
|
+
framework.on('plugin:loaded', (plugin) => {
|
|
804
|
+
console.log('插件已加载:', plugin.name)
|
|
805
|
+
})
|
|
806
|
+
|
|
807
|
+
framework.on('tool:registered', (tool) => {
|
|
808
|
+
console.log('工具已注册:', tool.name)
|
|
809
|
+
})
|
|
810
|
+
```
|
|
811
|
+
|
|
812
|
+
### 10.3 Agent 事件
|
|
813
|
+
|
|
814
|
+
| 事件名 | 参数 | 说明 |
|
|
815
|
+
|--------|------|------|
|
|
816
|
+
| `status` | `{ status }` | 状态变化:`idle`/`busy`/`error` |
|
|
817
|
+
| `message` | `msg` | 非流式消息响应 |
|
|
818
|
+
| `chunk` | `chunk` | 流式响应片段 |
|
|
819
|
+
| `tool-call` | `{ name, args }` | 工具调用 |
|
|
820
|
+
| `tool-result` | `{ name, args, result }` | 工具结果 |
|
|
821
|
+
| `error` | `{ error }` | 错误发生 |
|
|
822
|
+
| `destroyed` | - | Agent 销毁 |
|
|
823
|
+
|
|
824
|
+
```javascript
|
|
825
|
+
// 状态监听
|
|
826
|
+
agent.on('status', ({ status }) => {
|
|
827
|
+
console.log('Agent 状态:', status)
|
|
828
|
+
})
|
|
829
|
+
|
|
830
|
+
// 流式输出
|
|
831
|
+
agent.on('chunk', (chunk) => {
|
|
832
|
+
if (chunk.type === 'text') {
|
|
833
|
+
process.stdout.write(chunk.text)
|
|
834
|
+
}
|
|
835
|
+
})
|
|
836
|
+
|
|
837
|
+
// 工具调用
|
|
838
|
+
agent.on('tool-call', ({ name, args }) => {
|
|
839
|
+
console.log('调用工具:', name, args)
|
|
840
|
+
})
|
|
841
|
+
|
|
842
|
+
agent.on('tool-result', ({ name, result }) => {
|
|
843
|
+
console.log('工具结果:', name, result)
|
|
844
|
+
})
|
|
845
|
+
|
|
846
|
+
// 错误处理
|
|
847
|
+
agent.on('error', ({ error }) => {
|
|
848
|
+
console.error('Agent 错误:', error)
|
|
849
|
+
})
|
|
850
|
+
```
|
|
851
|
+
|
|
852
|
+
### 10.4 子 Agent 事件
|
|
853
|
+
|
|
854
|
+
| 事件名 | 参数 | 说明 |
|
|
855
|
+
|--------|------|------|
|
|
856
|
+
| `subagent:chat:start` | `{ parentAgent, subAgent, subAgentName, task }` | 子 Agent 开始处理 |
|
|
857
|
+
| `subagent:chat:end` | `{ parentAgent, subAgent, subAgentName, result }` | 子 Agent 完成 |
|
|
858
|
+
| `subagent:chunk` | `{ parentAgent, subAgent, subAgentName, chunk }` | 子 Agent 流式输出 |
|
|
859
|
+
| `subagent:error` | `{ parentAgent, subAgent, subAgentName, error }` | 子 Agent 错误 |
|
|
860
|
+
|
|
861
|
+
```javascript
|
|
862
|
+
// 监听所有子 Agent 事件
|
|
863
|
+
agent.on('subagent:chat:start', ({ subAgentName, task }) => {
|
|
864
|
+
console.log(`子 Agent ${subAgentName} 开始处理:`, task)
|
|
865
|
+
})
|
|
866
|
+
|
|
867
|
+
agent.on('subagent:chat:end', ({ subAgentName, result }) => {
|
|
868
|
+
console.log(`子 Agent ${subAgentName} 完成:`, result)
|
|
869
|
+
})
|
|
870
|
+
|
|
871
|
+
agent.on('subagent:chunk', ({ subAgentName, chunk }) => {
|
|
872
|
+
console.log(`子 Agent ${subAgentName} 输出:`, chunk)
|
|
873
|
+
})
|
|
874
|
+
|
|
875
|
+
agent.on('subagent:error', ({ subAgentName, error }) => {
|
|
876
|
+
console.error(`子 Agent ${subAgentName} 错误:`, error)
|
|
877
|
+
})
|
|
878
|
+
```
|
|
879
|
+
|
|
880
|
+
### 10.5 工具执行事件
|
|
881
|
+
|
|
882
|
+
| 事件名 | 参数 | 说明 |
|
|
883
|
+
|--------|------|------|
|
|
884
|
+
| `tool:call` | `{ name, args }` | 工具被调用 |
|
|
885
|
+
| `tool:result` | `{ name, args, result }` | 工具执行结果 |
|
|
886
|
+
| `tool:error` | `{ name, args, error }` | 工具执行错误 |
|
|
887
|
+
|
|
888
|
+
```javascript
|
|
889
|
+
// 通过 toolRegistry 监听
|
|
890
|
+
framework.toolRegistry.on('tool:call', ({ name, args }) => {
|
|
891
|
+
console.log('工具调用:', name, args)
|
|
892
|
+
})
|
|
893
|
+
|
|
894
|
+
framework.toolRegistry.on('tool:result', ({ name, result }) => {
|
|
895
|
+
console.log('工具结果:', name, result)
|
|
896
|
+
})
|
|
897
|
+
|
|
898
|
+
framework.toolRegistry.on('tool:error', ({ name, error }) => {
|
|
899
|
+
console.error('工具错误:', name, error)
|
|
900
|
+
})
|
|
901
|
+
```
|
|
902
|
+
|
|
903
|
+
### 10.6 完整示例
|
|
904
|
+
|
|
905
|
+
```javascript
|
|
906
|
+
const { Framework } = require('./src')
|
|
907
|
+
|
|
908
|
+
async function main() {
|
|
909
|
+
const framework = new Framework({ debug: false })
|
|
910
|
+
await framework.bootstrap({ agentDir: './.agent' })
|
|
911
|
+
|
|
912
|
+
const agent = framework.createAgent({
|
|
913
|
+
name: 'MyAgent',
|
|
914
|
+
systemPrompt: '你是一个有帮助的助手。'
|
|
915
|
+
})
|
|
916
|
+
|
|
917
|
+
// 监听所有事件
|
|
918
|
+
framework.on('*', (event, ...args) => {
|
|
919
|
+
console.log(`[Framework Event] ${event}`)
|
|
920
|
+
})
|
|
921
|
+
|
|
922
|
+
agent.on('status', ({ status }) => {
|
|
923
|
+
console.log(`[Agent Status] ${status}`)
|
|
924
|
+
})
|
|
925
|
+
|
|
926
|
+
agent.on('tool-call', ({ name, args }) => {
|
|
927
|
+
console.log(`[Tool Call] ${name}:`, args)
|
|
928
|
+
})
|
|
929
|
+
|
|
930
|
+
agent.on('subagent:*', (event, data) => {
|
|
931
|
+
console.log(`[SubAgent Event] ${event}:`, data)
|
|
932
|
+
})
|
|
933
|
+
|
|
934
|
+
// 对话
|
|
935
|
+
for await (const chunk of agent.chatStream('你好')) {
|
|
936
|
+
if (chunk.type === 'text') {
|
|
937
|
+
process.stdout.write(chunk.text)
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
main()
|
|
943
|
+
```
|
|
944
|
+
|
|
945
|
+
### 10.7 Chat Stream Chunk 类型
|
|
946
|
+
|
|
947
|
+
`chatStream` 返回的 chunk 对象有以下类型:
|
|
948
|
+
|
|
949
|
+
| type | 字段 | 说明 |
|
|
950
|
+
|------|------|------|
|
|
951
|
+
| `text` | `text` | 文本片段 |
|
|
952
|
+
| `tool-call` | `toolName`, `args` | 工具调用 |
|
|
953
|
+
| `tool-result` | `toolName`, `result` | 工具结果 |
|
|
954
|
+
| `error` | `error` | 错误信息 |
|
|
955
|
+
|
|
956
|
+
```javascript
|
|
957
|
+
for await (const chunk of agent.chatStream('帮我读取文件')) {
|
|
958
|
+
switch (chunk.type) {
|
|
959
|
+
case 'text':
|
|
960
|
+
process.stdout.write(chunk.text)
|
|
961
|
+
break
|
|
962
|
+
case 'tool-call':
|
|
963
|
+
console.log('调用:', chunk.toolName, chunk.args)
|
|
964
|
+
break
|
|
965
|
+
case 'tool-result':
|
|
966
|
+
console.log('结果:', chunk.toolName, chunk.result)
|
|
967
|
+
break
|
|
968
|
+
case 'error':
|
|
969
|
+
console.error('错误:', chunk.error)
|
|
970
|
+
break
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
```
|
|
974
|
+
|
|
975
|
+
---
|
|
976
|
+
|
|
977
|
+
## 11. API 参考
|
|
978
|
+
|
|
979
|
+
### 11.1 Framework
|
|
980
|
+
|
|
981
|
+
```javascript
|
|
982
|
+
// 创建框架
|
|
983
|
+
const framework = new Framework({ debug: false })
|
|
984
|
+
|
|
985
|
+
// Bootstrap(加载所有配置和插件)
|
|
986
|
+
await framework.bootstrap({
|
|
987
|
+
agentDir: './.agent',
|
|
988
|
+
aiConfig: { provider, model, apiKey }
|
|
989
|
+
})
|
|
990
|
+
|
|
991
|
+
// 创建 Agent
|
|
992
|
+
const agent = framework.createAgent({
|
|
993
|
+
name, systemPrompt, sharedPrompt, metadata, enableToolRouting
|
|
994
|
+
})
|
|
995
|
+
|
|
996
|
+
// 注册/加载插件
|
|
997
|
+
await framework.loadPlugin(plugin)
|
|
998
|
+
await framework.reloadPlugin(name)
|
|
999
|
+
await framework.reloadAllPlugins()
|
|
1000
|
+
|
|
1001
|
+
// 执行工具
|
|
1002
|
+
const result = await framework.executeTool(name, args)
|
|
1003
|
+
|
|
1004
|
+
// 获取插件/工具
|
|
1005
|
+
const plugin = framework.pluginManager.get(name)
|
|
1006
|
+
const tools = framework.getTools()
|
|
1007
|
+
|
|
1008
|
+
// 销毁
|
|
1009
|
+
await framework.destroy()
|
|
1010
|
+
```
|
|
1011
|
+
|
|
1012
|
+
### 11.2 Agent
|
|
1013
|
+
|
|
1014
|
+
```javascript
|
|
1015
|
+
// 创建(在 framework.createAgent() 后)
|
|
1016
|
+
const agent = framework.createAgent(config)
|
|
1017
|
+
|
|
1018
|
+
// 发送消息
|
|
1019
|
+
const result = await agent.chat(message)
|
|
1020
|
+
for await (const chunk of agent.chatStream(message)) {
|
|
1021
|
+
// chunk.type: 'text' | 'tool-call' | 'tool-result' | 'error'
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
// 注册工具
|
|
1025
|
+
agent.registerTool(toolDef)
|
|
1026
|
+
|
|
1027
|
+
// 子 Agent
|
|
1028
|
+
agent.registerSubAgent(name, agent, role, goal)
|
|
1029
|
+
agent.getSubAgents()
|
|
1030
|
+
|
|
1031
|
+
// 元数据
|
|
1032
|
+
agent.setMetadata(key, value)
|
|
1033
|
+
agent.getMetadata(key)
|
|
1034
|
+
|
|
1035
|
+
// 状态
|
|
1036
|
+
agent.getStatus() // 'idle' | 'busy' | 'error'
|
|
1037
|
+
agent.resetStatus() // 重置状态
|
|
1038
|
+
|
|
1039
|
+
// 清空历史
|
|
1040
|
+
agent.clearHistory()
|
|
1041
|
+
|
|
1042
|
+
// 销毁
|
|
1043
|
+
agent.destroy()
|
|
1044
|
+
```
|
|
1045
|
+
|
|
1046
|
+
### 11.3 Plugin
|
|
1047
|
+
|
|
1048
|
+
```javascript
|
|
1049
|
+
class MyPlugin extends Plugin {
|
|
1050
|
+
constructor() {
|
|
1051
|
+
super()
|
|
1052
|
+
this.name = 'my-plugin'
|
|
1053
|
+
this.version = '1.0.0'
|
|
1054
|
+
this.description = '我的插件'
|
|
1055
|
+
this.priority = 10
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
install(framework) { /* 安装 */ return this }
|
|
1059
|
+
start(framework) { /* 启动 */ return this }
|
|
1060
|
+
reload(framework) { /* 重载 */ return this }
|
|
1061
|
+
uninstall(framework) { /* 卸载 */ return this }
|
|
1062
|
+
}
|
|
1063
|
+
```
|
|
1064
|
+
|
|
1065
|
+
### 11.4 工具定义
|
|
1066
|
+
|
|
1067
|
+
```javascript
|
|
1068
|
+
{
|
|
1069
|
+
name: 'tool_name',
|
|
1070
|
+
description: '工具描述',
|
|
1071
|
+
inputSchema: z.object({
|
|
1072
|
+
arg1: z.string().describe('参数1'),
|
|
1073
|
+
arg2: z.number().optional()
|
|
1074
|
+
}),
|
|
1075
|
+
execute: async (args, framework) => {
|
|
1076
|
+
// args: { arg1: '...', arg2: 123 }
|
|
1077
|
+
// framework: Framework 实例
|
|
1078
|
+
return { success: true, result: '...' }
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
```
|
|
1082
|
+
|
|
1083
|
+
---
|
|
1084
|
+
|
|
1085
|
+
## 示例代码
|
|
1086
|
+
|
|
1087
|
+
### 基础对话
|
|
1088
|
+
|
|
1089
|
+
```javascript
|
|
1090
|
+
const { Framework } = require('./src')
|
|
1091
|
+
|
|
1092
|
+
async function main() {
|
|
1093
|
+
const framework = new Framework()
|
|
1094
|
+
await framework.bootstrap({ agentDir: './.agent' })
|
|
1095
|
+
|
|
1096
|
+
const agent = framework.createAgent({
|
|
1097
|
+
systemPrompt: '你是一个有帮助的助手。'
|
|
1098
|
+
})
|
|
1099
|
+
|
|
1100
|
+
const result = await agent.chat('你好')
|
|
1101
|
+
console.log(result.message)
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
main()
|
|
1105
|
+
```
|
|
1106
|
+
|
|
1107
|
+
### 带工具的对话
|
|
1108
|
+
|
|
1109
|
+
```javascript
|
|
1110
|
+
const { Framework } = require('./src')
|
|
1111
|
+
|
|
1112
|
+
async function main() {
|
|
1113
|
+
const framework = new Framework()
|
|
1114
|
+
await framework.bootstrap({ agentDir: './.agent' })
|
|
1115
|
+
|
|
1116
|
+
const agent = framework.createAgent({
|
|
1117
|
+
systemPrompt: '你是一个有帮助的助手,擅长文件操作。'
|
|
1118
|
+
})
|
|
1119
|
+
|
|
1120
|
+
for await (const chunk of agent.chatStream('帮我读取 package.json')) {
|
|
1121
|
+
if (chunk.type === 'text') process.stdout.write(chunk.text)
|
|
1122
|
+
if (chunk.type === 'tool-call') console.log('调用工具:', chunk.toolName)
|
|
1123
|
+
if (chunk.type === 'tool-result') console.log('结果:', chunk.result)
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
main()
|
|
1128
|
+
```
|
|
1129
|
+
|
|
1130
|
+
### 子 Agent 分工
|
|
1131
|
+
|
|
1132
|
+
```javascript
|
|
1133
|
+
const { Framework } = require('./src')
|
|
1134
|
+
const { SubAgentPlugin } = require('./plugins/subagent-plugin')
|
|
1135
|
+
|
|
1136
|
+
async function main() {
|
|
1137
|
+
const framework = new Framework()
|
|
1138
|
+
await framework.bootstrap({ agentDir: './.agent' })
|
|
1139
|
+
|
|
1140
|
+
// 创建主 Agent
|
|
1141
|
+
const mainAgent = framework.createAgent({
|
|
1142
|
+
systemPrompt: '你是主 Agent,负责协调子 Agent 工作。'
|
|
1143
|
+
})
|
|
1144
|
+
|
|
1145
|
+
// 添加子 Agent
|
|
1146
|
+
await framework.loadPlugin(new SubAgentPlugin({
|
|
1147
|
+
name: 'code-agent',
|
|
1148
|
+
role: '代码专家',
|
|
1149
|
+
description: '负责代码开发',
|
|
1150
|
+
tools: {
|
|
1151
|
+
compile: {
|
|
1152
|
+
description: '编译代码',
|
|
1153
|
+
inputSchema: z.object({ language: z.string(), code: z.string() }),
|
|
1154
|
+
execute: async (args) => ({ success: true })
|
|
1155
|
+
}
|
|
1156
|
+
},
|
|
1157
|
+
parentTools: ['read_file', 'write_file']
|
|
1158
|
+
}))
|
|
1159
|
+
|
|
1160
|
+
// 对话
|
|
1161
|
+
const result = await mainAgent.chat('帮我创建一个 Hello World 程序')
|
|
1162
|
+
console.log(result.message)
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
main()
|
|
1166
|
+
```
|
|
1167
|
+
|
|
1168
|
+
---
|
|
1169
|
+
|
|
1170
|
+
## 常见问题
|
|
1171
|
+
|
|
1172
|
+
### Q: 如何调试插件?
|
|
1173
|
+
|
|
1174
|
+
```javascript
|
|
1175
|
+
const framework = new Framework({ debug: true })
|
|
1176
|
+
```
|
|
1177
|
+
|
|
1178
|
+
### Q: MCP 连接失败怎么办?
|
|
1179
|
+
|
|
1180
|
+
1. 检查 `.agent/mcp_config.json` 配置
|
|
1181
|
+
2. 确认 MCP 服务器命令已安装
|
|
1182
|
+
3. 查看日志中的错误信息
|
|
1183
|
+
|
|
1184
|
+
### Q: 如何查看所有可用工具?
|
|
1185
|
+
|
|
1186
|
+
调用 `list_tools` 工具,或在系统提示中查看【可用工具】部分。
|
|
1187
|
+
|
|
1188
|
+
---
|
|
1189
|
+
|
|
1190
|
+
## 更新日志
|
|
1191
|
+
|
|
1192
|
+
### v1.0.0
|
|
1193
|
+
|
|
1194
|
+
- 插件化架构
|
|
1195
|
+
- 工具注册系统
|
|
1196
|
+
- 技能管理
|
|
1197
|
+
- 子 Agent 支持
|
|
1198
|
+
- 工具路由
|
|
1199
|
+
- 热重载
|
|
1200
|
+
|
|
1201
|
+
---
|
|
1202
|
+
|
|
1203
|
+
## License
|
|
1204
|
+
|
|
1205
|
+
MIT
|