@timmyjl12/pi-subagents 0.0.0-stage → 0.20.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 +827 -0
- package/CONTRIBUTING.md +72 -0
- package/LICENSE +21 -0
- package/README.md +1079 -2
- package/SECURITY.md +97 -0
- package/dist/abortable.d.ts +12 -0
- package/dist/abortable.js +42 -0
- package/dist/active-agents-prompt.d.ts +18 -0
- package/dist/active-agents-prompt.js +34 -0
- package/dist/advisor-status.d.ts +16 -0
- package/dist/advisor-status.js +74 -0
- package/dist/agent-color.d.ts +35 -0
- package/dist/agent-color.js +123 -0
- package/dist/agent-file-toggle.d.ts +125 -0
- package/dist/agent-file-toggle.js +264 -0
- package/dist/agent-manager.d.ts +485 -0
- package/dist/agent-manager.js +1380 -0
- package/dist/agent-runner.d.ts +319 -0
- package/dist/agent-runner.js +1128 -0
- package/dist/agent-types.d.ts +119 -0
- package/dist/agent-types.js +286 -0
- package/dist/child-context.d.ts +15 -0
- package/dist/child-context.js +181 -0
- package/dist/child-session-shutdown.d.ts +3 -0
- package/dist/child-session-shutdown.js +48 -0
- package/dist/context.d.ts +12 -0
- package/dist/context.js +56 -0
- package/dist/cross-extension-rpc.d.ts +66 -0
- package/dist/cross-extension-rpc.js +138 -0
- package/dist/custom-agents.d.ts +54 -0
- package/dist/custom-agents.js +386 -0
- package/dist/default-agents.d.ts +7 -0
- package/dist/default-agents.js +125 -0
- package/dist/enabled-models.d.ts +49 -0
- package/dist/enabled-models.js +145 -0
- package/dist/env.d.ts +6 -0
- package/dist/env.js +28 -0
- package/dist/error-log.d.ts +58 -0
- package/dist/error-log.js +168 -0
- package/dist/group-join.d.ts +32 -0
- package/dist/group-join.js +116 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +3896 -0
- package/dist/invocation-config.d.ts +137 -0
- package/dist/invocation-config.js +129 -0
- package/dist/memory.d.ts +53 -0
- package/dist/memory.js +165 -0
- package/dist/mention-clone.d.ts +87 -0
- package/dist/mention-clone.js +178 -0
- package/dist/mention.d.ts +81 -0
- package/dist/mention.js +131 -0
- package/dist/model-resolver.d.ts +36 -0
- package/dist/model-resolver.js +95 -0
- package/dist/model-scope.d.ts +49 -0
- package/dist/model-scope.js +48 -0
- package/dist/nested-tools.d.ts +55 -0
- package/dist/nested-tools.js +299 -0
- package/dist/output-diagnostics.d.ts +11 -0
- package/dist/output-diagnostics.js +64 -0
- package/dist/output-file.d.ts +53 -0
- package/dist/output-file.js +248 -0
- package/dist/prompts.d.ts +61 -0
- package/dist/prompts.js +94 -0
- package/dist/recovery.d.ts +56 -0
- package/dist/recovery.js +341 -0
- package/dist/schedule-store.d.ts +44 -0
- package/dist/schedule-store.js +210 -0
- package/dist/schedule.d.ts +109 -0
- package/dist/schedule.js +359 -0
- package/dist/settings.d.ts +360 -0
- package/dist/settings.js +251 -0
- package/dist/skill-loader.d.ts +24 -0
- package/dist/skill-loader.js +93 -0
- package/dist/standalone-fallback.d.ts +30 -0
- package/dist/standalone-fallback.js +239 -0
- package/dist/status-note.d.ts +61 -0
- package/dist/status-note.js +85 -0
- package/dist/structured-output.d.ts +61 -0
- package/dist/structured-output.js +112 -0
- package/dist/terminal-progress.d.ts +115 -0
- package/dist/terminal-progress.js +206 -0
- package/dist/types.d.ts +382 -0
- package/dist/types.js +5 -0
- package/dist/ui/agent-mention.d.ts +82 -0
- package/dist/ui/agent-mention.js +187 -0
- package/dist/ui/agent-widget.d.ts +235 -0
- package/dist/ui/agent-widget.js +652 -0
- package/dist/ui/conversation-viewer.d.ts +105 -0
- package/dist/ui/conversation-viewer.js +540 -0
- package/dist/ui/fleet-list.d.ts +183 -0
- package/dist/ui/fleet-list.js +494 -0
- package/dist/ui/fleet-overlay.d.ts +172 -0
- package/dist/ui/fleet-overlay.js +691 -0
- package/dist/ui/native-transcript.d.ts +33 -0
- package/dist/ui/native-transcript.js +230 -0
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +94 -0
- package/dist/ui/select-item.d.ts +27 -0
- package/dist/ui/select-item.js +34 -0
- package/dist/ui/tool-progress.d.ts +22 -0
- package/dist/ui/tool-progress.js +56 -0
- package/dist/ui/viewer-keys.d.ts +20 -0
- package/dist/ui/viewer-keys.js +17 -0
- package/dist/ui/workflow-card.d.ts +175 -0
- package/dist/ui/workflow-card.js +332 -0
- package/dist/ui/workflow-dialog.d.ts +305 -0
- package/dist/ui/workflow-dialog.js +843 -0
- package/dist/ui/workflow-menu.d.ts +60 -0
- package/dist/ui/workflow-menu.js +147 -0
- package/dist/usage.d.ts +135 -0
- package/dist/usage.js +120 -0
- package/dist/workflow/collisions.d.ts +95 -0
- package/dist/workflow/collisions.js +88 -0
- package/dist/workflow/entry.d.ts +32 -0
- package/dist/workflow/entry.js +29 -0
- package/dist/workflow/host.d.ts +62 -0
- package/dist/workflow/host.js +363 -0
- package/dist/workflow/journal.d.ts +97 -0
- package/dist/workflow/journal.js +120 -0
- package/dist/workflow/json-schema.d.ts +51 -0
- package/dist/workflow/json-schema.js +111 -0
- package/dist/workflow/meta.d.ts +67 -0
- package/dist/workflow/meta.js +317 -0
- package/dist/workflow/progress.d.ts +224 -0
- package/dist/workflow/progress.js +361 -0
- package/dist/workflow/runtime.d.ts +334 -0
- package/dist/workflow/runtime.js +830 -0
- package/dist/workflow/saved.d.ts +90 -0
- package/dist/workflow/saved.js +203 -0
- package/dist/workflow/task.d.ts +136 -0
- package/dist/workflow/task.js +207 -0
- package/dist/workflow/tool-description.d.ts +38 -0
- package/dist/workflow/tool-description.js +199 -0
- package/dist/workflow/worker-source.d.ts +47 -0
- package/dist/workflow/worker-source.js +778 -0
- package/dist/worktree.d.ts +52 -0
- package/dist/worktree.js +164 -0
- package/dist/xml.d.ts +10 -0
- package/dist/xml.js +12 -0
- package/docs/FORK.md +40 -0
- package/docs/recovery.md +62 -0
- package/docs/rpc.md +189 -0
- package/docs/workflows.md +437 -0
- package/examples/agent-tool-description.md +43 -0
- package/examples/workflows/compose.js +51 -0
- package/examples/workflows/fan-out-audit.js +47 -0
- package/examples/workflows/gated-fix.js +60 -0
- package/examples/workflows/lib/count-child.js +27 -0
- package/examples/workflows/review-panel.js +63 -0
- package/examples/workflows/structured-findings.js +78 -0
- package/package.json +75 -6
- package/src/abortable.ts +43 -0
- package/src/active-agents-prompt.ts +39 -0
- package/src/advisor-status.ts +74 -0
- package/src/agent-color.ts +161 -0
- package/src/agent-file-toggle.ts +273 -0
- package/src/agent-manager.ts +1601 -0
- package/src/agent-runner.ts +1372 -0
- package/src/agent-types.ts +345 -0
- package/src/child-context.ts +173 -0
- package/src/child-session-shutdown.ts +46 -0
- package/src/context.ts +58 -0
- package/src/cross-extension-rpc.ts +198 -0
- package/src/custom-agents.ts +417 -0
- package/src/default-agents.ts +129 -0
- package/src/enabled-models.ts +180 -0
- package/src/env.ts +33 -0
- package/src/error-log.ts +193 -0
- package/src/group-join.ts +141 -0
- package/src/index.ts +4218 -0
- package/src/invocation-config.ts +230 -0
- package/src/memory.ts +179 -0
- package/src/mention-clone.ts +221 -0
- package/src/mention.ts +141 -0
- package/src/model-resolver.ts +118 -0
- package/src/model-scope.ts +70 -0
- package/src/nested-tools.ts +422 -0
- package/src/output-diagnostics.ts +68 -0
- package/src/output-file.ts +258 -0
- package/src/prompts.ts +151 -0
- package/src/recovery.ts +320 -0
- package/src/schedule-store.ts +201 -0
- package/src/schedule.ts +386 -0
- package/src/settings.ts +587 -0
- package/src/skill-loader.ts +102 -0
- package/src/standalone-fallback.ts +195 -0
- package/src/status-note.ts +90 -0
- package/src/structured-output.ts +130 -0
- package/src/terminal-progress.ts +215 -0
- package/src/types.ts +394 -0
- package/src/ui/agent-mention.ts +216 -0
- package/src/ui/agent-widget.ts +732 -0
- package/src/ui/conversation-viewer.ts +523 -0
- package/src/ui/fleet-list.ts +563 -0
- package/src/ui/fleet-overlay.ts +847 -0
- package/src/ui/native-transcript.ts +220 -0
- package/src/ui/schedule-menu.ts +105 -0
- package/src/ui/select-item.ts +45 -0
- package/src/ui/tool-progress.ts +67 -0
- package/src/ui/viewer-keys.ts +39 -0
- package/src/ui/workflow-card.ts +470 -0
- package/src/ui/workflow-dialog.ts +1115 -0
- package/src/ui/workflow-menu.ts +193 -0
- package/src/usage.ts +167 -0
- package/src/workflow/collisions.ts +123 -0
- package/src/workflow/entry.ts +47 -0
- package/src/workflow/host.ts +404 -0
- package/src/workflow/journal.ts +164 -0
- package/src/workflow/json-schema.ts +128 -0
- package/src/workflow/meta.ts +325 -0
- package/src/workflow/progress.ts +550 -0
- package/src/workflow/runtime.ts +1219 -0
- package/src/workflow/saved.ts +217 -0
- package/src/workflow/task.ts +302 -0
- package/src/workflow/tool-description.ts +200 -0
- package/src/workflow/worker-source.ts +781 -0
- package/src/worktree.ts +205 -0
- package/src/xml.ts +13 -0
|
@@ -0,0 +1,847 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* fleet-overlay.ts — model-picker-style two-pane fleet overlay.
|
|
3
|
+
*
|
|
4
|
+
* The full-screen counterpart to the below-editor FleetView list
|
|
5
|
+
* (`fleet-list.ts`, which stays the compact glanceable surface). Frame, top to
|
|
6
|
+
* bottom:
|
|
7
|
+
*
|
|
8
|
+
* ```
|
|
9
|
+
* ╭─ Subagents ─────────────────── 9 agents · 4 workflows ─╮
|
|
10
|
+
* │ │
|
|
11
|
+
* ├─ Fleet ────────────────┬───────────────────────────────┤
|
|
12
|
+
* │ ▾ Running 2 │ ● general-purpose │
|
|
13
|
+
* │ ❯ ● general-purpose … │ Sleep then report 2 │
|
|
14
|
+
* │ ● Explore find thing│ ───────── │
|
|
15
|
+
* │ ▾ Failed 1 │ Type general-purpose │
|
|
16
|
+
* │ ▾ Completed 1 │ Status running │
|
|
17
|
+
* │ │ Model provider/model │
|
|
18
|
+
* ├────────────────────────┴───────────────────────────────┤
|
|
19
|
+
* │ ↑↓ move · enter open · d dismiss · esc close │
|
|
20
|
+
* ╰────────────────────────────────────────────────────────╯
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* Left pane: the complete session roster — running, paused, failed and
|
|
24
|
+
* completed runs, grouped into sections (headers are display-only, the cursor
|
|
25
|
+
* skips them). Unlike the widget, completed/failed agents do not drop out after
|
|
26
|
+
* a few seconds; rows live until the manager sweeps settled records (~10
|
|
27
|
+
* minutes after settle — see agent-manager.ts `cleanup()`), matching the
|
|
28
|
+
* retention the workflow dialog already documents. The manager is untouched:
|
|
29
|
+
* dismissal from this list (`d`) is an overlay-local view filter.
|
|
30
|
+
*
|
|
31
|
+
* Right pane: a detail card for the selected entry — type, description, status,
|
|
32
|
+
* model, thinking, elapsed, tokens, cost (when the `showCost` setting is on),
|
|
33
|
+
* and a short activity/result tail. Enter opens the existing full surface for
|
|
34
|
+
* the entry — the conversation overlay for an agent, the workflow inspector for
|
|
35
|
+
* a run — stacked over this one; Esc there returns here.
|
|
36
|
+
*
|
|
37
|
+
* Narrow terminals (below {@link TWO_PANE_MIN_WIDTH} columns) drop the left
|
|
38
|
+
* pane entirely: the detail card renders full width and carries a position
|
|
39
|
+
* marker (`3 of 9`), so the roster is still navigable by ↑/↓ without ever
|
|
40
|
+
* seeing the list — the same trade the model picker makes for its facet pane.
|
|
41
|
+
*
|
|
42
|
+
* Frame geometry mirrors the model picker's discipline (pi-model-picker
|
|
43
|
+
* src/picker.ts): a fixed chrome budget around one body, a target height of a
|
|
44
|
+
* fraction of the terminal capped at `rows - 2`, and an internal render floor
|
|
45
|
+
* of {@link MIN_RENDER_WIDTH} columns with narrower requests clipped back — so
|
|
46
|
+
* the frame never exceeds terminal width or height. Rendering uses pi-tui
|
|
47
|
+
* primitives and the structural widget `Theme` (fg/bold) only — no sibling
|
|
48
|
+
* imports, no hardcoded colors.
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
import { type Component, type Focusable, isKeyRelease, type KeyId, matchesKey, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
52
|
+
import { formatAdvisorLastReview, formatAdvisorState } from "../advisor-status.js";
|
|
53
|
+
import { hasAgentBadge, renderAgentName } from "../agent-color.js";
|
|
54
|
+
import { isTopLevelAgent } from "../agent-manager.js";
|
|
55
|
+
import type { AgentRecord } from "../types.js";
|
|
56
|
+
import { getLifetimeCost, getLifetimeTotal } from "../usage.js";
|
|
57
|
+
import { type AgentActivity, buildInvocationTags, describeActivity, formatCost, type Theme } from "./agent-widget.js";
|
|
58
|
+
import { type FleetWorkflow, formatFleetElapsed, formatFleetTokens } from "./fleet-list.js";
|
|
59
|
+
|
|
60
|
+
// ─── geometry ────────────────────────────────────────────────────────────────
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Rows outside the body pane: top rule, blank, split opener, split closer,
|
|
64
|
+
* footer hints, bottom rule.
|
|
65
|
+
*/
|
|
66
|
+
const CHROME_ROWS = 6;
|
|
67
|
+
/** Fraction of the terminal the overlay targets, below the host's maxHeight cap. */
|
|
68
|
+
const HEIGHT_FRACTION = 0.88;
|
|
69
|
+
/** Smallest body the frame will render. */
|
|
70
|
+
const MIN_BODY_ROWS = 1;
|
|
71
|
+
/** Below this the left pane is dropped and the detail card runs full width. */
|
|
72
|
+
export const TWO_PANE_MIN_WIDTH = 72;
|
|
73
|
+
const MIN_LEFT_WIDTH = 24;
|
|
74
|
+
const MAX_LEFT_WIDTH = 36;
|
|
75
|
+
/**
|
|
76
|
+
* Internal rendering floor. A narrower request is laid out at this width and
|
|
77
|
+
* clipped back, so the layout arithmetic never degenerates.
|
|
78
|
+
*/
|
|
79
|
+
export const MIN_RENDER_WIDTH = 40;
|
|
80
|
+
/** Re-render cadence so elapsed/token stats tick while agents run. */
|
|
81
|
+
const TICK_MS = 250;
|
|
82
|
+
/** Activity/result lines shown at the bottom of the detail card. */
|
|
83
|
+
const MAX_TAIL_LINES = 3;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Total frame height for a terminal of `terminalRows`, including chrome.
|
|
87
|
+
* Mirrored from the model picker's guard: capped at `rows - 2` so the frame is
|
|
88
|
+
* never clipped by the overlay host, floored at one body row so the layout math
|
|
89
|
+
* always has somewhere to draw.
|
|
90
|
+
*/
|
|
91
|
+
export function overlayHeightFor(terminalRows: number): number {
|
|
92
|
+
const rows = Number.isFinite(terminalRows) && terminalRows > 0 ? Math.floor(terminalRows) : 40;
|
|
93
|
+
const smallest = CHROME_ROWS + MIN_BODY_ROWS;
|
|
94
|
+
return Math.max(smallest, Math.min(Math.floor(rows * HEIGHT_FRACTION), rows - 2));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface Layout {
|
|
98
|
+
readonly width: number;
|
|
99
|
+
readonly height: number;
|
|
100
|
+
readonly twoPane: boolean;
|
|
101
|
+
readonly leftWidth: number;
|
|
102
|
+
readonly bodyHeight: number;
|
|
103
|
+
/** Content width inside the right pane. */
|
|
104
|
+
readonly bodyWidth: number;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ─── glyphs ──────────────────────────────────────────────────────────────────
|
|
108
|
+
|
|
109
|
+
interface Glyphs {
|
|
110
|
+
/** Marks the highlighted roster row. */
|
|
111
|
+
readonly cursor: string;
|
|
112
|
+
/** Status dot; the section color carries the meaning. */
|
|
113
|
+
readonly bullet: string;
|
|
114
|
+
/** Section header prefix. */
|
|
115
|
+
readonly group: string;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const UNICODE_GLYPHS: Glyphs = { cursor: "❯", bullet: "●", group: "▾" };
|
|
119
|
+
const ASCII_GLYPHS: Glyphs = { cursor: ">", bullet: "*", group: "v" };
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Resolve the glyph set. Set `PI_SUBAGENTS_GLYPHS=ascii` for terminals where
|
|
123
|
+
* ambiguous-width glyphs render two cells wide and shift the frame. Box-drawing
|
|
124
|
+
* frame characters stay — they are single-width in virtually all terminals;
|
|
125
|
+
* only the label glyphs shift.
|
|
126
|
+
*/
|
|
127
|
+
function resolveGlyphs(): Glyphs {
|
|
128
|
+
return process.env.PI_SUBAGENTS_GLYPHS?.trim().toLowerCase() === "ascii" ? ASCII_GLYPHS : UNICODE_GLYPHS;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ─── frame drawing (pi's dialog chrome, reproduced locally) ─────────────────
|
|
132
|
+
|
|
133
|
+
const BOX = {
|
|
134
|
+
topLeft: "╭",
|
|
135
|
+
topRight: "╮",
|
|
136
|
+
bottomLeft: "╰",
|
|
137
|
+
bottomRight: "╯",
|
|
138
|
+
horizontal: "─",
|
|
139
|
+
vertical: "│",
|
|
140
|
+
teeDown: "┬",
|
|
141
|
+
teeUp: "┴",
|
|
142
|
+
teeRight: "├",
|
|
143
|
+
teeLeft: "┤",
|
|
144
|
+
} as const;
|
|
145
|
+
|
|
146
|
+
/** Left/right inset between a vertical border and its content. */
|
|
147
|
+
const INSET = 1;
|
|
148
|
+
|
|
149
|
+
/** Force `text` to exactly `width` columns: truncate with ellipsis, else pad. ANSI-aware. */
|
|
150
|
+
function fitText(text: string, width: number): string {
|
|
151
|
+
if (width <= 0) return "";
|
|
152
|
+
const measured = visibleWidth(text);
|
|
153
|
+
if (measured > width) {
|
|
154
|
+
const clipped = truncateToWidth(text, width);
|
|
155
|
+
const clippedWidth = visibleWidth(clipped);
|
|
156
|
+
return clippedWidth < width ? clipped + " ".repeat(width - clippedWidth) : clipped;
|
|
157
|
+
}
|
|
158
|
+
return measured === width ? text : text + " ".repeat(width - measured);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Trim `text` to at most `width` columns without padding. */
|
|
162
|
+
function clip(text: string, width: number): string {
|
|
163
|
+
return width <= 0 ? "" : truncateToWidth(text, width);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Place `right` flush to `width`, truncating `left` first so the stats survive. */
|
|
167
|
+
function rightAlign(left: string, right: string, width: number): string {
|
|
168
|
+
const rightWidth = visibleWidth(right);
|
|
169
|
+
const maxLeft = Math.max(0, width - rightWidth - 1);
|
|
170
|
+
const leftClamped = clip(left, maxLeft);
|
|
171
|
+
const gap = Math.max(1, width - visibleWidth(leftClamped) - rightWidth);
|
|
172
|
+
return fitText(leftClamped + " ".repeat(gap) + right, width);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** A dim `key` + muted `description` pair for the footer hint row. */
|
|
176
|
+
function keyHint(theme: Theme, key: string, description: string): string {
|
|
177
|
+
return theme.fg("dim", key) + theme.fg("muted", ` ${description}`);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Titled top rule: `╭─ Subagents ─────── right ─╮`. Right label is budgeted first. */
|
|
181
|
+
function topRule(theme: Theme, width: number, title: string, right: string): string {
|
|
182
|
+
const bar = (s: string) => theme.fg("border", s);
|
|
183
|
+
const inner = Math.max(0, width - 2);
|
|
184
|
+
if (inner <= 1) return bar(BOX.topLeft + BOX.horizontal.repeat(inner) + BOX.topRight);
|
|
185
|
+
|
|
186
|
+
const rule = inner - 1; // columns for title + filler + right label
|
|
187
|
+
const rightShown = right ? clip(` ${right} `, Math.max(0, Math.floor(rule * 0.4))) : "";
|
|
188
|
+
const rightWidth = visibleWidth(rightShown);
|
|
189
|
+
const rightBlock = rightShown.length > 0 ? rightWidth + 1 : 0;
|
|
190
|
+
|
|
191
|
+
const titleShown = title ? clip(` ${title} `, Math.max(0, rule - rightBlock)) : "";
|
|
192
|
+
const titleWidth = visibleWidth(titleShown);
|
|
193
|
+
const fill = Math.max(0, rule - titleWidth - rightBlock);
|
|
194
|
+
|
|
195
|
+
let line = bar(BOX.topLeft + BOX.horizontal);
|
|
196
|
+
if (titleWidth > 0) line += theme.bold(theme.fg("accent", titleShown));
|
|
197
|
+
line += bar(BOX.horizontal.repeat(fill));
|
|
198
|
+
if (rightShown.length > 0) line += bar(BOX.horizontal) + theme.fg("dim", rightShown);
|
|
199
|
+
return line + bar(BOX.topRight);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Full-width interior rule: `├────┤`. */
|
|
203
|
+
function divider(theme: Theme, width: number): string {
|
|
204
|
+
return (
|
|
205
|
+
theme.fg("border", BOX.teeRight) +
|
|
206
|
+
theme.fg("border", BOX.horizontal.repeat(Math.max(0, width - 2))) +
|
|
207
|
+
theme.fg("border", BOX.teeLeft)
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Closing rule: `╰────╯`. */
|
|
212
|
+
function bottomRule(theme: Theme, width: number): string {
|
|
213
|
+
return (
|
|
214
|
+
theme.fg("border", BOX.bottomLeft) +
|
|
215
|
+
theme.fg("border", BOX.horizontal.repeat(Math.max(0, width - 2))) +
|
|
216
|
+
theme.fg("border", BOX.bottomRight)
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** `│ content │` with one column of inset on each side; exact width. */
|
|
221
|
+
function contentRow(theme: Theme, content: string, width: number): string {
|
|
222
|
+
const budget = width - 2 * (INSET + 1);
|
|
223
|
+
const bar = theme.fg("border", BOX.vertical);
|
|
224
|
+
if (budget <= 0) return bar + bar;
|
|
225
|
+
return `${bar} ${fitText(content, budget)} ${bar}`;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Left/right rule lengths for an interior split rule over the pane divider. */
|
|
229
|
+
function splitArms(width: number, leftWidth: number): { left: number; right: number } {
|
|
230
|
+
const dividerColumn = leftWidth + INSET + 1 + INSET;
|
|
231
|
+
return { left: Math.max(0, dividerColumn - 1), right: Math.max(0, width - 2 - dividerColumn) };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Rule opening the two-pane region, splitting over the divider: `├─ Fleet ──┬───┤`. */
|
|
235
|
+
function openSplit(theme: Theme, width: number, title: string, leftWidth: number): string {
|
|
236
|
+
const { left, right } = splitArms(width, leftWidth);
|
|
237
|
+
let head: string;
|
|
238
|
+
if (!title || left <= 1) {
|
|
239
|
+
head = theme.fg("border", BOX.teeRight + BOX.horizontal.repeat(left));
|
|
240
|
+
} else {
|
|
241
|
+
const shown = clip(` ${title} `, Math.max(0, left - 1));
|
|
242
|
+
head =
|
|
243
|
+
theme.fg("border", BOX.teeRight + BOX.horizontal) +
|
|
244
|
+
theme.bold(theme.fg("accent", shown)) +
|
|
245
|
+
theme.fg("border", BOX.horizontal.repeat(Math.max(0, left - 1 - visibleWidth(shown))));
|
|
246
|
+
}
|
|
247
|
+
return (
|
|
248
|
+
head + theme.fg("border", BOX.teeDown + BOX.horizontal.repeat(right) + BOX.teeLeft)
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Closing rule of the two-pane region: `├─────┴────┤`. */
|
|
253
|
+
function closeSplit(theme: Theme, width: number, leftWidth: number): string {
|
|
254
|
+
const { left, right } = splitArms(width, leftWidth);
|
|
255
|
+
return theme.fg(
|
|
256
|
+
"border",
|
|
257
|
+
BOX.teeRight + BOX.horizontal.repeat(left) + BOX.teeUp + BOX.horizontal.repeat(right) + BOX.teeLeft,
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** Right-pane content budget for a two-pane layout of `width`. */
|
|
262
|
+
function bodyWidthFor(width: number, leftWidth: number): number {
|
|
263
|
+
// Fixed overhead: `│ ` + ` │ ` + ` │` = 4 * INSET + 3.
|
|
264
|
+
return Math.max(0, width - leftWidth - (4 * INSET + 3));
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** `│ left │ body │`, each pane padded to its exact budget. */
|
|
268
|
+
function splitRow(theme: Theme, left: string, body: string, width: number, leftWidth: number): string {
|
|
269
|
+
const bodyBudget = bodyWidthFor(width, leftWidth);
|
|
270
|
+
const bar = theme.fg("border", BOX.vertical);
|
|
271
|
+
if (bodyBudget <= 0) return bar + bar;
|
|
272
|
+
return `${bar} ${fitText(left, leftWidth)} ${bar} ${fitText(body, bodyBudget)} ${bar}`;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// ─── roster model ────────────────────────────────────────────────────────────
|
|
276
|
+
|
|
277
|
+
type SectionId = "running" | "paused" | "failed" | "completed";
|
|
278
|
+
|
|
279
|
+
interface Section {
|
|
280
|
+
readonly id: SectionId;
|
|
281
|
+
readonly label: string;
|
|
282
|
+
/** Theme color for the section header and every status dot in it. */
|
|
283
|
+
readonly color: string;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Fixed display order: actionable first, history at the bottom. */
|
|
287
|
+
const SECTIONS: readonly Section[] = [
|
|
288
|
+
{ id: "running", label: "Running", color: "accent" },
|
|
289
|
+
{ id: "paused", label: "Paused", color: "warning" },
|
|
290
|
+
{ id: "failed", label: "Failed", color: "error" },
|
|
291
|
+
{ id: "completed", label: "Completed", color: "success" },
|
|
292
|
+
];
|
|
293
|
+
|
|
294
|
+
function sectionOfAgent(record: AgentRecord): SectionId {
|
|
295
|
+
if (record.status === "running" || record.status === "queued") return "running";
|
|
296
|
+
// aborted (user/tool cut) and stopped (manual halt) read as failures next to
|
|
297
|
+
// error; steered is a completed run that happened to be redirected along the
|
|
298
|
+
// way, so it settles into the success group.
|
|
299
|
+
if (record.status === "error" || record.status === "aborted" || record.status === "stopped") return "failed";
|
|
300
|
+
return "completed";
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function sectionOfWorkflow(run: FleetWorkflow): SectionId {
|
|
304
|
+
if (run.status === "running") return "running";
|
|
305
|
+
if (run.status === "paused") return "paused";
|
|
306
|
+
if (run.status === "failed" || run.status === "killed") return "failed";
|
|
307
|
+
return "completed";
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export type RosterEntry =
|
|
311
|
+
| { readonly kind: "workflow"; readonly workflow: FleetWorkflow }
|
|
312
|
+
| { readonly kind: "agent"; readonly record: AgentRecord };
|
|
313
|
+
|
|
314
|
+
function entryKey(entry: RosterEntry): string {
|
|
315
|
+
return entry.kind === "agent" ? `agent:${entry.record.id}` : `workflow:${entry.workflow.id}`;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
interface SectionRow {
|
|
319
|
+
readonly kind: "section";
|
|
320
|
+
readonly key: string;
|
|
321
|
+
readonly section: Section;
|
|
322
|
+
readonly count: number;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
interface EntryRow {
|
|
326
|
+
readonly kind: "entry";
|
|
327
|
+
readonly key: string;
|
|
328
|
+
readonly entry: RosterEntry;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
type Row = SectionRow | EntryRow;
|
|
332
|
+
|
|
333
|
+
/** What the overlay reads from the host; every source is pulled live per render. */
|
|
334
|
+
export interface FleetOverlayDeps {
|
|
335
|
+
/** Render pump; the real pi `TUI` satisfies this structurally. */
|
|
336
|
+
readonly tui: { requestRender(): void; terminal: { rows: number; columns: number } };
|
|
337
|
+
readonly theme: Theme;
|
|
338
|
+
/**
|
|
339
|
+
* Top-level agent records for the session; read on every render. Records
|
|
340
|
+
* without a session are filtered here (nothing to open behind them) — they
|
|
341
|
+
* are typically queued and row into view once they start.
|
|
342
|
+
*/
|
|
343
|
+
readonly records: () => AgentRecord[];
|
|
344
|
+
/** Workflow runs as the fleet list sees them. */
|
|
345
|
+
readonly workflows: () => readonly FleetWorkflow[];
|
|
346
|
+
/** Live activity per running agent id, for the detail card's tail. */
|
|
347
|
+
readonly activity: ReadonlyMap<string, AgentActivity>;
|
|
348
|
+
/** Whether the detail card shows an estimated cost. Read per render. */
|
|
349
|
+
readonly showCost: () => boolean;
|
|
350
|
+
/** Opens the full conversation overlay for an agent, stacked over this one. */
|
|
351
|
+
readonly openAgent: (record: AgentRecord) => Promise<void> | void;
|
|
352
|
+
/** Opens the workflow inspector for a run, stacked over this one. */
|
|
353
|
+
readonly openWorkflow: (id: string) => Promise<void> | void;
|
|
354
|
+
readonly done: (result: undefined) => void;
|
|
355
|
+
readonly notify?: (message: string, type?: "info" | "warning" | "error") => void;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* The fleet overlay. See the file header for the frame layout and the
|
|
360
|
+
* roster → detail navigation model.
|
|
361
|
+
*/
|
|
362
|
+
export class FleetOverlay implements Component, Focusable {
|
|
363
|
+
private readonly deps: FleetOverlayDeps;
|
|
364
|
+
private readonly theme: Theme;
|
|
365
|
+
private readonly glyphs: Glyphs;
|
|
366
|
+
|
|
367
|
+
/** `agent:<id>` / `workflow:<id>` of the highlighted row — key, never index. */
|
|
368
|
+
private selectedKey = "";
|
|
369
|
+
/** Fallback position when the keyed row is gone; the nearest survivor keeps the cursor. */
|
|
370
|
+
private selectedIndex = 0;
|
|
371
|
+
private windowStart = 0;
|
|
372
|
+
/** Settled rows the user dismissed from this view; overlay-local, manager untouched. */
|
|
373
|
+
private readonly dismissed = new Set<string>();
|
|
374
|
+
|
|
375
|
+
private closed = false;
|
|
376
|
+
private layout: Layout | undefined;
|
|
377
|
+
private readonly timer: ReturnType<typeof setInterval>;
|
|
378
|
+
|
|
379
|
+
// Focusable: no embedded editor/IME, so the flag is a plain holder.
|
|
380
|
+
private focusedFlag = false;
|
|
381
|
+
|
|
382
|
+
constructor(deps: FleetOverlayDeps) {
|
|
383
|
+
this.deps = deps;
|
|
384
|
+
this.theme = deps.theme;
|
|
385
|
+
this.glyphs = resolveGlyphs();
|
|
386
|
+
// Tick keeps elapsed/token stats live while anything is running; the roster
|
|
387
|
+
// itself is rebuilt on every render, so this only needs to re-render.
|
|
388
|
+
this.timer = setInterval(() => {
|
|
389
|
+
if (!this.closed) this.deps.tui.requestRender();
|
|
390
|
+
}, TICK_MS);
|
|
391
|
+
(this.timer as { unref?: () => void }).unref?.();
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
get focused(): boolean {
|
|
395
|
+
return this.focusedFlag;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
set focused(value: boolean) {
|
|
399
|
+
this.focusedFlag = value;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// ── roster ───────────────────────────────────────────────────────────
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* The full session roster. No linger cutoff — that window is the widget's
|
|
406
|
+
* glance behavior, not a property of the records; the manager keeps settled
|
|
407
|
+
* records long enough (~10 min) for this list to read as history.
|
|
408
|
+
*/
|
|
409
|
+
private agentRecords(): AgentRecord[] {
|
|
410
|
+
return this.deps.records().filter(record => isTopLevelAgent(record) && record.session !== undefined);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** Flat row list: section headers interleaved, entries newest-first within. */
|
|
414
|
+
private buildRows(): Row[] {
|
|
415
|
+
const agents = this.agentRecords();
|
|
416
|
+
const runs = this.deps.workflows().filter(run => !this.dismissed.has(`workflow:${run.id}`));
|
|
417
|
+
const rows: Row[] = [];
|
|
418
|
+
|
|
419
|
+
for (const section of SECTIONS) {
|
|
420
|
+
const sectionRuns = runs
|
|
421
|
+
.filter(run => sectionOfWorkflow(run) === section.id)
|
|
422
|
+
.sort((a, b) => b.startedAt - a.startedAt);
|
|
423
|
+
const sectionAgents = agents
|
|
424
|
+
.filter(record => sectionOfAgent(record) === section.id && !this.dismissed.has(`agent:${record.id}`))
|
|
425
|
+
.sort((a, b) => b.startedAt - a.startedAt);
|
|
426
|
+
const entries: RosterEntry[] = [
|
|
427
|
+
// Runs sit above agents, as in the widget: a run owns most of the
|
|
428
|
+
// agents that would otherwise crowd the same section.
|
|
429
|
+
...sectionRuns.map(workflow => ({ kind: "workflow" as const, workflow })),
|
|
430
|
+
...sectionAgents.map(record => ({ kind: "agent" as const, record })),
|
|
431
|
+
];
|
|
432
|
+
if (entries.length === 0) continue;
|
|
433
|
+
rows.push({ kind: "section", key: `section:${section.id}`, section, count: entries.length });
|
|
434
|
+
for (const entry of entries) rows.push({ kind: "entry", key: entryKey(entry), entry });
|
|
435
|
+
}
|
|
436
|
+
return rows;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
private entriesOf(rows: Row[]): EntryRow[] {
|
|
440
|
+
return rows.filter((row): row is EntryRow => row.kind === "entry");
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** Re-seat the selection on the same key; a removed row falls to the nearest survivor. */
|
|
444
|
+
private reconcile(rows: Row[]): EntryRow[] {
|
|
445
|
+
const entries = this.entriesOf(rows);
|
|
446
|
+
if (entries.length === 0) {
|
|
447
|
+
this.selectedKey = "";
|
|
448
|
+
this.selectedIndex = 0;
|
|
449
|
+
return entries;
|
|
450
|
+
}
|
|
451
|
+
const found = entries.findIndex(row => row.key === this.selectedKey);
|
|
452
|
+
this.selectedIndex = found >= 0 ? found : Math.max(0, Math.min(this.selectedIndex, entries.length - 1));
|
|
453
|
+
this.selectedKey = entries[this.selectedIndex].key;
|
|
454
|
+
return entries;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
private selectedEntry(rows: Row[]): RosterEntry | undefined {
|
|
458
|
+
return this.entriesOf(rows)[this.selectedIndex]?.entry;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
private move(delta: number, wrap: boolean, entries: EntryRow[]): void {
|
|
462
|
+
const total = entries.length;
|
|
463
|
+
if (total === 0) return;
|
|
464
|
+
const next = wrap
|
|
465
|
+
? ((this.selectedIndex + delta) % total + total) % total
|
|
466
|
+
: Math.max(0, Math.min(this.selectedIndex + delta, total - 1));
|
|
467
|
+
this.selectedIndex = next;
|
|
468
|
+
this.selectedKey = entries[next].key;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/** Pan the roster window so the highlighted entry's row is inside it. */
|
|
472
|
+
private ensureSelectionVisible(rows: Row[]): void {
|
|
473
|
+
const visible = this.layout?.bodyHeight ?? 10;
|
|
474
|
+
const maxStart = Math.max(0, rows.length - visible);
|
|
475
|
+
this.windowStart = Math.max(0, Math.min(this.windowStart, maxStart));
|
|
476
|
+
if (!this.selectedKey) return;
|
|
477
|
+
const rowIndex = rows.findIndex(row => row.kind === "entry" && row.key === this.selectedKey);
|
|
478
|
+
if (rowIndex < 0) return;
|
|
479
|
+
if (rowIndex < this.windowStart) this.windowStart = rowIndex;
|
|
480
|
+
else if (rowIndex >= this.windowStart + visible) this.windowStart = rowIndex - visible + 1;
|
|
481
|
+
this.windowStart = Math.max(0, Math.min(this.windowStart, maxStart));
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// ── actions ──────────────────────────────────────────────────────────────
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* Hand control back to the caller, which closes and disposes the overlay.
|
|
488
|
+
* Same done semantics as the conversation viewer: resolve with `undefined`.
|
|
489
|
+
*/
|
|
490
|
+
private finish(): void {
|
|
491
|
+
this.dispose();
|
|
492
|
+
this.deps.done(undefined);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** After a stacked view closes: put the cursor back on the entry it came from. */
|
|
496
|
+
private afterStackedClose(key: string): void {
|
|
497
|
+
if (this.closed) return;
|
|
498
|
+
this.selectedKey = key;
|
|
499
|
+
this.deps.tui.requestRender();
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
private activate(rows: Row[]): void {
|
|
503
|
+
const entry = this.selectedEntry(rows);
|
|
504
|
+
if (!entry) return;
|
|
505
|
+
if (entry.kind === "workflow") {
|
|
506
|
+
const id = entry.workflow.id;
|
|
507
|
+
void Promise.resolve(this.deps.openWorkflow(id)).then(
|
|
508
|
+
() => this.afterStackedClose(`workflow:${id}`),
|
|
509
|
+
error => this.deps.notify?.(`Could not open the run: ${errorMessage(error)}`, "error"),
|
|
510
|
+
);
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
const record = entry.record;
|
|
514
|
+
// The roster filters to records with sessions, so this is only reachable
|
|
515
|
+
// when the session was detached between render and keypress.
|
|
516
|
+
if (!record.session) {
|
|
517
|
+
this.deps.notify?.(`Agent is ${record.status} — no session available.`, "info");
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
void Promise.resolve(this.deps.openAgent(record)).then(
|
|
521
|
+
() => this.afterStackedClose(`agent:${record.id}`),
|
|
522
|
+
error => this.deps.notify?.(`Could not open the conversation: ${errorMessage(error)}`, "error"),
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/** Remove the selected settled row from this view. Running work is not dismissible. */
|
|
527
|
+
private dismissSelected(rows: Row[]): void {
|
|
528
|
+
const entry = this.selectedEntry(rows);
|
|
529
|
+
if (!entry) return;
|
|
530
|
+
const section = entry.kind === "workflow" ? sectionOfWorkflow(entry.workflow) : sectionOfAgent(entry.record);
|
|
531
|
+
if (section === "running" || section === "paused") return;
|
|
532
|
+
this.dismissed.add(entryKey(entry));
|
|
533
|
+
this.deps.tui.requestRender();
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// ── input ──────────────────────────────────────────────────────────────
|
|
537
|
+
|
|
538
|
+
handleInput(data: string): void {
|
|
539
|
+
if (this.closed || isKeyRelease(data)) return;
|
|
540
|
+
|
|
541
|
+
if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
|
|
542
|
+
this.finish();
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
const rows = this.buildRows();
|
|
547
|
+
const entries = this.reconcile(rows);
|
|
548
|
+
const page = Math.max(1, (this.layout?.bodyHeight ?? 10) - 1);
|
|
549
|
+
|
|
550
|
+
const movements: ReadonlyArray<{ keys: readonly KeyId[]; delta: number; wrap: boolean }> = [
|
|
551
|
+
{ keys: ["up"], delta: -1, wrap: true },
|
|
552
|
+
{ keys: ["down"], delta: 1, wrap: true },
|
|
553
|
+
{ keys: ["pageUp"], delta: -page, wrap: false },
|
|
554
|
+
{ keys: ["pageDown"], delta: page, wrap: false },
|
|
555
|
+
{ keys: ["home"], delta: -Number.MAX_SAFE_INTEGER, wrap: false },
|
|
556
|
+
{ keys: ["end"], delta: Number.MAX_SAFE_INTEGER, wrap: false },
|
|
557
|
+
];
|
|
558
|
+
for (const movement of movements) {
|
|
559
|
+
if (!movement.keys.some(key => matchesKey(data, key))) continue;
|
|
560
|
+
this.move(movement.delta, movement.wrap, entries);
|
|
561
|
+
this.deps.tui.requestRender();
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
if (matchesKey(data, "enter") || matchesKey(data, "return")) {
|
|
566
|
+
this.activate(rows);
|
|
567
|
+
this.deps.tui.requestRender();
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
if (matchesKey(data, "d")) {
|
|
571
|
+
this.dismissSelected(rows);
|
|
572
|
+
this.deps.tui.requestRender();
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// ── layout ────────────────────────────────────────────────────────────
|
|
578
|
+
|
|
579
|
+
private terminalRows(): number {
|
|
580
|
+
const rows = this.deps.tui.terminal?.rows ?? 40;
|
|
581
|
+
return Number.isFinite(rows) && rows > 12 ? Math.floor(rows) : 40;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
private layoutFor(width: number): Layout {
|
|
585
|
+
const height = overlayHeightFor(this.terminalRows());
|
|
586
|
+
const cached = this.layout;
|
|
587
|
+
if (cached && cached.height === height && cached.width === width) return cached;
|
|
588
|
+
const twoPane = width >= TWO_PANE_MIN_WIDTH;
|
|
589
|
+
const leftWidth = twoPane
|
|
590
|
+
? Math.max(MIN_LEFT_WIDTH, Math.min(MAX_LEFT_WIDTH, Math.round(width * 0.28), width - 46))
|
|
591
|
+
: 0;
|
|
592
|
+
const layout: Layout = {
|
|
593
|
+
width,
|
|
594
|
+
height,
|
|
595
|
+
twoPane,
|
|
596
|
+
leftWidth,
|
|
597
|
+
bodyHeight: Math.max(MIN_BODY_ROWS, height - CHROME_ROWS),
|
|
598
|
+
bodyWidth: twoPane ? bodyWidthFor(width, leftWidth) : width - 2 * (INSET + 1),
|
|
599
|
+
};
|
|
600
|
+
this.layout = layout;
|
|
601
|
+
return layout;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// ── row rendering ─────────────────────────────────────────────────────
|
|
605
|
+
|
|
606
|
+
private renderSectionRow(row: SectionRow, width: number): string {
|
|
607
|
+
const head =
|
|
608
|
+
this.theme.fg(row.section.color, this.glyphs.group) + " " + this.theme.bold(this.theme.fg("muted", row.section.label));
|
|
609
|
+
return fitText(`${head} ${this.theme.fg("dim", String(row.count))}`, width);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
private renderEntryRow(row: EntryRow, width: number): string {
|
|
613
|
+
const entry = row.entry;
|
|
614
|
+
const selected = row.key === this.selectedKey;
|
|
615
|
+
const section = entry.kind === "workflow" ? sectionOfWorkflow(entry.workflow) : sectionOfAgent(entry.record);
|
|
616
|
+
const sectionColor = (SECTIONS.find(s => s.id === section) ?? SECTIONS[0]).color;
|
|
617
|
+
|
|
618
|
+
const cursor = selected ? `${this.theme.fg("accent", this.glyphs.cursor)} ` : " ";
|
|
619
|
+
const dot = this.theme.fg(sectionColor, this.glyphs.bullet);
|
|
620
|
+
|
|
621
|
+
let head: string;
|
|
622
|
+
let stats: string;
|
|
623
|
+
if (entry.kind === "workflow") {
|
|
624
|
+
const run = entry.workflow;
|
|
625
|
+
const kind = this.theme.fg(selected ? "text" : "muted", "workflow");
|
|
626
|
+
const name = selected ? this.theme.fg("text", run.name) : run.name;
|
|
627
|
+
head = `${cursor}${dot} ${kind} ${name}`;
|
|
628
|
+
const elapsed = (run.completedAt ?? Date.now()) - run.startedAt;
|
|
629
|
+
const agents = `${run.doneCount}/${run.totalCount} agent${run.totalCount === 1 ? "" : "s"}`;
|
|
630
|
+
stats = `${agents} · ${formatFleetElapsed(elapsed)} · ${formatFleetTokens(run.tokens)}`;
|
|
631
|
+
} else {
|
|
632
|
+
const record = entry.record;
|
|
633
|
+
const name = renderAgentName(record.type, this.theme, selected
|
|
634
|
+
? { fallbackColor: "text", bold: hasAgentBadge(record.type) }
|
|
635
|
+
: { fallbackColor: "muted" });
|
|
636
|
+
const description = selected ? this.theme.fg("text", record.description) : record.description;
|
|
637
|
+
head = `${cursor}${dot} ${name} ${description}`;
|
|
638
|
+
const elapsed = formatFleetElapsed((record.completedAt ?? Date.now()) - record.startedAt);
|
|
639
|
+
const tokens = formatFleetTokens(getLifetimeTotal(record.lifetimeUsage));
|
|
640
|
+
const cost = this.deps.showCost() ? formatCost(getLifetimeCost(record.lifetimeUsage)) : "";
|
|
641
|
+
stats = `${elapsed} · ${tokens}${cost ? ` · ${cost}` : ""}`;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// A pane too narrow for the stats column shows the identity only; the
|
|
645
|
+
// detail card next to it carries the numbers.
|
|
646
|
+
if (width - visibleWidth(head) >= visibleWidth(stats) + 2 && width >= 30) {
|
|
647
|
+
return rightAlign(head, this.theme.fg(selected ? "text" : "dim", stats), width);
|
|
648
|
+
}
|
|
649
|
+
return fitText(head, width);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
// ── detail card (right pane) ─────────────────────────────────────────────
|
|
653
|
+
|
|
654
|
+
/** One ` Label value` row, already styled, never wider than `width`. */
|
|
655
|
+
private field(label: string, value: string, width: number): string {
|
|
656
|
+
return clip(` ${this.theme.fg("dim", label.padEnd(12))}${value}`, width);
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* The tail of what an agent just did or said: the live activity line while it
|
|
661
|
+
* works, the final answer once it settles, the message when it errored.
|
|
662
|
+
* Workflows have no per-run prose feed — the progress counters above carry it.
|
|
663
|
+
*/
|
|
664
|
+
private agentTail(record: AgentRecord, width: number): string[] {
|
|
665
|
+
const take = (text: string, color: string): string[] =>
|
|
666
|
+
text
|
|
667
|
+
.split("\n")
|
|
668
|
+
.map(line => line.trim())
|
|
669
|
+
.filter(line => line.length > 0)
|
|
670
|
+
.slice(-MAX_TAIL_LINES)
|
|
671
|
+
.map(line => this.theme.fg(color, clip(` ${line}`, width)));
|
|
672
|
+
|
|
673
|
+
if (record.status === "error" && record.error) return take(record.error, "error");
|
|
674
|
+
if ((record.status === "completed" || record.status === "steered") && record.result?.trim()) {
|
|
675
|
+
return take(record.result, "dim");
|
|
676
|
+
}
|
|
677
|
+
if (record.status === "running" || record.status === "queued") {
|
|
678
|
+
const activity = this.deps.activity.get(record.id);
|
|
679
|
+
if (activity?.responseText?.trim()) return take(activity.responseText, "dim");
|
|
680
|
+
if (activity) return [this.theme.fg("dim", clip(` ${describeActivity(activity.activeTools, activity.responseText)}`, width))];
|
|
681
|
+
}
|
|
682
|
+
return [];
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
private detailLines(rows: Row[], width: number): string[] {
|
|
686
|
+
const lines: string[] = [];
|
|
687
|
+
const entries = this.entriesOf(rows);
|
|
688
|
+
const entry = this.selectedEntry(rows);
|
|
689
|
+
if (!entry) {
|
|
690
|
+
lines.push(this.theme.fg("muted", " Nothing to show"));
|
|
691
|
+
lines.push(this.theme.fg("dim", " Settled rows may have been dismissed, or the roster is empty."));
|
|
692
|
+
return lines;
|
|
693
|
+
}
|
|
694
|
+
const positionRaw = `${this.selectedIndex + 1} of ${entries.length}`;
|
|
695
|
+
|
|
696
|
+
if (entry.kind === "workflow") {
|
|
697
|
+
const run = entry.workflow;
|
|
698
|
+
const section = SECTIONS.find(s => s.id === sectionOfWorkflow(run)) ?? SECTIONS[0];
|
|
699
|
+
const nameLine = `${this.theme.fg(section.color, this.glyphs.bullet)} ${this.theme.fg("muted", "workflow")} ${run.name}`;
|
|
700
|
+
lines.push(rightAlign(nameLine, this.theme.fg("dim", positionRaw), width));
|
|
701
|
+
lines.push(this.theme.fg("dim", ` ${BOX.horizontal.repeat(Math.max(0, width - 4))}`));
|
|
702
|
+
const elapsed = formatFleetElapsed((run.completedAt ?? Date.now()) - run.startedAt);
|
|
703
|
+
lines.push(this.field("Status", this.theme.fg(section.color, run.status), width));
|
|
704
|
+
lines.push(
|
|
705
|
+
this.field("Progress", this.theme.fg("text", `${run.doneCount}/${run.totalCount} agent${run.totalCount === 1 ? "" : "s"}`), width),
|
|
706
|
+
);
|
|
707
|
+
lines.push(this.field("Elapsed", this.theme.fg("text", elapsed), width));
|
|
708
|
+
lines.push(this.field("Tokens", this.theme.fg("text", formatFleetTokens(run.tokens)), width));
|
|
709
|
+
lines.push("");
|
|
710
|
+
lines.push(this.theme.fg("dim", " Enter to open the run · Esc to close"));
|
|
711
|
+
return lines.map(line => clip(line, width));
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
const record = entry.record;
|
|
715
|
+
const section = SECTIONS.find(s => s.id === sectionOfAgent(record)) ?? SECTIONS[0];
|
|
716
|
+
const nameLine =
|
|
717
|
+
`${this.theme.fg(section.color, this.glyphs.bullet)} ` + renderAgentName(record.type, this.theme, { bold: true });
|
|
718
|
+
lines.push(rightAlign(nameLine, this.theme.fg("dim", positionRaw), width));
|
|
719
|
+
lines.push(this.theme.fg("dim", ` ${BOX.horizontal.repeat(Math.max(0, width - 4))}`));
|
|
720
|
+
lines.push(this.field("Type", this.theme.fg("text", record.type), width));
|
|
721
|
+
lines.push(this.field("Task", this.theme.fg("muted", record.description), width));
|
|
722
|
+
lines.push(this.field("Status", this.theme.fg(section.color, record.status), width));
|
|
723
|
+
|
|
724
|
+
const { modelName, modelId, tags } = buildInvocationTags(record.invocation);
|
|
725
|
+
lines.push(this.field("Model", this.theme.fg("text", modelId ?? modelName ?? "session default"), width));
|
|
726
|
+
const [thinking, ...flags] = splitThinkingTag(tags);
|
|
727
|
+
if (thinking) lines.push(this.field("Thinking", this.theme.fg("text", thinking), width));
|
|
728
|
+
if (flags.length > 0) lines.push(this.field("Flags", this.theme.fg("muted", flags.join(" · ")), width));
|
|
729
|
+
|
|
730
|
+
lines.push(this.field("Elapsed", this.theme.fg("text", formatFleetElapsed((record.completedAt ?? Date.now()) - record.startedAt)), width));
|
|
731
|
+
lines.push(this.field("Tokens", this.theme.fg("text", formatFleetTokens(getLifetimeTotal(record.lifetimeUsage))), width));
|
|
732
|
+
if (this.deps.showCost()) {
|
|
733
|
+
const cost = formatCost(getLifetimeCost(record.lifetimeUsage));
|
|
734
|
+
if (cost) lines.push(this.field("Cost", this.theme.fg("text", cost), width));
|
|
735
|
+
}
|
|
736
|
+
lines.push(this.field("Tools", this.theme.fg("text", String(record.toolUses)), width));
|
|
737
|
+
const advisor = record.advisorStatus;
|
|
738
|
+
lines.push(this.field("Advisor", this.theme.fg("muted", formatAdvisorState(advisor)), width));
|
|
739
|
+
if (advisor && (advisor.state !== "off" || advisor.reviews > 0 || advisor.lastReviewAt !== undefined)) {
|
|
740
|
+
lines.push(this.field("Adv model", this.theme.fg("text", advisor.models.join(", ") || "—"), width));
|
|
741
|
+
lines.push(this.field("Reviews", this.theme.fg("text", String(advisor.reviews)), width));
|
|
742
|
+
lines.push(this.field("Last review", this.theme.fg("text", formatAdvisorLastReview(advisor)), width));
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
const tail = this.agentTail(record, width);
|
|
746
|
+
if (tail.length > 0) {
|
|
747
|
+
lines.push("");
|
|
748
|
+
lines.push(this.theme.fg("dim", record.status === "error" ? " Error" : record.status === "running" || record.status === "queued" ? " Activity" : " Result"));
|
|
749
|
+
lines.push(...tail);
|
|
750
|
+
}
|
|
751
|
+
lines.push("");
|
|
752
|
+
lines.push(this.theme.fg("dim", " Enter to open full view · Esc to close"));
|
|
753
|
+
return lines.map(line => clip(line, width));
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// ── frame ─────────────────────────────────────────────────────────────
|
|
757
|
+
|
|
758
|
+
private headerRight(entries: EntryRow[]): string {
|
|
759
|
+
const agents = entries.filter(row => row.entry.kind === "agent").length;
|
|
760
|
+
const workflows = entries.length - agents;
|
|
761
|
+
const parts = [`${agents} agent${agents === 1 ? "" : "s"}`];
|
|
762
|
+
if (workflows > 0) parts.push(`${workflows} workflow${workflows === 1 ? "" : "s"}`);
|
|
763
|
+
return parts.join(" · ");
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
private footerLine(width: number): string {
|
|
767
|
+
const separator = this.theme.fg("dim", " · ");
|
|
768
|
+
const required = [
|
|
769
|
+
keyHint(this.theme, "↑↓", "move"),
|
|
770
|
+
keyHint(this.theme, "enter", "open"),
|
|
771
|
+
keyHint(this.theme, "d", "dismiss"),
|
|
772
|
+
];
|
|
773
|
+
const close = keyHint(this.theme, "esc", "close");
|
|
774
|
+
return clip(` ${required.join(separator)}${separator}${close}`, width);
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
render(width: number): string[] {
|
|
778
|
+
const requested = Math.max(1, Math.floor(width));
|
|
779
|
+
const w = Math.max(MIN_RENDER_WIDTH, requested);
|
|
780
|
+
const lines = this.renderFrame(w);
|
|
781
|
+
// Narrower than the internal floor: lay out at the floor, clip back.
|
|
782
|
+
const output = requested >= w ? lines : lines.map(line => clip(line, requested));
|
|
783
|
+
// Belt and braces: the TUI rejects lines wider than the viewport.
|
|
784
|
+
return output.map(line => (visibleWidth(line) > requested ? clip(line, requested) : line));
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
private renderFrame(w: number): string[] {
|
|
788
|
+
const layout = this.layoutFor(w);
|
|
789
|
+
|
|
790
|
+
const rows = this.buildRows();
|
|
791
|
+
const entries = this.reconcile(rows);
|
|
792
|
+
this.ensureSelectionVisible(rows);
|
|
793
|
+
|
|
794
|
+
const detail = this.detailLines(rows, layout.bodyWidth).slice(0, layout.bodyHeight);
|
|
795
|
+
|
|
796
|
+
const lines: string[] = [];
|
|
797
|
+
lines.push(topRule(this.theme, w, "Subagents", this.headerRight(entries)));
|
|
798
|
+
lines.push(contentRow(this.theme, "", w));
|
|
799
|
+
lines.push(
|
|
800
|
+
layout.twoPane ? openSplit(this.theme, w, "Fleet", layout.leftWidth) : divider(this.theme, w),
|
|
801
|
+
);
|
|
802
|
+
|
|
803
|
+
const windowed = rows.slice(this.windowStart, this.windowStart + layout.bodyHeight);
|
|
804
|
+
for (let i = 0; i < layout.bodyHeight; i++) {
|
|
805
|
+
const right = detail[i] ?? "";
|
|
806
|
+
if (!layout.twoPane) {
|
|
807
|
+
lines.push(contentRow(this.theme, right, w));
|
|
808
|
+
continue;
|
|
809
|
+
}
|
|
810
|
+
const row = windowed[i];
|
|
811
|
+
const left = row === undefined
|
|
812
|
+
? ""
|
|
813
|
+
: row.kind === "section"
|
|
814
|
+
? this.renderSectionRow(row, layout.leftWidth)
|
|
815
|
+
: this.renderEntryRow(row, layout.leftWidth);
|
|
816
|
+
lines.push(splitRow(this.theme, left, right, w, layout.leftWidth));
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
lines.push(
|
|
820
|
+
layout.twoPane ? closeSplit(this.theme, w, layout.leftWidth) : divider(this.theme, w),
|
|
821
|
+
);
|
|
822
|
+
lines.push(contentRow(this.theme, this.footerLine(w - 2 * (INSET + 1)), w));
|
|
823
|
+
lines.push(bottomRule(this.theme, w));
|
|
824
|
+
return lines.map(line => (visibleWidth(line) > w ? clip(line, w) : line));
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
invalidate(): void {
|
|
828
|
+
this.layout = undefined;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
dispose(): void {
|
|
832
|
+
this.closed = true;
|
|
833
|
+
clearInterval(this.timer);
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
// ─── small helpers ───────────────────────────────────────────────────────────
|
|
838
|
+
|
|
839
|
+
function errorMessage(error: unknown): string {
|
|
840
|
+
return error instanceof Error ? error.message : String(error);
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
/** Pull `thinking: x` out of the invocation tags; the rest are plain flags. */
|
|
844
|
+
function splitThinkingTag(tags: readonly string[]): [thinking: string | undefined, flags: string[]] {
|
|
845
|
+
const thinking = tags.find(tag => tag.startsWith("thinking: "));
|
|
846
|
+
return [thinking ? thinking.slice("thinking: ".length) : undefined, tags.filter(tag => !tag.startsWith("thinking: "))];
|
|
847
|
+
}
|