@chen_hu_hu/pi-qq 1.3.1 → 1.3.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.
- package/CHANGELOG.md +61 -0
- package/doc/archive/README.md +18 -0
- package/doc/{delivery-v1-zh.md → archive/delivery-v1-zh.md} +2 -1
- package/doc/{delivery-v1.md → archive/delivery-v1.md} +2 -1
- package/doc/{delivery-v2-zh.md → archive/delivery-v2-zh.md} +2 -1
- package/doc/{delivery-v2.md → archive/delivery-v2.md} +2 -1
- package/doc/{delivery-v3-zh.md → archive/delivery-v3-zh.md} +2 -1
- package/doc/{delivery-v3.md → archive/delivery-v3.md} +3 -2
- package/index.ts +34 -25
- package/package.json +2 -1
- package/src/infra/file-logger.ts +21 -6
- package/src/qq/inbound-attachments.ts +101 -0
- package/src/qq/inbound-command.ts +74 -0
- package/src/qq/qq-message-handler.ts +25 -116
- package/src/relay/bubble-writer.ts +338 -0
- package/src/relay/run-stream-relay.ts +48 -336
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import type { QQBot } from "@tencent-connect/qqbot-nodejs";
|
|
2
|
-
import {
|
|
2
|
+
import { BubbleWriter } from "./bubble-writer";
|
|
3
3
|
import type { ReplyBudget } from "../quota/reply-quota";
|
|
4
4
|
import { log } from "../infra/file-logger";
|
|
5
|
-
import { THINKING_HEADER, formatToolLine, formatToolResult } from "./stream-block-format";
|
|
6
5
|
|
|
7
6
|
export interface RunFeedbackDeps {
|
|
8
7
|
bot: () => QQBot | undefined;
|
|
@@ -11,8 +10,6 @@ export interface RunFeedbackDeps {
|
|
|
11
10
|
onWarn: (msg: string) => void;
|
|
12
11
|
}
|
|
13
12
|
|
|
14
|
-
type BlockKind = "none" | "thinking" | "tool" | "text";
|
|
15
|
-
|
|
16
13
|
interface ToolCall {
|
|
17
14
|
id: string;
|
|
18
15
|
name: string;
|
|
@@ -27,47 +24,31 @@ interface RunState {
|
|
|
27
24
|
openid: string;
|
|
28
25
|
msgId?: string;
|
|
29
26
|
thinkingSentLen: number;
|
|
30
|
-
thinkingAtLineStart: boolean;
|
|
31
|
-
snapshotText: string;
|
|
32
27
|
finalizedText: string;
|
|
33
28
|
resultSentLen: number;
|
|
34
|
-
lastKind: BlockKind;
|
|
35
|
-
cur: AppendStream | null;
|
|
36
|
-
pendingResume?: AppendStreamCheckpoint;
|
|
37
|
-
streamDead: boolean;
|
|
38
|
-
resumeAttempts: number;
|
|
39
|
-
pushedAny: boolean;
|
|
40
|
-
bubbleChars: number;
|
|
41
|
-
fenceOpen: boolean;
|
|
42
|
-
fenceLen: number;
|
|
43
|
-
fenceQuoted: boolean;
|
|
44
|
-
lastChar: string;
|
|
45
|
-
toolOpen: boolean;
|
|
46
|
-
activeTool?: string;
|
|
47
29
|
tools: ToolCall[];
|
|
30
|
+
activeTool?: string;
|
|
48
31
|
deferredThinking: string;
|
|
49
32
|
deferredText: string;
|
|
50
|
-
blockClosed: boolean;
|
|
51
33
|
hadError: boolean;
|
|
34
|
+
writer: BubbleWriter;
|
|
52
35
|
typingTimer?: ReturnType<typeof setInterval>;
|
|
53
36
|
}
|
|
54
37
|
|
|
55
38
|
const TYPING_INTERVAL_MS = 50_000;
|
|
56
39
|
const PENDING_TTL_MS = 15_000;
|
|
57
40
|
const RECOVERY_TTL_MS = 60_000;
|
|
58
|
-
const MAX_RESUME_ATTEMPTS = 2;
|
|
59
|
-
const MAX_BUBBLE_CHARS = 12_000;
|
|
60
|
-
|
|
61
|
-
function trackThinkingLineStart(delta: string, run: RunState): void {
|
|
62
|
-
run.thinkingAtLineStart = delta.endsWith("\n");
|
|
63
|
-
}
|
|
64
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Coordinates a QQ-triggered agent run: target selection, tool ordering,
|
|
44
|
+
* deferred content, typing indicators, and run lifetime. Bubble layout and
|
|
45
|
+
* stream I/O are delegated to BubbleWriter.
|
|
46
|
+
*/
|
|
65
47
|
export class RunFeedback {
|
|
66
48
|
private readonly deps: RunFeedbackDeps;
|
|
67
49
|
private pending: { openid: string; msgId: string; at: number } | null = null;
|
|
68
50
|
private recovery: { openid: string; msgId: string; at: number } | null = null;
|
|
69
51
|
private run: RunState | null = null;
|
|
70
|
-
private queue: Promise<void> = Promise.resolve();
|
|
71
52
|
private endPromise: Promise<void> = Promise.resolve();
|
|
72
53
|
private endResolve?: () => void;
|
|
73
54
|
|
|
@@ -109,34 +90,29 @@ export class RunFeedback {
|
|
|
109
90
|
const bot = this.deps.bot();
|
|
110
91
|
if (!bot) return;
|
|
111
92
|
|
|
93
|
+
const startedAt = Date.now();
|
|
94
|
+
const writer = new BubbleWriter({
|
|
95
|
+
bot: this.deps.bot,
|
|
96
|
+
budget: this.deps.budget,
|
|
97
|
+
openid: target.openid,
|
|
98
|
+
msgId: target.msgId,
|
|
99
|
+
startedAt,
|
|
100
|
+
onWarn: this.deps.onWarn,
|
|
101
|
+
});
|
|
112
102
|
const run: RunState = {
|
|
113
|
-
startedAt
|
|
103
|
+
startedAt,
|
|
114
104
|
openid: target.openid,
|
|
115
105
|
msgId: target.msgId,
|
|
116
106
|
thinkingSentLen: 0,
|
|
117
|
-
thinkingAtLineStart: true,
|
|
118
|
-
snapshotText: "",
|
|
119
107
|
finalizedText: "",
|
|
120
108
|
resultSentLen: 0,
|
|
121
|
-
lastKind: "none",
|
|
122
|
-
cur: null,
|
|
123
|
-
streamDead: false,
|
|
124
|
-
resumeAttempts: 0,
|
|
125
|
-
pushedAny: false,
|
|
126
|
-
bubbleChars: 0,
|
|
127
|
-
fenceOpen: false,
|
|
128
|
-
fenceLen: 0,
|
|
129
|
-
fenceQuoted: false,
|
|
130
|
-
lastChar: "",
|
|
131
|
-
toolOpen: false,
|
|
132
109
|
tools: [],
|
|
133
110
|
deferredThinking: "",
|
|
134
111
|
deferredText: "",
|
|
135
|
-
blockClosed: false,
|
|
136
112
|
hadError: false,
|
|
113
|
+
writer,
|
|
137
114
|
};
|
|
138
115
|
this.run = run;
|
|
139
|
-
this.queue = Promise.resolve();
|
|
140
116
|
log.debug("run", "started", { msgId: run.msgId?.slice(0, 8) ?? "?" });
|
|
141
117
|
|
|
142
118
|
const keepTyping = () => {
|
|
@@ -148,7 +124,7 @@ export class RunFeedback {
|
|
|
148
124
|
|
|
149
125
|
onToolStart(toolName: string, args?: unknown, toolCallId?: string): void {
|
|
150
126
|
const run = this.run;
|
|
151
|
-
if (!run || run.
|
|
127
|
+
if (!run || run.writer.dead) return;
|
|
152
128
|
const id = toolCallId ?? toolName;
|
|
153
129
|
if (run.tools.some((tool) => tool.id === id)) return;
|
|
154
130
|
run.tools.push({ id, name: toolName, args, started: false, finished: false });
|
|
@@ -157,7 +133,7 @@ export class RunFeedback {
|
|
|
157
133
|
|
|
158
134
|
onToolEnd(toolName: string, isError: boolean, result?: unknown, toolCallId?: string): void {
|
|
159
135
|
const run = this.run;
|
|
160
|
-
if (!run || run.
|
|
136
|
+
if (!run || run.writer.dead) return;
|
|
161
137
|
const id = toolCallId ?? toolName;
|
|
162
138
|
const tool = run.tools.find((candidate) => candidate.id === id);
|
|
163
139
|
if (!tool) return;
|
|
@@ -165,20 +141,9 @@ export class RunFeedback {
|
|
|
165
141
|
if (run.activeTool === id) this.finishTool(run, tool);
|
|
166
142
|
}
|
|
167
143
|
|
|
168
|
-
private pushText(run: RunState, text: string): void {
|
|
169
|
-
const newBubble = this.startsNewBlock(run, "text");
|
|
170
|
-
const prefix = this.beginBlock(run, "text", newBubble);
|
|
171
|
-
this.push(run, `${prefix}${text}`, prefix.length > 0 || newBubble, false, newBubble);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
private finishEnd(): void {
|
|
175
|
-
this.endResolve?.();
|
|
176
|
-
this.endResolve = undefined;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
144
|
onAssistantDelta(snapshotText: string, snapshotThinking: string): void {
|
|
180
145
|
const run = this.run;
|
|
181
|
-
if (!run || run.
|
|
146
|
+
if (!run || run.writer.dead) return;
|
|
182
147
|
|
|
183
148
|
if (snapshotThinking.length > run.thinkingSentLen) {
|
|
184
149
|
const delta = snapshotThinking.slice(run.thinkingSentLen);
|
|
@@ -187,10 +152,7 @@ export class RunFeedback {
|
|
|
187
152
|
if (this.hasPendingTools(run)) {
|
|
188
153
|
run.deferredThinking += delta;
|
|
189
154
|
} else {
|
|
190
|
-
|
|
191
|
-
const prefix = this.beginBlock(run, "thinking", newBubble);
|
|
192
|
-
trackThinkingLineStart(delta, run);
|
|
193
|
-
this.push(run, `${prefix}${delta}`, prefix.length > 0 || newBubble, false, newBubble, true);
|
|
155
|
+
run.writer.pushThinking(delta);
|
|
194
156
|
}
|
|
195
157
|
}
|
|
196
158
|
}
|
|
@@ -203,7 +165,7 @@ export class RunFeedback {
|
|
|
203
165
|
run.deferredText += delta;
|
|
204
166
|
return;
|
|
205
167
|
}
|
|
206
|
-
|
|
168
|
+
run.writer.pushText(delta);
|
|
207
169
|
}
|
|
208
170
|
|
|
209
171
|
async onAssistantFinal(text: string, isError: boolean): Promise<boolean> {
|
|
@@ -211,17 +173,16 @@ export class RunFeedback {
|
|
|
211
173
|
if (!run) return false;
|
|
212
174
|
|
|
213
175
|
run.finalizedText = [run.finalizedText, text].filter((part) => part.trim()).join("\n\n");
|
|
214
|
-
run.snapshotText = "";
|
|
215
176
|
const delta = run.finalizedText.slice(run.resultSentLen);
|
|
216
177
|
run.resultSentLen = run.finalizedText.length;
|
|
217
178
|
if (!isError && text && delta) {
|
|
218
179
|
if (this.hasPendingTools(run)) {
|
|
219
180
|
run.deferredText += delta;
|
|
220
181
|
} else {
|
|
221
|
-
|
|
182
|
+
run.writer.pushText(delta);
|
|
222
183
|
}
|
|
223
184
|
}
|
|
224
|
-
if (!isError && text) run.
|
|
185
|
+
if (!isError && text) run.writer.closeBlock();
|
|
225
186
|
|
|
226
187
|
run.thinkingSentLen = 0;
|
|
227
188
|
if (isError) {
|
|
@@ -229,12 +190,11 @@ export class RunFeedback {
|
|
|
229
190
|
this.recovery = { openid: run.openid, msgId: run.msgId ?? "", at: Date.now() };
|
|
230
191
|
}
|
|
231
192
|
|
|
232
|
-
const ok = await
|
|
193
|
+
const ok = await run.writer.flush();
|
|
233
194
|
run.finalizedText = "";
|
|
234
|
-
run.snapshotText = "";
|
|
235
195
|
run.resultSentLen = 0;
|
|
236
196
|
if (isError) return false;
|
|
237
|
-
return ok && run.
|
|
197
|
+
return ok && run.writer.hasPushed;
|
|
238
198
|
}
|
|
239
199
|
|
|
240
200
|
async onAgentEnd(): Promise<void> {
|
|
@@ -243,86 +203,39 @@ export class RunFeedback {
|
|
|
243
203
|
this.finishEnd();
|
|
244
204
|
return;
|
|
245
205
|
}
|
|
246
|
-
await
|
|
206
|
+
await run.writer.whenIdle();
|
|
247
207
|
if (!this.hasPendingTools(run)) this.flushDeferred(run);
|
|
248
|
-
await
|
|
208
|
+
await run.writer.whenIdle();
|
|
249
209
|
this.clearTimers();
|
|
250
|
-
if (!run.hadError && run.
|
|
210
|
+
if (!run.hadError && run.writer.hasPushed) this.recovery = null;
|
|
251
211
|
this.run = null;
|
|
252
212
|
log.debug("run", "ended", {
|
|
253
213
|
msgId: run.msgId?.slice(0, 8) ?? "?",
|
|
254
214
|
elapsedMs: Date.now() - run.startedAt,
|
|
255
215
|
});
|
|
256
|
-
if (!run.
|
|
257
|
-
run.
|
|
216
|
+
if (!run.writer.hasPushed) {
|
|
217
|
+
run.writer.dispose();
|
|
258
218
|
this.finishEnd();
|
|
259
219
|
return;
|
|
260
220
|
}
|
|
261
|
-
const ok = await
|
|
262
|
-
if (ok
|
|
263
|
-
|
|
264
|
-
await run.cur.done();
|
|
265
|
-
} catch (err) {
|
|
266
|
-
log.warn("stream", "DONE failed", {
|
|
267
|
-
msgId: run.msgId?.slice(0, 8) ?? "?",
|
|
268
|
-
err,
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
run.cur?.dispose();
|
|
221
|
+
const ok = await run.writer.flush();
|
|
222
|
+
if (ok) await run.writer.done();
|
|
223
|
+
run.writer.dispose();
|
|
273
224
|
this.finishEnd();
|
|
274
225
|
}
|
|
275
226
|
|
|
276
227
|
dispose(): void {
|
|
277
228
|
this.clearTimers();
|
|
278
|
-
this.run?.
|
|
229
|
+
this.run?.writer.dispose();
|
|
279
230
|
this.run = null;
|
|
280
231
|
this.pending = null;
|
|
281
232
|
this.recovery = null;
|
|
282
233
|
this.finishEnd();
|
|
283
234
|
}
|
|
284
235
|
|
|
285
|
-
private
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
run.blockClosed = false;
|
|
289
|
-
if (kind === "thinking") run.thinkingAtLineStart = true;
|
|
290
|
-
return "";
|
|
291
|
-
}
|
|
292
|
-
if (run.lastKind === kind) return "";
|
|
293
|
-
let prefix = "";
|
|
294
|
-
if (run.lastKind !== "none") {
|
|
295
|
-
if (run.lastKind === "thinking" && !run.thinkingAtLineStart) prefix += "\n";
|
|
296
|
-
prefix += run.lastKind === "text" ? "\n\n" : "\n";
|
|
297
|
-
}
|
|
298
|
-
run.lastKind = kind;
|
|
299
|
-
run.blockClosed = false;
|
|
300
|
-
if (kind === "thinking") run.thinkingAtLineStart = true;
|
|
301
|
-
return prefix;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
private startsNewBlock(run: RunState, kind: BlockKind): boolean {
|
|
305
|
-
return run.blockClosed || (run.lastKind !== "none" && run.lastKind !== kind);
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
private async closeOpenFence(run: RunState): Promise<boolean> {
|
|
309
|
-
if (!run.fenceOpen) return true;
|
|
310
|
-
const stream = this.ensureStream(run);
|
|
311
|
-
if (!stream) return false;
|
|
312
|
-
const ticks = "`".repeat(run.fenceLen);
|
|
313
|
-
const closing = `\n${run.fenceQuoted ? "> " : ""}${ticks}\n`;
|
|
314
|
-
try {
|
|
315
|
-
await stream.append(closing);
|
|
316
|
-
run.bubbleChars += closing.length;
|
|
317
|
-
run.lastChar = "\n";
|
|
318
|
-
run.fenceOpen = false;
|
|
319
|
-
run.fenceLen = 0;
|
|
320
|
-
run.fenceQuoted = false;
|
|
321
|
-
return true;
|
|
322
|
-
} catch (err) {
|
|
323
|
-
if (run.cur === stream) this.handleStreamError(run, err as Error);
|
|
324
|
-
return false;
|
|
325
|
-
}
|
|
236
|
+
private finishEnd(): void {
|
|
237
|
+
this.endResolve?.();
|
|
238
|
+
this.endResolve = undefined;
|
|
326
239
|
}
|
|
327
240
|
|
|
328
241
|
private hasPendingTools(run: RunState): boolean {
|
|
@@ -330,48 +243,35 @@ export class RunFeedback {
|
|
|
330
243
|
}
|
|
331
244
|
|
|
332
245
|
private flushDeferred(run: RunState): void {
|
|
333
|
-
if (run.
|
|
246
|
+
if (run.writer.dead) return;
|
|
334
247
|
if (run.deferredThinking) {
|
|
335
248
|
const thinking = run.deferredThinking;
|
|
336
249
|
run.deferredThinking = "";
|
|
337
|
-
|
|
338
|
-
const prefix = this.beginBlock(run, "thinking", newBubble);
|
|
339
|
-
trackThinkingLineStart(thinking, run);
|
|
340
|
-
this.push(run, `${prefix}${thinking}`, prefix.length > 0 || newBubble, false, newBubble, true);
|
|
250
|
+
run.writer.pushThinking(thinking);
|
|
341
251
|
}
|
|
342
252
|
if (run.deferredText) {
|
|
343
253
|
const text = run.deferredText;
|
|
344
254
|
run.deferredText = "";
|
|
345
|
-
|
|
255
|
+
run.writer.pushText(text);
|
|
346
256
|
}
|
|
347
257
|
}
|
|
348
258
|
|
|
349
259
|
private startNextTool(run: RunState): void {
|
|
350
|
-
if (run.
|
|
260
|
+
if (run.writer.dead || run.activeTool) return;
|
|
351
261
|
const tool = run.tools.find((candidate) => !candidate.started);
|
|
352
262
|
if (!tool) return;
|
|
353
263
|
tool.started = true;
|
|
354
264
|
run.activeTool = tool.id;
|
|
355
|
-
run.
|
|
356
|
-
const newBubble = run.lastKind !== "none";
|
|
357
|
-
const prefix = this.beginBlock(run, "tool", newBubble);
|
|
358
|
-
this.push(
|
|
359
|
-
run,
|
|
360
|
-
`${prefix}${formatToolLine(tool.name, tool.args)}\n\n`,
|
|
361
|
-
prefix.length > 0 || newBubble,
|
|
362
|
-
false,
|
|
363
|
-
newBubble,
|
|
364
|
-
);
|
|
265
|
+
run.writer.pushTool(tool.name, tool.args);
|
|
365
266
|
if (tool.result) this.finishTool(run, tool);
|
|
366
267
|
}
|
|
367
268
|
|
|
368
269
|
private finishTool(run: RunState, tool: ToolCall): void {
|
|
369
270
|
if (!tool.result || tool.finished) return;
|
|
370
271
|
tool.finished = true;
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
if (this.run !== run || run.streamDead) return;
|
|
272
|
+
run.writer.pushToolResult(tool.result.result, tool.result.isError);
|
|
273
|
+
run.writer.schedule(() => {
|
|
274
|
+
if (this.run !== run) return;
|
|
375
275
|
if (run.activeTool !== tool.id) return;
|
|
376
276
|
run.activeTool = undefined;
|
|
377
277
|
this.startNextTool(run);
|
|
@@ -379,194 +279,6 @@ export class RunFeedback {
|
|
|
379
279
|
});
|
|
380
280
|
}
|
|
381
281
|
|
|
382
|
-
private push(
|
|
383
|
-
run: RunState,
|
|
384
|
-
text: string,
|
|
385
|
-
blockBoundary = false,
|
|
386
|
-
endTool = false,
|
|
387
|
-
newBubble = false,
|
|
388
|
-
thinking = false,
|
|
389
|
-
): void {
|
|
390
|
-
if (!text || run.streamDead) return;
|
|
391
|
-
void this.enqueue(async () => {
|
|
392
|
-
if (this.run !== run || run.streamDead) return;
|
|
393
|
-
if (blockBoundary && !(await this.closeOpenFence(run))) return;
|
|
394
|
-
if (newBubble && run.cur && run.bubbleChars > 0 && !run.pendingResume) {
|
|
395
|
-
await this.rotateStream(run);
|
|
396
|
-
if (this.run !== run || run.streamDead) return;
|
|
397
|
-
}
|
|
398
|
-
if (
|
|
399
|
-
!newBubble &&
|
|
400
|
-
run.bubbleChars >= MAX_BUBBLE_CHARS &&
|
|
401
|
-
run.cur &&
|
|
402
|
-
!run.pendingResume &&
|
|
403
|
-
!run.fenceOpen &&
|
|
404
|
-
!run.toolOpen &&
|
|
405
|
-
(blockBoundary || run.lastChar === "\n")
|
|
406
|
-
) {
|
|
407
|
-
await this.rotateStream(run);
|
|
408
|
-
if (this.run !== run || run.streamDead) return;
|
|
409
|
-
}
|
|
410
|
-
const stream = this.ensureStream(run);
|
|
411
|
-
if (!stream) return;
|
|
412
|
-
run.pushedAny = true;
|
|
413
|
-
const outgoing = thinking && (!run.cur || run.bubbleChars === 0) ? `${THINKING_HEADER}${text}` : text;
|
|
414
|
-
try {
|
|
415
|
-
await stream.append(outgoing);
|
|
416
|
-
run.bubbleChars += outgoing.length;
|
|
417
|
-
this.trackFences(run, outgoing);
|
|
418
|
-
run.lastChar = outgoing.slice(-1);
|
|
419
|
-
if (endTool) run.toolOpen = false;
|
|
420
|
-
} catch (err) {
|
|
421
|
-
if (run.cur === stream) this.handleStreamError(run, err as Error);
|
|
422
|
-
}
|
|
423
|
-
});
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
private trackFences(run: RunState, text: string): void {
|
|
427
|
-
const previousChar = run.lastChar;
|
|
428
|
-
for (const match of text.matchAll(/(?:^|\n)[ \t>]*(`{3,})/g)) {
|
|
429
|
-
if (match.index === 0 && previousChar && previousChar !== "\n") continue;
|
|
430
|
-
const fenceLen = match[1].length;
|
|
431
|
-
const lineEnd = text.indexOf("\n", match.index + match[0].length);
|
|
432
|
-
const restOfLine = text.slice(match.index + match[0].length, lineEnd < 0 ? undefined : lineEnd);
|
|
433
|
-
if (restOfLine.includes("`".repeat(fenceLen))) continue;
|
|
434
|
-
if (!run.fenceOpen) {
|
|
435
|
-
run.fenceOpen = true;
|
|
436
|
-
run.fenceLen = fenceLen;
|
|
437
|
-
run.fenceQuoted = match[0].includes(">");
|
|
438
|
-
} else if (fenceLen >= run.fenceLen) {
|
|
439
|
-
run.fenceOpen = false;
|
|
440
|
-
run.fenceLen = 0;
|
|
441
|
-
run.fenceQuoted = false;
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
private async rotateStream(run: RunState): Promise<void> {
|
|
447
|
-
const stream = run.cur;
|
|
448
|
-
if (!stream) return;
|
|
449
|
-
try {
|
|
450
|
-
if (run.lastChar && run.lastChar !== "\n") {
|
|
451
|
-
await stream.append("\n");
|
|
452
|
-
run.bubbleChars += 1;
|
|
453
|
-
run.lastChar = "\n";
|
|
454
|
-
run.thinkingAtLineStart = true;
|
|
455
|
-
}
|
|
456
|
-
await stream.flush();
|
|
457
|
-
await stream.done();
|
|
458
|
-
} catch (err) {
|
|
459
|
-
log.warn("stream", "bubble split failed", {
|
|
460
|
-
msgId: run.msgId?.slice(0, 8) ?? "?",
|
|
461
|
-
err,
|
|
462
|
-
});
|
|
463
|
-
if (run.cur === stream) this.handleStreamError(run, err as Error);
|
|
464
|
-
return;
|
|
465
|
-
}
|
|
466
|
-
stream.dispose();
|
|
467
|
-
if (run.cur === stream) run.cur = null;
|
|
468
|
-
run.bubbleChars = 0;
|
|
469
|
-
run.fenceOpen = false;
|
|
470
|
-
run.fenceLen = 0;
|
|
471
|
-
run.fenceQuoted = false;
|
|
472
|
-
run.lastChar = "";
|
|
473
|
-
run.thinkingAtLineStart = true;
|
|
474
|
-
log.debug("stream", "bubble split", {
|
|
475
|
-
msgId: run.msgId?.slice(0, 8) ?? "?",
|
|
476
|
-
elapsedMs: Date.now() - run.startedAt,
|
|
477
|
-
});
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
private async drainQueue(): Promise<void> {
|
|
481
|
-
for (;;) {
|
|
482
|
-
const current = this.queue;
|
|
483
|
-
await current;
|
|
484
|
-
if (current === this.queue) return;
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
private enqueue(op: () => Promise<void>): Promise<void> {
|
|
489
|
-
const next = this.queue.then(op, op);
|
|
490
|
-
this.queue = next.catch(() => {});
|
|
491
|
-
return next;
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
private ensureStream(run: RunState): AppendStream | null {
|
|
495
|
-
if (run.streamDead) return null;
|
|
496
|
-
if (run.cur) return run.cur;
|
|
497
|
-
const bot = this.deps.bot();
|
|
498
|
-
if (!bot || !run.msgId) return null;
|
|
499
|
-
const resume = run.pendingResume;
|
|
500
|
-
run.pendingResume = undefined;
|
|
501
|
-
const stream = new AppendStream(bot.messageApi, bot.getCredentials(), run.openid, run.msgId, {
|
|
502
|
-
resume,
|
|
503
|
-
onError: (err) => {
|
|
504
|
-
void this.enqueue(async () => {
|
|
505
|
-
if (this.run !== run) return;
|
|
506
|
-
if (run.cur === stream) this.handleStreamError(run, err);
|
|
507
|
-
});
|
|
508
|
-
},
|
|
509
|
-
onDebug: (message) => log.debug("stream", message, { msgId: run.msgId?.slice(0, 8) ?? "?" }),
|
|
510
|
-
});
|
|
511
|
-
run.cur = stream;
|
|
512
|
-
if (!resume) this.deps.budget.noteStreamBubble(run.msgId);
|
|
513
|
-
return stream;
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
private async flushStream(run: RunState): Promise<boolean> {
|
|
517
|
-
await this.drainQueue();
|
|
518
|
-
if (run.streamDead) return false;
|
|
519
|
-
if (!run.cur && !run.pendingResume) return true;
|
|
520
|
-
for (let attempt = 0; attempt <= MAX_RESUME_ATTEMPTS; attempt++) {
|
|
521
|
-
const stream = this.ensureStream(run);
|
|
522
|
-
if (!stream) return false;
|
|
523
|
-
try {
|
|
524
|
-
await stream.flush();
|
|
525
|
-
await this.queue;
|
|
526
|
-
if (run.streamDead) return false;
|
|
527
|
-
if (run.cur !== stream) continue;
|
|
528
|
-
return true;
|
|
529
|
-
} catch (err) {
|
|
530
|
-
this.handleStreamError(run, err as Error);
|
|
531
|
-
if (run.streamDead) return false;
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
this.killStream(run, "resume attempts exhausted");
|
|
535
|
-
return false;
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
private handleStreamError(run: RunState, err: Error): void {
|
|
539
|
-
if (run.streamDead) return;
|
|
540
|
-
const stream = run.cur;
|
|
541
|
-
const checkpoint = stream?.checkpoint() ?? null;
|
|
542
|
-
stream?.dispose();
|
|
543
|
-
run.cur = null;
|
|
544
|
-
log.warn("stream", "stream error", {
|
|
545
|
-
msgId: run.msgId?.slice(0, 8) ?? "?",
|
|
546
|
-
elapsedMs: Date.now() - run.startedAt,
|
|
547
|
-
err,
|
|
548
|
-
});
|
|
549
|
-
if (checkpoint && run.resumeAttempts < MAX_RESUME_ATTEMPTS) {
|
|
550
|
-
run.resumeAttempts += 1;
|
|
551
|
-
run.pendingResume = checkpoint;
|
|
552
|
-
return;
|
|
553
|
-
}
|
|
554
|
-
this.killStream(run, err.message);
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
private killStream(run: RunState, reason: string): void {
|
|
558
|
-
if (run.streamDead) return;
|
|
559
|
-
run.streamDead = true;
|
|
560
|
-
run.pendingResume = undefined;
|
|
561
|
-
run.cur?.dispose();
|
|
562
|
-
run.cur = null;
|
|
563
|
-
log.warn("stream", "stream unavailable", {
|
|
564
|
-
msgId: run.msgId?.slice(0, 8) ?? "?",
|
|
565
|
-
reason,
|
|
566
|
-
});
|
|
567
|
-
this.deps.onWarn(`Stream unavailable; falling back to text (${reason}).`);
|
|
568
|
-
}
|
|
569
|
-
|
|
570
282
|
private clearTimers(): void {
|
|
571
283
|
const run = this.run;
|
|
572
284
|
if (!run) return;
|