@tt-a1i/openpi 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/README.md +643 -0
- package/SETUP.md +74 -0
- package/THIRD_PARTY_NOTICES.md +16 -0
- package/assets/openpi-package.png +0 -0
- package/assets/readme-hero-mobile.svg +72 -0
- package/assets/readme-hero.svg +118 -0
- package/assets/readme-runtime-mobile.svg +91 -0
- package/assets/readme-runtime.svg +111 -0
- package/extensions/ask-user/handoff.ts +205 -0
- package/extensions/ask-user/index.ts +1110 -0
- package/extensions/ask-user/limits.ts +89 -0
- package/extensions/ask-user/prompt.ts +76 -0
- package/extensions/background-terminals/index.ts +653 -0
- package/extensions/background-terminals/src/domain.ts +99 -0
- package/extensions/background-terminals/src/manager.ts +989 -0
- package/extensions/background-terminals/src/output.ts +84 -0
- package/extensions/background-terminals/src/prompt.ts +195 -0
- package/extensions/background-terminals/src/result-delivery.ts +43 -0
- package/extensions/background-terminals/src/runtime.ts +36 -0
- package/extensions/background-terminals/src/ui/output-view.ts +55 -0
- package/extensions/background-terminals/src/ui/ps.ts +642 -0
- package/extensions/background-terminals/src/ui/tool-result.ts +146 -0
- package/extensions/background-terminals/src/watch.ts +192 -0
- package/extensions/context-pivot/index.ts +222 -0
- package/extensions/copy-all/index.ts +65 -0
- package/extensions/cron/index.ts +173 -0
- package/extensions/cron/schedule.ts +127 -0
- package/extensions/file-mutation-display/index.ts +105 -0
- package/extensions/file-mutation-display/render.ts +107 -0
- package/extensions/file-search/index.ts +515 -0
- package/extensions/file-search/src/args.ts +129 -0
- package/extensions/file-search/src/binaries.ts +419 -0
- package/extensions/file-search/src/output.ts +142 -0
- package/extensions/file-search/src/process.ts +309 -0
- package/extensions/file-search/src/prompt.ts +53 -0
- package/extensions/git-info/index.ts +272 -0
- package/extensions/git-info/src/changed-files-view.ts +414 -0
- package/extensions/git-info/src/process.ts +107 -0
- package/extensions/git-info/src/refresh-coordinator.ts +13 -0
- package/extensions/git-info/src/runtime.ts +28 -0
- package/extensions/goal/controller.ts +794 -0
- package/extensions/goal/index.ts +521 -0
- package/extensions/goal/prompts.ts +122 -0
- package/extensions/goal/state.ts +763 -0
- package/extensions/goal/ui.ts +158 -0
- package/extensions/model-info/index.ts +234 -0
- package/extensions/plan-mode/bash-policy.ts +313 -0
- package/extensions/plan-mode/index.ts +539 -0
- package/extensions/post-edit/index.ts +129 -0
- package/extensions/sessions/LICENSE.upstream +21 -0
- package/extensions/sessions/git-stats.ts +226 -0
- package/extensions/sessions/index.ts +1092 -0
- package/extensions/sessions/sessions.ts +385 -0
- package/extensions/setup/index.ts +408 -0
- package/extensions/shared/activity-status.ts +65 -0
- package/extensions/shared/below-editor-navigation.ts +343 -0
- package/extensions/shared/child-session.ts +352 -0
- package/extensions/shared/context-utilization.ts +47 -0
- package/extensions/shared/dashboard-state.ts +102 -0
- package/extensions/shared/plan-mode-state.ts +65 -0
- package/extensions/shared/setup-config.ts +971 -0
- package/extensions/shared/subagent-roles.ts +22 -0
- package/extensions/shared/terminal-text.ts +38 -0
- package/extensions/shared/tool-call-timeout.ts +104 -0
- package/extensions/shared/worktree.ts +526 -0
- package/extensions/subagents/index.ts +1225 -0
- package/extensions/subagents/navigation.ts +121 -0
- package/extensions/subagents/src/agent-types.ts +543 -0
- package/extensions/subagents/src/backend.ts +63 -0
- package/extensions/subagents/src/backends/pi.ts +493 -0
- package/extensions/subagents/src/backends/stub.ts +296 -0
- package/extensions/subagents/src/by-the-way.ts +21 -0
- package/extensions/subagents/src/domain.ts +271 -0
- package/extensions/subagents/src/format.ts +48 -0
- package/extensions/subagents/src/manager.ts +769 -0
- package/extensions/subagents/src/prompt.ts +190 -0
- package/extensions/subagents/src/result-delivery.ts +20 -0
- package/extensions/subagents/src/runtime.ts +51 -0
- package/extensions/subagents/src/ui/takeover.ts +615 -0
- package/extensions/subagents/src/ui/transcript.ts +293 -0
- package/extensions/subagents/src/ui/wait-result.ts +89 -0
- package/extensions/suggestions/index.ts +172 -0
- package/extensions/suggestions/src/config.ts +12 -0
- package/extensions/suggestions/src/predictor.ts +147 -0
- package/extensions/suggestions/src/prompt.ts +20 -0
- package/extensions/suggestions/src/transcript.ts +233 -0
- package/extensions/suggestions/src/ui.ts +224 -0
- package/extensions/tasks/index.ts +512 -0
- package/extensions/tasks/tasks.ts +649 -0
- package/extensions/tasks/ui.ts +421 -0
- package/extensions/turn-time/index.ts +61 -0
- package/extensions/ui-customization/footer.ts +512 -0
- package/extensions/ui-customization/index.ts +217 -0
- package/extensions/workflows/acceptance.ts +298 -0
- package/extensions/workflows/artifacts.ts +225 -0
- package/extensions/workflows/controller.ts +210 -0
- package/extensions/workflows/dashboard.ts +1226 -0
- package/extensions/workflows/index.ts +1884 -0
- package/extensions/workflows/journal.ts +188 -0
- package/extensions/workflows/meta.ts +250 -0
- package/extensions/workflows/model.ts +423 -0
- package/extensions/workflows/navigation.ts +93 -0
- package/extensions/workflows/prompt.ts +212 -0
- package/extensions/workflows/replay-safety.ts +577 -0
- package/extensions/workflows/runner.ts +786 -0
- package/extensions/workflows/sandbox-child.cjs +402 -0
- package/extensions/workflows/sandbox.ts +397 -0
- package/extensions/workflows/serialization.ts +162 -0
- package/extensions/workflows/worktree-handoff.ts +216 -0
- package/package.json +87 -0
- package/scripts/prepare-effect-tsgo.mjs +16 -0
- package/skills/background-terminals/SKILL.md +30 -0
- package/skills/subagents/SKILL.md +15 -0
- package/themes/github-dark-default.json +89 -0
|
@@ -0,0 +1,615 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Takeover UI for subagents (ported from v1, rendering from the synchronous
|
|
3
|
+
* SubagentReadModel instead of live pi sessions):
|
|
4
|
+
* - SubagentDashboard: full popup (overlay) listing all subagents.
|
|
5
|
+
* - TakeoverView: full interactive view of one subagent with an input line
|
|
6
|
+
* to steer/continue it.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
ExtensionContext,
|
|
11
|
+
KeybindingsManager,
|
|
12
|
+
Theme,
|
|
13
|
+
} from "@earendil-works/pi-coding-agent";
|
|
14
|
+
import type { Component, Focusable, TUI } from "@earendil-works/pi-tui";
|
|
15
|
+
import { Input, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
16
|
+
import { sanitizeTerminalText } from "../../../shared/terminal-text.ts";
|
|
17
|
+
import { formatElapsed, type SubagentSnapshot } from "../domain.ts";
|
|
18
|
+
import { formatContextUtilization } from "../format.ts";
|
|
19
|
+
import type { SubagentReadModel } from "../manager.ts";
|
|
20
|
+
import { TranscriptRenderer, buildTranscriptLines } from "./transcript.ts";
|
|
21
|
+
|
|
22
|
+
export function sanitizeSubagentDisplayLine(value: string) {
|
|
23
|
+
return sanitizeTerminalText(value).replace(/\s+/g, " ").trim();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function configuredKeys(
|
|
27
|
+
keybindings: KeybindingsManager,
|
|
28
|
+
binding: Parameters<KeybindingsManager["getKeys"]>[0],
|
|
29
|
+
) {
|
|
30
|
+
return keybindings.getKeys(binding).join("/") || "unbound";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function statusGlyph(snap: SubagentSnapshot, theme: Theme): string {
|
|
34
|
+
switch (snap.status) {
|
|
35
|
+
case "running":
|
|
36
|
+
return theme.fg("warning", "■");
|
|
37
|
+
case "done":
|
|
38
|
+
return theme.fg("success", "■");
|
|
39
|
+
case "error":
|
|
40
|
+
return theme.fg("error", "■");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function statusWord(snap: SubagentSnapshot, theme: Theme): string {
|
|
45
|
+
switch (snap.status) {
|
|
46
|
+
case "running":
|
|
47
|
+
return theme.fg("warning", "running");
|
|
48
|
+
case "done":
|
|
49
|
+
return theme.fg("success", "done");
|
|
50
|
+
case "error":
|
|
51
|
+
return theme.fg("error", "failed");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// --- Entry points --------------------------------------------------------------
|
|
56
|
+
|
|
57
|
+
export interface TakeoverOptions {
|
|
58
|
+
readonly badge?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function openSubagentTakeover(
|
|
62
|
+
ctx: ExtensionContext,
|
|
63
|
+
view: SubagentReadModel,
|
|
64
|
+
id: string,
|
|
65
|
+
options?: TakeoverOptions,
|
|
66
|
+
) {
|
|
67
|
+
if (!view.get(id)) return;
|
|
68
|
+
await ctx.ui.custom<null>(
|
|
69
|
+
(tui, theme, keybindings, done) =>
|
|
70
|
+
new TakeoverView(tui, theme, keybindings, id, view, done, options),
|
|
71
|
+
{
|
|
72
|
+
overlay: true,
|
|
73
|
+
overlayOptions: { anchor: "center", width: "100%", maxHeight: "100%" },
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export async function openSubagentPicker(
|
|
79
|
+
ctx: ExtensionContext,
|
|
80
|
+
view: SubagentReadModel,
|
|
81
|
+
initialId?: string,
|
|
82
|
+
) {
|
|
83
|
+
const selection: DashboardSelection = { id: initialId, index: 0 };
|
|
84
|
+
|
|
85
|
+
while (true) {
|
|
86
|
+
if (view.size() === 0) {
|
|
87
|
+
ctx.ui.notify("No subagents", "info");
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const picked = await ctx.ui.custom<string | null>(
|
|
92
|
+
(tui, theme, keybindings, done) =>
|
|
93
|
+
new SubagentDashboard(tui, theme, keybindings, view, selection, done),
|
|
94
|
+
{
|
|
95
|
+
overlay: true,
|
|
96
|
+
overlayOptions: { anchor: "center", width: "100%", maxHeight: "100%" },
|
|
97
|
+
},
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
if (!picked) return;
|
|
101
|
+
if (!view.get(picked)) continue;
|
|
102
|
+
|
|
103
|
+
await openSubagentTakeover(ctx, view, picked);
|
|
104
|
+
// After leaving the takeover view, fall back to the dashboard.
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// --- Dashboard (fullscreen overlay) ----------------------------------------------
|
|
109
|
+
|
|
110
|
+
export interface DashboardSelection {
|
|
111
|
+
id?: string;
|
|
112
|
+
index: number;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function reconcileDashboardSelection(
|
|
116
|
+
selection: DashboardSelection,
|
|
117
|
+
subs: ReadonlyArray<Pick<SubagentSnapshot, "id">>,
|
|
118
|
+
) {
|
|
119
|
+
const stableIndex = selection.id
|
|
120
|
+
? subs.findIndex((snap) => snap.id === selection.id)
|
|
121
|
+
: -1;
|
|
122
|
+
selection.index =
|
|
123
|
+
stableIndex >= 0
|
|
124
|
+
? stableIndex
|
|
125
|
+
: Math.min(Math.max(0, selection.index), Math.max(0, subs.length - 1));
|
|
126
|
+
selection.id = subs[selection.index]?.id;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
class SubagentDashboard implements Component {
|
|
130
|
+
private tui: TUI;
|
|
131
|
+
private theme: Theme;
|
|
132
|
+
private keybindings: KeybindingsManager;
|
|
133
|
+
private view: SubagentReadModel;
|
|
134
|
+
private selection: DashboardSelection;
|
|
135
|
+
private done: (value: string | null) => void;
|
|
136
|
+
|
|
137
|
+
private closed = false;
|
|
138
|
+
private ticker: ReturnType<typeof setInterval>;
|
|
139
|
+
private unsubChange: () => void;
|
|
140
|
+
|
|
141
|
+
constructor(
|
|
142
|
+
tui: TUI,
|
|
143
|
+
theme: Theme,
|
|
144
|
+
keybindings: KeybindingsManager,
|
|
145
|
+
view: SubagentReadModel,
|
|
146
|
+
selection: DashboardSelection,
|
|
147
|
+
done: (value: string | null) => void,
|
|
148
|
+
) {
|
|
149
|
+
this.tui = tui;
|
|
150
|
+
this.theme = theme;
|
|
151
|
+
this.keybindings = keybindings;
|
|
152
|
+
this.view = view;
|
|
153
|
+
this.selection = selection;
|
|
154
|
+
this.done = done;
|
|
155
|
+
// Elapsed times, token counts, and statuses tick along at 1Hz.
|
|
156
|
+
this.ticker = setInterval(() => this.tui.requestRender(), 1000);
|
|
157
|
+
this.unsubChange = view.subscribe(() => this.tui.requestRender());
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private subs(): ReadonlyArray<SubagentSnapshot> {
|
|
161
|
+
return this.view.list();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private cleanup() {
|
|
165
|
+
if (this.closed) return false;
|
|
166
|
+
this.closed = true;
|
|
167
|
+
clearInterval(this.ticker);
|
|
168
|
+
this.unsubChange();
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private close(result: string | null) {
|
|
173
|
+
if (this.cleanup()) this.done(result);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
dispose(): void {
|
|
177
|
+
this.cleanup();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
handleInput(data: string): void {
|
|
181
|
+
const subs = this.subs();
|
|
182
|
+
reconcileDashboardSelection(this.selection, subs);
|
|
183
|
+
|
|
184
|
+
if (this.keybindings.matches(data, "tui.select.cancel")) {
|
|
185
|
+
this.close(null);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (this.keybindings.matches(data, "tui.select.confirm")) {
|
|
189
|
+
const snap = subs[this.selection.index];
|
|
190
|
+
if (snap) this.close(snap.id);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (this.keybindings.matches(data, "tui.select.up") || data === "k") {
|
|
194
|
+
if (subs.length > 0) {
|
|
195
|
+
this.selection.index =
|
|
196
|
+
(this.selection.index - 1 + subs.length) % subs.length;
|
|
197
|
+
this.selection.id = subs[this.selection.index]?.id;
|
|
198
|
+
this.tui.requestRender();
|
|
199
|
+
}
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (this.keybindings.matches(data, "tui.select.down") || data === "j") {
|
|
203
|
+
if (subs.length > 0) {
|
|
204
|
+
this.selection.index = (this.selection.index + 1) % subs.length;
|
|
205
|
+
this.selection.id = subs[this.selection.index]?.id;
|
|
206
|
+
this.tui.requestRender();
|
|
207
|
+
}
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (data === "x") {
|
|
211
|
+
const snap = subs[this.selection.index];
|
|
212
|
+
if (snap && snap.status === "running") this.view.requestAbort(snap.id);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private pad(text: string, width: number): string {
|
|
218
|
+
const truncated = truncateToWidth(text, width);
|
|
219
|
+
return truncated + " ".repeat(Math.max(0, width - visibleWidth(truncated)));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private borderSegment(width: number, title: string): string {
|
|
223
|
+
const theme = this.theme;
|
|
224
|
+
const label = title
|
|
225
|
+
? ` ${truncateToWidth(title, Math.max(0, width - 3))} `
|
|
226
|
+
: "";
|
|
227
|
+
const labelWidth = visibleWidth(label);
|
|
228
|
+
return (
|
|
229
|
+
theme.fg("border", "─") +
|
|
230
|
+
(label ? theme.fg("text", label) : "") +
|
|
231
|
+
theme.fg("border", "─".repeat(Math.max(0, width - 1 - labelWidth)))
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
render(width: number): string[] {
|
|
236
|
+
const theme = this.theme;
|
|
237
|
+
const subs = this.subs();
|
|
238
|
+
reconcileDashboardSelection(this.selection, subs);
|
|
239
|
+
|
|
240
|
+
const rows = this.tui.terminal.rows || 30;
|
|
241
|
+
// Render exactly terminal rows - 1 so the overlay covers the header,
|
|
242
|
+
// chat, editor, and extra footer lines while leaving pi's final footer
|
|
243
|
+
// row visible.
|
|
244
|
+
const bodyHeight = Math.max(6, rows - 5);
|
|
245
|
+
const innerWidth = width - 2;
|
|
246
|
+
|
|
247
|
+
const lines: string[] = [];
|
|
248
|
+
|
|
249
|
+
// Header: title left, count right
|
|
250
|
+
const headerLeft = theme.fg("accent", theme.bold("Subagents"));
|
|
251
|
+
const headerRight = theme.fg(
|
|
252
|
+
"muted",
|
|
253
|
+
`${subs.length} agent${subs.length === 1 ? "" : "s"}`,
|
|
254
|
+
);
|
|
255
|
+
const headerPad = Math.max(
|
|
256
|
+
1,
|
|
257
|
+
width - visibleWidth(headerLeft) - visibleWidth(headerRight) - 4,
|
|
258
|
+
);
|
|
259
|
+
lines.push(
|
|
260
|
+
truncateToWidth(
|
|
261
|
+
` ${headerLeft}${" ".repeat(headerPad)}${headerRight} `,
|
|
262
|
+
width,
|
|
263
|
+
),
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
// Top border with panel title
|
|
267
|
+
const settled = subs.filter((s) => s.status !== "running").length;
|
|
268
|
+
lines.push(
|
|
269
|
+
theme.fg("border", "╭") +
|
|
270
|
+
this.borderSegment(innerWidth, `agents · ${settled}/${subs.length}`) +
|
|
271
|
+
theme.fg("border", "╮"),
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
// Rows
|
|
275
|
+
const divider = theme.fg("border", "│");
|
|
276
|
+
const rowLines = this.renderRows(subs, innerWidth, bodyHeight);
|
|
277
|
+
for (let i = 0; i < bodyHeight; i++) {
|
|
278
|
+
lines.push(divider + this.pad(rowLines[i] ?? "", innerWidth) + divider);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Bottom border
|
|
282
|
+
lines.push(
|
|
283
|
+
theme.fg("border", "╰") +
|
|
284
|
+
theme.fg("border", "─".repeat(innerWidth)) +
|
|
285
|
+
theme.fg("border", "╯"),
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
// Hints
|
|
289
|
+
lines.push(
|
|
290
|
+
truncateToWidth(
|
|
291
|
+
theme.fg(
|
|
292
|
+
"dim",
|
|
293
|
+
` ${configuredKeys(this.keybindings, "tui.select.up")}/${configuredKeys(this.keybindings, "tui.select.down")}/jk select · ${configuredKeys(this.keybindings, "tui.select.confirm")} take over · x abort · ${configuredKeys(this.keybindings, "tui.select.cancel")} close`,
|
|
294
|
+
),
|
|
295
|
+
width,
|
|
296
|
+
),
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
return lines;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
private renderRows(
|
|
303
|
+
subs: ReadonlyArray<SubagentSnapshot>,
|
|
304
|
+
width: number,
|
|
305
|
+
height: number,
|
|
306
|
+
): string[] {
|
|
307
|
+
const theme = this.theme;
|
|
308
|
+
const out: string[] = [];
|
|
309
|
+
|
|
310
|
+
// Scroll window around selection
|
|
311
|
+
let start = 0;
|
|
312
|
+
if (subs.length > height) {
|
|
313
|
+
start = Math.min(
|
|
314
|
+
Math.max(0, this.selection.index - Math.floor(height / 2)),
|
|
315
|
+
subs.length - height,
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
const visible = subs.slice(start, start + height);
|
|
319
|
+
|
|
320
|
+
for (let i = 0; i < visible.length; i++) {
|
|
321
|
+
const snap = visible[i];
|
|
322
|
+
const index = start + i;
|
|
323
|
+
const isSelected = index === this.selection.index;
|
|
324
|
+
|
|
325
|
+
// Left: marker, status square, title
|
|
326
|
+
const marker = isSelected ? theme.fg("accent", "❯") : " ";
|
|
327
|
+
const safeTitle = sanitizeSubagentDisplayLine(snap.title) || snap.id;
|
|
328
|
+
const title = isSelected
|
|
329
|
+
? theme.fg("accent", safeTitle)
|
|
330
|
+
: theme.fg("text", safeTitle);
|
|
331
|
+
const left = ` ${marker} ${statusGlyph(snap, theme)} ${title}`;
|
|
332
|
+
|
|
333
|
+
// Right: backend · model · context utilization · elapsed · status
|
|
334
|
+
const utilization = formatContextUtilization(snap.usage);
|
|
335
|
+
const dot = theme.fg("dim", " · ");
|
|
336
|
+
const rightParts = [
|
|
337
|
+
theme.fg("muted", snap.backend),
|
|
338
|
+
theme.fg(
|
|
339
|
+
"muted",
|
|
340
|
+
sanitizeSubagentDisplayLine(snap.meta.modelLabel ?? "?") || "?",
|
|
341
|
+
),
|
|
342
|
+
...(utilization ? [theme.fg("muted", utilization)] : []),
|
|
343
|
+
theme.fg("muted", formatElapsed(snap)),
|
|
344
|
+
statusWord(snap, theme),
|
|
345
|
+
];
|
|
346
|
+
const right = `${rightParts.join(dot)} `;
|
|
347
|
+
|
|
348
|
+
const rightWidth = visibleWidth(right);
|
|
349
|
+
const leftMax = Math.max(0, width - rightWidth - 2);
|
|
350
|
+
const leftTruncated = truncateToWidth(left, leftMax);
|
|
351
|
+
const gap = Math.max(2, width - visibleWidth(leftTruncated) - rightWidth);
|
|
352
|
+
out.push(truncateToWidth(leftTruncated + " ".repeat(gap) + right, width));
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (start > 0) {
|
|
356
|
+
out[0] = truncateToWidth(theme.fg("dim", ` ... ${start} more`), width);
|
|
357
|
+
}
|
|
358
|
+
if (start + height < subs.length) {
|
|
359
|
+
out[out.length - 1] = truncateToWidth(
|
|
360
|
+
theme.fg("dim", ` ... ${subs.length - start - height} more`),
|
|
361
|
+
width,
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
return out;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
invalidate(): void {}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// --- Takeover view ------------------------------------------------------------
|
|
371
|
+
|
|
372
|
+
const TRANSCRIPT_SCROLL_STEP = 6;
|
|
373
|
+
|
|
374
|
+
class TakeoverView implements Component, Focusable {
|
|
375
|
+
private tui: TUI;
|
|
376
|
+
private theme: Theme;
|
|
377
|
+
private keybindings: KeybindingsManager;
|
|
378
|
+
private id: string;
|
|
379
|
+
private view: SubagentReadModel;
|
|
380
|
+
private done: (value: null) => void;
|
|
381
|
+
private options?: TakeoverOptions;
|
|
382
|
+
|
|
383
|
+
private input = new Input();
|
|
384
|
+
private transcriptRenderer = new TranscriptRenderer();
|
|
385
|
+
/** Scroll offset in lines from the bottom of the transcript. 0 = pinned to bottom. */
|
|
386
|
+
private scrollOffset = 0;
|
|
387
|
+
private unsubscribe: () => void;
|
|
388
|
+
private renderTimer?: ReturnType<typeof setTimeout>;
|
|
389
|
+
private ticker: ReturnType<typeof setInterval>;
|
|
390
|
+
private closed = false;
|
|
391
|
+
|
|
392
|
+
private _focused = false;
|
|
393
|
+
get focused(): boolean {
|
|
394
|
+
return this._focused;
|
|
395
|
+
}
|
|
396
|
+
set focused(value: boolean) {
|
|
397
|
+
this._focused = value;
|
|
398
|
+
this.input.focused = value;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
constructor(
|
|
402
|
+
tui: TUI,
|
|
403
|
+
theme: Theme,
|
|
404
|
+
keybindings: KeybindingsManager,
|
|
405
|
+
id: string,
|
|
406
|
+
view: SubagentReadModel,
|
|
407
|
+
done: (value: null) => void,
|
|
408
|
+
options?: TakeoverOptions,
|
|
409
|
+
) {
|
|
410
|
+
this.tui = tui;
|
|
411
|
+
this.theme = theme;
|
|
412
|
+
this.keybindings = keybindings;
|
|
413
|
+
this.id = id;
|
|
414
|
+
this.view = view;
|
|
415
|
+
this.done = done;
|
|
416
|
+
this.options = options;
|
|
417
|
+
this.unsubscribe = view.subscribeTo(id, () => this.scheduleRender());
|
|
418
|
+
// Elapsed time in the header ticks along at 1Hz.
|
|
419
|
+
this.ticker = setInterval(() => this.tui.requestRender(), 1000);
|
|
420
|
+
this.input.onSubmit = (value: string) => {
|
|
421
|
+
const text = value.trim();
|
|
422
|
+
if (!text) return;
|
|
423
|
+
this.input.setValue("");
|
|
424
|
+
this.view.requestSend(this.id, text);
|
|
425
|
+
this.scrollOffset = 0;
|
|
426
|
+
this.tui.requestRender();
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private snap(): SubagentSnapshot | undefined {
|
|
431
|
+
return this.view.get(this.id);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
private scheduleRender() {
|
|
435
|
+
if (this.renderTimer) return;
|
|
436
|
+
// Streaming can emit an event per token. Limit terminal repaints so this
|
|
437
|
+
// view cannot starve input handling or make the child look frozen.
|
|
438
|
+
this.renderTimer = setTimeout(() => {
|
|
439
|
+
this.renderTimer = undefined;
|
|
440
|
+
if (!this.closed) this.tui.requestRender();
|
|
441
|
+
}, 50);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
private cleanup() {
|
|
445
|
+
if (this.closed) return false;
|
|
446
|
+
this.closed = true;
|
|
447
|
+
this.unsubscribe();
|
|
448
|
+
clearInterval(this.ticker);
|
|
449
|
+
if (this.renderTimer) clearTimeout(this.renderTimer);
|
|
450
|
+
this.renderTimer = undefined;
|
|
451
|
+
return true;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
private close() {
|
|
455
|
+
if (this.cleanup()) this.done(null);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
dispose(): void {
|
|
459
|
+
this.cleanup();
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
handleInput(data: string): void {
|
|
463
|
+
if (this.keybindings.matches(data, "app.clear")) {
|
|
464
|
+
const snap = this.snap();
|
|
465
|
+
if (snap?.status === "running") this.view.requestAbort(this.id);
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
if (
|
|
469
|
+
this.keybindings.matches(data, "app.interrupt") ||
|
|
470
|
+
this.keybindings.matches(data, "tui.select.cancel")
|
|
471
|
+
) {
|
|
472
|
+
this.close();
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
if (this.keybindings.matches(data, "tui.editor.cursorUp")) {
|
|
476
|
+
this.scrollOffset += TRANSCRIPT_SCROLL_STEP;
|
|
477
|
+
this.tui.requestRender();
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
if (this.keybindings.matches(data, "tui.editor.cursorDown")) {
|
|
481
|
+
this.scrollOffset = Math.max(
|
|
482
|
+
0,
|
|
483
|
+
this.scrollOffset - TRANSCRIPT_SCROLL_STEP,
|
|
484
|
+
);
|
|
485
|
+
this.tui.requestRender();
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
if (this.keybindings.matches(data, "tui.editor.pageUp")) {
|
|
489
|
+
this.scrollOffset += this.viewportHeight();
|
|
490
|
+
this.tui.requestRender();
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
if (this.keybindings.matches(data, "tui.editor.pageDown")) {
|
|
494
|
+
this.scrollOffset = Math.max(
|
|
495
|
+
0,
|
|
496
|
+
this.scrollOffset - this.viewportHeight(),
|
|
497
|
+
);
|
|
498
|
+
this.tui.requestRender();
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
this.input.handleInput(data);
|
|
502
|
+
this.tui.requestRender();
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
private viewportHeight(): number {
|
|
506
|
+
const rows = this.tui.terminal.rows || 30;
|
|
507
|
+
// The complete view renders viewport + 7 chrome rows. Using rows - 8
|
|
508
|
+
// makes the overlay exactly terminal rows - 1.
|
|
509
|
+
return Math.max(6, rows - 8);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
render(width: number): string[] {
|
|
513
|
+
const theme = this.theme;
|
|
514
|
+
const border = theme.fg("borderAccent", "─".repeat(Math.max(1, width)));
|
|
515
|
+
const lines: string[] = [];
|
|
516
|
+
const snap = this.snap();
|
|
517
|
+
|
|
518
|
+
if (!snap) {
|
|
519
|
+
lines.push(border);
|
|
520
|
+
lines.push(theme.fg("dim", `${this.id} is no longer tracked`));
|
|
521
|
+
lines.push(border);
|
|
522
|
+
return lines;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
lines.push(border);
|
|
526
|
+
const utilization = formatContextUtilization(snap.usage);
|
|
527
|
+
const header =
|
|
528
|
+
`${statusGlyph(snap, theme)} ` +
|
|
529
|
+
theme.fg(
|
|
530
|
+
"accent",
|
|
531
|
+
theme.bold(sanitizeSubagentDisplayLine(snap.title) || snap.id),
|
|
532
|
+
) +
|
|
533
|
+
theme.fg("muted", ` · ${snap.status} · ${formatElapsed(snap)}`) +
|
|
534
|
+
(this.options?.badge
|
|
535
|
+
? theme.fg(
|
|
536
|
+
"muted",
|
|
537
|
+
` · ${sanitizeSubagentDisplayLine(this.options.badge)}`,
|
|
538
|
+
)
|
|
539
|
+
: "") +
|
|
540
|
+
theme.fg(
|
|
541
|
+
"dim",
|
|
542
|
+
` · ${snap.backend}: ${sanitizeSubagentDisplayLine(snap.meta.modelLabel ?? "?") || "?"}`,
|
|
543
|
+
) +
|
|
544
|
+
(utilization ? theme.fg("dim", ` · ${utilization}`) : "");
|
|
545
|
+
lines.push(truncateToWidth(header, width));
|
|
546
|
+
lines.push(border);
|
|
547
|
+
|
|
548
|
+
// Fixed-height transcript viewport. Error and scroll status consume rows
|
|
549
|
+
// inside the viewport so streaming/scrolling never changes overlay height.
|
|
550
|
+
const transcript = buildTranscriptLines(
|
|
551
|
+
snap,
|
|
552
|
+
width,
|
|
553
|
+
theme,
|
|
554
|
+
this.transcriptRenderer,
|
|
555
|
+
);
|
|
556
|
+
const viewport = this.viewportHeight();
|
|
557
|
+
const errorRows = snap.errorText ? 1 : 0;
|
|
558
|
+
const scrollRows = this.scrollOffset > 0 ? 1 : 0;
|
|
559
|
+
const transcriptCapacity = Math.max(1, viewport - errorRows - scrollRows);
|
|
560
|
+
const maxOffset = Math.max(0, transcript.length - transcriptCapacity);
|
|
561
|
+
if (this.scrollOffset > maxOffset) this.scrollOffset = maxOffset;
|
|
562
|
+
|
|
563
|
+
const body: string[] = [];
|
|
564
|
+
if (snap.errorText) {
|
|
565
|
+
body.push(
|
|
566
|
+
truncateToWidth(
|
|
567
|
+
theme.fg(
|
|
568
|
+
"error",
|
|
569
|
+
`error: ${sanitizeSubagentDisplayLine(snap.errorText)}`,
|
|
570
|
+
),
|
|
571
|
+
width,
|
|
572
|
+
),
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
const capacity = Math.max(
|
|
577
|
+
1,
|
|
578
|
+
viewport - body.length - (this.scrollOffset > 0 ? 1 : 0),
|
|
579
|
+
);
|
|
580
|
+
const end = transcript.length - this.scrollOffset;
|
|
581
|
+
const visible = transcript.slice(Math.max(0, end - capacity), end);
|
|
582
|
+
if (visible.length === 0) body.push(theme.fg("dim", "(no output yet)"));
|
|
583
|
+
else body.push(...visible);
|
|
584
|
+
|
|
585
|
+
if (this.scrollOffset > 0) {
|
|
586
|
+
body.push(
|
|
587
|
+
truncateToWidth(
|
|
588
|
+
theme.fg("dim", `... ${this.scrollOffset} lines below · ↓/pgdn`),
|
|
589
|
+
width,
|
|
590
|
+
),
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
while (body.length < viewport) body.push("");
|
|
594
|
+
lines.push(...body.slice(0, viewport));
|
|
595
|
+
|
|
596
|
+
lines.push(border);
|
|
597
|
+
lines.push(...this.input.render(width));
|
|
598
|
+
lines.push(
|
|
599
|
+
truncateToWidth(
|
|
600
|
+
theme.fg(
|
|
601
|
+
"dim",
|
|
602
|
+
`${configuredKeys(this.keybindings, "tui.input.submit")} send · ${configuredKeys(this.keybindings, "app.interrupt")} back · ${configuredKeys(this.keybindings, "app.clear")} abort run · ${configuredKeys(this.keybindings, "tui.editor.cursorUp")}/${configuredKeys(this.keybindings, "tui.editor.cursorDown")} scroll · ${configuredKeys(this.keybindings, "tui.editor.pageUp")}/${configuredKeys(this.keybindings, "tui.editor.pageDown")} page`,
|
|
603
|
+
),
|
|
604
|
+
width,
|
|
605
|
+
),
|
|
606
|
+
);
|
|
607
|
+
lines.push(border);
|
|
608
|
+
return lines;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
invalidate(): void {
|
|
612
|
+
this.input.invalidate();
|
|
613
|
+
this.transcriptRenderer.invalidate();
|
|
614
|
+
}
|
|
615
|
+
}
|