foliko 1.0.39 → 1.0.41

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.
@@ -112,7 +112,7 @@ class ThinkPlugin extends Plugin {
112
112
  }
113
113
 
114
114
  const reflectPrompt = this._buildReflectPrompt(mode, depth, topic)
115
- const result = await agent.chat(reflectPrompt)
115
+ const result = await agent.pushMessage(reflectPrompt)
116
116
 
117
117
  const thought = {
118
118
  id: Date.now(),
@@ -218,7 +218,7 @@ ${topic || '接下来的行动'}
218
218
  如果发现不足,请直接补充。如果没问题,请简短回复"检查完毕,无需补充"。`
219
219
 
220
220
  // 静默思考,不打断用户
221
- const result = await agent.chat(reflectPrompt)
221
+ const result = await agent.pushMessage(reflectPrompt)
222
222
 
223
223
  const thought = {
224
224
  id: Date.now(),
@@ -0,0 +1,542 @@
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
+ // 服务器配置
22
+ this._port = config.port || process.env.WEB_PORT || 8088
23
+ this._host = config.host || process.env.WEB_HOST || '127.0.0.1'
24
+ this._baseUrl = config.baseUrl || process.env.WEB_BASE_URL || null // 公网可访问的域名
25
+
26
+ // 运行时状态
27
+ this._server = null
28
+ this._app = null
29
+ this._framework = null
30
+
31
+ // 数据存储(始终保持原始类型)
32
+ this._routes = [] // 路由列表
33
+ this._webhooks = new Map() // webhook Map: id -> {id, path, prompt, sessionId}
34
+ this._statics = [] // 静态文件夹列表
35
+ }
36
+
37
+ // ==================== 生命周期 ====================
38
+
39
+ install(framework) {
40
+ this._framework = framework
41
+ this._registerTools()
42
+ return this
43
+ }
44
+
45
+ start() {
46
+ return this
47
+ }
48
+
49
+ reload(framework) {
50
+ this._framework = framework
51
+ }
52
+
53
+ uninstall() {
54
+ this._stopServer()
55
+ this._framework = null
56
+ }
57
+
58
+ // ==================== 工具注册 ====================
59
+
60
+ _registerTools() {
61
+ // 启动 Web 服务
62
+ this._framework.registerTool({
63
+ name: 'web_start',
64
+ description: '启动 Web 服务',
65
+ inputSchema: z.object({
66
+ port: z.number().optional().describe('端口号,默认 3000'),
67
+ host: z.string().optional().describe('主机地址,默认 0.0.0.0')
68
+ }),
69
+ execute: async (args) => this._startServer(args.port, args.host)
70
+ })
71
+
72
+ // 停止 Web 服务
73
+ this._framework.registerTool({
74
+ name: 'web_stop',
75
+ description: '停止 Web 服务',
76
+ inputSchema: z.object({}),
77
+ execute: async () => this._stopServer()
78
+ })
79
+
80
+ // 注册 HTTP 路由
81
+ this._framework.registerTool({
82
+ name: 'web_register_route',
83
+ description: '注册 HTTP 路由',
84
+ inputSchema: z.object({
85
+ method: z.enum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH']).describe('HTTP 方法'),
86
+ path: z.string().describe('路由路径,如 /api/user'),
87
+ handler: z.string().describe('处理逻辑,JavaScript 代码字符串,必须用 return 返回内容。\n示例:\n return "hello"\n return { hello: "world" }\n return context.params.id\n return context.query\n return context.body\n可用变量:context.params、context.query、context.body'),
88
+ description: z.string().optional().describe('路由描述')
89
+ }),
90
+ execute: async (args) => this._registerRoute(args.method, args.path, args.handler, args.description)
91
+ })
92
+
93
+ // 注册 Webhook(自动生成 /webhook/{id} 链接)
94
+ this._framework.registerTool({
95
+ name: 'web_register_webhook',
96
+ description: '注册 Webhook,接收的数据会交给 LLM 处理。自动生成唯一 URL',
97
+ inputSchema: z.object({
98
+ prompt: z.string().optional().describe('提示词,描述如何处理请求'),
99
+ awaitResponse: z.boolean().optional().describe('是否等待 LLM 处理完成再返回响应,默认 false')
100
+ }),
101
+ execute: async (args) => this._registerWebhook(args.prompt, args.awaitResponse)
102
+ })
103
+
104
+ // 注册静态资源
105
+ this._framework.registerTool({
106
+ name: 'web_register_static',
107
+ description: '注册静态资源文件夹',
108
+ inputSchema: z.object({
109
+ urlPath: z.string().describe('URL 路径前缀,如 /public'),
110
+ folder: z.string().describe('本地文件夹路径,如 ./static'),
111
+ options: z.object({
112
+ dotfiles: z.enum(['ignore', 'allow', 'deny']).optional(),
113
+ index: z.string().optional()
114
+ }).optional()
115
+ }),
116
+ execute: async (args) => this._registerStatic(args.urlPath, args.folder, args.options)
117
+ })
118
+
119
+ // 列出所有路由
120
+ this._framework.registerTool({
121
+ name: 'web_list_routes',
122
+ description: '列出所有已注册的路由和 Webhook',
123
+ inputSchema: z.object({}),
124
+ execute: async () => this._listRoutes()
125
+ })
126
+
127
+ // 发送 HTTP 请求
128
+ this._framework.registerTool({
129
+ name: 'web_request',
130
+ description: '发送 HTTP 请求',
131
+ inputSchema: z.object({
132
+ method: z.enum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH']).describe('HTTP 方法'),
133
+ path: z.string().describe('请求路径'),
134
+ body: z.any().optional().describe('请求体'),
135
+ headers: z.record(z.string()).optional().describe('请求头')
136
+ }),
137
+ execute: async (args) => this._sendRequest(args.method, args.path, args.body, args.headers)
138
+ })
139
+ }
140
+
141
+ // ==================== 服务器控制 ====================
142
+
143
+ async _startServer(port, host) {
144
+ if (this._server) {
145
+ return { success: true, message: 'Server already running', port: this._port }
146
+ }
147
+
148
+ this._port = port || this._port
149
+ this._host = host || this._host
150
+
151
+ this._app = new Hono()
152
+ this._setupMiddleware()
153
+
154
+ try {
155
+ this._server = serve({
156
+ fetch: this._app.fetch,
157
+ port: this._port,
158
+ hostname: this._host
159
+ })
160
+ console.log(`[Web] Server started on http://${this._host}:${this._port}`)
161
+ return {
162
+ success: true,
163
+ message: `Server started on http://${this._host}:${this._port}`,
164
+ port: this._port,
165
+ host: this._host
166
+ }
167
+ } catch (err) {
168
+ this._server = null
169
+ this._app = null
170
+ if (err.code === 'EADDRINUSE') {
171
+ return { success: false, error: `Port ${this._port} is already in use` }
172
+ }
173
+ return { success: false, error: err.message }
174
+ }
175
+ }
176
+
177
+ async _stopServer() {
178
+ if (!this._server) {
179
+ return { success: true, message: 'Server not running' }
180
+ }
181
+ this._server.close()
182
+ this._server = null
183
+ this._app = null
184
+ console.log('[Web] Server stopped')
185
+ return { success: true, message: 'Server stopped' }
186
+ }
187
+
188
+ // ==================== 中间件 ====================
189
+
190
+ _setupMiddleware() {
191
+ this._app.use('*', async (c) => {
192
+ const pathname = c.req.path
193
+
194
+ // CORS 预检
195
+ if (c.req.method === 'OPTIONS') {
196
+ return c.text('', 200, {
197
+ 'Access-Control-Allow-Origin': '*',
198
+ 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
199
+ 'Access-Control-Allow-Headers': 'Content-Type, Authorization'
200
+ })
201
+ }
202
+
203
+ // 1. 静态文件
204
+ const staticResult = this._serveStatic(pathname)
205
+ if (staticResult) return staticResult
206
+
207
+ // 2. Webhook(精确匹配)
208
+ const webhook = this._webhooks.get(pathname)
209
+ if (webhook) {
210
+ const result = await this._handleWebhook(c, webhook)
211
+ return c.json(result)
212
+ }
213
+
214
+ // 3. 路由(支持参数)
215
+ for (const route of this._routes) {
216
+ if (route.method === c.req.method && this._matchPath(route.path, pathname)) {
217
+ return await this._handleRoute(c, route, pathname)
218
+ }
219
+ }
220
+
221
+ // 404
222
+ return c.json({ success: false, error: 'Not Found', path: pathname }, 404)
223
+ })
224
+ }
225
+
226
+ // ==================== 请求处理 ====================
227
+
228
+ async _handleRoute(c, route, pathname) {
229
+ const params = this._extractParams(route.path, pathname)
230
+ const query = this._parseQuery(c)
231
+ const body = await c.req.json().catch(() => ({}))
232
+
233
+ const context = { params, query, body }
234
+ const result = await this._executeHandler(route.handler, context)
235
+ return c.json(result)
236
+ }
237
+
238
+ async _handleWebhook(c, webhook) {
239
+ const query = this._parseQuery(c)
240
+ const body = await c.req.json().catch(() => ({}))
241
+ const webhookData = {
242
+ path: webhook.path,
243
+ method: c.req.method,
244
+ query,
245
+ body,
246
+ timestamp: new Date().toISOString()
247
+ }
248
+
249
+ // 从执行上下文获取 sessionId
250
+ const ctx = this._framework.getExecutionContext()
251
+ const sessionId = ctx?.sessionId || null
252
+
253
+ // 获取 Agent
254
+ const agent = this._getAgent(sessionId)
255
+ if (!agent) {
256
+ console.error('[Web] No agent available')
257
+ return { success: false, error: 'No agent available' }
258
+ }
259
+
260
+ const prompt = webhook.prompt || '处理以下 webhook 数据,返回适当的响应:'
261
+ const finalSessionId = sessionId || `web_${Date.now()}`
262
+
263
+ // 触发 webhook 接收事件
264
+ this._framework.emit('webhook:received', { webhook, data: webhookData })
265
+
266
+ if (!webhook.awaitResponse) {
267
+ // 不等待,立即返回
268
+ agent.pushMessage(`${prompt}\n\n数据:\n${JSON.stringify(webhookData, null, 2)}`, {
269
+ sessionId: finalSessionId
270
+ }).then(result => {
271
+ const responseText = result.message || result.text || ''
272
+ console.log(`[Web] Webhook processed (${webhook.path}), LLM response (${responseText.length} chars)`)
273
+
274
+ // 添加到 session 历史
275
+ if (sessionId) {
276
+ const sessionPlugin = this._framework.pluginManager.get('session')
277
+ if (sessionPlugin) {
278
+ sessionPlugin.addMessage(sessionId, { role: 'user', content: `【Webhook 数据】\n${JSON.stringify(webhookData, null, 2)}` })
279
+ sessionPlugin.addMessage(sessionId, { role: 'assistant', content: responseText })
280
+ }
281
+ }
282
+
283
+ // 触发 webhook 处理完成事件
284
+ this._framework.emit('webhook:received', { webhook, data: webhookData, response: responseText })
285
+ }).catch(err => {
286
+ console.error('[Web] Webhook error:', err.message)
287
+ })
288
+
289
+ return { success: true, message: 'Webhook received, processing in background' }
290
+ }
291
+
292
+ // 等待 LLM 处理完成
293
+ try {
294
+ const result = await agent.pushMessage(`${prompt}\n\n数据:\n${JSON.stringify(webhookData, null, 2)}`, {
295
+ sessionId: finalSessionId
296
+ })
297
+ const responseText = result.message || result.text || ''
298
+ console.log(`[Web] Webhook processed (${webhook.path}), LLM response (${responseText.length} chars)`)
299
+
300
+ // 添加到 session 历史
301
+ if (sessionId) {
302
+ const sessionPlugin = this._framework.pluginManager.get('session')
303
+ if (sessionPlugin) {
304
+ sessionPlugin.addMessage(sessionId, { role: 'user', content: `【Webhook 数据】\n${JSON.stringify(webhookData, null, 2)}` })
305
+ sessionPlugin.addMessage(sessionId, { role: 'assistant', content: responseText })
306
+ }
307
+ }
308
+
309
+ // 触发 webhook 处理完成事件
310
+ this._framework.emit('webhook:received', { webhook, data: webhookData, response: responseText })
311
+
312
+ return { success: true, message: 'Webhook processed', response: responseText }
313
+ } catch (err) {
314
+ console.error('[Web] Webhook error:', err.message)
315
+ return { success: false, error: err.message }
316
+ }
317
+ }
318
+
319
+ // ==================== 路由注册 ====================
320
+
321
+ async _registerRoute(method, path, handler, description) {
322
+ if (!path.startsWith('/') || path.length < 2) {
323
+ return { success: false, error: 'Invalid path format' }
324
+ }
325
+
326
+ if (!this._server) {
327
+ await this._startServer()
328
+ }
329
+
330
+ const route = { method: method.toUpperCase(), path, handler, description: description || '' }
331
+ const index = this._routes.findIndex(r => r.method === route.method && r.path === path)
332
+ if (index >= 0) {
333
+ this._routes[index] = route
334
+ } else {
335
+ this._routes.push(route)
336
+ }
337
+
338
+ console.log(`[Web] Route registered: ${method} ${path}`)
339
+ return { success: true, message: `Route ${method} ${path} registered`, route: { method, path, description } }
340
+ }
341
+
342
+ async _registerWebhook(prompt, awaitResponse = false) {
343
+ if (!this._server) {
344
+ await this._startServer()
345
+ }
346
+
347
+ // 生成唯一 ID 和路径:/webhook/{id}
348
+ const id = this._generateId()
349
+ const webhookPath = `/webhook/${id}`
350
+
351
+ this._webhooks.set(webhookPath, {
352
+ id,
353
+ path: webhookPath,
354
+ prompt: prompt || '处理以下 webhook 数据,返回适当的响应:',
355
+ awaitResponse
356
+ })
357
+
358
+ console.log(`[Web] Webhook registered: ${webhookPath}`)
359
+ return {
360
+ success: true,
361
+ message: `Webhook registered`,
362
+ webhook: {
363
+ id,
364
+ path: webhookPath,
365
+ url: this._getUrl(webhookPath)
366
+ }
367
+ }
368
+ }
369
+
370
+ async _registerStatic(urlPath, folder, options = {}) {
371
+ if (!this._server) {
372
+ await this._startServer()
373
+ }
374
+
375
+ const normalizedPath = urlPath.endsWith('/') ? urlPath : urlPath + '/'
376
+ this._statics.push({
377
+ urlPath: normalizedPath,
378
+ folder: path.resolve(folder),
379
+ options: {
380
+ dotfiles: options.dotfiles || 'ignore',
381
+ index: options.index || 'index.html'
382
+ }
383
+ })
384
+
385
+ console.log(`[Web] Static registered: ${normalizedPath} -> ${folder}`)
386
+ return {
387
+ success: true,
388
+ message: `Static folder registered`,
389
+ url: this._getUrl(normalizedPath)
390
+ }
391
+ }
392
+
393
+ _listRoutes() {
394
+ const baseUrl = this._getUrl('')
395
+
396
+ return {
397
+ success: true,
398
+ server: this._server ? { running: true, host: this._host, port: this._port } : { running: false },
399
+ routes: this._routes.map(r => ({ type: 'route', method: r.method, path: r.path, description: r.description })),
400
+ webhooks: Array.from(this._webhooks.values()).map(w => ({ type: 'webhook', id: w.id, path: w.path, url: `${baseUrl}${w.path}`, prompt: w.prompt })),
401
+ statics: this._statics.map(s => ({ type: 'static', path: s.urlPath, folder: s.folder, url: `${baseUrl}${s.urlPath}` }))
402
+ }
403
+ }
404
+
405
+ // ==================== HTTP 客户端 ====================
406
+
407
+ async _sendRequest(method, urlPath, body, headers) {
408
+ if (!this._server) {
409
+ return { success: false, error: 'Server not started' }
410
+ }
411
+
412
+ const url = this._getUrl(urlPath)
413
+ try {
414
+ const options = {
415
+ method: method.toUpperCase(),
416
+ headers: headers || {}
417
+ }
418
+
419
+ if (body && !['GET', 'HEAD'].includes(options.method)) {
420
+ options.body = JSON.stringify(body)
421
+ options.headers['Content-Type'] = options.headers['Content-Type'] || 'application/json'
422
+ }
423
+
424
+ const response = await fetch(url, options)
425
+ const text = await response.text()
426
+ let parsed = text
427
+ try { parsed = JSON.parse(text) } catch { /* not JSON */ }
428
+
429
+ return { success: true, status: response.status, headers: Object.fromEntries(response.headers.entries()), body: parsed }
430
+ } catch (err) {
431
+ return { success: false, error: err.message }
432
+ }
433
+ }
434
+
435
+ // ==================== 工具方法 ====================
436
+
437
+ _matchPath(routePath, reqPath) {
438
+ const routeParts = routePath.split('/').filter(Boolean)
439
+ const reqParts = reqPath.split('/').filter(Boolean)
440
+ if (routeParts.length !== reqParts.length) return false
441
+ return routeParts.every((part, i) => part.startsWith(':') || part === reqParts[i])
442
+ }
443
+
444
+ _extractParams(routePath, reqPath) {
445
+ const params = {}
446
+ const routeParts = routePath.split('/').filter(Boolean)
447
+ const reqParts = reqPath.split('/').filter(Boolean)
448
+ routeParts.forEach((part, i) => {
449
+ if (part.startsWith(':')) {
450
+ params[part.substring(1)] = reqParts[i]
451
+ }
452
+ })
453
+ return params
454
+ }
455
+
456
+ _parseQuery(c) {
457
+ const raw = c.req.queries() || {}
458
+ const query = {}
459
+ for (const [key, value] of Object.entries(raw)) {
460
+ query[key] = value.length === 1 ? value[0] : value
461
+ }
462
+ return query
463
+ }
464
+
465
+ _getUrl(path) {
466
+ if (this._baseUrl) {
467
+ return `${this._baseUrl}${path}`
468
+ }
469
+ return `http://${this._host}:${this._port}${path}`
470
+ }
471
+
472
+ async _executeHandler(handlerCode, context) {
473
+ try {
474
+ const fn = new Function('context', `return (async () => { ${handlerCode} })()`)
475
+ return await fn(context)
476
+ } catch (err) {
477
+ return { success: false, error: `Handler error: ${err.message}` }
478
+ }
479
+ }
480
+
481
+ _serveStatic(pathname) {
482
+ for (const staticFolder of this._statics) {
483
+ if (pathname.startsWith(staticFolder.urlPath)) {
484
+ const relativePath = pathname.substring(staticFolder.urlPath.length) || staticFolder.options.index
485
+ const filePath = path.join(staticFolder.folder, relativePath)
486
+
487
+ // 安全检查
488
+ if (!filePath.startsWith(staticFolder.folder)) {
489
+ return { type: 'forbidden' }
490
+ }
491
+ if (staticFolder.options.dotfiles === 'deny' && path.basename(filePath).startsWith('.')) {
492
+ return { type: 'notFound' }
493
+ }
494
+
495
+ try {
496
+ const content = fs.readFileSync(filePath)
497
+ const ext = path.extname(filePath).toLowerCase()
498
+ const contentTypes = {
499
+ '.html': 'text/html', '.css': 'text/css', '.js': 'application/javascript',
500
+ '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg',
501
+ '.gif': 'image/gif', '.svg': 'image/svg+xml', '.ico': 'image/x-icon',
502
+ '.txt': 'text/plain'
503
+ }
504
+ const contentType = contentTypes[ext] || 'application/octet-stream'
505
+ return { type: 'file', content, contentType }
506
+ } catch (err) {
507
+ if (err.code === 'ENOENT') {
508
+ return { type: 'notFound' }
509
+ }
510
+ return { type: 'error', message: err.message }
511
+ }
512
+ }
513
+ }
514
+ return null
515
+ }
516
+
517
+ _generateId() {
518
+ return Math.random().toString(36).substring(2, 10) + Date.now().toString(36)
519
+ }
520
+
521
+ _getAgent(sessionId) {
522
+ const finalSessionId = sessionId || `web_${Date.now()}`
523
+
524
+ // 尝试从对应平台插件获取 Agent
525
+ const platformPrefixes = ['weixin_', 'telegram_', 'feishu_']
526
+ for (const prefix of platformPrefixes) {
527
+ if (finalSessionId.startsWith(prefix)) {
528
+ const pluginName = prefix.replace('_', '')
529
+ const plugin = this._framework.pluginManager.get(pluginName)
530
+ if (plugin?._sessionAgents?.size > 0) {
531
+ const firstAgent = plugin._sessionAgents.values().next().value
532
+ if (firstAgent) return firstAgent
533
+ }
534
+ }
535
+ }
536
+
537
+ // 返回主 Agent
538
+ return this._framework._mainAgent || this._framework._agents?.[0]
539
+ }
540
+ }
541
+
542
+ module.exports = { WebPlugin }