pi-subagent-manager 0.8.0 → 0.10.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/README.md +2 -2
- package/docs/settings.md +12 -0
- package/docs/tools-and-ui.md +42 -5
- package/package.json +1 -1
- package/src/index.ts +54 -6
- package/src/orch/manager.ts +96 -0
- package/src/orch/runtime.ts +27 -0
- package/src/orch/transcript.ts +186 -0
- package/src/prefs/settings.ts +12 -1
- package/src/types.ts +30 -0
- package/src/ui/agent-navigation-editor.ts +195 -0
- package/src/ui/dialog.ts +14 -13
- package/src/ui/live-agent-view.ts +373 -0
- package/src/ui/settings-ui.ts +31 -0
- package/src/ui/status-ui.ts +273 -46
- package/src/ui/ui.ts +90 -22
package/src/ui/status-ui.ts
CHANGED
|
@@ -1,28 +1,41 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
ExtensionCommandContext,
|
|
3
|
+
ExtensionContext,
|
|
4
|
+
ExtensionUIContext,
|
|
3
5
|
Theme,
|
|
4
6
|
} from "@earendil-works/pi-coding-agent";
|
|
5
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
getKeybindings,
|
|
9
|
+
setKeybindings,
|
|
10
|
+
Key,
|
|
11
|
+
matchesKey,
|
|
12
|
+
type Keybinding,
|
|
13
|
+
} from "@earendil-works/pi-tui";
|
|
6
14
|
import {
|
|
7
15
|
canOpenDialog,
|
|
8
16
|
DIALOG_OPTIONS,
|
|
9
17
|
type DialogHost,
|
|
18
|
+
DialogSession,
|
|
19
|
+
scopeDialogContext,
|
|
20
|
+
dialogMenu,
|
|
21
|
+
dialogEditor,
|
|
10
22
|
dialogHeight,
|
|
11
23
|
dialogText,
|
|
12
24
|
frameDialog,
|
|
13
|
-
withDialogSession,
|
|
14
25
|
} from "./dialog.ts";
|
|
15
26
|
import type { ThreadService } from "../types.ts";
|
|
16
27
|
import { buildStatusTree, type StatusRow } from "./thread-tree.ts";
|
|
17
|
-
import { showThreads, threadMetrics } from "./ui.ts";
|
|
28
|
+
import { agentTypeBadge, showThreads, threadMetrics } from "./ui.ts";
|
|
29
|
+
import { LiveAgentView, fillViewport, type AgentViewportState } from "./live-agent-view.ts";
|
|
18
30
|
|
|
19
31
|
export { buildStatusTree, type StatusRow };
|
|
20
32
|
|
|
21
33
|
const ROOT = "/root";
|
|
34
|
+
const INACTIVE_STATES = new Set(["paused", "stopped", "failed", "completed"]);
|
|
22
35
|
const TITLE = "Agents status";
|
|
23
36
|
const TREE_TITLE = "Agents tree";
|
|
24
37
|
const FOOTER =
|
|
25
|
-
"Esc close · ↑↓ select · ←→ fold · PgUp/PgDn · Enter inspect · r refresh";
|
|
38
|
+
"Esc close · ↑↓ select · ←→ fold · PgUp/PgDn · Enter inspect · r refresh · Ctrl+C stop all";
|
|
26
39
|
|
|
27
40
|
function agentCount(threads: readonly { path?: string }[]): number {
|
|
28
41
|
const paths = new Set<string>();
|
|
@@ -47,9 +60,7 @@ function treeParent(rows: StatusRow[], index: number): StatusRow | undefined {
|
|
|
47
60
|
|
|
48
61
|
function firstChild(rows: StatusRow[], index: number): StatusRow | undefined {
|
|
49
62
|
const next = rows[index + 1];
|
|
50
|
-
return next && next.prefix.length > (rows[index]?.prefix.length ?? 0)
|
|
51
|
-
? next
|
|
52
|
-
: undefined;
|
|
63
|
+
return next && next.prefix.length > (rows[index]?.prefix.length ?? 0) ? next : undefined;
|
|
53
64
|
}
|
|
54
65
|
|
|
55
66
|
function label(row: StatusRow): string {
|
|
@@ -62,9 +73,22 @@ function label(row: StatusRow): string {
|
|
|
62
73
|
.join(" ");
|
|
63
74
|
}
|
|
64
75
|
|
|
76
|
+
export interface AgentTreeState {
|
|
77
|
+
selectedPath: string;
|
|
78
|
+
collapsed: Set<string>;
|
|
79
|
+
}
|
|
80
|
+
|
|
65
81
|
export class StatusDialog {
|
|
66
|
-
private
|
|
67
|
-
private
|
|
82
|
+
private state: AgentTreeState;
|
|
83
|
+
private get selectedPath(): string {
|
|
84
|
+
return this.state.selectedPath;
|
|
85
|
+
}
|
|
86
|
+
private set selectedPath(value: string) {
|
|
87
|
+
this.state.selectedPath = value;
|
|
88
|
+
}
|
|
89
|
+
private get collapsed(): Set<string> {
|
|
90
|
+
return this.state.collapsed;
|
|
91
|
+
}
|
|
68
92
|
private viewport = 1;
|
|
69
93
|
private timer: ReturnType<typeof setInterval> | undefined;
|
|
70
94
|
|
|
@@ -75,8 +99,13 @@ export class StatusDialog {
|
|
|
75
99
|
private done: (path: string | undefined) => void,
|
|
76
100
|
selected?: string,
|
|
77
101
|
private title = TITLE,
|
|
102
|
+
private navigation?: {
|
|
103
|
+
state: AgentTreeState;
|
|
104
|
+
actions(path: string): void;
|
|
105
|
+
fullscreen?: boolean;
|
|
106
|
+
},
|
|
78
107
|
) {
|
|
79
|
-
this.
|
|
108
|
+
this.state = navigation?.state ?? { selectedPath: selected || ROOT, collapsed: new Set() };
|
|
80
109
|
}
|
|
81
110
|
|
|
82
111
|
invalidate(): void {}
|
|
@@ -99,6 +128,22 @@ export class StatusDialog {
|
|
|
99
128
|
this.timer = undefined;
|
|
100
129
|
}
|
|
101
130
|
|
|
131
|
+
/** Stop every live agent. Stopping a subtree covers its descendants, so only top-most live paths are sent. */
|
|
132
|
+
private stopAll(): void {
|
|
133
|
+
const live = this.service
|
|
134
|
+
.list()
|
|
135
|
+
.filter((thread) => thread.path !== ROOT && (thread.state === "starting" || thread.state === "running"))
|
|
136
|
+
.map((thread) => thread.path);
|
|
137
|
+
const tops = live.filter((path) => !live.some((other) => path.startsWith(`${other}/`)));
|
|
138
|
+
for (const path of tops) {
|
|
139
|
+
void this.service
|
|
140
|
+
.stop(path)
|
|
141
|
+
.catch(() => {})
|
|
142
|
+
.finally(() => this.host.requestRender());
|
|
143
|
+
}
|
|
144
|
+
this.host.requestRender();
|
|
145
|
+
}
|
|
146
|
+
|
|
102
147
|
private rows(): StatusRow[] {
|
|
103
148
|
return buildStatusTree(this.service.list(), this.collapsed);
|
|
104
149
|
}
|
|
@@ -118,6 +163,10 @@ export class StatusDialog {
|
|
|
118
163
|
|
|
119
164
|
handleInput(data: string): void {
|
|
120
165
|
const keys = getKeybindings();
|
|
166
|
+
if (matchesKey(data, Key.ctrl("c"))) {
|
|
167
|
+
this.stopAll();
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
121
170
|
if (keys.matches(data, "tui.select.cancel")) {
|
|
122
171
|
this.done(undefined);
|
|
123
172
|
return;
|
|
@@ -127,7 +176,16 @@ export class StatusDialog {
|
|
|
127
176
|
const row = rows[index];
|
|
128
177
|
if (row) this.selectedPath = row.path;
|
|
129
178
|
if (keys.matches(data, "tui.select.confirm")) {
|
|
130
|
-
if (
|
|
179
|
+
if (this.navigation && row?.path === ROOT) this.done(undefined);
|
|
180
|
+
else if (row?.thread && row.path !== ROOT) this.done(row.path);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (data === "i" && this.navigation) {
|
|
184
|
+
if (row?.thread && row.path !== ROOT) this.navigation.actions(row.path);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (matchesKey(data, Key.ctrl("q")) && this.navigation) {
|
|
188
|
+
this.done(undefined);
|
|
131
189
|
return;
|
|
132
190
|
}
|
|
133
191
|
if (data === "r") {
|
|
@@ -135,15 +193,13 @@ export class StatusDialog {
|
|
|
135
193
|
return;
|
|
136
194
|
}
|
|
137
195
|
if (matchesKey(data, Key.left)) {
|
|
138
|
-
if (row?.hasChildren && !this.collapsed.has(row.path))
|
|
139
|
-
this.collapsed.add(row.path);
|
|
196
|
+
if (row?.hasChildren && !this.collapsed.has(row.path)) this.collapsed.add(row.path);
|
|
140
197
|
else if (row) {
|
|
141
198
|
const parent = treeParent(rows, index);
|
|
142
199
|
if (parent) this.selectedPath = parent.path;
|
|
143
200
|
}
|
|
144
201
|
} else if (matchesKey(data, Key.right)) {
|
|
145
|
-
if (row?.hasChildren && this.collapsed.has(row.path))
|
|
146
|
-
this.collapsed.delete(row.path);
|
|
202
|
+
if (row?.hasChildren && this.collapsed.has(row.path)) this.collapsed.delete(row.path);
|
|
147
203
|
else {
|
|
148
204
|
const child = row?.hasChildren ? firstChild(rows, index) : undefined;
|
|
149
205
|
if (child) this.selectedPath = child.path;
|
|
@@ -158,41 +214,58 @@ export class StatusDialog {
|
|
|
158
214
|
else if (matchesKey(data, Key.end)) next = Math.max(0, rows.length - 1);
|
|
159
215
|
else return;
|
|
160
216
|
this.selectedPath =
|
|
161
|
-
rows[Math.max(0, Math.min(rows.length - 1, next))]?.path ??
|
|
162
|
-
this.selectedPath;
|
|
217
|
+
rows[Math.max(0, Math.min(rows.length - 1, next))]?.path ?? this.selectedPath;
|
|
163
218
|
}
|
|
164
219
|
this.host.requestRender();
|
|
165
220
|
}
|
|
166
221
|
|
|
167
222
|
private line(row: StatusRow, selected: boolean): string {
|
|
168
|
-
const marker = row.hasChildren
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
if (
|
|
177
|
-
|
|
223
|
+
const marker = row.hasChildren ? (this.collapsed.has(row.path) ? "▸ " : "▾ ") : "";
|
|
224
|
+
const head = ` ${row.prefix}${selected ? "›" : " "} ${marker}`;
|
|
225
|
+
const thread = row.thread;
|
|
226
|
+
if (!thread) {
|
|
227
|
+
const text = `${head}${label(row)}`;
|
|
228
|
+
return selected ? this.theme.fg("accent", text) : text;
|
|
229
|
+
}
|
|
230
|
+
const inactive = INACTIVE_STATES.has(thread.state);
|
|
231
|
+
if (inactive) {
|
|
232
|
+
// Completed fades furthest (dim); the state word keeps a theme hint for done/failed.
|
|
233
|
+
const base = thread.state === "completed" ? "dim" : "muted";
|
|
234
|
+
const hint =
|
|
235
|
+
thread.state === "completed" ? "success" : thread.state === "failed" ? "error" : base;
|
|
236
|
+
const state = dialogText(thread.state);
|
|
237
|
+
const path = dialogText(thread.path);
|
|
238
|
+
const status = dialogText(thread.status);
|
|
239
|
+
return [
|
|
240
|
+
this.theme.fg(selected ? "accent" : base, head),
|
|
241
|
+
this.theme.fg(base, `${dialogText(thread.type)} ${path} `),
|
|
242
|
+
this.theme.fg(hint, state),
|
|
243
|
+
this.theme.fg(base, ` ${status}`),
|
|
244
|
+
].join("");
|
|
245
|
+
}
|
|
246
|
+
const rest = [thread.path, thread.state, thread.status]
|
|
247
|
+
.map((part) => dialogText(part))
|
|
248
|
+
.join(" ");
|
|
249
|
+
if (selected)
|
|
250
|
+
return `${this.theme.fg("accent", head)}${agentTypeBadge(thread.type, thread.color, this.theme)} ${this.theme.fg("accent", rest)}`;
|
|
251
|
+
return `${head}${agentTypeBadge(thread.type, thread.color, this.theme)} ${rest}`;
|
|
178
252
|
}
|
|
179
253
|
|
|
180
254
|
private detail(row: StatusRow | undefined): string[] {
|
|
181
255
|
if (!row?.thread) {
|
|
182
256
|
return [
|
|
183
|
-
` ${row?.path === ROOT ? "Main Pi session" : "Missing parent"}`,
|
|
257
|
+
` ${row?.path === ROOT ? (agentCount(this.service.list()) ? "Main Pi session" : "Main Pi session — No agents yet") : "Missing parent"}`,
|
|
184
258
|
"",
|
|
185
259
|
];
|
|
186
260
|
}
|
|
187
|
-
return [
|
|
188
|
-
` task ${dialogText(row.thread.task)}`,
|
|
189
|
-
` ${threadMetrics(row.thread)}`,
|
|
190
|
-
];
|
|
261
|
+
return [` task ${dialogText(row.thread.task)}`, ` ${threadMetrics(row.thread)}`];
|
|
191
262
|
}
|
|
192
263
|
|
|
193
264
|
render(width: number): string[] {
|
|
194
265
|
const rows = this.rows();
|
|
195
|
-
const height =
|
|
266
|
+
const height = this.navigation?.fullscreen
|
|
267
|
+
? Math.max(1, this.host.terminal?.rows ?? 24)
|
|
268
|
+
: dialogHeight(this.host);
|
|
196
269
|
const index = this.locate(rows);
|
|
197
270
|
if (rows[index]) this.selectedPath = rows[index].path;
|
|
198
271
|
const budget = Math.max(0, height - 4);
|
|
@@ -200,10 +273,7 @@ export class StatusDialog {
|
|
|
200
273
|
const treeCount = Math.max(0, budget - detail.length);
|
|
201
274
|
this.viewport = Math.max(1, treeCount);
|
|
202
275
|
const start = treeCount
|
|
203
|
-
? Math.max(
|
|
204
|
-
0,
|
|
205
|
-
Math.min(index - treeCount + 1, Math.max(0, rows.length - treeCount)),
|
|
206
|
-
)
|
|
276
|
+
? Math.max(0, Math.min(index - treeCount + 1, Math.max(0, rows.length - treeCount)))
|
|
207
277
|
: 0;
|
|
208
278
|
const body = [
|
|
209
279
|
...rows
|
|
@@ -217,7 +287,9 @@ export class StatusDialog {
|
|
|
217
287
|
height,
|
|
218
288
|
this.heading(rows, start, treeCount),
|
|
219
289
|
body,
|
|
220
|
-
|
|
290
|
+
this.navigation
|
|
291
|
+
? "Esc main · ↑↓ select · ←→ fold · i Actions · Ctrl+C stop all"
|
|
292
|
+
: FOOTER,
|
|
221
293
|
);
|
|
222
294
|
}
|
|
223
295
|
|
|
@@ -273,16 +345,171 @@ export async function showAgentStatus(
|
|
|
273
345
|
await showAgentDialog(ctx, service, { inspect });
|
|
274
346
|
}
|
|
275
347
|
|
|
348
|
+
/** Root-owned navigation state. Opening and closing never changes the executing session. */
|
|
349
|
+
export class AgentNavigationController {
|
|
350
|
+
private tree: AgentTreeState = { selectedPath: ROOT, collapsed: new Set() };
|
|
351
|
+
private viewports = new Map<string, AgentViewportState>();
|
|
352
|
+
private session: DialogSession | undefined;
|
|
353
|
+
private opening: Promise<void> | undefined;
|
|
354
|
+
private generation = 0;
|
|
355
|
+
|
|
356
|
+
get isOpen(): boolean {
|
|
357
|
+
return this.opening !== undefined;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
close(): void {
|
|
361
|
+
++this.generation;
|
|
362
|
+
this.session?.close();
|
|
363
|
+
this.session = undefined;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
open(ctx: ExtensionContext, service: ThreadService, selectedPath?: string): Promise<void> {
|
|
367
|
+
if (this.opening) return this.opening;
|
|
368
|
+
if (!canOpenDialog(ctx)) return Promise.resolve();
|
|
369
|
+
if (selectedPath) this.tree.selectedPath = selectedPath;
|
|
370
|
+
const generation = ++this.generation;
|
|
371
|
+
let failure: unknown;
|
|
372
|
+
let failed = false;
|
|
373
|
+
let ownedSession: DialogSession | undefined;
|
|
374
|
+
const task = ctx.ui.custom<void>(
|
|
375
|
+
(host, theme, keys, done) => {
|
|
376
|
+
const session = new DialogSession(host, theme, keys, done);
|
|
377
|
+
ownedSession = session;
|
|
378
|
+
if (generation !== this.generation) {
|
|
379
|
+
void Promise.resolve().then(() => session.close());
|
|
380
|
+
return session;
|
|
381
|
+
}
|
|
382
|
+
this.session = session;
|
|
383
|
+
// The alt-screen renderer's listener runs before extension listeners. Temporarily
|
|
384
|
+
// remove only its search action from the public keybinding lookup, before dispatch.
|
|
385
|
+
const previousKeybindings = getKeybindings();
|
|
386
|
+
const watchingKeybindings = new Proxy(previousKeybindings, {
|
|
387
|
+
get(target, property) {
|
|
388
|
+
if (property === "matches")
|
|
389
|
+
return (data: string, action: Keybinding) =>
|
|
390
|
+
action === "tui.altScreen.search" ? false : target.matches(data, action);
|
|
391
|
+
// Preserve all other public manager methods and their original receiver.
|
|
392
|
+
const value = Reflect.get(target, property, target);
|
|
393
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
394
|
+
},
|
|
395
|
+
});
|
|
396
|
+
setKeybindings(watchingKeybindings);
|
|
397
|
+
const dispose = session.dispose.bind(session);
|
|
398
|
+
session.dispose = () => {
|
|
399
|
+
// Do not overwrite a newer keybinding owner installed by another extension.
|
|
400
|
+
if (getKeybindings() === watchingKeybindings) setKeybindings(previousKeybindings);
|
|
401
|
+
dispose();
|
|
402
|
+
};
|
|
403
|
+
// One fullscreen surface also covers shorter Actions dialogs without exposing main.
|
|
404
|
+
const render = session.render.bind(session);
|
|
405
|
+
session.render = (width) => fillViewport(render(width), width, host.terminal.rows);
|
|
406
|
+
const scoped = scopeDialogContext(ctx, session);
|
|
407
|
+
// Built-in UI dialogs otherwise stack independently. Mount these actions in our child slot.
|
|
408
|
+
const ui = scoped.ui as ExtensionUIContext;
|
|
409
|
+
ui.select = (title, options) =>
|
|
410
|
+
dialogMenu(
|
|
411
|
+
scoped,
|
|
412
|
+
title,
|
|
413
|
+
options.map((label) => ({ id: label, label })),
|
|
414
|
+
);
|
|
415
|
+
ui.confirm = async (title, message) =>
|
|
416
|
+
(await dialogMenu(
|
|
417
|
+
scoped,
|
|
418
|
+
title,
|
|
419
|
+
[
|
|
420
|
+
{ id: "Yes", label: "Yes", help: message },
|
|
421
|
+
{ id: "No", label: "No", help: message },
|
|
422
|
+
],
|
|
423
|
+
{ selectedId: "No" },
|
|
424
|
+
)) === "Yes";
|
|
425
|
+
ui.editor = (title, prefill) => dialogEditor(scoped, title, prefill ?? "");
|
|
426
|
+
// Defer until Pi has installed/focused the overlay.
|
|
427
|
+
void Promise.resolve()
|
|
428
|
+
.then(async () => {
|
|
429
|
+
while (generation === this.generation) {
|
|
430
|
+
let treeDialog: StatusDialog | undefined;
|
|
431
|
+
const chosen = await session.mount<
|
|
432
|
+
{ kind: "watch" | "actions"; path: string } | undefined
|
|
433
|
+
>((tui, activeTheme, _keys, finish) => {
|
|
434
|
+
treeDialog = new StatusDialog(
|
|
435
|
+
tui,
|
|
436
|
+
activeTheme,
|
|
437
|
+
service,
|
|
438
|
+
(path) => finish(path ? { kind: "watch", path } : undefined),
|
|
439
|
+
undefined,
|
|
440
|
+
TREE_TITLE,
|
|
441
|
+
{
|
|
442
|
+
state: this.tree,
|
|
443
|
+
fullscreen: true,
|
|
444
|
+
actions: (path) => finish({ kind: "actions", path }),
|
|
445
|
+
},
|
|
446
|
+
);
|
|
447
|
+
treeDialog.startRefresh();
|
|
448
|
+
return treeDialog;
|
|
449
|
+
});
|
|
450
|
+
treeDialog?.dispose();
|
|
451
|
+
if (!chosen || generation !== this.generation) break;
|
|
452
|
+
if (chosen.kind === "actions") {
|
|
453
|
+
await showThreads(scoped, service, chosen.path);
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
let viewport = this.viewports.get(chosen.path);
|
|
457
|
+
if (!viewport) {
|
|
458
|
+
viewport = { scrollTop: 0, follow: true };
|
|
459
|
+
this.viewports.set(chosen.path, viewport);
|
|
460
|
+
}
|
|
461
|
+
const result = await session.mount<"back" | "main">(
|
|
462
|
+
(tui, activeTheme, _keys, finish) =>
|
|
463
|
+
new LiveAgentView(tui, activeTheme, service, chosen.path, viewport!, finish),
|
|
464
|
+
);
|
|
465
|
+
if (result !== "back") break;
|
|
466
|
+
}
|
|
467
|
+
})
|
|
468
|
+
.then(
|
|
469
|
+
() => session.close(),
|
|
470
|
+
(error: unknown) => {
|
|
471
|
+
failed = true;
|
|
472
|
+
failure = error;
|
|
473
|
+
ctx.ui.notify(
|
|
474
|
+
dialogText(error instanceof Error ? error.message : String(error)),
|
|
475
|
+
"error",
|
|
476
|
+
);
|
|
477
|
+
session.close();
|
|
478
|
+
},
|
|
479
|
+
);
|
|
480
|
+
return session;
|
|
481
|
+
},
|
|
482
|
+
{
|
|
483
|
+
overlay: true,
|
|
484
|
+
overlayOptions: { width: "100%", maxHeight: "100%", row: 0, col: 0, margin: 0 },
|
|
485
|
+
},
|
|
486
|
+
);
|
|
487
|
+
this.opening = task
|
|
488
|
+
.then(() => {
|
|
489
|
+
if (failed) throw failure;
|
|
490
|
+
})
|
|
491
|
+
.finally(() => {
|
|
492
|
+
// Also clean up if the host rejects mounting after our factory returned.
|
|
493
|
+
ownedSession?.dispose();
|
|
494
|
+
if (this.opening === result) this.opening = undefined;
|
|
495
|
+
if (generation === this.generation) this.session = undefined;
|
|
496
|
+
});
|
|
497
|
+
const result = this.opening;
|
|
498
|
+
return result;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
const navigationByService = new WeakMap<ThreadService, AgentNavigationController>();
|
|
503
|
+
|
|
276
504
|
export async function showAgentTree(
|
|
277
|
-
ctx:
|
|
505
|
+
ctx: ExtensionContext,
|
|
278
506
|
service: ThreadService,
|
|
279
507
|
selectedPath?: string,
|
|
280
508
|
): Promise<void> {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
);
|
|
509
|
+
let navigation = navigationByService.get(service);
|
|
510
|
+
if (!navigation) {
|
|
511
|
+
navigation = new AgentNavigationController();
|
|
512
|
+
navigationByService.set(service, navigation);
|
|
513
|
+
}
|
|
514
|
+
await navigation.open(ctx, service, selectedPath);
|
|
288
515
|
}
|
package/src/ui/ui.ts
CHANGED
|
@@ -38,6 +38,7 @@ import {
|
|
|
38
38
|
withDialogSession,
|
|
39
39
|
} from "./dialog.ts";
|
|
40
40
|
import { getModelPreferences } from "../prefs/models.ts";
|
|
41
|
+
import type { WidgetMode } from "../prefs/settings.ts";
|
|
41
42
|
import { buildStatusTree, type StatusRow } from "./thread-tree.ts";
|
|
42
43
|
import {
|
|
43
44
|
THINKING_LEVELS,
|
|
@@ -48,7 +49,7 @@ import {
|
|
|
48
49
|
|
|
49
50
|
export type ThreadController = Pick<
|
|
50
51
|
ThreadService,
|
|
51
|
-
"list" | "get" | "output" | "transcript" | "steer" | "stop"
|
|
52
|
+
"list" | "get" | "output" | "transcript" | "observeTranscript" | "steer" | "stop"
|
|
52
53
|
>;
|
|
53
54
|
|
|
54
55
|
/** Plain terminal-safe text: never pass agent-supplied terminal commands through. */
|
|
@@ -88,7 +89,7 @@ function contrastPill(
|
|
|
88
89
|
});
|
|
89
90
|
}
|
|
90
91
|
|
|
91
|
-
function agentTypeBadge(
|
|
92
|
+
export function agentTypeBadge(
|
|
92
93
|
type: string,
|
|
93
94
|
color: string | undefined,
|
|
94
95
|
theme: AgentBadgeTheme,
|
|
@@ -217,8 +218,33 @@ export function renderThreads(
|
|
|
217
218
|
}
|
|
218
219
|
|
|
219
220
|
const WIDGET_ROOT = "/root";
|
|
220
|
-
const MAX_WIDGET_LINES =
|
|
221
|
+
const MAX_WIDGET_LINES = 12;
|
|
221
222
|
const AGENT_WIDGET_PLACEMENT = { placement: "aboveEditor" as const };
|
|
223
|
+
const AGENT_BROWSER_HINT = "Press ← to open subagent browser";
|
|
224
|
+
|
|
225
|
+
interface AgentWidgetRenderOptions {
|
|
226
|
+
/** RPC widgets cannot open a terminal browser. */
|
|
227
|
+
showBrowserHint?: boolean;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function agentWidgetStatus(
|
|
231
|
+
running: number,
|
|
232
|
+
omitted: number,
|
|
233
|
+
width: number,
|
|
234
|
+
theme: Pick<Theme, "fg">,
|
|
235
|
+
showBrowserHint: boolean,
|
|
236
|
+
): string {
|
|
237
|
+
const details = [
|
|
238
|
+
omitted > 0 ? `+${omitted} more agents` : "",
|
|
239
|
+
showBrowserHint ? AGENT_BROWSER_HINT : "",
|
|
240
|
+
].filter(Boolean);
|
|
241
|
+
return truncateToWidth(
|
|
242
|
+
theme.fg("accent", `${running} running`) +
|
|
243
|
+
(details.length ? theme.fg("muted", ` · ${details.join(" · ")}`) : ""),
|
|
244
|
+
width,
|
|
245
|
+
"",
|
|
246
|
+
);
|
|
247
|
+
}
|
|
222
248
|
|
|
223
249
|
function statePriority(state: ThreadView["state"] | undefined): number {
|
|
224
250
|
if (state === "starting" || state === "running") return 0;
|
|
@@ -367,7 +393,8 @@ function takeWidgetRows(rows: StatusRow[], budget: number): StatusRow[] {
|
|
|
367
393
|
.sort(
|
|
368
394
|
(a, b) =>
|
|
369
395
|
statePriority(a.row.thread!.state) -
|
|
370
|
-
statePriority(b.row.thread!.state) ||
|
|
396
|
+
statePriority(b.row.thread!.state) ||
|
|
397
|
+
b.row.thread!.createdAt - a.row.thread!.createdAt || a.index - b.index,
|
|
371
398
|
);
|
|
372
399
|
const chosen = new Set<number>();
|
|
373
400
|
let used = 0;
|
|
@@ -402,6 +429,7 @@ export function renderAgentTree(
|
|
|
402
429
|
threads: ThreadView[],
|
|
403
430
|
width: number,
|
|
404
431
|
theme: AgentBadgeTheme,
|
|
432
|
+
{ showBrowserHint = true }: AgentWidgetRenderOptions = {},
|
|
405
433
|
): string[] {
|
|
406
434
|
const columns = Math.max(0, width);
|
|
407
435
|
const rows = buildStatusTree(
|
|
@@ -421,12 +449,9 @@ export function renderAgentTree(
|
|
|
421
449
|
theme.fg("muted", `${live} live · ${paused} paused`),
|
|
422
450
|
columns,
|
|
423
451
|
);
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
visible = takeWidgetRows(rows, MAX_WIDGET_LINES - 2);
|
|
428
|
-
omitted = agents.length - visible.filter((row) => row.thread).length;
|
|
429
|
-
}
|
|
452
|
+
// Reserve the last line for status and browser entry, even with no omissions.
|
|
453
|
+
const visible = takeWidgetRows(rows, MAX_WIDGET_LINES - 2);
|
|
454
|
+
const omitted = agents.length - visible.filter((row) => row.thread).length;
|
|
430
455
|
const lines = [heading];
|
|
431
456
|
for (const row of visible) {
|
|
432
457
|
if (!row.thread) {
|
|
@@ -436,32 +461,75 @@ export function renderAgentTree(
|
|
|
436
461
|
lines.push(agentLine(row, columns, theme));
|
|
437
462
|
lines.push(activityLine(row, columns, theme));
|
|
438
463
|
}
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
464
|
+
lines.push(agentWidgetStatus(live, omitted, columns, theme, showBrowserHint));
|
|
465
|
+
return lines;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** One-line counts for real agents; starting agents count as running, including tokens. */
|
|
469
|
+
export function renderAgentSummary(
|
|
470
|
+
threads: ThreadView[],
|
|
471
|
+
width: number,
|
|
472
|
+
theme: Pick<Theme, "fg">,
|
|
473
|
+
{ showBrowserHint = true }: AgentWidgetRenderOptions = {},
|
|
474
|
+
): string[] {
|
|
475
|
+
const agents = new Map<string, ThreadView>();
|
|
476
|
+
for (const thread of threads) {
|
|
477
|
+
if (thread?.path && thread.path !== WIDGET_ROOT && !agents.has(thread.path))
|
|
478
|
+
agents.set(thread.path, thread);
|
|
447
479
|
}
|
|
480
|
+
if (!agents.size) return [];
|
|
481
|
+
|
|
482
|
+
const counts: Record<Exclude<ThreadView["state"], "starting">, number> = {
|
|
483
|
+
running: 0,
|
|
484
|
+
stopped: 0,
|
|
485
|
+
failed: 0,
|
|
486
|
+
paused: 0,
|
|
487
|
+
completed: 0,
|
|
488
|
+
};
|
|
489
|
+
let input = 0;
|
|
490
|
+
let output = 0;
|
|
491
|
+
for (const thread of agents.values()) {
|
|
492
|
+
counts[thread.state === "starting" ? "running" : thread.state] += 1;
|
|
493
|
+
if (isLive(thread)) {
|
|
494
|
+
input += metricCount(thread.inputTokens);
|
|
495
|
+
output += metricCount(thread.outputTokens);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
const statuses: [keyof typeof counts, ThemeColor][] = [
|
|
499
|
+
["running", "accent"],
|
|
500
|
+
["stopped", "muted"],
|
|
501
|
+
["failed", "error"],
|
|
502
|
+
["paused", "warning"],
|
|
503
|
+
["completed", "success"],
|
|
504
|
+
];
|
|
505
|
+
const left = statuses
|
|
506
|
+
.filter(([state]) => state === "running" || counts[state] > 0)
|
|
507
|
+
.map(([state, color]) => theme.fg(color, `${counts[state]} ${state}`))
|
|
508
|
+
.join(theme.fg("muted", ", "));
|
|
509
|
+
const right = theme.fg("muted", `↑${formatCount(input)} ↓${formatCount(output)}`);
|
|
510
|
+
const columns = Math.max(0, width);
|
|
511
|
+
const lines = [fitLine(left, right, columns)];
|
|
512
|
+
if (showBrowserHint)
|
|
513
|
+
lines.push(truncateToWidth(theme.fg("muted", AGENT_BROWSER_HINT), columns, ""));
|
|
448
514
|
return lines;
|
|
449
515
|
}
|
|
450
516
|
|
|
451
517
|
export function updateWidget(
|
|
452
518
|
ctx: ExtensionContext,
|
|
453
519
|
threads: ThreadView[],
|
|
520
|
+
mode: WidgetMode = "full",
|
|
454
521
|
): void {
|
|
455
522
|
if (!ctx.hasUI) return;
|
|
456
523
|
if (!threads.some((thread) => thread.path !== "/root")) {
|
|
457
524
|
ctx.ui.setWidget("pi-subagent", undefined, AGENT_WIDGET_PLACEMENT);
|
|
458
525
|
return;
|
|
459
526
|
}
|
|
527
|
+
const render = mode === "minimal" ? renderAgentSummary : renderAgentTree;
|
|
460
528
|
// RPC hosts accept string widgets only; a component factory is ignored.
|
|
461
529
|
if (ctx.mode === "rpc") {
|
|
462
530
|
ctx.ui.setWidget(
|
|
463
531
|
"pi-subagent",
|
|
464
|
-
|
|
532
|
+
render(threads, 80, ctx.ui.theme, { showBrowserHint: false }),
|
|
465
533
|
AGENT_WIDGET_PLACEMENT,
|
|
466
534
|
);
|
|
467
535
|
return;
|
|
@@ -471,12 +539,12 @@ export function updateWidget(
|
|
|
471
539
|
"pi-subagent",
|
|
472
540
|
(tui) => {
|
|
473
541
|
let timer: ReturnType<typeof setInterval> | undefined;
|
|
474
|
-
if (snapshot.some(isLive)) {
|
|
542
|
+
if (mode === "full" && snapshot.some(isLive)) {
|
|
475
543
|
timer = setInterval(() => tui.requestRender(), 1000);
|
|
476
544
|
timer.unref();
|
|
477
545
|
}
|
|
478
546
|
return {
|
|
479
|
-
render: (width) =>
|
|
547
|
+
render: (width) => render(snapshot, width, ctx.ui.theme),
|
|
480
548
|
invalidate: () => {},
|
|
481
549
|
dispose: () => {
|
|
482
550
|
if (timer) clearInterval(timer);
|
|
@@ -489,7 +557,7 @@ export function updateWidget(
|
|
|
489
557
|
}
|
|
490
558
|
|
|
491
559
|
export async function showThreads(
|
|
492
|
-
ctx:
|
|
560
|
+
ctx: ExtensionContext,
|
|
493
561
|
controller: ThreadController,
|
|
494
562
|
path?: string,
|
|
495
563
|
): Promise<void> {
|