@nestim/koishi-plugin-qq-group-manager 1.5.0 → 1.5.1

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/CHANGELOG.md CHANGED
@@ -2,6 +2,19 @@
2
2
 
3
3
  本文件记录所有值得注意的变更。版本号遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
4
4
 
5
+ ## 1.5.1
6
+
7
+ ### 修复
8
+
9
+ - **机器人会忘记自己刚说过的话**:
10
+ - 普通回复改为**发送前**把自身发言写入上下文,发送失败再回滚;
11
+ - 主动话题发送成功后调用 `recordSelfText()` 写回上下文;
12
+ - 解决“发送等待期间用户追问,新消息 prompt 里还没有上一句”的竞态。
13
+
14
+ ### 改进
15
+
16
+ - `ai.request.start` / `ai.request.end` 增加 `callId`,监控插件可以精确关联同一次模型调用(含重试/失败)。
17
+
5
18
  ## 1.5.0
6
19
 
7
20
  ### SDK v1(重要)
@@ -127,7 +127,11 @@ export declare class AiEngine {
127
127
  /** 追加一条系统注记(复读等),不进计数、不触发 */
128
128
  note(text: string, groupKey?: string, session?: any): void;
129
129
  /** 机器人自己发出的消息:写回上下文,解决"说完就忘" */
130
- recordSelf(session: any, text: string): void;
130
+ recordSelf(session: any, text: string, id?: string): void;
131
+ /** 主动话题等非 handle 路径发消息后,补一条自身发言到上下文 */
132
+ recordSelfText(groupId: string, text: string): void;
133
+ /** 发送失败时撤回预先写入的自身发言,避免上下文出现没发出去的话 */
134
+ private removeSelfRecord;
131
135
  /**
132
136
  * 把本次被挤出上下文的消息交给外部插件(用于自动归纳沉淀)。
133
137
  * 失败不影响主流程。
package/lib/ai/engine.js CHANGED
@@ -563,13 +563,13 @@ class AiEngine {
563
563
  this.schedulePersist();
564
564
  }
565
565
  /** 机器人自己发出的消息:写回上下文,解决"说完就忘" */
566
- recordSelf(session, text) {
566
+ recordSelf(session, text, id) {
567
567
  const key = this.groupKey(session);
568
568
  if (!key || !text?.trim())
569
569
  return;
570
570
  const state = this.state.get(key);
571
571
  const evicted = state.push({
572
- id: `self-${this.now()}-${Math.random().toString(36).slice(2, 6)}`,
572
+ id: id || `self-${this.now()}-${Math.random().toString(36).slice(2, 6)}`,
573
573
  userId: String(session.bot?.selfId ?? ''),
574
574
  name: this.selfName,
575
575
  role: 'unknown',
@@ -583,6 +583,24 @@ class AiEngine {
583
583
  this.emitEvicted(evicted, key, session);
584
584
  this.schedulePersist();
585
585
  }
586
+ /** 主动话题等非 handle 路径发消息后,补一条自身发言到上下文 */
587
+ recordSelfText(groupId, text) {
588
+ const bot = this.findBot();
589
+ this.recordSelf({
590
+ guildId: String(groupId),
591
+ platform: 'onebot',
592
+ bot: { selfId: bot?.selfId },
593
+ }, text);
594
+ }
595
+ /** 发送失败时撤回预先写入的自身发言,避免上下文出现没发出去的话 */
596
+ removeSelfRecord(session, id) {
597
+ const key = this.groupKey(session);
598
+ const state = key ? this.state.peek(key) : undefined;
599
+ if (!state)
600
+ return;
601
+ state.records = state.records.filter((record) => record.id !== id);
602
+ this.schedulePersist();
603
+ }
586
604
  /**
587
605
  * 把本次被挤出上下文的消息交给外部插件(用于自动归纳沉淀)。
588
606
  * 失败不影响主流程。
@@ -1407,6 +1425,9 @@ class AiEngine {
1407
1425
  at: renderedAt,
1408
1426
  renderedMs: renderedAt - startedAt,
1409
1427
  });
1428
+ const selfId = `self-${this.now()}-${Math.random().toString(36).slice(2, 6)}`;
1429
+ // 先写入上下文再发送:发送等待期间用户追问,也能看到这句;失败再撤回
1430
+ this.recordSelf(session, finalText, selfId);
1410
1431
  const sendStartedAt = this.now();
1411
1432
  let sendError = null;
1412
1433
  try {
@@ -1414,6 +1435,7 @@ class AiEngine {
1414
1435
  }
1415
1436
  catch (error) {
1416
1437
  sendError = error;
1438
+ this.removeSelfRecord(session, selfId);
1417
1439
  throw error;
1418
1440
  }
1419
1441
  finally {
@@ -1429,7 +1451,6 @@ class AiEngine {
1429
1451
  });
1430
1452
  }
1431
1453
  state.counter = 0;
1432
- this.recordSelf(session, finalText);
1433
1454
  if (mention)
1434
1455
  this.startFollowup(state, record);
1435
1456
  else if (followup && state.followup)
@@ -1472,11 +1493,13 @@ class AiEngine {
1472
1493
  async callModel(request, meta = {}) {
1473
1494
  const startedAt = this.now();
1474
1495
  const traceId = String(meta.traceId || newTraceId());
1496
+ const callId = `${traceId}-${String(meta.kind || 'reply')}-${startedAt.toString(36)}-${Math.random().toString(36).slice(2, 5)}`;
1475
1497
  const kind = String(meta.kind || 'reply');
1476
1498
  const groupId = String(meta.groupId || '');
1477
1499
  const model = String(request?.model || this.config.aiModel || '');
1478
1500
  this.emit('ai.request.start', {
1479
1501
  traceId,
1502
+ callId,
1480
1503
  kind,
1481
1504
  groupId,
1482
1505
  trigger: meta.trigger,
@@ -1491,6 +1514,7 @@ class AiEngine {
1491
1514
  const endedAt = this.now();
1492
1515
  this.emit('ai.request.end', {
1493
1516
  traceId,
1517
+ callId,
1494
1518
  kind,
1495
1519
  groupId,
1496
1520
  trigger: meta.trigger,
@@ -1508,6 +1532,7 @@ class AiEngine {
1508
1532
  const endedAt = this.now();
1509
1533
  this.emit('ai.request.end', {
1510
1534
  traceId,
1535
+ callId,
1511
1536
  kind,
1512
1537
  groupId,
1513
1538
  trigger: meta.trigger,
package/lib/service.js CHANGED
@@ -566,6 +566,10 @@ class QQGroupManagerService extends koishi_1.Service {
566
566
  if (!text)
567
567
  return;
568
568
  await this.sendGroupText(groupId, text);
569
+ try {
570
+ this.ai.recordSelfText(groupId, text);
571
+ }
572
+ catch { /* ignore */ }
569
573
  this._lastIdleTopicAt.set(groupId, Date.now());
570
574
  this._logger.info(`[idle] topic sent group=${groupId} chars=${text.length}`);
571
575
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nestim/koishi-plugin-qq-group-manager",
3
3
  "description": "Koishi QQ群管理插件(OneBot/LLOneBot):群管命令、权限校验、图片菜单、状态卡片、复读、AI群聊回复、记忆库、识图与自动管控(禁言/踢人)。",
4
- "version": "1.5.0",
4
+ "version": "1.5.1",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
package/src/ai/engine.ts CHANGED
@@ -609,13 +609,13 @@ export class AiEngine {
609
609
  }
610
610
 
611
611
  /** 机器人自己发出的消息:写回上下文,解决"说完就忘" */
612
- recordSelf(session: any, text: string): void {
612
+ recordSelf(session: any, text: string, id?: string): void {
613
613
  const key = this.groupKey(session);
614
614
  if (!key || !text?.trim()) return;
615
615
  const state = this.state.get(key);
616
616
  const evicted = state.push(
617
617
  {
618
- id: `self-${this.now()}-${Math.random().toString(36).slice(2, 6)}`,
618
+ id: id || `self-${this.now()}-${Math.random().toString(36).slice(2, 6)}`,
619
619
  userId: String(session.bot?.selfId ?? ''),
620
620
  name: this.selfName,
621
621
  role: 'unknown',
@@ -632,6 +632,25 @@ export class AiEngine {
632
632
  this.schedulePersist();
633
633
  }
634
634
 
635
+ /** 主动话题等非 handle 路径发消息后,补一条自身发言到上下文 */
636
+ recordSelfText(groupId: string, text: string): void {
637
+ const bot = this.findBot();
638
+ this.recordSelf({
639
+ guildId: String(groupId),
640
+ platform: 'onebot',
641
+ bot: { selfId: bot?.selfId },
642
+ }, text);
643
+ }
644
+
645
+ /** 发送失败时撤回预先写入的自身发言,避免上下文出现没发出去的话 */
646
+ private removeSelfRecord(session: any, id: string): void {
647
+ const key = this.groupKey(session);
648
+ const state = key ? this.state.peek(key) : undefined;
649
+ if (!state) return;
650
+ state.records = state.records.filter((record) => record.id !== id);
651
+ this.schedulePersist();
652
+ }
653
+
635
654
  /**
636
655
  * 把本次被挤出上下文的消息交给外部插件(用于自动归纳沉淀)。
637
656
  * 失败不影响主流程。
@@ -1487,12 +1506,16 @@ export class AiEngine {
1487
1506
  at: renderedAt,
1488
1507
  renderedMs: renderedAt - startedAt,
1489
1508
  });
1509
+ const selfId = `self-${this.now()}-${Math.random().toString(36).slice(2, 6)}`;
1510
+ // 先写入上下文再发送:发送等待期间用户追问,也能看到这句;失败再撤回
1511
+ this.recordSelf(session, finalText, selfId);
1490
1512
  const sendStartedAt = this.now();
1491
1513
  let sendError: any = null;
1492
1514
  try {
1493
1515
  await this.host.sendReply(session, finalText);
1494
1516
  } catch (error: any) {
1495
1517
  sendError = error;
1518
+ this.removeSelfRecord(session, selfId);
1496
1519
  throw error;
1497
1520
  } finally {
1498
1521
  const sentAt = this.now();
@@ -1507,7 +1530,6 @@ export class AiEngine {
1507
1530
  });
1508
1531
  }
1509
1532
  state.counter = 0;
1510
- this.recordSelf(session, finalText);
1511
1533
 
1512
1534
  if (mention) this.startFollowup(state, record);
1513
1535
  else if (followup && state.followup) this.bumpFollowup(state, record);
@@ -1550,11 +1572,13 @@ export class AiEngine {
1550
1572
  private async callModel(request: any, meta: any = {}): Promise<any> {
1551
1573
  const startedAt = this.now();
1552
1574
  const traceId = String(meta.traceId || newTraceId());
1575
+ const callId = `${traceId}-${String(meta.kind || 'reply')}-${startedAt.toString(36)}-${Math.random().toString(36).slice(2, 5)}`;
1553
1576
  const kind = String(meta.kind || 'reply');
1554
1577
  const groupId = String(meta.groupId || '');
1555
1578
  const model = String(request?.model || this.config.aiModel || '');
1556
1579
  this.emit('ai.request.start', {
1557
1580
  traceId,
1581
+ callId,
1558
1582
  kind,
1559
1583
  groupId,
1560
1584
  trigger: meta.trigger,
@@ -1569,6 +1593,7 @@ export class AiEngine {
1569
1593
  const endedAt = this.now();
1570
1594
  this.emit('ai.request.end', {
1571
1595
  traceId,
1596
+ callId,
1572
1597
  kind,
1573
1598
  groupId,
1574
1599
  trigger: meta.trigger,
@@ -1587,6 +1612,7 @@ export class AiEngine {
1587
1612
  const endedAt = this.now();
1588
1613
  this.emit('ai.request.end', {
1589
1614
  traceId,
1615
+ callId,
1590
1616
  kind,
1591
1617
  groupId,
1592
1618
  trigger: meta.trigger,
package/src/service.js CHANGED
@@ -569,6 +569,10 @@ class QQGroupManagerService extends koishi_1.Service {
569
569
  if (!text)
570
570
  return;
571
571
  await this.sendGroupText(groupId, text);
572
+ try {
573
+ this.ai.recordSelfText(groupId, text);
574
+ }
575
+ catch { /* ignore */ }
572
576
  this._lastIdleTopicAt.set(groupId, Date.now());
573
577
  this._logger.info(`[idle] topic sent group=${groupId} chars=${text.length}`);
574
578
  }