foliko 1.0.81 → 1.0.83

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 (88) hide show
  1. package/cli/bin/foliko.js +12 -12
  2. package/cli/src/commands/chat.js +143 -143
  3. package/cli/src/commands/list.js +93 -93
  4. package/cli/src/index.js +75 -75
  5. package/cli/src/ui/chat-ui.js +201 -201
  6. package/cli/src/utils/ansi.js +40 -40
  7. package/cli/src/utils/markdown.js +292 -292
  8. package/examples/ambient-example.js +194 -194
  9. package/examples/basic.js +115 -115
  10. package/examples/bootstrap.js +121 -121
  11. package/examples/mcp-example.js +56 -56
  12. package/examples/skill-example.js +49 -49
  13. package/examples/test-chat.js +137 -137
  14. package/examples/test-mcp.js +85 -85
  15. package/examples/test-reload.js +59 -59
  16. package/examples/test-telegram.js +50 -50
  17. package/examples/test-tg-bot.js +45 -45
  18. package/examples/test-tg-simple.js +47 -47
  19. package/examples/test-tg.js +62 -62
  20. package/examples/test-think.js +43 -43
  21. package/examples/test-web-plugin.js +103 -103
  22. package/examples/test-weixin-feishu.js +103 -103
  23. package/examples/workflow.js +158 -158
  24. package/package.json +83 -83
  25. package/plugins/ai-plugin.js +102 -102
  26. package/plugins/ambient-agent/EventWatcher.js +113 -113
  27. package/plugins/ambient-agent/ExplorerLoop.js +640 -640
  28. package/plugins/ambient-agent/GoalManager.js +197 -197
  29. package/plugins/ambient-agent/Reflector.js +95 -95
  30. package/plugins/ambient-agent/StateStore.js +90 -90
  31. package/plugins/ambient-agent/constants.js +101 -101
  32. package/plugins/ambient-agent/index.js +579 -579
  33. package/plugins/audit-plugin.js +187 -187
  34. package/plugins/default-plugins.js +548 -548
  35. package/plugins/email/constants.js +64 -64
  36. package/plugins/email/handlers.js +461 -461
  37. package/plugins/email/index.js +278 -278
  38. package/plugins/email/monitor.js +269 -269
  39. package/plugins/email/parser.js +138 -138
  40. package/plugins/email/reply.js +151 -151
  41. package/plugins/email/utils.js +124 -124
  42. package/plugins/extension-executor-plugin.js +326 -326
  43. package/plugins/feishu-plugin.js +481 -481
  44. package/plugins/file-system-plugin.js +920 -920
  45. package/plugins/gate-trading.js +747 -747
  46. package/plugins/install-plugin.js +199 -199
  47. package/plugins/python-executor-plugin.js +367 -367
  48. package/plugins/python-plugin-loader.js +651 -651
  49. package/plugins/rules-plugin.js +294 -294
  50. package/plugins/scheduler-plugin.js +691 -691
  51. package/plugins/session-plugin.js +494 -494
  52. package/plugins/shell-executor-plugin.js +197 -197
  53. package/plugins/storage-plugin.js +263 -263
  54. package/plugins/subagent-plugin.js +845 -845
  55. package/plugins/telegram-plugin.js +482 -482
  56. package/plugins/think-plugin.js +345 -345
  57. package/plugins/tools-plugin.js +196 -196
  58. package/plugins/web-plugin.js +637 -637
  59. package/plugins/weixin-plugin.js +545 -545
  60. package/src/capabilities/index.js +11 -11
  61. package/src/capabilities/skill-manager.js +609 -609
  62. package/src/capabilities/workflow-engine.js +1109 -1109
  63. package/src/core/agent-chat.js +1 -1
  64. package/src/core/agent.js +958 -958
  65. package/src/core/framework.js +465 -465
  66. package/src/core/index.js +19 -19
  67. package/src/core/plugin-base.js +262 -262
  68. package/src/core/plugin-manager.js +863 -863
  69. package/src/core/provider.js +114 -114
  70. package/src/core/sub-agent-config.js +264 -264
  71. package/src/core/system-prompt-builder.js +120 -120
  72. package/src/core/tool-registry.js +517 -517
  73. package/src/core/tool-router.js +297 -297
  74. package/src/executors/executor-base.js +58 -58
  75. package/src/executors/mcp-executor.js +845 -845
  76. package/src/index.js +25 -25
  77. package/src/utils/circuit-breaker.js +301 -301
  78. package/src/utils/error-boundary.js +363 -363
  79. package/src/utils/error.js +374 -374
  80. package/src/utils/event-emitter.js +97 -97
  81. package/src/utils/id.js +133 -133
  82. package/src/utils/index.js +217 -217
  83. package/src/utils/logger.js +181 -181
  84. package/src/utils/plugin-helpers.js +90 -90
  85. package/src/utils/retry.js +122 -122
  86. package/src/utils/sandbox.js +292 -292
  87. package/test/tool-registry-validation.test.js +218 -218
  88. package/website/script.js +136 -136
package/package.json CHANGED
@@ -1,83 +1,83 @@
1
- {
2
- "name": "foliko",
3
- "version": "1.0.81",
4
- "description": "简约的插件化 Agent 框架",
5
- "main": "src/index.js",
6
- "type": "commonjs",
7
- "bin": {
8
- "foliko": "./cli/bin/foliko.js"
9
- },
10
- "scripts": {
11
- "start": "node examples/basic.js",
12
- "chat": "node cli/bin/foliko.js chat",
13
- "dev": "node --watch examples/basic.js",
14
- "lint": "echo \"No linter configured\"",
15
- "format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,yaml,yml,md,css,html}\"",
16
- "format:check": "prettier --check \"**/*.{js,jsx,ts,tsx,json,yaml,yml,md,css,html}\"",
17
- "prepare": "husky"
18
- },
19
- "lint-staged": {
20
- "*.{js,jsx,ts,tsx}": [
21
- "prettier --write"
22
- ],
23
- "*.{json,yaml,yml}": [
24
- "prettier --write"
25
- ],
26
- "*.{md,mdx}": [
27
- "prettier --write"
28
- ],
29
- "*.{css,scss,less}": [
30
- "prettier --write"
31
- ],
32
- "*.{html,svg}": [
33
- "prettier --write"
34
- ]
35
- },
36
- "engines": {
37
- "node": ">=18.0.0"
38
- },
39
- "keywords": [
40
- "agent",
41
- "ai",
42
- "framework",
43
- "plugin",
44
- "claude",
45
- "openai",
46
- "anthropic"
47
- ],
48
- "author": "",
49
- "license": "MIT",
50
- "dependencies": {
51
- "@ai-sdk/anthropic": "^3.0.58",
52
- "@ai-sdk/mcp": "^1.0.25",
53
- "@ai-sdk/openai": "^3.0.41",
54
- "@ai-sdk/openai-compatible": "^2.0.35",
55
- "@anthropic-ai/sdk": "^0.39.0",
56
- "@chnak/weixin-bot": "^1.2.1",
57
- "@chnak/zod-to-markdown": "^1.0.1",
58
- "@hono/node-server": "^1.19.11",
59
- "@larksuiteoapi/node-sdk": "^1.59.0",
60
- "@modelcontextprotocol/sdk": "^1.27.1",
61
- "ai": "^6.0.116",
62
- "dotenv": "^17.3.1",
63
- "gate-api": "^7.2.57",
64
- "hono": "^4.12.9",
65
- "imap-mkl": "^1.0.2",
66
- "mailparser": "^3.7.2",
67
- "marked": "^11.2.0",
68
- "marked-terminal": "6",
69
- "node-cron": "^4.2.1",
70
- "node-html-markdown": "^1.3.0",
71
- "node-telegram-bot-api": "^0.67.0",
72
- "nodemailer": "^6.10.0",
73
- "qrcode-terminal": "^0.12.0",
74
- "remove-markdown": "^0.6.3",
75
- "vm2": "^3.10.5",
76
- "zod": "^3.25.76"
77
- },
78
- "devDependencies": {
79
- "husky": "^9.1.7",
80
- "lint-staged": "^16.4.0",
81
- "prettier": "^3.8.1"
82
- }
83
- }
1
+ {
2
+ "name": "foliko",
3
+ "version": "1.0.83",
4
+ "description": "简约的插件化 Agent 框架",
5
+ "main": "src/index.js",
6
+ "type": "commonjs",
7
+ "bin": {
8
+ "foliko": "./cli/bin/foliko.js"
9
+ },
10
+ "scripts": {
11
+ "start": "node examples/basic.js",
12
+ "chat": "node cli/bin/foliko.js chat",
13
+ "dev": "node --watch examples/basic.js",
14
+ "lint": "echo \"No linter configured\"",
15
+ "format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,yaml,yml,md,css,html}\"",
16
+ "format:check": "prettier --check \"**/*.{js,jsx,ts,tsx,json,yaml,yml,md,css,html}\"",
17
+ "prepare": "husky"
18
+ },
19
+ "lint-staged": {
20
+ "*.{js,jsx,ts,tsx}": [
21
+ "prettier --write"
22
+ ],
23
+ "*.{json,yaml,yml}": [
24
+ "prettier --write"
25
+ ],
26
+ "*.{md,mdx}": [
27
+ "prettier --write"
28
+ ],
29
+ "*.{css,scss,less}": [
30
+ "prettier --write"
31
+ ],
32
+ "*.{html,svg}": [
33
+ "prettier --write"
34
+ ]
35
+ },
36
+ "engines": {
37
+ "node": ">=18.0.0"
38
+ },
39
+ "keywords": [
40
+ "agent",
41
+ "ai",
42
+ "framework",
43
+ "plugin",
44
+ "claude",
45
+ "openai",
46
+ "anthropic"
47
+ ],
48
+ "author": "",
49
+ "license": "MIT",
50
+ "dependencies": {
51
+ "@ai-sdk/anthropic": "^3.0.58",
52
+ "@ai-sdk/mcp": "^1.0.25",
53
+ "@ai-sdk/openai": "^3.0.41",
54
+ "@ai-sdk/openai-compatible": "^2.0.35",
55
+ "@anthropic-ai/sdk": "^0.39.0",
56
+ "@chnak/weixin-bot": "^1.2.1",
57
+ "@chnak/zod-to-markdown": "^1.0.1",
58
+ "@hono/node-server": "^1.19.11",
59
+ "@larksuiteoapi/node-sdk": "^1.59.0",
60
+ "@modelcontextprotocol/sdk": "^1.27.1",
61
+ "ai": "^6.0.116",
62
+ "dotenv": "^17.3.1",
63
+ "gate-api": "^7.2.57",
64
+ "hono": "^4.12.9",
65
+ "imap-mkl": "^1.0.2",
66
+ "mailparser": "^3.7.2",
67
+ "marked": "^11.2.0",
68
+ "marked-terminal": "6",
69
+ "node-cron": "^4.2.1",
70
+ "node-html-markdown": "^1.3.0",
71
+ "node-telegram-bot-api": "^0.67.0",
72
+ "nodemailer": "^6.10.0",
73
+ "qrcode-terminal": "^0.12.0",
74
+ "remove-markdown": "^0.6.3",
75
+ "vm2": "^3.10.5",
76
+ "zod": "^3.25.76"
77
+ },
78
+ "devDependencies": {
79
+ "husky": "^9.1.7",
80
+ "lint-staged": "^16.4.0",
81
+ "prettier": "^3.8.1"
82
+ }
83
+ }
@@ -1,102 +1,102 @@
1
- /**
2
- * AI 插件
3
- * 提供 AI 对话能力
4
- */
5
-
6
- const { Plugin } = require('../src/core/plugin-base')
7
- const { logger } = require('../src/utils/logger')
8
- const log = logger.child('AIPlugin')
9
- const { createAI } = require('../src/core/provider')
10
-
11
- class AIPlugin extends Plugin {
12
- constructor(config = {}) {
13
- super()
14
- this.name = 'ai'
15
- this.version = '1.0.0'
16
- this.description = 'AI 对话能力插件'
17
- this.priority = 1 // 最先加载
18
- this.system = true
19
-
20
- this.config = {
21
- provider: config.provider || 'deepseek',
22
- model: config.model || 'deepseek-chat',
23
- apiKey: config.apiKey,
24
- baseURL: config.baseURL,
25
- maxSteps: config.maxSteps || 20,
26
- maxOutputTokens:config.maxOutputTokens||parseInt(process.env.MAX_OUTPUT_TOKENS)||8192
27
- }
28
-
29
- this._aiClient = null
30
- this._framework = null
31
- }
32
-
33
- install(framework) {
34
- this._framework = framework
35
- this._initAIClient()
36
- return this
37
- }
38
-
39
- start(framework) {
40
- // AI client 已在 install 中初始化
41
- return this
42
- }
43
-
44
- _initAIClient() {
45
-
46
- if (!this.config.apiKey) {
47
- log.warn(' No API key provided, AI features disabled')
48
- return
49
- }
50
- try {
51
- const provider = createAI({
52
- provider: this.config.provider,
53
- model: this.config.model,
54
- apiKey: this.config.apiKey,
55
- baseURL: this.config.baseURL
56
- })
57
-
58
- // 创建模型实例
59
- this._aiClient = provider(this.config.model)
60
- log.info(` Initialized ${this.config.provider}/${this.config.model}`)
61
- } catch (err) {
62
- log.error(' Failed to initialize AI client:', err.message)
63
- }
64
- }
65
-
66
- /**
67
- * 获取 AI 客户端
68
- * @returns {Object} AI 模型客户端
69
- */
70
- getAIClient() {
71
- return this._aiClient
72
- }
73
-
74
- /**
75
- * 获取配置
76
- * @returns {Object}
77
- */
78
- getConfig() {
79
- return { ...this.config }
80
- }
81
-
82
- reload(framework) {
83
- log.info(' Reloading...')
84
- this._framework = framework
85
- this._initAIClient()
86
-
87
- // 刷新所有已有 Agent 的 AI client
88
- for (const agent of framework._agents || []) {
89
- if (agent._chatHandler) {
90
- agent._chatHandler.setAIClient(this._aiClient)
91
- log.info(` Refreshed AI client for agent: ${agent.name}`)
92
- }
93
- }
94
- }
95
-
96
- uninstall(framework) {
97
- this._aiClient = null
98
- this._framework = null
99
- }
100
- }
101
-
102
- module.exports = AIPlugin
1
+ /**
2
+ * AI 插件
3
+ * 提供 AI 对话能力
4
+ */
5
+
6
+ const { Plugin } = require('../src/core/plugin-base')
7
+ const { logger } = require('../src/utils/logger')
8
+ const log = logger.child('AIPlugin')
9
+ const { createAI } = require('../src/core/provider')
10
+
11
+ class AIPlugin extends Plugin {
12
+ constructor(config = {}) {
13
+ super()
14
+ this.name = 'ai'
15
+ this.version = '1.0.0'
16
+ this.description = 'AI 对话能力插件'
17
+ this.priority = 1 // 最先加载
18
+ this.system = true
19
+
20
+ this.config = {
21
+ provider: config.provider || 'deepseek',
22
+ model: config.model || 'deepseek-chat',
23
+ apiKey: config.apiKey,
24
+ baseURL: config.baseURL,
25
+ maxSteps: config.maxSteps || 20,
26
+ maxOutputTokens:config.maxOutputTokens||parseInt(process.env.MAX_OUTPUT_TOKENS)||8192
27
+ }
28
+
29
+ this._aiClient = null
30
+ this._framework = null
31
+ }
32
+
33
+ install(framework) {
34
+ this._framework = framework
35
+ this._initAIClient()
36
+ return this
37
+ }
38
+
39
+ start(framework) {
40
+ // AI client 已在 install 中初始化
41
+ return this
42
+ }
43
+
44
+ _initAIClient() {
45
+
46
+ if (!this.config.apiKey) {
47
+ log.warn(' No API key provided, AI features disabled')
48
+ return
49
+ }
50
+ try {
51
+ const provider = createAI({
52
+ provider: this.config.provider,
53
+ model: this.config.model,
54
+ apiKey: this.config.apiKey,
55
+ baseURL: this.config.baseURL
56
+ })
57
+
58
+ // 创建模型实例
59
+ this._aiClient = provider(this.config.model)
60
+ log.info(` Initialized ${this.config.provider}/${this.config.model}`)
61
+ } catch (err) {
62
+ log.error(' Failed to initialize AI client:', err.message)
63
+ }
64
+ }
65
+
66
+ /**
67
+ * 获取 AI 客户端
68
+ * @returns {Object} AI 模型客户端
69
+ */
70
+ getAIClient() {
71
+ return this._aiClient
72
+ }
73
+
74
+ /**
75
+ * 获取配置
76
+ * @returns {Object}
77
+ */
78
+ getConfig() {
79
+ return { ...this.config }
80
+ }
81
+
82
+ reload(framework) {
83
+ log.info(' Reloading...')
84
+ this._framework = framework
85
+ this._initAIClient()
86
+
87
+ // 刷新所有已有 Agent 的 AI client
88
+ for (const agent of framework._agents || []) {
89
+ if (agent._chatHandler) {
90
+ agent._chatHandler.setAIClient(this._aiClient)
91
+ log.info(` Refreshed AI client for agent: ${agent.name}`)
92
+ }
93
+ }
94
+ }
95
+
96
+ uninstall(framework) {
97
+ this._aiClient = null
98
+ this._framework = null
99
+ }
100
+ }
101
+
102
+ module.exports = AIPlugin
@@ -1,113 +1,113 @@
1
- /**
2
- * EventWatcher - 订阅框架事件,筛选相关事件
3
- */
4
-
5
- const { logger } = require('../../src/utils/logger')
6
- const { DefaultEvents } = require('./constants')
7
-
8
- const log = logger.child('Ambient:EventWatcher')
9
-
10
- class EventWatcher {
11
- constructor(goalManager, framework) {
12
- this._goalManager = goalManager
13
- this._framework = framework
14
- this._handlers = []
15
- this._relevantEvents = new Set(DefaultEvents)
16
- }
17
-
18
- /**
19
- * 启动事件监听
20
- */
21
- start() {
22
- const events = [
23
- ['tool:result', (data) => this._handleEvent('tool:result', data)],
24
- ['scheduler:reminder', (data) => this._handleEvent('scheduler:reminder', data)],
25
- ['agent:message', (data) => this._handleEvent('agent:message', data)],
26
- ['think:thought_completed', (data) => this._handleEvent('think:thought_completed', data)],
27
- ['scheduler:task_completed', (data) => this._handleEvent('scheduler:task_completed', data)],
28
- ['scheduler:task_failed', (data) => this._handleEvent('scheduler:task_failed', data)],
29
- ['email:received', (data) => this._handleEvent('email:received', data)],
30
- ['webhook:received', (data) => this._handleEvent('webhook:received', data)]
31
- ]
32
-
33
- for (const [event, handler] of events) {
34
- this._framework.on(event, handler)
35
- this._handlers.push({ event, handler })
36
- }
37
- }
38
-
39
- /**
40
- * 停止事件监听
41
- */
42
- stop() {
43
- for (const { event, handler } of this._handlers) {
44
- this._framework.off(event, handler)
45
- }
46
- this._handlers = []
47
- }
48
-
49
- /**
50
- * 处理事件
51
- * @param {string} type - 事件类型
52
- * @param {Object} data - 事件数据
53
- */
54
- _handleEvent(type, data) {
55
- const activeGoals = this._goalManager.getActiveGoals()
56
- log.info(`_handleEvent: type=${type}, activeGoals=${activeGoals.length}`)
57
-
58
- for (const goal of activeGoals) {
59
- // 检查目标条件是否匹配此事件
60
- const isRelevant = this._isRelevantToGoal(goal, type, data)
61
- log.info(`goal=${goal.id}, isRelevant=${isRelevant}, conditions=${JSON.stringify(goal.conditions)}`)
62
-
63
- if (isRelevant) {
64
- // 将 data 展平到事件对象中,方便通过 {{_event.xxx}} 访问
65
- this._goalManager.addEventToGoal(goal.id, { type, ...data })
66
- log.info(`Event added to goal ${goal.id}`)
67
- }
68
- }
69
-
70
- // 检查是否有 pending 目标需要激活
71
- const pendingGoals = this._goalManager.getPendingGoals()
72
- for (const goal of pendingGoals) {
73
- if (this._isRelevantToGoal(goal, type, data)) {
74
- // 激活目标
75
- this._goalManager.activateGoal(goal.id)
76
- // 添加事件
77
- this._goalManager.addEventToGoal(goal.id, { type, ...data })
78
- log.info(`事件触发激活目标: ${goal.id} (事件: ${type})`)
79
- }
80
- }
81
- }
82
-
83
- /**
84
- * 检查事件是否与目标相关
85
- * @param {Object} goal - 目标对象
86
- * @param {string} eventType - 事件类型
87
- * @param {Object} data - 事件数据
88
- * @returns {boolean} 是否相关
89
- */
90
- _isRelevantToGoal(goal, eventType, data) {
91
- // 检查条件中是否有明确的事件类型
92
- if (goal.conditions && goal.conditions.events) {
93
- const allowedEvents = Array.isArray(goal.conditions.events)
94
- ? goal.conditions.events
95
- : [goal.conditions.events]
96
- if (!allowedEvents.includes(eventType)) {
97
- return false
98
- }
99
- }
100
- // 检查工具名称过滤器
101
- if (goal.conditions && goal.conditions.toolNames) {
102
- const toolNames = Array.isArray(goal.conditions.toolNames)
103
- ? goal.conditions.toolNames
104
- : [goal.conditions.toolNames]
105
- if (data && data.toolName && !toolNames.includes(data.toolName)) {
106
- return false
107
- }
108
- }
109
- return true
110
- }
111
- }
112
-
113
- module.exports = { EventWatcher }
1
+ /**
2
+ * EventWatcher - 订阅框架事件,筛选相关事件
3
+ */
4
+
5
+ const { logger } = require('../../src/utils/logger')
6
+ const { DefaultEvents } = require('./constants')
7
+
8
+ const log = logger.child('Ambient:EventWatcher')
9
+
10
+ class EventWatcher {
11
+ constructor(goalManager, framework) {
12
+ this._goalManager = goalManager
13
+ this._framework = framework
14
+ this._handlers = []
15
+ this._relevantEvents = new Set(DefaultEvents)
16
+ }
17
+
18
+ /**
19
+ * 启动事件监听
20
+ */
21
+ start() {
22
+ const events = [
23
+ ['tool:result', (data) => this._handleEvent('tool:result', data)],
24
+ ['scheduler:reminder', (data) => this._handleEvent('scheduler:reminder', data)],
25
+ ['agent:message', (data) => this._handleEvent('agent:message', data)],
26
+ ['think:thought_completed', (data) => this._handleEvent('think:thought_completed', data)],
27
+ ['scheduler:task_completed', (data) => this._handleEvent('scheduler:task_completed', data)],
28
+ ['scheduler:task_failed', (data) => this._handleEvent('scheduler:task_failed', data)],
29
+ ['email:received', (data) => this._handleEvent('email:received', data)],
30
+ ['webhook:received', (data) => this._handleEvent('webhook:received', data)]
31
+ ]
32
+
33
+ for (const [event, handler] of events) {
34
+ this._framework.on(event, handler)
35
+ this._handlers.push({ event, handler })
36
+ }
37
+ }
38
+
39
+ /**
40
+ * 停止事件监听
41
+ */
42
+ stop() {
43
+ for (const { event, handler } of this._handlers) {
44
+ this._framework.off(event, handler)
45
+ }
46
+ this._handlers = []
47
+ }
48
+
49
+ /**
50
+ * 处理事件
51
+ * @param {string} type - 事件类型
52
+ * @param {Object} data - 事件数据
53
+ */
54
+ _handleEvent(type, data) {
55
+ const activeGoals = this._goalManager.getActiveGoals()
56
+ log.info(`_handleEvent: type=${type}, activeGoals=${activeGoals.length}`)
57
+
58
+ for (const goal of activeGoals) {
59
+ // 检查目标条件是否匹配此事件
60
+ const isRelevant = this._isRelevantToGoal(goal, type, data)
61
+ log.info(`goal=${goal.id}, isRelevant=${isRelevant}, conditions=${JSON.stringify(goal.conditions)}`)
62
+
63
+ if (isRelevant) {
64
+ // 将 data 展平到事件对象中,方便通过 {{_event.xxx}} 访问
65
+ this._goalManager.addEventToGoal(goal.id, { type, ...data })
66
+ log.info(`Event added to goal ${goal.id}`)
67
+ }
68
+ }
69
+
70
+ // 检查是否有 pending 目标需要激活
71
+ const pendingGoals = this._goalManager.getPendingGoals()
72
+ for (const goal of pendingGoals) {
73
+ if (this._isRelevantToGoal(goal, type, data)) {
74
+ // 激活目标
75
+ this._goalManager.activateGoal(goal.id)
76
+ // 添加事件
77
+ this._goalManager.addEventToGoal(goal.id, { type, ...data })
78
+ log.info(`事件触发激活目标: ${goal.id} (事件: ${type})`)
79
+ }
80
+ }
81
+ }
82
+
83
+ /**
84
+ * 检查事件是否与目标相关
85
+ * @param {Object} goal - 目标对象
86
+ * @param {string} eventType - 事件类型
87
+ * @param {Object} data - 事件数据
88
+ * @returns {boolean} 是否相关
89
+ */
90
+ _isRelevantToGoal(goal, eventType, data) {
91
+ // 检查条件中是否有明确的事件类型
92
+ if (goal.conditions && goal.conditions.events) {
93
+ const allowedEvents = Array.isArray(goal.conditions.events)
94
+ ? goal.conditions.events
95
+ : [goal.conditions.events]
96
+ if (!allowedEvents.includes(eventType)) {
97
+ return false
98
+ }
99
+ }
100
+ // 检查工具名称过滤器
101
+ if (goal.conditions && goal.conditions.toolNames) {
102
+ const toolNames = Array.isArray(goal.conditions.toolNames)
103
+ ? goal.conditions.toolNames
104
+ : [goal.conditions.toolNames]
105
+ if (data && data.toolName && !toolNames.includes(data.toolName)) {
106
+ return false
107
+ }
108
+ }
109
+ return true
110
+ }
111
+ }
112
+
113
+ module.exports = { EventWatcher }