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.
Files changed (79) hide show
  1. package/.agent/data/email/processed-emails.json +1 -0
  2. package/.agent/data/plugins-state.json +5 -1
  3. package/.agent/data/web/web-config.json +5 -0
  4. package/.agent/memory/feedback/mnt7jrlt-d67qs7.md +15 -0
  5. package/.agent/memory/feedback/mnt88ja3-al4fuy.md +9 -0
  6. package/.agent/plugins/test-plugin.py +108 -0
  7. package/.agent/sessions/cli_default.json +514 -5298
  8. package/.claude/settings.local.json +2 -1
  9. package/SPEC.md +735 -696
  10. package/output/zen_silence.png +0 -0
  11. package/package.json +2 -2
  12. package/plugins/ambient-agent/EventWatcher.js +33 -37
  13. package/plugins/ambient-agent/ExplorerLoop.js +338 -36
  14. package/plugins/ambient-agent/GoalManager.js +7 -3
  15. package/plugins/ambient-agent/StateStore.js +30 -1
  16. package/plugins/ambient-agent/constants.js +15 -1
  17. package/plugins/ambient-agent/index.js +26 -33
  18. package/plugins/coordinator-plugin.js +3 -3
  19. package/plugins/default-plugins.js +2 -2
  20. package/plugins/email/index.js +150 -36
  21. package/plugins/email/monitor.js +79 -5
  22. package/plugins/email/reply.js +15 -25
  23. package/plugins/extension-executor-plugin.js +160 -31
  24. package/plugins/file-system-plugin.js +57 -24
  25. package/plugins/memory-plugin.js +176 -64
  26. package/plugins/python-plugin-loader.js +79 -9
  27. package/plugins/scheduler-plugin.js +64 -24
  28. package/plugins/think-plugin.js +7 -2
  29. package/plugins/web-plugin.js +263 -4
  30. package/skills/ambient-agent/SKILL.md +342 -314
  31. package/src/core/agent-chat.js +64 -9
  32. package/src/core/agent.js +118 -59
  33. package/src/core/tool-registry.js +5 -5
  34. package/src/executors/mcp-executor.js +188 -26
  35. package/src/utils/id.js +5 -0
  36. package/system.md +3480 -0
  37. package/.agent/data/ambient/goals.json +0 -50
  38. package/.agent/data/ambient/memories.json +0 -7
  39. package/.agent/memory/core.md +0 -1
  40. package/.agent/memory/feedback/mnrsiuoc-e1ru74.md +0 -9
  41. package/.agent/memory/feedback/mnrt2mmz-98az6n.md +0 -9
  42. package/.agent/memory/feedback/mnrtqrhm-kxsicz.md +0 -9
  43. package/.agent/memory/feedback/mnrts8vg-i0ngzp.md +0 -15
  44. package/.agent/memory/feedback/mnrtt7jt-c0trb2.md +0 -9
  45. package/.agent/memory/feedback/mnruc2f0-5s52la.md +0 -16
  46. package/.agent/memory/feedback/mnrumbmx-63sa0v.md +0 -9
  47. package/.agent/memory/project/mnn93ogy-ypjn27.md +0 -9
  48. package/.agent/memory/project/mnn98fqy-5nhc1u.md +0 -25
  49. package/.agent/memory/project/mnrp7p5n-8enm2a.md +0 -31
  50. package/.agent/memory/project/mnrp9ifb-yynks0.md +0 -40
  51. package/.agent/memory/project/mnrpb3b8-f617s4.md +0 -25
  52. package/.agent/memory/project/mnrrmqgg-focprv.md +0 -9
  53. package/.agent/memory/project/mnrtykbh-6atsor.md +0 -9
  54. package/.agent/memory/project/mnru9jiu-kgau16.md +0 -35
  55. package/.agent/memory/reference/mnq3oenw-46haj6.md +0 -63
  56. package/.agent/memory/reference/mnq5qxm2-mjoooh.md +0 -116
  57. package/.agent/memory/reference/mnrnvpwo-rcqv9m.md +0 -52
  58. package/.agent/memory/reference/mnrovxvz-zy9xqm.md +0 -25
  59. package/.agent/memory/reference/mnroxabj-1b3930.md +0 -68
  60. package/.agent/memory/reference/mnrpjtlp-mnb9od.md +0 -35
  61. package/.agent/memory/reference/mnrps1x3-6b8xfm.md +0 -28
  62. package/.agent/memory/reference/mnrpt9ov-15er5w.md +0 -22
  63. package/.agent/memory/reference/mnrq82dn-y9tv9e.md +0 -50
  64. package/.agent/memory/reference/mnrqnr5v-v75drf.md +0 -34
  65. package/.agent/memory/reference/mnrrfzys-urudaf.md +0 -31
  66. package/.agent/memory/reference/mnrrocha-t0027n.md +0 -21
  67. package/.agent/memory/reference/mnrukklc-bxndsb.md +0 -35
  68. package/.agent/memory/user/mnm67t9m-x8rekk.md +0 -9
  69. package/.agent/memory/user/mnn5mmqh-w6aktx.md +0 -11
  70. package/.agent/memory/user/mnnbfhhn-dk1bd1.md +0 -22
  71. package/.agent/memory/user/mnrt39t8-8eosy0.md +0 -9
  72. package/foliko-cloud-rising.png +0 -0
  73. package/foliko-dawn-of-ai.png +0 -0
  74. package/foliko-mindful-observation.png +0 -0
  75. package/foliko-stellar-dreams.png +0 -0
  76. package/foliko-zen-jing.png +0 -0
  77. package/foliko-zen-kong.png +0 -0
  78. package/foliko-zen-wu.png +0 -0
  79. 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
- 'plugin:install'; // 插件安装
310
- 'plugin:start'; // 插件启动
311
- 'plugin:reload'; // 插件重载
312
- 'plugin:uninstall'; // 插件卸载
313
- 'plugin:enabled'; // 插件启用
314
- 'plugin:disabled'; // 插件禁用
315
- 'agent:message'; // Agent消息
316
- 'agent:tool-call'; // Agent调用工具
317
- 'agent:tool-result'; // 工具执行结果
318
- 'notification'; // 统一通知事件(推送用户)
319
- 'scheduler:reminder'; // 定时提醒触发(业务事件)
320
- 'think:thought_completed'; // 思考完成
321
- 'email:received'; // 收到新邮件
322
- 'webhook:received'; // 收到Webhook请求
323
- ```
324
-
325
- ### 通知事件 (notification)
326
-
327
- 统一的通知事件,用于向用户发送通知。消息插件(微信、飞书、Telegram)监听此事件并推送给用户。
328
-
329
- ```javascript
330
- {
331
- title: '目标完成', // 通知标题
332
- message: '目标 "xxx" 已完成', // 通知内容
333
- source: 'ambient', // 来源:'scheduler' | 'ambient' | 'email' | 'webhook'
334
- level: 'success', // 级别:'info' | 'warning' | 'success' | 'error'
335
- sessionId: 'weixin_xxx', // 会话ID(可选)
336
- timestamp: new Date() // 时间戳
337
- }
338
- ```
339
-
340
- ### 业务事件
341
-
342
- 业务事件用于触发 Ambient Agent 的目标,不直接通知用户。
343
-
344
- | 事件 | 数据 | 说明 |
345
- | ------------------------- | ----------------------------------------------- | ----------------- |
346
- | `scheduler:reminder` | `{ taskId, taskName, message, sessionId, llm }` | 定时提醒触发时 |
347
- | `think:thought_completed` | `thought` | 思考完成时 |
348
- | `email:received` | `email` | 收到新邮件时 |
349
- | `webhook:received` | `{ webhook, data, response, sessionId }` | 收到Webhook请求时 |
350
- | `tool:result` | `{ name, args, result }` | 工具执行结果 |
351
- | `agent:message` | `{ content }` | Agent发送消息时 |
352
-
353
- ## 六、热重载机制
354
-
355
- ### 设计原则
356
-
357
- - **不监测文件变化** - 避免复杂性和性能开销
358
- - **手动调用重载** - 通过 API 触发
359
- - **插件自管理** - 插件提供 reload() 方法
360
-
361
- ### API
362
-
363
- ```javascript
364
- // 重载单个插件
365
- await framework.reloadPlugin('my-plugin');
366
-
367
- // 重载所有插件
368
- await framework.reloadAllPlugins();
369
-
370
- // 在 Agent 中调用
371
- agent.chat('请重载 my-plugin 插件');
372
- // 需要插件提供 reload_plugins 工具
373
- ```
374
-
375
- ### 重载流程
376
-
377
- 1. 调用 `plugin.reload(framework)`
378
- 2. 插件清理旧资源
379
- 3. 插件重新初始化
380
- 4. 更新工具注册表
381
-
382
- ## 七、内置插件
383
-
384
- ### 7.1 AI Plugin (plugins/ai-plugin.js)
385
-
386
- **功能**:
387
-
388
- - AI 对话能力
389
- - 工具调用循环
390
-
391
- **配置**:
392
-
393
- ```javascript
394
- {
395
- type: 'ai-plugin',
396
- provider: 'deepseek', // deepseek / openai / anthropic
397
- model: 'deepseek-chat',
398
- apiKey: '...',
399
- baseURL: '...' // 可选,自定义端点
400
- }
401
- ```
402
-
403
- ### 7.2 Tools Plugin (plugins/tools-plugin.js)
404
-
405
- **功能**:
406
-
407
- - 内置工具注册
408
- - 工具调用执行
409
-
410
- **内置工具**:
411
-
412
- - `reload_plugins` - 热重载插件
413
- - `list_plugins` - 列出已加载插件
414
- - `enable_plugin` - 启用插件
415
- - `disable_plugin` - 禁用插件
416
- - `get_plugin_config` - 获取插件配置
417
- - `update_plugin_config` - 更新插件配置
418
-
419
- ### 7.3 Email Plugin (plugins/email-plugin.js)
420
-
421
- **功能**:
422
-
423
- - SMTP 发送邮件
424
- - IMAP 读取邮件
425
-
426
- **工具**:
427
-
428
- - `email_send` - 发送邮件
429
- - `email_read` - 读取邮件
430
- - `email_unread_count` - 未读数量
431
- - `email_mark_read` - 标记已读
432
-
433
- ### 7.4 Telegram Plugin (plugins/telegram-plugin.js)
434
-
435
- **功能**:
436
-
437
- - Telegram Bot 对话
438
- - 绑定主 Agent 持续对话
439
- - 支持 MarkdownV2
440
- - 图片/文档接收保存
441
- - 监听 `scheduler:reminder` 事件发送定时提醒
442
-
443
- **监听事件**:
444
-
445
- - `scheduler:reminder` - 接收定时任务提醒并发送到 Telegram
446
-
447
- **配置**:
448
-
449
- ```javascript
450
- {
451
- botToken: '...',
452
- allowedChats: ['123'],
453
- groupMode: false,
454
- prefix: '/'
455
- }
456
- ```
457
-
458
- ### 7.5 Scheduler Plugin (plugins/scheduler-plugin.js)
459
-
460
- **功能**:
461
-
462
- - 定时任务调度(Cron 表达式、相对时间、绝对时间)
463
- - 支持 LLM 自动检测(简单提醒直接显示,需要处理的任务启用 LLM)
464
- - 会话自动检测(自动发送到最近活跃的会话)
465
- - 事件系统(供其他插件监听和响应)
466
-
467
- **工具**:
468
-
469
- - `schedule_task` - 创建定时任务
470
- - `schedule_list` - 列出所有任务
471
- - `schedule_cancel` - 取消任务
472
- - `cron_examples` - Cron 表达式示例
473
-
474
- **schedule_task 参数**:
475
-
476
- ```javascript
477
- {
478
- name: '任务名称', // 可选
479
- scheduleTime: '10 minutes', // 必填,支持多种格式
480
- message: '提醒内容', // 必填
481
- repeat: false, // 可选,是否重复
482
- cronExpression: '*/5 * * * *', // 可选,Cron 表达式
483
- sessionId: 'telegram_123', // 可选,会话 ID
484
- llm: true // 可选,是否需要 LLM 处理(自动检测)
485
- }
486
- ```
487
-
488
- **时间格式**:
489
- | 格式 | 示例 | 说明 |
490
- |------|------|------|
491
- | 相对时间 | `10 seconds`, `5 minutes`, `1 hour`, `2 days` | 多久后执行 |
492
- | 绝对时间 | `12:00`, `14:30:00` | 具体时间(次日开始) |
493
- | Cron | `*/5 * * * *`, `0 9 * * *` | Cron 表达式 |
494
-
495
- **自动 LLM 检测**:
496
-
497
- - 简单提醒(喝水、吃饭)→ 直接显示
498
- - 查询/分析任务(查看列表、分析数据)→ 启用 LLM
499
-
500
- **持久化**:
501
-
502
- - 任务自动保存到 `.agent/data/scheduler/tasks.json`
503
- - 重启后自动恢复 Cron 任务和未执行的一次性任务
504
-
505
- **通知**:
506
-
507
- - 任务完成/失败/取消时发送 `notification` 事件
508
- - 消息插件(微信、飞书)自动推送给用户
509
-
510
- ## 八、Ambient Agent 插件
511
-
512
- ### 8.1 Ambient Agent (plugins/ambient-agent-plugin.js)
513
-
514
- **功能**:
515
-
516
- - 持续后台运行的智能代理
517
- - 监控事件并主动执行操作
518
- - 目标(Goal)管理系统
519
- - 持久化记忆
520
-
521
- **支持的事件**:
522
-
523
- - `tool:result` - 工具执行结果
524
- - `scheduler:reminder` - 定时提醒
525
- - `agent:message` - 代理消息
526
- - `think:thought_completed` - 思考完成
527
- - `email:received` - 收到邮件
528
- - `webhook:received` - 收到Webhook
529
-
530
- **工具**:
531
-
532
- - `ambient_goals` - 管理目标(列出/创建/更新/删除/激活)
533
- - `ambient_status` - 获取状态
534
- - `ambient_think` - 触发主动思考
535
- - `ambient_remember` - 存储/检索记忆
536
- - `ambient_control` - 控制循环(暂停/恢复/调整)
537
-
538
- **目标结构**:
539
-
540
- ```javascript
541
- {
542
- title: '目标标题',
543
- description: '目标描述',
544
- priority: 5, // 1-10,数值越高越重要
545
- actions: [ // 操作列表
546
- { type: 'tool', name: 'tool_name', args: {...} },
547
- { type: 'message', content: '消息内容' },
548
- { type: 'think', mode: 'reflect', topic: '思考主题' }
549
- ],
550
- conditions: { // 激活条件(可选)
551
- events: ['email:received'], // 监听的事件类型
552
- toolNames: ['email_read'] // 过滤的工具名称
553
- }
554
- }
555
- ```
556
-
557
- **通知**:
558
-
559
- - 目标完成时发送 `notification` 事件(level: 'success')
560
- - 目标失败时发送 `notification` 事件(level: 'error')
561
-
562
- ## 九、使用示例
563
-
564
- ### 9.1 基础用法
565
-
566
- ```javascript
567
- const { Framework } = require('./src');
568
-
569
- const framework = new Framework({ debug: true });
570
-
571
- // 注册插件
572
- framework.registerPlugin({
573
- name: 'my-plugin',
574
- install(f) {
575
- f.registerTool({
576
- name: 'greet',
577
- description: '打招呼',
578
- execute: async (args) => `Hello, ${args.name}!`,
579
- });
580
- },
581
- });
582
-
583
- await framework.ready();
584
-
585
- const agent = framework.createAgent({
586
- systemPrompt: '你是一个助手',
587
- model: 'deepseek-chat',
588
- apiKey: process.env.API_KEY,
589
- });
590
-
591
- const response = await agent.chat('你好');
592
- console.log(response);
593
- ```
594
-
595
- ### 9.2 热重载
596
-
597
- ```javascript
598
- // 重载单个插件
599
- await framework.reloadPlugin('my-plugin');
600
-
601
- // 重载所有插件
602
- await framework.reloadAllPlugins();
603
- ```
604
-
605
- ## 九、代码规范
606
-
607
- ### 9.1 文件命名
608
-
609
- - 类文件:`小写-连字符.js`,如 `plugin-base.js`
610
- - 目录:`小写-连字符`,如 `src/core`
611
-
612
- ### 9.2 类命名
613
-
614
- - 类名:`大写驼峰`,如 `Framework`, `Agent`
615
- - 方法名:`小写驼峰`,如 `registerTool`, `loadPlugin`
616
-
617
- ### 9.3 变量命名
618
-
619
- - 类属性:`小写驼峰`
620
- - 常量:`全大写+下划线`,如 `DEFAULT_PRIORITY`
621
- - 私有属性:`_下划线前缀`
622
-
623
- ### 9.4 注释
624
-
625
- ```javascript
626
- /**
627
- * 类描述
628
- */
629
- class Framework {
630
- /**
631
- * 方法描述
632
- * @param {string} name - 参数描述
633
- * @returns {Promise<void>}
634
- */
635
- async loadPlugin(name) {}
636
- }
637
- ```
638
-
639
- ## 十、已实现功能
640
-
641
- ### Phase 1 - 核心框架 ✓
642
-
643
- - [x] Framework 核心类 (`src/core/framework.js`)
644
- - [x] Agent 基础类 (`src/core/agent.js`)
645
- - [x] 插件系统 (`src/core/plugin-manager.js`, `src/core/plugin-base.js`)
646
- - [x] 工具注册表 (`src/core/tool-registry.js`)
647
- - [x] 事件系统 (`src/utils/event-emitter.js`)
648
- - [x] 热重载 API (`reloadPlugin()`, `reloadAllPlugins()`)
649
-
650
- ### Phase 2 - 内置插件 ✓
651
-
652
- - [x] AI Plugin (`plugins/ai-plugin.js`) - 对接 Vercel AI SDK
653
- - [x] 内置工具插件 (`plugins/tools-plugin.js`) - 热重载、列表工具
654
-
655
- ### Phase 3 - 高级功能 ✓
656
-
657
- - [x] Skill 管理 (`src/capabilities/skill-manager.js`) - 加载和管理 Skill
658
- - [x] 工作流引擎 (`src/capabilities/workflow-engine.js`) - 支持脚本、条件、循环等
659
- - [x] MCP 执行器 (`src/executors/mcp-executor.js`) - 连接 MCP 服务器
660
- - [x] Shell 执行器 (`plugins/shell-executor-plugin.js`) - 执行终端命令
661
- - [x] Python 执行器 (`plugins/python-executor-plugin.js`) - 执行 Python 代码和脚本
662
- - [x] Session 管理 (`plugins/session-plugin.js`) - 多会话支持、历史记录
663
- - [x] Audit 审计日志 (`plugins/audit-plugin.js`) - 操作日志记录和查询
664
- - [x] Rules 规则引擎 (`plugins/rules-plugin.js`) - 权限控制、内容过滤
665
- - [x] Scheduler 定时任务 (`plugins/scheduler-plugin.js`) - Cron 调度、事件系统、自动 LLM 检测
666
- - [x] Storage 存储 (`plugins/storage-plugin.js`) - 键值对持久化存储
667
- - [x] SubAgent 子Agent (`plugins/subagent-plugin.js`) - 子Agent隔离工具集
668
- - [x] Email 插件 (`plugins/email.js`) - 邮件收发
669
- - [x] Telegram 插件 (`plugins/telegram-plugin.js`) - Telegram Bot 对话
670
- - [x] 插件配置持久化 - enabled/config 保存到 plugins-state.json
671
- - [x] AI 热重载 - LLM 配置更新后自动刷新已有 Agent
672
-
673
- ## 十一、依赖
674
-
675
- ```json
676
- {
677
- "dependencies": {
678
- "@ai-sdk/anthropic": "^3.0.58",
679
- "@ai-sdk/mcp": "^1.0.25",
680
- "@ai-sdk/openai": "^3.0.41",
681
- "@ai-sdk/openai-compatible": "^2.0.35",
682
- "@anthropic-ai/sdk": "^0.39.0",
683
- "@modelcontextprotocol/sdk": "^1.27.1",
684
- "ai": "^6.0.116",
685
- "dotenv": "^17.3.1",
686
- "imap": "^0.8.19",
687
- "mailparser": "^3.7.2",
688
- "marked": "^11.2.0",
689
- "marked-terminal": "6",
690
- "node-cron": "^4.2.1",
691
- "node-telegram-bot-api": "^0.67.0",
692
- "nodemailer": "^6.10.0",
693
- "zod": "^3.24.0"
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
+ ```