foliko 1.1.6 → 1.1.7
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/.agent/data/email/processed-emails.json +1 -0
- package/.agent/data/plugins-state.json +5 -1
- package/.agent/data/web/web-config.json +5 -0
- package/.agent/memory/feedback/mnt7jrlt-d67qs7.md +15 -0
- package/.agent/memory/feedback/mnt88ja3-al4fuy.md +9 -0
- package/.agent/plugins/test-plugin.py +108 -0
- package/.agent/sessions/cli_default.json +514 -5298
- package/.claude/settings.local.json +2 -1
- package/SPEC.md +735 -696
- package/output/zen_silence.png +0 -0
- package/package.json +2 -2
- package/plugins/ambient-agent/EventWatcher.js +33 -37
- package/plugins/ambient-agent/ExplorerLoop.js +338 -36
- package/plugins/ambient-agent/GoalManager.js +7 -3
- package/plugins/ambient-agent/StateStore.js +30 -1
- package/plugins/ambient-agent/constants.js +15 -1
- package/plugins/ambient-agent/index.js +26 -33
- package/plugins/coordinator-plugin.js +3 -3
- package/plugins/default-plugins.js +2 -2
- package/plugins/email/index.js +150 -36
- package/plugins/email/monitor.js +79 -5
- package/plugins/email/reply.js +15 -25
- package/plugins/extension-executor-plugin.js +160 -31
- package/plugins/file-system-plugin.js +57 -24
- package/plugins/memory-plugin.js +176 -64
- package/plugins/python-plugin-loader.js +79 -9
- package/plugins/scheduler-plugin.js +64 -24
- package/plugins/think-plugin.js +7 -2
- package/plugins/web-plugin.js +263 -4
- package/skills/ambient-agent/SKILL.md +342 -314
- package/src/core/agent-chat.js +64 -9
- package/src/core/agent.js +118 -59
- package/src/core/tool-registry.js +5 -5
- package/src/executors/mcp-executor.js +188 -26
- package/src/utils/id.js +5 -0
- package/system.md +3480 -0
- package/.agent/data/ambient/goals.json +0 -50
- package/.agent/data/ambient/memories.json +0 -7
- package/.agent/memory/core.md +0 -1
- package/.agent/memory/feedback/mnrsiuoc-e1ru74.md +0 -9
- package/.agent/memory/feedback/mnrt2mmz-98az6n.md +0 -9
- package/.agent/memory/feedback/mnrtqrhm-kxsicz.md +0 -9
- package/.agent/memory/feedback/mnrts8vg-i0ngzp.md +0 -15
- package/.agent/memory/feedback/mnrtt7jt-c0trb2.md +0 -9
- package/.agent/memory/feedback/mnruc2f0-5s52la.md +0 -16
- package/.agent/memory/feedback/mnrumbmx-63sa0v.md +0 -9
- package/.agent/memory/project/mnn93ogy-ypjn27.md +0 -9
- package/.agent/memory/project/mnn98fqy-5nhc1u.md +0 -25
- package/.agent/memory/project/mnrp7p5n-8enm2a.md +0 -31
- package/.agent/memory/project/mnrp9ifb-yynks0.md +0 -40
- package/.agent/memory/project/mnrpb3b8-f617s4.md +0 -25
- package/.agent/memory/project/mnrrmqgg-focprv.md +0 -9
- package/.agent/memory/project/mnrtykbh-6atsor.md +0 -9
- package/.agent/memory/project/mnru9jiu-kgau16.md +0 -35
- package/.agent/memory/reference/mnq3oenw-46haj6.md +0 -63
- package/.agent/memory/reference/mnq5qxm2-mjoooh.md +0 -116
- package/.agent/memory/reference/mnrnvpwo-rcqv9m.md +0 -52
- package/.agent/memory/reference/mnrovxvz-zy9xqm.md +0 -25
- package/.agent/memory/reference/mnroxabj-1b3930.md +0 -68
- package/.agent/memory/reference/mnrpjtlp-mnb9od.md +0 -35
- package/.agent/memory/reference/mnrps1x3-6b8xfm.md +0 -28
- package/.agent/memory/reference/mnrpt9ov-15er5w.md +0 -22
- package/.agent/memory/reference/mnrq82dn-y9tv9e.md +0 -50
- package/.agent/memory/reference/mnrqnr5v-v75drf.md +0 -34
- package/.agent/memory/reference/mnrrfzys-urudaf.md +0 -31
- package/.agent/memory/reference/mnrrocha-t0027n.md +0 -21
- package/.agent/memory/reference/mnrukklc-bxndsb.md +0 -35
- package/.agent/memory/user/mnm67t9m-x8rekk.md +0 -9
- package/.agent/memory/user/mnn5mmqh-w6aktx.md +0 -11
- package/.agent/memory/user/mnnbfhhn-dk1bd1.md +0 -22
- package/.agent/memory/user/mnrt39t8-8eosy0.md +0 -9
- package/foliko-cloud-rising.png +0 -0
- package/foliko-dawn-of-ai.png +0 -0
- package/foliko-mindful-observation.png +0 -0
- package/foliko-stellar-dreams.png +0 -0
- package/foliko-zen-jing.png +0 -0
- package/foliko-zen-kong.png +0 -0
- package/foliko-zen-wu.png +0 -0
- package/zen_karesansui.png +0 -0
package/SPEC.md
CHANGED
|
@@ -1,696 +1,735 @@
|
|
|
1
|
-
# Foliko Framework 规划文档
|
|
2
|
-
|
|
3
|
-
## 一、设计目标
|
|
4
|
-
|
|
5
|
-
1. **纯 JS 实现** - 无 TypeScript,简明易懂
|
|
6
|
-
2. **架构简约** - 核心仅包含必要组件,通过插件扩展功能
|
|
7
|
-
3. **插件系统与 Agent 分离** - Agent 专注于对话推理,插件提供扩展能力
|
|
8
|
-
4. **手动热重载** - 不监测文件变化,通过 API 手动调用重载
|
|
9
|
-
|
|
10
|
-
## 二、核心架构
|
|
11
|
-
|
|
12
|
-
```
|
|
13
|
-
┌─────────────────────────────────────────────────┐
|
|
14
|
-
│ Foliko Framework │
|
|
15
|
-
├─────────────────────────────────────────────────┤
|
|
16
|
-
│ Framework (容器层) │
|
|
17
|
-
│ ├── pluginManager - 插件加载/卸载/重载 │
|
|
18
|
-
│ ├── toolRegistry - 工具注册表 │
|
|
19
|
-
│ ├── skillManager - Skill管理 (可选) │
|
|
20
|
-
│ └── eventEmitter - 事件总线 │
|
|
21
|
-
├─────────────────────────────────────────────────┤
|
|
22
|
-
│ Agent (对话层) │
|
|
23
|
-
│ ├── chat() - 对话接口 │
|
|
24
|
-
│ ├── tools - 从 Framework 获取 │
|
|
25
|
-
│ └── events - 事件监听 │
|
|
26
|
-
└─────────────────────────────────────────────────┘
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
### 2.1 Framework 类
|
|
30
|
-
|
|
31
|
-
```javascript
|
|
32
|
-
class Framework {
|
|
33
|
-
constructor(config = {})
|
|
34
|
-
registerPlugin(plugin) // 注册插件
|
|
35
|
-
loadPlugin(pluginDef) // 加载插件
|
|
36
|
-
unloadPlugin(name) // 卸载插件
|
|
37
|
-
reloadPlugin(name) // 重载单个插件
|
|
38
|
-
reloadAllPlugins() // 重载所有插件
|
|
39
|
-
registerTool(tool) // 注册工具
|
|
40
|
-
getTools() // 获取所有工具
|
|
41
|
-
on(event, handler) // 监听事件
|
|
42
|
-
emit(event, data) // 触发事件
|
|
43
|
-
ready() // 等待初始化完成
|
|
44
|
-
}
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
### 2.2 Agent 类
|
|
48
|
-
|
|
49
|
-
```javascript
|
|
50
|
-
// 创建 Agent
|
|
51
|
-
const agent = framework.createAgent({
|
|
52
|
-
name: 'MyAgent', // Agent 名称
|
|
53
|
-
systemPrompt: '你是一个助手', // 系统提示
|
|
54
|
-
|
|
55
|
-
// 共享提示模板 (支持 {{VAR}} 占位符)
|
|
56
|
-
sharedPrompt: `工作目录: {{WORK_DIR}}`,
|
|
57
|
-
|
|
58
|
-
// 元数据 (供 sharedPrompt 和自身使用)
|
|
59
|
-
metadata: {
|
|
60
|
-
projectName: 'MyProject',
|
|
61
|
-
version: '1.0.0',
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
// 运行时元数据操作
|
|
66
|
-
agent.setMetadata('task', '数据分析'); // 设置单个
|
|
67
|
-
agent.setMetadata({ key: 'value' }); // 批量设置
|
|
68
|
-
agent.getMetadata('task'); // 获取
|
|
69
|
-
agent.deleteMetadata('task'); // 删除
|
|
70
|
-
agent.clearMetadata(); // 清空
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
**内置占位符:**
|
|
74
|
-
| 占位符 | 说明 |
|
|
75
|
-
|--------|------|
|
|
76
|
-
| `{{WORK_DIR}}` | 工作目录 |
|
|
77
|
-
| `{{HOME_DIR}}` | 主目录 |
|
|
78
|
-
| `{{HOST_NAME}}` | 主机名 |
|
|
79
|
-
| `{{PLATFORM}}` | 平台 |
|
|
80
|
-
| `{{TIME}}` | 当前时间 |
|
|
81
|
-
| `{{DATE}}` | 当前日期 |
|
|
82
|
-
|
|
83
|
-
**Agent 方法:**
|
|
84
|
-
|
|
85
|
-
```javascript
|
|
86
|
-
class Agent {
|
|
87
|
-
constructor(framework, config) // 创建 Agent
|
|
88
|
-
chat(message, options) // 发送消息
|
|
89
|
-
chatStream(message, options) // 流式发送消息
|
|
90
|
-
setSystemPrompt(prompt) // 设置系统提示
|
|
91
|
-
registerTool(tool) // 注册工具
|
|
92
|
-
getTools() // 获取工具列表
|
|
93
|
-
clearHistory() // 清空对话历史
|
|
94
|
-
getStatus() // 获取状态
|
|
95
|
-
destroy() // 销毁 Agent
|
|
96
|
-
|
|
97
|
-
// 元数据管理
|
|
98
|
-
setMetadata(key, value) // 设置元数据
|
|
99
|
-
getMetadata(key) // 获取元数据
|
|
100
|
-
deleteMetadata(key) // 删除元数据
|
|
101
|
-
clearMetadata() // 清空元数据
|
|
102
|
-
}
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
**生成的 System Prompt 示例:**
|
|
106
|
-
|
|
107
|
-
```
|
|
108
|
-
你是一个助手。
|
|
109
|
-
|
|
110
|
-
工作目录: D:\project
|
|
111
|
-
|
|
112
|
-
【元数据】
|
|
113
|
-
- projectName: MyProject
|
|
114
|
-
- version: 1.0.0
|
|
115
|
-
|
|
116
|
-
【可用工具】
|
|
117
|
-
- shell: 执行命令
|
|
118
|
-
- python: 执行Python代码
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
### 2.3 插件基类
|
|
122
|
-
|
|
123
|
-
```javascript
|
|
124
|
-
class Plugin {
|
|
125
|
-
name; // 插件名称 (必填)
|
|
126
|
-
version = '1.0.0';
|
|
127
|
-
description; // 插件描述
|
|
128
|
-
|
|
129
|
-
install(framework) {} // 安装时调用
|
|
130
|
-
start(framework) {} // 启动时调用
|
|
131
|
-
reload(framework) {} // 热重载时调用
|
|
132
|
-
uninstall(framework) {} // 卸载时调用
|
|
133
|
-
}
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
## 三、目录结构
|
|
137
|
-
|
|
138
|
-
```
|
|
139
|
-
D:\code\foliko\
|
|
140
|
-
├── src/
|
|
141
|
-
│ ├── core/
|
|
142
|
-
│ │ ├── framework.js # 核心容器
|
|
143
|
-
│ │ ├── agent.js # Agent类
|
|
144
|
-
│ │ ├── agent-chat.js # 聊天处理器
|
|
145
|
-
│ │ ├── plugin-manager.js# 插件管理器
|
|
146
|
-
│ │ ├── plugin-base.js # 插件基类
|
|
147
|
-
│ │ ├── tool-registry.js # 工具注册表
|
|
148
|
-
│ │ ├── provider.js # AI Provider 工厂
|
|
149
|
-
│ │ └── index.js # 统一导出
|
|
150
|
-
│ ├── capabilities/
|
|
151
|
-
│ │ ├── skill-manager.js # Skill 管理器
|
|
152
|
-
│ │ └── workflow-engine.js # 工作流引擎
|
|
153
|
-
│ ├── executors/
|
|
154
|
-
│ │ ├── executor-base.js # 执行器基类
|
|
155
|
-
│ │ └── mcp-executor.js # MCP 执行器
|
|
156
|
-
│ ├── utils/
|
|
157
|
-
│ │ └── event-emitter.js # 事件发射器
|
|
158
|
-
│ └── index.js
|
|
159
|
-
├── plugins/ # 内置插件
|
|
160
|
-
│ ├── ai-plugin.js # AI对话插件
|
|
161
|
-
│ ├── tools-plugin.js # 内置工具插件
|
|
162
|
-
│ ├── default-plugins.js # 默认插件加载器
|
|
163
|
-
│ ├── shell-executor-plugin.js # Shell执行器插件
|
|
164
|
-
│ ├── python-executor-plugin.js # Python执行器插件
|
|
165
|
-
│ ├── session-plugin.js # Session会话管理插件
|
|
166
|
-
│ ├── audit-plugin.js # Audit审计日志插件
|
|
167
|
-
│ ├── rules-plugin.js # Rules规则引擎插件
|
|
168
|
-
│ ├── scheduler-plugin.js # Scheduler定时任务插件
|
|
169
|
-
│ ├── storage-plugin.js # Storage存储插件
|
|
170
|
-
│ ├── subagent-plugin.js # SubAgent子代理插件
|
|
171
|
-
│ ├── email.js # Email邮件插件
|
|
172
|
-
│ └── telegram-plugin.js # Telegram对话插件
|
|
173
|
-
├── examples/
|
|
174
|
-
│ ├── basic.js # 基础示例
|
|
175
|
-
│ ├── bootstrap.js # Bootstrap示例
|
|
176
|
-
│ └── workflow.js # 工作流示例
|
|
177
|
-
├── .agent/ # Agent 配置目录
|
|
178
|
-
│ ├── config # 配置文件
|
|
179
|
-
│ ├── ai.json # AI 配置
|
|
180
|
-
│ ├── plugins.json # 插件列表
|
|
181
|
-
│ ├── mcp_config.json # MCP 服务器配置
|
|
182
|
-
│ └── skills/ # Skill 目录
|
|
183
|
-
├── package.json
|
|
184
|
-
└── SPEC.md
|
|
185
|
-
```
|
|
186
|
-
|
|
187
|
-
## 四、核心模块
|
|
188
|
-
|
|
189
|
-
### 4.5 插件配置持久化
|
|
190
|
-
|
|
191
|
-
插件状态和配置自动保存到 `.agent/data/plugins-state.json`
|
|
192
|
-
|
|
193
|
-
```javascript
|
|
194
|
-
// 更新插件配置
|
|
195
|
-
framework.updatePluginConfig('telegram', { allowedChats: ['123'] });
|
|
196
|
-
|
|
197
|
-
// 启用/禁用插件
|
|
198
|
-
framework.enablePlugin('telegram');
|
|
199
|
-
framework.disablePlugin('telegram');
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
**保存的内容:**
|
|
203
|
-
|
|
204
|
-
- `enabled` - 插件启用状态
|
|
205
|
-
- `config` - 插件配置对象
|
|
206
|
-
|
|
207
|
-
**自动恢复:** 框架启动时自动加载保存的状态
|
|
208
|
-
|
|
209
|
-
### 4.1 Framework (src/core/framework.js)
|
|
210
|
-
|
|
211
|
-
**职责**:
|
|
212
|
-
|
|
213
|
-
- 管理插件生命周期
|
|
214
|
-
- 提供工具注册表
|
|
215
|
-
- 事件总线
|
|
216
|
-
- 初始化协调
|
|
217
|
-
|
|
218
|
-
**API**:
|
|
219
|
-
|
|
220
|
-
```javascript
|
|
221
|
-
const framework = new Framework({
|
|
222
|
-
debug: false
|
|
223
|
-
})
|
|
224
|
-
|
|
225
|
-
// 插件操作
|
|
226
|
-
framework.registerPlugin(myPlugin)
|
|
227
|
-
await framework.loadPlugin({ type: 'ai-plugin', config: {...} })
|
|
228
|
-
await framework.unloadPlugin('my-plugin')
|
|
229
|
-
await framework.reloadPlugin('my-plugin') // 手动热重载
|
|
230
|
-
await framework.reloadAllPlugins()
|
|
231
|
-
|
|
232
|
-
// 工具操作
|
|
233
|
-
framework.registerTool({
|
|
234
|
-
name: 'my_tool',
|
|
235
|
-
description: '...',
|
|
236
|
-
execute: async (args) => { ... }
|
|
237
|
-
})
|
|
238
|
-
|
|
239
|
-
// 事件
|
|
240
|
-
framework.on('plugin:loaded', (plugin) => { ... })
|
|
241
|
-
framework.emit('agent:message', { content: '...' })
|
|
242
|
-
|
|
243
|
-
// 就绪
|
|
244
|
-
await framework.ready()
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
### 4.2 Agent (src/core/agent.js)
|
|
248
|
-
|
|
249
|
-
**职责**:
|
|
250
|
-
|
|
251
|
-
- 维护对话上下文
|
|
252
|
-
- 调用 AI 进行对话
|
|
253
|
-
- 管理工具调用循环
|
|
254
|
-
|
|
255
|
-
**API**:
|
|
256
|
-
|
|
257
|
-
```javascript
|
|
258
|
-
const agent = framework.createAgent({
|
|
259
|
-
systemPrompt: '你是一个有帮助的助手',
|
|
260
|
-
model: 'deepseek-chat',
|
|
261
|
-
apiKey: '...'
|
|
262
|
-
})
|
|
263
|
-
|
|
264
|
-
agent.on('message', (msg) => { ... })
|
|
265
|
-
agent.on('tool-call', (tool) => { ... })
|
|
266
|
-
|
|
267
|
-
const response = await agent.chat('你好')
|
|
268
|
-
```
|
|
269
|
-
|
|
270
|
-
### 4.3 PluginManager (src/core/plugin-manager.js)
|
|
271
|
-
|
|
272
|
-
**职责**:
|
|
273
|
-
|
|
274
|
-
- 维护已加载插件列表
|
|
275
|
-
- 按优先级排序加载
|
|
276
|
-
- 协调插件生命周期
|
|
277
|
-
|
|
278
|
-
**生命周期**:
|
|
279
|
-
|
|
280
|
-
1. `install()` - 注册工具/事件
|
|
281
|
-
2. `start()` - 初始化完成,开始工作
|
|
282
|
-
3. `reload()` - 热重载
|
|
283
|
-
4. `uninstall()` - 清理
|
|
284
|
-
|
|
285
|
-
### 4.4 ToolRegistry (src/core/tool-registry.js)
|
|
286
|
-
|
|
287
|
-
**职责**:
|
|
288
|
-
|
|
289
|
-
- 注册工具
|
|
290
|
-
- 调用工具
|
|
291
|
-
- 工具描述管理
|
|
292
|
-
|
|
293
|
-
**工具格式**:
|
|
294
|
-
|
|
295
|
-
```javascript
|
|
296
|
-
{
|
|
297
|
-
name: 'tool_name',
|
|
298
|
-
description: '工具描述',
|
|
299
|
-
parameters: { ... }, // Zod schema 或 JSON schema
|
|
300
|
-
execute: async (args, framework) => { ... }
|
|
301
|
-
}
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
## 五、事件系统
|
|
305
|
-
|
|
306
|
-
```javascript
|
|
307
|
-
// 事件列表
|
|
308
|
-
'framework:ready'; // 框架就绪
|
|
309
|
-
'
|
|
310
|
-
'plugin:
|
|
311
|
-
'plugin:
|
|
312
|
-
'plugin:
|
|
313
|
-
'plugin:
|
|
314
|
-
'plugin:
|
|
315
|
-
'
|
|
316
|
-
'agent:
|
|
317
|
-
'agent:
|
|
318
|
-
'
|
|
319
|
-
'
|
|
320
|
-
'
|
|
321
|
-
'
|
|
322
|
-
'
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
###
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
//
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
### 7.
|
|
459
|
-
|
|
460
|
-
**功能**:
|
|
461
|
-
|
|
462
|
-
-
|
|
463
|
-
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
- `
|
|
470
|
-
- `
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
-
|
|
503
|
-
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
-
|
|
659
|
-
-
|
|
660
|
-
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
1
|
+
# Foliko Framework 规划文档
|
|
2
|
+
|
|
3
|
+
## 一、设计目标
|
|
4
|
+
|
|
5
|
+
1. **纯 JS 实现** - 无 TypeScript,简明易懂
|
|
6
|
+
2. **架构简约** - 核心仅包含必要组件,通过插件扩展功能
|
|
7
|
+
3. **插件系统与 Agent 分离** - Agent 专注于对话推理,插件提供扩展能力
|
|
8
|
+
4. **手动热重载** - 不监测文件变化,通过 API 手动调用重载
|
|
9
|
+
|
|
10
|
+
## 二、核心架构
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
┌─────────────────────────────────────────────────┐
|
|
14
|
+
│ Foliko Framework │
|
|
15
|
+
├─────────────────────────────────────────────────┤
|
|
16
|
+
│ Framework (容器层) │
|
|
17
|
+
│ ├── pluginManager - 插件加载/卸载/重载 │
|
|
18
|
+
│ ├── toolRegistry - 工具注册表 │
|
|
19
|
+
│ ├── skillManager - Skill管理 (可选) │
|
|
20
|
+
│ └── eventEmitter - 事件总线 │
|
|
21
|
+
├─────────────────────────────────────────────────┤
|
|
22
|
+
│ Agent (对话层) │
|
|
23
|
+
│ ├── chat() - 对话接口 │
|
|
24
|
+
│ ├── tools - 从 Framework 获取 │
|
|
25
|
+
│ └── events - 事件监听 │
|
|
26
|
+
└─────────────────────────────────────────────────┘
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### 2.1 Framework 类
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
class Framework {
|
|
33
|
+
constructor(config = {})
|
|
34
|
+
registerPlugin(plugin) // 注册插件
|
|
35
|
+
loadPlugin(pluginDef) // 加载插件
|
|
36
|
+
unloadPlugin(name) // 卸载插件
|
|
37
|
+
reloadPlugin(name) // 重载单个插件
|
|
38
|
+
reloadAllPlugins() // 重载所有插件
|
|
39
|
+
registerTool(tool) // 注册工具
|
|
40
|
+
getTools() // 获取所有工具
|
|
41
|
+
on(event, handler) // 监听事件
|
|
42
|
+
emit(event, data) // 触发事件
|
|
43
|
+
ready() // 等待初始化完成
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 2.2 Agent 类
|
|
48
|
+
|
|
49
|
+
```javascript
|
|
50
|
+
// 创建 Agent
|
|
51
|
+
const agent = framework.createAgent({
|
|
52
|
+
name: 'MyAgent', // Agent 名称
|
|
53
|
+
systemPrompt: '你是一个助手', // 系统提示
|
|
54
|
+
|
|
55
|
+
// 共享提示模板 (支持 {{VAR}} 占位符)
|
|
56
|
+
sharedPrompt: `工作目录: {{WORK_DIR}}`,
|
|
57
|
+
|
|
58
|
+
// 元数据 (供 sharedPrompt 和自身使用)
|
|
59
|
+
metadata: {
|
|
60
|
+
projectName: 'MyProject',
|
|
61
|
+
version: '1.0.0',
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// 运行时元数据操作
|
|
66
|
+
agent.setMetadata('task', '数据分析'); // 设置单个
|
|
67
|
+
agent.setMetadata({ key: 'value' }); // 批量设置
|
|
68
|
+
agent.getMetadata('task'); // 获取
|
|
69
|
+
agent.deleteMetadata('task'); // 删除
|
|
70
|
+
agent.clearMetadata(); // 清空
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**内置占位符:**
|
|
74
|
+
| 占位符 | 说明 |
|
|
75
|
+
|--------|------|
|
|
76
|
+
| `{{WORK_DIR}}` | 工作目录 |
|
|
77
|
+
| `{{HOME_DIR}}` | 主目录 |
|
|
78
|
+
| `{{HOST_NAME}}` | 主机名 |
|
|
79
|
+
| `{{PLATFORM}}` | 平台 |
|
|
80
|
+
| `{{TIME}}` | 当前时间 |
|
|
81
|
+
| `{{DATE}}` | 当前日期 |
|
|
82
|
+
|
|
83
|
+
**Agent 方法:**
|
|
84
|
+
|
|
85
|
+
```javascript
|
|
86
|
+
class Agent {
|
|
87
|
+
constructor(framework, config) // 创建 Agent
|
|
88
|
+
chat(message, options) // 发送消息
|
|
89
|
+
chatStream(message, options) // 流式发送消息
|
|
90
|
+
setSystemPrompt(prompt) // 设置系统提示
|
|
91
|
+
registerTool(tool) // 注册工具
|
|
92
|
+
getTools() // 获取工具列表
|
|
93
|
+
clearHistory() // 清空对话历史
|
|
94
|
+
getStatus() // 获取状态
|
|
95
|
+
destroy() // 销毁 Agent
|
|
96
|
+
|
|
97
|
+
// 元数据管理
|
|
98
|
+
setMetadata(key, value) // 设置元数据
|
|
99
|
+
getMetadata(key) // 获取元数据
|
|
100
|
+
deleteMetadata(key) // 删除元数据
|
|
101
|
+
clearMetadata() // 清空元数据
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
**生成的 System Prompt 示例:**
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
你是一个助手。
|
|
109
|
+
|
|
110
|
+
工作目录: D:\project
|
|
111
|
+
|
|
112
|
+
【元数据】
|
|
113
|
+
- projectName: MyProject
|
|
114
|
+
- version: 1.0.0
|
|
115
|
+
|
|
116
|
+
【可用工具】
|
|
117
|
+
- shell: 执行命令
|
|
118
|
+
- python: 执行Python代码
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### 2.3 插件基类
|
|
122
|
+
|
|
123
|
+
```javascript
|
|
124
|
+
class Plugin {
|
|
125
|
+
name; // 插件名称 (必填)
|
|
126
|
+
version = '1.0.0';
|
|
127
|
+
description; // 插件描述
|
|
128
|
+
|
|
129
|
+
install(framework) {} // 安装时调用
|
|
130
|
+
start(framework) {} // 启动时调用
|
|
131
|
+
reload(framework) {} // 热重载时调用
|
|
132
|
+
uninstall(framework) {} // 卸载时调用
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## 三、目录结构
|
|
137
|
+
|
|
138
|
+
```
|
|
139
|
+
D:\code\foliko\
|
|
140
|
+
├── src/
|
|
141
|
+
│ ├── core/
|
|
142
|
+
│ │ ├── framework.js # 核心容器
|
|
143
|
+
│ │ ├── agent.js # Agent类
|
|
144
|
+
│ │ ├── agent-chat.js # 聊天处理器
|
|
145
|
+
│ │ ├── plugin-manager.js# 插件管理器
|
|
146
|
+
│ │ ├── plugin-base.js # 插件基类
|
|
147
|
+
│ │ ├── tool-registry.js # 工具注册表
|
|
148
|
+
│ │ ├── provider.js # AI Provider 工厂
|
|
149
|
+
│ │ └── index.js # 统一导出
|
|
150
|
+
│ ├── capabilities/
|
|
151
|
+
│ │ ├── skill-manager.js # Skill 管理器
|
|
152
|
+
│ │ └── workflow-engine.js # 工作流引擎
|
|
153
|
+
│ ├── executors/
|
|
154
|
+
│ │ ├── executor-base.js # 执行器基类
|
|
155
|
+
│ │ └── mcp-executor.js # MCP 执行器
|
|
156
|
+
│ ├── utils/
|
|
157
|
+
│ │ └── event-emitter.js # 事件发射器
|
|
158
|
+
│ └── index.js
|
|
159
|
+
├── plugins/ # 内置插件
|
|
160
|
+
│ ├── ai-plugin.js # AI对话插件
|
|
161
|
+
│ ├── tools-plugin.js # 内置工具插件
|
|
162
|
+
│ ├── default-plugins.js # 默认插件加载器
|
|
163
|
+
│ ├── shell-executor-plugin.js # Shell执行器插件
|
|
164
|
+
│ ├── python-executor-plugin.js # Python执行器插件
|
|
165
|
+
│ ├── session-plugin.js # Session会话管理插件
|
|
166
|
+
│ ├── audit-plugin.js # Audit审计日志插件
|
|
167
|
+
│ ├── rules-plugin.js # Rules规则引擎插件
|
|
168
|
+
│ ├── scheduler-plugin.js # Scheduler定时任务插件
|
|
169
|
+
│ ├── storage-plugin.js # Storage存储插件
|
|
170
|
+
│ ├── subagent-plugin.js # SubAgent子代理插件
|
|
171
|
+
│ ├── email.js # Email邮件插件
|
|
172
|
+
│ └── telegram-plugin.js # Telegram对话插件
|
|
173
|
+
├── examples/
|
|
174
|
+
│ ├── basic.js # 基础示例
|
|
175
|
+
│ ├── bootstrap.js # Bootstrap示例
|
|
176
|
+
│ └── workflow.js # 工作流示例
|
|
177
|
+
├── .agent/ # Agent 配置目录
|
|
178
|
+
│ ├── config # 配置文件
|
|
179
|
+
│ ├── ai.json # AI 配置
|
|
180
|
+
│ ├── plugins.json # 插件列表
|
|
181
|
+
│ ├── mcp_config.json # MCP 服务器配置
|
|
182
|
+
│ └── skills/ # Skill 目录
|
|
183
|
+
├── package.json
|
|
184
|
+
└── SPEC.md
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## 四、核心模块
|
|
188
|
+
|
|
189
|
+
### 4.5 插件配置持久化
|
|
190
|
+
|
|
191
|
+
插件状态和配置自动保存到 `.agent/data/plugins-state.json`
|
|
192
|
+
|
|
193
|
+
```javascript
|
|
194
|
+
// 更新插件配置
|
|
195
|
+
framework.updatePluginConfig('telegram', { allowedChats: ['123'] });
|
|
196
|
+
|
|
197
|
+
// 启用/禁用插件
|
|
198
|
+
framework.enablePlugin('telegram');
|
|
199
|
+
framework.disablePlugin('telegram');
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
**保存的内容:**
|
|
203
|
+
|
|
204
|
+
- `enabled` - 插件启用状态
|
|
205
|
+
- `config` - 插件配置对象
|
|
206
|
+
|
|
207
|
+
**自动恢复:** 框架启动时自动加载保存的状态
|
|
208
|
+
|
|
209
|
+
### 4.1 Framework (src/core/framework.js)
|
|
210
|
+
|
|
211
|
+
**职责**:
|
|
212
|
+
|
|
213
|
+
- 管理插件生命周期
|
|
214
|
+
- 提供工具注册表
|
|
215
|
+
- 事件总线
|
|
216
|
+
- 初始化协调
|
|
217
|
+
|
|
218
|
+
**API**:
|
|
219
|
+
|
|
220
|
+
```javascript
|
|
221
|
+
const framework = new Framework({
|
|
222
|
+
debug: false
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
// 插件操作
|
|
226
|
+
framework.registerPlugin(myPlugin)
|
|
227
|
+
await framework.loadPlugin({ type: 'ai-plugin', config: {...} })
|
|
228
|
+
await framework.unloadPlugin('my-plugin')
|
|
229
|
+
await framework.reloadPlugin('my-plugin') // 手动热重载
|
|
230
|
+
await framework.reloadAllPlugins()
|
|
231
|
+
|
|
232
|
+
// 工具操作
|
|
233
|
+
framework.registerTool({
|
|
234
|
+
name: 'my_tool',
|
|
235
|
+
description: '...',
|
|
236
|
+
execute: async (args) => { ... }
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
// 事件
|
|
240
|
+
framework.on('plugin:loaded', (plugin) => { ... })
|
|
241
|
+
framework.emit('agent:message', { content: '...' })
|
|
242
|
+
|
|
243
|
+
// 就绪
|
|
244
|
+
await framework.ready()
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### 4.2 Agent (src/core/agent.js)
|
|
248
|
+
|
|
249
|
+
**职责**:
|
|
250
|
+
|
|
251
|
+
- 维护对话上下文
|
|
252
|
+
- 调用 AI 进行对话
|
|
253
|
+
- 管理工具调用循环
|
|
254
|
+
|
|
255
|
+
**API**:
|
|
256
|
+
|
|
257
|
+
```javascript
|
|
258
|
+
const agent = framework.createAgent({
|
|
259
|
+
systemPrompt: '你是一个有帮助的助手',
|
|
260
|
+
model: 'deepseek-chat',
|
|
261
|
+
apiKey: '...'
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
agent.on('message', (msg) => { ... })
|
|
265
|
+
agent.on('tool-call', (tool) => { ... })
|
|
266
|
+
|
|
267
|
+
const response = await agent.chat('你好')
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### 4.3 PluginManager (src/core/plugin-manager.js)
|
|
271
|
+
|
|
272
|
+
**职责**:
|
|
273
|
+
|
|
274
|
+
- 维护已加载插件列表
|
|
275
|
+
- 按优先级排序加载
|
|
276
|
+
- 协调插件生命周期
|
|
277
|
+
|
|
278
|
+
**生命周期**:
|
|
279
|
+
|
|
280
|
+
1. `install()` - 注册工具/事件
|
|
281
|
+
2. `start()` - 初始化完成,开始工作
|
|
282
|
+
3. `reload()` - 热重载
|
|
283
|
+
4. `uninstall()` - 清理
|
|
284
|
+
|
|
285
|
+
### 4.4 ToolRegistry (src/core/tool-registry.js)
|
|
286
|
+
|
|
287
|
+
**职责**:
|
|
288
|
+
|
|
289
|
+
- 注册工具
|
|
290
|
+
- 调用工具
|
|
291
|
+
- 工具描述管理
|
|
292
|
+
|
|
293
|
+
**工具格式**:
|
|
294
|
+
|
|
295
|
+
```javascript
|
|
296
|
+
{
|
|
297
|
+
name: 'tool_name',
|
|
298
|
+
description: '工具描述',
|
|
299
|
+
parameters: { ... }, // Zod schema 或 JSON schema
|
|
300
|
+
execute: async (args, framework) => { ... }
|
|
301
|
+
}
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
## 五、事件系统
|
|
305
|
+
|
|
306
|
+
```javascript
|
|
307
|
+
// 事件列表
|
|
308
|
+
'framework:ready'; // 框架就绪
|
|
309
|
+
'framework:destroyed'; // 框架销毁
|
|
310
|
+
'plugin:registered'; // 插件注册
|
|
311
|
+
'plugin:loaded'; // 插件加载
|
|
312
|
+
'plugin:unloaded'; // 插件卸载
|
|
313
|
+
'plugin:reloaded'; // 插件重载
|
|
314
|
+
'plugin:enabled'; // 插件启用
|
|
315
|
+
'plugin:disabled'; // 插件禁用
|
|
316
|
+
'agent:created'; // Agent创建
|
|
317
|
+
'agent:message'; // Agent消息
|
|
318
|
+
'tool:registered'; // 工具注册
|
|
319
|
+
'tool:unregistered'; // 工具注销
|
|
320
|
+
'tool:call'; // 工具调用
|
|
321
|
+
'tool:result'; // 工具执行结果
|
|
322
|
+
'tool:error'; // 工具执行错误
|
|
323
|
+
'tool:retry'; // 工具重试
|
|
324
|
+
'tool:cleared'; // 工具清除
|
|
325
|
+
'session:created'; // 会话创建
|
|
326
|
+
'session:started'; // 会话启动
|
|
327
|
+
'session:ended'; // 会话结束
|
|
328
|
+
'session:deleted'; // 会话删除
|
|
329
|
+
'session:context-created'; // Session Context创建
|
|
330
|
+
'session:context-destroyed'; // Session Context销毁
|
|
331
|
+
'notification'; // 统一通知事件(推送用户)
|
|
332
|
+
'scheduler:task_created'; // 定时任务创建
|
|
333
|
+
'scheduler:reminder'; // 定时提醒触发
|
|
334
|
+
'scheduler:task_completed'; // 定时任务完成
|
|
335
|
+
'scheduler:task_failed'; // 定时任务失败
|
|
336
|
+
'think:thought_completed'; // 思考完成
|
|
337
|
+
'think:reflection_needs_attention'; // 反思需要关注
|
|
338
|
+
'email:received'; // 收到新邮件
|
|
339
|
+
'webhook:received'; // 收到Webhook请求
|
|
340
|
+
'webhook:processed'; // Webhook处理完成
|
|
341
|
+
'worker:created'; // Worker创建
|
|
342
|
+
'worker:started'; // Worker启动
|
|
343
|
+
'worker:completed'; // Worker完成
|
|
344
|
+
'worker:error'; // Worker错误
|
|
345
|
+
'worker:stopped'; // Worker停止
|
|
346
|
+
'worker:message'; // Worker消息
|
|
347
|
+
'coordinator:worker-completed'; // 协调器Worker完成
|
|
348
|
+
'coordinator:worker-error'; // 协调器Worker错误
|
|
349
|
+
'coordinator:worker-stopped'; // 协调器Worker停止
|
|
350
|
+
'subagent:chat:start'; // 子Agent聊天开始
|
|
351
|
+
'subagent:chat:end'; // 子Agent聊天结束
|
|
352
|
+
'subagent:error'; // 子Agent错误
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
### 通知事件 (notification)
|
|
356
|
+
|
|
357
|
+
统一的通知事件,用于向用户发送通知。消息插件(微信、飞书、Telegram)监听此事件并推送给用户。
|
|
358
|
+
|
|
359
|
+
```javascript
|
|
360
|
+
{
|
|
361
|
+
title: '目标完成', // 通知标题
|
|
362
|
+
message: '目标 "xxx" 已完成', // 通知内容
|
|
363
|
+
source: 'scheduler', // 来源:'scheduler' | 'ambient' | 'email' | 'webhook'
|
|
364
|
+
level: 'info', // 级别:'info' | 'warning' | 'success' | 'error'
|
|
365
|
+
sessionId: 'default', // 会话ID(可选)
|
|
366
|
+
timestamp: new Date() // 时间戳
|
|
367
|
+
}
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
### 业务事件
|
|
371
|
+
|
|
372
|
+
业务事件用于触发 Ambient Agent 的目标,不直接通知用户。
|
|
373
|
+
|
|
374
|
+
| 事件 | 数据 | 说明 |
|
|
375
|
+
| -------------------------- | ------------------------------------------------------------------------------- | --------------- | ------------ |
|
|
376
|
+
| `email:received` | `{uid, messageId, from, to, subject, text, body, html, attachments, timestamp}` | 收到新邮件 |
|
|
377
|
+
| `webhook:received` | `{path, method, query, body, timestamp}` | 收到Webhook请求 |
|
|
378
|
+
| `webhook:processed` | `{path, method, query, body, response, timestamp}` | Webhook处理完成 |
|
|
379
|
+
| `scheduler:task_created` | `{taskId, taskName, type, scheduleTime, cronExpression}` | 定时任务创建 |
|
|
380
|
+
| `scheduler:reminder` | `{taskId, message, time}` | 定时提醒触发 |
|
|
381
|
+
| `scheduler:task_completed` | `{taskId, result}` | 定时任务完成 |
|
|
382
|
+
| `scheduler:task_failed` | `{taskId, error}` | 定时任务失败 |
|
|
383
|
+
| `think:thought_completed` | `{mode, topic, thought, depth}` | 思考完成 |
|
|
384
|
+
| `tool:result` | `{name, args, result, source}` (source: tool | mcp) | 工具执行结果 |
|
|
385
|
+
| `tool:error` | `{name, args, error, source}` (source: tool | mcp) | 工具执行错误 |
|
|
386
|
+
| `tool:call` | `{name, args, source}` (source: tool | mcp) | 工具调用 |
|
|
387
|
+
| `agent:message` | `{content, sessionId, userMessage}` | Agent发送消息 |
|
|
388
|
+
| `worker:completed` | `{workerId, name, result, notification}` | Worker完成 |
|
|
389
|
+
| `worker:error` | `{workerId, name, error, notification}` | Worker错误 |
|
|
390
|
+
| `subagent:chat:end` | `{parentAgent, subAgent, subAgentName, result}` | 子Agent聊天结束 |
|
|
391
|
+
|
|
392
|
+
## 六、热重载机制
|
|
393
|
+
|
|
394
|
+
### 设计原则
|
|
395
|
+
|
|
396
|
+
- **不监测文件变化** - 避免复杂性和性能开销
|
|
397
|
+
- **手动调用重载** - 通过 API 触发
|
|
398
|
+
- **插件自管理** - 插件提供 reload() 方法
|
|
399
|
+
|
|
400
|
+
### API
|
|
401
|
+
|
|
402
|
+
```javascript
|
|
403
|
+
// 重载单个插件
|
|
404
|
+
await framework.reloadPlugin('my-plugin');
|
|
405
|
+
|
|
406
|
+
// 重载所有插件
|
|
407
|
+
await framework.reloadAllPlugins();
|
|
408
|
+
|
|
409
|
+
// 在 Agent 中调用
|
|
410
|
+
agent.chat('请重载 my-plugin 插件');
|
|
411
|
+
// 需要插件提供 reload_plugins 工具
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
### 重载流程
|
|
415
|
+
|
|
416
|
+
1. 调用 `plugin.reload(framework)`
|
|
417
|
+
2. 插件清理旧资源
|
|
418
|
+
3. 插件重新初始化
|
|
419
|
+
4. 更新工具注册表
|
|
420
|
+
|
|
421
|
+
## 七、内置插件
|
|
422
|
+
|
|
423
|
+
### 7.1 AI Plugin (plugins/ai-plugin.js)
|
|
424
|
+
|
|
425
|
+
**功能**:
|
|
426
|
+
|
|
427
|
+
- AI 对话能力
|
|
428
|
+
- 工具调用循环
|
|
429
|
+
|
|
430
|
+
**配置**:
|
|
431
|
+
|
|
432
|
+
```javascript
|
|
433
|
+
{
|
|
434
|
+
type: 'ai-plugin',
|
|
435
|
+
provider: 'deepseek', // deepseek / openai / anthropic
|
|
436
|
+
model: 'deepseek-chat',
|
|
437
|
+
apiKey: '...',
|
|
438
|
+
baseURL: '...' // 可选,自定义端点
|
|
439
|
+
}
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
### 7.2 Tools Plugin (plugins/tools-plugin.js)
|
|
443
|
+
|
|
444
|
+
**功能**:
|
|
445
|
+
|
|
446
|
+
- 内置工具注册
|
|
447
|
+
- 工具调用执行
|
|
448
|
+
|
|
449
|
+
**内置工具**:
|
|
450
|
+
|
|
451
|
+
- `reload_plugins` - 热重载插件
|
|
452
|
+
- `list_plugins` - 列出已加载插件
|
|
453
|
+
- `enable_plugin` - 启用插件
|
|
454
|
+
- `disable_plugin` - 禁用插件
|
|
455
|
+
- `get_plugin_config` - 获取插件配置
|
|
456
|
+
- `update_plugin_config` - 更新插件配置
|
|
457
|
+
|
|
458
|
+
### 7.3 Email Plugin (plugins/email-plugin.js)
|
|
459
|
+
|
|
460
|
+
**功能**:
|
|
461
|
+
|
|
462
|
+
- SMTP 发送邮件
|
|
463
|
+
- IMAP 读取邮件
|
|
464
|
+
|
|
465
|
+
**工具**:
|
|
466
|
+
|
|
467
|
+
- `email_send` - 发送邮件
|
|
468
|
+
- `email_read` - 读取邮件
|
|
469
|
+
- `email_unread_count` - 未读数量
|
|
470
|
+
- `email_mark_read` - 标记已读
|
|
471
|
+
|
|
472
|
+
### 7.4 Telegram Plugin (plugins/telegram-plugin.js)
|
|
473
|
+
|
|
474
|
+
**功能**:
|
|
475
|
+
|
|
476
|
+
- Telegram Bot 对话
|
|
477
|
+
- 绑定主 Agent 持续对话
|
|
478
|
+
- 支持 MarkdownV2
|
|
479
|
+
- 图片/文档接收保存
|
|
480
|
+
- 监听 `scheduler:reminder` 事件发送定时提醒
|
|
481
|
+
|
|
482
|
+
**监听事件**:
|
|
483
|
+
|
|
484
|
+
- `scheduler:reminder` - 接收定时任务提醒并发送到 Telegram
|
|
485
|
+
|
|
486
|
+
**配置**:
|
|
487
|
+
|
|
488
|
+
```javascript
|
|
489
|
+
{
|
|
490
|
+
botToken: '...',
|
|
491
|
+
allowedChats: ['123'],
|
|
492
|
+
groupMode: false,
|
|
493
|
+
prefix: '/'
|
|
494
|
+
}
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
### 7.5 Scheduler Plugin (plugins/scheduler-plugin.js)
|
|
498
|
+
|
|
499
|
+
**功能**:
|
|
500
|
+
|
|
501
|
+
- 定时任务调度(Cron 表达式、相对时间、绝对时间)
|
|
502
|
+
- 支持 LLM 自动检测(简单提醒直接显示,需要处理的任务启用 LLM)
|
|
503
|
+
- 会话自动检测(自动发送到最近活跃的会话)
|
|
504
|
+
- 事件系统(供其他插件监听和响应)
|
|
505
|
+
|
|
506
|
+
**工具**:
|
|
507
|
+
|
|
508
|
+
- `schedule_task` - 创建定时任务
|
|
509
|
+
- `schedule_list` - 列出所有任务
|
|
510
|
+
- `schedule_cancel` - 取消任务
|
|
511
|
+
- `cron_examples` - Cron 表达式示例
|
|
512
|
+
|
|
513
|
+
**schedule_task 参数**:
|
|
514
|
+
|
|
515
|
+
```javascript
|
|
516
|
+
{
|
|
517
|
+
name: '任务名称', // 可选
|
|
518
|
+
scheduleTime: '10 minutes', // 必填,支持多种格式
|
|
519
|
+
message: '提醒内容', // 必填
|
|
520
|
+
repeat: false, // 可选,是否重复
|
|
521
|
+
cronExpression: '*/5 * * * *', // 可选,Cron 表达式
|
|
522
|
+
sessionId: 'telegram_123', // 可选,会话 ID
|
|
523
|
+
llm: true // 可选,是否需要 LLM 处理(自动检测)
|
|
524
|
+
}
|
|
525
|
+
```
|
|
526
|
+
|
|
527
|
+
**时间格式**:
|
|
528
|
+
| 格式 | 示例 | 说明 |
|
|
529
|
+
|------|------|------|
|
|
530
|
+
| 相对时间 | `10 seconds`, `5 minutes`, `1 hour`, `2 days` | 多久后执行 |
|
|
531
|
+
| 绝对时间 | `12:00`, `14:30:00` | 具体时间(次日开始) |
|
|
532
|
+
| Cron | `*/5 * * * *`, `0 9 * * *` | Cron 表达式 |
|
|
533
|
+
|
|
534
|
+
**自动 LLM 检测**:
|
|
535
|
+
|
|
536
|
+
- 简单提醒(喝水、吃饭)→ 直接显示
|
|
537
|
+
- 查询/分析任务(查看列表、分析数据)→ 启用 LLM
|
|
538
|
+
|
|
539
|
+
**持久化**:
|
|
540
|
+
|
|
541
|
+
- 任务自动保存到 `.agent/data/scheduler/tasks.json`
|
|
542
|
+
- 重启后自动恢复 Cron 任务和未执行的一次性任务
|
|
543
|
+
|
|
544
|
+
**通知**:
|
|
545
|
+
|
|
546
|
+
- 任务完成/失败/取消时发送 `notification` 事件
|
|
547
|
+
- 消息插件(微信、飞书)自动推送给用户
|
|
548
|
+
|
|
549
|
+
## 八、Ambient Agent 插件
|
|
550
|
+
|
|
551
|
+
### 8.1 Ambient Agent (plugins/ambient-agent-plugin.js)
|
|
552
|
+
|
|
553
|
+
**功能**:
|
|
554
|
+
|
|
555
|
+
- 持续后台运行的智能代理
|
|
556
|
+
- 监控事件并主动执行操作
|
|
557
|
+
- 目标(Goal)管理系统
|
|
558
|
+
- 持久化记忆
|
|
559
|
+
|
|
560
|
+
**支持的事件**:
|
|
561
|
+
|
|
562
|
+
- `tool:result` - 工具执行结果
|
|
563
|
+
- `scheduler:reminder` - 定时提醒
|
|
564
|
+
- `agent:message` - 代理消息
|
|
565
|
+
- `think:thought_completed` - 思考完成
|
|
566
|
+
- `email:received` - 收到邮件
|
|
567
|
+
- `webhook:received` - 收到Webhook
|
|
568
|
+
|
|
569
|
+
**工具**:
|
|
570
|
+
|
|
571
|
+
- `ambient_goals` - 管理目标(列出/创建/更新/删除/激活)
|
|
572
|
+
- `ambient_status` - 获取状态
|
|
573
|
+
- `ambient_think` - 触发主动思考
|
|
574
|
+
- `ambient_remember` - 存储/检索记忆
|
|
575
|
+
- `ambient_control` - 控制循环(暂停/恢复/调整)
|
|
576
|
+
|
|
577
|
+
**目标结构**:
|
|
578
|
+
|
|
579
|
+
```javascript
|
|
580
|
+
{
|
|
581
|
+
title: '目标标题',
|
|
582
|
+
description: '目标描述',
|
|
583
|
+
priority: 5, // 1-10,数值越高越重要
|
|
584
|
+
actions: [ // 操作列表
|
|
585
|
+
{ type: 'tool', name: 'tool_name', args: {...} },
|
|
586
|
+
{ type: 'message', content: '消息内容' },
|
|
587
|
+
{ type: 'think', mode: 'reflect', topic: '思考主题' }
|
|
588
|
+
],
|
|
589
|
+
conditions: { // 激活条件(可选)
|
|
590
|
+
events: ['email:received'], // 监听的事件类型
|
|
591
|
+
toolNames: ['email_read'] // 过滤的工具名称
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
```
|
|
595
|
+
|
|
596
|
+
**通知**:
|
|
597
|
+
|
|
598
|
+
- 目标完成时发送 `notification` 事件(level: 'success')
|
|
599
|
+
- 目标失败时发送 `notification` 事件(level: 'error')
|
|
600
|
+
|
|
601
|
+
## 九、使用示例
|
|
602
|
+
|
|
603
|
+
### 9.1 基础用法
|
|
604
|
+
|
|
605
|
+
```javascript
|
|
606
|
+
const { Framework } = require('./src');
|
|
607
|
+
|
|
608
|
+
const framework = new Framework({ debug: true });
|
|
609
|
+
|
|
610
|
+
// 注册插件
|
|
611
|
+
framework.registerPlugin({
|
|
612
|
+
name: 'my-plugin',
|
|
613
|
+
install(f) {
|
|
614
|
+
f.registerTool({
|
|
615
|
+
name: 'greet',
|
|
616
|
+
description: '打招呼',
|
|
617
|
+
execute: async (args) => `Hello, ${args.name}!`,
|
|
618
|
+
});
|
|
619
|
+
},
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
await framework.ready();
|
|
623
|
+
|
|
624
|
+
const agent = framework.createAgent({
|
|
625
|
+
systemPrompt: '你是一个助手',
|
|
626
|
+
model: 'deepseek-chat',
|
|
627
|
+
apiKey: process.env.API_KEY,
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
const response = await agent.chat('你好');
|
|
631
|
+
console.log(response);
|
|
632
|
+
```
|
|
633
|
+
|
|
634
|
+
### 9.2 热重载
|
|
635
|
+
|
|
636
|
+
```javascript
|
|
637
|
+
// 重载单个插件
|
|
638
|
+
await framework.reloadPlugin('my-plugin');
|
|
639
|
+
|
|
640
|
+
// 重载所有插件
|
|
641
|
+
await framework.reloadAllPlugins();
|
|
642
|
+
```
|
|
643
|
+
|
|
644
|
+
## 九、代码规范
|
|
645
|
+
|
|
646
|
+
### 9.1 文件命名
|
|
647
|
+
|
|
648
|
+
- 类文件:`小写-连字符.js`,如 `plugin-base.js`
|
|
649
|
+
- 目录:`小写-连字符`,如 `src/core`
|
|
650
|
+
|
|
651
|
+
### 9.2 类命名
|
|
652
|
+
|
|
653
|
+
- 类名:`大写驼峰`,如 `Framework`, `Agent`
|
|
654
|
+
- 方法名:`小写驼峰`,如 `registerTool`, `loadPlugin`
|
|
655
|
+
|
|
656
|
+
### 9.3 变量命名
|
|
657
|
+
|
|
658
|
+
- 类属性:`小写驼峰`
|
|
659
|
+
- 常量:`全大写+下划线`,如 `DEFAULT_PRIORITY`
|
|
660
|
+
- 私有属性:`_下划线前缀`
|
|
661
|
+
|
|
662
|
+
### 9.4 注释
|
|
663
|
+
|
|
664
|
+
```javascript
|
|
665
|
+
/**
|
|
666
|
+
* 类描述
|
|
667
|
+
*/
|
|
668
|
+
class Framework {
|
|
669
|
+
/**
|
|
670
|
+
* 方法描述
|
|
671
|
+
* @param {string} name - 参数描述
|
|
672
|
+
* @returns {Promise<void>}
|
|
673
|
+
*/
|
|
674
|
+
async loadPlugin(name) {}
|
|
675
|
+
}
|
|
676
|
+
```
|
|
677
|
+
|
|
678
|
+
## 十、已实现功能
|
|
679
|
+
|
|
680
|
+
### Phase 1 - 核心框架 ✓
|
|
681
|
+
|
|
682
|
+
- [x] Framework 核心类 (`src/core/framework.js`)
|
|
683
|
+
- [x] Agent 基础类 (`src/core/agent.js`)
|
|
684
|
+
- [x] 插件系统 (`src/core/plugin-manager.js`, `src/core/plugin-base.js`)
|
|
685
|
+
- [x] 工具注册表 (`src/core/tool-registry.js`)
|
|
686
|
+
- [x] 事件系统 (`src/utils/event-emitter.js`)
|
|
687
|
+
- [x] 热重载 API (`reloadPlugin()`, `reloadAllPlugins()`)
|
|
688
|
+
|
|
689
|
+
### Phase 2 - 内置插件 ✓
|
|
690
|
+
|
|
691
|
+
- [x] AI Plugin (`plugins/ai-plugin.js`) - 对接 Vercel AI SDK
|
|
692
|
+
- [x] 内置工具插件 (`plugins/tools-plugin.js`) - 热重载、列表工具
|
|
693
|
+
|
|
694
|
+
### Phase 3 - 高级功能 ✓
|
|
695
|
+
|
|
696
|
+
- [x] Skill 管理 (`src/capabilities/skill-manager.js`) - 加载和管理 Skill
|
|
697
|
+
- [x] 工作流引擎 (`src/capabilities/workflow-engine.js`) - 支持脚本、条件、循环等
|
|
698
|
+
- [x] MCP 执行器 (`src/executors/mcp-executor.js`) - 连接 MCP 服务器
|
|
699
|
+
- [x] Shell 执行器 (`plugins/shell-executor-plugin.js`) - 执行终端命令
|
|
700
|
+
- [x] Python 执行器 (`plugins/python-executor-plugin.js`) - 执行 Python 代码和脚本
|
|
701
|
+
- [x] Session 管理 (`plugins/session-plugin.js`) - 多会话支持、历史记录
|
|
702
|
+
- [x] Audit 审计日志 (`plugins/audit-plugin.js`) - 操作日志记录和查询
|
|
703
|
+
- [x] Rules 规则引擎 (`plugins/rules-plugin.js`) - 权限控制、内容过滤
|
|
704
|
+
- [x] Scheduler 定时任务 (`plugins/scheduler-plugin.js`) - Cron 调度、事件系统、自动 LLM 检测
|
|
705
|
+
- [x] Storage 存储 (`plugins/storage-plugin.js`) - 键值对持久化存储
|
|
706
|
+
- [x] SubAgent 子Agent (`plugins/subagent-plugin.js`) - 子Agent隔离工具集
|
|
707
|
+
- [x] Email 插件 (`plugins/email.js`) - 邮件收发
|
|
708
|
+
- [x] Telegram 插件 (`plugins/telegram-plugin.js`) - Telegram Bot 对话
|
|
709
|
+
- [x] 插件配置持久化 - enabled/config 保存到 plugins-state.json
|
|
710
|
+
- [x] AI 热重载 - LLM 配置更新后自动刷新已有 Agent
|
|
711
|
+
|
|
712
|
+
## 十一、依赖
|
|
713
|
+
|
|
714
|
+
```json
|
|
715
|
+
{
|
|
716
|
+
"dependencies": {
|
|
717
|
+
"@ai-sdk/anthropic": "^3.0.58",
|
|
718
|
+
"@ai-sdk/mcp": "^1.0.25",
|
|
719
|
+
"@ai-sdk/openai": "^3.0.41",
|
|
720
|
+
"@ai-sdk/openai-compatible": "^2.0.35",
|
|
721
|
+
"@anthropic-ai/sdk": "^0.39.0",
|
|
722
|
+
"@modelcontextprotocol/sdk": "^1.27.1",
|
|
723
|
+
"ai": "^6.0.116",
|
|
724
|
+
"dotenv": "^17.3.1",
|
|
725
|
+
"imap": "^0.8.19",
|
|
726
|
+
"mailparser": "^3.7.2",
|
|
727
|
+
"marked": "^11.2.0",
|
|
728
|
+
"marked-terminal": "6",
|
|
729
|
+
"node-cron": "^4.2.1",
|
|
730
|
+
"node-telegram-bot-api": "^0.67.0",
|
|
731
|
+
"nodemailer": "^6.10.0",
|
|
732
|
+
"zod": "^3.24.0"
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
```
|