@trim21/personal-pi-extensions 0.1.633 → 0.1.634
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/package.json +1 -1
- package/src/spawn-agent-progress.ts +214 -0
- package/src/spawn-agent.ts +11 -132
package/package.json
CHANGED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Progress state machine for the `spawn_agent` tool.
|
|
3
|
+
*
|
|
4
|
+
* The parent sees a rolling log: `tool: <name>` lines for tool calls and
|
|
5
|
+
* `text: <content>` lines for completed text blocks, keeping the last
|
|
6
|
+
* `MAX_PROGRESS_LINES` lines. Consecutive tool calls are merged into a single
|
|
7
|
+
* `tool:` line (`read x 2, glob`) and the merged content is folded to the
|
|
8
|
+
* first/last 9 chars joined by `…`, so a burst of tool calls or a long text
|
|
9
|
+
* block does not flood the window. Only a text block starts a new line —
|
|
10
|
+
* thinking is a transient status line, so it does not break the merge across
|
|
11
|
+
* turn boundaries. Line content is sanitized first: markdown marker characters
|
|
12
|
+
* are stripped and whitespace (including newlines) is collapsed to single
|
|
13
|
+
* spaces, so one log entry is always exactly one rendered line.
|
|
14
|
+
*
|
|
15
|
+
* The rendered panel is the window, then an optional transient
|
|
16
|
+
* `thinking ( N chars )` line while the model is thinking, then a fixed footer
|
|
17
|
+
* line: the subagent name as a code span (`` `scout` ``) followed by the live
|
|
18
|
+
* usage stats when there are any. The thinking line and the footer ride
|
|
19
|
+
* outside the rolling window, so the footer is never trimmed. The model name
|
|
20
|
+
* on the footer is the model the session actually uses, which differs from the
|
|
21
|
+
* frontmatter string when the declared model resolved to nothing and the SDK
|
|
22
|
+
* fell back to a default.
|
|
23
|
+
*
|
|
24
|
+
* State is held in the closure of a `createSubagentProgress` instance, one per
|
|
25
|
+
* run. The `noteX` methods only mutate state and return nothing; pushing the
|
|
26
|
+
* rendered text to the host (`onUpdate`) is the caller's job, so the caller
|
|
27
|
+
* decides when a state change is worth a refresh.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Progress log keeps only the most recent lines (rolling window).
|
|
32
|
+
* 展开后面板最多 6 行内容:4 行日志 + 可能的一行瞬态 thinking + 固定的 metadata 行。
|
|
33
|
+
*/
|
|
34
|
+
const MAX_PROGRESS_LINES = 4;
|
|
35
|
+
/** Progress line content (without the `tool:` / `text:` prefix) is capped at 21 chars; longer text is folded to the first/last 9 chars joined by ` … `. */
|
|
36
|
+
const MAX_PROGRESS_CHARS_PER_LINE = 21;
|
|
37
|
+
/**
|
|
38
|
+
* 进度内容会被 pi 按 markdown 渲染,这些标记字符会改变显示效果(代码块、粗体、
|
|
39
|
+
* 链接、标题等),因此在进日志前统一删掉。
|
|
40
|
+
*/
|
|
41
|
+
const PROGRESS_MARKDOWN_MARKERS_RE = /[`*_~[\]<>#|]/g;
|
|
42
|
+
|
|
43
|
+
/** 进度面板要展示的运行统计(`runAgent` 的 `result.usage`)。 */
|
|
44
|
+
export interface ProgressUsage {
|
|
45
|
+
turns: number;
|
|
46
|
+
cost: number;
|
|
47
|
+
contextTokens: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 一次子代理运行的进度状态机:改状态用 `noteX`,取文本用 `render`。 */
|
|
51
|
+
export interface SubagentProgress {
|
|
52
|
+
/** 工具调用开始:合并进当前 `tool:` 行。 */
|
|
53
|
+
noteToolCall(rawName: string): void;
|
|
54
|
+
/** 一个完成的 text 块:作为 `text:` 行写入并打断工具合并。 */
|
|
55
|
+
noteTextBlock(content: string): void;
|
|
56
|
+
/** thinking 开始:打开瞬态行(计数从 0 开始)。 */
|
|
57
|
+
thinkingStart(): void;
|
|
58
|
+
/** thinking 增量:累加字符数。 */
|
|
59
|
+
thinkingDelta(length: number): void;
|
|
60
|
+
/** thinking 结束:关闭瞬态行。 */
|
|
61
|
+
thinkingEnd(): void;
|
|
62
|
+
/** 渲染面板:滚动窗口 + 瞬态 thinking 行 + footer。 */
|
|
63
|
+
render(usage: ProgressUsage, model?: string): string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Fold over-long progress line content: keep the first/last 9 chars joined by
|
|
68
|
+
* ` … ` (space, ellipsis, space), so the folded line never exceeds
|
|
69
|
+
* `MAX_PROGRESS_CHARS_PER_LINE` chars (9 + 3 + 9 = 21). Shorter text is
|
|
70
|
+
* returned as-is.
|
|
71
|
+
*/
|
|
72
|
+
function foldProgressLine(text: string): string {
|
|
73
|
+
if (text.length <= MAX_PROGRESS_CHARS_PER_LINE) {
|
|
74
|
+
return text;
|
|
75
|
+
}
|
|
76
|
+
const keep = Math.floor((MAX_PROGRESS_CHARS_PER_LINE - 3) / 2);
|
|
77
|
+
return `${text.slice(0, keep)} … ${text.slice(-keep)}`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 进度行是「单行内容 + markdown 渲染」:内容里的换行会打乱按行滚动的窗口,
|
|
82
|
+
* markdown 标记会改变渲染效果。先删掉标记字符,再把换行/制表符/连续空格折成
|
|
83
|
+
* 单个空格并去掉首尾空白,保证一条日志恒为一行。
|
|
84
|
+
*/
|
|
85
|
+
function sanitizeProgressLine(text: string): string {
|
|
86
|
+
return text.replaceAll(PROGRESS_MARKDOWN_MARKERS_RE, "").replaceAll(/\s+/g, " ").trim();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* token 计数的可读格式。进度面板 footer 与 spawn_agent 的输出截断提示共用,
|
|
91
|
+
* 改动会同时影响两处。
|
|
92
|
+
*/
|
|
93
|
+
export function formatTokens(count: number): string {
|
|
94
|
+
if (count < 1000) {
|
|
95
|
+
return count.toString();
|
|
96
|
+
}
|
|
97
|
+
if (count < 10_000) {
|
|
98
|
+
return `${(count / 1000).toFixed(1)}k`;
|
|
99
|
+
}
|
|
100
|
+
if (count < 1_000_000) {
|
|
101
|
+
return `${Math.round(count / 1000)}k`;
|
|
102
|
+
}
|
|
103
|
+
return `${(count / 1_000_000).toFixed(1)}M`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function formatUsageStats(usage: ProgressUsage, model?: string): string {
|
|
107
|
+
const parts: string[] = [];
|
|
108
|
+
if (usage.turns) {
|
|
109
|
+
parts.push(`${usage.turns} turn${usage.turns > 1 ? "s" : ""}`);
|
|
110
|
+
}
|
|
111
|
+
if (usage.cost) {
|
|
112
|
+
parts.push(`$${usage.cost.toFixed(4)}`);
|
|
113
|
+
}
|
|
114
|
+
if (usage.contextTokens > 0) {
|
|
115
|
+
parts.push(`ctx:${formatTokens(usage.contextTokens)}`);
|
|
116
|
+
}
|
|
117
|
+
if (model) {
|
|
118
|
+
parts.push(model);
|
|
119
|
+
}
|
|
120
|
+
return parts.join(" ");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function toolSegment(name: string, count: number): string {
|
|
124
|
+
return count > 1 ? `${name} x ${count}` : name;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Create the progress state machine for one subagent run.
|
|
129
|
+
*
|
|
130
|
+
* `maxLines` only controls the rolling window of log lines — the transient
|
|
131
|
+
* thinking line and the footer ride outside it. Defaults to
|
|
132
|
+
* `MAX_PROGRESS_LINES`.
|
|
133
|
+
*/
|
|
134
|
+
export function createSubagentProgress(options: {
|
|
135
|
+
/** 面板标题(子代理名)。 */
|
|
136
|
+
name: string;
|
|
137
|
+
/** 滚动窗口行数,缺省 4。 */
|
|
138
|
+
maxLines?: number;
|
|
139
|
+
}): SubagentProgress {
|
|
140
|
+
const maxLines = options.maxLines ?? MAX_PROGRESS_LINES;
|
|
141
|
+
let logLines: string[] = [];
|
|
142
|
+
// 工具调用行合并:连续的 tool_execution_start 事件合并在同一 `tool:` 行
|
|
143
|
+
// (如 `tool: read x 2, glob`),相同工具名连续出现时计为 `name x N`,
|
|
144
|
+
// 不同名按调用顺序罗列;只有写入日志行的事件(text 块)打断合并,thinking
|
|
145
|
+
// 不写日志行、也不打断合并。
|
|
146
|
+
let toolLineSegments: string[] = [];
|
|
147
|
+
let toolLine: { name: string; count: number } | undefined;
|
|
148
|
+
// 思考中状态:thinkingStart 打开、thinkingDelta 累计字符数、thinkingEnd
|
|
149
|
+
// 关闭;非 undefined 时 render 在 footer 上方插一行瞬态 thinking 状态。
|
|
150
|
+
let thinkingChars: number | undefined;
|
|
151
|
+
|
|
152
|
+
function pushLogLine(line: string) {
|
|
153
|
+
logLines.push(line);
|
|
154
|
+
if (logLines.length > maxLines) {
|
|
155
|
+
logLines = logLines.slice(-maxLines);
|
|
156
|
+
}
|
|
157
|
+
// 写进日志的新行都会打断工具调用合并,下一批调用另起一行。
|
|
158
|
+
toolLine = undefined;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function appendToolLine(rawName: string) {
|
|
162
|
+
const name = sanitizeProgressLine(rawName);
|
|
163
|
+
const firstInBatch = toolLine === undefined;
|
|
164
|
+
if (toolLine === undefined) {
|
|
165
|
+
toolLineSegments = [];
|
|
166
|
+
toolLine = { name, count: 1 };
|
|
167
|
+
} else if (toolLine.name === name) {
|
|
168
|
+
toolLine.count++;
|
|
169
|
+
} else {
|
|
170
|
+
toolLineSegments.push(toolSegment(toolLine.name, toolLine.count));
|
|
171
|
+
toolLine = { name, count: 1 };
|
|
172
|
+
}
|
|
173
|
+
const parts = [...toolLineSegments, toolSegment(toolLine.name, toolLine.count)].join(", ");
|
|
174
|
+
const line = `tool: ${foldProgressLine(parts)}`;
|
|
175
|
+
if (firstInBatch) {
|
|
176
|
+
logLines.push(line);
|
|
177
|
+
if (logLines.length > maxLines) {
|
|
178
|
+
logLines = logLines.slice(-maxLines);
|
|
179
|
+
}
|
|
180
|
+
} else {
|
|
181
|
+
logLines[logLines.length - 1] = line;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
noteToolCall: appendToolLine,
|
|
187
|
+
noteTextBlock(content) {
|
|
188
|
+
pushLogLine(`text: ${foldProgressLine(sanitizeProgressLine(content))}`);
|
|
189
|
+
},
|
|
190
|
+
thinkingStart() {
|
|
191
|
+
thinkingChars = 0;
|
|
192
|
+
},
|
|
193
|
+
thinkingDelta(length) {
|
|
194
|
+
thinkingChars = (thinkingChars ?? 0) + length;
|
|
195
|
+
},
|
|
196
|
+
thinkingEnd() {
|
|
197
|
+
thinkingChars = undefined;
|
|
198
|
+
},
|
|
199
|
+
render(usage, model) {
|
|
200
|
+
// 最后一行固定是「子代理名 + 运行中统计」:名字用 code span 标出,进度流里
|
|
201
|
+
// 一眼能看出属于哪个 subagent;usage 与它同行,TUI 始终能看到实时 token 开销。
|
|
202
|
+
// 这行位于滚动窗口之外,因此永远不会被挤掉。
|
|
203
|
+
const usageLine = formatUsageStats(usage, model);
|
|
204
|
+
const name = sanitizeProgressLine(options.name);
|
|
205
|
+
const footer = usageLine ? `\`${name}\` ${usageLine}` : `\`${name}\``;
|
|
206
|
+
const lines = [...logLines];
|
|
207
|
+
if (thinkingChars !== undefined) {
|
|
208
|
+
lines.push(`thinking ( ${thinkingChars} chars )`);
|
|
209
|
+
}
|
|
210
|
+
lines.push(footer);
|
|
211
|
+
return lines.join("\n");
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
}
|
package/src/spawn-agent.ts
CHANGED
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
* returns its final output to the parent model. Progress is streamed through
|
|
13
13
|
* `onUpdate`, the same channel the built-in bash tool uses for live output.
|
|
14
14
|
* Progress is a rolling log: `tool: <name>` lines for tool calls and
|
|
15
|
-
* `text: <content>` lines for completed text blocks, keeping the last
|
|
16
|
-
* `MAX_PROGRESS_LINES`
|
|
15
|
+
* `text: <content>` lines for completed text blocks, keeping only the last few
|
|
16
|
+
* lines (`MAX_PROGRESS_LINES` in spawn-agent-progress.ts). Consecutive tool calls are merged into a
|
|
17
17
|
* single `tool:` line (`read x 2, glob`) and over-long line content is
|
|
18
18
|
* folded to the first/last 9 chars joined by `…`, so a burst of tool calls
|
|
19
19
|
* or a long text block does not flood the window; only a text block starts
|
|
@@ -79,6 +79,7 @@ import {
|
|
|
79
79
|
formatAgentList,
|
|
80
80
|
loadSpawnAgentConfig,
|
|
81
81
|
} from "./spawn-agent-agents.js";
|
|
82
|
+
import { createSubagentProgress, formatTokens } from "./spawn-agent-progress.js";
|
|
82
83
|
|
|
83
84
|
// ── constants ────────────────────────────────────────────────────────────────
|
|
84
85
|
|
|
@@ -96,18 +97,6 @@ export const SUBAGENT_DEFAULT_SANDBOX: BwrapConfig = completeBwrapConfig({
|
|
|
96
97
|
fs: { mode: "readonly" },
|
|
97
98
|
network: { mode: "block" },
|
|
98
99
|
});
|
|
99
|
-
/**
|
|
100
|
-
* Progress log keeps only the most recent lines (rolling window).
|
|
101
|
-
* 展开后面板最多 6 行内容:4 行日志 + 可能的一行瞬态 thinking + 固定的 metadata 行。
|
|
102
|
-
*/
|
|
103
|
-
const MAX_PROGRESS_LINES = 4;
|
|
104
|
-
/** Progress line content (without the `tool:` / `text:` prefix) is capped at 21 chars; longer text is folded to the first/last 9 chars joined by ` … `. */
|
|
105
|
-
const MAX_PROGRESS_CHARS_PER_LINE = 21;
|
|
106
|
-
/**
|
|
107
|
-
* 进度内容会被 pi 按 markdown 渲染,这些标记字符会改变显示效果(代码块、粗体、
|
|
108
|
-
* 链接、标题等),因此在进日志前统一删掉。
|
|
109
|
-
*/
|
|
110
|
-
const PROGRESS_MARKDOWN_MARKERS_RE = /[`*_~[\]<>#|]/g;
|
|
111
100
|
/** 错误消息里 stderr 的展示上限。 */
|
|
112
101
|
const MAX_STDERR_ERROR_BYTES = 4 * 1024;
|
|
113
102
|
/** 全局默认配置:~/.pi/agent/spawn-agent.json,字段可被 frontmatter 覆盖。 */
|
|
@@ -231,59 +220,6 @@ export function formatSubagentError(result: SubagentResult): { reason: string; m
|
|
|
231
220
|
return { reason, message: parts.length > 0 ? parts.join("\n") : "(no output)" };
|
|
232
221
|
}
|
|
233
222
|
|
|
234
|
-
/**
|
|
235
|
-
* Fold over-long progress line content: keep the first/last 9 chars joined by
|
|
236
|
-
* ` … ` (space, ellipsis, space), so the folded line never exceeds
|
|
237
|
-
* `MAX_PROGRESS_CHARS_PER_LINE` chars (9 + 3 + 9 = 21). Shorter text is
|
|
238
|
-
* returned as-is.
|
|
239
|
-
*/
|
|
240
|
-
function foldProgressLine(text: string): string {
|
|
241
|
-
if (text.length <= MAX_PROGRESS_CHARS_PER_LINE) {
|
|
242
|
-
return text;
|
|
243
|
-
}
|
|
244
|
-
const keep = Math.floor((MAX_PROGRESS_CHARS_PER_LINE - 3) / 2);
|
|
245
|
-
return `${text.slice(0, keep)} … ${text.slice(-keep)}`;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* 进度行是「单行内容 + markdown 渲染」:内容里的换行会打乱按行滚动的窗口,
|
|
250
|
-
* markdown 标记会改变渲染效果。先删掉标记字符,再把换行/制表符/连续空格折成
|
|
251
|
-
* 单个空格并去掉首尾空白,保证一条日志恒为一行。
|
|
252
|
-
*/
|
|
253
|
-
function sanitizeProgressLine(text: string): string {
|
|
254
|
-
return text.replaceAll(PROGRESS_MARKDOWN_MARKERS_RE, "").replaceAll(/\s+/g, " ").trim();
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function formatTokens(count: number): string {
|
|
258
|
-
if (count < 1000) {
|
|
259
|
-
return count.toString();
|
|
260
|
-
}
|
|
261
|
-
if (count < 10_000) {
|
|
262
|
-
return `${(count / 1000).toFixed(1)}k`;
|
|
263
|
-
}
|
|
264
|
-
if (count < 1_000_000) {
|
|
265
|
-
return `${Math.round(count / 1000)}k`;
|
|
266
|
-
}
|
|
267
|
-
return `${(count / 1_000_000).toFixed(1)}M`;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
function formatUsageStats(usage: UsageStats, model?: string): string {
|
|
271
|
-
const parts: string[] = [];
|
|
272
|
-
if (usage.turns) {
|
|
273
|
-
parts.push(`${usage.turns} turn${usage.turns > 1 ? "s" : ""}`);
|
|
274
|
-
}
|
|
275
|
-
if (usage.cost) {
|
|
276
|
-
parts.push(`$${usage.cost.toFixed(4)}`);
|
|
277
|
-
}
|
|
278
|
-
if (usage.contextTokens > 0) {
|
|
279
|
-
parts.push(`ctx:${formatTokens(usage.contextTokens)}`);
|
|
280
|
-
}
|
|
281
|
-
if (model) {
|
|
282
|
-
parts.push(model);
|
|
283
|
-
}
|
|
284
|
-
return parts.join(" ");
|
|
285
|
-
}
|
|
286
|
-
|
|
287
223
|
/** 子 agent 结果的折叠面板 markdown:父 agent 的 prompt 与父 agent 看到的结果。 */
|
|
288
224
|
function formatPendantMarkdown(task: string, response: string): string {
|
|
289
225
|
return `# prompt:\n${task.trim()}\n# response\n${response.trim()}`;
|
|
@@ -375,10 +311,6 @@ export function resolveModel(
|
|
|
375
311
|
|
|
376
312
|
type OnUpdateCallback = (partial: AgentToolResult<SubagentDetails>) => void;
|
|
377
313
|
|
|
378
|
-
function toolSegment(name: string, count: number): string {
|
|
379
|
-
return count > 1 ? `${name} x ${count}` : name;
|
|
380
|
-
}
|
|
381
|
-
|
|
382
314
|
/** The subset of AgentSession runAgent relies on (injectable for tests). */
|
|
383
315
|
export interface SubagentSession {
|
|
384
316
|
agent: { state: { messages: AgentMessage[] } };
|
|
@@ -478,65 +410,11 @@ export async function runAgent(
|
|
|
478
410
|
// fallback 到默认模型,回显配置原文会掩盖这次 fallback。
|
|
479
411
|
result.model = session.model?.id;
|
|
480
412
|
|
|
481
|
-
|
|
482
|
-
// 工具调用行合并:连续的 tool_execution_start 事件合并在同一 `tool:` 行
|
|
483
|
-
// (如 `tool: read x 2, glob`),相同工具名连续出现时计为 `name x N`,
|
|
484
|
-
// 不同名按调用顺序罗列;只有写入日志行的事件(text 块)打断合并,thinking
|
|
485
|
-
// 不写日志行、也不打断合并。
|
|
486
|
-
let toolLineSegments: string[] = [];
|
|
487
|
-
let toolLine: { name: string; count: number } | undefined;
|
|
488
|
-
// 思考中状态:thinking_start 打开、thinking_delta 累计字符数、thinking_end
|
|
489
|
-
// 关闭;非 undefined 时 emitUpdate 在 footer 上方插一行瞬态 thinking 状态。
|
|
490
|
-
let thinkingChars: number | undefined;
|
|
491
|
-
|
|
492
|
-
const pushLogLine = (line: string) => {
|
|
493
|
-
logLines.push(line);
|
|
494
|
-
if (logLines.length > MAX_PROGRESS_LINES) {
|
|
495
|
-
logLines = logLines.slice(-MAX_PROGRESS_LINES);
|
|
496
|
-
}
|
|
497
|
-
// 写进日志的新行都会打断工具调用合并,下一批调用另起一行。
|
|
498
|
-
toolLine = undefined;
|
|
499
|
-
};
|
|
500
|
-
|
|
501
|
-
const appendToolLine = (rawName: string) => {
|
|
502
|
-
const name = sanitizeProgressLine(rawName);
|
|
503
|
-
const firstInBatch = toolLine === undefined;
|
|
504
|
-
if (toolLine === undefined) {
|
|
505
|
-
toolLineSegments = [];
|
|
506
|
-
toolLine = { name, count: 1 };
|
|
507
|
-
} else if (toolLine.name === name) {
|
|
508
|
-
toolLine.count++;
|
|
509
|
-
} else {
|
|
510
|
-
toolLineSegments.push(toolSegment(toolLine.name, toolLine.count));
|
|
511
|
-
toolLine = { name, count: 1 };
|
|
512
|
-
}
|
|
513
|
-
const parts = [...toolLineSegments, toolSegment(toolLine.name, toolLine.count)].join(", ");
|
|
514
|
-
const line = `tool: ${foldProgressLine(parts)}`;
|
|
515
|
-
if (firstInBatch) {
|
|
516
|
-
logLines.push(line);
|
|
517
|
-
if (logLines.length > MAX_PROGRESS_LINES) {
|
|
518
|
-
logLines = logLines.slice(-MAX_PROGRESS_LINES);
|
|
519
|
-
}
|
|
520
|
-
} else {
|
|
521
|
-
logLines[logLines.length - 1] = line;
|
|
522
|
-
}
|
|
523
|
-
emitUpdate();
|
|
524
|
-
};
|
|
413
|
+
const progress = createSubagentProgress({ name: result.agent });
|
|
525
414
|
|
|
526
415
|
const emitUpdate = () => {
|
|
527
|
-
// 最后一行固定是「子代理名 + 运行中统计」:名字用 code span 标出,进度流里
|
|
528
|
-
// 一眼能看出属于哪个 subagent;usage 与它同行,TUI 始终能看到实时 token 开销。
|
|
529
|
-
// 这行位于滚动窗口之外,因此永远不会被挤掉。
|
|
530
|
-
const usageLine = formatUsageStats(result.usage, result.model);
|
|
531
|
-
const name = sanitizeProgressLine(result.agent);
|
|
532
|
-
const footer = usageLine ? `\`${name}\` ${usageLine}` : `\`${name}\``;
|
|
533
|
-
const lines = [...logLines];
|
|
534
|
-
if (thinkingChars !== undefined) {
|
|
535
|
-
lines.push(`thinking ( ${thinkingChars} chars )`);
|
|
536
|
-
}
|
|
537
|
-
lines.push(footer);
|
|
538
416
|
onUpdate?.({
|
|
539
|
-
content: [{ type: "text", text:
|
|
417
|
+
content: [{ type: "text", text: progress.render(result.usage, result.model) }],
|
|
540
418
|
details: {},
|
|
541
419
|
});
|
|
542
420
|
};
|
|
@@ -550,24 +428,24 @@ export async function runAgent(
|
|
|
550
428
|
const delta = event.assistantMessageEvent;
|
|
551
429
|
switch (delta.type) {
|
|
552
430
|
case "text_end": {
|
|
553
|
-
|
|
431
|
+
progress.noteTextBlock(delta.content);
|
|
554
432
|
emitUpdate();
|
|
555
433
|
break;
|
|
556
434
|
}
|
|
557
435
|
case "thinking_start": {
|
|
558
436
|
// thinking 不产生日志行,因此不打断工具行合并:跨轮次的连续工具调用
|
|
559
437
|
// 仍累加到同一 `tool:` 行。
|
|
560
|
-
|
|
438
|
+
progress.thinkingStart();
|
|
561
439
|
emitUpdate();
|
|
562
440
|
break;
|
|
563
441
|
}
|
|
564
442
|
case "thinking_delta": {
|
|
565
|
-
|
|
443
|
+
progress.thinkingDelta(delta.delta.length);
|
|
566
444
|
emitUpdate();
|
|
567
445
|
break;
|
|
568
446
|
}
|
|
569
447
|
case "thinking_end": {
|
|
570
|
-
|
|
448
|
+
progress.thinkingEnd();
|
|
571
449
|
emitUpdate();
|
|
572
450
|
break;
|
|
573
451
|
}
|
|
@@ -577,7 +455,8 @@ export async function runAgent(
|
|
|
577
455
|
break;
|
|
578
456
|
}
|
|
579
457
|
case "tool_execution_start": {
|
|
580
|
-
|
|
458
|
+
progress.noteToolCall(event.toolName);
|
|
459
|
+
emitUpdate();
|
|
581
460
|
break;
|
|
582
461
|
}
|
|
583
462
|
case "message_end": {
|