foliko 1.0.74 → 1.0.76

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 (238) hide show
  1. package/.agent/.shared/ui-ux-pro-max/data/charts.csv +26 -0
  2. package/.agent/.shared/ui-ux-pro-max/data/colors.csv +97 -0
  3. package/.agent/.shared/ui-ux-pro-max/data/icons.csv +101 -0
  4. package/.agent/.shared/ui-ux-pro-max/data/landing.csv +31 -0
  5. package/.agent/.shared/ui-ux-pro-max/data/products.csv +97 -0
  6. package/.agent/.shared/ui-ux-pro-max/data/prompts.csv +24 -0
  7. package/.agent/.shared/ui-ux-pro-max/data/react-performance.csv +45 -0
  8. package/.agent/.shared/ui-ux-pro-max/data/stacks/flutter.csv +53 -0
  9. package/.agent/.shared/ui-ux-pro-max/data/stacks/html-tailwind.csv +56 -0
  10. package/.agent/.shared/ui-ux-pro-max/data/stacks/jetpack-compose.csv +53 -0
  11. package/.agent/.shared/ui-ux-pro-max/data/stacks/nextjs.csv +53 -0
  12. package/.agent/.shared/ui-ux-pro-max/data/stacks/nuxt-ui.csv +51 -0
  13. package/.agent/.shared/ui-ux-pro-max/data/stacks/nuxtjs.csv +59 -0
  14. package/.agent/.shared/ui-ux-pro-max/data/stacks/react-native.csv +52 -0
  15. package/.agent/.shared/ui-ux-pro-max/data/stacks/react.csv +54 -0
  16. package/.agent/.shared/ui-ux-pro-max/data/stacks/shadcn.csv +61 -0
  17. package/.agent/.shared/ui-ux-pro-max/data/stacks/svelte.csv +54 -0
  18. package/.agent/.shared/ui-ux-pro-max/data/stacks/swiftui.csv +51 -0
  19. package/.agent/.shared/ui-ux-pro-max/data/stacks/vue.csv +50 -0
  20. package/.agent/.shared/ui-ux-pro-max/data/styles.csv +59 -0
  21. package/.agent/.shared/ui-ux-pro-max/data/typography.csv +58 -0
  22. package/.agent/.shared/ui-ux-pro-max/data/ui-reasoning.csv +101 -0
  23. package/.agent/.shared/ui-ux-pro-max/data/ux-guidelines.csv +100 -0
  24. package/.agent/.shared/ui-ux-pro-max/data/web-interface.csv +31 -0
  25. package/.agent/.shared/ui-ux-pro-max/scripts/__pycache__/core.cpython-313.pyc +0 -0
  26. package/.agent/.shared/ui-ux-pro-max/scripts/__pycache__/design_system.cpython-313.pyc +0 -0
  27. package/.agent/.shared/ui-ux-pro-max/scripts/core.py +258 -0
  28. package/.agent/.shared/ui-ux-pro-max/scripts/design_system.py +1067 -0
  29. package/.agent/.shared/ui-ux-pro-max/scripts/search.py +106 -0
  30. package/.agent/ARCHITECTURE.md +288 -0
  31. package/.agent/agents/ambient-agent.md +57 -0
  32. package/.agent/agents/debugger.md +55 -0
  33. package/.agent/agents/email-assistant.md +49 -0
  34. package/.agent/agents/file-manager.md +42 -0
  35. package/.agent/agents/python-developer.md +60 -0
  36. package/.agent/agents/scheduler.md +59 -0
  37. package/.agent/agents/web-developer.md +45 -0
  38. package/.agent/data/default.json +29 -0
  39. package/.agent/data/plugins-state.json +255 -0
  40. package/.agent/mcp_config.json +4 -0
  41. package/.agent/mcp_config_updated.json +12 -0
  42. package/.agent/plugins.json +5 -0
  43. package/.agent/rules/GEMINI.md +273 -0
  44. package/.agent/rules/allow-rule.md +77 -0
  45. package/.agent/rules/log-rule.md +83 -0
  46. package/.agent/rules/security-rule.md +93 -0
  47. package/.agent/scripts/auto_preview.py +148 -0
  48. package/.agent/scripts/checklist.py +217 -0
  49. package/.agent/scripts/session_manager.py +120 -0
  50. package/.agent/scripts/verify_all.py +327 -0
  51. package/.agent/skills/api-patterns/SKILL.md +81 -0
  52. package/.agent/skills/api-patterns/api-style.md +42 -0
  53. package/.agent/skills/api-patterns/auth.md +24 -0
  54. package/.agent/skills/api-patterns/documentation.md +26 -0
  55. package/.agent/skills/api-patterns/graphql.md +41 -0
  56. package/.agent/skills/api-patterns/rate-limiting.md +31 -0
  57. package/.agent/skills/api-patterns/response.md +37 -0
  58. package/.agent/skills/api-patterns/rest.md +40 -0
  59. package/.agent/skills/api-patterns/scripts/api_validator.py +211 -0
  60. package/.agent/skills/api-patterns/security-testing.md +122 -0
  61. package/.agent/skills/api-patterns/trpc.md +41 -0
  62. package/.agent/skills/api-patterns/versioning.md +22 -0
  63. package/.agent/skills/app-builder/SKILL.md +75 -0
  64. package/.agent/skills/app-builder/agent-coordination.md +71 -0
  65. package/.agent/skills/app-builder/feature-building.md +53 -0
  66. package/.agent/skills/app-builder/project-detection.md +34 -0
  67. package/.agent/skills/app-builder/scaffolding.md +118 -0
  68. package/.agent/skills/app-builder/tech-stack.md +40 -0
  69. package/.agent/skills/app-builder/templates/SKILL.md +39 -0
  70. package/.agent/skills/app-builder/templates/astro-static/TEMPLATE.md +76 -0
  71. package/.agent/skills/app-builder/templates/chrome-extension/TEMPLATE.md +92 -0
  72. package/.agent/skills/app-builder/templates/cli-tool/TEMPLATE.md +88 -0
  73. package/.agent/skills/app-builder/templates/electron-desktop/TEMPLATE.md +88 -0
  74. package/.agent/skills/app-builder/templates/express-api/TEMPLATE.md +83 -0
  75. package/.agent/skills/app-builder/templates/flutter-app/TEMPLATE.md +90 -0
  76. package/.agent/skills/app-builder/templates/monorepo-turborepo/TEMPLATE.md +90 -0
  77. package/.agent/skills/app-builder/templates/nextjs-fullstack/TEMPLATE.md +122 -0
  78. package/.agent/skills/app-builder/templates/nextjs-saas/TEMPLATE.md +122 -0
  79. package/.agent/skills/app-builder/templates/nextjs-static/TEMPLATE.md +169 -0
  80. package/.agent/skills/app-builder/templates/nuxt-app/TEMPLATE.md +134 -0
  81. package/.agent/skills/app-builder/templates/python-fastapi/TEMPLATE.md +83 -0
  82. package/.agent/skills/app-builder/templates/react-native-app/TEMPLATE.md +119 -0
  83. package/.agent/skills/architecture/SKILL.md +55 -0
  84. package/.agent/skills/architecture/context-discovery.md +43 -0
  85. package/.agent/skills/architecture/examples.md +94 -0
  86. package/.agent/skills/architecture/pattern-selection.md +68 -0
  87. package/.agent/skills/architecture/patterns-reference.md +50 -0
  88. package/.agent/skills/architecture/trade-off-analysis.md +77 -0
  89. package/.agent/skills/clean-code/SKILL.md +201 -0
  90. package/.agent/skills/doc.md +177 -0
  91. package/.agent/skills/frontend-design/SKILL.md +418 -0
  92. package/.agent/skills/frontend-design/animation-guide.md +331 -0
  93. package/.agent/skills/frontend-design/color-system.md +311 -0
  94. package/.agent/skills/frontend-design/decision-trees.md +418 -0
  95. package/.agent/skills/frontend-design/motion-graphics.md +306 -0
  96. package/.agent/skills/frontend-design/scripts/accessibility_checker.py +183 -0
  97. package/.agent/skills/frontend-design/scripts/ux_audit.py +722 -0
  98. package/.agent/skills/frontend-design/typography-system.md +345 -0
  99. package/.agent/skills/frontend-design/ux-psychology.md +1116 -0
  100. package/.agent/skills/frontend-design/visual-effects.md +383 -0
  101. package/.agent/skills/i18n-localization/SKILL.md +154 -0
  102. package/.agent/skills/i18n-localization/scripts/i18n_checker.py +241 -0
  103. package/.agent/skills/mcp-builder/SKILL.md +176 -0
  104. package/.agent/skills/web-design-guidelines/SKILL.md +57 -0
  105. package/.agent/workflows/brainstorm.md +113 -0
  106. package/.agent/workflows/create.md +59 -0
  107. package/.agent/workflows/debug.md +103 -0
  108. package/.agent/workflows/deploy.md +176 -0
  109. package/.agent/workflows/enhance.md +63 -0
  110. package/.agent/workflows/orchestrate.md +237 -0
  111. package/.agent/workflows/plan.md +89 -0
  112. package/.agent/workflows/preview.md +81 -0
  113. package/.agent/workflows/simple-test.md +42 -0
  114. package/.agent/workflows/status.md +86 -0
  115. package/.agent/workflows/structured-orchestrate.md +180 -0
  116. package/.agent/workflows/test.md +144 -0
  117. package/.agent/workflows/ui-ux-pro-max.md +296 -0
  118. package/.claude/settings.local.json +11 -1
  119. package/.editorconfig +56 -0
  120. package/.husky/pre-commit +4 -0
  121. package/.lintstagedrc +7 -0
  122. package/.prettierignore +29 -0
  123. package/.prettierrc +11 -0
  124. package/CLAUDE.md +2 -0
  125. package/README.md +64 -55
  126. package/SPEC.md +102 -61
  127. package/cli/bin/foliko.js +11 -11
  128. package/cli/src/commands/chat.js +143 -141
  129. package/cli/src/commands/list.js +93 -90
  130. package/cli/src/index.js +75 -75
  131. package/cli/src/ui/chat-ui.js +201 -199
  132. package/cli/src/utils/ansi.js +40 -40
  133. package/cli/src/utils/markdown.js +292 -296
  134. package/docker-compose.yml +1 -1
  135. package/docs/ai-sdk-optimization.md +655 -643
  136. package/docs/features.md +80 -80
  137. package/docs/quick-reference.md +49 -46
  138. package/docs/user-manual.md +411 -380
  139. package/examples/ambient-example.js +194 -196
  140. package/examples/basic.js +50 -45
  141. package/examples/bootstrap.js +121 -112
  142. package/examples/mcp-example.js +19 -16
  143. package/examples/skill-example.js +20 -20
  144. package/examples/test-chat.js +137 -135
  145. package/examples/test-mcp.js +85 -79
  146. package/examples/test-reload.js +59 -61
  147. package/examples/test-telegram.js +50 -50
  148. package/examples/test-tg-bot.js +45 -42
  149. package/examples/test-tg-simple.js +47 -46
  150. package/examples/test-tg.js +62 -62
  151. package/examples/test-think.js +43 -37
  152. package/examples/test-web-plugin.js +103 -98
  153. package/examples/test-weixin-feishu.js +103 -100
  154. package/examples/workflow.js +158 -158
  155. package/package.json +37 -3
  156. package/plugins/ai-plugin.js +102 -100
  157. package/plugins/ambient-agent/EventWatcher.js +113 -0
  158. package/plugins/ambient-agent/ExplorerLoop.js +640 -0
  159. package/plugins/ambient-agent/GoalManager.js +197 -0
  160. package/plugins/ambient-agent/Reflector.js +95 -0
  161. package/plugins/ambient-agent/StateStore.js +90 -0
  162. package/plugins/ambient-agent/constants.js +101 -0
  163. package/plugins/ambient-agent/index.js +579 -0
  164. package/plugins/audit-plugin.js +187 -187
  165. package/plugins/default-plugins.js +662 -649
  166. package/plugins/email/constants.js +64 -0
  167. package/plugins/email/handlers.js +461 -0
  168. package/plugins/email/index.js +278 -0
  169. package/plugins/email/monitor.js +269 -0
  170. package/plugins/email/parser.js +138 -0
  171. package/plugins/email/reply.js +151 -0
  172. package/plugins/email/utils.js +124 -0
  173. package/plugins/feishu-plugin.js +481 -477
  174. package/plugins/file-system-plugin.js +826 -476
  175. package/plugins/install-plugin.js +199 -197
  176. package/plugins/python-executor-plugin.js +367 -365
  177. package/plugins/python-plugin-loader.js +481 -479
  178. package/plugins/rules-plugin.js +294 -292
  179. package/plugins/scheduler-plugin.js +691 -689
  180. package/plugins/session-plugin.js +369 -367
  181. package/plugins/shell-executor-plugin.js +197 -197
  182. package/plugins/storage-plugin.js +240 -238
  183. package/plugins/subagent-plugin.js +845 -785
  184. package/plugins/telegram-plugin.js +482 -475
  185. package/plugins/think-plugin.js +345 -343
  186. package/plugins/tools-plugin.js +196 -194
  187. package/plugins/web-plugin.js +606 -604
  188. package/plugins/weixin-plugin.js +545 -538
  189. package/reports/system-health-report-20260401.md +79 -0
  190. package/skills/ambient-agent/SKILL.md +49 -39
  191. package/skills/foliko-dev/AGENTS.md +64 -61
  192. package/skills/foliko-dev/SKILL.md +125 -119
  193. package/skills/mcp-usage/SKILL.md +19 -17
  194. package/skills/python-plugin-dev/SKILL.md +16 -15
  195. package/skills/skill-guide/SKILL.md +12 -12
  196. package/skills/subagent-guide/SKILL.md +237 -0
  197. package/skills/workflow-guide/SKILL.md +90 -45
  198. package/skills/workflow-troubleshooting/DEBUGGING.md +36 -21
  199. package/skills/workflow-troubleshooting/SKILL.md +156 -79
  200. package/src/capabilities/index.js +11 -11
  201. package/src/capabilities/skill-manager.js +609 -595
  202. package/src/capabilities/workflow-engine.js +1109 -1195
  203. package/src/core/agent-chat.js +882 -735
  204. package/src/core/agent.js +892 -688
  205. package/src/core/framework.js +465 -431
  206. package/src/core/index.js +19 -19
  207. package/src/core/plugin-base.js +219 -219
  208. package/src/core/plugin-manager.js +863 -767
  209. package/src/core/provider.js +114 -111
  210. package/src/core/sub-agent-config.js +264 -0
  211. package/src/core/system-prompt-builder.js +120 -0
  212. package/src/core/tool-registry.js +517 -134
  213. package/src/core/tool-router.js +297 -216
  214. package/src/executors/executor-base.js +12 -12
  215. package/src/executors/mcp-executor.js +741 -729
  216. package/src/index.js +25 -37
  217. package/src/utils/circuit-breaker.js +301 -0
  218. package/src/utils/error-boundary.js +363 -0
  219. package/src/utils/error.js +374 -0
  220. package/src/utils/event-emitter.js +97 -97
  221. package/src/utils/id.js +133 -0
  222. package/src/utils/index.js +217 -3
  223. package/src/utils/logger.js +181 -0
  224. package/src/utils/plugin-helpers.js +90 -0
  225. package/src/utils/retry.js +122 -0
  226. package/src/utils/sandbox.js +292 -0
  227. package/test/tool-registry-validation.test.js +218 -0
  228. package/test_report.md +70 -0
  229. package/website/docs/api.html +169 -107
  230. package/website/docs/configuration.html +296 -144
  231. package/website/docs/plugin-development.html +154 -85
  232. package/website/docs/project-structure.html +110 -109
  233. package/website/docs/skill-development.html +117 -61
  234. package/website/index.html +209 -205
  235. package/website/script.js +136 -133
  236. package/website/styles.css +1 -1
  237. package/plugins/ambient-agent-plugin.js +0 -1565
  238. package/plugins/email.js +0 -1142
@@ -1,604 +1,606 @@
1
- /**
2
- * Web 服务插件
3
- * 支持 HTTP 服务、路由注册、Webhook(自动生成 /webhook/{id} 链接)
4
- */
5
-
6
- const { Plugin } = require('../src/core/plugin-base')
7
- const { z } = require('zod')
8
- const { serve } = require('@hono/node-server')
9
- const { Hono } = require('hono')
10
- const fs = require('fs')
11
- const path = require('path')
12
-
13
- class WebPlugin extends Plugin {
14
- constructor(config = {}) {
15
- super()
16
- this.name = 'web'
17
- this.version = '3.1.0'
18
- this.description = 'Web 服务插件,支持 HTTP 服务、路由注册、Webhook'
19
- this.priority = 50
20
-
21
- this.system = true
22
-
23
- // 服务器配置
24
- this._port = process.env.WEB_PORT || 3000
25
- this._host = process.env.WEB_HOST || '127.0.0.1'
26
- this._baseUrl = process.env.WEB_BASE_URL || null // 公网可访问的域名
27
-
28
- // 运行时状态
29
- this._server = null
30
- this._app = null
31
- this._framework = null
32
-
33
- // 数据存储(始终保持原始类型)
34
- this._routes = [] // 路由列表
35
- this._webhooks = new Map() // webhook Map: id -> {id, path, prompt, sessionId}
36
- this._statics = [] // 静态文件夹列表
37
- }
38
-
39
- // ==================== 生命周期 ====================
40
-
41
- install(framework) {
42
- this._framework = framework
43
- this._registerTools()
44
-
45
- // 将 WEB_BASE_URL 注入到 framework 的元数据,供所有 agent 使用
46
- if (this._baseUrl && framework._mainAgent) {
47
- framework._mainAgent.setMetadata('WEB_BASE_URL', this._baseUrl)
48
- }
49
-
50
- return this
51
- }
52
-
53
- start() {
54
- return this
55
- }
56
-
57
- reload(framework) {
58
- this._framework = framework
59
- // 重新注入 WEB_BASE_URL
60
- if (this._baseUrl && framework._mainAgent) {
61
- framework._mainAgent.setMetadata('WEB_BASE_URL', this._baseUrl)
62
- }
63
- }
64
-
65
- uninstall() {
66
- this._stopServer()
67
- this._framework = null
68
- }
69
-
70
- // ==================== 工具注册 ====================
71
-
72
- _registerTools() {
73
- // 启动 Web 服务
74
- this._framework.registerTool({
75
- name: 'web_start',
76
- description: '启动 Web 服务',
77
- inputSchema: z.object({
78
- port: z.number().optional().describe('端口号,默认 3000'),
79
- host: z.string().optional().describe('主机地址,默认 0.0.0.0')
80
- }),
81
- execute: async (args) => this._startServer(args.port, args.host)
82
- })
83
-
84
- // 停止 Web 服务
85
- this._framework.registerTool({
86
- name: 'web_stop',
87
- description: '停止 Web 服务',
88
- inputSchema: z.object({}),
89
- execute: async () => this._stopServer()
90
- })
91
-
92
- // 注册 HTTP 路由
93
- this._framework.registerTool({
94
- name: 'web_register_route',
95
- description: '注册 HTTP 路由',
96
- inputSchema: z.object({
97
- method: z.enum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH']).describe('HTTP 方法'),
98
- path: z.string().describe('路由路径,如 /api/user'),
99
- handler: z.string().describe(
100
- '处理逻辑,JavaScript 代码字符串,必须用 return 返回内容。' +
101
- '优先使用 tools.{toolName}(args) 调用工具获取真实数据。' +
102
- '可用变量:context.params, context.query, context.body, tools。' +
103
- '示例:return await tools.get_user({ id: context.params.id })'
104
- ),
105
- description: z.string().optional().describe('路由描述')
106
- }),
107
- execute: async (args) => this._registerRoute(args.method, args.path, args.handler, args.description)
108
- })
109
-
110
- // 注册 Webhook(自动生成 /webhook/{id} 链接)
111
- this._framework.registerTool({
112
- name: 'web_register_webhook',
113
- description: '注册 Webhook,接收的数据会交给 LLM 处理。自动生成唯一 URL',
114
- inputSchema: z.object({
115
- prompt: z.string().optional().describe('提示词,描述如何处理请求'),
116
- awaitResponse: z.boolean().optional().describe('是否等待 LLM 处理完成再返回响应,默认 false')
117
- }),
118
- execute: async (args) => this._registerWebhook(args.prompt, args.awaitResponse)
119
- })
120
-
121
- // 注册静态资源
122
- this._framework.registerTool({
123
- name: 'web_register_static',
124
- description: '注册静态资源文件夹',
125
- inputSchema: z.object({
126
- urlPath: z.string().describe('URL 路径前缀,如 /public'),
127
- folder: z.string().describe('本地文件夹路径,如 ./static'),
128
- options: z.object({
129
- dotfiles: z.enum(['ignore', 'allow', 'deny']).optional(),
130
- index: z.string().optional()
131
- }).optional()
132
- }),
133
- execute: async (args) => this._registerStatic(args.urlPath, args.folder, args.options)
134
- })
135
-
136
- // 列出所有路由
137
- this._framework.registerTool({
138
- name: 'web_list_routes',
139
- description: '列出所有已注册的路由和 Webhook',
140
- inputSchema: z.object({}),
141
- execute: async () => this._listRoutes()
142
- })
143
-
144
- // 发送 HTTP 请求
145
- this._framework.registerTool({
146
- name: 'web_request',
147
- description: '发送 HTTP 请求',
148
- inputSchema: z.object({
149
- method: z.enum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH']).describe('HTTP 方法'),
150
- path: z.string().describe('请求路径'),
151
- body: z.any().optional().describe('请求体'),
152
- headers: z.record(z.string()).optional().describe('请求头')
153
- }),
154
- execute: async (args) => this._sendRequest(args.method, args.path, args.body, args.headers)
155
- })
156
- }
157
-
158
- // ==================== 服务器控制 ====================
159
-
160
- async _startServer(port, host) {
161
- if (this._server) {
162
- return { success: true, message: 'Server already running', port: this._port }
163
- }
164
-
165
- this._port = port || this._port
166
- this._host = host || this._host
167
-
168
- this._app = new Hono()
169
- this._setupMiddleware()
170
-
171
- try {
172
- this._server = serve({
173
- fetch: this._app.fetch,
174
- port: this._port,
175
- hostname: this._host
176
- })
177
- const serverUrl = this._getUrl()
178
- console.log(`[Web] Server started on ${serverUrl}`)
179
- return {
180
- success: true,
181
- message: `Server started on ${serverUrl}`,
182
- // port: this._port,
183
- // host: this._host,
184
- host: serverUrl,
185
- url:serverUrl,
186
- }
187
- } catch (err) {
188
- this._server = null
189
- this._app = null
190
- if (err.code === 'EADDRINUSE') {
191
- return { success: false, error: `Port ${this._port} is already in use` }
192
- }
193
- return { success: false, error: err.message }
194
- }
195
- }
196
-
197
- async _stopServer() {
198
- if (!this._server) {
199
- return { success: true, message: 'Server not running' }
200
- }
201
- this._server.close()
202
- this._server = null
203
- this._app = null
204
- console.log('[Web] Server stopped')
205
- return { success: true, message: 'Server stopped' }
206
- }
207
-
208
- // ==================== 中间件 ====================
209
-
210
- _setupMiddleware() {
211
- this._app.use('*', async (c) => {
212
- const pathname = c.req.path
213
-
214
- // CORS 预检
215
- if (c.req.method === 'OPTIONS') {
216
- return c.text('', 200, {
217
- 'Access-Control-Allow-Origin': '*',
218
- 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
219
- 'Access-Control-Allow-Headers': 'Content-Type, Authorization'
220
- })
221
- }
222
-
223
- // 1. 静态文件
224
- const staticResult = this._serveStatic(pathname)
225
- if (staticResult) {
226
- if (staticResult.type === 'file') {
227
- return c.newResponse(staticResult.content, {
228
- headers: { 'Content-Type': staticResult.contentType }
229
- })
230
- }
231
- if (staticResult.type === 'notFound') {
232
- return c.json({ error: 'Not Found' }, 404)
233
- }
234
- if (staticResult.type === 'forbidden') {
235
- return c.json({ error: 'Forbidden' }, 403)
236
- }
237
- if (staticResult.type === 'error') {
238
- return c.json({ error: staticResult.message }, 500)
239
- }
240
- }
241
-
242
- // 2. Webhook(精确匹配)
243
- const webhook = this._webhooks.get(pathname)
244
- if (webhook) {
245
- const result = await this._handleWebhook(c, webhook)
246
- return c.json(result)
247
- }
248
-
249
- // 3. 路由(支持参数)
250
- for (const route of this._routes) {
251
- if (route.method === c.req.method && this._matchPath(route.path, pathname)) {
252
- return await this._handleRoute(c, route, pathname)
253
- }
254
- }
255
-
256
- // 404
257
- return c.json({ success: false, error: 'Not Found', path: pathname }, 404)
258
- })
259
- }
260
-
261
- // ==================== 请求处理 ====================
262
-
263
- async _handleRoute(c, route, pathname) {
264
- const params = this._extractParams(route.path, pathname)
265
- const query = this._parseQuery(c)
266
- const body = await this._parseBody(c)
267
-
268
- const context = { params, query, body }
269
-
270
- // 构建 tools 代理对象,允许 handler 以函数方式直接调用工具
271
- // 例如: const user = await tools.get_user({ id: 1 })
272
- const registry = this._framework.toolRegistry
273
- const tools = new Proxy({}, {
274
- get: (_, name) => {
275
- if (name === 'call') {
276
- // 保留 call 方式作为后备
277
- return async (toolName, args) => registry.execute(toolName, args || {}, this._framework)
278
- }
279
- return async (args) => registry.execute(name, args || {}, this._framework)
280
- }
281
- })
282
-
283
- const result = await this._executeHandler(route.handler, context, tools)
284
- return c.json(result)
285
- }
286
-
287
- async _handleWebhook(c, webhook) {
288
- const query = this._parseQuery(c)
289
- const body = await this._parseBody(c)
290
- const webhookData = {
291
- path: webhook.path,
292
- method: c.req.method,
293
- query,
294
- body,
295
- timestamp: new Date().toISOString()
296
- }
297
-
298
- // 从执行上下文获取 sessionId
299
- const ctx = this._framework.getExecutionContext()
300
- const sessionId = ctx?.sessionId || null
301
-
302
- // 获取 Agent
303
- const agent = this._getAgent(sessionId)
304
- if (!agent) {
305
- console.error('[Web] No agent available')
306
- return { success: false, error: 'No agent available' }
307
- }
308
-
309
- const prompt = webhook.prompt || '处理以下 webhook 数据,返回适当的响应:'
310
- const finalSessionId = sessionId || `web_${Date.now()}`
311
-
312
- // 触发 webhook 接收事件
313
- this._framework.emit('webhook:received', { webhook, data: webhookData, sessionId: finalSessionId })
314
-
315
- // 使用子Agent处理 webhook
316
- const webhookAgent = this._framework.createSubAgent({
317
- name: 'webhook_handler',
318
- role: 'Webhook处理助手,专注于处理webhook数据并生成适当响应'
319
- })
320
-
321
- if (!webhook.awaitResponse) {
322
- // 不等待,立即返回
323
- webhookAgent.chat(`${prompt}\n\n数据:\n${JSON.stringify(webhookData, null, 2)}`).then(result => {
324
- const responseText = result.message || result.text || ''
325
- console.log(`[Web] Webhook processed (${webhook.path}), LLM response (${responseText.length} chars)`)
326
-
327
- // 添加到 session 历史
328
- if (sessionId) {
329
- const sessionPlugin = this._framework.pluginManager.get('session')
330
- if (sessionPlugin) {
331
- sessionPlugin.addMessage(sessionId, { role: 'user', content: `【Webhook 数据】\n${JSON.stringify(webhookData, null, 2)}` })
332
- sessionPlugin.addMessage(sessionId, { role: 'assistant', content: responseText })
333
- }
334
- }
335
-
336
- // 触发 webhook 处理完成事件
337
- this._framework.emit('webhook:received', { webhook, data: webhookData, response: responseText, sessionId: finalSessionId })
338
- }).catch(err => {
339
- console.error('[Web] Webhook error:', err.message)
340
- })
341
-
342
- return { success: true, message: 'Webhook received, processing in background' }
343
- }
344
-
345
- // 等待 LLM 处理完成
346
- try {
347
- const result = await webhookAgent.chat(`${prompt}\n\n数据:\n${JSON.stringify(webhookData, null, 2)}`)
348
- const responseText = result.message || result.text || ''
349
- console.log(`[Web] Webhook processed (${webhook.path}), LLM response (${responseText.length} chars)`)
350
-
351
- // 添加到 session 历史
352
- if (sessionId) {
353
- const sessionPlugin = this._framework.pluginManager.get('session')
354
- if (sessionPlugin) {
355
- sessionPlugin.addMessage(sessionId, { role: 'user', content: `【Webhook 数据】\n${JSON.stringify(webhookData, null, 2)}` })
356
- sessionPlugin.addMessage(sessionId, { role: 'assistant', content: responseText })
357
- }
358
- }
359
-
360
- // 触发 webhook 处理完成事件
361
- this._framework.emit('webhook:received', { webhook, data: webhookData, response: responseText, sessionId: finalSessionId })
362
-
363
- return { success: true, message: 'Webhook processed', response: responseText }
364
- } catch (err) {
365
- console.error('[Web] Webhook error:', err.message)
366
- return { success: false, error: err.message }
367
- }
368
- }
369
-
370
- // ==================== 路由注册 ====================
371
-
372
- async _registerRoute(method, path, handler, description) {
373
- if (!path.startsWith('/') || path.length < 2) {
374
- return { success: false, error: 'Invalid path format' }
375
- }
376
-
377
- if (!this._server) {
378
- await this._startServer()
379
- }
380
-
381
- const route = { method: method.toUpperCase(), path, handler, description: description || '' }
382
- const index = this._routes.findIndex(r => r.method === route.method && r.path === path)
383
- if (index >= 0) {
384
- this._routes[index] = route
385
- } else {
386
- this._routes.push(route)
387
- }
388
-
389
- console.log(`[Web] Route registered: ${method} ${path}`)
390
- return { success: true, message: `Route ${method} ${path} registered`, url: this._getUrl(path), route: { method, path, description } }
391
- }
392
-
393
- async _registerWebhook(prompt, awaitResponse = false) {
394
- if (!this._server) {
395
- await this._startServer()
396
- }
397
-
398
- // 生成唯一 ID 和路径:/webhook/{id}
399
- const id = this._generateId()
400
- const webhookPath = `/webhook/${id}`
401
-
402
- this._webhooks.set(webhookPath, {
403
- id,
404
- path: webhookPath,
405
- prompt: prompt || '处理以下 webhook 数据,返回适当的响应:',
406
- awaitResponse
407
- })
408
-
409
- console.log(`[Web] Webhook registered: ${webhookPath}`)
410
- return {
411
- success: true,
412
- message: `Webhook registered`,
413
- webhook: {
414
- id,
415
- path: webhookPath,
416
- url: this._getUrl(webhookPath)
417
- }
418
- }
419
- }
420
-
421
- async _registerStatic(urlPath, folder, options = {}) {
422
- if (!this._server) {
423
- await this._startServer()
424
- }
425
-
426
- const normalizedPath = urlPath.endsWith('/') ? urlPath : urlPath + '/'
427
- this._statics.push({
428
- urlPath: normalizedPath,
429
- folder: path.resolve(folder),
430
- options: {
431
- dotfiles: options.dotfiles || 'ignore',
432
- index: options.index || 'index.html'
433
- }
434
- })
435
-
436
- console.log(`[Web] Static registered: ${normalizedPath} -> ${folder}`)
437
- return {
438
- success: true,
439
- message: `Static folder registered`,
440
- url: this._getUrl(normalizedPath)
441
- }
442
- }
443
-
444
- _listRoutes() {
445
- const baseUrl = this._getUrl()
446
-
447
- return {
448
- success: true,
449
- server: this._server ? { running: true, host: this._host, port: this._port } : { running: false },
450
- routes: this._routes.map(r => ({ type: 'route', method: r.method, path: r.path, description: r.description })),
451
- webhooks: Array.from(this._webhooks.values()).map(w => ({ type: 'webhook', id: w.id, path: w.path, url: `${baseUrl}${w.path}`, prompt: w.prompt })),
452
- statics: this._statics.map(s => ({ type: 'static', path: s.urlPath, folder: s.folder, url: `${baseUrl}${s.urlPath}` }))
453
- }
454
- }
455
-
456
- // ==================== HTTP 客户端 ====================
457
-
458
- async _sendRequest(method, urlPath, body, headers) {
459
- if (!this._server) {
460
- return { success: false, error: 'Server not started' }
461
- }
462
-
463
- const url = this._getUrl(urlPath)
464
- try {
465
- const options = {
466
- method: method.toUpperCase(),
467
- headers: headers || {}
468
- }
469
-
470
- if (body && !['GET', 'HEAD'].includes(options.method)) {
471
- options.body = JSON.stringify(body)
472
- options.headers['Content-Type'] = options.headers['Content-Type'] || 'application/json'
473
- }
474
-
475
- const response = await fetch(url, options)
476
- const text = await response.text()
477
- let parsed = text
478
- try { parsed = JSON.parse(text) } catch { /* not JSON */ }
479
-
480
- return { success: true, status: response.status, headers: Object.fromEntries(response.headers.entries()), body: parsed }
481
- } catch (err) {
482
- return { success: false, error: err.message }
483
- }
484
- }
485
-
486
- // ==================== 工具方法 ====================
487
-
488
- _matchPath(routePath, reqPath) {
489
- const routeParts = routePath.split('/').filter(Boolean)
490
- const reqParts = reqPath.split('/').filter(Boolean)
491
- if (routeParts.length !== reqParts.length) return false
492
- return routeParts.every((part, i) => part.startsWith(':') || part === reqParts[i])
493
- }
494
-
495
- _extractParams(routePath, reqPath) {
496
- const params = {}
497
- const routeParts = routePath.split('/').filter(Boolean)
498
- const reqParts = reqPath.split('/').filter(Boolean)
499
- routeParts.forEach((part, i) => {
500
- if (part.startsWith(':')) {
501
- params[part.substring(1)] = reqParts[i]
502
- }
503
- })
504
- return params
505
- }
506
-
507
- _parseQuery(c) {
508
- const raw = c.req.queries() || {}
509
- const query = {}
510
- for (const [key, value] of Object.entries(raw)) {
511
- query[key] = value.length === 1 ? value[0] : value
512
- }
513
- return query
514
- }
515
-
516
- async _parseBody(c) {
517
- try {
518
- const rawText = await c.req.text()
519
- console.log(rawText)
520
- if (!rawText) return {}
521
- return JSON.parse(rawText)
522
- } catch (e) {
523
- return {}
524
- }
525
- }
526
-
527
- _getUrl(path='') {
528
- if (this._baseUrl) {
529
- return `${this._baseUrl}${path}`
530
- }
531
- return `http://${this._host}:${this._port}${path}`
532
- }
533
-
534
- async _executeHandler(handlerCode, context, tools) {
535
- try {
536
- const fn = new Function('context', 'tools', `return (async () => { ${handlerCode} })()`)
537
- return await fn(context, tools)
538
- } catch (err) {
539
- return { success: false, error: `Handler error: ${err.message}` }
540
- }
541
- }
542
-
543
- _serveStatic(pathname) {
544
- for (const staticFolder of this._statics) {
545
- if (pathname.startsWith(staticFolder.urlPath)) {
546
- const relativePath = pathname.substring(staticFolder.urlPath.length) || staticFolder.options.index
547
- const filePath = path.join(staticFolder.folder, relativePath)
548
-
549
- // 安全检查
550
- if (!filePath.startsWith(staticFolder.folder)) {
551
- return { type: 'forbidden' }
552
- }
553
- if (staticFolder.options.dotfiles === 'deny' && path.basename(filePath).startsWith('.')) {
554
- return { type: 'notFound' }
555
- }
556
-
557
- try {
558
- const content = fs.readFileSync(filePath)
559
- const ext = path.extname(filePath).toLowerCase()
560
- const contentTypes = {
561
- '.html': 'text/html', '.css': 'text/css', '.js': 'application/javascript',
562
- '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg',
563
- '.gif': 'image/gif', '.svg': 'image/svg+xml', '.ico': 'image/x-icon',
564
- '.txt': 'text/plain'
565
- }
566
- const contentType = contentTypes[ext] || 'application/octet-stream'
567
- return { type: 'file', content, contentType }
568
- } catch (err) {
569
- if (err.code === 'ENOENT') {
570
- return { type: 'notFound' }
571
- }
572
- return { type: 'error', message: err.message }
573
- }
574
- }
575
- }
576
- return null
577
- }
578
-
579
- _generateId() {
580
- return Math.random().toString(36).substring(2, 10) + Date.now().toString(36)
581
- }
582
-
583
- _getAgent(sessionId) {
584
- const finalSessionId = sessionId || `web_${Date.now()}`
585
-
586
- // 尝试从对应平台插件获取 Agent
587
- const platformPrefixes = ['weixin_', 'telegram_', 'feishu_']
588
- for (const prefix of platformPrefixes) {
589
- if (finalSessionId.startsWith(prefix)) {
590
- const pluginName = prefix.replace('_', '')
591
- const plugin = this._framework.pluginManager.get(pluginName)
592
- if (plugin?._sessionAgents?.size > 0) {
593
- const firstAgent = plugin._sessionAgents.values().next().value
594
- if (firstAgent) return firstAgent
595
- }
596
- }
597
- }
598
-
599
- // 返回主 Agent
600
- return this._framework._mainAgent || this._framework._agents?.[0]
601
- }
602
- }
603
-
604
- module.exports = { WebPlugin }
1
+ /**
2
+ * Web 服务插件
3
+ * 支持 HTTP 服务、路由注册、Webhook(自动生成 /webhook/{id} 链接)
4
+ */
5
+
6
+ const { Plugin } = require('../src/core/plugin-base')
7
+ const { logger } = require('../src/utils/logger')
8
+ const log = logger.child('Web')
9
+ const { z } = require('zod')
10
+ const { serve } = require('@hono/node-server')
11
+ const { Hono } = require('hono')
12
+ const fs = require('fs')
13
+ const path = require('path')
14
+
15
+ class WebPlugin extends Plugin {
16
+ constructor(config = {}) {
17
+ super()
18
+ this.name = 'web'
19
+ this.version = '3.1.0'
20
+ this.description = 'Web 服务插件,支持 HTTP 服务、路由注册、Webhook'
21
+ this.priority = 50
22
+
23
+ this.system = true
24
+
25
+ // 服务器配置
26
+ this._port = process.env.WEB_PORT || 3000
27
+ this._host = process.env.WEB_HOST || '127.0.0.1'
28
+ this._baseUrl = process.env.WEB_BASE_URL || null // 公网可访问的域名
29
+
30
+ // 运行时状态
31
+ this._server = null
32
+ this._app = null
33
+ this._framework = null
34
+
35
+ // 数据存储(始终保持原始类型)
36
+ this._routes = [] // 路由列表
37
+ this._webhooks = new Map() // webhook Map: id -> {id, path, prompt, sessionId}
38
+ this._statics = [] // 静态文件夹列表
39
+ }
40
+
41
+ // ==================== 生命周期 ====================
42
+
43
+ install(framework) {
44
+ this._framework = framework
45
+ this._registerTools()
46
+
47
+ // 将 WEB_BASE_URL 注入到 framework 的元数据,供所有 agent 使用
48
+ if (this._baseUrl && framework._mainAgent) {
49
+ framework._mainAgent.setMetadata('WEB_BASE_URL', this._baseUrl)
50
+ }
51
+
52
+ return this
53
+ }
54
+
55
+ start() {
56
+ return this
57
+ }
58
+
59
+ reload(framework) {
60
+ this._framework = framework
61
+ // 重新注入 WEB_BASE_URL
62
+ if (this._baseUrl && framework._mainAgent) {
63
+ framework._mainAgent.setMetadata('WEB_BASE_URL', this._baseUrl)
64
+ }
65
+ }
66
+
67
+ uninstall() {
68
+ this._stopServer()
69
+ this._framework = null
70
+ }
71
+
72
+ // ==================== 工具注册 ====================
73
+
74
+ _registerTools() {
75
+ // 启动 Web 服务
76
+ this._framework.registerTool({
77
+ name: 'web_start',
78
+ description: '启动 Web 服务',
79
+ inputSchema: z.object({
80
+ port: z.number().optional().describe('端口号,默认 3000'),
81
+ host: z.string().optional().describe('主机地址,默认 0.0.0.0')
82
+ }),
83
+ execute: async (args) => this._startServer(args.port, args.host)
84
+ })
85
+
86
+ // 停止 Web 服务
87
+ this._framework.registerTool({
88
+ name: 'web_stop',
89
+ description: '停止 Web 服务',
90
+ inputSchema: z.object({}),
91
+ execute: async () => this._stopServer()
92
+ })
93
+
94
+ // 注册 HTTP 路由
95
+ this._framework.registerTool({
96
+ name: 'web_register_route',
97
+ description: '注册 HTTP 路由',
98
+ inputSchema: z.object({
99
+ method: z.enum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH']).describe('HTTP 方法'),
100
+ path: z.string().describe('路由路径,如 /api/user'),
101
+ handler: z.string().describe(
102
+ '处理逻辑,JavaScript 代码字符串,必须用 return 返回内容。' +
103
+ '优先使用 tools.{toolName}(args) 调用工具获取真实数据。' +
104
+ '可用变量:context.params, context.query, context.body, tools。' +
105
+ '示例:return await tools.get_user({ id: context.params.id })'
106
+ ),
107
+ description: z.string().optional().describe('路由描述')
108
+ }),
109
+ execute: async (args) => this._registerRoute(args.method, args.path, args.handler, args.description)
110
+ })
111
+
112
+ // 注册 Webhook(自动生成 /webhook/{id} 链接)
113
+ this._framework.registerTool({
114
+ name: 'web_register_webhook',
115
+ description: '注册 Webhook,接收的数据会交给 LLM 处理。自动生成唯一 URL',
116
+ inputSchema: z.object({
117
+ prompt: z.string().optional().describe('提示词,描述如何处理请求'),
118
+ awaitResponse: z.boolean().optional().describe('是否等待 LLM 处理完成再返回响应,默认 false')
119
+ }),
120
+ execute: async (args) => this._registerWebhook(args.prompt, args.awaitResponse)
121
+ })
122
+
123
+ // 注册静态资源
124
+ this._framework.registerTool({
125
+ name: 'web_register_static',
126
+ description: '注册静态资源文件夹',
127
+ inputSchema: z.object({
128
+ urlPath: z.string().describe('URL 路径前缀,如 /public'),
129
+ folder: z.string().describe('本地文件夹路径,如 ./static'),
130
+ options: z.object({
131
+ dotfiles: z.enum(['ignore', 'allow', 'deny']).optional(),
132
+ index: z.string().optional()
133
+ }).optional()
134
+ }),
135
+ execute: async (args) => this._registerStatic(args.urlPath, args.folder, args.options)
136
+ })
137
+
138
+ // 列出所有路由
139
+ this._framework.registerTool({
140
+ name: 'web_list_routes',
141
+ description: '列出所有已注册的路由和 Webhook',
142
+ inputSchema: z.object({}),
143
+ execute: async () => this._listRoutes()
144
+ })
145
+
146
+ // 发送 HTTP 请求
147
+ this._framework.registerTool({
148
+ name: 'web_request',
149
+ description: '发送 HTTP 请求',
150
+ inputSchema: z.object({
151
+ method: z.enum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH']).describe('HTTP 方法'),
152
+ path: z.string().describe('请求路径'),
153
+ body: z.any().optional().describe('请求体'),
154
+ headers: z.record(z.string()).optional().describe('请求头')
155
+ }),
156
+ execute: async (args) => this._sendRequest(args.method, args.path, args.body, args.headers)
157
+ })
158
+ }
159
+
160
+ // ==================== 服务器控制 ====================
161
+
162
+ async _startServer(port, host) {
163
+ if (this._server) {
164
+ return { success: true, message: 'Server already running', port: this._port }
165
+ }
166
+
167
+ this._port = port || this._port
168
+ this._host = host || this._host
169
+
170
+ this._app = new Hono()
171
+ this._setupMiddleware()
172
+
173
+ try {
174
+ this._server = serve({
175
+ fetch: this._app.fetch,
176
+ port: this._port,
177
+ hostname: this._host
178
+ })
179
+ const serverUrl = this._getUrl()
180
+ log.info(` Server started on ${serverUrl}`)
181
+ return {
182
+ success: true,
183
+ message: `Server started on ${serverUrl}`,
184
+ // port: this._port,
185
+ // host: this._host,
186
+ host: serverUrl,
187
+ url:serverUrl,
188
+ }
189
+ } catch (err) {
190
+ this._server = null
191
+ this._app = null
192
+ if (err.code === 'EADDRINUSE') {
193
+ return { success: false, error: `Port ${this._port} is already in use` }
194
+ }
195
+ return { success: false, error: err.message }
196
+ }
197
+ }
198
+
199
+ async _stopServer() {
200
+ if (!this._server) {
201
+ return { success: true, message: 'Server not running' }
202
+ }
203
+ this._server.close()
204
+ this._server = null
205
+ this._app = null
206
+ log.info(' Server stopped')
207
+ return { success: true, message: 'Server stopped' }
208
+ }
209
+
210
+ // ==================== 中间件 ====================
211
+
212
+ _setupMiddleware() {
213
+ this._app.use('*', async (c) => {
214
+ const pathname = c.req.path
215
+
216
+ // CORS 预检
217
+ if (c.req.method === 'OPTIONS') {
218
+ return c.text('', 200, {
219
+ 'Access-Control-Allow-Origin': '*',
220
+ 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
221
+ 'Access-Control-Allow-Headers': 'Content-Type, Authorization'
222
+ })
223
+ }
224
+
225
+ // 1. 静态文件
226
+ const staticResult = this._serveStatic(pathname)
227
+ if (staticResult) {
228
+ if (staticResult.type === 'file') {
229
+ return c.newResponse(staticResult.content, {
230
+ headers: { 'Content-Type': staticResult.contentType }
231
+ })
232
+ }
233
+ if (staticResult.type === 'notFound') {
234
+ return c.json({ error: 'Not Found' }, 404)
235
+ }
236
+ if (staticResult.type === 'forbidden') {
237
+ return c.json({ error: 'Forbidden' }, 403)
238
+ }
239
+ if (staticResult.type === 'error') {
240
+ return c.json({ error: staticResult.message }, 500)
241
+ }
242
+ }
243
+
244
+ // 2. Webhook(精确匹配)
245
+ const webhook = this._webhooks.get(pathname)
246
+ if (webhook) {
247
+ const result = await this._handleWebhook(c, webhook)
248
+ return c.json(result)
249
+ }
250
+
251
+ // 3. 路由(支持参数)
252
+ for (const route of this._routes) {
253
+ if (route.method === c.req.method && this._matchPath(route.path, pathname)) {
254
+ return await this._handleRoute(c, route, pathname)
255
+ }
256
+ }
257
+
258
+ // 404
259
+ return c.json({ success: false, error: 'Not Found', path: pathname }, 404)
260
+ })
261
+ }
262
+
263
+ // ==================== 请求处理 ====================
264
+
265
+ async _handleRoute(c, route, pathname) {
266
+ const params = this._extractParams(route.path, pathname)
267
+ const query = this._parseQuery(c)
268
+ const body = await this._parseBody(c)
269
+
270
+ const context = { params, query, body }
271
+
272
+ // 构建 tools 代理对象,允许 handler 以函数方式直接调用工具
273
+ // 例如: const user = await tools.get_user({ id: 1 })
274
+ const registry = this._framework.toolRegistry
275
+ const tools = new Proxy({}, {
276
+ get: (_, name) => {
277
+ if (name === 'call') {
278
+ // 保留 call 方式作为后备
279
+ return async (toolName, args) => registry.execute(toolName, args || {}, this._framework)
280
+ }
281
+ return async (args) => registry.execute(name, args || {}, this._framework)
282
+ }
283
+ })
284
+
285
+ const result = await this._executeHandler(route.handler, context, tools)
286
+ return c.json(result)
287
+ }
288
+
289
+ async _handleWebhook(c, webhook) {
290
+ const query = this._parseQuery(c)
291
+ const body = await this._parseBody(c)
292
+ const webhookData = {
293
+ path: webhook.path,
294
+ method: c.req.method,
295
+ query,
296
+ body,
297
+ timestamp: new Date().toISOString()
298
+ }
299
+
300
+ // 从执行上下文获取 sessionId
301
+ const ctx = this._framework.getExecutionContext()
302
+ const sessionId = ctx?.sessionId || null
303
+
304
+ // 获取 Agent
305
+ const agent = this._getAgent(sessionId)
306
+ if (!agent) {
307
+ log.error(' No agent available')
308
+ return { success: false, error: 'No agent available' }
309
+ }
310
+
311
+ const prompt = webhook.prompt || '处理以下 webhook 数据,返回适当的响应:'
312
+ const finalSessionId = sessionId || `web_${Date.now()}`
313
+
314
+ // 触发 webhook 接收事件
315
+ this._framework.emit('webhook:received', { webhook, data: webhookData, sessionId: finalSessionId })
316
+
317
+ // 使用子Agent处理 webhook
318
+ const webhookAgent = this._framework.createSubAgent({
319
+ name: 'webhook_handler',
320
+ role: 'Webhook处理助手,专注于处理webhook数据并生成适当响应'
321
+ })
322
+
323
+ if (!webhook.awaitResponse) {
324
+ // 不等待,立即返回
325
+ webhookAgent.chat(`${prompt}\n\n数据:\n${JSON.stringify(webhookData, null, 2)}`).then(result => {
326
+ const responseText = result.message || result.text || ''
327
+ log.info(` Webhook processed (${webhook.path}), LLM response (${responseText.length} chars)`)
328
+
329
+ // 添加到 session 历史
330
+ if (sessionId) {
331
+ const sessionPlugin = this._framework.pluginManager.get('session')
332
+ if (sessionPlugin) {
333
+ sessionPlugin.addMessage(sessionId, { role: 'user', content: `【Webhook 数据】\n${JSON.stringify(webhookData, null, 2)}` })
334
+ sessionPlugin.addMessage(sessionId, { role: 'assistant', content: responseText })
335
+ }
336
+ }
337
+
338
+ // 触发 webhook 处理完成事件
339
+ this._framework.emit('webhook:received', { webhook, data: webhookData, response: responseText, sessionId: finalSessionId })
340
+ }).catch(err => {
341
+ log.error(' Webhook error:', err.message)
342
+ })
343
+
344
+ return { success: true, message: 'Webhook received, processing in background' }
345
+ }
346
+
347
+ // 等待 LLM 处理完成
348
+ try {
349
+ const result = await webhookAgent.chat(`${prompt}\n\n数据:\n${JSON.stringify(webhookData, null, 2)}`)
350
+ const responseText = result.message || result.text || ''
351
+ log.info(` Webhook processed (${webhook.path}), LLM response (${responseText.length} chars)`)
352
+
353
+ // 添加到 session 历史
354
+ if (sessionId) {
355
+ const sessionPlugin = this._framework.pluginManager.get('session')
356
+ if (sessionPlugin) {
357
+ sessionPlugin.addMessage(sessionId, { role: 'user', content: `【Webhook 数据】\n${JSON.stringify(webhookData, null, 2)}` })
358
+ sessionPlugin.addMessage(sessionId, { role: 'assistant', content: responseText })
359
+ }
360
+ }
361
+
362
+ // 触发 webhook 处理完成事件
363
+ this._framework.emit('webhook:received', { webhook, data: webhookData, response: responseText, sessionId: finalSessionId })
364
+
365
+ return { success: true, message: 'Webhook processed', response: responseText }
366
+ } catch (err) {
367
+ log.error(' Webhook error:', err.message)
368
+ return { success: false, error: err.message }
369
+ }
370
+ }
371
+
372
+ // ==================== 路由注册 ====================
373
+
374
+ async _registerRoute(method, path, handler, description) {
375
+ if (!path.startsWith('/') || path.length < 2) {
376
+ return { success: false, error: 'Invalid path format' }
377
+ }
378
+
379
+ if (!this._server) {
380
+ await this._startServer()
381
+ }
382
+
383
+ const route = { method: method.toUpperCase(), path, handler, description: description || '' }
384
+ const index = this._routes.findIndex(r => r.method === route.method && r.path === path)
385
+ if (index >= 0) {
386
+ this._routes[index] = route
387
+ } else {
388
+ this._routes.push(route)
389
+ }
390
+
391
+ log.info(` Route registered: ${method} ${path}`)
392
+ return { success: true, message: `Route ${method} ${path} registered`, url: this._getUrl(path), route: { method, path, description } }
393
+ }
394
+
395
+ async _registerWebhook(prompt, awaitResponse = false) {
396
+ if (!this._server) {
397
+ await this._startServer()
398
+ }
399
+
400
+ // 生成唯一 ID 和路径:/webhook/{id}
401
+ const id = this._generateId()
402
+ const webhookPath = `/webhook/${id}`
403
+
404
+ this._webhooks.set(webhookPath, {
405
+ id,
406
+ path: webhookPath,
407
+ prompt: prompt || '处理以下 webhook 数据,返回适当的响应:',
408
+ awaitResponse
409
+ })
410
+
411
+ log.info(` Webhook registered: ${webhookPath}`)
412
+ return {
413
+ success: true,
414
+ message: `Webhook registered`,
415
+ webhook: {
416
+ id,
417
+ path: webhookPath,
418
+ url: this._getUrl(webhookPath)
419
+ }
420
+ }
421
+ }
422
+
423
+ async _registerStatic(urlPath, folder, options = {}) {
424
+ if (!this._server) {
425
+ await this._startServer()
426
+ }
427
+
428
+ const normalizedPath = urlPath.endsWith('/') ? urlPath : urlPath + '/'
429
+ this._statics.push({
430
+ urlPath: normalizedPath,
431
+ folder: path.resolve(folder),
432
+ options: {
433
+ dotfiles: options.dotfiles || 'ignore',
434
+ index: options.index || 'index.html'
435
+ }
436
+ })
437
+
438
+ log.info(` Static registered: ${normalizedPath} -> ${folder}`)
439
+ return {
440
+ success: true,
441
+ message: `Static folder registered`,
442
+ url: this._getUrl(normalizedPath)
443
+ }
444
+ }
445
+
446
+ _listRoutes() {
447
+ const baseUrl = this._getUrl()
448
+
449
+ return {
450
+ success: true,
451
+ server: this._server ? { running: true, host: this._host, port: this._port } : { running: false },
452
+ routes: this._routes.map(r => ({ type: 'route', method: r.method, path: r.path, description: r.description })),
453
+ webhooks: Array.from(this._webhooks.values()).map(w => ({ type: 'webhook', id: w.id, path: w.path, url: `${baseUrl}${w.path}`, prompt: w.prompt })),
454
+ statics: this._statics.map(s => ({ type: 'static', path: s.urlPath, folder: s.folder, url: `${baseUrl}${s.urlPath}` }))
455
+ }
456
+ }
457
+
458
+ // ==================== HTTP 客户端 ====================
459
+
460
+ async _sendRequest(method, urlPath, body, headers) {
461
+ if (!this._server) {
462
+ return { success: false, error: 'Server not started' }
463
+ }
464
+
465
+ const url = this._getUrl(urlPath)
466
+ try {
467
+ const options = {
468
+ method: method.toUpperCase(),
469
+ headers: headers || {}
470
+ }
471
+
472
+ if (body && !['GET', 'HEAD'].includes(options.method)) {
473
+ options.body = JSON.stringify(body)
474
+ options.headers['Content-Type'] = options.headers['Content-Type'] || 'application/json'
475
+ }
476
+
477
+ const response = await fetch(url, options)
478
+ const text = await response.text()
479
+ let parsed = text
480
+ try { parsed = JSON.parse(text) } catch { /* not JSON */ }
481
+
482
+ return { success: true, status: response.status, headers: Object.fromEntries(response.headers.entries()), body: parsed }
483
+ } catch (err) {
484
+ return { success: false, error: err.message }
485
+ }
486
+ }
487
+
488
+ // ==================== 工具方法 ====================
489
+
490
+ _matchPath(routePath, reqPath) {
491
+ const routeParts = routePath.split('/').filter(Boolean)
492
+ const reqParts = reqPath.split('/').filter(Boolean)
493
+ if (routeParts.length !== reqParts.length) return false
494
+ return routeParts.every((part, i) => part.startsWith(':') || part === reqParts[i])
495
+ }
496
+
497
+ _extractParams(routePath, reqPath) {
498
+ const params = {}
499
+ const routeParts = routePath.split('/').filter(Boolean)
500
+ const reqParts = reqPath.split('/').filter(Boolean)
501
+ routeParts.forEach((part, i) => {
502
+ if (part.startsWith(':')) {
503
+ params[part.substring(1)] = reqParts[i]
504
+ }
505
+ })
506
+ return params
507
+ }
508
+
509
+ _parseQuery(c) {
510
+ const raw = c.req.queries() || {}
511
+ const query = {}
512
+ for (const [key, value] of Object.entries(raw)) {
513
+ query[key] = value.length === 1 ? value[0] : value
514
+ }
515
+ return query
516
+ }
517
+
518
+ async _parseBody(c) {
519
+ try {
520
+ const rawText = await c.req.text()
521
+ log.info(rawText)
522
+ if (!rawText) return {}
523
+ return JSON.parse(rawText)
524
+ } catch (e) {
525
+ return {}
526
+ }
527
+ }
528
+
529
+ _getUrl(path='') {
530
+ if (this._baseUrl) {
531
+ return `${this._baseUrl}${path}`
532
+ }
533
+ return `http://${this._host}:${this._port}${path}`
534
+ }
535
+
536
+ async _executeHandler(handlerCode, context, tools) {
537
+ try {
538
+ // 使用沙箱执行用户代码,防止恶意代码执行
539
+ return await runInSandbox(handlerCode, { context, tools }, { timeout: 10000 })
540
+ } catch (err) {
541
+ return { success: false, error: `Handler error: ${err.message}` }
542
+ }
543
+ }
544
+
545
+ _serveStatic(pathname) {
546
+ for (const staticFolder of this._statics) {
547
+ if (pathname.startsWith(staticFolder.urlPath)) {
548
+ const relativePath = pathname.substring(staticFolder.urlPath.length) || staticFolder.options.index
549
+ const filePath = path.join(staticFolder.folder, relativePath)
550
+
551
+ // 安全检查
552
+ if (!filePath.startsWith(staticFolder.folder)) {
553
+ return { type: 'forbidden' }
554
+ }
555
+ if (staticFolder.options.dotfiles === 'deny' && path.basename(filePath).startsWith('.')) {
556
+ return { type: 'notFound' }
557
+ }
558
+
559
+ try {
560
+ const content = fs.readFileSync(filePath)
561
+ const ext = path.extname(filePath).toLowerCase()
562
+ const contentTypes = {
563
+ '.html': 'text/html', '.css': 'text/css', '.js': 'application/javascript',
564
+ '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg',
565
+ '.gif': 'image/gif', '.svg': 'image/svg+xml', '.ico': 'image/x-icon',
566
+ '.txt': 'text/plain'
567
+ }
568
+ const contentType = contentTypes[ext] || 'application/octet-stream'
569
+ return { type: 'file', content, contentType }
570
+ } catch (err) {
571
+ if (err.code === 'ENOENT') {
572
+ return { type: 'notFound' }
573
+ }
574
+ return { type: 'error', message: err.message }
575
+ }
576
+ }
577
+ }
578
+ return null
579
+ }
580
+
581
+ _generateId() {
582
+ return Math.random().toString(36).substring(2, 10) + Date.now().toString(36)
583
+ }
584
+
585
+ _getAgent(sessionId) {
586
+ const finalSessionId = sessionId || `web_${Date.now()}`
587
+
588
+ // 尝试从对应平台插件获取 Agent
589
+ const platformPrefixes = ['weixin_', 'telegram_', 'feishu_']
590
+ for (const prefix of platformPrefixes) {
591
+ if (finalSessionId.startsWith(prefix)) {
592
+ const pluginName = prefix.replace('_', '')
593
+ const plugin = this._framework.pluginManager.get(pluginName)
594
+ if (plugin?._sessionAgents?.size > 0) {
595
+ const firstAgent = plugin._sessionAgents.values().next().value
596
+ if (firstAgent) return firstAgent
597
+ }
598
+ }
599
+ }
600
+
601
+ // 返回主 Agent
602
+ return this._framework._mainAgent || this._framework._agents?.[0]
603
+ }
604
+ }
605
+
606
+ module.exports = { WebPlugin }