@chen_hu_hu/pi-qq 1.3.6 → 1.4.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 +13 -1
- package/README-zh.md +1 -1
- package/README.md +1 -1
- package/doc/archive/README.md +14 -0
- package/doc/archive/delivery-v4-zh.md +327 -0
- package/doc/archive/delivery-v4.md +327 -0
- package/doc/todo.md +107 -2
- package/index.ts +64 -0
- package/package.json +1 -1
- package/src/qq/inbound-command.ts +7 -0
- package/src/qq/qq-diagnostics.ts +2 -2
- package/src/qq/reply-sender.ts +5 -0
- package/src/relay/bubble-writer.ts +110 -21
- package/src/relay/run-stream-relay.ts +166 -12
- package/src/relay/stream-block-format.ts +139 -22
- package/src/relay/stream-session.ts +27 -4
- package/doc/bugfix-v4.md +0 -698
- package/doc/enhance-status.md +0 -427
- package/doc/send-skill.md +0 -257
- package/doc/tool-return.md +0 -378
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import type { QQBot } from "@tencent-connect/qqbot-nodejs";
|
|
2
2
|
import { AppendStream, type AppendStreamCheckpoint } from "./stream-session";
|
|
3
3
|
import type { ReplyBudget } from "../quota/reply-quota";
|
|
4
|
-
import { log } from "../infra/file-logger";
|
|
5
|
-
import {
|
|
4
|
+
import { isLogEnabled, log } from "../infra/file-logger";
|
|
5
|
+
import {
|
|
6
|
+
formatSkillBlock,
|
|
7
|
+
formatToolLine,
|
|
8
|
+
formatToolResult,
|
|
9
|
+
resultLanguage,
|
|
10
|
+
thinkingHeader,
|
|
11
|
+
} from "./stream-block-format";
|
|
6
12
|
import { t } from "../infra/i18n";
|
|
7
13
|
|
|
8
14
|
type BlockKind = "none" | "thinking" | "tool" | "text";
|
|
@@ -31,6 +37,7 @@ export class BubbleWriter {
|
|
|
31
37
|
private resumeAttempts = 0;
|
|
32
38
|
private pushedAny = false;
|
|
33
39
|
private bubbleChars = 0;
|
|
40
|
+
private lastFrameAt = 0;
|
|
34
41
|
private fenceOpen = false;
|
|
35
42
|
private fenceLen = 0;
|
|
36
43
|
private fenceQuoted = false;
|
|
@@ -52,6 +59,33 @@ export class BubbleWriter {
|
|
|
52
59
|
return this.streamDead;
|
|
53
60
|
}
|
|
54
61
|
|
|
62
|
+
/** Snapshot used by debug logs to explain stalls without changing relay behavior. */
|
|
63
|
+
stats(now = Date.now()): {
|
|
64
|
+
dead: boolean;
|
|
65
|
+
disposed: boolean;
|
|
66
|
+
hasStream: boolean;
|
|
67
|
+
hasPendingResume: boolean;
|
|
68
|
+
resumeAttempts: number;
|
|
69
|
+
bubbleChars: number;
|
|
70
|
+
hasPushed: boolean;
|
|
71
|
+
lastKind: string;
|
|
72
|
+
toolOpen: boolean;
|
|
73
|
+
lastFrameAgeMs: number | null;
|
|
74
|
+
} {
|
|
75
|
+
return {
|
|
76
|
+
dead: this.streamDead,
|
|
77
|
+
disposed: this.disposed,
|
|
78
|
+
hasStream: Boolean(this.cur),
|
|
79
|
+
hasPendingResume: Boolean(this.pendingResume),
|
|
80
|
+
resumeAttempts: this.resumeAttempts,
|
|
81
|
+
bubbleChars: this.bubbleChars,
|
|
82
|
+
hasPushed: this.pushedAny,
|
|
83
|
+
lastKind: this.lastKind,
|
|
84
|
+
toolOpen: this.toolOpen,
|
|
85
|
+
lastFrameAgeMs: this.lastFrameAt ? now - this.lastFrameAt : null,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
55
89
|
/** Mark the current block as complete so the next content starts a new bubble. */
|
|
56
90
|
closeBlock(): void {
|
|
57
91
|
this.blockClosed = true;
|
|
@@ -77,13 +111,16 @@ export class BubbleWriter {
|
|
|
77
111
|
this.toolOpen = true;
|
|
78
112
|
const newBubble = this.lastKind !== "none";
|
|
79
113
|
const prefix = this.beginBlock("tool", newBubble);
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
114
|
+
if (isLogEnabled("debug")) {
|
|
115
|
+
log.debug("stream", "stream timing", {
|
|
116
|
+
phase: "tool queued",
|
|
117
|
+
msgId: this.logMsgId(),
|
|
118
|
+
tool: name,
|
|
119
|
+
at: Date.now(),
|
|
120
|
+
stats: this.stats(),
|
|
121
|
+
budget: this.deps.budget.snapshot(),
|
|
122
|
+
});
|
|
123
|
+
}
|
|
87
124
|
this.push(
|
|
88
125
|
`${prefix}${formatToolLine(name, args)}\n\n`,
|
|
89
126
|
prefix.length > 0 || newBubble,
|
|
@@ -102,10 +139,11 @@ export class BubbleWriter {
|
|
|
102
139
|
this.push(`${prefix}${formatSkillBlock(name)}`, prefix.length > 0 || newBubble, false, newBubble, false, "skill");
|
|
103
140
|
}
|
|
104
141
|
|
|
105
|
-
pushToolResult(result: unknown, isError: boolean): void {
|
|
142
|
+
pushToolResult(result: unknown, isError: boolean, toolName?: string, args?: unknown): void {
|
|
106
143
|
if (this.streamDead || this.disposed) return;
|
|
107
144
|
const prefix = this.beginBlock("tool");
|
|
108
|
-
|
|
145
|
+
const language = toolName ? resultLanguage(toolName, args, isError) : "text";
|
|
146
|
+
this.push(`${prefix}${formatToolResult(result, isError, language)}`, prefix.length > 0, true);
|
|
109
147
|
}
|
|
110
148
|
|
|
111
149
|
/** Run `op` after all currently queued appends, unless the stream died first. */
|
|
@@ -247,13 +285,14 @@ export class BubbleWriter {
|
|
|
247
285
|
const queuedAt = trace ? Date.now() : 0;
|
|
248
286
|
void this.enqueue(async () => {
|
|
249
287
|
if (this.streamDead || this.disposed) return;
|
|
250
|
-
if (trace) {
|
|
288
|
+
if (trace && isLogEnabled("debug")) {
|
|
289
|
+
const now = Date.now();
|
|
251
290
|
log.debug("stream", "stream timing", {
|
|
252
291
|
phase: "append start",
|
|
253
292
|
trace,
|
|
254
293
|
msgId: this.logMsgId(),
|
|
255
|
-
waitedMs:
|
|
256
|
-
|
|
294
|
+
waitedMs: now - queuedAt,
|
|
295
|
+
stats: this.stats(now),
|
|
257
296
|
});
|
|
258
297
|
}
|
|
259
298
|
if (blockBoundary && !(await this.closeOpenFence())) return;
|
|
@@ -280,16 +319,17 @@ export class BubbleWriter {
|
|
|
280
319
|
try {
|
|
281
320
|
await stream.append(outgoing);
|
|
282
321
|
this.bubbleChars += outgoing.length;
|
|
322
|
+
this.lastFrameAt = Date.now();
|
|
283
323
|
this.trackFences(outgoing);
|
|
284
324
|
this.lastChar = outgoing.slice(-1);
|
|
285
325
|
if (endTool) this.toolOpen = false;
|
|
286
|
-
if (trace) {
|
|
326
|
+
if (trace && isLogEnabled("debug")) {
|
|
287
327
|
log.debug("stream", "stream timing", {
|
|
288
328
|
phase: "append done",
|
|
289
329
|
trace,
|
|
290
330
|
msgId: this.logMsgId(),
|
|
291
331
|
totalMs: Date.now() - queuedAt,
|
|
292
|
-
|
|
332
|
+
stats: this.stats(),
|
|
293
333
|
});
|
|
294
334
|
}
|
|
295
335
|
} catch (err) {
|
|
@@ -343,10 +383,13 @@ export class BubbleWriter {
|
|
|
343
383
|
this.fenceQuoted = false;
|
|
344
384
|
this.lastChar = "";
|
|
345
385
|
this.thinkingAtLineStart = true;
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
386
|
+
if (isLogEnabled("debug")) {
|
|
387
|
+
log.debug("stream", "bubble split", {
|
|
388
|
+
msgId: this.logMsgId(),
|
|
389
|
+
elapsedMs: Date.now() - this.deps.startedAt,
|
|
390
|
+
stats: this.stats(),
|
|
391
|
+
});
|
|
392
|
+
}
|
|
350
393
|
}
|
|
351
394
|
|
|
352
395
|
private drainQueue(): Promise<void> {
|
|
@@ -380,10 +423,42 @@ export class BubbleWriter {
|
|
|
380
423
|
this.handleStreamError(err);
|
|
381
424
|
});
|
|
382
425
|
},
|
|
426
|
+
onFrameError: (err, info) => {
|
|
427
|
+
if (!isLogEnabled("debug")) return;
|
|
428
|
+
const e = err as { name?: string; message?: string; httpStatus?: number; bizCode?: number; path?: string };
|
|
429
|
+
log.debug("stream", "stream error", {
|
|
430
|
+
phase: "frame failed",
|
|
431
|
+
msgId: this.logMsgId(),
|
|
432
|
+
...info,
|
|
433
|
+
errorName: e?.name ?? null,
|
|
434
|
+
errorMessage: e?.message ?? String(err),
|
|
435
|
+
httpStatus: e?.httpStatus ?? null,
|
|
436
|
+
bizCode: e?.bizCode ?? null,
|
|
437
|
+
path: e?.path ?? null,
|
|
438
|
+
stats: this.stats(),
|
|
439
|
+
budget: this.deps.budget.snapshot(),
|
|
440
|
+
});
|
|
441
|
+
},
|
|
383
442
|
onDebug: (message) => log.debug("stream", message, { msgId: this.logMsgId() }),
|
|
384
443
|
});
|
|
385
444
|
this.cur = stream;
|
|
386
445
|
if (!resume) this.deps.budget.noteStreamBubble(this.deps.msgId);
|
|
446
|
+
if (isLogEnabled("debug")) {
|
|
447
|
+
log.debug("stream", "stream timing", {
|
|
448
|
+
phase: "stream open",
|
|
449
|
+
msgId: this.logMsgId(),
|
|
450
|
+
resumed: Boolean(resume),
|
|
451
|
+
resume: resume
|
|
452
|
+
? {
|
|
453
|
+
index: resume.index,
|
|
454
|
+
hasStreamMsgId: Boolean(resume.streamMsgId),
|
|
455
|
+
bufferLength: resume.buffer?.length ?? 0,
|
|
456
|
+
}
|
|
457
|
+
: null,
|
|
458
|
+
stats: this.stats(),
|
|
459
|
+
budget: this.deps.budget.snapshot(),
|
|
460
|
+
});
|
|
461
|
+
}
|
|
387
462
|
return stream;
|
|
388
463
|
}
|
|
389
464
|
|
|
@@ -396,6 +471,15 @@ export class BubbleWriter {
|
|
|
396
471
|
log.warn("stream", "stream error", {
|
|
397
472
|
msgId: this.logMsgId(),
|
|
398
473
|
elapsedMs: Date.now() - this.deps.startedAt,
|
|
474
|
+
checkpoint: checkpoint
|
|
475
|
+
? {
|
|
476
|
+
index: checkpoint.index,
|
|
477
|
+
hasStreamMsgId: Boolean(checkpoint.streamMsgId),
|
|
478
|
+
bufferLength: checkpoint.buffer?.length ?? 0,
|
|
479
|
+
}
|
|
480
|
+
: null,
|
|
481
|
+
stats: this.stats(),
|
|
482
|
+
budget: this.deps.budget.snapshot(),
|
|
399
483
|
err,
|
|
400
484
|
});
|
|
401
485
|
if (checkpoint && this.resumeAttempts < MAX_RESUME_ATTEMPTS) {
|
|
@@ -412,7 +496,12 @@ export class BubbleWriter {
|
|
|
412
496
|
this.pendingResume = undefined;
|
|
413
497
|
this.cur?.dispose();
|
|
414
498
|
this.cur = null;
|
|
415
|
-
log.warn("stream", "stream unavailable", {
|
|
499
|
+
log.warn("stream", "stream unavailable", {
|
|
500
|
+
msgId: this.logMsgId(),
|
|
501
|
+
reason,
|
|
502
|
+
stats: this.stats(),
|
|
503
|
+
budget: this.deps.budget.snapshot(),
|
|
504
|
+
});
|
|
416
505
|
this.deps.onWarn(t("stream.unavailable", { reason }));
|
|
417
506
|
}
|
|
418
507
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { QQBot } from "@tencent-connect/qqbot-nodejs";
|
|
2
2
|
import { BubbleWriter } from "./bubble-writer";
|
|
3
3
|
import type { ReplyBudget } from "../quota/reply-quota";
|
|
4
|
-
import { log } from "../infra/file-logger";
|
|
4
|
+
import { isLogEnabled, log } from "../infra/file-logger";
|
|
5
5
|
import { detectSkillRead } from "./stream-block-format";
|
|
6
6
|
|
|
7
7
|
export interface RunFeedbackDeps {
|
|
@@ -93,7 +93,21 @@ export class RunFeedback {
|
|
|
93
93
|
repushCurrent(openid: string, msgId: string): boolean {
|
|
94
94
|
const run = this.run;
|
|
95
95
|
const text = run?.currentText;
|
|
96
|
-
if (!run || !text?.trim())
|
|
96
|
+
if (!run || !text?.trim()) {
|
|
97
|
+
log.debug("run", "stream timing", {
|
|
98
|
+
phase: "last repush ignored",
|
|
99
|
+
hasRun: Boolean(run),
|
|
100
|
+
textLength: text?.length ?? 0,
|
|
101
|
+
...(run ? this.diagnostics(run) : {}),
|
|
102
|
+
});
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
log.debug("run", "stream timing", {
|
|
106
|
+
phase: "last repush",
|
|
107
|
+
msgId: msgId.slice(0, 8),
|
|
108
|
+
textLength: text.length,
|
|
109
|
+
...this.diagnostics(run),
|
|
110
|
+
});
|
|
97
111
|
// Rebind to the /last message so the new stream has a fresh passive window.
|
|
98
112
|
run.openid = openid;
|
|
99
113
|
run.msgId = msgId;
|
|
@@ -117,9 +131,21 @@ export class RunFeedback {
|
|
|
117
131
|
: this.recovery && now - this.recovery.at <= RECOVERY_TTL_MS
|
|
118
132
|
? this.recovery
|
|
119
133
|
: undefined;
|
|
120
|
-
if (!target)
|
|
134
|
+
if (!target) {
|
|
135
|
+
log.debug("run", "run started", {
|
|
136
|
+
phase: "no qq target",
|
|
137
|
+
pendingAgeMs: pending ? now - pending.at : null,
|
|
138
|
+
recoveryAgeMs: this.recovery ? now - this.recovery.at : null,
|
|
139
|
+
pendingTtlMs: PENDING_TTL_MS,
|
|
140
|
+
recoveryTtlMs: RECOVERY_TTL_MS,
|
|
141
|
+
});
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
121
144
|
const bot = this.deps.bot();
|
|
122
|
-
if (!bot)
|
|
145
|
+
if (!bot) {
|
|
146
|
+
log.debug("run", "run started", { phase: "no bot", msgId: target.msgId.slice(0, 8) });
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
123
149
|
|
|
124
150
|
const startedAt = Date.now();
|
|
125
151
|
const writer = new BubbleWriter({
|
|
@@ -145,7 +171,14 @@ export class RunFeedback {
|
|
|
145
171
|
writer,
|
|
146
172
|
};
|
|
147
173
|
this.run = run;
|
|
148
|
-
|
|
174
|
+
if (isLogEnabled("debug")) {
|
|
175
|
+
log.debug("run", "run started", {
|
|
176
|
+
msgId: run.msgId?.slice(0, 8) ?? "?",
|
|
177
|
+
source: target === pending ? "pending" : "recovery",
|
|
178
|
+
targetAgeMs: now - target.at,
|
|
179
|
+
budget: this.deps.budget.snapshot(),
|
|
180
|
+
});
|
|
181
|
+
}
|
|
149
182
|
|
|
150
183
|
const keepTyping = () => {
|
|
151
184
|
void bot.sendTyping({ scope: "c2c", targetId: run.openid }, 60).catch(() => {});
|
|
@@ -156,8 +189,20 @@ export class RunFeedback {
|
|
|
156
189
|
|
|
157
190
|
onToolStart(toolName: string, args?: unknown, toolCallId?: string): void {
|
|
158
191
|
const run = this.run;
|
|
159
|
-
if (!run || run.writer.dead) return;
|
|
160
192
|
const id = toolCallId ?? toolName;
|
|
193
|
+
if (!run) {
|
|
194
|
+
log.debug("run", "tool start", { phase: "ignored: no active run", toolName, toolCallId: id });
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (run.writer.dead) {
|
|
198
|
+
log.debug("run", "tool start", {
|
|
199
|
+
phase: "ignored: stream dead",
|
|
200
|
+
toolName,
|
|
201
|
+
toolCallId: id,
|
|
202
|
+
...this.diagnostics(run),
|
|
203
|
+
});
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
161
206
|
if (run.tools.some((tool) => tool.id === id)) return;
|
|
162
207
|
const skill = detectSkillRead(toolName, args);
|
|
163
208
|
run.tools.push({
|
|
@@ -168,16 +213,46 @@ export class RunFeedback {
|
|
|
168
213
|
started: false,
|
|
169
214
|
finished: false,
|
|
170
215
|
});
|
|
216
|
+
log.debug("run", "tool start", {
|
|
217
|
+
phase: "queued",
|
|
218
|
+
toolName,
|
|
219
|
+
toolCallId: id,
|
|
220
|
+
skillName: skill?.name ?? null,
|
|
221
|
+
...this.diagnostics(run),
|
|
222
|
+
});
|
|
171
223
|
this.startNextTool(run);
|
|
172
224
|
}
|
|
173
225
|
|
|
174
226
|
onToolEnd(toolName: string, isError: boolean, result?: unknown, toolCallId?: string): void {
|
|
175
227
|
const run = this.run;
|
|
176
|
-
if (!run || run.writer.dead) return;
|
|
177
228
|
const id = toolCallId ?? toolName;
|
|
229
|
+
if (!run) {
|
|
230
|
+
log.debug("run", "tool end", { phase: "ignored: no active run", toolName, toolCallId: id });
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
178
233
|
const tool = run.tools.find((candidate) => candidate.id === id);
|
|
179
|
-
if (!tool)
|
|
234
|
+
if (!tool) {
|
|
235
|
+
log.debug("run", "tool end", { phase: "ignored: unknown tool", toolName, toolCallId: id });
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (run.writer.dead) {
|
|
239
|
+
log.debug("run", "tool end", {
|
|
240
|
+
phase: "ignored: stream dead",
|
|
241
|
+
toolName,
|
|
242
|
+
toolCallId: id,
|
|
243
|
+
isError,
|
|
244
|
+
...this.diagnostics(run),
|
|
245
|
+
});
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
180
248
|
tool.result = { isError, result };
|
|
249
|
+
log.debug("run", "tool end", {
|
|
250
|
+
phase: "result recorded",
|
|
251
|
+
toolName,
|
|
252
|
+
toolCallId: id,
|
|
253
|
+
isError,
|
|
254
|
+
...this.diagnostics(run),
|
|
255
|
+
});
|
|
181
256
|
if (run.activeTool === id) this.finishTool(run, tool);
|
|
182
257
|
}
|
|
183
258
|
|
|
@@ -214,7 +289,14 @@ export class RunFeedback {
|
|
|
214
289
|
|
|
215
290
|
async onAssistantFinal(text: string, isError: boolean): Promise<boolean> {
|
|
216
291
|
const run = this.run;
|
|
217
|
-
if (!run)
|
|
292
|
+
if (!run) {
|
|
293
|
+
log.debug("run", "stream takeover decision", {
|
|
294
|
+
phase: "assistant final ignored: no active run",
|
|
295
|
+
textLength: text.length,
|
|
296
|
+
isError,
|
|
297
|
+
});
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
218
300
|
|
|
219
301
|
run.finalizedText = [run.finalizedText, text].filter((part) => part.trim()).join("\n\n");
|
|
220
302
|
if (text.trim()) run.currentText = text;
|
|
@@ -235,11 +317,26 @@ export class RunFeedback {
|
|
|
235
317
|
this.recovery = { openid: run.openid, msgId: run.msgId ?? "", at: Date.now() };
|
|
236
318
|
}
|
|
237
319
|
|
|
320
|
+
const hadPendingTools = this.hasPendingTools(run);
|
|
238
321
|
const ok = await run.writer.flush();
|
|
322
|
+
const streamTookOver = !isError && ok && run.writer.hasPushed;
|
|
323
|
+
log.debug("run", "stream takeover decision", {
|
|
324
|
+
phase: "assistant final",
|
|
325
|
+
textLength: text.length,
|
|
326
|
+
deltaLength: delta.length,
|
|
327
|
+
isError,
|
|
328
|
+
hadPendingTools,
|
|
329
|
+
flushOk: ok,
|
|
330
|
+
hasPushed: run.writer.hasPushed,
|
|
331
|
+
streamTookOver,
|
|
332
|
+
deferredTextLen: run.deferredText.length,
|
|
333
|
+
deferredThinkingLen: run.deferredThinking.length,
|
|
334
|
+
...this.diagnostics(run),
|
|
335
|
+
});
|
|
239
336
|
run.finalizedText = "";
|
|
240
337
|
run.resultSentLen = 0;
|
|
241
338
|
if (isError) return false;
|
|
242
|
-
return
|
|
339
|
+
return streamTookOver;
|
|
243
340
|
}
|
|
244
341
|
|
|
245
342
|
async onAgentEnd(): Promise<void> {
|
|
@@ -249,13 +346,24 @@ export class RunFeedback {
|
|
|
249
346
|
return;
|
|
250
347
|
}
|
|
251
348
|
await run.writer.whenIdle();
|
|
252
|
-
|
|
349
|
+
const hasPendingTools = this.hasPendingTools(run);
|
|
350
|
+
if (!hasPendingTools) this.flushDeferred(run);
|
|
253
351
|
await run.writer.whenIdle();
|
|
254
352
|
this.clearTimers();
|
|
255
353
|
if (run.currentText.trim()) this.lastCompletedText = run.currentText;
|
|
256
354
|
if (!run.hadError && run.writer.hasPushed) this.recovery = null;
|
|
355
|
+
log.debug("run", "run ended", {
|
|
356
|
+
phase: "cleanup",
|
|
357
|
+
msgId: run.msgId?.slice(0, 8) ?? "?",
|
|
358
|
+
elapsedMs: Date.now() - run.startedAt,
|
|
359
|
+
hasPendingTools,
|
|
360
|
+
hasPushed: run.writer.hasPushed,
|
|
361
|
+
lastCompletedTextLength: this.lastCompletedText?.length ?? 0,
|
|
362
|
+
...this.diagnostics(run),
|
|
363
|
+
});
|
|
257
364
|
this.run = null;
|
|
258
365
|
log.debug("run", "run ended", {
|
|
366
|
+
phase: "finished",
|
|
259
367
|
msgId: run.msgId?.slice(0, 8) ?? "?",
|
|
260
368
|
elapsedMs: Date.now() - run.startedAt,
|
|
261
369
|
});
|
|
@@ -271,6 +379,10 @@ export class RunFeedback {
|
|
|
271
379
|
}
|
|
272
380
|
|
|
273
381
|
dispose(): void {
|
|
382
|
+
const run = this.run;
|
|
383
|
+
if (run) {
|
|
384
|
+
log.debug("run", "run ended", { phase: "dispose", ...this.diagnostics(run) });
|
|
385
|
+
}
|
|
274
386
|
this.clearTimers();
|
|
275
387
|
this.run?.writer.dispose();
|
|
276
388
|
this.run = null;
|
|
@@ -309,6 +421,13 @@ export class RunFeedback {
|
|
|
309
421
|
if (!tool) return;
|
|
310
422
|
tool.started = true;
|
|
311
423
|
run.activeTool = tool.id;
|
|
424
|
+
log.debug("run", "tool start", {
|
|
425
|
+
phase: "output started",
|
|
426
|
+
toolName: tool.name,
|
|
427
|
+
toolCallId: tool.id,
|
|
428
|
+
skillName: tool.skillName ?? null,
|
|
429
|
+
...this.diagnostics(run),
|
|
430
|
+
});
|
|
312
431
|
if (tool.skillName) {
|
|
313
432
|
run.writer.pushSkill(tool.skillName);
|
|
314
433
|
} else {
|
|
@@ -320,8 +439,15 @@ export class RunFeedback {
|
|
|
320
439
|
private finishTool(run: RunState, tool: ToolCall): void {
|
|
321
440
|
if (!tool.result || tool.finished) return;
|
|
322
441
|
tool.finished = true;
|
|
442
|
+
log.debug("run", "tool end", {
|
|
443
|
+
phase: "output finished",
|
|
444
|
+
toolName: tool.name,
|
|
445
|
+
toolCallId: tool.id,
|
|
446
|
+
isError: tool.result.isError,
|
|
447
|
+
...this.diagnostics(run),
|
|
448
|
+
});
|
|
323
449
|
if (!tool.skillName) {
|
|
324
|
-
run.writer.pushToolResult(tool.result.result, tool.result.isError);
|
|
450
|
+
run.writer.pushToolResult(tool.result.result, tool.result.isError, tool.name, tool.args);
|
|
325
451
|
}
|
|
326
452
|
run.writer.schedule(() => {
|
|
327
453
|
if (this.run !== run) return;
|
|
@@ -338,4 +464,32 @@ export class RunFeedback {
|
|
|
338
464
|
if (run.typingTimer) clearInterval(run.typingTimer);
|
|
339
465
|
run.typingTimer = undefined;
|
|
340
466
|
}
|
|
467
|
+
|
|
468
|
+
/** Structured snapshot for debug logs; must not be called on hot paths. */
|
|
469
|
+
private diagnostics(run: RunState): Record<string, unknown> {
|
|
470
|
+
if (!isLogEnabled("debug")) return {};
|
|
471
|
+
return {
|
|
472
|
+
runElapsedMs: Date.now() - run.startedAt,
|
|
473
|
+
activeTool: run.activeTool ?? null,
|
|
474
|
+
toolCount: run.tools.length,
|
|
475
|
+
pendingToolCount: run.tools.filter((tool) => !tool.finished).length,
|
|
476
|
+
tools: run.tools.map((tool) => ({
|
|
477
|
+
id: tool.id,
|
|
478
|
+
name: tool.name,
|
|
479
|
+
started: tool.started,
|
|
480
|
+
finished: tool.finished,
|
|
481
|
+
hasResult: Boolean(tool.result),
|
|
482
|
+
isError: tool.result?.isError ?? null,
|
|
483
|
+
})),
|
|
484
|
+
deferredTextLen: run.deferredText.length,
|
|
485
|
+
deferredThinkingLen: run.deferredThinking.length,
|
|
486
|
+
currentTextLen: run.currentText.length,
|
|
487
|
+
finalizedTextLen: run.finalizedText.length,
|
|
488
|
+
resultSentLen: run.resultSentLen,
|
|
489
|
+
thinkingSentLen: run.thinkingSentLen,
|
|
490
|
+
hadError: run.hadError,
|
|
491
|
+
writer: run.writer.stats(),
|
|
492
|
+
budget: this.deps.budget.snapshot(),
|
|
493
|
+
};
|
|
494
|
+
}
|
|
341
495
|
}
|
|
@@ -6,7 +6,7 @@ export function thinkingHeader(): string {
|
|
|
6
6
|
const ARG_MAX_CHARS = 200;
|
|
7
7
|
const RESULT_MAX_CHARS = 600;
|
|
8
8
|
const RESULT_MAX_LINES = 8;
|
|
9
|
-
const ARG_KEYS = ["command", "path", "file_path", "filename", "pattern", "query", "url"];
|
|
9
|
+
const ARG_KEYS = ["command", "path", "file_path", "filename", "pattern", "query", "url"] as const;
|
|
10
10
|
const TOOL_ICONS: Record<string, string> = {
|
|
11
11
|
bash: "💻",
|
|
12
12
|
read: "📖",
|
|
@@ -51,24 +51,32 @@ function truncateText(text: string, maxChars: number, maxLines: number): string
|
|
|
51
51
|
return truncated ? `${out}…` : out;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
function
|
|
55
|
-
|
|
56
|
-
if (typeof args === "
|
|
57
|
-
value = args;
|
|
58
|
-
} else if (args && typeof args === "object") {
|
|
54
|
+
function stringArg(args: unknown, keys: readonly string[]): string | undefined {
|
|
55
|
+
if (typeof args === "string") return args;
|
|
56
|
+
if (args && typeof args === "object") {
|
|
59
57
|
const record = args as Record<string, unknown>;
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
58
|
+
for (const key of keys) {
|
|
59
|
+
const candidate = record[key];
|
|
60
|
+
if (typeof candidate === "string" && candidate.length > 0) return candidate;
|
|
61
|
+
}
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
if (args !== undefined && args !== null) return String(args);
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function rawPrimaryArg(args: unknown, keys: readonly string[] = ARG_KEYS): string | undefined {
|
|
69
|
+
return stringArg(args, keys);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function primaryArg(args: unknown): string {
|
|
73
|
+
let value = rawPrimaryArg(args);
|
|
74
|
+
if (!value && args && typeof args === "object") {
|
|
75
|
+
try {
|
|
76
|
+
value = JSON.stringify(args);
|
|
77
|
+
} catch {
|
|
78
|
+
value = String(args);
|
|
69
79
|
}
|
|
70
|
-
} else if (args !== undefined && args !== null) {
|
|
71
|
-
value = String(args);
|
|
72
80
|
}
|
|
73
81
|
const normalized = (value ?? "").replace(/\s+/g, " ").trim();
|
|
74
82
|
return normalized.length > ARG_MAX_CHARS ? `${normalized.slice(0, ARG_MAX_CHARS)}…` : normalized;
|
|
@@ -99,9 +107,114 @@ export function formatSkillBlock(name: string): string {
|
|
|
99
107
|
}
|
|
100
108
|
|
|
101
109
|
export function formatToolLine(name: string, args: unknown): string {
|
|
110
|
+
const lowerName = name.toLowerCase();
|
|
111
|
+
const label = `${TOOL_ICONS[lowerName] ?? "🔧"} ${name}`;
|
|
112
|
+
const header = `## ${label}`;
|
|
113
|
+
|
|
114
|
+
if (lowerName === "bash") {
|
|
115
|
+
const command = rawPrimaryArg(args, ["command"]);
|
|
116
|
+
const formatted = command ? formatBashCommand(command) : "";
|
|
117
|
+
return formatted ? `${header}\n\n${formatted}` : header;
|
|
118
|
+
}
|
|
119
|
+
|
|
102
120
|
const primary = primaryArg(args);
|
|
103
|
-
|
|
104
|
-
|
|
121
|
+
return primary ? `${header}\n\n${inlineCode(primary)}` : header;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function formatBashCommand(command: string): string {
|
|
125
|
+
const normalized = command
|
|
126
|
+
.replace(/\r\n?/g, "\n")
|
|
127
|
+
.replace(/[ \t]+$/gm, "")
|
|
128
|
+
.trim();
|
|
129
|
+
if (!normalized) return "";
|
|
130
|
+
const fence = backtickFence(normalized, 3);
|
|
131
|
+
return `${fence}bash\n${normalized}\n${fence}`;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const EXTENSION_LANGUAGES: Record<string, string> = {
|
|
135
|
+
md: "markdown",
|
|
136
|
+
markdown: "markdown",
|
|
137
|
+
mdx: "markdown",
|
|
138
|
+
html: "html",
|
|
139
|
+
htm: "html",
|
|
140
|
+
xml: "xml",
|
|
141
|
+
svg: "xml",
|
|
142
|
+
tex: "latex",
|
|
143
|
+
rst: "rst",
|
|
144
|
+
css: "css",
|
|
145
|
+
scss: "scss",
|
|
146
|
+
less: "less",
|
|
147
|
+
vue: "vue",
|
|
148
|
+
svelte: "svelte",
|
|
149
|
+
json: "json",
|
|
150
|
+
jsonc: "json",
|
|
151
|
+
json5: "json",
|
|
152
|
+
ipynb: "json",
|
|
153
|
+
yaml: "yaml",
|
|
154
|
+
yml: "yaml",
|
|
155
|
+
toml: "toml",
|
|
156
|
+
ini: "ini",
|
|
157
|
+
cfg: "ini",
|
|
158
|
+
conf: "ini",
|
|
159
|
+
sql: "sql",
|
|
160
|
+
graphql: "graphql",
|
|
161
|
+
gql: "graphql",
|
|
162
|
+
proto: "protobuf",
|
|
163
|
+
ts: "typescript",
|
|
164
|
+
mts: "typescript",
|
|
165
|
+
cts: "typescript",
|
|
166
|
+
tsx: "tsx",
|
|
167
|
+
js: "javascript",
|
|
168
|
+
mjs: "javascript",
|
|
169
|
+
cjs: "javascript",
|
|
170
|
+
jsx: "jsx",
|
|
171
|
+
py: "python",
|
|
172
|
+
pyw: "python",
|
|
173
|
+
sh: "bash",
|
|
174
|
+
bash: "bash",
|
|
175
|
+
zsh: "bash",
|
|
176
|
+
fish: "fish",
|
|
177
|
+
ps1: "powershell",
|
|
178
|
+
bat: "bat",
|
|
179
|
+
cmd: "bat",
|
|
180
|
+
go: "go",
|
|
181
|
+
rs: "rust",
|
|
182
|
+
java: "java",
|
|
183
|
+
kt: "kotlin",
|
|
184
|
+
kts: "kotlin",
|
|
185
|
+
c: "c",
|
|
186
|
+
h: "c",
|
|
187
|
+
cpp: "cpp",
|
|
188
|
+
cc: "cpp",
|
|
189
|
+
cxx: "cpp",
|
|
190
|
+
hpp: "cpp",
|
|
191
|
+
cs: "csharp",
|
|
192
|
+
rb: "ruby",
|
|
193
|
+
php: "php",
|
|
194
|
+
swift: "swift",
|
|
195
|
+
dart: "dart",
|
|
196
|
+
lua: "lua",
|
|
197
|
+
r: "r",
|
|
198
|
+
pl: "perl",
|
|
199
|
+
diff: "diff",
|
|
200
|
+
patch: "diff",
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
function languageForPath(filePath: string): string {
|
|
204
|
+
const base = filePath.trim().split(/[\\/]/).pop()?.toLowerCase() ?? "";
|
|
205
|
+
if (base === "dockerfile") return "dockerfile";
|
|
206
|
+
if (base === "makefile") return "makefile";
|
|
207
|
+
if (base === "cmakelists.txt") return "cmake";
|
|
208
|
+
if (base === "readme") return "markdown";
|
|
209
|
+
const ext = base.includes(".") ? base.slice(base.lastIndexOf(".") + 1) : "";
|
|
210
|
+
return EXTENSION_LANGUAGES[ext] ?? "text";
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function resultLanguage(toolName: string, args: unknown, isError: boolean): string {
|
|
214
|
+
if (isError) return "text";
|
|
215
|
+
if (toolName.toLowerCase() !== "read") return "text";
|
|
216
|
+
const path = rawPrimaryArg(args, ["path", "file_path"]);
|
|
217
|
+
return path ? languageForPath(path) : "text";
|
|
105
218
|
}
|
|
106
219
|
|
|
107
220
|
function resultText(result: unknown): string {
|
|
@@ -134,12 +247,16 @@ function resultText(result: unknown): string {
|
|
|
134
247
|
}
|
|
135
248
|
}
|
|
136
249
|
|
|
137
|
-
export function formatToolResult(result: unknown, isError: boolean): string {
|
|
250
|
+
export function formatToolResult(result: unknown, isError: boolean, language = "text"): string {
|
|
138
251
|
let text = resultText(result);
|
|
139
252
|
text = text.replace(/\r\n/g, "\n").replace(/\s+$/, "");
|
|
140
|
-
if (!text)
|
|
253
|
+
if (!text) {
|
|
254
|
+
text = t("stream.noOutput");
|
|
255
|
+
language = "text";
|
|
256
|
+
}
|
|
141
257
|
text = truncateText(text, RESULT_MAX_CHARS, RESULT_MAX_LINES);
|
|
142
258
|
if (isError) text = `❌ ${text}`;
|
|
259
|
+
language = isError || !/^[a-z0-9+#.-]+$/i.test(language) ? "text" : language;
|
|
143
260
|
const fence = backtickFence(text, 3);
|
|
144
|
-
return `${fence}
|
|
261
|
+
return `${fence}${language}\n${text}\n${fence}\n`;
|
|
145
262
|
}
|