@chen_hu_hu/pi-qq 1.3.1 → 1.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/CHANGELOG.md +68 -0
- package/README-zh.md +8 -7
- package/README.md +8 -7
- 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/doc/bugfix-v4.md +698 -0
- package/index.ts +36 -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 +99 -0
- package/src/qq/qq-message-handler.ts +48 -117
- package/src/qq/reply-sender.ts +23 -0
- package/src/relay/bubble-writer.ts +369 -0
- package/src/relay/run-stream-relay.ts +88 -339
|
@@ -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,34 @@ interface RunState {
|
|
|
27
24
|
openid: string;
|
|
28
25
|
msgId?: string;
|
|
29
26
|
thinkingSentLen: number;
|
|
30
|
-
thinkingAtLineStart: boolean;
|
|
31
|
-
snapshotText: string;
|
|
32
27
|
finalizedText: string;
|
|
28
|
+
/** Latest text of the current run, kept for the QQ /last command. */
|
|
29
|
+
currentText: string;
|
|
33
30
|
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
31
|
tools: ToolCall[];
|
|
32
|
+
activeTool?: string;
|
|
48
33
|
deferredThinking: string;
|
|
49
34
|
deferredText: string;
|
|
50
|
-
blockClosed: boolean;
|
|
51
35
|
hadError: boolean;
|
|
36
|
+
writer: BubbleWriter;
|
|
52
37
|
typingTimer?: ReturnType<typeof setInterval>;
|
|
53
38
|
}
|
|
54
39
|
|
|
55
40
|
const TYPING_INTERVAL_MS = 50_000;
|
|
56
41
|
const PENDING_TTL_MS = 15_000;
|
|
57
42
|
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
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Coordinates a QQ-triggered agent run: target selection, tool ordering,
|
|
46
|
+
* deferred content, typing indicators, and run lifetime. Bubble layout and
|
|
47
|
+
* stream I/O are delegated to BubbleWriter.
|
|
48
|
+
*/
|
|
65
49
|
export class RunFeedback {
|
|
66
50
|
private readonly deps: RunFeedbackDeps;
|
|
67
51
|
private pending: { openid: string; msgId: string; at: number } | null = null;
|
|
68
52
|
private recovery: { openid: string; msgId: string; at: number } | null = null;
|
|
69
53
|
private run: RunState | null = null;
|
|
70
|
-
private
|
|
54
|
+
private lastCompletedText: string | undefined;
|
|
71
55
|
private endPromise: Promise<void> = Promise.resolve();
|
|
72
56
|
private endResolve?: () => void;
|
|
73
57
|
|
|
@@ -92,6 +76,32 @@ export class RunFeedback {
|
|
|
92
76
|
return this.run ? this.endPromise : Promise.resolve();
|
|
93
77
|
}
|
|
94
78
|
|
|
79
|
+
/**
|
|
80
|
+
* The final assistant text of the most recently completed QQ run.
|
|
81
|
+
* No transcript is retained.
|
|
82
|
+
*/
|
|
83
|
+
getLastMessage(): string | undefined {
|
|
84
|
+
return this.lastCompletedText;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Restart the current run's push with its latest text. This revives a stream
|
|
89
|
+
* that may have died while the phone was offline; later deltas continue.
|
|
90
|
+
*/
|
|
91
|
+
repushCurrent(openid: string, msgId: string): boolean {
|
|
92
|
+
const run = this.run;
|
|
93
|
+
const text = run?.currentText;
|
|
94
|
+
if (!run || !text?.trim()) return false;
|
|
95
|
+
// Rebind to the /last message so the new stream has a fresh passive window.
|
|
96
|
+
run.openid = openid;
|
|
97
|
+
run.msgId = msgId;
|
|
98
|
+
// Push the current text in full. Do not rewrite resultSentLen: it belongs to
|
|
99
|
+
// the assistant message currently being diffed, and changing it could drop
|
|
100
|
+
// the beginning of the next message.
|
|
101
|
+
run.writer.restartText(text, { openid, msgId });
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
|
|
95
105
|
onAgentStart(): void {
|
|
96
106
|
this.endPromise = new Promise((resolve) => {
|
|
97
107
|
this.endResolve = resolve;
|
|
@@ -109,34 +119,30 @@ export class RunFeedback {
|
|
|
109
119
|
const bot = this.deps.bot();
|
|
110
120
|
if (!bot) return;
|
|
111
121
|
|
|
122
|
+
const startedAt = Date.now();
|
|
123
|
+
const writer = new BubbleWriter({
|
|
124
|
+
bot: this.deps.bot,
|
|
125
|
+
budget: this.deps.budget,
|
|
126
|
+
openid: target.openid,
|
|
127
|
+
msgId: target.msgId,
|
|
128
|
+
startedAt,
|
|
129
|
+
onWarn: this.deps.onWarn,
|
|
130
|
+
});
|
|
112
131
|
const run: RunState = {
|
|
113
|
-
startedAt
|
|
132
|
+
startedAt,
|
|
114
133
|
openid: target.openid,
|
|
115
134
|
msgId: target.msgId,
|
|
116
135
|
thinkingSentLen: 0,
|
|
117
|
-
thinkingAtLineStart: true,
|
|
118
|
-
snapshotText: "",
|
|
119
136
|
finalizedText: "",
|
|
137
|
+
currentText: "",
|
|
120
138
|
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
139
|
tools: [],
|
|
133
140
|
deferredThinking: "",
|
|
134
141
|
deferredText: "",
|
|
135
|
-
blockClosed: false,
|
|
136
142
|
hadError: false,
|
|
143
|
+
writer,
|
|
137
144
|
};
|
|
138
145
|
this.run = run;
|
|
139
|
-
this.queue = Promise.resolve();
|
|
140
146
|
log.debug("run", "started", { msgId: run.msgId?.slice(0, 8) ?? "?" });
|
|
141
147
|
|
|
142
148
|
const keepTyping = () => {
|
|
@@ -148,7 +154,7 @@ export class RunFeedback {
|
|
|
148
154
|
|
|
149
155
|
onToolStart(toolName: string, args?: unknown, toolCallId?: string): void {
|
|
150
156
|
const run = this.run;
|
|
151
|
-
if (!run || run.
|
|
157
|
+
if (!run || run.writer.dead) return;
|
|
152
158
|
const id = toolCallId ?? toolName;
|
|
153
159
|
if (run.tools.some((tool) => tool.id === id)) return;
|
|
154
160
|
run.tools.push({ id, name: toolName, args, started: false, finished: false });
|
|
@@ -157,7 +163,7 @@ export class RunFeedback {
|
|
|
157
163
|
|
|
158
164
|
onToolEnd(toolName: string, isError: boolean, result?: unknown, toolCallId?: string): void {
|
|
159
165
|
const run = this.run;
|
|
160
|
-
if (!run || run.
|
|
166
|
+
if (!run || run.writer.dead) return;
|
|
161
167
|
const id = toolCallId ?? toolName;
|
|
162
168
|
const tool = run.tools.find((candidate) => candidate.id === id);
|
|
163
169
|
if (!tool) return;
|
|
@@ -165,37 +171,27 @@ export class RunFeedback {
|
|
|
165
171
|
if (run.activeTool === id) this.finishTool(run, tool);
|
|
166
172
|
}
|
|
167
173
|
|
|
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
174
|
onAssistantDelta(snapshotText: string, snapshotThinking: string): void {
|
|
180
175
|
const run = this.run;
|
|
181
|
-
if (!run
|
|
176
|
+
if (!run) return;
|
|
177
|
+
|
|
178
|
+
const full = [run.finalizedText, snapshotText].filter((part) => part.trim()).join("\n\n");
|
|
179
|
+
if (full.trim()) run.currentText = full;
|
|
180
|
+
|
|
181
|
+
if (run.writer.dead) return;
|
|
182
182
|
|
|
183
183
|
if (snapshotThinking.length > run.thinkingSentLen) {
|
|
184
|
-
const
|
|
184
|
+
const thinkingDelta = snapshotThinking.slice(run.thinkingSentLen);
|
|
185
185
|
run.thinkingSentLen = snapshotThinking.length;
|
|
186
186
|
if (this.deps.getThinkingRelay()) {
|
|
187
187
|
if (this.hasPendingTools(run)) {
|
|
188
|
-
run.deferredThinking +=
|
|
188
|
+
run.deferredThinking += thinkingDelta;
|
|
189
189
|
} 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);
|
|
190
|
+
run.writer.pushThinking(thinkingDelta);
|
|
194
191
|
}
|
|
195
192
|
}
|
|
196
193
|
}
|
|
197
194
|
|
|
198
|
-
const full = [run.finalizedText, snapshotText].filter((part) => part.trim()).join("\n\n");
|
|
199
195
|
const delta = full.slice(run.resultSentLen);
|
|
200
196
|
if (!delta) return;
|
|
201
197
|
run.resultSentLen = full.length;
|
|
@@ -203,7 +199,7 @@ export class RunFeedback {
|
|
|
203
199
|
run.deferredText += delta;
|
|
204
200
|
return;
|
|
205
201
|
}
|
|
206
|
-
|
|
202
|
+
run.writer.pushText(delta);
|
|
207
203
|
}
|
|
208
204
|
|
|
209
205
|
async onAssistantFinal(text: string, isError: boolean): Promise<boolean> {
|
|
@@ -211,17 +207,17 @@ export class RunFeedback {
|
|
|
211
207
|
if (!run) return false;
|
|
212
208
|
|
|
213
209
|
run.finalizedText = [run.finalizedText, text].filter((part) => part.trim()).join("\n\n");
|
|
214
|
-
run.
|
|
210
|
+
if (text.trim()) run.currentText = text;
|
|
215
211
|
const delta = run.finalizedText.slice(run.resultSentLen);
|
|
216
212
|
run.resultSentLen = run.finalizedText.length;
|
|
217
213
|
if (!isError && text && delta) {
|
|
218
214
|
if (this.hasPendingTools(run)) {
|
|
219
215
|
run.deferredText += delta;
|
|
220
216
|
} else {
|
|
221
|
-
|
|
217
|
+
run.writer.pushText(delta);
|
|
222
218
|
}
|
|
223
219
|
}
|
|
224
|
-
if (!isError && text) run.
|
|
220
|
+
if (!isError && text) run.writer.closeBlock();
|
|
225
221
|
|
|
226
222
|
run.thinkingSentLen = 0;
|
|
227
223
|
if (isError) {
|
|
@@ -229,12 +225,11 @@ export class RunFeedback {
|
|
|
229
225
|
this.recovery = { openid: run.openid, msgId: run.msgId ?? "", at: Date.now() };
|
|
230
226
|
}
|
|
231
227
|
|
|
232
|
-
const ok = await
|
|
228
|
+
const ok = await run.writer.flush();
|
|
233
229
|
run.finalizedText = "";
|
|
234
|
-
run.snapshotText = "";
|
|
235
230
|
run.resultSentLen = 0;
|
|
236
231
|
if (isError) return false;
|
|
237
|
-
return ok && run.
|
|
232
|
+
return ok && run.writer.hasPushed;
|
|
238
233
|
}
|
|
239
234
|
|
|
240
235
|
async onAgentEnd(): Promise<void> {
|
|
@@ -243,86 +238,41 @@ export class RunFeedback {
|
|
|
243
238
|
this.finishEnd();
|
|
244
239
|
return;
|
|
245
240
|
}
|
|
246
|
-
await
|
|
241
|
+
await run.writer.whenIdle();
|
|
247
242
|
if (!this.hasPendingTools(run)) this.flushDeferred(run);
|
|
248
|
-
await
|
|
243
|
+
await run.writer.whenIdle();
|
|
249
244
|
this.clearTimers();
|
|
250
|
-
if (
|
|
245
|
+
if (run.currentText.trim()) this.lastCompletedText = run.currentText;
|
|
246
|
+
if (!run.hadError && run.writer.hasPushed) this.recovery = null;
|
|
251
247
|
this.run = null;
|
|
252
248
|
log.debug("run", "ended", {
|
|
253
249
|
msgId: run.msgId?.slice(0, 8) ?? "?",
|
|
254
250
|
elapsedMs: Date.now() - run.startedAt,
|
|
255
251
|
});
|
|
256
|
-
if (!run.
|
|
257
|
-
run.
|
|
252
|
+
if (!run.writer.hasPushed) {
|
|
253
|
+
run.writer.dispose();
|
|
258
254
|
this.finishEnd();
|
|
259
255
|
return;
|
|
260
256
|
}
|
|
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();
|
|
257
|
+
const ok = await run.writer.flush();
|
|
258
|
+
if (ok) await run.writer.done();
|
|
259
|
+
run.writer.dispose();
|
|
273
260
|
this.finishEnd();
|
|
274
261
|
}
|
|
275
262
|
|
|
276
263
|
dispose(): void {
|
|
277
264
|
this.clearTimers();
|
|
278
|
-
this.run?.
|
|
265
|
+
this.run?.writer.dispose();
|
|
279
266
|
this.run = null;
|
|
280
267
|
this.pending = null;
|
|
281
268
|
this.recovery = null;
|
|
269
|
+
this.lastCompletedText = undefined;
|
|
282
270
|
this.finishEnd();
|
|
283
271
|
}
|
|
284
272
|
|
|
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
|
-
}
|
|
273
|
+
private finishEnd(): void {
|
|
274
|
+
this.endResolve?.();
|
|
275
|
+
this.endResolve = undefined;
|
|
326
276
|
}
|
|
327
277
|
|
|
328
278
|
private hasPendingTools(run: RunState): boolean {
|
|
@@ -330,48 +280,35 @@ export class RunFeedback {
|
|
|
330
280
|
}
|
|
331
281
|
|
|
332
282
|
private flushDeferred(run: RunState): void {
|
|
333
|
-
if (run.
|
|
283
|
+
if (run.writer.dead) return;
|
|
334
284
|
if (run.deferredThinking) {
|
|
335
285
|
const thinking = run.deferredThinking;
|
|
336
286
|
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);
|
|
287
|
+
run.writer.pushThinking(thinking);
|
|
341
288
|
}
|
|
342
289
|
if (run.deferredText) {
|
|
343
290
|
const text = run.deferredText;
|
|
344
291
|
run.deferredText = "";
|
|
345
|
-
|
|
292
|
+
run.writer.pushText(text);
|
|
346
293
|
}
|
|
347
294
|
}
|
|
348
295
|
|
|
349
296
|
private startNextTool(run: RunState): void {
|
|
350
|
-
if (run.
|
|
297
|
+
if (run.writer.dead || run.activeTool) return;
|
|
351
298
|
const tool = run.tools.find((candidate) => !candidate.started);
|
|
352
299
|
if (!tool) return;
|
|
353
300
|
tool.started = true;
|
|
354
301
|
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
|
-
);
|
|
302
|
+
run.writer.pushTool(tool.name, tool.args);
|
|
365
303
|
if (tool.result) this.finishTool(run, tool);
|
|
366
304
|
}
|
|
367
305
|
|
|
368
306
|
private finishTool(run: RunState, tool: ToolCall): void {
|
|
369
307
|
if (!tool.result || tool.finished) return;
|
|
370
308
|
tool.finished = true;
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
if (this.run !== run || run.streamDead) return;
|
|
309
|
+
run.writer.pushToolResult(tool.result.result, tool.result.isError);
|
|
310
|
+
run.writer.schedule(() => {
|
|
311
|
+
if (this.run !== run) return;
|
|
375
312
|
if (run.activeTool !== tool.id) return;
|
|
376
313
|
run.activeTool = undefined;
|
|
377
314
|
this.startNextTool(run);
|
|
@@ -379,194 +316,6 @@ export class RunFeedback {
|
|
|
379
316
|
});
|
|
380
317
|
}
|
|
381
318
|
|
|
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
319
|
private clearTimers(): void {
|
|
571
320
|
const run = this.run;
|
|
572
321
|
if (!run) return;
|