foliko 1.0.56 → 1.0.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foliko",
3
- "version": "1.0.56",
3
+ "version": "1.0.58",
4
4
  "description": "简约的插件化 Agent 框架",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -9,7 +9,7 @@
9
9
  */
10
10
 
11
11
  const { Plugin } = require('../src/core/plugin-base')
12
-
12
+ const {cleanResponse} =require('../src/utils')
13
13
  class FeishuPlugin extends Plugin {
14
14
  constructor(config = {}) {
15
15
  super()
@@ -284,7 +284,7 @@ class FeishuPlugin extends Plugin {
284
284
  try {
285
285
  // 使用非流式响应
286
286
  const result = await agent.chat(text, { sessionId })
287
- const fullResponse = result.message || ''
287
+ const fullResponse = cleanResponse(result.message || '')
288
288
 
289
289
  if (this._sessionPlugin) {
290
290
  this._sessionPlugin.addMessage(sessionId, { role: 'assistant', content: fullResponse })
@@ -8,6 +8,7 @@
8
8
  */
9
9
 
10
10
  const { Plugin } = require('../src/core/plugin-base')
11
+ const {cleanResponse} =require('../src/utils')
11
12
  const { z } = require('zod')
12
13
  const { WeixinBot } = require('@chnak/weixin-bot')
13
14
 
@@ -246,24 +247,24 @@ class WeixinPlugin extends Plugin {
246
247
  const result = await agent.chat(text, {
247
248
  sessionId: sessionId
248
249
  })
249
- const fullResponse = result.message || ''
250
+ const fullResponse = cleanResponse(result.message || '')
250
251
 
251
252
  // 保存助手回复到历史(使用 SessionPlugin)
252
253
  if (this._sessionPlugin) {
253
254
  this._sessionPlugin.addMessage(sessionId, { role: 'assistant', content: fullResponse })
254
255
  }
255
256
 
256
- // 发送回复
257
+ // 发送回复(超过500字自动分批)
257
258
  if (fullResponse) {
258
- await this._bot.reply(originalMsg, fullResponse)
259
+ await this._sendMessageBatch(originalMsg, userId, fullResponse, true)
259
260
  console.log(`[WeChat] 回复成功 (${fullResponse.length} 字符)`)
260
261
  } else {
261
- await this._bot.reply(originalMsg, '抱歉,我没有收到有效的回复。')
262
+ await this._sendMessageBatch(originalMsg, userId, '抱歉,我没有收到有效的回复。', true)
262
263
  }
263
264
 
264
265
  } catch (err) {
265
266
  console.error('[WeChat] Chat error:', err)
266
- await this._bot.reply(originalMsg, `发生错误:${err.message}`)
267
+ await this._sendMessageBatch(originalMsg, userId, `发生错误:${err.message}`, true)
267
268
  }
268
269
  }
269
270
 
@@ -277,6 +278,46 @@ class WeixinPlugin extends Plugin {
277
278
  return this.config.allowedUsers.includes(userId)
278
279
  }
279
280
 
281
+ /**
282
+ * 发送消息(超过500字自动分批)
283
+ * @param {Object} originalMsg - 原始消息对象(用于reply)
284
+ * @param {string} userId - 用户ID(用于sendText)
285
+ * @param {string} text - 要发送的文本
286
+ * @param {boolean} useReply - 是否使用reply方式
287
+ */
288
+ async _sendMessageBatch(originalMsg, userId, text, useReply = true) {
289
+ const MAX_LEN = 500
290
+ if (!text || text.length <= MAX_LEN) {
291
+ if (useReply && originalMsg) {
292
+ await this._bot.reply(originalMsg, text)
293
+ } else {
294
+ await this._bot.sendText(userId, text)
295
+ }
296
+ return
297
+ }
298
+
299
+ // 分批发送
300
+ const chunks = []
301
+ for (let i = 0; i < text.length; i += MAX_LEN) {
302
+ chunks.push(text.slice(i, i + MAX_LEN))
303
+ }
304
+
305
+ console.log(`[WeChat] Message too long (${text.length}), splitting into ${chunks.length} parts`)
306
+
307
+ for (let i = 0; i < chunks.length; i++) {
308
+ const chunk = `[${i + 1}/${chunks.length}]\n${chunks[i]}`
309
+ if (useReply && originalMsg) {
310
+ await this._bot.reply(originalMsg, chunk)
311
+ } else {
312
+ await this._bot.sendText(userId, chunk)
313
+ }
314
+ // 批次间隔,避免发送太快
315
+ if (i < chunks.length - 1) {
316
+ await new Promise(r => setTimeout(r, 300))
317
+ }
318
+ }
319
+ }
320
+
280
321
  /**
281
322
  * 清除会话
282
323
  */
@@ -306,7 +347,7 @@ class WeixinPlugin extends Plugin {
306
347
  if (sessionId && sessionId.startsWith('weixin_')) {
307
348
  const userId = sessionId.replace('weixin_', '')
308
349
  try {
309
- await this._bot.sendText(userId, reminderText)
350
+ await this._sendMessageBatch(null, userId, reminderText, false)
310
351
  console.log(`[WeChat] Reminder sent to user ${userId}`)
311
352
  } catch (err) {
312
353
  console.error(`[WeChat] Failed to send reminder:`, err.message)
@@ -324,7 +365,7 @@ class WeixinPlugin extends Plugin {
324
365
  if (weixinSessions.length > 0) {
325
366
  const userId = weixinSessions[0].id.replace('weixin_', '')
326
367
  try {
327
- await this._bot.sendText(userId, reminderText)
368
+ await this._sendMessageBatch(null, userId, reminderText, false)
328
369
  console.log(`[WeChat] Reminder sent to recent user ${userId}`)
329
370
  } catch (err) {
330
371
  console.error(`[WeChat] Failed to send reminder:`, err.message)
@@ -355,7 +396,7 @@ class WeixinPlugin extends Plugin {
355
396
  const notificationText = `📥 [Webhook 接收]\n\n路径: ${webhookData.path}\n方法: ${webhookData.method}\n\n处理结果: ${response || '处理中...'}`
356
397
 
357
398
  try {
358
- await this._bot.sendText(userId, notificationText)
399
+ await this._sendMessageBatch(null, userId, notificationText, false)
359
400
  console.log(`[WeChat] Webhook notification sent to user ${userId}`)
360
401
  } catch (err) {
361
402
  console.error(`[WeChat] Failed to send webhook notification:`, err.message)
@@ -0,0 +1,3 @@
1
+ exports.cleanResponse=function (text) {
2
+ return text.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
3
+ }