foliko 1.1.79 → 1.1.80

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.1.79",
3
+ "version": "1.1.80",
4
4
  "description": "简约的插件化 Agent 框架",
5
5
  "main": "src/index.js",
6
6
  "type": "commonjs",
@@ -378,7 +378,7 @@ class WeixinPlugin extends Plugin {
378
378
  }
379
379
 
380
380
  _on_event_message(){
381
- const {sessionId}=this.ge
381
+ const {sessionId}=this
382
382
  const sessionScope = this.agent.createSessionScope(sessionId);
383
383
  sessionScope.on('stream:chunk', ({ chunk }) => {})
384
384
  }
@@ -388,25 +388,58 @@ class WeixinPlugin extends Plugin {
388
388
  */
389
389
  _setupSessionScopeListeners(agent, sessionId, originalMsg, userId) {
390
390
  const sessionScope = agent.createSessionScope(sessionId)
391
+ const renderState = { inThink: false, inCodeBlock: false };
392
+ const lineBuffer = { value: '' };
393
+
394
+ // 流式文本处理
395
+ sessionScope.on('stream:chunk', ({ chunk }) => {
396
+ if (chunk.type === 'text') {
397
+ lineBuffer.value += chunk.text;
398
+ } else if (chunk.type === 'tool-call') {
399
+ const args = chunk.input ? JSON.stringify(chunk.input).slice(0, 30) : '';
400
+ log.info(`[Tool] ${chunk.toolName} ${args}...`);
401
+ } else if (chunk.type === 'tool-result') {
402
+ const result = chunk.result;
403
+ const shortResult = typeof result === 'string' ? result.slice(0, 30) : JSON.stringify(result).slice(0, 30);
404
+ log.info(`[Tool] ${chunk.toolName} ${shortResult}...`);
405
+ } else if (chunk.type === 'usage') {
406
+ log.debug(`[Usage] input: ${chunk.inputTokens || 0}, output: ${chunk.outputTokens || 0}`);
407
+ }
408
+ });
409
+
410
+ // 消息开始
411
+ sessionScope.on('message:start', async () => {
412
+ this.startTypingInterval();
413
+ });
391
414
 
415
+ // 消息完成
392
416
  sessionScope.on('message:complete', async ({ content }) => {
393
- this.stopTypingInterval()
394
- if (content) {
395
- await this._sendMessageBatch(originalMsg, userId, content, true)
396
- } else {
397
- const msg = '继续进行下一步'
398
- await this._sendMessageBatch(originalMsg, userId, msg, true)
399
- await agent.sendMessage(msg, { sessionId, priority: 1 })
417
+
418
+ if (!content) {
419
+ const msg = '继续进行下一步';
420
+ //await this._sendMessageBatch(originalMsg, userId, msg, true);
421
+ await agent.sendMessage(msg, { sessionId, priority: 1 });
400
422
  }
401
- })
423
+ });
424
+
425
+ // 队列完成
426
+ sessionScope.on('queue:completed', async () => {
427
+ this.stopTypingInterval();
428
+ if (lineBuffer.value.trim()) {
429
+ await this._sendMessageBatch(originalMsg, userId, lineBuffer.value.trim(), true);
430
+ //log.debug(`[Queue] Remaining buffer: ${lineBuffer.value}`);
431
+ }
432
+ lineBuffer.value = '';
433
+ });
402
434
 
435
+ // 队列失败
403
436
  sessionScope.on('queue:failed', async ({ error }) => {
404
- this.stopTypingInterval()
405
- await this._sendMessageBatch(originalMsg, userId, error, true)
406
- })
437
+ this.stopTypingInterval();
438
+ await this._sendMessageBatch(originalMsg, userId, error, true);
439
+ });
407
440
 
408
- this._sessionScopes.set(sessionId, sessionScope)
409
- return sessionScope
441
+ this._sessionScopes.set(sessionId, sessionScope);
442
+ return sessionScope;
410
443
  }
411
444
 
412
445
  /**