foliko 1.0.75 → 1.0.77

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 (89) hide show
  1. package/.claude/settings.local.json +159 -157
  2. package/.env.example +3 -1
  3. package/cli/bin/foliko.js +12 -12
  4. package/cli/src/commands/chat.js +143 -143
  5. package/cli/src/commands/list.js +93 -93
  6. package/cli/src/index.js +75 -75
  7. package/cli/src/ui/chat-ui.js +201 -201
  8. package/cli/src/utils/ansi.js +40 -40
  9. package/cli/src/utils/markdown.js +292 -292
  10. package/examples/ambient-example.js +194 -194
  11. package/examples/basic.js +115 -115
  12. package/examples/bootstrap.js +121 -121
  13. package/examples/mcp-example.js +56 -56
  14. package/examples/skill-example.js +49 -49
  15. package/examples/test-chat.js +137 -137
  16. package/examples/test-mcp.js +85 -85
  17. package/examples/test-reload.js +59 -59
  18. package/examples/test-telegram.js +50 -50
  19. package/examples/test-tg-bot.js +45 -45
  20. package/examples/test-tg-simple.js +47 -47
  21. package/examples/test-tg.js +62 -62
  22. package/examples/test-think.js +43 -43
  23. package/examples/test-web-plugin.js +103 -103
  24. package/examples/test-weixin-feishu.js +103 -103
  25. package/examples/workflow.js +158 -158
  26. package/package.json +81 -81
  27. package/plugins/ai-plugin.js +102 -102
  28. package/plugins/ambient-agent/EventWatcher.js +113 -113
  29. package/plugins/ambient-agent/ExplorerLoop.js +640 -640
  30. package/plugins/ambient-agent/GoalManager.js +197 -197
  31. package/plugins/ambient-agent/Reflector.js +95 -95
  32. package/plugins/ambient-agent/StateStore.js +90 -90
  33. package/plugins/ambient-agent/constants.js +101 -101
  34. package/plugins/ambient-agent/index.js +579 -579
  35. package/plugins/audit-plugin.js +187 -187
  36. package/plugins/default-plugins.js +662 -662
  37. package/plugins/email/constants.js +64 -64
  38. package/plugins/email/handlers.js +461 -461
  39. package/plugins/email/index.js +278 -278
  40. package/plugins/email/monitor.js +269 -269
  41. package/plugins/email/parser.js +138 -138
  42. package/plugins/email/reply.js +151 -151
  43. package/plugins/email/utils.js +124 -124
  44. package/plugins/feishu-plugin.js +481 -481
  45. package/plugins/file-system-plugin.js +826 -826
  46. package/plugins/install-plugin.js +199 -199
  47. package/plugins/python-executor-plugin.js +367 -367
  48. package/plugins/python-plugin-loader.js +481 -481
  49. package/plugins/rules-plugin.js +294 -294
  50. package/plugins/scheduler-plugin.js +691 -691
  51. package/plugins/session-plugin.js +369 -369
  52. package/plugins/shell-executor-plugin.js +197 -197
  53. package/plugins/storage-plugin.js +240 -240
  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 +606 -606
  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 +882 -882
  64. package/src/core/agent.js +892 -892
  65. package/src/core/framework.js +465 -465
  66. package/src/core/index.js +19 -19
  67. package/src/core/plugin-base.js +219 -219
  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 +741 -741
  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
  89. package/foliko-1.0.75.tgz +0 -0
@@ -1,45 +1,45 @@
1
- require('dotenv').config();
2
- const { Framework } = require('../src');
3
-
4
- async function main() {
5
- console.log('Starting test...\n');
6
-
7
- const framework = new Framework({ debug: false });
8
-
9
- await framework.bootstrap({
10
- agentDir: process.cwd() + '/.agent',
11
- aiConfig: {
12
- provider: process.env.FOLIKO_PROVIDER || 'minimax',
13
- model: process.env.FOLIKO_MODEL || 'MiniMax-M2.7',
14
- baseURL: process.env.FOLIKO_BASE_URL || 'https://api.minimaxi.com/v1',
15
- apiKey: process.env.MINIMAX_API_KEY || process.env.DEEPSEEK_API_KEY,
16
- },
17
- });
18
-
19
- // 检查主 Agent
20
- const mainAgent = framework._mainAgent;
21
- console.log('Main Agent:', mainAgent ? mainAgent.name : 'NOT FOUND');
22
- console.log('Agent apiKey:', mainAgent?.apiKey ? 'set' : 'NOT SET');
23
- console.log('Agent _chatHandler:', mainAgent?._chatHandler ? 'exists' : 'NOT EXISTS');
24
- console.log(
25
- 'Agent _chatHandler._aiClient:',
26
- mainAgent?._chatHandler?._aiClient ? 'exists' : 'NOT EXISTS'
27
- );
28
-
29
- // 检查 Telegram 插件
30
- const tgPlugin = framework.pluginManager.get('telegram');
31
- console.log('\nTelegram Plugin:', tgPlugin ? 'loaded' : 'NOT LOADED');
32
-
33
- if (tgPlugin) {
34
- console.log('Telegram _bot:', tgPlugin._bot ? 'exists' : 'NOT EXISTS');
35
- }
36
-
37
- console.log('\n========================================');
38
- console.log('Bot is running. Send a message to test!');
39
- console.log('========================================\n');
40
-
41
- // 保持运行
42
- await new Promise(() => {});
43
- }
44
-
45
- main().catch(console.error);
1
+ require('dotenv').config();
2
+ const { Framework } = require('../src');
3
+
4
+ async function main() {
5
+ console.log('Starting test...\n');
6
+
7
+ const framework = new Framework({ debug: false });
8
+
9
+ await framework.bootstrap({
10
+ agentDir: process.cwd() + '/.agent',
11
+ aiConfig: {
12
+ provider: process.env.FOLIKO_PROVIDER || 'minimax',
13
+ model: process.env.FOLIKO_MODEL || 'MiniMax-M2.7',
14
+ baseURL: process.env.FOLIKO_BASE_URL || 'https://api.minimaxi.com/v1',
15
+ apiKey: process.env.MINIMAX_API_KEY || process.env.DEEPSEEK_API_KEY,
16
+ },
17
+ });
18
+
19
+ // 检查主 Agent
20
+ const mainAgent = framework._mainAgent;
21
+ console.log('Main Agent:', mainAgent ? mainAgent.name : 'NOT FOUND');
22
+ console.log('Agent apiKey:', mainAgent?.apiKey ? 'set' : 'NOT SET');
23
+ console.log('Agent _chatHandler:', mainAgent?._chatHandler ? 'exists' : 'NOT EXISTS');
24
+ console.log(
25
+ 'Agent _chatHandler._aiClient:',
26
+ mainAgent?._chatHandler?._aiClient ? 'exists' : 'NOT EXISTS'
27
+ );
28
+
29
+ // 检查 Telegram 插件
30
+ const tgPlugin = framework.pluginManager.get('telegram');
31
+ console.log('\nTelegram Plugin:', tgPlugin ? 'loaded' : 'NOT LOADED');
32
+
33
+ if (tgPlugin) {
34
+ console.log('Telegram _bot:', tgPlugin._bot ? 'exists' : 'NOT EXISTS');
35
+ }
36
+
37
+ console.log('\n========================================');
38
+ console.log('Bot is running. Send a message to test!');
39
+ console.log('========================================\n');
40
+
41
+ // 保持运行
42
+ await new Promise(() => {});
43
+ }
44
+
45
+ main().catch(console.error);
@@ -1,47 +1,47 @@
1
- require('dotenv').config();
2
-
3
- // 添加 .agent/node_modules 到搜索路径
4
- const path = require('path');
5
- const agentNodeModules = path.join(process.cwd(), '.agent', 'node_modules');
6
- if (!module.paths.includes(agentNodeModules)) {
7
- module.paths.unshift(agentNodeModules);
8
- }
9
-
10
- const TelegramBot = require('node-telegram-bot-api');
11
-
12
- const token = process.env.TELEGRAM_BOT_TOKEN;
13
-
14
- console.log('Token:', token ? token.substring(0, 10) + '...' : 'NOT SET');
15
-
16
- if (!token) {
17
- console.log('ERROR: No token!');
18
- process.exit(1);
19
- }
20
-
21
- const bot = new TelegramBot(token, { polling: true });
22
-
23
- console.log('Bot created, starting polling...');
24
-
25
- bot.on('polling_error', (err) => {
26
- console.log('Polling error:', err.message);
27
- });
28
-
29
- bot.on('error', (err) => {
30
- console.log('Error:', err.message);
31
- });
32
-
33
- bot.on('message', (msg) => {
34
- console.log('\n=== MESSAGE RECEIVED ===');
35
- console.log('Chat ID:', msg.chat.id);
36
- console.log('From:', msg.from.username);
37
- console.log('Text:', msg.text);
38
- console.log('========================\n');
39
-
40
- // 回复测试
41
- bot
42
- .sendMessage(msg.chat.id, 'Bot is working! Received: ' + msg.text)
43
- .then(() => console.log('Reply sent'))
44
- .catch((err) => console.log('Reply error:', err.message));
45
- });
46
-
47
- console.log('Waiting for messages...');
1
+ require('dotenv').config();
2
+
3
+ // 添加 .agent/node_modules 到搜索路径
4
+ const path = require('path');
5
+ const agentNodeModules = path.join(process.cwd(), '.agent', 'node_modules');
6
+ if (!module.paths.includes(agentNodeModules)) {
7
+ module.paths.unshift(agentNodeModules);
8
+ }
9
+
10
+ const TelegramBot = require('node-telegram-bot-api');
11
+
12
+ const token = process.env.TELEGRAM_BOT_TOKEN;
13
+
14
+ console.log('Token:', token ? token.substring(0, 10) + '...' : 'NOT SET');
15
+
16
+ if (!token) {
17
+ console.log('ERROR: No token!');
18
+ process.exit(1);
19
+ }
20
+
21
+ const bot = new TelegramBot(token, { polling: true });
22
+
23
+ console.log('Bot created, starting polling...');
24
+
25
+ bot.on('polling_error', (err) => {
26
+ console.log('Polling error:', err.message);
27
+ });
28
+
29
+ bot.on('error', (err) => {
30
+ console.log('Error:', err.message);
31
+ });
32
+
33
+ bot.on('message', (msg) => {
34
+ console.log('\n=== MESSAGE RECEIVED ===');
35
+ console.log('Chat ID:', msg.chat.id);
36
+ console.log('From:', msg.from.username);
37
+ console.log('Text:', msg.text);
38
+ console.log('========================\n');
39
+
40
+ // 回复测试
41
+ bot
42
+ .sendMessage(msg.chat.id, 'Bot is working! Received: ' + msg.text)
43
+ .then(() => console.log('Reply sent'))
44
+ .catch((err) => console.log('Reply error:', err.message));
45
+ });
46
+
47
+ console.log('Waiting for messages...');
@@ -1,62 +1,62 @@
1
- require('dotenv').config();
2
- const { Framework } = require('../src');
3
-
4
- async function main() {
5
- const framework = new Framework({ debug: true });
6
-
7
- await framework.bootstrap({
8
- agentDir: process.cwd() + '/.agent',
9
- aiConfig: {
10
- provider: process.env.FOLIKO_PROVIDER || 'minimax',
11
- model: process.env.FOLIKO_MODEL || 'MiniMax-M2.7',
12
- baseURL: process.env.FOLIKO_BASE_URL || 'https://api.minimaxi.com/v1',
13
- apiKey: process.env.MINIMAX_API_KEY || process.env.DEEPSEEK_API_KEY,
14
- },
15
- });
16
-
17
- // 检查 telegram 插件
18
- const tgPlugin = framework.pluginManager.get('telegram');
19
-
20
- if (!tgPlugin) {
21
- console.log('Telegram plugin NOT loaded!');
22
- await framework.destroy();
23
- return;
24
- }
25
-
26
- console.log('\n=== Telegram Plugin Debug ===');
27
- console.log('Bot token:', tgPlugin.config.botToken ? 'set' : 'NOT set');
28
- console.log('Bot instance:', tgPlugin._bot ? 'exists' : 'NOT exists');
29
- console.log('Group mode:', tgPlugin.config.groupMode);
30
- console.log('===========================\n');
31
-
32
- if (!tgPlugin._bot) {
33
- console.log('Bot is not initialized. Check if TELEGRAM_BOT_TOKEN is valid.');
34
- await framework.destroy();
35
- return;
36
- }
37
-
38
- // 监听所有事件
39
- tgPlugin._bot.on('message', (msg) => {
40
- console.log('\n[MESSAGE RECEIVED]');
41
- console.log(' chat.id:', msg.chat?.id);
42
- console.log(' chat.type:', msg.chat?.type);
43
- console.log(' from.username:', msg.from?.username);
44
- console.log(' text:', msg.text);
45
- console.log('===================\n');
46
- });
47
-
48
- tgPlugin._bot.on('polling_error', (err) => {
49
- console.log('\n[POLLING ERROR]', err.message);
50
- });
51
-
52
- tgPlugin._bot.on('error', (err) => {
53
- console.log('\n[BOT ERROR]', err.message);
54
- });
55
-
56
- console.log('Waiting for messages... Send a message to your bot.\n');
57
-
58
- // 保持运行
59
- await new Promise(() => {});
60
- }
61
-
62
- main().catch(console.error);
1
+ require('dotenv').config();
2
+ const { Framework } = require('../src');
3
+
4
+ async function main() {
5
+ const framework = new Framework({ debug: true });
6
+
7
+ await framework.bootstrap({
8
+ agentDir: process.cwd() + '/.agent',
9
+ aiConfig: {
10
+ provider: process.env.FOLIKO_PROVIDER || 'minimax',
11
+ model: process.env.FOLIKO_MODEL || 'MiniMax-M2.7',
12
+ baseURL: process.env.FOLIKO_BASE_URL || 'https://api.minimaxi.com/v1',
13
+ apiKey: process.env.MINIMAX_API_KEY || process.env.DEEPSEEK_API_KEY,
14
+ },
15
+ });
16
+
17
+ // 检查 telegram 插件
18
+ const tgPlugin = framework.pluginManager.get('telegram');
19
+
20
+ if (!tgPlugin) {
21
+ console.log('Telegram plugin NOT loaded!');
22
+ await framework.destroy();
23
+ return;
24
+ }
25
+
26
+ console.log('\n=== Telegram Plugin Debug ===');
27
+ console.log('Bot token:', tgPlugin.config.botToken ? 'set' : 'NOT set');
28
+ console.log('Bot instance:', tgPlugin._bot ? 'exists' : 'NOT exists');
29
+ console.log('Group mode:', tgPlugin.config.groupMode);
30
+ console.log('===========================\n');
31
+
32
+ if (!tgPlugin._bot) {
33
+ console.log('Bot is not initialized. Check if TELEGRAM_BOT_TOKEN is valid.');
34
+ await framework.destroy();
35
+ return;
36
+ }
37
+
38
+ // 监听所有事件
39
+ tgPlugin._bot.on('message', (msg) => {
40
+ console.log('\n[MESSAGE RECEIVED]');
41
+ console.log(' chat.id:', msg.chat?.id);
42
+ console.log(' chat.type:', msg.chat?.type);
43
+ console.log(' from.username:', msg.from?.username);
44
+ console.log(' text:', msg.text);
45
+ console.log('===================\n');
46
+ });
47
+
48
+ tgPlugin._bot.on('polling_error', (err) => {
49
+ console.log('\n[POLLING ERROR]', err.message);
50
+ });
51
+
52
+ tgPlugin._bot.on('error', (err) => {
53
+ console.log('\n[BOT ERROR]', err.message);
54
+ });
55
+
56
+ console.log('Waiting for messages... Send a message to your bot.\n');
57
+
58
+ // 保持运行
59
+ await new Promise(() => {});
60
+ }
61
+
62
+ main().catch(console.error);
@@ -1,43 +1,43 @@
1
- require('dotenv').config();
2
- const { Framework } = require('../src');
3
-
4
- async function main() {
5
- const framework = new Framework({ debug: false });
6
-
7
- await framework.bootstrap({
8
- agentDir: process.cwd() + '/.agent',
9
- aiConfig: {
10
- provider: process.env.FOLIKO_PROVIDER || 'minimax',
11
- model: process.env.FOLIKO_MODEL || 'MiniMax-M2.7',
12
- baseURL: process.env.FOLIKO_BASE_URL || 'https://api.minimaxi.com/v1',
13
- apiKey: process.env.MINIMAX_API_KEY || process.env.DEEPSEEK_API_KEY,
14
- },
15
- });
16
-
17
- const agent = framework.createAgent({
18
- name: 'FolikoAgent',
19
- systemPrompt: '你是一个有帮助的助手。',
20
- });
21
-
22
- console.log(
23
- '_mainAgent provider:',
24
- framework._mainAgent ? framework._mainAgent.provider : 'none'
25
- );
26
- console.log(
27
- '_mainAgent has apiKey:',
28
- framework._mainAgent && framework._mainAgent.apiKey ? 'yes' : 'no'
29
- );
30
-
31
- const tools = framework.getTools();
32
- const thinkTool = tools.find((t) => t.name === 'think_now');
33
-
34
- if (thinkTool) {
35
- const result = await thinkTool.execute({ mode: 'reflect', topic: '测试' });
36
- console.log('think_now success:', result.success);
37
- if (result.success === false) console.log('Error:', result.error);
38
- }
39
-
40
- await framework.destroy();
41
- }
42
-
43
- main().catch(console.error);
1
+ require('dotenv').config();
2
+ const { Framework } = require('../src');
3
+
4
+ async function main() {
5
+ const framework = new Framework({ debug: false });
6
+
7
+ await framework.bootstrap({
8
+ agentDir: process.cwd() + '/.agent',
9
+ aiConfig: {
10
+ provider: process.env.FOLIKO_PROVIDER || 'minimax',
11
+ model: process.env.FOLIKO_MODEL || 'MiniMax-M2.7',
12
+ baseURL: process.env.FOLIKO_BASE_URL || 'https://api.minimaxi.com/v1',
13
+ apiKey: process.env.MINIMAX_API_KEY || process.env.DEEPSEEK_API_KEY,
14
+ },
15
+ });
16
+
17
+ const agent = framework.createAgent({
18
+ name: 'FolikoAgent',
19
+ systemPrompt: '你是一个有帮助的助手。',
20
+ });
21
+
22
+ console.log(
23
+ '_mainAgent provider:',
24
+ framework._mainAgent ? framework._mainAgent.provider : 'none'
25
+ );
26
+ console.log(
27
+ '_mainAgent has apiKey:',
28
+ framework._mainAgent && framework._mainAgent.apiKey ? 'yes' : 'no'
29
+ );
30
+
31
+ const tools = framework.getTools();
32
+ const thinkTool = tools.find((t) => t.name === 'think_now');
33
+
34
+ if (thinkTool) {
35
+ const result = await thinkTool.execute({ mode: 'reflect', topic: '测试' });
36
+ console.log('think_now success:', result.success);
37
+ if (result.success === false) console.log('Error:', result.error);
38
+ }
39
+
40
+ await framework.destroy();
41
+ }
42
+
43
+ main().catch(console.error);
@@ -1,103 +1,103 @@
1
- /**
2
- * Web 插件测试示例
3
- * 测试路由注册和请求响应
4
- */
5
-
6
- const { WebPlugin } = require('../plugins/web-plugin');
7
-
8
- async function test() {
9
- console.log('=== Web 插件测试 ===\n');
10
-
11
- // 1. 创建插件
12
- const plugin = new WebPlugin();
13
- const mockFramework = {
14
- registerTool: (tool) => console.log(`[工具注册] ${tool.name}`),
15
- _agents: [],
16
- pluginManager: { get: () => null },
17
- };
18
- plugin.install(mockFramework);
19
-
20
- // 2. 启动服务
21
- console.log('\n[1] 启动 Web 服务...');
22
- const startResult = await plugin._startServer(3099);
23
- console.log('启动结果:', startResult.success ? '成功' : '失败');
24
- console.log('服务地址: http://localhost:3099\n');
25
-
26
- // 3. 注册测试路由
27
- console.log('[2] 注册测试路由...');
28
-
29
- // 返回固定字符串
30
- await plugin._registerRoute('GET', '/test', 'return "1232432"', '返回固定字符串');
31
- console.log('注册: GET /test -> return "1232432"');
32
-
33
- // 返回 JSON
34
- await plugin._registerRoute('GET', '/json', 'return { hello: "world", code: 200 }', '返回 JSON');
35
- console.log('注册: GET /json -> return { hello: "world", code: 200 }');
36
-
37
- // 带参数
38
- await plugin._registerRoute(
39
- 'GET',
40
- '/user/:id',
41
- 'return { userId: context.params.id }',
42
- '路径参数'
43
- );
44
- console.log('注册: GET /user/:id -> return { userId: context.params.id }');
45
-
46
- // POST 路由
47
- await plugin._registerRoute('POST', '/echo', 'return { received: context.body }', '回显 body');
48
- console.log('注册: POST /echo -> return { received: context.body }');
49
-
50
- // 4. 等待服务就绪
51
- await new Promise((r) => setTimeout(r, 500));
52
-
53
- // 5. 测试各路由
54
- console.log('\n[3] 测试路由响应...\n');
55
-
56
- // GET /test
57
- console.log('GET /test:');
58
- const r1 = await plugin._sendRequest('GET', '/test');
59
- console.log(' 状态:', r1.status);
60
- console.log(' 响应:', JSON.stringify(r1.body));
61
- console.log();
62
-
63
- // GET /json
64
- console.log('GET /json:');
65
- const r2 = await plugin._sendRequest('GET', '/json');
66
- console.log(' 状态:', r2.status);
67
- console.log(' 响应:', JSON.stringify(r2.body));
68
- console.log();
69
-
70
- // GET /user/123
71
- console.log('GET /user/123:');
72
- const r3 = await plugin._sendRequest('GET', '/user/123');
73
- console.log(' 状态:', r3.status);
74
- console.log(' 响应:', JSON.stringify(r3.body));
75
- console.log();
76
-
77
- // POST /echo
78
- console.log('POST /echo (body: { name: "test" }):');
79
- const r4 = await plugin._sendRequest('POST', '/echo', { name: 'test' });
80
- console.log(' 状态:', r4.status);
81
- console.log(' 响应:', JSON.stringify(r4.body));
82
- console.log();
83
-
84
- // 6. 列出所有路由
85
- console.log('[4] 已注册的路由:');
86
- const routes = plugin._listRoutes();
87
- routes.routes.forEach((r) => {
88
- console.log(` ${r.method} ${r.path} - ${r.description}`);
89
- });
90
- routes.webhooks.forEach((w) => {
91
- console.log(` WEBHOOK ${w.path}`);
92
- });
93
- routes.statics.forEach((s) => {
94
- console.log(` STATIC ${s.path} -> ${s.folder}`);
95
- });
96
-
97
- // 7. 停止服务
98
- console.log('\n[5] 停止服务...');
99
- await plugin._stopServer();
100
- console.log('测试完成!');
101
- }
102
-
103
- test().catch(console.error);
1
+ /**
2
+ * Web 插件测试示例
3
+ * 测试路由注册和请求响应
4
+ */
5
+
6
+ const { WebPlugin } = require('../plugins/web-plugin');
7
+
8
+ async function test() {
9
+ console.log('=== Web 插件测试 ===\n');
10
+
11
+ // 1. 创建插件
12
+ const plugin = new WebPlugin();
13
+ const mockFramework = {
14
+ registerTool: (tool) => console.log(`[工具注册] ${tool.name}`),
15
+ _agents: [],
16
+ pluginManager: { get: () => null },
17
+ };
18
+ plugin.install(mockFramework);
19
+
20
+ // 2. 启动服务
21
+ console.log('\n[1] 启动 Web 服务...');
22
+ const startResult = await plugin._startServer(3099);
23
+ console.log('启动结果:', startResult.success ? '成功' : '失败');
24
+ console.log('服务地址: http://localhost:3099\n');
25
+
26
+ // 3. 注册测试路由
27
+ console.log('[2] 注册测试路由...');
28
+
29
+ // 返回固定字符串
30
+ await plugin._registerRoute('GET', '/test', 'return "1232432"', '返回固定字符串');
31
+ console.log('注册: GET /test -> return "1232432"');
32
+
33
+ // 返回 JSON
34
+ await plugin._registerRoute('GET', '/json', 'return { hello: "world", code: 200 }', '返回 JSON');
35
+ console.log('注册: GET /json -> return { hello: "world", code: 200 }');
36
+
37
+ // 带参数
38
+ await plugin._registerRoute(
39
+ 'GET',
40
+ '/user/:id',
41
+ 'return { userId: context.params.id }',
42
+ '路径参数'
43
+ );
44
+ console.log('注册: GET /user/:id -> return { userId: context.params.id }');
45
+
46
+ // POST 路由
47
+ await plugin._registerRoute('POST', '/echo', 'return { received: context.body }', '回显 body');
48
+ console.log('注册: POST /echo -> return { received: context.body }');
49
+
50
+ // 4. 等待服务就绪
51
+ await new Promise((r) => setTimeout(r, 500));
52
+
53
+ // 5. 测试各路由
54
+ console.log('\n[3] 测试路由响应...\n');
55
+
56
+ // GET /test
57
+ console.log('GET /test:');
58
+ const r1 = await plugin._sendRequest('GET', '/test');
59
+ console.log(' 状态:', r1.status);
60
+ console.log(' 响应:', JSON.stringify(r1.body));
61
+ console.log();
62
+
63
+ // GET /json
64
+ console.log('GET /json:');
65
+ const r2 = await plugin._sendRequest('GET', '/json');
66
+ console.log(' 状态:', r2.status);
67
+ console.log(' 响应:', JSON.stringify(r2.body));
68
+ console.log();
69
+
70
+ // GET /user/123
71
+ console.log('GET /user/123:');
72
+ const r3 = await plugin._sendRequest('GET', '/user/123');
73
+ console.log(' 状态:', r3.status);
74
+ console.log(' 响应:', JSON.stringify(r3.body));
75
+ console.log();
76
+
77
+ // POST /echo
78
+ console.log('POST /echo (body: { name: "test" }):');
79
+ const r4 = await plugin._sendRequest('POST', '/echo', { name: 'test' });
80
+ console.log(' 状态:', r4.status);
81
+ console.log(' 响应:', JSON.stringify(r4.body));
82
+ console.log();
83
+
84
+ // 6. 列出所有路由
85
+ console.log('[4] 已注册的路由:');
86
+ const routes = plugin._listRoutes();
87
+ routes.routes.forEach((r) => {
88
+ console.log(` ${r.method} ${r.path} - ${r.description}`);
89
+ });
90
+ routes.webhooks.forEach((w) => {
91
+ console.log(` WEBHOOK ${w.path}`);
92
+ });
93
+ routes.statics.forEach((s) => {
94
+ console.log(` STATIC ${s.path} -> ${s.folder}`);
95
+ });
96
+
97
+ // 7. 停止服务
98
+ console.log('\n[5] 停止服务...');
99
+ await plugin._stopServer();
100
+ console.log('测试完成!');
101
+ }
102
+
103
+ test().catch(console.error);