@restranger/repi-subagents 0.0.0-stage → 0.0.1-ca9b82c
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/README.md +277 -2
- package/package.json +40 -5
- package/src/agent-file.ts +297 -0
- package/src/agent-schema.ts +90 -0
- package/src/child-guard.ts +151 -0
- package/src/command.ts +256 -0
- package/src/default-agents.ts +409 -0
- package/src/discovery.ts +354 -0
- package/src/extensions.ts +119 -0
- package/src/fleet-ui.ts +472 -0
- package/src/format.ts +974 -0
- package/src/frontmatter.ts +302 -0
- package/src/index.ts +112 -0
- package/src/manager.ts +1192 -0
- package/src/model-resolver.ts +58 -0
- package/src/overrides.ts +693 -0
- package/src/plugin.ts +568 -0
- package/src/prompt.ts +184 -0
- package/src/registry.ts +71 -0
- package/src/session.ts +304 -0
- package/src/status-widget.ts +65 -0
- package/src/tool-policy.ts +77 -0
- package/src/tools.ts +554 -0
- package/src/types.ts +163 -0
package/src/fleet-ui.ts
ADDED
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
import { ListOverlay } from "@restranger/repi-lib";
|
|
2
|
+
import {
|
|
3
|
+
alignRight,
|
|
4
|
+
boldText,
|
|
5
|
+
fitWidth,
|
|
6
|
+
formatActivity,
|
|
7
|
+
formatFleetElapsed,
|
|
8
|
+
paintIdentity,
|
|
9
|
+
sanitize,
|
|
10
|
+
statusColor,
|
|
11
|
+
truncate,
|
|
12
|
+
wrapDisplay,
|
|
13
|
+
} from "./format.js";
|
|
14
|
+
import type { SubagentRunSnapshot } from "./types.js";
|
|
15
|
+
|
|
16
|
+
export interface FleetViewOptions {
|
|
17
|
+
/** Always read fresh state; the manager owns the run table. */
|
|
18
|
+
readonly runs: () => readonly SubagentRunSnapshot[];
|
|
19
|
+
readonly now?: () => number;
|
|
20
|
+
readonly onCancel?: (id: string) => void;
|
|
21
|
+
/** Rows the component may occupy; defaults to the attached terminal height, 85% of it is used. */
|
|
22
|
+
readonly maxRows?: number;
|
|
23
|
+
/** Initial roster selection, used when the widget opens the inspector. */
|
|
24
|
+
readonly initialId?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const MIN_WIDTH = 36;
|
|
28
|
+
/** Below this the roster and the detail pane cannot both fit, so the view degrades to a notice. */
|
|
29
|
+
const MIN_ROWS = 8;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Memoized detail pane. Sanitizing, truncating and wrapping the whole transcript on every animation
|
|
33
|
+
* tick (12.5 Hz) froze the host on large child results, so the wrapped body is only rebuilt when one
|
|
34
|
+
* of its inputs actually changes.
|
|
35
|
+
*/
|
|
36
|
+
interface DetailCache {
|
|
37
|
+
readonly runId: string;
|
|
38
|
+
readonly width: number;
|
|
39
|
+
readonly showTools: boolean;
|
|
40
|
+
readonly pendingStop?: string;
|
|
41
|
+
readonly notice?: string;
|
|
42
|
+
readonly status: string;
|
|
43
|
+
readonly steps: number;
|
|
44
|
+
readonly activity: string;
|
|
45
|
+
readonly result?: string;
|
|
46
|
+
readonly error?: string;
|
|
47
|
+
readonly wrapped: string[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Fleet inspector for `/subagents fleet`, rendering the same frame, roster, pinned header and footer as
|
|
52
|
+
* `pi-subagents`' `SubagentFleetComponent`:
|
|
53
|
+
*
|
|
54
|
+
* ```text
|
|
55
|
+
* ╭──────────────────────────────────────────────────────────────╮
|
|
56
|
+
* │ Subagent fleet inspector · live controls ● scout · run │
|
|
57
|
+
* ├──────────────┬───────────────────────────────────────────────┤
|
|
58
|
+
* │ › ● scout · │ ● scout running │
|
|
59
|
+
* │ ● worker · │ background · a1b2c3d4 · single │
|
|
60
|
+
* ...
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* Pi overlays are opened at 95% width and 85% height by the plugin. The component is deliberately
|
|
64
|
+
* plain: it only needs the structural theme/TUI contract, so it works in tests and in Pi without
|
|
65
|
+
* importing host packages.
|
|
66
|
+
*/
|
|
67
|
+
export class FleetView extends ListOverlay {
|
|
68
|
+
private showTools = false;
|
|
69
|
+
private pendingStop?: string;
|
|
70
|
+
private notice?: string;
|
|
71
|
+
private detailCache?: DetailCache;
|
|
72
|
+
/** Wrapped transcript lines by source line, so a streaming result only re-wraps what changed. */
|
|
73
|
+
private readonly lineCache = new Map<string, string[]>();
|
|
74
|
+
|
|
75
|
+
constructor(private readonly options: FleetViewOptions) {
|
|
76
|
+
super({ maxRows: options.maxRows });
|
|
77
|
+
const initial = options.initialId ? options.runs().findIndex((run) => run.id === options.initialId) : -1;
|
|
78
|
+
if (initial >= 0) {
|
|
79
|
+
this.selected = initial;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
protected itemCount(): number {
|
|
84
|
+
return this.options.runs().length;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Changing the selected run cancels a pending stop confirmation. */
|
|
88
|
+
protected onSelectionChanged(): void {
|
|
89
|
+
this.pendingStop = undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
handleInput(data: string): void {
|
|
93
|
+
const runs = this.options.runs();
|
|
94
|
+
const current = runs[this.selected];
|
|
95
|
+
|
|
96
|
+
if (this.pendingStop) {
|
|
97
|
+
if (data.toLowerCase() === "y" || data === "\r" || data === "\n" || this.matches(data, "tui.select.confirm")) {
|
|
98
|
+
const id = this.pendingStop;
|
|
99
|
+
this.pendingStop = undefined;
|
|
100
|
+
this.notice = undefined;
|
|
101
|
+
try {
|
|
102
|
+
this.options.onCancel?.(id);
|
|
103
|
+
} catch {
|
|
104
|
+
// Cancellation is best-effort; the next manager change repairs the row.
|
|
105
|
+
}
|
|
106
|
+
this.tui?.requestRender();
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (data.toLowerCase() === "n" || data === "\x1b" || this.matches(data, "tui.select.cancel")) {
|
|
110
|
+
this.pendingStop = undefined;
|
|
111
|
+
this.tui?.requestRender();
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (data === "q" || this.matches(data, "tui.select.cancel")) {
|
|
118
|
+
this.close();
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (this.matches(data, "tui.select.up") || data === "\x1b[A" || data === "k") {
|
|
122
|
+
this.move(-1);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (this.matches(data, "tui.select.down") || data === "\x1b[B" || data === "j") {
|
|
126
|
+
this.move(1);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (data === "\x1b[H" || data === "g") {
|
|
130
|
+
this.select(0);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (data === "\x1b[F" || data === "G") {
|
|
134
|
+
this.select(runs.length - 1);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (data === "K") {
|
|
138
|
+
this.scrollDetail(-1);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (data === "J") {
|
|
142
|
+
this.scrollDetail(1);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (data === "\x1b[5~" || this.matches(data, "tui.select.pageUp")) {
|
|
146
|
+
this.scrollDetail(-this.bodyRows());
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (data === "\x1b[6~" || this.matches(data, "tui.select.pageDown")) {
|
|
150
|
+
this.scrollDetail(this.bodyRows());
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (data === "r" || data === "R") {
|
|
154
|
+
this.refresh();
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (data === "x" || data === "X" || data === "\x0f") {
|
|
158
|
+
this.showTools = !this.showTools;
|
|
159
|
+
this.detailScroll = 0;
|
|
160
|
+
this.tui?.requestRender();
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (data === "\r" || data === "\n" || this.matches(data, "tui.select.confirm")) {
|
|
164
|
+
this.showTools = !this.showTools;
|
|
165
|
+
this.detailScroll = 0;
|
|
166
|
+
this.tui?.requestRender();
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (data === "D") {
|
|
170
|
+
if (current && (current.status === "running" || current.status === "queued")) {
|
|
171
|
+
this.pendingStop = current.id;
|
|
172
|
+
this.notice = undefined;
|
|
173
|
+
this.detailScroll = 0;
|
|
174
|
+
this.tui?.requestRender();
|
|
175
|
+
}
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
render(width: number): string[] {
|
|
181
|
+
const runs = this.options.runs();
|
|
182
|
+
this.clampSelection();
|
|
183
|
+
this.clearStaleStop(runs);
|
|
184
|
+
const terminalWidth = Math.max(1, Math.floor(width));
|
|
185
|
+
const lines = this.renderFrame(runs, terminalWidth);
|
|
186
|
+
return lines.map((line) => truncate(line, terminalWidth));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
private renderFrame(runs: readonly SubagentRunSnapshot[], width: number): string[] {
|
|
190
|
+
if (width < MIN_WIDTH) {
|
|
191
|
+
return [truncate("Subagent fleet needs at least 36 columns. Esc closes.", width)];
|
|
192
|
+
}
|
|
193
|
+
const rows = this.rowsBudget();
|
|
194
|
+
if (rows < MIN_ROWS) {
|
|
195
|
+
return [truncate(`Subagent fleet needs at least ${MIN_ROWS} terminal rows. Esc closes.`, width)];
|
|
196
|
+
}
|
|
197
|
+
const bodyHeight = this.bodyHeightFor(rows);
|
|
198
|
+
const innerWidth = width - 2;
|
|
199
|
+
const rosterWidth = Math.max(22, Math.min(46, Math.floor((width - 3) * 0.38)));
|
|
200
|
+
const detailWidth = Math.max(1, width - rosterWidth - 3);
|
|
201
|
+
const selected = runs[this.selected];
|
|
202
|
+
const header = this.detailHeader(selected, detailWidth).slice(0, Math.max(0, bodyHeight - 1));
|
|
203
|
+
const outputBodyHeight = Math.max(0, bodyHeight - header.length);
|
|
204
|
+
const wrapped = this.detailWrapped(selected, detailWidth);
|
|
205
|
+
const visible = [...header, ...this.windowBody(wrapped, outputBodyHeight)];
|
|
206
|
+
|
|
207
|
+
const frame: string[] = [
|
|
208
|
+
this.border(`╭${"─".repeat(innerWidth)}╮`),
|
|
209
|
+
`${this.border("│")}${alignRight(this.title(), this.titleRight(selected), innerWidth)}${this.border("│")}`,
|
|
210
|
+
this.border(`├${"─".repeat(rosterWidth)}┬${"─".repeat(detailWidth)}┤`),
|
|
211
|
+
];
|
|
212
|
+
const roster = this.rosterLines(runs, rosterWidth, bodyHeight);
|
|
213
|
+
for (let index = 0; index < bodyHeight; index++) {
|
|
214
|
+
frame.push(
|
|
215
|
+
`${this.border("│")}${fitWidth(roster[index] ?? "", rosterWidth)}${this.border("│")}${fitWidth(visible[index] ?? "", detailWidth)}${this.border("│")}`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
frame.push(
|
|
219
|
+
this.border(`├${"─".repeat(rosterWidth)}┴${"─".repeat(detailWidth)}┤`),
|
|
220
|
+
`${this.border("│")}${fitWidth(this.footer(runs), innerWidth)}${this.border("│")}`,
|
|
221
|
+
this.border(`╰${"─".repeat(innerWidth)}╯`),
|
|
222
|
+
);
|
|
223
|
+
return frame;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
private title(): string {
|
|
227
|
+
return ` ${boldText(this.theme, "Subagent fleet inspector")} ${this.paint("dim", "· live controls")}`;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private titleRight(run: SubagentRunSnapshot | undefined): string {
|
|
231
|
+
if (!run) {
|
|
232
|
+
return this.paint("dim", "no children ");
|
|
233
|
+
}
|
|
234
|
+
return `${this.glyph(run)} ${paintIdentity(run.agent, this.theme)} · ${run.status} `;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private rosterLines(runs: readonly SubagentRunSnapshot[], width: number, bodyHeight: number): string[] {
|
|
238
|
+
if (runs.length === 0) {
|
|
239
|
+
return [this.paint("dim", "No tracked children")];
|
|
240
|
+
}
|
|
241
|
+
const start = Math.max(0, Math.min(this.selected - bodyHeight + 1, Math.max(0, runs.length - bodyHeight)));
|
|
242
|
+
const lines: string[] = [];
|
|
243
|
+
for (let offset = 0; offset < bodyHeight; offset++) {
|
|
244
|
+
const index = start + offset;
|
|
245
|
+
const run = runs[index];
|
|
246
|
+
if (!run) {
|
|
247
|
+
lines.push("");
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
const marker = index === this.selected ? this.paint("accent", "›") : " ";
|
|
251
|
+
const agent = index === this.selected ? boldText(this.theme, sanitize(run.agent)) : sanitize(run.agent);
|
|
252
|
+
const left = `${marker} ${this.glyph(run)} ${agent} ${this.paint("dim", `· ${sanitize(run.id).slice(0, 8)}`)}`;
|
|
253
|
+
lines.push(alignRight(left, this.paint("dim", sanitize(run.status)), width));
|
|
254
|
+
}
|
|
255
|
+
return lines;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** The host asks for a fresh frame when a child pushes an update; drop the memoized body. */
|
|
259
|
+
invalidate(): void {
|
|
260
|
+
this.detailCache = undefined;
|
|
261
|
+
this.lineCache.clear();
|
|
262
|
+
super.invalidate();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Sanitize + wrap one source line, memoized.
|
|
267
|
+
*
|
|
268
|
+
* A streaming result keeps every line but the last unchanged, so without this cache the whole
|
|
269
|
+
* transcript was sanitized and wrapped again on every animation tick (12.5 Hz), freezing the host.
|
|
270
|
+
*/
|
|
271
|
+
private wrapCached(line: string, width: number): string[] {
|
|
272
|
+
const key = `${width}\u0000${line}`;
|
|
273
|
+
const cached = this.lineCache.get(key);
|
|
274
|
+
if (cached) {
|
|
275
|
+
return cached;
|
|
276
|
+
}
|
|
277
|
+
const wrapped = wrapDisplay(sanitize(line), width);
|
|
278
|
+
if (this.lineCache.size > 4000) {
|
|
279
|
+
this.lineCache.clear();
|
|
280
|
+
}
|
|
281
|
+
this.lineCache.set(key, wrapped);
|
|
282
|
+
return wrapped;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** Wrapped transcript for the detail pane; rebuilt only when an input changed. */
|
|
286
|
+
private detailWrapped(run: SubagentRunSnapshot | undefined, width: number): string[] {
|
|
287
|
+
const cache = this.detailCache;
|
|
288
|
+
const activity = run?.activity ? `${run.activity.tool}|${run.activity.detail ?? ""}` : "";
|
|
289
|
+
if (
|
|
290
|
+
cache &&
|
|
291
|
+
run &&
|
|
292
|
+
cache.runId === run.id &&
|
|
293
|
+
cache.width === width &&
|
|
294
|
+
cache.showTools === this.showTools &&
|
|
295
|
+
cache.pendingStop === this.pendingStop &&
|
|
296
|
+
cache.notice === this.notice &&
|
|
297
|
+
cache.status === run.status &&
|
|
298
|
+
cache.steps === (run.steps?.length ?? 0) &&
|
|
299
|
+
cache.activity === activity &&
|
|
300
|
+
cache.result === run.result &&
|
|
301
|
+
cache.error === run.error
|
|
302
|
+
) {
|
|
303
|
+
return cache.wrapped;
|
|
304
|
+
}
|
|
305
|
+
const wrapped = this.detailBody(run).flatMap((line) => this.wrapCached(line, Math.max(1, width)));
|
|
306
|
+
this.detailCache = {
|
|
307
|
+
runId: run?.id ?? "",
|
|
308
|
+
width,
|
|
309
|
+
showTools: this.showTools,
|
|
310
|
+
pendingStop: this.pendingStop,
|
|
311
|
+
notice: this.notice,
|
|
312
|
+
status: run?.status ?? "",
|
|
313
|
+
steps: run?.steps?.length ?? 0,
|
|
314
|
+
activity,
|
|
315
|
+
result: run?.result,
|
|
316
|
+
error: run?.error,
|
|
317
|
+
wrapped,
|
|
318
|
+
};
|
|
319
|
+
return wrapped;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
private detailHeader(run: SubagentRunSnapshot | undefined, width: number): string[] {
|
|
323
|
+
if (!run) {
|
|
324
|
+
return [];
|
|
325
|
+
}
|
|
326
|
+
const now = this.options.now?.() ?? Date.now();
|
|
327
|
+
const source = run.background
|
|
328
|
+
? "background"
|
|
329
|
+
: run.status === "running" || run.status === "queued"
|
|
330
|
+
? "foreground · live"
|
|
331
|
+
: "foreground · recent";
|
|
332
|
+
const identity = `${source} · ${sanitize(run.id).slice(0, 8)} · single`;
|
|
333
|
+
const stats = this.stats(run, now);
|
|
334
|
+
const summary = run.task.replace(/\s+/g, " ").trim();
|
|
335
|
+
return [
|
|
336
|
+
alignRight(
|
|
337
|
+
` ${this.glyph(run)} ${boldText(this.theme, paintIdentity(run.agent, this.theme))}`,
|
|
338
|
+
this.paint("dim", sanitize(run.status)),
|
|
339
|
+
width,
|
|
340
|
+
),
|
|
341
|
+
` ${this.paint("dim", identity)}`,
|
|
342
|
+
` ${this.paint("muted", stats.join(" · "))}`,
|
|
343
|
+
...(summary ? [` ${this.paint("muted", `Task: ${truncate(summary, 160)}`)}`] : []),
|
|
344
|
+
`${this.paint("accent", "Conversation")} ${this.paint("dim", `· ${sanitize(run.status)}`)}`,
|
|
345
|
+
].map((line) => truncate(line, width));
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Logical (unwrapped) transcript lines for the detail pane. */
|
|
349
|
+
private detailBody(run: SubagentRunSnapshot | undefined): string[] {
|
|
350
|
+
if (!run) {
|
|
351
|
+
return [
|
|
352
|
+
"No current-session Fleet jobs.",
|
|
353
|
+
"",
|
|
354
|
+
"New jobs appear here automatically while this inspector remains open.",
|
|
355
|
+
];
|
|
356
|
+
}
|
|
357
|
+
const now = this.options.now?.() ?? Date.now();
|
|
358
|
+
const body: string[] = [];
|
|
359
|
+
if (this.pendingStop === run.id) {
|
|
360
|
+
body.push(
|
|
361
|
+
this.paint("warning", `Confirm stop for async run ${sanitize(run.id)}?`),
|
|
362
|
+
this.paint("dim", "Stop ends the run. Enter/Y confirms · N returns · Esc cancels"),
|
|
363
|
+
"",
|
|
364
|
+
);
|
|
365
|
+
} else if (this.notice) {
|
|
366
|
+
body.push(this.paint("dim", this.notice), "");
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const activity = run.status === "running" ? formatActivity(run.activity, now) : undefined;
|
|
370
|
+
if (activity) {
|
|
371
|
+
body.push(` ${activity}`);
|
|
372
|
+
}
|
|
373
|
+
if (this.showTools) {
|
|
374
|
+
for (const step of (run.steps ?? []).slice(-200)) {
|
|
375
|
+
const detail = step.detail ? ` ${sanitize(step.detail)}` : "";
|
|
376
|
+
const duration = step.durationMs !== undefined ? ` (${formatFleetElapsed(step.durationMs)})` : "";
|
|
377
|
+
const icon = step.isError ? "✗" : step.finishedAt !== undefined ? "✓" : "↳";
|
|
378
|
+
body.push(` ${icon} ${sanitize(step.tool)}${detail}${duration}`);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (run.error) {
|
|
382
|
+
body.push(
|
|
383
|
+
this.paint("error", "Error:"),
|
|
384
|
+
...sanitize(run.error)
|
|
385
|
+
.split("\n")
|
|
386
|
+
.slice(0, 40)
|
|
387
|
+
.map((line) => ` ${line}`),
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
if (run.result) {
|
|
391
|
+
const resultLines = run.result.split("\n");
|
|
392
|
+
body.push(this.paint("muted", "Output:"));
|
|
393
|
+
// Sanitizing and wrapping happen per line in `wrapCached`, where unchanged lines are reused.
|
|
394
|
+
body.push(...resultLines.slice(0, 400).map((line) => ` ${line.slice(0, 8_000)}`));
|
|
395
|
+
if (resultLines.length > 400) {
|
|
396
|
+
body.push(` ${this.paint("dim", `… ${resultLines.length - 400} more lines`)}`);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
if (body.length === 0) {
|
|
400
|
+
body.push(
|
|
401
|
+
this.paint("dim", run.status === "running" ? "Waiting for the first transcript event…" : "(no output)"),
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
return body;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
private stats(run: SubagentRunSnapshot, now: number): string[] {
|
|
408
|
+
const tokens = run.usage?.total ?? ((run.usage?.input ?? 0) + (run.usage?.output ?? 0) || undefined);
|
|
409
|
+
const stats = [
|
|
410
|
+
run.model ? sanitize(run.model) : undefined,
|
|
411
|
+
tokens !== undefined ? `${formatTokenValue(tokens)} tok` : undefined,
|
|
412
|
+
`${run.toolCalls} tool${run.toolCalls === 1 ? "" : "s"}`,
|
|
413
|
+
formatStatDuration(Math.max(0, (run.finishedAt ?? now) - (run.startedAt ?? run.createdAt))),
|
|
414
|
+
];
|
|
415
|
+
return stats.filter((value): value is string => Boolean(value));
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
private footer(runs: readonly SubagentRunSnapshot[]): string {
|
|
419
|
+
if (this.pendingStop) {
|
|
420
|
+
return ` ${this.paint("warning", `Stop ${sanitize(this.pendingStop)}? Enter/Y confirm · N/Esc cancel · 1/${runs.length}`)} `;
|
|
421
|
+
}
|
|
422
|
+
const up = this.keyLabel("tui.select.up", "↑");
|
|
423
|
+
const down = this.keyLabel("tui.select.down", "↓");
|
|
424
|
+
const close = this.keyLabel("tui.select.cancel", "Esc");
|
|
425
|
+
const position = `${runs.length === 0 ? 0 : this.selected + 1}/${runs.length}`;
|
|
426
|
+
return ` ${this.paint("dim", `${up}/${down} agent · x tools · D stop · K/J scroll · r refresh · ${close} close · ${position}`)} `;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
private clearStaleStop(runs: readonly SubagentRunSnapshot[]): void {
|
|
430
|
+
if (!this.pendingStop) {
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
const run = runs.find((candidate) => candidate.id === this.pendingStop);
|
|
434
|
+
if (!run || (run.status !== "running" && run.status !== "queued")) {
|
|
435
|
+
this.pendingStop = undefined;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
private glyph(run: SubagentRunSnapshot): string {
|
|
440
|
+
const glyph =
|
|
441
|
+
run.status === "running"
|
|
442
|
+
? "●"
|
|
443
|
+
: run.status === "queued"
|
|
444
|
+
? "◦"
|
|
445
|
+
: run.status === "completed"
|
|
446
|
+
? "✓"
|
|
447
|
+
: run.status === "aborted"
|
|
448
|
+
? "■"
|
|
449
|
+
: "✗";
|
|
450
|
+
return this.paint(statusColor(run.status), glyph);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function formatTokenValue(total: number): string {
|
|
455
|
+
if (total < 1_000) {
|
|
456
|
+
return `${total}`;
|
|
457
|
+
}
|
|
458
|
+
if (total < 1_000_000) {
|
|
459
|
+
return `${(total / 1_000).toFixed(total < 10_000 ? 1 : 0)}k`;
|
|
460
|
+
}
|
|
461
|
+
return `${(total / 1_000_000).toFixed(1)}M`;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function formatStatDuration(ms: number): string {
|
|
465
|
+
if (ms < 1_000) {
|
|
466
|
+
return `${Math.round(ms)}ms`;
|
|
467
|
+
}
|
|
468
|
+
if (ms < 60_000) {
|
|
469
|
+
return `${(ms / 1_000).toFixed(1)}s`;
|
|
470
|
+
}
|
|
471
|
+
return `${Math.floor(ms / 60_000)}m${Math.floor((ms % 60_000) / 1_000)}s`;
|
|
472
|
+
}
|