foliko 1.0.57 → 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.57",
3
+ "version": "1.0.58",
4
4
  "description": "简约的插件化 Agent 框架",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -254,17 +254,17 @@ class WeixinPlugin extends Plugin {
254
254
  this._sessionPlugin.addMessage(sessionId, { role: 'assistant', content: fullResponse })
255
255
  }
256
256
 
257
- // 发送回复
257
+ // 发送回复(超过500字自动分批)
258
258
  if (fullResponse) {
259
- await this._bot.reply(originalMsg, fullResponse)
259
+ await this._sendMessageBatch(originalMsg, userId, fullResponse, true)
260
260
  console.log(`[WeChat] 回复成功 (${fullResponse.length} 字符)`)
261
261
  } else {
262
- await this._bot.reply(originalMsg, '抱歉,我没有收到有效的回复。')
262
+ await this._sendMessageBatch(originalMsg, userId, '抱歉,我没有收到有效的回复。', true)
263
263
  }
264
264
 
265
265
  } catch (err) {
266
266
  console.error('[WeChat] Chat error:', err)
267
- await this._bot.reply(originalMsg, `发生错误:${err.message}`)
267
+ await this._sendMessageBatch(originalMsg, userId, `发生错误:${err.message}`, true)
268
268
  }
269
269
  }
270
270
 
@@ -278,6 +278,46 @@ class WeixinPlugin extends Plugin {
278
278
  return this.config.allowedUsers.includes(userId)
279
279
  }
280
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
+
281
321
  /**
282
322
  * 清除会话
283
323
  */
@@ -307,7 +347,7 @@ class WeixinPlugin extends Plugin {
307
347
  if (sessionId && sessionId.startsWith('weixin_')) {
308
348
  const userId = sessionId.replace('weixin_', '')
309
349
  try {
310
- await this._bot.sendText(userId, reminderText)
350
+ await this._sendMessageBatch(null, userId, reminderText, false)
311
351
  console.log(`[WeChat] Reminder sent to user ${userId}`)
312
352
  } catch (err) {
313
353
  console.error(`[WeChat] Failed to send reminder:`, err.message)
@@ -325,7 +365,7 @@ class WeixinPlugin extends Plugin {
325
365
  if (weixinSessions.length > 0) {
326
366
  const userId = weixinSessions[0].id.replace('weixin_', '')
327
367
  try {
328
- await this._bot.sendText(userId, reminderText)
368
+ await this._sendMessageBatch(null, userId, reminderText, false)
329
369
  console.log(`[WeChat] Reminder sent to recent user ${userId}`)
330
370
  } catch (err) {
331
371
  console.error(`[WeChat] Failed to send reminder:`, err.message)
@@ -356,7 +396,7 @@ class WeixinPlugin extends Plugin {
356
396
  const notificationText = `📥 [Webhook 接收]\n\n路径: ${webhookData.path}\n方法: ${webhookData.method}\n\n处理结果: ${response || '处理中...'}`
357
397
 
358
398
  try {
359
- await this._bot.sendText(userId, notificationText)
399
+ await this._sendMessageBatch(null, userId, notificationText, false)
360
400
  console.log(`[WeChat] Webhook notification sent to user ${userId}`)
361
401
  } catch (err) {
362
402
  console.error(`[WeChat] Failed to send webhook notification:`, err.message)