@chen_hu_hu/pi-qq 1.7.0 → 1.7.2

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.
@@ -5,6 +5,7 @@ import { policyFor, type ReplyPolicy } from "./reply-policy";
5
5
  import type { ReplyBudget } from "../quota/reply-quota";
6
6
  import type { ReplyType } from "../infra/reply-type";
7
7
  import { isLogEnabled, log } from "../infra/file-logger";
8
+ import { shortId } from "../infra/short-id";
8
9
 
9
10
  export interface RunFeedbackDeps {
10
11
  bot: () => QQBot | undefined;
@@ -20,6 +21,8 @@ interface RunState {
20
21
  thinkingSentLen: number;
21
22
  finalizedText: string;
22
23
  resultSentLen: number;
24
+ lastFinalizedTextLen: number;
25
+ lastResultSentLen: number;
23
26
  deferredThinking: string;
24
27
  deferredText: string;
25
28
  timeline: RunTimeline;
@@ -39,6 +42,7 @@ export type RepushResult =
39
42
  const TYPING_INTERVAL_MS = 50_000;
40
43
  const PENDING_TTL_MS = 15_000;
41
44
  const RECOVERY_TTL_MS = 60_000;
45
+ const AUTO_REBIND_TTL_MS = 5 * 60_000;
42
46
 
43
47
  function toRepushTool(tool: TimelineTool): RepushTool {
44
48
  return {
@@ -55,6 +59,7 @@ export class RunFeedback {
55
59
  private recovery: { openid: string; msgId: string; at: number } | null = null;
56
60
  private run: RunState | null = null;
57
61
  private lastCompletedText: string | undefined;
62
+ private autoRebinds = 0;
58
63
  private endPromise: Promise<void> = Promise.resolve();
59
64
  private endResolve?: () => void;
60
65
 
@@ -127,7 +132,7 @@ export class RunFeedback {
127
132
 
128
133
  log.debug("run", "stream timing", {
129
134
  phase: "last repush",
130
- msgId: msgId.slice(0, 8),
135
+ msgId: shortId(msgId),
131
136
  replyType,
132
137
  textLength,
133
138
  thinkingLength,
@@ -144,6 +149,7 @@ export class RunFeedback {
144
149
  }
145
150
 
146
151
  onAgentStart(): void {
152
+ this.autoRebinds = 0;
147
153
  this.endPromise = new Promise((resolve) => {
148
154
  this.endResolve = resolve;
149
155
  });
@@ -168,7 +174,7 @@ export class RunFeedback {
168
174
  }
169
175
  const bot = this.deps.bot();
170
176
  if (!bot) {
171
- log.debug("run", "run started", { phase: "no bot", msgId: target.msgId.slice(0, 8) });
177
+ log.debug("run", "run started", { phase: "no bot", msgId: shortId(target.msgId) });
172
178
  return;
173
179
  }
174
180
 
@@ -188,6 +194,8 @@ export class RunFeedback {
188
194
  thinkingSentLen: 0,
189
195
  finalizedText: "",
190
196
  resultSentLen: 0,
197
+ lastFinalizedTextLen: 0,
198
+ lastResultSentLen: 0,
191
199
  deferredThinking: "",
192
200
  deferredText: "",
193
201
  timeline: new RunTimeline(),
@@ -196,7 +204,7 @@ export class RunFeedback {
196
204
  this.run = run;
197
205
  if (isLogEnabled("debug")) {
198
206
  log.debug("run", "run started", {
199
- msgId: run.msgId?.slice(0, 8) ?? "?",
207
+ msgId: shortId(run.msgId),
200
208
  source: target === pending ? "pending" : "recovery",
201
209
  targetAgeMs: now - target.at,
202
210
  replyType: this.deps.getReplyType(),
@@ -205,6 +213,9 @@ export class RunFeedback {
205
213
  }
206
214
 
207
215
  const keepTyping = () => {
216
+ // A live stream already shows activity; do not compete with it for the
217
+ // serialized API client, especially when the network is slow.
218
+ if (run.writer.hasActiveStream) return;
208
219
  void bot.sendTyping({ scope: "c2c", targetId: run.openid }, 60).catch(() => {});
209
220
  };
210
221
  keepTyping();
@@ -220,13 +231,6 @@ export class RunFeedback {
220
231
  }
221
232
  const tool = run.timeline.addTool(toolName, args, id);
222
233
  if (!tool) return;
223
- log.debug("run", "tool start", {
224
- phase: "queued",
225
- toolName,
226
- toolCallId: id,
227
- skillName: tool.skillName ?? null,
228
- ...this.diagnostics(run),
229
- });
230
234
  this.advanceTools(run);
231
235
  }
232
236
 
@@ -242,13 +246,6 @@ export class RunFeedback {
242
246
  log.debug("run", "tool end", { phase: "ignored: unknown tool", toolName, toolCallId: id });
243
247
  return;
244
248
  }
245
- log.debug("run", "tool end", {
246
- phase: "result recorded",
247
- toolName,
248
- toolCallId: id,
249
- isError,
250
- ...this.diagnostics(run),
251
- });
252
249
  this.advanceTools(run);
253
250
  }
254
251
 
@@ -294,6 +291,7 @@ export class RunFeedback {
294
291
  run.finalizedText = [run.finalizedText, text].filter((part) => part.trim()).join("\n\n");
295
292
  const delta = run.finalizedText.slice(run.resultSentLen);
296
293
  run.resultSentLen = run.finalizedText.length;
294
+ if (!isError) this.tryAutoRebind(run);
297
295
  if (!isError && text) this.emitTextDelta(run, delta, policy);
298
296
  if (!isError && text) run.writer.closeBlock();
299
297
 
@@ -303,21 +301,29 @@ export class RunFeedback {
303
301
  }
304
302
 
305
303
  const hadPendingTools = this.hasPendingTools(run);
306
- const ok = await run.writer.flush();
304
+ let ok = await run.writer.flush();
305
+ if (!isError && !ok && run.writer.deadCode === 40034020) {
306
+ this.tryAutoRebind(run);
307
+ if (this.autoRebinds > 0) ok = await run.writer.flush();
308
+ }
307
309
  const streamTookOver = !isError && ok && run.writer.hasPushed;
308
- log.debug("run", "stream takeover decision", {
309
- phase: "assistant final",
310
- textLength: text.length,
311
- deltaLength: delta.length,
312
- isError,
313
- hadPendingTools,
314
- flushOk: ok,
315
- hasPushed: run.writer.hasPushed,
316
- streamTookOver,
317
- deferredTextLen: run.deferredText.length,
318
- deferredThinkingLen: run.deferredThinking.length,
319
- ...this.diagnostics(run),
320
- });
310
+ if (text.length > 0 || !streamTookOver) {
311
+ log.debug("run", "stream takeover decision", {
312
+ phase: "assistant final",
313
+ textLength: text.length,
314
+ deltaLength: delta.length,
315
+ isError,
316
+ hadPendingTools,
317
+ flushOk: ok,
318
+ hasPushed: run.writer.hasPushed,
319
+ streamTookOver,
320
+ deferredTextLen: run.deferredText.length,
321
+ deferredThinkingLen: run.deferredThinking.length,
322
+ ...this.diagnostics(run),
323
+ });
324
+ }
325
+ run.lastFinalizedTextLen = run.finalizedText.length;
326
+ run.lastResultSentLen = run.resultSentLen;
321
327
  run.finalizedText = "";
322
328
  run.resultSentLen = 0;
323
329
  if (isError) return false;
@@ -337,24 +343,26 @@ export class RunFeedback {
337
343
  this.clearTimers();
338
344
  if (run.timeline.currentText.trim()) this.lastCompletedText = run.timeline.currentText;
339
345
  if (!run.timeline.error && run.writer.hasPushed) this.recovery = null;
346
+ let delivered = false;
347
+ if (run.writer.hasPushed) {
348
+ const ok = await run.writer.flush();
349
+ if (ok) {
350
+ await run.writer.done();
351
+ delivered = true;
352
+ }
353
+ }
340
354
  log.debug("run", "run ended", {
341
355
  phase: "cleanup",
342
- msgId: run.msgId?.slice(0, 8) ?? "?",
356
+ msgId: shortId(run.msgId),
343
357
  elapsedMs: Date.now() - run.startedAt,
344
358
  hasPendingTools,
345
359
  hasPushed: run.writer.hasPushed,
360
+ delivered,
346
361
  lastCompletedTextLength: this.lastCompletedText?.length ?? 0,
362
+ lastFinalizedTextLen: run.lastFinalizedTextLen,
363
+ lastResultSentLen: run.lastResultSentLen,
347
364
  ...this.diagnostics(run),
348
365
  });
349
- log.debug("run", "run ended", {
350
- phase: "finished",
351
- msgId: run.msgId?.slice(0, 8) ?? "?",
352
- elapsedMs: Date.now() - run.startedAt,
353
- });
354
- if (run.writer.hasPushed) {
355
- const ok = await run.writer.flush();
356
- if (ok) await run.writer.done();
357
- }
358
366
  run.writer.dispose();
359
367
  if (this.run === run) this.run = null;
360
368
  this.finishEnd();
@@ -387,6 +395,36 @@ export class RunFeedback {
387
395
  return run.timeline.hasPendingTools();
388
396
  }
389
397
 
398
+ private tryAutoRebind(run: RunState): void {
399
+ if (this.autoRebinds > 0 || run.writer.deadCode !== 40034020) return;
400
+
401
+ let openid = run.openid;
402
+ let msgId = run.msgId;
403
+ let freshTarget = false;
404
+ let targetAgeMs: number | null = null;
405
+ const target = this.deps.budget.lastTarget;
406
+ if (target && Date.now() - target.arrivedAt <= AUTO_REBIND_TTL_MS) {
407
+ openid = target.openid;
408
+ msgId = target.msgId;
409
+ freshTarget = true;
410
+ targetAgeMs = Date.now() - target.arrivedAt;
411
+ }
412
+ if (!msgId) return;
413
+
414
+ const result = this.repushCurrent(openid, msgId);
415
+ if (!result.ok) return;
416
+ this.autoRebinds += 1;
417
+ log.debug("run", "stream timing", {
418
+ phase: "auto rebind",
419
+ msgId: shortId(msgId),
420
+ freshTarget,
421
+ targetAgeMs,
422
+ textLength: result.textLength,
423
+ thinkingLength: result.thinkingLength,
424
+ toolCount: result.toolCount,
425
+ });
426
+ }
427
+
390
428
  private emitTextDelta(run: RunState, delta: string, policy: ReplyPolicy): void {
391
429
  if (!delta || run.writer.dead) return;
392
430
  if (policy.deferTextWhileToolsPending && this.hasPendingTools(run)) {
@@ -439,6 +477,7 @@ export class RunFeedback {
439
477
  toolName: tool.name,
440
478
  toolCallId: tool.id,
441
479
  skillName: tool.skillName ?? null,
480
+ queueWaitMs: Date.now() - tool.startedAt,
442
481
  ...this.diagnostics(run),
443
482
  });
444
483
  if (!run.writer.dead && relayTools) {
@@ -111,7 +111,7 @@ export class RunTimeline {
111
111
  activeTool: this.activeToolId ?? null,
112
112
  toolCount: this.tools.length,
113
113
  pendingToolCount: this.tools.filter((tool) => !tool.finished).length,
114
- tools: this.tools.map((tool) => ({
114
+ tools: this.snapshot().tools.map((tool) => ({
115
115
  id: tool.id,
116
116
  name: tool.name,
117
117
  started: tool.started,
@@ -1,5 +1,6 @@
1
1
  import { StreamContentType, StreamInputState } from "@tencent-connect/qqbot-nodejs/protocol";
2
2
  import { allocateMsgSeq } from "./msg-seq";
3
+ import { shortId } from "../infra/short-id";
3
4
  import type {
4
5
  Credentials,
5
6
  MessageApi,
@@ -10,6 +11,11 @@ import type {
10
11
  const MIN_INTERVAL_MS = 300;
11
12
  const RATE_LIMIT_BASE_MS = 1000;
12
13
  const MAX_RETRIES = 3;
14
+ const SLOW_FRAME_MS = 1000;
15
+ // QQ rejects oversized markdown shards with 40034017. The platform appears to
16
+ // split frames larger than roughly 4k and then requires every shard to end at
17
+ // a newline; 3500 bytes leaves room for that limit and for multi-byte text.
18
+ export const MAX_FRAME_BYTES = 3500;
13
19
 
14
20
  export interface AppendStreamCheckpoint {
15
21
  msgSeq?: number;
@@ -73,15 +79,16 @@ export class AppendStream {
73
79
 
74
80
  async flush(): Promise<void> {
75
81
  if (this.doneSent) return;
76
- if (this.flushTimer) {
77
- clearTimeout(this.flushTimer);
78
- this.flushTimer = null;
79
- }
82
+ this.clearFlushTimer();
80
83
  while (this.flushing) {
81
84
  await new Promise((r) => setTimeout(r, 20));
82
85
  }
83
86
  if (this.doneSent) return;
84
- if (this.buffer) await this.doFlush("generating");
87
+ while (this.buffer) {
88
+ await this.doFlush("generating");
89
+ this.clearFlushTimer();
90
+ if (this.buffer) await this.waitForThrottle();
91
+ }
85
92
  }
86
93
 
87
94
  private throttleMs(): number {
@@ -91,58 +98,96 @@ export class AppendStream {
91
98
  async append(delta: string): Promise<void> {
92
99
  if (this.doneSent || !delta) return;
93
100
  this.buffer += delta;
101
+ if (this.flushing) return;
94
102
  const elapsed = Date.now() - this.lastFlushAt;
95
103
  const throttle = this.throttleMs();
96
- if (this.flushing) {
97
- this.opts.onDebug?.(`append: while flushing, buffer=${this.buffer.length}`);
98
- return;
99
- }
100
104
  if (elapsed >= throttle) {
101
- this.opts.onDebug?.(`append: immediate, elapsed=${elapsed}, buffer=${this.buffer.length}`);
102
105
  await this.doFlush("generating");
103
106
  return;
104
107
  }
105
108
  if (!this.flushTimer) {
106
- const waitMs = throttle - elapsed;
107
- this.opts.onDebug?.(`append: buffered, elapsed=${elapsed}, wait=${waitMs}, buffer=${this.buffer.length}`);
108
109
  this.flushTimer = setTimeout(() => {
109
110
  this.flushTimer = null;
110
- this.opts.onDebug?.(`timer: fired after ${waitMs}, buffer=${this.buffer.length}`);
111
111
  void this.doFlush("generating").catch((err) => this.opts.onError?.(err as Error));
112
- }, waitMs);
112
+ }, throttle - elapsed);
113
113
  }
114
114
  }
115
115
 
116
116
  async done(): Promise<void> {
117
117
  if (this.doneSent) return;
118
- if (this.flushTimer) {
119
- clearTimeout(this.flushTimer);
120
- this.flushTimer = null;
121
- }
118
+ this.clearFlushTimer();
122
119
  while (this.flushing) {
123
120
  await new Promise((r) => setTimeout(r, 20));
124
121
  }
125
122
  if (this.doneSent) return;
126
- if (this.buffer) await this.doFlush("generating");
123
+ while (this.buffer) {
124
+ await this.doFlush("generating");
125
+ this.clearFlushTimer();
126
+ if (this.buffer) await this.waitForThrottle();
127
+ }
127
128
  await this.doFlush("done");
128
129
  this.doneSent = true;
129
130
  }
130
131
 
131
132
  dispose(): void {
133
+ this.clearFlushTimer();
134
+ this.doneSent = true;
135
+ }
136
+
137
+ private clearFlushTimer(): void {
132
138
  if (this.flushTimer) {
133
139
  clearTimeout(this.flushTimer);
134
140
  this.flushTimer = null;
135
141
  }
136
- this.doneSent = true;
142
+ }
143
+
144
+ private async waitForThrottle(): Promise<void> {
145
+ const waitMs = this.throttleMs() - (Date.now() - this.lastFlushAt);
146
+ if (waitMs > 0) await new Promise((r) => setTimeout(r, waitMs));
147
+ }
148
+
149
+ private takeFrame(): { text: string; consumed: number } {
150
+ const buffer = this.buffer;
151
+ if (!buffer) return { text: "", consumed: 0 };
152
+ if (Buffer.byteLength(buffer, "utf8") <= MAX_FRAME_BYTES) {
153
+ return { text: buffer, consumed: buffer.length };
154
+ }
155
+
156
+ let bytes = 0;
157
+ let consumed = 0;
158
+ let lastNewline = -1;
159
+ // Reserve one byte for the newline added when no line break is in range.
160
+ const scanLimit = MAX_FRAME_BYTES - 1;
161
+ for (const char of buffer) {
162
+ const size = Buffer.byteLength(char, "utf8");
163
+ if (bytes + size > scanLimit) break;
164
+ bytes += size;
165
+ consumed += char.length;
166
+ if (char === "\n") lastNewline = consumed;
167
+ }
168
+
169
+ if (lastNewline > 0) {
170
+ return { text: buffer.slice(0, lastNewline), consumed: lastNewline };
171
+ }
172
+
173
+ // A single line longer than the platform shard limit: hard-cut on a code
174
+ // point boundary and terminate the shard with a newline. This is the only
175
+ // way to satisfy the API without dropping content.
176
+ if (consumed === 0) {
177
+ const first = buffer.codePointAt(0) ?? 0;
178
+ const char = String.fromCodePoint(first);
179
+ return { text: `${char}\n`, consumed: char.length };
180
+ }
181
+ return { text: `${buffer.slice(0, consumed)}\n`, consumed };
137
182
  }
138
183
 
139
184
  private async doFlush(state: "generating" | "done"): Promise<void> {
140
185
  if (this.flushing) return;
141
- const content = state === "done" ? "" : this.buffer;
186
+ this.clearFlushTimer();
187
+ const frame = state === "done" ? { text: "", consumed: 0 } : this.takeFrame();
188
+ const content = frame.text;
142
189
  if (!content && state === "generating") return;
143
- this.opts.onDebug?.(
144
- `flush: start state=${state}, content=${content.length}, index=${this.index}, msgSeq=${this.msgSeq ?? "none"}, streamMsgId=${this.streamMsgId ?? "none"}`,
145
- );
190
+ const frameStartedAt = Date.now();
146
191
  this.flushing = true;
147
192
  let failed = false;
148
193
  try {
@@ -161,11 +206,19 @@ export class AppendStream {
161
206
  } as unknown as StreamMessageRequest;
162
207
  const resp = await this.sendWithRetry(req, state);
163
208
  if (resp?.id && !this.streamMsgId) this.streamMsgId = resp.id;
164
- if (state === "generating") this.buffer = this.buffer.slice(content.length);
209
+ if (state === "generating") this.buffer = this.buffer.slice(frame.consumed);
165
210
  this.lastFlushAt = Date.now();
166
- this.opts.onDebug?.(
167
- `flush: done state=${state}, remaining=${this.buffer.length}, streamMsgId=${this.streamMsgId ?? "none"}`,
168
- );
211
+ const elapsedMs = this.lastFlushAt - frameStartedAt;
212
+ const remaining = this.buffer.length;
213
+ if (elapsedMs >= SLOW_FRAME_MS) {
214
+ this.opts.onDebug?.(
215
+ `flush: slow state=${state}, elapsed=${elapsedMs}, content=${content.length}, index=${index}, msgSeq=${this.msgSeq ?? "none"}, streamMsgId=${shortId(this.streamMsgId)}, remaining=${remaining}`,
216
+ );
217
+ } else if (remaining > 0) {
218
+ this.opts.onDebug?.(
219
+ `flush: partial state=${state}, index=${index}, msgSeq=${this.msgSeq ?? "none"}, streamMsgId=${shortId(this.streamMsgId)}, remaining=${remaining}`,
220
+ );
221
+ }
169
222
  } catch (err) {
170
223
  failed = true;
171
224
  throw err;