@aoshizuku/pi-subagent 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +173 -2
- package/agents/explore.md +20 -0
- package/agents/general.md +17 -0
- package/package.json +55 -5
- package/src/batcher.ts +77 -0
- package/src/child.ts +212 -0
- package/src/cleanup.ts +40 -0
- package/src/config.ts +181 -0
- package/src/detail.ts +408 -0
- package/src/focus.ts +240 -0
- package/src/hud-state.ts +183 -0
- package/src/index.ts +1013 -0
- package/src/list.ts +223 -0
- package/src/manager.ts +588 -0
- package/src/models.ts +80 -0
- package/src/observer.ts +275 -0
- package/src/prompts.ts +103 -0
- package/src/render.ts +592 -0
- package/src/result.ts +226 -0
- package/src/run.ts +204 -0
- package/src/settings.ts +140 -0
- package/src/shizuku-settings.ts +92 -0
- package/src/stop-confirm.ts +47 -0
- package/src/test-helpers.ts +194 -0
- package/src/tool-line.ts +85 -0
- package/src/types.ts +257 -0
package/src/list.ts
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// /agents list: every agent of the session; the fallback entry when the HUD rows are not available
|
|
2
|
+
// (pi-hud missing, /hud off) and the target of the HUD's "+N more" row (docs/07 §7).
|
|
3
|
+
// It sits in the editor slot at the bottom like pi's own selectors (not an overlay), so it stays short and
|
|
4
|
+
// leaves the transcript visible. Enter opens the detail overlay on top of it; the list stays open underneath
|
|
5
|
+
// and gets the keyboard back when the overlay closes.
|
|
6
|
+
|
|
7
|
+
import type { KeybindingsManager, Theme } from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { type Component, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
9
|
+
|
|
10
|
+
import { isMoveKey, isRunningStatus, isStoppable, isStopping } from "./detail.js";
|
|
11
|
+
import { focusOrder, windowAround } from "./hud-state.js";
|
|
12
|
+
import type { AgentRecord } from "./manager.js";
|
|
13
|
+
import { durationOf, formatDuration } from "./result.js";
|
|
14
|
+
import type { AgentStatus } from "./run.js";
|
|
15
|
+
import { ARMED_HINT, StopConfirm } from "./stop-confirm.js";
|
|
16
|
+
|
|
17
|
+
export interface ListSource {
|
|
18
|
+
list(): AgentRecord[];
|
|
19
|
+
stop(agentId: string): void;
|
|
20
|
+
/** Show the agent's detail overlay on top of the list; the list stays open. */
|
|
21
|
+
open(agentId: string): void;
|
|
22
|
+
/** x must be pressed twice (docs/07 §4.4); default true. */
|
|
23
|
+
confirmStop?(): boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ListHost {
|
|
27
|
+
requestRender(): void;
|
|
28
|
+
readonly terminal: { readonly rows: number };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Agent rows the list shows at once. The bottom slot is not height-bounded by pi, so the list keeps to a
|
|
33
|
+
* small share of the terminal: at most 10 rows or 40% of the height, never fewer than 3.
|
|
34
|
+
*/
|
|
35
|
+
export function listRowLimit(terminalRows: number): number {
|
|
36
|
+
return Math.max(3, Math.min(10, Math.floor(terminalRows * 0.4)));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const STATUS_COLORS: Record<AgentStatus, "accent" | "warning" | "success" | "error" | "dim" | "muted"> = {
|
|
40
|
+
queued: "muted",
|
|
41
|
+
running: "accent",
|
|
42
|
+
completed: "success",
|
|
43
|
+
failed: "error",
|
|
44
|
+
aborted: "warning",
|
|
45
|
+
max_turns: "warning",
|
|
46
|
+
context_limit: "warning",
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const STATUS_TEXT: Record<AgentStatus, string> = {
|
|
50
|
+
queued: "queued",
|
|
51
|
+
running: "running",
|
|
52
|
+
completed: "completed",
|
|
53
|
+
failed: "failed",
|
|
54
|
+
aborted: "stopped",
|
|
55
|
+
max_turns: "out of turns",
|
|
56
|
+
context_limit: "context full",
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Put a row on the theme's selection surface. Truncation leaves a full reset inside the row, and that
|
|
61
|
+
* reset would drop the background for everything after it, so the background is re-applied after each one.
|
|
62
|
+
*/
|
|
63
|
+
function onSelectionSurface(theme: Theme, content: string, armed: boolean): string {
|
|
64
|
+
// An armed row (first x pressed) swaps the selection surface for the error surface (docs/07 §4.4).
|
|
65
|
+
const surface = armed ? "toolErrorBg" : "selectedBg";
|
|
66
|
+
const band = theme.getBgAnsi(surface);
|
|
67
|
+
return theme.bg(surface, content.replace(/\x1b\[0m/g, `\x1b[0m${band}`));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Cut a row to the width and pad it, so the selection surface reaches the right edge. */
|
|
71
|
+
function fillToWidth(content: string, width: number): string {
|
|
72
|
+
const cut = truncateToWidth(content, width, "…");
|
|
73
|
+
return cut + " ".repeat(Math.max(0, width - visibleWidth(cut)));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* A titled rule, `─ left ───── right ─`, the full width (design_style.md §4.1): a bottom panel's top edge, and
|
|
78
|
+
* the same shape for a divider inside a panel. Labels come in already colored; only the rule is painted here.
|
|
79
|
+
* When the row is narrow the fill shrinks to one `─` first, then each right label is tried in turn (full form,
|
|
80
|
+
* then shorter ones) and finally dropped, and last the left label is cut with `…`. The leading `─ ` and the
|
|
81
|
+
* closing `─` always stay.
|
|
82
|
+
*/
|
|
83
|
+
export function titledRule(
|
|
84
|
+
width: number,
|
|
85
|
+
paint: (rule: string) => string,
|
|
86
|
+
left: string,
|
|
87
|
+
right: string | readonly string[] = [],
|
|
88
|
+
): string {
|
|
89
|
+
const rule = (count: number) => paint("─".repeat(count));
|
|
90
|
+
if (width < 4) return rule(Math.max(0, width));
|
|
91
|
+
const leftWidth = visibleWidth(left);
|
|
92
|
+
for (const label of typeof right === "string" ? [right] : right) {
|
|
93
|
+
// `─ ` left ` ` fill ` ` right ` ─`
|
|
94
|
+
const fill = width - leftWidth - visibleWidth(label) - 6;
|
|
95
|
+
if (label && fill >= 1) return `${rule(1)} ${left} ${rule(fill)} ${label} ${rule(1)}`;
|
|
96
|
+
}
|
|
97
|
+
const shown = leftWidth <= width - 4 ? left : truncateToWidth(left, width - 4, "…");
|
|
98
|
+
return `${rule(1)} ${shown} ${rule(width - 3 - visibleWidth(shown))}`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Plain-text list for modes without a TUI. */
|
|
102
|
+
export function describeAgents(records: readonly AgentRecord[], now = Date.now()): string {
|
|
103
|
+
if (records.length === 0) return "No subagents in this session.";
|
|
104
|
+
return focusOrder(records)
|
|
105
|
+
.map((record) => {
|
|
106
|
+
const duration = record.status === "queued" ? "" : ` ${formatDuration(durationOf(record, now))}`;
|
|
107
|
+
const reason = record.reason && record.status !== "completed" ? ` (${record.reason})` : "";
|
|
108
|
+
return `${record.agentId} ${STATUS_TEXT[record.status]}${duration} ${record.type} ${record.description}${reason}`;
|
|
109
|
+
})
|
|
110
|
+
.join("\n");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export class AgentListView implements Component {
|
|
114
|
+
private selectedId: string | undefined;
|
|
115
|
+
private readonly refresh: ReturnType<typeof setInterval>;
|
|
116
|
+
private closed = false;
|
|
117
|
+
private readonly confirm: StopConfirm;
|
|
118
|
+
|
|
119
|
+
constructor(
|
|
120
|
+
private readonly host: ListHost,
|
|
121
|
+
private readonly theme: Theme,
|
|
122
|
+
private readonly keybindings: KeybindingsManager,
|
|
123
|
+
private readonly source: ListSource,
|
|
124
|
+
private readonly done: () => void,
|
|
125
|
+
) {
|
|
126
|
+
this.confirm = new StopConfirm(() => source.confirmStop?.() ?? true);
|
|
127
|
+
// Durations and states change while the list is open.
|
|
128
|
+
this.refresh = setInterval(() => this.host.requestRender(), 1000);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private ordered(): AgentRecord[] {
|
|
132
|
+
return focusOrder(this.source.list());
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private index(records: readonly AgentRecord[]): number {
|
|
136
|
+
const index = records.findIndex((record) => record.agentId === this.selectedId);
|
|
137
|
+
return index === -1 ? 0 : index;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
render(width: number): string[] {
|
|
141
|
+
const { theme } = this;
|
|
142
|
+
const records = this.ordered();
|
|
143
|
+
const selected = this.index(records);
|
|
144
|
+
const selectedRecord = records[selected];
|
|
145
|
+
this.selectedId = selectedRecord?.agentId;
|
|
146
|
+
// The armed agent may have ended on its own or moved since the first x: the arm goes with it.
|
|
147
|
+
this.confirm.keep(this.selectedId, selectedRecord !== undefined && isStoppable(selectedRecord));
|
|
148
|
+
const armed = this.confirm.isArmed(this.selectedId);
|
|
149
|
+
const active = records.filter((record) => isRunningStatus(record.status)).length;
|
|
150
|
+
// The title sits on the top edge, which carries on the editor's top border; the counts go on its right.
|
|
151
|
+
const counts = [`${active} active, ${records.length} total`, `${records.length} total`].map((text) => theme.fg("dim", text));
|
|
152
|
+
const lines = [titledRule(width, (rule) => theme.fg("borderMuted", rule), theme.bold(theme.fg("accent", "Subagents")), counts), ""];
|
|
153
|
+
if (records.length === 0) lines.push(theme.fg("dim", " No subagents in this session."));
|
|
154
|
+
const { start, end } = windowAround(records.length, selected, listRowLimit(this.host.terminal.rows));
|
|
155
|
+
if (start > 0) lines.push(theme.fg("dim", ` ↑ ${start} more`));
|
|
156
|
+
const now = Date.now();
|
|
157
|
+
for (let i = start; i < end; i++) {
|
|
158
|
+
const record = records[i];
|
|
159
|
+
if (!record) continue;
|
|
160
|
+
const isSelected = i === selected;
|
|
161
|
+
// The selected row is marked twice: a bar at the left edge and the theme's selection surface.
|
|
162
|
+
// Armed, the bar turns to the error color along with the surface.
|
|
163
|
+
const cursor = isSelected ? theme.fg(armed ? "error" : "accent", "▎ ") : " ";
|
|
164
|
+
const status = isStopping(record)
|
|
165
|
+
? theme.fg(isSelected ? "muted" : "dim", "stopping".padEnd(12))
|
|
166
|
+
: theme.fg(STATUS_COLORS[record.status], STATUS_TEXT[record.status].padEnd(12));
|
|
167
|
+
// The selection surface is lighter than the terminal background, so dim text fades on it.
|
|
168
|
+
// The selected row steps one level up to stay readable.
|
|
169
|
+
const duration = theme.fg(
|
|
170
|
+
isSelected ? "muted" : "dim",
|
|
171
|
+
(record.status === "queued" ? "" : formatDuration(durationOf(record, now))).padStart(7),
|
|
172
|
+
);
|
|
173
|
+
const content = `${cursor}${theme.fg("text", record.agentId.padEnd(6))} ${status} ${duration} ${theme.fg("accent", record.type)} ${record.description}`;
|
|
174
|
+
// The selected row fills the line: the surface runs to the right edge. It is cut to the width
|
|
175
|
+
// first, because cutting it afterwards would drop the background at the cut.
|
|
176
|
+
lines.push(isSelected ? onSelectionSurface(theme, fillToWidth(content, width), armed) : content);
|
|
177
|
+
}
|
|
178
|
+
if (end < records.length) lines.push(theme.fg("dim", ` ↓ ${records.length - end} more`));
|
|
179
|
+
// Armed, the whole key line gives way to the confirmation prompt.
|
|
180
|
+
lines.push("", armed ? theme.fg("error", ARMED_HINT) : theme.fg("dim", "↑↓ select · enter details · x stop · esc close"));
|
|
181
|
+
return lines.map((line) => truncateToWidth(line, width, "…"));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
handleInput(data: string): void {
|
|
185
|
+
const records = this.ordered();
|
|
186
|
+
const index = this.index(records);
|
|
187
|
+
const kb = this.keybindings;
|
|
188
|
+
const current = records[index];
|
|
189
|
+
// Armed (docs/07 §4.4): only x goes on to stop. ↑↓ cancel and still move; every other key, Esc and
|
|
190
|
+
// Enter included, only cancels, so backing out of the stop does not also close the list or open a detail.
|
|
191
|
+
this.confirm.keep(current?.agentId, current !== undefined && isStoppable(current));
|
|
192
|
+
if (this.confirm.isArmed(current?.agentId) && data !== "x") {
|
|
193
|
+
this.confirm.cancel();
|
|
194
|
+
if (!isMoveKey(data, kb)) return this.host.requestRender();
|
|
195
|
+
}
|
|
196
|
+
if (kb.matches(data, "tui.select.cancel") || data === "q") return this.close();
|
|
197
|
+
if (kb.matches(data, "tui.select.up")) this.selectedId = records[Math.max(0, index - 1)]?.agentId;
|
|
198
|
+
else if (kb.matches(data, "tui.select.down")) this.selectedId = records[Math.min(records.length - 1, index + 1)]?.agentId;
|
|
199
|
+
else if (kb.matches(data, "tui.select.confirm")) {
|
|
200
|
+
const id = records[index]?.agentId;
|
|
201
|
+
if (id && !this.closed) this.source.open(id);
|
|
202
|
+
return;
|
|
203
|
+
} else if (data === "x") {
|
|
204
|
+
if (current && isStoppable(current) && this.confirm.press(current.agentId)) this.source.stop(current.agentId);
|
|
205
|
+
} else return;
|
|
206
|
+
this.host.requestRender();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
invalidate(): void {
|
|
210
|
+
// Rendered from live records every time.
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
dispose(): void {
|
|
214
|
+
clearInterval(this.refresh);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private close(): void {
|
|
218
|
+
if (this.closed) return;
|
|
219
|
+
this.closed = true;
|
|
220
|
+
clearInterval(this.refresh);
|
|
221
|
+
this.done();
|
|
222
|
+
}
|
|
223
|
+
}
|