@chen_hu_hu/pi-qq 1.6.1 → 1.7.0
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 +27 -1
- package/README-zh.md +6 -7
- package/README.md +19 -20
- package/doc/archive/delivery.1.6.2.md +196 -0
- package/doc/archive/delivery.1.7.0.md +96 -0
- package/index.ts +0 -16
- package/package.json +1 -1
- package/src/attachments/attachment-download.ts +2 -1
- package/src/attachments/file-attachments.ts +4 -5
- package/src/infra/file-logger.ts +6 -6
- package/src/infra/i18n.ts +4 -196
- package/src/infra/log-message-keys.ts +66 -0
- package/src/infra/power-status.ts +1 -11
- package/src/infra/reply-type.ts +1 -1
- package/src/qq/inbound-attachments.ts +2 -2
- package/src/qq/inbound-command.ts +8 -7
- package/src/qq/media-tool.ts +43 -61
- package/src/qq/msg-seq-send.ts +11 -0
- package/src/qq/qq-bot.ts +1 -1
- package/src/qq/qq-diagnostics.ts +3 -2
- package/src/qq/qq-message-handler.ts +5 -6
- package/src/qq/reply-sender.ts +6 -2
- package/src/qq/status-report.ts +1 -13
- package/src/quota/reply-quota.ts +2 -2
- package/src/relay/bubble-writer.ts +84 -38
- package/src/relay/msg-seq.ts +15 -0
- package/src/relay/reply-policy.ts +0 -13
- package/src/relay/run-stream-relay.ts +129 -202
- package/src/relay/run-timeline.ts +127 -0
- package/src/relay/stream-block-format.ts +1 -1
- package/src/relay/stream-session.ts +28 -10
|
@@ -1,27 +1,16 @@
|
|
|
1
1
|
import type { QQBot } from "@tencent-connect/qqbot-nodejs";
|
|
2
|
-
import { BubbleWriter } from "./bubble-writer";
|
|
2
|
+
import { BubbleWriter, type RepushBlock, type RepushTool } from "./bubble-writer";
|
|
3
|
+
import { RunTimeline, type TimelineTool } from "./run-timeline";
|
|
3
4
|
import { policyFor, type ReplyPolicy } from "./reply-policy";
|
|
4
5
|
import type { ReplyBudget } from "../quota/reply-quota";
|
|
5
6
|
import type { ReplyType } from "../infra/reply-type";
|
|
6
7
|
import { isLogEnabled, log } from "../infra/file-logger";
|
|
7
|
-
import { detectSkillRead } from "./stream-block-format";
|
|
8
8
|
|
|
9
9
|
export interface RunFeedbackDeps {
|
|
10
10
|
bot: () => QQBot | undefined;
|
|
11
11
|
budget: ReplyBudget;
|
|
12
12
|
getReplyType: () => ReplyType;
|
|
13
13
|
onWarn: (msg: string) => void;
|
|
14
|
-
onFallbackReply?: (text: string) => void;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
interface ToolCall {
|
|
18
|
-
id: string;
|
|
19
|
-
name: string;
|
|
20
|
-
args?: unknown;
|
|
21
|
-
result?: { isError: boolean; result?: unknown };
|
|
22
|
-
skillName?: string;
|
|
23
|
-
started: boolean;
|
|
24
|
-
finished: boolean;
|
|
25
14
|
}
|
|
26
15
|
|
|
27
16
|
interface RunState {
|
|
@@ -30,27 +19,36 @@ interface RunState {
|
|
|
30
19
|
msgId?: string;
|
|
31
20
|
thinkingSentLen: number;
|
|
32
21
|
finalizedText: string;
|
|
33
|
-
currentText: string;
|
|
34
22
|
resultSentLen: number;
|
|
35
|
-
tools: ToolCall[];
|
|
36
|
-
activeTool?: string;
|
|
37
23
|
deferredThinking: string;
|
|
38
24
|
deferredText: string;
|
|
39
|
-
|
|
40
|
-
lastText: string;
|
|
25
|
+
timeline: RunTimeline;
|
|
41
26
|
writer: BubbleWriter;
|
|
42
27
|
typingTimer?: ReturnType<typeof setInterval>;
|
|
43
28
|
}
|
|
44
29
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
30
|
+
export type RepushResult =
|
|
31
|
+
| {
|
|
32
|
+
ok: true;
|
|
33
|
+
thinkingLength: number;
|
|
34
|
+
toolCount: number;
|
|
35
|
+
textLength: number;
|
|
36
|
+
}
|
|
37
|
+
| { ok: false; reason: "no-run" | "empty" };
|
|
49
38
|
|
|
50
39
|
const TYPING_INTERVAL_MS = 50_000;
|
|
51
40
|
const PENDING_TTL_MS = 15_000;
|
|
52
41
|
const RECOVERY_TTL_MS = 60_000;
|
|
53
42
|
|
|
43
|
+
function toRepushTool(tool: TimelineTool): RepushTool {
|
|
44
|
+
return {
|
|
45
|
+
name: tool.name,
|
|
46
|
+
args: tool.args,
|
|
47
|
+
...(tool.skillName ? { skillName: tool.skillName } : {}),
|
|
48
|
+
...(tool.result ? { result: tool.result } : {}),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
54
52
|
export class RunFeedback {
|
|
55
53
|
private readonly deps: RunFeedbackDeps;
|
|
56
54
|
private pending: { openid: string; msgId: string; at: number } | null = null;
|
|
@@ -85,28 +83,64 @@ export class RunFeedback {
|
|
|
85
83
|
return this.lastCompletedText;
|
|
86
84
|
}
|
|
87
85
|
|
|
88
|
-
repushCurrent(openid: string, msgId: string):
|
|
86
|
+
repushCurrent(openid: string, msgId: string): RepushResult {
|
|
89
87
|
const run = this.run;
|
|
90
|
-
|
|
91
|
-
if (!run || !text?.trim()) {
|
|
88
|
+
if (!run) {
|
|
92
89
|
log.debug("run", "stream timing", {
|
|
93
90
|
phase: "last repush ignored",
|
|
94
|
-
|
|
95
|
-
textLength: text?.length ?? 0,
|
|
96
|
-
...(run ? this.diagnostics(run) : {}),
|
|
91
|
+
reason: "no-run",
|
|
97
92
|
});
|
|
98
|
-
return false;
|
|
93
|
+
return { ok: false, reason: "no-run" };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const snapshot = run.timeline.snapshot();
|
|
97
|
+
const replyType = this.deps.getReplyType();
|
|
98
|
+
const blocks: RepushBlock[] = [];
|
|
99
|
+
if (replyType === "full") {
|
|
100
|
+
if (snapshot.thinking.trim()) {
|
|
101
|
+
blocks.push({ kind: "thinking", text: snapshot.thinking });
|
|
102
|
+
}
|
|
103
|
+
for (const tool of snapshot.tools) {
|
|
104
|
+
blocks.push({ kind: "tool", tool: toRepushTool(tool) });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (snapshot.text.trim()) {
|
|
108
|
+
blocks.push({ kind: "text", text: snapshot.text });
|
|
99
109
|
}
|
|
110
|
+
|
|
111
|
+
const thinkingLength = snapshot.thinking.length;
|
|
112
|
+
const toolCount = blocks.filter((block) => block.kind === "tool").length;
|
|
113
|
+
const textLength = snapshot.text.length;
|
|
114
|
+
if (blocks.length === 0) {
|
|
115
|
+
log.debug("run", "stream timing", {
|
|
116
|
+
phase: "last repush ignored",
|
|
117
|
+
reason: "empty",
|
|
118
|
+
replyType,
|
|
119
|
+
textLength,
|
|
120
|
+
thinkingLength,
|
|
121
|
+
toolCount,
|
|
122
|
+
writerDead: run.writer.dead,
|
|
123
|
+
...this.diagnostics(run),
|
|
124
|
+
});
|
|
125
|
+
return { ok: false, reason: "empty" };
|
|
126
|
+
}
|
|
127
|
+
|
|
100
128
|
log.debug("run", "stream timing", {
|
|
101
129
|
phase: "last repush",
|
|
102
130
|
msgId: msgId.slice(0, 8),
|
|
103
|
-
|
|
131
|
+
replyType,
|
|
132
|
+
textLength,
|
|
133
|
+
thinkingLength,
|
|
134
|
+
toolCount,
|
|
135
|
+
writerDead: run.writer.dead,
|
|
104
136
|
...this.diagnostics(run),
|
|
105
137
|
});
|
|
106
138
|
run.openid = openid;
|
|
107
139
|
run.msgId = msgId;
|
|
108
|
-
run.writer.
|
|
109
|
-
|
|
140
|
+
run.writer.restartSnapshot(blocks, { openid, msgId });
|
|
141
|
+
run.deferredThinking = "";
|
|
142
|
+
run.deferredText = "";
|
|
143
|
+
return { ok: true, thinkingLength, toolCount, textLength };
|
|
110
144
|
}
|
|
111
145
|
|
|
112
146
|
onAgentStart(): void {
|
|
@@ -153,13 +187,10 @@ export class RunFeedback {
|
|
|
153
187
|
msgId: target.msgId,
|
|
154
188
|
thinkingSentLen: 0,
|
|
155
189
|
finalizedText: "",
|
|
156
|
-
currentText: "",
|
|
157
190
|
resultSentLen: 0,
|
|
158
|
-
tools: [],
|
|
159
191
|
deferredThinking: "",
|
|
160
192
|
deferredText: "",
|
|
161
|
-
|
|
162
|
-
lastText: "",
|
|
193
|
+
timeline: new RunTimeline(),
|
|
163
194
|
writer,
|
|
164
195
|
};
|
|
165
196
|
this.run = run;
|
|
@@ -187,33 +218,16 @@ export class RunFeedback {
|
|
|
187
218
|
log.debug("run", "tool start", { phase: "ignored: no active run", toolName, toolCallId: id });
|
|
188
219
|
return;
|
|
189
220
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
phase: "ignored: stream dead",
|
|
193
|
-
toolName,
|
|
194
|
-
toolCallId: id,
|
|
195
|
-
...this.diagnostics(run),
|
|
196
|
-
});
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
if (run.tools.some((tool) => tool.id === id)) return;
|
|
200
|
-
const skill = detectSkillRead(toolName, args);
|
|
201
|
-
run.tools.push({
|
|
202
|
-
id,
|
|
203
|
-
name: toolName,
|
|
204
|
-
args,
|
|
205
|
-
...(skill ? { skillName: skill.name } : {}),
|
|
206
|
-
started: false,
|
|
207
|
-
finished: false,
|
|
208
|
-
});
|
|
221
|
+
const tool = run.timeline.addTool(toolName, args, id);
|
|
222
|
+
if (!tool) return;
|
|
209
223
|
log.debug("run", "tool start", {
|
|
210
224
|
phase: "queued",
|
|
211
225
|
toolName,
|
|
212
226
|
toolCallId: id,
|
|
213
|
-
skillName:
|
|
227
|
+
skillName: tool.skillName ?? null,
|
|
214
228
|
...this.diagnostics(run),
|
|
215
229
|
});
|
|
216
|
-
this.
|
|
230
|
+
this.advanceTools(run);
|
|
217
231
|
}
|
|
218
232
|
|
|
219
233
|
onToolEnd(toolName: string, isError: boolean, result?: unknown, toolCallId?: string): void {
|
|
@@ -223,22 +237,11 @@ export class RunFeedback {
|
|
|
223
237
|
log.debug("run", "tool end", { phase: "ignored: no active run", toolName, toolCallId: id });
|
|
224
238
|
return;
|
|
225
239
|
}
|
|
226
|
-
const tool = run.
|
|
240
|
+
const tool = run.timeline.completeTool(id, isError, result);
|
|
227
241
|
if (!tool) {
|
|
228
242
|
log.debug("run", "tool end", { phase: "ignored: unknown tool", toolName, toolCallId: id });
|
|
229
243
|
return;
|
|
230
244
|
}
|
|
231
|
-
if (run.writer.dead) {
|
|
232
|
-
log.debug("run", "tool end", {
|
|
233
|
-
phase: "ignored: stream dead",
|
|
234
|
-
toolName,
|
|
235
|
-
toolCallId: id,
|
|
236
|
-
isError,
|
|
237
|
-
...this.diagnostics(run),
|
|
238
|
-
});
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
|
-
tool.result = { isError, result };
|
|
242
245
|
log.debug("run", "tool end", {
|
|
243
246
|
phase: "result recorded",
|
|
244
247
|
toolName,
|
|
@@ -246,7 +249,7 @@ export class RunFeedback {
|
|
|
246
249
|
isError,
|
|
247
250
|
...this.diagnostics(run),
|
|
248
251
|
});
|
|
249
|
-
|
|
252
|
+
this.advanceTools(run);
|
|
250
253
|
}
|
|
251
254
|
|
|
252
255
|
onAssistantDelta(snapshotText: string, snapshotThinking: string): void {
|
|
@@ -254,24 +257,13 @@ export class RunFeedback {
|
|
|
254
257
|
if (!run) return;
|
|
255
258
|
|
|
256
259
|
const policy = this.policy();
|
|
257
|
-
|
|
258
|
-
if (!policy.relayTextLive) {
|
|
259
|
-
if (snapshotText.trim()) {
|
|
260
|
-
run.currentText = snapshotText;
|
|
261
|
-
run.lastText = snapshotText;
|
|
262
|
-
}
|
|
263
|
-
return;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
260
|
const full = [run.finalizedText, snapshotText].filter((part) => part.trim()).join("\n\n");
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
if (run.writer.dead) return;
|
|
261
|
+
run.timeline.updateAssistant(full, snapshotThinking);
|
|
270
262
|
|
|
271
263
|
if (snapshotThinking.length > run.thinkingSentLen) {
|
|
272
264
|
const thinkingDelta = snapshotThinking.slice(run.thinkingSentLen);
|
|
273
265
|
run.thinkingSentLen = snapshotThinking.length;
|
|
274
|
-
if (policy.relayThinking) {
|
|
266
|
+
if (!run.writer.dead && policy.relayThinking) {
|
|
275
267
|
if (this.hasPendingTools(run)) {
|
|
276
268
|
run.deferredThinking += thinkingDelta;
|
|
277
269
|
} else {
|
|
@@ -283,11 +275,7 @@ export class RunFeedback {
|
|
|
283
275
|
const delta = full.slice(run.resultSentLen);
|
|
284
276
|
if (!delta) return;
|
|
285
277
|
run.resultSentLen = full.length;
|
|
286
|
-
|
|
287
|
-
run.deferredText += delta;
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
run.writer.pushText(delta);
|
|
278
|
+
this.emitTextDelta(run, delta, policy);
|
|
291
279
|
}
|
|
292
280
|
|
|
293
281
|
async onAssistantFinal(text: string, isError: boolean): Promise<boolean> {
|
|
@@ -302,41 +290,15 @@ export class RunFeedback {
|
|
|
302
290
|
}
|
|
303
291
|
|
|
304
292
|
const policy = this.policy();
|
|
305
|
-
|
|
306
|
-
if (!policy.relayTextLive) {
|
|
307
|
-
run.finalizedText = "";
|
|
308
|
-
run.resultSentLen = 0;
|
|
309
|
-
run.thinkingSentLen = 0;
|
|
310
|
-
if (isError) {
|
|
311
|
-
run.hadError = true;
|
|
312
|
-
this.recovery = { openid: run.openid, msgId: run.msgId ?? "", at: Date.now() };
|
|
313
|
-
log.debug("run", "last buffered", { textLength: 0, isError: true });
|
|
314
|
-
return false;
|
|
315
|
-
}
|
|
316
|
-
if (text.trim()) {
|
|
317
|
-
run.lastText = text;
|
|
318
|
-
run.currentText = text;
|
|
319
|
-
}
|
|
320
|
-
log.debug("run", "last buffered", { textLength: run.lastText.length, isError: false });
|
|
321
|
-
return true;
|
|
322
|
-
}
|
|
323
|
-
|
|
293
|
+
run.timeline.finishAssistant(text, isError);
|
|
324
294
|
run.finalizedText = [run.finalizedText, text].filter((part) => part.trim()).join("\n\n");
|
|
325
|
-
if (text.trim()) run.currentText = text;
|
|
326
295
|
const delta = run.finalizedText.slice(run.resultSentLen);
|
|
327
296
|
run.resultSentLen = run.finalizedText.length;
|
|
328
|
-
if (!isError && text
|
|
329
|
-
if (policy.deferTextWhileToolsPending && this.hasPendingTools(run)) {
|
|
330
|
-
run.deferredText += delta;
|
|
331
|
-
} else {
|
|
332
|
-
run.writer.pushText(delta);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
297
|
+
if (!isError && text) this.emitTextDelta(run, delta, policy);
|
|
335
298
|
if (!isError && text) run.writer.closeBlock();
|
|
336
299
|
|
|
337
300
|
run.thinkingSentLen = 0;
|
|
338
301
|
if (isError) {
|
|
339
|
-
run.hadError = true;
|
|
340
302
|
this.recovery = { openid: run.openid, msgId: run.msgId ?? "", at: Date.now() };
|
|
341
303
|
}
|
|
342
304
|
|
|
@@ -368,14 +330,13 @@ export class RunFeedback {
|
|
|
368
330
|
this.finishEnd();
|
|
369
331
|
return;
|
|
370
332
|
}
|
|
371
|
-
const lastDelivery = this.planLastDelivery(run);
|
|
372
333
|
await run.writer.whenIdle();
|
|
373
334
|
const hasPendingTools = this.hasPendingTools(run);
|
|
374
335
|
if (!hasPendingTools) this.flushDeferred(run);
|
|
375
336
|
await run.writer.whenIdle();
|
|
376
337
|
this.clearTimers();
|
|
377
|
-
if (run.currentText.trim()) this.lastCompletedText = run.currentText;
|
|
378
|
-
if (!run.
|
|
338
|
+
if (run.timeline.currentText.trim()) this.lastCompletedText = run.timeline.currentText;
|
|
339
|
+
if (!run.timeline.error && run.writer.hasPushed) this.recovery = null;
|
|
379
340
|
log.debug("run", "run ended", {
|
|
380
341
|
phase: "cleanup",
|
|
381
342
|
msgId: run.msgId?.slice(0, 8) ?? "?",
|
|
@@ -390,16 +351,11 @@ export class RunFeedback {
|
|
|
390
351
|
msgId: run.msgId?.slice(0, 8) ?? "?",
|
|
391
352
|
elapsedMs: Date.now() - run.startedAt,
|
|
392
353
|
});
|
|
393
|
-
let delivered = false;
|
|
394
354
|
if (run.writer.hasPushed) {
|
|
395
355
|
const ok = await run.writer.flush();
|
|
396
|
-
if (ok)
|
|
397
|
-
await run.writer.done();
|
|
398
|
-
delivered = true;
|
|
399
|
-
}
|
|
356
|
+
if (ok) await run.writer.done();
|
|
400
357
|
}
|
|
401
358
|
run.writer.dispose();
|
|
402
|
-
this.settleLastDelivery(lastDelivery, delivered);
|
|
403
359
|
if (this.run === run) this.run = null;
|
|
404
360
|
this.finishEnd();
|
|
405
361
|
}
|
|
@@ -427,31 +383,17 @@ export class RunFeedback {
|
|
|
427
383
|
return policyFor(this.deps.getReplyType());
|
|
428
384
|
}
|
|
429
385
|
|
|
430
|
-
private
|
|
431
|
-
|
|
432
|
-
const text = run.lastText.trim() ? run.lastText : "";
|
|
433
|
-
if (!text) return undefined;
|
|
434
|
-
const fresh = this.deps.budget.snapshot().fresh;
|
|
435
|
-
if (fresh) {
|
|
436
|
-
run.writer.closeBlock();
|
|
437
|
-
run.writer.pushText(text);
|
|
438
|
-
}
|
|
439
|
-
return { text, streamed: fresh };
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
private settleLastDelivery(delivery: LastDelivery | undefined, delivered: boolean): void {
|
|
443
|
-
if (!delivery) return;
|
|
444
|
-
const ok = delivered && delivery.streamed;
|
|
445
|
-
log.debug("run", "last delivered", {
|
|
446
|
-
delivered: ok,
|
|
447
|
-
fallback: !ok,
|
|
448
|
-
textLength: delivery.text.length,
|
|
449
|
-
});
|
|
450
|
-
if (!ok) this.deps.onFallbackReply?.(delivery.text);
|
|
386
|
+
private hasPendingTools(run: RunState): boolean {
|
|
387
|
+
return run.timeline.hasPendingTools();
|
|
451
388
|
}
|
|
452
389
|
|
|
453
|
-
private
|
|
454
|
-
|
|
390
|
+
private emitTextDelta(run: RunState, delta: string, policy: ReplyPolicy): void {
|
|
391
|
+
if (!delta || run.writer.dead) return;
|
|
392
|
+
if (policy.deferTextWhileToolsPending && this.hasPendingTools(run)) {
|
|
393
|
+
run.deferredText += delta;
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
run.writer.pushText(delta);
|
|
455
397
|
}
|
|
456
398
|
|
|
457
399
|
private flushDeferred(run: RunState): void {
|
|
@@ -468,49 +410,46 @@ export class RunFeedback {
|
|
|
468
410
|
}
|
|
469
411
|
}
|
|
470
412
|
|
|
471
|
-
private
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
413
|
+
private advanceTools(run: RunState): void {
|
|
414
|
+
const relayTools = this.policy().relayTools;
|
|
415
|
+
for (;;) {
|
|
416
|
+
const active = run.timeline.active;
|
|
417
|
+
if (active) {
|
|
418
|
+
if (!active.result) return;
|
|
419
|
+
if (!run.writer.dead && relayTools && !active.skillName) {
|
|
420
|
+
run.writer.pushToolResult(active.result.result, active.result.isError, active.name, active.args);
|
|
421
|
+
}
|
|
422
|
+
const finished = run.timeline.finishActiveTool();
|
|
423
|
+
if (finished) {
|
|
424
|
+
log.debug("run", "tool end", {
|
|
425
|
+
phase: "output finished",
|
|
426
|
+
toolName: finished.name,
|
|
427
|
+
toolCallId: finished.id,
|
|
428
|
+
isError: finished.result?.isError ?? false,
|
|
429
|
+
...this.diagnostics(run),
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
if (!this.hasPendingTools(run)) this.flushDeferred(run);
|
|
433
|
+
continue;
|
|
489
434
|
}
|
|
435
|
+
const tool = run.timeline.startNextTool();
|
|
436
|
+
if (!tool) return;
|
|
437
|
+
log.debug("run", "tool start", {
|
|
438
|
+
phase: "output started",
|
|
439
|
+
toolName: tool.name,
|
|
440
|
+
toolCallId: tool.id,
|
|
441
|
+
skillName: tool.skillName ?? null,
|
|
442
|
+
...this.diagnostics(run),
|
|
443
|
+
});
|
|
444
|
+
if (!run.writer.dead && relayTools) {
|
|
445
|
+
if (tool.skillName) {
|
|
446
|
+
run.writer.pushSkill(tool.skillName);
|
|
447
|
+
} else {
|
|
448
|
+
run.writer.pushTool(tool.name, tool.args);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
if (!tool.result) return;
|
|
490
452
|
}
|
|
491
|
-
if (tool.result) this.finishTool(run, tool);
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
private finishTool(run: RunState, tool: ToolCall): void {
|
|
495
|
-
if (!tool.result || tool.finished) return;
|
|
496
|
-
tool.finished = true;
|
|
497
|
-
log.debug("run", "tool end", {
|
|
498
|
-
phase: "output finished",
|
|
499
|
-
toolName: tool.name,
|
|
500
|
-
toolCallId: tool.id,
|
|
501
|
-
isError: tool.result.isError,
|
|
502
|
-
...this.diagnostics(run),
|
|
503
|
-
});
|
|
504
|
-
if (this.policy().relayTools && !tool.skillName) {
|
|
505
|
-
run.writer.pushToolResult(tool.result.result, tool.result.isError, tool.name, tool.args);
|
|
506
|
-
}
|
|
507
|
-
run.writer.schedule(() => {
|
|
508
|
-
if (this.run !== run) return;
|
|
509
|
-
if (run.activeTool !== tool.id) return;
|
|
510
|
-
run.activeTool = undefined;
|
|
511
|
-
this.startNextTool(run);
|
|
512
|
-
if (!this.hasPendingTools(run)) this.flushDeferred(run);
|
|
513
|
-
});
|
|
514
453
|
}
|
|
515
454
|
|
|
516
455
|
private clearTimers(): void {
|
|
@@ -524,24 +463,12 @@ export class RunFeedback {
|
|
|
524
463
|
if (!isLogEnabled("debug")) return {};
|
|
525
464
|
return {
|
|
526
465
|
runElapsedMs: Date.now() - run.startedAt,
|
|
527
|
-
|
|
528
|
-
toolCount: run.tools.length,
|
|
529
|
-
pendingToolCount: run.tools.filter((tool) => !tool.finished).length,
|
|
530
|
-
tools: run.tools.map((tool) => ({
|
|
531
|
-
id: tool.id,
|
|
532
|
-
name: tool.name,
|
|
533
|
-
started: tool.started,
|
|
534
|
-
finished: tool.finished,
|
|
535
|
-
hasResult: Boolean(tool.result),
|
|
536
|
-
isError: tool.result?.isError ?? null,
|
|
537
|
-
})),
|
|
466
|
+
...run.timeline.diagnostics(),
|
|
538
467
|
deferredTextLen: run.deferredText.length,
|
|
539
468
|
deferredThinkingLen: run.deferredThinking.length,
|
|
540
|
-
currentTextLen: run.currentText.length,
|
|
541
469
|
finalizedTextLen: run.finalizedText.length,
|
|
542
470
|
resultSentLen: run.resultSentLen,
|
|
543
471
|
thinkingSentLen: run.thinkingSentLen,
|
|
544
|
-
hadError: run.hadError,
|
|
545
472
|
writer: run.writer.stats(),
|
|
546
473
|
budget: this.deps.budget.snapshot(),
|
|
547
474
|
};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { detectSkillRead } from "./stream-block-format";
|
|
2
|
+
|
|
3
|
+
export interface TimelineTool {
|
|
4
|
+
id: string;
|
|
5
|
+
name: string;
|
|
6
|
+
args?: unknown;
|
|
7
|
+
result?: { isError: boolean; result?: unknown };
|
|
8
|
+
skillName?: string;
|
|
9
|
+
startedAt: number;
|
|
10
|
+
finishedAt?: number;
|
|
11
|
+
started: boolean;
|
|
12
|
+
finished: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface RunSnapshot {
|
|
16
|
+
thinking: string;
|
|
17
|
+
text: string;
|
|
18
|
+
tools: TimelineTool[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const MAX_SNAPSHOT_TOOLS = 5;
|
|
22
|
+
const MAX_SNAPSHOT_FINISHED_TOOLS = 3;
|
|
23
|
+
|
|
24
|
+
export class RunTimeline {
|
|
25
|
+
private tools: TimelineTool[] = [];
|
|
26
|
+
private activeToolId?: string;
|
|
27
|
+
private thinking = "";
|
|
28
|
+
private text = "";
|
|
29
|
+
private hadError = false;
|
|
30
|
+
|
|
31
|
+
updateAssistant(text: string, thinking: string): void {
|
|
32
|
+
if (text.trim()) this.text = text;
|
|
33
|
+
this.thinking = thinking;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
finishAssistant(text: string, isError: boolean): void {
|
|
37
|
+
if (text.trim()) this.text = text;
|
|
38
|
+
if (isError) this.hadError = true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
addTool(toolName: string, args: unknown, toolCallId: string): TimelineTool | undefined {
|
|
42
|
+
if (this.tools.some((tool) => tool.id === toolCallId)) return undefined;
|
|
43
|
+
const skill = detectSkillRead(toolName, args);
|
|
44
|
+
const tool: TimelineTool = {
|
|
45
|
+
id: toolCallId,
|
|
46
|
+
name: toolName,
|
|
47
|
+
args,
|
|
48
|
+
...(skill ? { skillName: skill.name } : {}),
|
|
49
|
+
startedAt: Date.now(),
|
|
50
|
+
started: false,
|
|
51
|
+
finished: false,
|
|
52
|
+
};
|
|
53
|
+
this.tools.push(tool);
|
|
54
|
+
return tool;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
completeTool(toolCallId: string, isError: boolean, result: unknown): TimelineTool | undefined {
|
|
58
|
+
const tool = this.tools.find((candidate) => candidate.id === toolCallId);
|
|
59
|
+
if (!tool) return undefined;
|
|
60
|
+
tool.result = { isError, result };
|
|
61
|
+
tool.finishedAt = Date.now();
|
|
62
|
+
return tool;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
get active(): TimelineTool | undefined {
|
|
66
|
+
return this.activeToolId ? this.tools.find((tool) => tool.id === this.activeToolId) : undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
startNextTool(): TimelineTool | undefined {
|
|
70
|
+
if (this.activeToolId) return undefined;
|
|
71
|
+
const tool = this.tools.find((candidate) => !candidate.started);
|
|
72
|
+
if (!tool) return undefined;
|
|
73
|
+
tool.started = true;
|
|
74
|
+
this.activeToolId = tool.id;
|
|
75
|
+
return tool;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
finishActiveTool(): TimelineTool | undefined {
|
|
79
|
+
const tool = this.active;
|
|
80
|
+
if (!tool) return undefined;
|
|
81
|
+
tool.finished = true;
|
|
82
|
+
if (!tool.finishedAt) tool.finishedAt = Date.now();
|
|
83
|
+
this.activeToolId = undefined;
|
|
84
|
+
return tool;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
hasPendingTools(): boolean {
|
|
88
|
+
return Boolean(this.activeToolId) || this.tools.some((tool) => !tool.finished);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
get currentText(): string {
|
|
92
|
+
return this.text;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
get error(): boolean {
|
|
96
|
+
return this.hadError;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
snapshot(): RunSnapshot {
|
|
100
|
+
const finished = this.tools.filter((tool) => tool.finished).slice(-MAX_SNAPSHOT_FINISHED_TOOLS);
|
|
101
|
+
const active = this.active;
|
|
102
|
+
const tools = [...finished, ...(active ? [active] : [])]
|
|
103
|
+
.filter((tool, index, list) => list.findIndex((candidate) => candidate.id === tool.id) === index)
|
|
104
|
+
.sort((a, b) => a.startedAt - b.startedAt)
|
|
105
|
+
.slice(-MAX_SNAPSHOT_TOOLS);
|
|
106
|
+
return { thinking: this.thinking, text: this.text, tools };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
diagnostics(): Record<string, unknown> {
|
|
110
|
+
return {
|
|
111
|
+
activeTool: this.activeToolId ?? null,
|
|
112
|
+
toolCount: this.tools.length,
|
|
113
|
+
pendingToolCount: this.tools.filter((tool) => !tool.finished).length,
|
|
114
|
+
tools: this.tools.map((tool) => ({
|
|
115
|
+
id: tool.id,
|
|
116
|
+
name: tool.name,
|
|
117
|
+
started: tool.started,
|
|
118
|
+
finished: tool.finished,
|
|
119
|
+
hasResult: Boolean(tool.result),
|
|
120
|
+
isError: tool.result?.isError ?? null,
|
|
121
|
+
})),
|
|
122
|
+
currentTextLen: this.text.length,
|
|
123
|
+
currentThinkingLen: this.thinking.length,
|
|
124
|
+
hadError: this.hadError,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
}
|