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