foliko 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +30 -0
- package/22.txt +10 -0
- package/README.md +218 -0
- package/SPEC.md +452 -0
- package/cli/bin/foliko.js +12 -0
- package/cli/src/commands/chat.js +75 -0
- package/cli/src/index.js +64 -0
- package/cli/src/ui/chat-ui.js +272 -0
- package/cli/src/utils/ansi.js +40 -0
- package/cli/src/utils/markdown.js +296 -0
- package/docs/quick-reference.md +131 -0
- package/docs/user-manual.md +1205 -0
- package/examples/basic.js +110 -0
- package/examples/bootstrap.js +93 -0
- package/examples/mcp-example.js +53 -0
- package/examples/skill-example.js +49 -0
- package/examples/workflow.js +158 -0
- package/package.json +36 -0
- package/plugins/ai-plugin.js +89 -0
- package/plugins/audit-plugin.js +187 -0
- package/plugins/default-plugins.js +412 -0
- package/plugins/file-system-plugin.js +344 -0
- package/plugins/install-plugin.js +93 -0
- package/plugins/python-executor-plugin.js +331 -0
- package/plugins/rules-plugin.js +292 -0
- package/plugins/scheduler-plugin.js +426 -0
- package/plugins/session-plugin.js +343 -0
- package/plugins/shell-executor-plugin.js +196 -0
- package/plugins/storage-plugin.js +237 -0
- package/plugins/subagent-plugin.js +395 -0
- package/plugins/think-plugin.js +329 -0
- package/plugins/tools-plugin.js +114 -0
- package/skills/mcp-usage/SKILL.md +198 -0
- package/skills/vb-agent-dev/AGENTS.md +162 -0
- package/skills/vb-agent-dev/SKILL.md +370 -0
- package/src/capabilities/index.js +11 -0
- package/src/capabilities/skill-manager.js +319 -0
- package/src/capabilities/workflow-engine.js +401 -0
- package/src/core/agent-chat.js +311 -0
- package/src/core/agent.js +573 -0
- package/src/core/framework.js +255 -0
- package/src/core/index.js +19 -0
- package/src/core/plugin-base.js +205 -0
- package/src/core/plugin-manager.js +392 -0
- package/src/core/provider.js +108 -0
- package/src/core/tool-registry.js +134 -0
- package/src/core/tool-router.js +216 -0
- package/src/executors/executor-base.js +58 -0
- package/src/executors/mcp-executor.js +728 -0
- package/src/index.js +37 -0
- package/src/utils/event-emitter.js +97 -0
- package/test-chat.js +129 -0
- package/test-mcp.js +79 -0
- package/test-reload.js +61 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 意图路由工具管理器
|
|
3
|
+
* 根据用户输入智能选择要传递给 LLM 的工具
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const { EventEmitter } = require('../utils/event-emitter')
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 内置意图分类
|
|
10
|
+
*/
|
|
11
|
+
const INTENT_PATTERNS = {
|
|
12
|
+
'file_operation': {
|
|
13
|
+
keywords: ['读取文件', '写文件', '创建文件', '删除文件', '修改文件', '查看文件', 'read', 'write', 'create', 'delete', 'modify', 'file', '文件'],
|
|
14
|
+
tools: ['read_file', 'write_file', 'delete_file', 'modify_file', 'read_directory', 'create_directory', 'search_file']
|
|
15
|
+
},
|
|
16
|
+
'code_development': {
|
|
17
|
+
keywords: ['写代码', '开发', '编程', '创建插件', '编写函数', 'code', 'develop', 'plugin', '编程'],
|
|
18
|
+
tools: ['loadSkill', 'read_file', 'write_file', 'execute_command', 'shell']
|
|
19
|
+
},
|
|
20
|
+
'system_info': {
|
|
21
|
+
keywords: ['系统信息', '内存', 'CPU', '磁盘', '进程', 'system', 'memory', 'cpu', 'disk', 'process', 'info'],
|
|
22
|
+
tools: ['get_system_info', 'get_memory_usage', 'get_cpu_info', 'get_disk_space', 'get_process_info']
|
|
23
|
+
},
|
|
24
|
+
'data_analysis': {
|
|
25
|
+
keywords: ['分析', '数据', '统计', '查询', 'analyze', 'data', 'statistics', 'query'],
|
|
26
|
+
tools: ['execute_command', 'python', 'search_file']
|
|
27
|
+
},
|
|
28
|
+
'plugin_management': {
|
|
29
|
+
keywords: ['插件', '重载', '加载插件', 'plugin', 'reload', 'load plugin'],
|
|
30
|
+
tools: ['reload_plugins', 'list_plugins', 'loadSkill']
|
|
31
|
+
},
|
|
32
|
+
'shell_command': {
|
|
33
|
+
keywords: ['命令', '终端', 'shell', '执行', 'command', 'terminal', 'bash', 'cmd'],
|
|
34
|
+
tools: ['shell', 'powershell', 'execute_command']
|
|
35
|
+
},
|
|
36
|
+
'skill_usage': {
|
|
37
|
+
keywords: ['技能', 'skill', '使用技能', '加载技能'],
|
|
38
|
+
tools: ['loadSkill']
|
|
39
|
+
},
|
|
40
|
+
'scheduling': {
|
|
41
|
+
keywords: ['定时', '提醒', '通知', 'schedule', 'remind', 'notify', 'cron', '任务', '稍后', '天后', '秒后', '分钟后', '小时后', '明天', '今天'],
|
|
42
|
+
tools: ['schedule_cron', 'schedule_once', 'schedule_list', 'schedule_cancel', 'schedule_get_results', 'schedule_notify']
|
|
43
|
+
},
|
|
44
|
+
'thinking': {
|
|
45
|
+
keywords: ['思考', '反思', '想想', 'think', 'reflect', '分析一下', '头脑风暴', '持续思考', '自动思考'],
|
|
46
|
+
tools: ['think_now', 'think_continue', 'think_stop', 'think_get_thoughts']
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class ToolRouter extends EventEmitter {
|
|
51
|
+
constructor(tools = []) {
|
|
52
|
+
super()
|
|
53
|
+
this._tools = new Map()
|
|
54
|
+
this._defaultTools = new Set()
|
|
55
|
+
|
|
56
|
+
// 注册工具
|
|
57
|
+
for (const tool of tools) {
|
|
58
|
+
this.register(tool)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 注册工具
|
|
64
|
+
*/
|
|
65
|
+
register(tool) {
|
|
66
|
+
this._tools.set(tool.name, tool)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 设置默认工具(每个请求都会包含)
|
|
71
|
+
*/
|
|
72
|
+
setDefaultTools(toolNames) {
|
|
73
|
+
this._defaultTools = new Set(toolNames)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 判断是否为默认工具
|
|
78
|
+
*/
|
|
79
|
+
isDefaultTool(name) {
|
|
80
|
+
return this._defaultTools.has(name)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 分析用户输入,识别意图
|
|
85
|
+
*/
|
|
86
|
+
analyzeIntent(userMessage) {
|
|
87
|
+
const message = userMessage.toLowerCase()
|
|
88
|
+
|
|
89
|
+
const matchedIntents = []
|
|
90
|
+
|
|
91
|
+
for (const [intentName, intentConfig] of Object.entries(INTENT_PATTERNS)) {
|
|
92
|
+
let score = 0
|
|
93
|
+
|
|
94
|
+
for (const keyword of intentConfig.keywords) {
|
|
95
|
+
if (message.includes(keyword.toLowerCase())) {
|
|
96
|
+
score++
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (score > 0) {
|
|
101
|
+
matchedIntents.push({
|
|
102
|
+
name: intentName,
|
|
103
|
+
score,
|
|
104
|
+
tools: intentConfig.tools
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// 按分数排序
|
|
110
|
+
matchedIntents.sort((a, b) => b.score - a.score)
|
|
111
|
+
|
|
112
|
+
return matchedIntents
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 获取指定意图对应的工具
|
|
117
|
+
*/
|
|
118
|
+
getToolsForIntent(intent) {
|
|
119
|
+
const result = []
|
|
120
|
+
|
|
121
|
+
for (const toolName of intent.tools) {
|
|
122
|
+
const tool = this._tools.get(toolName)
|
|
123
|
+
if (tool) {
|
|
124
|
+
result.push(tool)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return result
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 为用户消息选择合适的工具
|
|
133
|
+
* @param {string} userMessage - 用户消息
|
|
134
|
+
* @param {Object} options - 选项
|
|
135
|
+
* @param {number} options.maxTools - 最大工具数量(默认10)
|
|
136
|
+
* @param {boolean} options.forceIntent - 强制使用指定意图
|
|
137
|
+
*/
|
|
138
|
+
selectTools(userMessage, options = {}) {
|
|
139
|
+
const { maxTools = 10 } = options
|
|
140
|
+
|
|
141
|
+
// 如果没有工具,直接返回空的
|
|
142
|
+
if (this._tools.size === 0) {
|
|
143
|
+
return []
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 如果消息为空,返回默认工具
|
|
147
|
+
if (!userMessage || userMessage.trim() === '') {
|
|
148
|
+
return this.getDefaultTools()
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 分析意图
|
|
152
|
+
const intents = this.analyzeIntent(userMessage)
|
|
153
|
+
|
|
154
|
+
// 如果没有匹配到意图,返回默认工具
|
|
155
|
+
if (intents.length === 0) {
|
|
156
|
+
return this.getDefaultTools()
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const selectedTools = new Map()
|
|
160
|
+
|
|
161
|
+
// 添加默认工具
|
|
162
|
+
for (const tool of this.getDefaultTools()) {
|
|
163
|
+
selectedTools.set(tool.name, tool)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// 按意图选择工具
|
|
167
|
+
for (const intent of intents) {
|
|
168
|
+
if (selectedTools.size >= maxTools) break
|
|
169
|
+
|
|
170
|
+
const intentTools = this.getToolsForIntent(intent)
|
|
171
|
+
for (const tool of intentTools) {
|
|
172
|
+
if (selectedTools.size >= maxTools) break
|
|
173
|
+
if (!selectedTools.has(tool.name)) {
|
|
174
|
+
selectedTools.set(tool.name, tool)
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return Array.from(selectedTools.values())
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* 获取默认工具
|
|
184
|
+
*/
|
|
185
|
+
getDefaultTools() {
|
|
186
|
+
const result = []
|
|
187
|
+
for (const name of this._defaultTools) {
|
|
188
|
+
const tool = this._tools.get(name)
|
|
189
|
+
if (tool) {
|
|
190
|
+
result.push(tool)
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return result
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 获取所有工具
|
|
198
|
+
*/
|
|
199
|
+
getAllTools() {
|
|
200
|
+
return Array.from(this._tools.values())
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* 按类别获取工具
|
|
205
|
+
*/
|
|
206
|
+
getToolsByCategory(category) {
|
|
207
|
+
const intent = INTENT_PATTERNS[category]
|
|
208
|
+
if (!intent) return []
|
|
209
|
+
|
|
210
|
+
return intent.tools
|
|
211
|
+
.map(name => this._tools.get(name))
|
|
212
|
+
.filter(t => t !== undefined)
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
module.exports = { ToolRouter, INTENT_PATTERNS }
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Executor 基类
|
|
3
|
+
* 执行器的基类,定义执行器接口
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const { EventEmitter } = require('../utils/event-emitter')
|
|
7
|
+
|
|
8
|
+
class ExecutorBase extends EventEmitter {
|
|
9
|
+
/**
|
|
10
|
+
* @param {string} name - 执行器名称
|
|
11
|
+
*/
|
|
12
|
+
constructor(name) {
|
|
13
|
+
super()
|
|
14
|
+
this.name = name
|
|
15
|
+
this._enabled = true
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 执行
|
|
20
|
+
* @param {Object} params - 执行参数
|
|
21
|
+
* @returns {Promise<any>}
|
|
22
|
+
*/
|
|
23
|
+
async execute(params) {
|
|
24
|
+
throw new Error('execute() must be implemented')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 启用执行器
|
|
29
|
+
*/
|
|
30
|
+
enable() {
|
|
31
|
+
this._enabled = true
|
|
32
|
+
return this
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 禁用执行器
|
|
37
|
+
*/
|
|
38
|
+
disable() {
|
|
39
|
+
this._enabled = false
|
|
40
|
+
return this
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 是否启用
|
|
45
|
+
*/
|
|
46
|
+
isEnabled() {
|
|
47
|
+
return this._enabled
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 销毁
|
|
52
|
+
*/
|
|
53
|
+
destroy() {
|
|
54
|
+
this.removeAllListeners()
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = { ExecutorBase }
|