dsh-multigameassistant 2.3.2 → 2.3.3

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/cordis.patch.yml CHANGED
@@ -1,4 +1,4 @@
1
- # dsh-multigameassistant 2.3.2
1
+ # dsh-multigameassistant 2.3.3
2
2
  # dsh-multigameassistant: the app's plugins (packages/bundle/cordis.patch.yml, each @adg/<plugin> as dsh-multigameassistant/<plugin>)
3
3
  # sharing the dsh with the person's other chats, and the app's agent preset (dsh's standard one with the app's
4
4
  # tools and prompts, and compaction as 设置 → 对话压缩 says). No row of dsh is restated.
package/lib/analyzer.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  discoverMcp,
4
4
  discoverSkills,
5
5
  mcpEndpoints
6
- } from "./chunk-HRP7ALS6.js";
6
+ } from "./chunk-AXOXWMQT.js";
7
7
  import {
8
8
  probeProject
9
9
  } from "./chunk-B4UT4NYH.js";
@@ -20,7 +20,7 @@ import {
20
20
  strayKeys,
21
21
  stripJsonc
22
22
  } from "./chunk-7UJ3LK3G.js";
23
- import "./chunk-SMXJWLAZ.js";
23
+ import "./chunk-RREVLKRY.js";
24
24
  import "./chunk-OCJFAT7J.js";
25
25
  import "./chunk-4NR36PKL.js";
26
26
  import "./chunk-RESS7WVY.js";
@@ -38,7 +38,7 @@ import {
38
38
  } from "./chunk-7UJ3LK3G.js";
39
39
  import {
40
40
  sessionConversations
41
- } from "./chunk-SMXJWLAZ.js";
41
+ } from "./chunk-RREVLKRY.js";
42
42
  import {
43
43
  pluginVersion
44
44
  } from "./chunk-OCJFAT7J.js";
@@ -23070,4 +23070,4 @@ export {
23070
23070
  AdgScripts,
23071
23071
  lib_default
23072
23072
  };
23073
- /*! For license information please see chunk-HRP7ALS6.js.LEGAL.txt */
23073
+ /*! For license information please see chunk-AXOXWMQT.js.LEGAL.txt */
@@ -1,5 +1,6 @@
1
1
  import { createRequire as __adgCreateRequire } from 'node:module'; import { dirname as __adgDirname } from 'node:path'; import { fileURLToPath as __adgFileURLToPath } from 'node:url'; const require = __adgCreateRequire(import.meta.url); const __filename = __adgFileURLToPath(import.meta.url); const __dirname = __adgDirname(__filename);
2
2
  import {
3
+ convertLiveFrame,
3
4
  convertRecord
4
5
  } from "./chunk-OCJFAT7J.js";
5
6
  import {
@@ -98,6 +99,20 @@ import { randomUUID as randomUUID2 } from "node:crypto";
98
99
  function textReply(text) {
99
100
  return { blocks: [{ kind: "text", text }] };
100
101
  }
102
+ function saidKey(seq, text) {
103
+ return `${seq}:${text}`;
104
+ }
105
+ function finalKey(reply) {
106
+ for (let i = reply.blocks.length - 1; i >= 0; i--) {
107
+ const block = reply.blocks[i];
108
+ if (block.kind === "text" && block.text.trim() !== "")
109
+ return block.seq === void 0 ? void 0 : saidKey(block.seq, block.text);
110
+ }
111
+ return void 0;
112
+ }
113
+ function replyRest(reply) {
114
+ return [reply.note, reply.ended].filter((p) => p !== void 0 && p !== "").join("\n\n");
115
+ }
101
116
  function finalText(reply) {
102
117
  for (let i = reply.blocks.length - 1; i >= 0; i--) {
103
118
  const block = reply.blocks[i];
@@ -505,7 +520,8 @@ function sessionConversations(sessions, _cwd = () => void 0, focus, app) {
505
520
  signal.throwIfAborted();
506
521
  options.onAccepted?.();
507
522
  const settled = app?.()?.settled;
508
- return await workReply(iterator, requestId, text, turn, settled === void 0 ? void 0 : () => settled.call(app(), sessionId, observing), options.onWorkerReport);
523
+ return await workReply(iterator, requestId, text, turn, settled === void 0 ? void 0 : () => settled.call(app(), sessionId, observing), options.onWorkerReport, options.onSay === void 0 && options.onTyping === void 0 ? void 0 : { say: options.onSay ?? (() => {
524
+ }), ...options.onTyping === void 0 ? {} : { typing: options.onTyping } });
509
525
  } finally {
510
526
  controller.abort();
511
527
  if (signal.aborted && iterator !== void 0)
@@ -619,7 +635,7 @@ var TurnReply = class {
619
635
  if (event.type === "assistant/message") {
620
636
  const said = event.content.map((p) => p.type === "text" ? p.text : "").join("").trim();
621
637
  if (said !== "")
622
- blocks.push({ kind: "text", text: said });
638
+ blocks.push({ kind: "text", text: said, seq: event.seq });
623
639
  } else if (event.type === "tool/call") {
624
640
  if (blocks.at(-1)?.kind !== "steps")
625
641
  blocks.push({ kind: "steps", steps: [] });
@@ -647,7 +663,83 @@ var TurnReply = class {
647
663
  return { blocks: [...this.#blocks], ...this.#outcome === void 0 ? {} : { outcome: this.#outcome }, ...this.#ended === void 0 ? {} : { ended: this.#ended } };
648
664
  }
649
665
  };
650
- async function workReply(iterator, requestId, text, turn, settled, onWorkerReport) {
666
+ var KEEP_TYPED = 32;
667
+ var LiveWords = class {
668
+ #sink;
669
+ #held;
670
+ /** Text typed per attempt, by content block. */
671
+ #typed = /* @__PURE__ */ new Map();
672
+ /** Attempts committed before their event was seen: event seq → attempt. */
673
+ #committed = /* @__PURE__ */ new Map();
674
+ /** Events seen before their attempt's end: seq → the words' key (null: no words to show). */
675
+ #events = /* @__PURE__ */ new Map();
676
+ constructor(sink) {
677
+ this.#sink = typeof sink === "function" ? { say: sink } : sink;
678
+ }
679
+ add(event) {
680
+ if (event.type !== "assistant/message" && event.type !== "tool/call")
681
+ return;
682
+ if (this.#held !== void 0)
683
+ this.#sink.say(this.#held);
684
+ this.#held = void 0;
685
+ if (event.type !== "assistant/message")
686
+ return;
687
+ const text = event.content.map((p) => p.type === "text" ? p.text : "").join("").trim();
688
+ const shown2 = text !== "" && !isQuiet(text);
689
+ if (shown2)
690
+ this.#held = { text, key: saidKey(event.seq, text) };
691
+ if (this.#sink.typing === void 0)
692
+ return;
693
+ const key = shown2 ? saidKey(event.seq, text) : null;
694
+ const attempt = this.#committed.get(event.seq);
695
+ if (attempt === void 0)
696
+ keep(this.#events, event.seq, key);
697
+ else {
698
+ this.#committed.delete(event.seq);
699
+ this.#settle(attempt, key);
700
+ }
701
+ }
702
+ /** A frame of dsh's assistant stream (the step being typed). */
703
+ frame(frame) {
704
+ const typing = this.#sink.typing;
705
+ if (typing === void 0)
706
+ return;
707
+ if (frame.type === "start")
708
+ this.#typed.set(frame.attemptId, []);
709
+ else if (frame.type === "delta") {
710
+ if (frame.kind !== "text")
711
+ return;
712
+ let parts = this.#typed.get(frame.attemptId);
713
+ if (parts === void 0)
714
+ this.#typed.set(frame.attemptId, parts = []);
715
+ parts[frame.index] = (parts[frame.index] ?? "") + frame.text;
716
+ const text = parts.join("").trim();
717
+ if (text !== "")
718
+ typing({ kind: "typing", id: frame.attemptId, text });
719
+ } else {
720
+ this.#typed.delete(frame.attemptId);
721
+ if (frame.outcome !== "committed" || frame.seq === void 0)
722
+ return typing({ kind: "dropped", id: frame.attemptId });
723
+ const key = this.#events.get(frame.seq);
724
+ if (key === void 0)
725
+ keep(this.#committed, frame.seq, frame.attemptId);
726
+ else {
727
+ this.#events.delete(frame.seq);
728
+ this.#settle(frame.attemptId, key);
729
+ }
730
+ }
731
+ }
732
+ #settle(attempt, key) {
733
+ this.#sink.typing?.(key === null ? { kind: "dropped", id: attempt } : { kind: "committed", id: attempt, key });
734
+ }
735
+ };
736
+ function keep(map, seq, value) {
737
+ map.set(seq, value);
738
+ while (map.size > KEEP_TYPED)
739
+ map.delete(map.keys().next().value);
740
+ }
741
+ async function workReply(iterator, requestId, text, turn, settled, onWorkerReport, words) {
742
+ const live = words === void 0 ? void 0 : new LiveWords(words);
651
743
  const inbox = {};
652
744
  let cancelled = false;
653
745
  let mine = false;
@@ -658,6 +750,12 @@ async function workReply(iterator, requestId, text, turn, settled, onWorkerRepor
658
750
  let reply = new TurnReply();
659
751
  let switched = false;
660
752
  const take = (frame) => {
753
+ if (frame.type === "assistant-stream") {
754
+ const typed = mine && !other && live !== void 0 ? convertLiveFrame(frame.frame) : null;
755
+ if (typed !== null)
756
+ live.frame(typed);
757
+ return false;
758
+ }
661
759
  if (frame.type !== "event")
662
760
  return false;
663
761
  if (!mine) {
@@ -699,6 +797,7 @@ async function workReply(iterator, requestId, text, turn, settled, onWorkerRepor
699
797
  other = false;
700
798
  return false;
701
799
  }
800
+ live?.add(event);
702
801
  if (event.type === "tool/call" && SUB_AGENT_TOOLS.has(event.name))
703
802
  opened = true;
704
803
  if (event.type === "turn/end" && event.reason.kind === "error" && afterSwitch) {
@@ -1113,6 +1212,10 @@ var Notify = class {
1113
1212
  * anything else is a conversation with the agent.
1114
1213
  * @returns the reply to send back, for the channel to render.
1115
1214
  */
1215
+ /**
1216
+ * @param message_.onSay - each thing the agent says while it works, as it says it; the reply then ends with its last words.
1217
+ * @param message_.onTyping - the agent's words while it types them (all of them, the last words too).
1218
+ */
1116
1219
  async inbound(message_) {
1117
1220
  const text = message_.text.trim();
1118
1221
  const key = `${message_.actor.channel}:${message_.actor.userId}`;
@@ -1149,12 +1252,13 @@ var Notify = class {
1149
1252
  ${text}`;
1150
1253
  }
1151
1254
  const signal = message_.signal ?? AbortSignal.timeout(20 * 6e4);
1152
- prompt = `${channelNote(message_.actor.channel)}
1255
+ const live = message_.onSay !== void 0 && conversations.converse !== void 0;
1256
+ prompt = `${channelNote(message_.actor.channel, live)}
1153
1257
  ${prompt}`;
1154
1258
  this.#origins.set(prompt.trim(), message_.actor);
1155
1259
  while (this.#origins.size > KEEP_ORIGINS)
1156
1260
  this.#origins.delete(this.#origins.keys().next().value);
1157
- return conversations.converse !== void 0 ? await conversations.converse(sessionId, prompt, signal, [], { insert: true }) : textReply(await conversations.ask(sessionId, prompt, signal));
1261
+ return conversations.converse !== void 0 ? await conversations.converse(sessionId, prompt, signal, [], { insert: true, ...live ? { onSay: message_.onSay } : {}, ...live && message_.onTyping !== void 0 ? { onTyping: message_.onTyping } : {} }) : textReply(await conversations.ask(sessionId, prompt, signal));
1158
1262
  }
1159
1263
  /** The agent waits for this person's answer (a question, an approval): their next message is it, not one to queue. */
1160
1264
  waiting(actor) {
@@ -1373,7 +1477,9 @@ ${text}
1373
1477
  "\u4E3B\u4EBA\u7684\u8BDD\u662F\u5728\u56DE\u5E94\u67D0\u6761\u8FD8\u5728\u7B49\u9009\u62E9\u7684\u901A\u77E5\u65F6\uFF08\u4F8B\u5982\u300C\u6539\u5427\u300D\u300C\u597D\u300D\u300C\u4E0D\u7528\u4E86\u300D\u300C\u505C\u300D\uFF09\uFF0C\u7528 notice_act \u66FF\u4ED6\u9009\u5BF9\u5E94\u7684\u64CD\u4F5C\uFF0C\u518D\u544A\u8BC9\u4ED6\u7ED3\u679C\uFF1B\u62FF\u4E0D\u51C6\u6307\u54EA\u6761\u5C31\u95EE\u3002\u8DDF\u901A\u77E5\u65E0\u5173\u5C31\u7167\u5E38\u56DE\u7B54\uFF0C\u4E0D\u7528\u63D0\u8FD9\u4E9B\u901A\u77E5\u3002"
1374
1478
  ].join("\n");
1375
1479
  }
1376
- function channelNote(channel) {
1480
+ function channelNote(channel, live = false) {
1481
+ if (live)
1482
+ return `\uFF08\u8FD9\u6761\u6D88\u606F\u6765\u81EA${CHANNEL_NAMES[channel] ?? channel}\u3002\u4F60\u8BF4\u7684\u6BCF\u6BB5\u8BDD\u90FD\u4F1A\u9A6C\u4E0A\u53D1\u7ED9\u5BF9\u65B9\uFF0C\u6B65\u9AA4\u4E0D\u4F1A\uFF1A\u8981\u505A\u4E00\u9635\u7684\u4E8B\u5148\u8BF4\u4E00\u53E5\u5728\u505A\u4EC0\u4E48\uFF1B\u6700\u540E\u7528\u51E0\u53E5\u8BDD\u603B\u7ED3\u8FD9\u4E00\u8F6E\u2014\u2014\u7ED3\u8BBA\u3001\u505A\u4E86\u4EC0\u4E48\u3001\u9700\u8981\u5BF9\u65B9\u505A\u4EC0\u4E48\u3002\uFF09`;
1377
1483
  return `\uFF08\u8FD9\u6761\u6D88\u606F\u6765\u81EA${CHANNEL_NAMES[channel] ?? channel}\u3002\u53EA\u6709\u4F60\u8FD9\u4E00\u8F6E\u7684\u6700\u540E\u4E00\u6761\u6D88\u606F\u4F1A\u53D1\u7ED9\u5BF9\u65B9\uFF0C\u4E2D\u95F4\u7684\u8BDD\u548C\u6B65\u9AA4\u5BF9\u65B9\u770B\u4E0D\u5230\uFF1A\u6700\u540E\u7528\u51E0\u53E5\u8BDD\u603B\u7ED3\u8FD9\u4E00\u8F6E\u2014\u2014\u7ED3\u8BBA\u3001\u505A\u4E86\u4EC0\u4E48\u3001\u9700\u8981\u5BF9\u65B9\u505A\u4EC0\u4E48\u3002\uFF09`;
1378
1484
  }
1379
1485
  var CHANNEL_NAMES = { feishu: "\u98DE\u4E66", qq: "QQ" };
@@ -1447,20 +1553,28 @@ var HomeMirror = class {
1447
1553
  event(event) {
1448
1554
  if (event.type === "user/message") {
1449
1555
  const text = event.content.map((p) => p.type === "text" ? p.text : "").join("");
1450
- const origin2 = event.origin.human ? this.#message(text) : void 0;
1556
+ const { origin, person } = event.origin.human ? this.#message(text) : { origin: void 0, person: false };
1451
1557
  const reminder = event.origin.human ? void 0 : reminderPrompts(text);
1452
1558
  if (reminder !== void 0)
1453
1559
  this.#deps.post({ text: reminderLine(reminder) });
1454
- this.#turn ??= { origin: void 0, reply: new TurnReply() };
1455
- this.#turn.origin ??= origin2;
1560
+ const turn2 = this.#turn ??= this.#newTurn();
1561
+ turn2.origin ??= origin;
1562
+ if (person && turn2.live === void 0)
1563
+ turn2.live = new LiveWords((said) => {
1564
+ turn2.said.add(said.key);
1565
+ this.#deps.post({ text: said.text, markdown: true, key: said.key }, turn2.origin);
1566
+ });
1456
1567
  return;
1457
1568
  }
1458
- this.#turn ??= { origin: void 0, reply: new TurnReply() };
1459
- if (!this.#turn.reply.add(event))
1569
+ const turn = this.#turn ??= this.#newTurn();
1570
+ turn.live?.add(event);
1571
+ if (!turn.reply.add(event))
1460
1572
  return;
1461
- const { origin, reply } = this.#turn;
1462
1573
  this.#turn = void 0;
1463
- this.#reply(reply.reply, origin);
1574
+ this.#reply(turn.reply.reply, turn.origin, turn.said);
1575
+ }
1576
+ #newTurn() {
1577
+ return { origin: void 0, reply: new TurnReply(), live: void 0, said: /* @__PURE__ */ new Set() };
1464
1578
  }
1465
1579
  /** A hand-off (the gateway's); 助手's (from its conversation) become lines. */
1466
1580
  handOff(item) {
@@ -1474,36 +1588,45 @@ var HomeMirror = class {
1474
1588
  const to = this.#deps.agentName(item.to);
1475
1589
  this.#deps.post({ text: item.state === "running" ? `${item.by} \u2192 ${to}\uFF1A${item.title}` : `${to} \u2192 ${item.by}\uFF1A${item.title}\uFF08${item.state === "done" ? "\u4EA4\u56DE\u4E86" : "\u6CA1\u6709\u505A\u5B8C"}\uFF09` });
1476
1590
  }
1477
- /** @returns who wrote it from a chat app, if someone did. */
1591
+ /** @returns who wrote it from a chat app, if someone did; `person`: someone wrote it (not an agent, a task or a reminder). */
1478
1592
  #message(text) {
1593
+ const none = { origin: void 0, person: false };
1479
1594
  if (parseRelay(text) !== void 0)
1480
- return void 0;
1595
+ return none;
1481
1596
  const said = parseThreadNote(text);
1482
1597
  if (said !== void 0) {
1483
1598
  this.#deps.post({ text: `${said.from} \u2192 ${MAIN_NAME}\uFF1A${{ question: "\u95EE", answer: "\u7B54", update: "\u8BF4" }[said.kind]}\uFF1A${said.text}` });
1484
- return void 0;
1599
+ return none;
1485
1600
  }
1486
1601
  const task = parseTask(text);
1487
1602
  if (task !== void 0) {
1488
1603
  this.#deps.post({ text: task.by === BY_YOU ? `\u4F60\uFF08\u8F6F\u4EF6\uFF09\u70B9\u4E86\uFF1A${task.title}` : `${task.by} \u2192 ${MAIN_NAME}\uFF1A${task.title}` });
1489
- return void 0;
1604
+ return none;
1490
1605
  }
1491
1606
  const origin = this.#deps.originOf(text);
1492
1607
  const words = personWords(text);
1493
1608
  if (words === "")
1494
- return origin;
1609
+ return { origin, person: false };
1495
1610
  if (origin === void 0)
1496
1611
  this.#deps.post({ text: `\u4F60\uFF08\u8F6F\u4EF6\uFF09\uFF1A${words}` });
1497
1612
  else
1498
1613
  this.#deps.post({ text: `${origin.name ?? "\u5BF9\u65B9"}\uFF08${this.#deps.channelName(origin.channel)}\uFF09\uFF1A${words}` }, origin);
1499
- return origin;
1614
+ return { origin, person: true };
1500
1615
  }
1501
- #reply(reply, origin) {
1616
+ /** @param said - what of it already went out as it was said. */
1617
+ #reply(reply, origin, said) {
1502
1618
  const words = finalText(reply).trim();
1503
1619
  const stepped = reply.blocks.some((b) => b.kind === "steps");
1504
1620
  if (words === "" && !stepped || isQuiet(words))
1505
1621
  return;
1506
- this.#deps.post({ text: replyMarkdown(reply), markdown: true }, origin);
1622
+ const key = finalKey(reply);
1623
+ if (key !== void 0 && said.has(key)) {
1624
+ const rest = replyRest(reply);
1625
+ if (rest !== "")
1626
+ this.#deps.post({ text: rest }, origin);
1627
+ return;
1628
+ }
1629
+ this.#deps.post({ text: replyMarkdown(reply), markdown: true, ...key === void 0 ? {} : { key } }, origin);
1507
1630
  }
1508
1631
  };
1509
1632
 
@@ -1679,6 +1802,8 @@ export {
1679
1802
  NOTICES_SOURCE,
1680
1803
  noticeActTool,
1681
1804
  textReply,
1805
+ finalKey,
1806
+ replyRest,
1682
1807
  finalText,
1683
1808
  replyMarkdown,
1684
1809
  replyPlain,
package/lib/feishu.js CHANGED
@@ -2,7 +2,7 @@ import { createRequire as __adgCreateRequire } from 'node:module'; import { dirn
2
2
  import {
3
3
  replyMarkdown,
4
4
  replyPlain
5
- } from "./chunk-SMXJWLAZ.js";
5
+ } from "./chunk-RREVLKRY.js";
6
6
  import {
7
7
  require_extension,
8
8
  require_permessage_deflate,
package/lib/game-intel.js CHANGED
@@ -10,7 +10,7 @@ import {
10
10
  import {
11
11
  finalText,
12
12
  sessionConversations
13
- } from "./chunk-SMXJWLAZ.js";
13
+ } from "./chunk-RREVLKRY.js";
14
14
  import "./chunk-OCJFAT7J.js";
15
15
  import "./chunk-4NR36PKL.js";
16
16
  import {
package/lib/intervene.js CHANGED
@@ -2,7 +2,7 @@ import { createRequire as __adgCreateRequire } from 'node:module'; import { dirn
2
2
  import {
3
3
  finalText,
4
4
  sessionConversations
5
- } from "./chunk-SMXJWLAZ.js";
5
+ } from "./chunk-RREVLKRY.js";
6
6
  import "./chunk-OCJFAT7J.js";
7
7
  import "./chunk-4NR36PKL.js";
8
8
  import {
package/lib/notify.js CHANGED
@@ -7,6 +7,7 @@ import {
7
7
  Notify,
8
8
  approvalText,
9
9
  describeQuestion,
10
+ finalKey,
10
11
  finalText,
11
12
  lib_default,
12
13
  noticeActTool,
@@ -14,9 +15,10 @@ import {
14
15
  questionText,
15
16
  replyMarkdown,
16
17
  replyPlain,
18
+ replyRest,
17
19
  sessionConversations,
18
20
  textReply
19
- } from "./chunk-SMXJWLAZ.js";
21
+ } from "./chunk-RREVLKRY.js";
20
22
  import "./chunk-OCJFAT7J.js";
21
23
  import "./chunk-4NR36PKL.js";
22
24
  import "./chunk-RESS7WVY.js";
@@ -32,12 +34,14 @@ export {
32
34
  approvalText,
33
35
  lib_default as default,
34
36
  describeQuestion,
37
+ finalKey,
35
38
  finalText,
36
39
  noticeActTool,
37
40
  personWords,
38
41
  questionText,
39
42
  replyMarkdown,
40
43
  replyPlain,
44
+ replyRest,
41
45
  sessionConversations,
42
46
  textReply
43
47
  };
@@ -13,7 +13,7 @@ import {
13
13
  MAIN,
14
14
  finalText,
15
15
  sessionConversations
16
- } from "./chunk-SMXJWLAZ.js";
16
+ } from "./chunk-RREVLKRY.js";
17
17
  import "./chunk-OCJFAT7J.js";
18
18
  import "./chunk-4NR36PKL.js";
19
19
  import {
package/lib/qqbot.js CHANGED
@@ -1,10 +1,12 @@
1
1
  import { createRequire as __adgCreateRequire } from 'node:module'; import { dirname as __adgDirname } from 'node:path'; import { fileURLToPath as __adgFileURLToPath } from 'node:url'; const require = __adgCreateRequire(import.meta.url); const __filename = __adgFileURLToPath(import.meta.url); const __dirname = __adgDirname(__filename);
2
2
  import {
3
3
  approvalText,
4
+ finalKey,
4
5
  questionText,
5
6
  replyMarkdown,
6
- replyPlain
7
- } from "./chunk-SMXJWLAZ.js";
7
+ replyPlain,
8
+ replyRest
9
+ } from "./chunk-RREVLKRY.js";
8
10
  import "./chunk-OCJFAT7J.js";
9
11
  import "./chunk-4NR36PKL.js";
10
12
  import {
@@ -47,7 +49,9 @@ var HINTS = {
47
49
  11265: "\u673A\u5668\u4EBA\u5DF2\u88AB\u5C01\u7981",
48
50
  40034005: "\u8981\u56DE\u590D\u7684\u6D88\u606F\u5DF2\u7ECF\u8FC7\u671F",
49
51
  22009: "\u6D88\u606F\u53D1\u9001\u592A\u9891\u7E41\uFF0C\u88AB\u9650\u5236\u4E86",
50
- 40054005: "\u5BF9\u65B9\u5173\u95ED\u4E86\u673A\u5668\u4EBA\u7684\u4E3B\u52A8\u6D88\u606F"
52
+ 40054005: "\u5BF9\u65B9\u5173\u95ED\u4E86\u673A\u5668\u4EBA\u7684\u4E3B\u52A8\u6D88\u606F",
53
+ 40007: "\u6D41\u5F0F\u6D88\u606F\u5DF2\u7ECF\u663E\u793A\u7684\u5F00\u5934\u4E0D\u80FD\u6539",
54
+ 50002: "\u6D41\u5F0F\u6D88\u606F\u53D1\u5F97\u592A\u9891\u7E41\uFF0C\u88AB\u9650\u5236\u4E86"
51
55
  };
52
56
  var FATAL_CLOSE = {
53
57
  4004: "\u9274\u6743\u5931\u8D25\uFF1AAppID \u6216 AppSecret \u4E0D\u5BF9",
@@ -193,6 +197,20 @@ function httpApi(options) {
193
197
  token,
194
198
  sendText: (openId, text, reply) => sendMessage(openId, { text }, reply),
195
199
  sendMessage,
200
+ async streamMessage(openId, piece) {
201
+ const sent = await request("\u53D1\u9001\u6D41\u5F0F\u6D88\u606F", "POST", `/v2/users/${encodeURIComponent(openId)}/stream_messages`, {
202
+ input_mode: "replace",
203
+ input_state: piece.done ? 10 : 1,
204
+ index: piece.index,
205
+ content_type: "markdown",
206
+ content_raw: piece.text,
207
+ msg_id: piece.reply.msgId,
208
+ msg_seq: piece.reply.seq,
209
+ ...piece.streamId === void 0 ? {} : { stream_msg_id: piece.streamId },
210
+ ...piece.wakeup === true ? { is_wakeup: true } : {}
211
+ });
212
+ return sent.id ?? piece.streamId ?? "";
213
+ },
196
214
  async connect(handlers, onState) {
197
215
  closed = false;
198
216
  await open(handlers, onState);
@@ -236,6 +254,10 @@ var DEFAULT_QQ = { enabled: false, allowedUsers: [] };
236
254
  var KEEP_PENDING = 20;
237
255
  var RETRY_DELAYS_MS = [5e3, 15e3, 3e4, 6e4, 3e5];
238
256
  var PASSIVE_MS = 55 * 6e4;
257
+ var MAX_REPLIES = 5;
258
+ var STREAM_EVERY_MS = 400;
259
+ var STREAM_TOO_FAST = 50002;
260
+ var KEEP_GOT = 200;
239
261
  var QQBot = class {
240
262
  id = "qq";
241
263
  name = "QQ";
@@ -246,7 +268,14 @@ var QQBot = class {
246
268
  #error;
247
269
  #checks = [];
248
270
  #secretConfigured = false;
249
- #queues = /* @__PURE__ */ new Map();
271
+ /** Each person's messages out, one after another: what 助手 says arrives in the order it said it. */
272
+ #outbox = /* @__PURE__ */ new Map();
273
+ /** Each person's latest message: what goes to them is a reply to it while QQ takes one. */
274
+ #replyTo = /* @__PURE__ */ new Map();
275
+ /** What of 助手's words each person got (reply.ts keys): answers to two messages following one turn, and the mirror, say each thing once. */
276
+ #got = /* @__PURE__ */ new Map();
277
+ /** Messages being typed, by `openId:attempt`. */
278
+ #streams = /* @__PURE__ */ new Map();
250
279
  /** Approvals and questions asked of a person here: id → their user_openid. */
251
280
  #asked = /* @__PURE__ */ new Map();
252
281
  /** What QQ took or refused since connecting (undefined: not tried yet). */
@@ -333,6 +362,9 @@ var QQBot = class {
333
362
  this.#api?.close();
334
363
  this.#api = void 0;
335
364
  this.#connection = "idle";
365
+ for (const stream of this.#streams.values())
366
+ clearTimeout(stream.timer);
367
+ this.#streams.clear();
336
368
  }
337
369
  // ── settings ──────────────────────────────────────────────────────────
338
370
  async configure(patch) {
@@ -414,7 +446,7 @@ var QQBot = class {
414
446
  }
415
447
  // ── out ───────────────────────────────────────────────────────────────
416
448
  async send(notice) {
417
- const api = this.#ready();
449
+ this.#ready();
418
450
  const people = this.#state.settings.allowedUsers;
419
451
  if (people.length === 0)
420
452
  throw new Error("\u8FD8\u6CA1\u6709\u5141\u8BB8\u4EFB\u4F55 QQ \u7528\u6237");
@@ -422,7 +454,7 @@ var QQBot = class {
422
454
  let delivered = 0;
423
455
  for (const { openId } of people) {
424
456
  try {
425
- await this.#deliver(api, openId, noticeMessage(notice));
457
+ await this.#post(openId, noticeMessage(notice));
426
458
  delivered++;
427
459
  } catch (error) {
428
460
  errors.push(message(error));
@@ -434,22 +466,27 @@ var QQBot = class {
434
466
  async sendTo(openId, notice) {
435
467
  if (!this.#allowed(openId))
436
468
  throw new Error("\u8FD9\u4E2A QQ \u7528\u6237\u6CA1\u6709\u88AB\u5141\u8BB8");
437
- await this.#ready().sendText(openId, noticeText(notice));
469
+ this.#ready();
470
+ await this.#post(openId, noticeText(notice));
438
471
  }
439
- /** 助手's conversation, to each allowed person but `except`. */
472
+ /** 助手's conversation, to each allowed person but `except`, and but whoever got these words already. */
440
473
  async mirror(post, except) {
441
- const api = this.#ready();
474
+ this.#ready();
475
+ const message2 = post.markdown === true ? { markdown: post.text, plain: plainText(post.text) } : { markdown: post.text, plain: post.text };
476
+ const sends = [];
442
477
  for (const { openId } of this.#state.settings.allowedUsers) {
443
- if (openId === except)
478
+ if (openId === except || !this.#first(openId, post.key))
444
479
  continue;
445
- await this.#deliver(api, openId, post.markdown === true ? { markdown: post.text, plain: plainText(post.text) } : { markdown: post.text, plain: post.text });
480
+ sends.push(this.#post(openId, message2));
446
481
  }
482
+ await Promise.all(sends);
447
483
  }
448
484
  /** A conversation's approvals, to its person only, as text; other sessions' stay in the app. */
449
485
  approvals = {
450
486
  active: (owner) => owner !== void 0 && this.#connection === "connected" && this.#allowed(owner),
451
487
  ask: async (request, owner) => {
452
- await this.#deliver(this.#ready(), owner, approvalMessage(request));
488
+ this.#ready();
489
+ await this.#post(owner, approvalMessage(request));
453
490
  this.#asked.set(request.approvalId, owner);
454
491
  },
455
492
  resolved: async (approvalId, outcome) => this.#settled(approvalId, outcome === "cancelled" ? "agent \u5DF2\u53D6\u6D88\u8FD9\u4E2A\u64CD\u4F5C" : outcome === "unavailable" ? "\u8FD9\u4E2A\u786E\u8BA4\u5DF2\u5931\u6548" : void 0)
@@ -458,7 +495,8 @@ var QQBot = class {
458
495
  questions = {
459
496
  active: (owner) => this.approvals.active(owner),
460
497
  ask: async (request, owner) => {
461
- await this.#deliver(this.#ready(), owner, questionMessage(request));
498
+ this.#ready();
499
+ await this.#post(owner, questionMessage(request));
462
500
  this.#asked.set(request.questionId, owner);
463
501
  },
464
502
  resolved: async (questionId, outcome) => this.#settled(questionId, outcome === "cancelled" ? "agent \u4E0D\u518D\u7B49\u8FD9\u4E2A\u56DE\u7B54\u4E86" : outcome === "unavailable" ? "\u8FD9\u4E2A\u95EE\u9898\u5DF2\u5931\u6548" : void 0)
@@ -469,8 +507,191 @@ var QQBot = class {
469
507
  this.#asked.delete(id);
470
508
  if (owner === void 0 || note === void 0 || this.#api === void 0)
471
509
  return;
472
- await this.#api.sendText(owner, `\uFF08${note}\uFF09`).catch(() => {
510
+ await this.#post(owner, `\uFF08${note}\uFF09`).catch(() => {
511
+ });
512
+ }
513
+ /**
514
+ * To one person, after what is already on its way to them: as a reply to their latest message while QQ
515
+ * takes one, else (or when QQ refuses the reply) as an active message.
516
+ */
517
+ #post(openId, message2) {
518
+ return this.#enqueue(openId, () => this.#sendNow(openId, message2));
519
+ }
520
+ /** Run `job` for this person after what is already on its way to them. */
521
+ #enqueue(openId, job) {
522
+ const next = (this.#outbox.get(openId) ?? Promise.resolve()).catch(() => {
523
+ }).then(job);
524
+ this.#outbox.set(openId, next);
525
+ void next.catch(() => {
526
+ }).then(() => {
527
+ if (this.#outbox.get(openId) === next)
528
+ this.#outbox.delete(openId);
473
529
  });
530
+ return next;
531
+ }
532
+ async #sendNow(openId, message2) {
533
+ const api = this.#ready();
534
+ const send = async (reply2) => {
535
+ if (typeof message2 === "string")
536
+ await api.sendText(openId, message2, reply2);
537
+ else
538
+ await this.#deliver(api, openId, message2, reply2);
539
+ };
540
+ const reply = this.#nextReply(openId);
541
+ if (reply === void 0)
542
+ return send();
543
+ try {
544
+ await send(reply);
545
+ } catch {
546
+ this.#replyTo.delete(openId);
547
+ await send();
548
+ }
549
+ }
550
+ // ── typing: QQ's streamed messages ────────────────────────────────────
551
+ /** 助手's words while it types them: one QQ message that fills in, kept up to date about once a second. */
552
+ #typing(openId, typing) {
553
+ const at = `${openId}:${typing.id}`;
554
+ const stream = this.#streams.get(at);
555
+ if (typing.kind !== "typing") {
556
+ this.#streams.delete(at);
557
+ if (stream === void 0)
558
+ return;
559
+ if (typing.kind === "committed" && this.#first(openId, typing.key)) {
560
+ const words = typing.key.slice(typing.key.indexOf(":") + 1);
561
+ void this.#finish(stream, words, { markdown: words, plain: plainText(words) }).catch(() => {
562
+ });
563
+ return;
564
+ }
565
+ void this.#finish(stream, typing.kind === "dropped" && stream.want !== "" ? `${stream.want}\u2026` : stream.want, void 0).catch(() => {
566
+ });
567
+ return;
568
+ }
569
+ if (stream === void 0) {
570
+ if (this.#formats.stream === false || this.#formats.markdown === false)
571
+ return;
572
+ const reply = this.#streamReply(openId);
573
+ if (reply === void 0)
574
+ return;
575
+ const started = { openId, ...reply, id: void 0, index: 0, shown: "", want: typing.text, last: 0, timer: void 0, closing: void 0, broken: false };
576
+ this.#streams.set(at, started);
577
+ this.#schedule(started);
578
+ return;
579
+ }
580
+ if (stream.closing !== void 0 || stream.broken)
581
+ return;
582
+ stream.want = typing.text;
583
+ this.#schedule(stream);
584
+ }
585
+ /**
586
+ * The message still being typed whose words `text` turned out to be (they, or the summary that starts
587
+ * with them): the words were said before the stream's end came. Taken, to be closed with `text`.
588
+ */
589
+ #typed(openId, key, text) {
590
+ if (key === void 0)
591
+ return void 0;
592
+ for (const [at, stream] of this.#streams) {
593
+ if (stream.openId !== openId || stream.closing !== void 0 || stream.want === "" || !text.startsWith(stream.want))
594
+ continue;
595
+ this.#streams.delete(at);
596
+ return stream;
597
+ }
598
+ return void 0;
599
+ }
600
+ /** A stream is a reply to the person's latest message: while QQ takes replies to it, else as a wake-up (is_wakeup). */
601
+ #streamReply(openId) {
602
+ const passive = this.#nextReply(openId);
603
+ if (passive !== void 0)
604
+ return { reply: passive, wakeup: false };
605
+ const latest = this.#replyTo.get(openId);
606
+ return latest === void 0 ? void 0 : { reply: { msgId: latest.msgId, seq: ++latest.seq }, wakeup: true };
607
+ }
608
+ #schedule(stream) {
609
+ if (stream.timer !== void 0)
610
+ return;
611
+ const wait = Math.max(0, stream.last + (this.#options.streamEveryMs ?? STREAM_EVERY_MS) - Date.now());
612
+ stream.timer = setTimeout(() => {
613
+ stream.timer = void 0;
614
+ void this.#enqueue(stream.openId, () => this.#piece(stream)).catch(() => {
615
+ });
616
+ }, wait);
617
+ stream.timer.unref?.();
618
+ }
619
+ /**
620
+ * Close a typed message with `text` (what its words became). When QQ cannot show them there (the text
621
+ * no longer starts with what it shows, or QQ refuses), `instead` goes as a message of its own.
622
+ */
623
+ #finish(stream, text, instead) {
624
+ clearTimeout(stream.timer);
625
+ stream.timer = void 0;
626
+ stream.want = text;
627
+ stream.closing = instead ?? { markdown: "", plain: "" };
628
+ return this.#enqueue(stream.openId, () => this.#piece(stream));
629
+ }
630
+ /** Bring a typed message up to date, or close it; runs in the person's outbox. */
631
+ async #piece(stream) {
632
+ const closing = stream.closing;
633
+ if (closing === void 0 && (stream.broken || stream.want === stream.shown || !stream.want.startsWith(stream.shown)))
634
+ return;
635
+ if (closing !== void 0 && stream.index < 0)
636
+ return;
637
+ const instead = closing !== void 0 && closing.markdown !== "" ? closing : void 0;
638
+ if (closing !== void 0 && (stream.broken || stream.id === void 0 && (instead === void 0 || stream.want === ""))) {
639
+ stream.index = -1;
640
+ if (instead !== void 0)
641
+ await this.#sendNow(stream.openId, instead);
642
+ return;
643
+ }
644
+ const fits = stream.want.startsWith(stream.shown);
645
+ const text = fits ? stream.want : stream.shown;
646
+ const done = closing !== void 0;
647
+ try {
648
+ const id = await this.#ready().streamMessage(stream.openId, { text, index: stream.index, done, streamId: stream.id, reply: stream.reply, wakeup: stream.wakeup });
649
+ stream.id ??= id;
650
+ stream.index = done ? -1 : stream.index + 1;
651
+ stream.shown = text;
652
+ stream.last = Date.now();
653
+ this.#formats.stream = true;
654
+ if (done && !fits && instead !== void 0)
655
+ await this.#sendNow(stream.openId, instead);
656
+ } catch (error) {
657
+ if (!done && error instanceof QQError && error.code === STREAM_TOO_FAST) {
658
+ stream.last = Date.now();
659
+ this.#schedule(stream);
660
+ return;
661
+ }
662
+ const first = stream.id === void 0;
663
+ stream.broken = true;
664
+ if (first && !stream.wakeup && error instanceof QQError && this.#formats.stream !== true) {
665
+ this.#formats.stream = false;
666
+ this.#options.log.info(`qq: streamed messages not accepted (${errorText(error)}), sending each part when it is said`);
667
+ }
668
+ if (done) {
669
+ stream.index = -1;
670
+ if (instead !== void 0)
671
+ await this.#sendNow(stream.openId, instead);
672
+ }
673
+ }
674
+ }
675
+ /** The next reply to this person's latest message, while QQ takes one. */
676
+ #nextReply(openId) {
677
+ const latest = this.#replyTo.get(openId);
678
+ if (latest === void 0 || Date.now() - latest.at >= PASSIVE_MS || latest.seq >= MAX_REPLIES)
679
+ return void 0;
680
+ return { msgId: latest.msgId, seq: ++latest.seq };
681
+ }
682
+ /** @returns whether these words (a reply.ts key) are new to this person, noting that they now have them; no key: always new. */
683
+ #first(openId, key) {
684
+ if (key === void 0)
685
+ return true;
686
+ let got = this.#got.get(openId);
687
+ if (got === void 0)
688
+ this.#got.set(openId, got = /* @__PURE__ */ new Set());
689
+ if (got.has(key))
690
+ return false;
691
+ got.add(key);
692
+ if (got.size > KEEP_GOT)
693
+ got.delete(got.values().next().value);
694
+ return true;
474
695
  }
475
696
  /**
476
697
  * Send as markdown with its buttons; what QQ refuses is dropped — the buttons, then markdown — and not
@@ -520,49 +741,62 @@ ${replyHint(message2.buttons)}` : message2.plain, reply);
520
741
  });
521
742
  return;
522
743
  }
744
+ this.#replyTo.set(event.openId, { msgId: event.id, at: Date.now(), seq: 0 });
523
745
  if (event.text === "") {
524
- void api.sendText(event.openId, "\u76EE\u524D\u53EA\u770B\u5F97\u61C2\u6587\u5B57\u6D88\u606F\u3002", { msgId: event.id, seq: 1 }).catch(() => {
746
+ void this.#post(event.openId, "\u76EE\u524D\u53EA\u770B\u5F97\u61C2\u6587\u5B57\u6D88\u606F\u3002").catch(() => {
525
747
  });
526
748
  return;
527
749
  }
528
- if (this.#options.notify()?.waiting?.(this.#actor(event)) === true) {
529
- void this.#answer(api, event).catch((error) => this.#options.log.warn(`qq reply failed: ${message(error)}`));
530
- return;
531
- }
532
- const previous = this.#queues.get(event.openId) ?? Promise.resolve();
533
- const next = previous.then(() => this.#answer(api, event)).catch((error) => this.#options.log.warn(`qq reply failed: ${message(error)}`));
534
- this.#queues.set(event.openId, next);
535
- void next.then(() => {
536
- if (this.#queues.get(event.openId) === next)
537
- this.#queues.delete(event.openId);
538
- });
750
+ void this.#answer(event).catch((error) => this.#options.log.warn(`qq reply failed: ${message(error)}`));
539
751
  }
540
- async #answer(api, event) {
752
+ async #answer(event) {
753
+ const { openId } = event;
541
754
  const notify = this.#options.notify();
542
- const started = Date.now();
543
- let seq = 0;
544
- const passive = () => Date.now() - started < PASSIVE_MS ? { msgId: event.id, seq: ++seq } : void 0;
545
- const reply = (text) => api.sendText(event.openId, text, passive());
546
755
  if (notify === void 0) {
547
- await reply("\u6D88\u606F\u670D\u52A1\u6CA1\u6709\u52A0\u8F7D\u3002");
756
+ await this.#post(openId, "\u6D88\u606F\u670D\u52A1\u6CA1\u6709\u52A0\u8F7D\u3002");
548
757
  return;
549
758
  }
550
- let answered = false;
759
+ let heard = false;
551
760
  const ack = setTimeout(() => {
552
- if (!answered)
553
- void reply("\u6536\u5230\uFF0C\u6B63\u5728\u5904\u7406\u2026").catch(() => {
761
+ if (!heard)
762
+ void this.#post(openId, "\u6536\u5230\uFF0C\u6B63\u5728\u5904\u7406\u2026").catch(() => {
554
763
  });
555
764
  }, this.#options.ackAfterMs ?? 4e3);
765
+ const failed = (error) => this.#options.log.warn(`qq message failed: ${message(error)}`);
766
+ const onTyping = (typing) => {
767
+ heard = true;
768
+ this.#typing(openId, typing);
769
+ };
770
+ const onSay = (said) => {
771
+ heard = true;
772
+ if (!this.#first(openId, said.key))
773
+ return;
774
+ const out = { markdown: said.text, plain: plainText(said.text) };
775
+ const typed = this.#typed(openId, said.key, said.text);
776
+ void (typed === void 0 ? this.#post(openId, out) : this.#finish(typed, said.text, out)).catch(failed);
777
+ };
556
778
  try {
557
779
  const actor = this.#actor(event);
558
- const answer = await notify.inbound({ actor, text: event.text });
559
- answered = true;
780
+ const answer = await notify.inbound({ actor, text: event.text, onSay, onTyping });
781
+ heard = true;
782
+ const key = finalKey(answer);
783
+ if (!this.#first(openId, key)) {
784
+ const rest = replyRest(answer);
785
+ if (rest !== "" && this.#first(openId, `${key}|rest`))
786
+ await this.#post(openId, rest);
787
+ return;
788
+ }
560
789
  const next = notify.nextQuestion?.(actor);
561
790
  const out = { markdown: replyMarkdown(answer), plain: plainText(replyPlain(answer)), ...next === void 0 ? {} : { buttons: choiceButtons(next) } };
562
- await this.#deliver(api, event.openId, out, passive()).catch(() => this.#deliver(api, event.openId, out));
791
+ const typed = this.#typed(openId, key, out.markdown);
792
+ if (typed === void 0)
793
+ return await this.#post(openId, out);
794
+ await this.#finish(typed, out.markdown, { ...out, buttons: [] });
795
+ if (out.buttons !== void 0)
796
+ await this.#post(openId, { markdown: replyHint(out.buttons), plain: replyHint(out.buttons), buttons: out.buttons });
563
797
  } catch (error) {
564
- answered = true;
565
- await reply(`\u51FA\u9519\u4E86\uFF1A${message(error)}`).catch(() => {
798
+ heard = true;
799
+ await this.#post(openId, `\u51FA\u9519\u4E86\uFF1A${message(error)}`).catch(() => {
566
800
  });
567
801
  } finally {
568
802
  clearTimeout(ack);
@@ -679,7 +913,7 @@ var AdgQQBot = class extends Service {
679
913
  await writeFileAtomic(file, `${JSON.stringify(state, null, 2)}
680
914
  `);
681
915
  },
682
- notify: () => ({ inbound: (m) => notify.inbound(m), waiting: (actor) => notify.notify.waiting(actor), nextQuestion: (actor) => notify.notify.nextQuestion(actor) }),
916
+ notify: () => ({ inbound: (m) => notify.inbound(m), nextQuestion: (actor) => notify.notify.nextQuestion(actor) }),
683
917
  changed: () => notify.changed(),
684
918
  log: logPort
685
919
  });
@@ -17,10 +17,10 @@ import {
17
17
  packForRepo,
18
18
  parseMcpConfig,
19
19
  readSkills
20
- } from "./chunk-HRP7ALS6.js";
20
+ } from "./chunk-AXOXWMQT.js";
21
21
  import "./chunk-B4UT4NYH.js";
22
22
  import "./chunk-7UJ3LK3G.js";
23
- import "./chunk-SMXJWLAZ.js";
23
+ import "./chunk-RREVLKRY.js";
24
24
  import "./chunk-OCJFAT7J.js";
25
25
  import "./chunk-4NR36PKL.js";
26
26
  import "./chunk-RESS7WVY.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-multigameassistant",
3
- "version": "2.3.2",
3
+ "version": "2.3.3",
4
4
  "description": "多游戏助手 MultiGameAssistant 的 dsh 插件版:让 agent 管好各个游戏的日常脚本(安装、编排定时流水线、看护运行、查活动),界面在自己的网页里。",
5
5
  "keywords": [
6
6
  "dsh",