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.
Files changed (54) hide show
  1. package/.claude/settings.local.json +30 -0
  2. package/22.txt +10 -0
  3. package/README.md +218 -0
  4. package/SPEC.md +452 -0
  5. package/cli/bin/foliko.js +12 -0
  6. package/cli/src/commands/chat.js +75 -0
  7. package/cli/src/index.js +64 -0
  8. package/cli/src/ui/chat-ui.js +272 -0
  9. package/cli/src/utils/ansi.js +40 -0
  10. package/cli/src/utils/markdown.js +296 -0
  11. package/docs/quick-reference.md +131 -0
  12. package/docs/user-manual.md +1205 -0
  13. package/examples/basic.js +110 -0
  14. package/examples/bootstrap.js +93 -0
  15. package/examples/mcp-example.js +53 -0
  16. package/examples/skill-example.js +49 -0
  17. package/examples/workflow.js +158 -0
  18. package/package.json +36 -0
  19. package/plugins/ai-plugin.js +89 -0
  20. package/plugins/audit-plugin.js +187 -0
  21. package/plugins/default-plugins.js +412 -0
  22. package/plugins/file-system-plugin.js +344 -0
  23. package/plugins/install-plugin.js +93 -0
  24. package/plugins/python-executor-plugin.js +331 -0
  25. package/plugins/rules-plugin.js +292 -0
  26. package/plugins/scheduler-plugin.js +426 -0
  27. package/plugins/session-plugin.js +343 -0
  28. package/plugins/shell-executor-plugin.js +196 -0
  29. package/plugins/storage-plugin.js +237 -0
  30. package/plugins/subagent-plugin.js +395 -0
  31. package/plugins/think-plugin.js +329 -0
  32. package/plugins/tools-plugin.js +114 -0
  33. package/skills/mcp-usage/SKILL.md +198 -0
  34. package/skills/vb-agent-dev/AGENTS.md +162 -0
  35. package/skills/vb-agent-dev/SKILL.md +370 -0
  36. package/src/capabilities/index.js +11 -0
  37. package/src/capabilities/skill-manager.js +319 -0
  38. package/src/capabilities/workflow-engine.js +401 -0
  39. package/src/core/agent-chat.js +311 -0
  40. package/src/core/agent.js +573 -0
  41. package/src/core/framework.js +255 -0
  42. package/src/core/index.js +19 -0
  43. package/src/core/plugin-base.js +205 -0
  44. package/src/core/plugin-manager.js +392 -0
  45. package/src/core/provider.js +108 -0
  46. package/src/core/tool-registry.js +134 -0
  47. package/src/core/tool-router.js +216 -0
  48. package/src/executors/executor-base.js +58 -0
  49. package/src/executors/mcp-executor.js +728 -0
  50. package/src/index.js +37 -0
  51. package/src/utils/event-emitter.js +97 -0
  52. package/test-chat.js +129 -0
  53. package/test-mcp.js +79 -0
  54. package/test-reload.js +61 -0
@@ -0,0 +1,255 @@
1
+ /**
2
+ * Framework 核心容器
3
+ * 管理所有子系统,提供统一入口
4
+ */
5
+
6
+ const path = require('path')
7
+ const { EventEmitter } = require('../utils/event-emitter')
8
+ const { PluginManager } = require('./plugin-manager')
9
+ const { ToolRegistry } = require('./tool-registry')
10
+ const { Agent } = require('./agent')
11
+
12
+ // 添加框架目录的 node_modules 到全局搜索路径
13
+ // 让项目插件能 require 框架内置的包(如 zod)
14
+ const frameworkNodeModules = path.join(__dirname, '..', '..', 'node_modules')
15
+ if (!module.paths.includes(frameworkNodeModules)) {
16
+ module.paths.unshift(frameworkNodeModules)
17
+ }
18
+
19
+ class Framework extends EventEmitter {
20
+ /**
21
+ * @param {Object} config - 配置
22
+ * @param {boolean} [config.debug=false] - 调试模式
23
+ */
24
+ constructor(config = {}) {
25
+ super()
26
+
27
+ this._debug = config.debug || false
28
+ this._ready = false
29
+ this._readyPromise = null
30
+ this._resolveReady = null
31
+
32
+ // 初始化子系统
33
+ this.toolRegistry = new ToolRegistry()
34
+ this.pluginManager = new PluginManager(this)
35
+
36
+ // Agent 管理
37
+ this._agents = [] // 所有创建的 agent
38
+ this._mainAgent = null // 主 agent
39
+
40
+ // 事件转发
41
+ this.toolRegistry.on('tool:registered', (tool) => {
42
+ this.emit('tool:registered', tool)
43
+ })
44
+
45
+ this._registerBuiltinTools()
46
+ }
47
+
48
+ /**
49
+ * 注册插件(不加载)
50
+ * @param {Plugin|Object} plugin - 插件
51
+ */
52
+ registerPlugin(plugin) {
53
+ this.pluginManager.register(plugin)
54
+ return this
55
+ }
56
+
57
+ /**
58
+ * 加载并启动插件
59
+ * @param {Plugin|Object} plugin - 插件
60
+ */
61
+ async loadPlugin(plugin) {
62
+ await this.pluginManager.load(plugin)
63
+ return this
64
+ }
65
+
66
+ /**
67
+ * 卸载插件
68
+ * @param {string} name - 插件名称
69
+ */
70
+ async unloadPlugin(name) {
71
+ await this.pluginManager.unload(name)
72
+ return this
73
+ }
74
+
75
+ /**
76
+ * 重载单个插件
77
+ * @param {string} name - 插件名称
78
+ */
79
+ async reloadPlugin(name) {
80
+ await this.pluginManager.reload(name)
81
+ return this
82
+ }
83
+
84
+ /**
85
+ * 重载所有插件
86
+ */
87
+ async reloadAllPlugins() {
88
+ await this.pluginManager.reloadAll()
89
+ return this
90
+ }
91
+
92
+ /**
93
+ * 注册工具
94
+ * @param {Object} tool - 工具定义
95
+ */
96
+ registerTool(tool) {
97
+ this.toolRegistry.register(tool)
98
+ return this
99
+ }
100
+
101
+ /**
102
+ * 获取所有工具
103
+ * @returns {Array<Object>}
104
+ */
105
+ getTools() {
106
+ return this.toolRegistry.getAll()
107
+ }
108
+
109
+ /**
110
+ * 执行工具
111
+ * @param {string} name - 工具名称
112
+ * @param {Object} args - 参数
113
+ */
114
+ async executeTool(name, args) {
115
+ return this.toolRegistry.execute(name, args, this)
116
+ }
117
+
118
+ /**
119
+ * 创建 Agent
120
+ * @param {Object} config - Agent 配置
121
+ */
122
+ createAgent(config) {
123
+ // 如果配置了 AI 相关参数但没有 apiKey,尝试从 AI 插件获取
124
+ if (!config.apiKey) {
125
+ const aiPlugin = this.pluginManager.get('ai')
126
+ if (aiPlugin) {
127
+ config.apiKey = aiPlugin.config.apiKey
128
+ config.provider = config.provider || aiPlugin.config.provider
129
+ config.model = config.model || aiPlugin.config.model
130
+ config.baseURL = config.baseURL || aiPlugin.config.baseURL
131
+ }
132
+ }
133
+
134
+ const agent = new Agent(this, config)
135
+
136
+ // 跟踪 agent
137
+ this._agents.push(agent)
138
+ if (!this._mainAgent) {
139
+ this._mainAgent = agent
140
+ }
141
+
142
+ this.emit('agent:created', agent)
143
+ return agent
144
+ }
145
+
146
+ /**
147
+ * 获取 AI 插件
148
+ * @returns {Object|undefined}
149
+ */
150
+ getAIPlugin() {
151
+ return this.pluginManager.get('ai')
152
+ }
153
+
154
+ /**
155
+ * Bootstrap - 使用默认配置启动框架
156
+ * 自动加载 .agent/ 目录下的配置和所有默认插件
157
+ * @param {Object} config - 配置
158
+ * @param {string} [config.agentDir='.agent'] - Agent 配置目录
159
+ * @param {Object} [config.aiConfig] - AI 配置(可选,覆盖文件配置)
160
+ * @returns {Promise<Framework>}
161
+ */
162
+ async bootstrap(config = {}) {
163
+ const { bootstrapDefaults, DefaultPlugins, loadAgentConfig } = require('../../plugins/default-plugins')
164
+
165
+ // 先加载默认插件配置
166
+ const defaultsPlugin = new DefaultPlugins({
167
+ agentDir: config.agentDir || '.agent'
168
+ })
169
+
170
+ await this.loadPlugin(defaultsPlugin)
171
+
172
+ // 获取配置
173
+ const agentConfig = defaultsPlugin.getConfig()
174
+
175
+ // 如果提供了 AI 配置,覆盖文件配置
176
+ if (config.aiConfig) {
177
+ agentConfig.ai = { ...agentConfig.ai, ...config.aiConfig }
178
+ }
179
+
180
+ // 加载所有默认插件(传入已加载的配置避免重复加载)
181
+ await bootstrapDefaults(this, { _config: agentConfig, _skipConfigLoad: true })
182
+
183
+ this._setReady()
184
+ return this
185
+ }
186
+
187
+ /**
188
+ * 等待框架就绪
189
+ */
190
+ async ready() {
191
+ if (this._ready) {
192
+ return this
193
+ }
194
+
195
+ if (!this._readyPromise) {
196
+ this._readyPromise = new Promise((resolve) => {
197
+ this._resolveReady = resolve
198
+ })
199
+ }
200
+
201
+ await this._readyPromise
202
+ return this
203
+ }
204
+
205
+ /**
206
+ * 标记框架就绪
207
+ * @private
208
+ */
209
+ _setReady() {
210
+ this._ready = true
211
+ if (this._resolveReady) {
212
+ this._resolveReady(this)
213
+ }
214
+ this.emit('framework:ready', this)
215
+ }
216
+
217
+ /**
218
+ * 调试日志
219
+ * @private
220
+ */
221
+ _log(...args) {
222
+ if (this._debug) {
223
+ console.log('[Framework]', ...args)
224
+ }
225
+ }
226
+
227
+ /**
228
+ * 注册内置工具
229
+ * @private
230
+ */
231
+ _registerBuiltinTools() {
232
+ // 内置工具会在 Agent 创建时添加
233
+ }
234
+
235
+ /**
236
+ * 销毁框架
237
+ */
238
+ async destroy() {
239
+ // 卸载所有插件
240
+ const plugins = this.pluginManager.getAll()
241
+ for (const { name } of plugins) {
242
+ await this.pluginManager.unload(name)
243
+ }
244
+
245
+ // 清空工具
246
+ this.toolRegistry.clear()
247
+
248
+ // 清空事件
249
+ this.removeAllListeners()
250
+
251
+ this.emit('framework:destroyed')
252
+ }
253
+ }
254
+
255
+ module.exports = { Framework }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * 核心模块统一导出
3
+ */
4
+
5
+ const { Framework } = require('./framework')
6
+ const { Agent } = require('./agent')
7
+ const { Plugin } = require('./plugin-base')
8
+ const { PluginManager } = require('./plugin-manager')
9
+ const { ToolRegistry } = require('./tool-registry')
10
+ const { EventEmitter } = require('../utils/event-emitter')
11
+
12
+ module.exports = {
13
+ Framework,
14
+ Agent,
15
+ Plugin,
16
+ PluginManager,
17
+ ToolRegistry,
18
+ EventEmitter
19
+ }
@@ -0,0 +1,205 @@
1
+ /**
2
+ * Plugin 基类
3
+ * 所有插件的基类
4
+ */
5
+
6
+ class Plugin {
7
+ /**
8
+ * 插件名称
9
+ * @type {string}
10
+ */
11
+ name = 'unnamed-plugin'
12
+
13
+ /**
14
+ * 插件版本
15
+ * @type {string}
16
+ */
17
+ version = '1.0.0'
18
+
19
+ /**
20
+ * 插件描述
21
+ * @type {string}
22
+ */
23
+ description = ''
24
+
25
+ /**
26
+ * 插件优先级,数值越小越先加载
27
+ * @type {number}
28
+ */
29
+ priority = 100
30
+
31
+ /**
32
+ * 子Agent配置数组 - 配置后自动注册子Agent
33
+ * 格式: [{ name: 'xxx', role: '角色', tools: {...}, parentTools: [...] }]
34
+ * @type {Array}
35
+ */
36
+ agents = []
37
+
38
+ _framework = null
39
+ _subAgents = []
40
+
41
+ /**
42
+ * 安装插件 - 注册工具/事件等
43
+ * @param {Framework} framework - 框架实例
44
+ */
45
+ install(framework) {
46
+ this._framework = framework
47
+ return this
48
+ }
49
+
50
+ /**
51
+ * 启动插件 - 初始化完成后的启动
52
+ * @param {Framework} framework - 框架实例
53
+ */
54
+ start(framework) {
55
+ // 自动注册配置的子Agent
56
+ this._autoRegisterSubAgents()
57
+ return this
58
+ }
59
+
60
+ /**
61
+ * 自动注册子Agent(根据 this.agents 配置)
62
+ * @private
63
+ */
64
+ _autoRegisterSubAgents() {
65
+ // 确定要注册的agent配置
66
+ const agentsToRegister = this._deferredSubAgents || this.agents
67
+ if (!agentsToRegister || !Array.isArray(agentsToRegister) || agentsToRegister.length === 0) {
68
+ return
69
+ }
70
+
71
+ // 获取父Agent
72
+ const parentAgent = this._getParentAgent()
73
+
74
+ if (!parentAgent) {
75
+ // 没有父Agent,推迟到框架准备好时再注册
76
+ if (!this._deferredSubAgents) {
77
+ console.log(`[Plugin:${this.name}] No parent agent found, deferring subagent registration`)
78
+ this._deferredSubAgents = agentsToRegister
79
+ // 监听框架准备好事件
80
+ if (this._framework) {
81
+ this._framework.once('framework:ready', () => {
82
+ // 延迟注册,确保主Agent已创建
83
+ setTimeout(() => this._autoRegisterSubAgents(), 100)
84
+ })
85
+ }
86
+ }
87
+ return
88
+ }
89
+
90
+ // 有父Agent了,清除延迟标记
91
+ this._deferredSubAgents = null
92
+
93
+ for (const agentConfig of agentsToRegister) {
94
+ try {
95
+ const plugin = this.registerSubAgent(agentConfig)
96
+ this._subAgents.push(plugin)
97
+ } catch (err) {
98
+ console.error(`[Plugin:${this.name}] Failed to register subagent ${agentConfig.name}:`, err.message)
99
+ }
100
+ }
101
+ }
102
+
103
+ /**
104
+ * 获取父Agent
105
+ * @private
106
+ */
107
+ _getParentAgent() {
108
+ if (!this._framework) return null
109
+ if (this._framework._mainAgent) return this._framework._mainAgent
110
+ const agents = this._framework._agents || []
111
+ return agents.length > 0 ? agents[agents.length - 1] : null
112
+ }
113
+
114
+ /**
115
+ * 注册子Agent
116
+ * @param {Object} config - 子Agent配置
117
+ * @param {string} config.name - 子Agent名称
118
+ * @param {string} config.role - 子Agent角色
119
+ * @param {string} [config.description] - 子Agent描述
120
+ * @param {Object} [config.tools] - 自定义工具 { name: toolDef }
121
+ * @param {string[]} [config.parentTools] - 从父Agent继承的工具名称
122
+ * @returns {SubAgentPlugin} 注册的子Agent插件实例
123
+ */
124
+ registerSubAgent(config) {
125
+ if (!this._framework) {
126
+ throw new Error('Plugin not installed, call install(framework) first')
127
+ }
128
+
129
+ // 动态导入避免循环依赖
130
+ const { SubAgentPlugin } = require('../../plugins/subagent-plugin')
131
+
132
+ const plugin = new SubAgentPlugin({
133
+ ...config,
134
+ // 确保子Agent能获取父Agent
135
+ _parentAgentGetter: () => {
136
+ // 尝试获取主Agent
137
+ if (this._framework._mainAgent) {
138
+ return this._framework._mainAgent
139
+ }
140
+ const agents = this._framework._agents || []
141
+ return agents.length > 0 ? agents[agents.length - 1] : null
142
+ }
143
+ })
144
+
145
+ plugin.install(this._framework)
146
+ plugin.start(this._framework)
147
+
148
+ console.log(`[Plugin:${this.name}] Registered subagent: ${config.name}`)
149
+ return plugin
150
+ }
151
+
152
+ /**
153
+ * 热重载插件 - 手动调用重载时执行
154
+ * @param {Framework} framework - 框架实例
155
+ */
156
+ reload(framework) {
157
+ this._framework = framework
158
+ // 清除之前的延迟注册
159
+ this._deferredSubAgents = null
160
+ // 重新注册子Agent
161
+ this._autoRegisterSubAgents()
162
+ }
163
+
164
+ /**
165
+ * 卸载插件 - 清理资源
166
+ * @param {Framework} framework - 框架实例
167
+ */
168
+ uninstall(framework) {
169
+ // 销毁所有子Agent
170
+ for (const plugin of this._subAgents) {
171
+ try {
172
+ plugin.uninstall(framework)
173
+ } catch (err) {
174
+ console.error(`[Plugin:${this.name}] Failed to uninstall subagent:`, err.message)
175
+ }
176
+ }
177
+ this._subAgents = []
178
+ this._framework = null
179
+ }
180
+
181
+ /**
182
+ * 获取插件信息
183
+ * @returns {Object}
184
+ */
185
+ getInfo() {
186
+ return {
187
+ name: this.name,
188
+ version: this.version,
189
+ description: this.description,
190
+ priority: this.priority
191
+ }
192
+ }
193
+ }
194
+
195
+ /**
196
+ * 内置插件类型常量
197
+ */
198
+ const PluginType = {
199
+ AI: 'ai',
200
+ TOOLS: 'tools',
201
+ EXECUTOR: 'executor',
202
+ CAPABILITY: 'capability'
203
+ }
204
+
205
+ module.exports = { Plugin, PluginType }