@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,642 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* /ps UI — two-stage full-screen overlay over the synchronous
|
|
3
|
+
* TerminalReadModel:
|
|
4
|
+
* - TerminalDashboard: list of all tracked terminals (select, kill, open).
|
|
5
|
+
* - TerminalDetailView: read-only inspector for one terminal — metadata,
|
|
6
|
+
* stdout/stderr toggle, scrolling, live tail. No input surface: background
|
|
7
|
+
* terminals have no stdin by design.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type {
|
|
11
|
+
ExtensionCommandContext,
|
|
12
|
+
KeybindingsManager,
|
|
13
|
+
Theme,
|
|
14
|
+
} from "@earendil-works/pi-coding-agent";
|
|
15
|
+
import { formatSize } from "@earendil-works/pi-coding-agent";
|
|
16
|
+
import type { Component, TUI } from "@earendil-works/pi-tui";
|
|
17
|
+
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
18
|
+
import {
|
|
19
|
+
formatDuration,
|
|
20
|
+
formatElapsed,
|
|
21
|
+
formatExit,
|
|
22
|
+
type TerminalSnapshot,
|
|
23
|
+
} from "../domain.ts";
|
|
24
|
+
import type { TerminalReadModel } from "../manager.ts";
|
|
25
|
+
import { createOutputLineCache, sanitizeText } from "./output-view.ts";
|
|
26
|
+
|
|
27
|
+
/** One-line-safe rendering of model-provided text (titles, commands): a
|
|
28
|
+
* newline or control char inside a fixed-height row desyncs the renderer. */
|
|
29
|
+
function oneLine(text: string) {
|
|
30
|
+
return sanitizeText(text.replace(/\s+/g, " "));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function configuredKeys(
|
|
34
|
+
keybindings: KeybindingsManager,
|
|
35
|
+
binding: Parameters<KeybindingsManager["getKeys"]>[0],
|
|
36
|
+
) {
|
|
37
|
+
return keybindings.getKeys(binding).join("/") || "unbound";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function statusGlyph(snap: TerminalSnapshot, theme: Theme) {
|
|
41
|
+
switch (snap.status) {
|
|
42
|
+
case "running":
|
|
43
|
+
return theme.fg("warning", "■");
|
|
44
|
+
case "done":
|
|
45
|
+
return theme.fg("success", "■");
|
|
46
|
+
case "failed":
|
|
47
|
+
return theme.fg("error", "■");
|
|
48
|
+
case "killed":
|
|
49
|
+
return theme.fg("muted", "■");
|
|
50
|
+
case "timed_out":
|
|
51
|
+
return theme.fg("error", "■");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function statusWord(snap: TerminalSnapshot, theme: Theme) {
|
|
56
|
+
switch (snap.status) {
|
|
57
|
+
case "running":
|
|
58
|
+
return theme.fg("warning", "running");
|
|
59
|
+
case "done":
|
|
60
|
+
return theme.fg("success", "done");
|
|
61
|
+
case "failed":
|
|
62
|
+
return theme.fg("error", "failed");
|
|
63
|
+
case "killed":
|
|
64
|
+
return theme.fg("muted", "killed");
|
|
65
|
+
case "timed_out":
|
|
66
|
+
return theme.fg("error", "timed out");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// --- Entry point ---------------------------------------------------------------
|
|
71
|
+
|
|
72
|
+
export async function openTerminalPicker(
|
|
73
|
+
ctx: ExtensionCommandContext,
|
|
74
|
+
view: TerminalReadModel,
|
|
75
|
+
) {
|
|
76
|
+
const selection: DashboardSelection = { index: 0 };
|
|
77
|
+
|
|
78
|
+
while (true) {
|
|
79
|
+
if (view.size() === 0) {
|
|
80
|
+
ctx.ui.notify("No background terminals", "info");
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const picked = await ctx.ui.custom<string | null>(
|
|
85
|
+
(tui, theme, keybindings, done) =>
|
|
86
|
+
new TerminalDashboard(tui, theme, keybindings, view, selection, done),
|
|
87
|
+
{
|
|
88
|
+
overlay: true,
|
|
89
|
+
overlayOptions: { anchor: "center", width: "100%", maxHeight: "100%" },
|
|
90
|
+
},
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
if (!picked) return;
|
|
94
|
+
if (!view.get(picked)) continue;
|
|
95
|
+
|
|
96
|
+
await ctx.ui.custom<null>(
|
|
97
|
+
(tui, theme, keybindings, done) =>
|
|
98
|
+
new TerminalDetailView(tui, theme, keybindings, picked, view, done),
|
|
99
|
+
{
|
|
100
|
+
overlay: true,
|
|
101
|
+
overlayOptions: { anchor: "center", width: "100%", maxHeight: "100%" },
|
|
102
|
+
},
|
|
103
|
+
);
|
|
104
|
+
// After leaving the detail 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
|
+
terminals: ReadonlyArray<Pick<TerminalSnapshot, "id">>,
|
|
118
|
+
) {
|
|
119
|
+
const stableIndex = selection.id
|
|
120
|
+
? terminals.findIndex((snap) => snap.id === selection.id)
|
|
121
|
+
: -1;
|
|
122
|
+
selection.index =
|
|
123
|
+
stableIndex >= 0
|
|
124
|
+
? stableIndex
|
|
125
|
+
: Math.min(
|
|
126
|
+
Math.max(0, selection.index),
|
|
127
|
+
Math.max(0, terminals.length - 1),
|
|
128
|
+
);
|
|
129
|
+
selection.id = terminals[selection.index]?.id;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
class TerminalDashboard implements Component {
|
|
133
|
+
private tui: TUI;
|
|
134
|
+
private theme: Theme;
|
|
135
|
+
private keybindings: KeybindingsManager;
|
|
136
|
+
private view: TerminalReadModel;
|
|
137
|
+
private selection: DashboardSelection;
|
|
138
|
+
private done: (value: string | null) => void;
|
|
139
|
+
|
|
140
|
+
private closed = false;
|
|
141
|
+
private ticker: ReturnType<typeof setInterval>;
|
|
142
|
+
private unsubChange: () => void;
|
|
143
|
+
|
|
144
|
+
constructor(
|
|
145
|
+
tui: TUI,
|
|
146
|
+
theme: Theme,
|
|
147
|
+
keybindings: KeybindingsManager,
|
|
148
|
+
view: TerminalReadModel,
|
|
149
|
+
selection: DashboardSelection,
|
|
150
|
+
done: (value: string | null) => void,
|
|
151
|
+
) {
|
|
152
|
+
this.tui = tui;
|
|
153
|
+
this.theme = theme;
|
|
154
|
+
this.keybindings = keybindings;
|
|
155
|
+
this.view = view;
|
|
156
|
+
this.selection = selection;
|
|
157
|
+
this.done = done;
|
|
158
|
+
// Elapsed times and output sizes tick along at 1Hz.
|
|
159
|
+
this.ticker = setInterval(() => this.tui.requestRender(), 1000);
|
|
160
|
+
this.unsubChange = view.subscribe(() => this.tui.requestRender());
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private terminals(): ReadonlyArray<TerminalSnapshot> {
|
|
164
|
+
return this.view.list();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private cleanup() {
|
|
168
|
+
if (this.closed) return false;
|
|
169
|
+
this.closed = true;
|
|
170
|
+
clearInterval(this.ticker);
|
|
171
|
+
this.unsubChange();
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private close(result: string | null) {
|
|
176
|
+
if (this.cleanup()) this.done(result);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
dispose(): void {
|
|
180
|
+
this.cleanup();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
handleInput(data: string): void {
|
|
184
|
+
const terminals = this.terminals();
|
|
185
|
+
reconcileDashboardSelection(this.selection, terminals);
|
|
186
|
+
|
|
187
|
+
if (this.keybindings.matches(data, "tui.select.cancel")) {
|
|
188
|
+
this.close(null);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (this.keybindings.matches(data, "tui.select.confirm")) {
|
|
192
|
+
const snap = terminals[this.selection.index];
|
|
193
|
+
if (snap) this.close(snap.id);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (this.keybindings.matches(data, "tui.select.up") || data === "k") {
|
|
197
|
+
if (terminals.length > 0) {
|
|
198
|
+
this.selection.index =
|
|
199
|
+
(this.selection.index - 1 + terminals.length) % terminals.length;
|
|
200
|
+
this.selection.id = terminals[this.selection.index]?.id;
|
|
201
|
+
this.tui.requestRender();
|
|
202
|
+
}
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
if (this.keybindings.matches(data, "tui.select.down") || data === "j") {
|
|
206
|
+
if (terminals.length > 0) {
|
|
207
|
+
this.selection.index = (this.selection.index + 1) % terminals.length;
|
|
208
|
+
this.selection.id = terminals[this.selection.index]?.id;
|
|
209
|
+
this.tui.requestRender();
|
|
210
|
+
}
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (data === "x") {
|
|
214
|
+
const snap = terminals[this.selection.index];
|
|
215
|
+
if (snap && snap.status === "running") this.view.requestKill(snap.id);
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private pad(text: string, width: number): string {
|
|
221
|
+
const truncated = truncateToWidth(text, width);
|
|
222
|
+
return truncated + " ".repeat(Math.max(0, width - visibleWidth(truncated)));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private borderSegment(width: number, title: string): string {
|
|
226
|
+
const theme = this.theme;
|
|
227
|
+
const label = title
|
|
228
|
+
? ` ${truncateToWidth(title, Math.max(0, width - 3))} `
|
|
229
|
+
: "";
|
|
230
|
+
const labelWidth = visibleWidth(label);
|
|
231
|
+
return (
|
|
232
|
+
theme.fg("border", "─") +
|
|
233
|
+
(label ? theme.fg("text", label) : "") +
|
|
234
|
+
theme.fg("border", "─".repeat(Math.max(0, width - 1 - labelWidth)))
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
render(width: number): string[] {
|
|
239
|
+
const theme = this.theme;
|
|
240
|
+
const terminals = this.terminals();
|
|
241
|
+
reconcileDashboardSelection(this.selection, terminals);
|
|
242
|
+
|
|
243
|
+
const rows = this.tui.terminal.rows || 30;
|
|
244
|
+
// Render exactly terminal rows - 1 so the overlay covers the header,
|
|
245
|
+
// chat, editor, and extra footer lines while leaving pi's final footer
|
|
246
|
+
// row visible.
|
|
247
|
+
const bodyHeight = Math.max(6, rows - 5);
|
|
248
|
+
const innerWidth = width - 2;
|
|
249
|
+
|
|
250
|
+
const lines: string[] = [];
|
|
251
|
+
|
|
252
|
+
// Header: title left, count right
|
|
253
|
+
const headerLeft = theme.fg("accent", theme.bold("Background terminals"));
|
|
254
|
+
const headerRight = theme.fg(
|
|
255
|
+
"muted",
|
|
256
|
+
`${terminals.length} terminal${terminals.length === 1 ? "" : "s"}`,
|
|
257
|
+
);
|
|
258
|
+
const headerPad = Math.max(
|
|
259
|
+
1,
|
|
260
|
+
width - visibleWidth(headerLeft) - visibleWidth(headerRight) - 4,
|
|
261
|
+
);
|
|
262
|
+
lines.push(
|
|
263
|
+
truncateToWidth(
|
|
264
|
+
` ${headerLeft}${" ".repeat(headerPad)}${headerRight} `,
|
|
265
|
+
width,
|
|
266
|
+
),
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
// Top border with panel title
|
|
270
|
+
const running = terminals.filter((s) => s.status === "running").length;
|
|
271
|
+
lines.push(
|
|
272
|
+
theme.fg("border", "╭") +
|
|
273
|
+
this.borderSegment(
|
|
274
|
+
innerWidth,
|
|
275
|
+
`terminals · ${running} running / ${terminals.length}`,
|
|
276
|
+
) +
|
|
277
|
+
theme.fg("border", "╮"),
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
// Rows
|
|
281
|
+
const divider = theme.fg("border", "│");
|
|
282
|
+
const rowLines = this.renderRows(terminals, innerWidth, bodyHeight);
|
|
283
|
+
for (let i = 0; i < bodyHeight; i++) {
|
|
284
|
+
lines.push(divider + this.pad(rowLines[i] ?? "", innerWidth) + divider);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// Bottom border
|
|
288
|
+
lines.push(
|
|
289
|
+
theme.fg("border", "╰") +
|
|
290
|
+
theme.fg("border", "─".repeat(Math.max(0, innerWidth))) +
|
|
291
|
+
theme.fg("border", "╯"),
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
// Hints
|
|
295
|
+
lines.push(
|
|
296
|
+
truncateToWidth(
|
|
297
|
+
theme.fg(
|
|
298
|
+
"dim",
|
|
299
|
+
` ${configuredKeys(this.keybindings, "tui.select.up")}/${configuredKeys(this.keybindings, "tui.select.down")}/jk select · ${configuredKeys(this.keybindings, "tui.select.confirm")} inspect · x kill · ${configuredKeys(this.keybindings, "tui.select.cancel")} close`,
|
|
300
|
+
),
|
|
301
|
+
width,
|
|
302
|
+
),
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
return lines;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
private renderRows(
|
|
309
|
+
terminals: ReadonlyArray<TerminalSnapshot>,
|
|
310
|
+
width: number,
|
|
311
|
+
height: number,
|
|
312
|
+
): string[] {
|
|
313
|
+
const theme = this.theme;
|
|
314
|
+
const out: string[] = [];
|
|
315
|
+
|
|
316
|
+
// Scroll window around selection
|
|
317
|
+
let start = 0;
|
|
318
|
+
if (terminals.length > height) {
|
|
319
|
+
start = Math.min(
|
|
320
|
+
Math.max(0, this.selection.index - Math.floor(height / 2)),
|
|
321
|
+
terminals.length - height,
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
const visible = terminals.slice(start, start + height);
|
|
325
|
+
|
|
326
|
+
for (let i = 0; i < visible.length; i++) {
|
|
327
|
+
const snap = visible[i];
|
|
328
|
+
const index = start + i;
|
|
329
|
+
const isSelected = index === this.selection.index;
|
|
330
|
+
|
|
331
|
+
// Left: marker, status square, title, dim id
|
|
332
|
+
const marker = isSelected ? theme.fg("accent", "❯") : " ";
|
|
333
|
+
const title = isSelected
|
|
334
|
+
? theme.fg("accent", oneLine(snap.title))
|
|
335
|
+
: theme.fg("text", oneLine(snap.title));
|
|
336
|
+
const left = ` ${marker} ${statusGlyph(snap, theme)} ${title} ${theme.fg("dim", snap.id)}`;
|
|
337
|
+
|
|
338
|
+
// Right: pid · elapsed · exit/status
|
|
339
|
+
const dot = theme.fg("dim", " · ");
|
|
340
|
+
const rightParts = [
|
|
341
|
+
theme.fg("muted", `pid ${snap.pid ?? "?"}`),
|
|
342
|
+
theme.fg("muted", formatElapsed(snap)),
|
|
343
|
+
snap.status === "running"
|
|
344
|
+
? statusWord(snap, theme)
|
|
345
|
+
: theme.fg("muted", formatExit(snap)),
|
|
346
|
+
snap.status === "running" && snap.timeoutAt !== undefined
|
|
347
|
+
? theme.fg(
|
|
348
|
+
"warning",
|
|
349
|
+
`${formatDuration((snap.timeoutAt - Date.now()) / 1_000)} left`,
|
|
350
|
+
)
|
|
351
|
+
: "",
|
|
352
|
+
];
|
|
353
|
+
const right = `${rightParts.filter(Boolean).join(dot)} `;
|
|
354
|
+
|
|
355
|
+
const rightWidth = visibleWidth(right);
|
|
356
|
+
const leftMax = Math.max(0, width - rightWidth - 2);
|
|
357
|
+
const leftTruncated = truncateToWidth(left, leftMax);
|
|
358
|
+
const gap = Math.max(2, width - visibleWidth(leftTruncated) - rightWidth);
|
|
359
|
+
out.push(truncateToWidth(leftTruncated + " ".repeat(gap) + right, width));
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if (start > 0) {
|
|
363
|
+
out[0] = truncateToWidth(theme.fg("dim", ` ... ${start} more`), width);
|
|
364
|
+
}
|
|
365
|
+
if (start + height < terminals.length) {
|
|
366
|
+
out[out.length - 1] = truncateToWidth(
|
|
367
|
+
theme.fg("dim", ` ... ${terminals.length - start - height} more`),
|
|
368
|
+
width,
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
return out;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
invalidate(): void {}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// --- Detail view (read-only inspector) --------------------------------------------
|
|
378
|
+
|
|
379
|
+
const OUTPUT_SCROLL_STEP = 6;
|
|
380
|
+
|
|
381
|
+
class TerminalDetailView implements Component {
|
|
382
|
+
private tui: TUI;
|
|
383
|
+
private theme: Theme;
|
|
384
|
+
private keybindings: KeybindingsManager;
|
|
385
|
+
private id: string;
|
|
386
|
+
private view: TerminalReadModel;
|
|
387
|
+
private done: (value: null) => void;
|
|
388
|
+
|
|
389
|
+
/** Active output stream shown in the viewport; `t` toggles. */
|
|
390
|
+
private stream: "stdout" | "stderr" = "stdout";
|
|
391
|
+
/** Scroll offset in lines from the bottom. 0 = pinned to bottom (live tail). */
|
|
392
|
+
private scrollOffset = 0;
|
|
393
|
+
private lineCache = createOutputLineCache();
|
|
394
|
+
private unsubscribe: () => void;
|
|
395
|
+
private renderTimer?: ReturnType<typeof setTimeout>;
|
|
396
|
+
private ticker: ReturnType<typeof setInterval>;
|
|
397
|
+
private closed = false;
|
|
398
|
+
|
|
399
|
+
constructor(
|
|
400
|
+
tui: TUI,
|
|
401
|
+
theme: Theme,
|
|
402
|
+
keybindings: KeybindingsManager,
|
|
403
|
+
id: string,
|
|
404
|
+
view: TerminalReadModel,
|
|
405
|
+
done: (value: null) => void,
|
|
406
|
+
) {
|
|
407
|
+
this.tui = tui;
|
|
408
|
+
this.theme = theme;
|
|
409
|
+
this.keybindings = keybindings;
|
|
410
|
+
this.id = id;
|
|
411
|
+
this.view = view;
|
|
412
|
+
this.done = done;
|
|
413
|
+
this.unsubscribe = view.subscribeTo(id, () => this.scheduleRender());
|
|
414
|
+
// Elapsed time in the header ticks along at 1Hz.
|
|
415
|
+
this.ticker = setInterval(() => this.tui.requestRender(), 1000);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
private snap(): TerminalSnapshot | undefined {
|
|
419
|
+
return this.view.get(this.id);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
private scheduleRender() {
|
|
423
|
+
if (this.renderTimer) return;
|
|
424
|
+
// A chatty process emits a chunk per write. Limit terminal repaints so
|
|
425
|
+
// this view cannot starve input handling.
|
|
426
|
+
this.renderTimer = setTimeout(() => {
|
|
427
|
+
this.renderTimer = undefined;
|
|
428
|
+
if (!this.closed) this.tui.requestRender();
|
|
429
|
+
}, 50);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
private cleanup() {
|
|
433
|
+
if (this.closed) return false;
|
|
434
|
+
this.closed = true;
|
|
435
|
+
this.unsubscribe();
|
|
436
|
+
clearInterval(this.ticker);
|
|
437
|
+
if (this.renderTimer) clearTimeout(this.renderTimer);
|
|
438
|
+
this.renderTimer = undefined;
|
|
439
|
+
return true;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
private close() {
|
|
443
|
+
if (this.cleanup()) this.done(null);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
dispose(): void {
|
|
447
|
+
this.cleanup();
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
handleInput(data: string): void {
|
|
451
|
+
if (
|
|
452
|
+
this.keybindings.matches(data, "app.interrupt") ||
|
|
453
|
+
this.keybindings.matches(data, "tui.select.cancel")
|
|
454
|
+
) {
|
|
455
|
+
this.close();
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
if (data === "t") {
|
|
459
|
+
this.stream = this.stream === "stdout" ? "stderr" : "stdout";
|
|
460
|
+
this.lineCache = createOutputLineCache();
|
|
461
|
+
this.scrollOffset = 0;
|
|
462
|
+
this.tui.requestRender();
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
if (data === "x") {
|
|
466
|
+
const snap = this.snap();
|
|
467
|
+
if (snap?.status === "running") this.view.requestKill(this.id);
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
if (this.keybindings.matches(data, "tui.editor.cursorUp") || data === "k") {
|
|
471
|
+
this.scrollOffset += OUTPUT_SCROLL_STEP;
|
|
472
|
+
this.tui.requestRender();
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
if (
|
|
476
|
+
this.keybindings.matches(data, "tui.editor.cursorDown") ||
|
|
477
|
+
data === "j"
|
|
478
|
+
) {
|
|
479
|
+
this.scrollOffset = Math.max(0, this.scrollOffset - OUTPUT_SCROLL_STEP);
|
|
480
|
+
this.tui.requestRender();
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
if (this.keybindings.matches(data, "tui.editor.pageUp")) {
|
|
484
|
+
this.scrollOffset += this.viewportHeight();
|
|
485
|
+
this.tui.requestRender();
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
if (this.keybindings.matches(data, "tui.editor.pageDown")) {
|
|
489
|
+
this.scrollOffset = Math.max(
|
|
490
|
+
0,
|
|
491
|
+
this.scrollOffset - this.viewportHeight(),
|
|
492
|
+
);
|
|
493
|
+
this.tui.requestRender();
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
if (data === "g") {
|
|
497
|
+
this.scrollOffset = Number.MAX_SAFE_INTEGER; // clamped to top in render
|
|
498
|
+
this.tui.requestRender();
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
if (data === "G") {
|
|
502
|
+
this.scrollOffset = 0;
|
|
503
|
+
this.tui.requestRender();
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
private viewportHeight(): number {
|
|
509
|
+
const rows = this.tui.terminal.rows || 30;
|
|
510
|
+
// The complete view renders viewport + 8 chrome rows (borders, header,
|
|
511
|
+
// command, tab, hints). rows - 9 makes the overlay ~terminal rows - 1.
|
|
512
|
+
return Math.max(6, rows - 9);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
render(width: number): string[] {
|
|
516
|
+
const theme = this.theme;
|
|
517
|
+
const border = theme.fg("borderAccent", "─".repeat(Math.max(1, width)));
|
|
518
|
+
const lines: string[] = [];
|
|
519
|
+
const snap = this.snap();
|
|
520
|
+
|
|
521
|
+
if (!snap) {
|
|
522
|
+
lines.push(border);
|
|
523
|
+
lines.push(theme.fg("dim", `${this.id} is no longer tracked`));
|
|
524
|
+
lines.push(border);
|
|
525
|
+
return lines;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
lines.push(border);
|
|
529
|
+
const header =
|
|
530
|
+
`${statusGlyph(snap, theme)} ` +
|
|
531
|
+
theme.fg("accent", theme.bold(`${snap.id} · ${oneLine(snap.title)}`)) +
|
|
532
|
+
theme.fg(
|
|
533
|
+
"muted",
|
|
534
|
+
` · ${snap.status} · ${formatElapsed(snap)} · pid ${snap.pid ?? "?"}`,
|
|
535
|
+
) +
|
|
536
|
+
(snap.status !== "running"
|
|
537
|
+
? theme.fg("muted", ` · ${formatExit(snap)}`)
|
|
538
|
+
: "") +
|
|
539
|
+
(snap.status === "running" && snap.timeoutAt !== undefined
|
|
540
|
+
? theme.fg(
|
|
541
|
+
"warning",
|
|
542
|
+
` · ${formatDuration((snap.timeoutAt - Date.now()) / 1_000)} remaining`,
|
|
543
|
+
)
|
|
544
|
+
: "") +
|
|
545
|
+
theme.fg("dim", ` · ${snap.cwd}`);
|
|
546
|
+
lines.push(truncateToWidth(header, width));
|
|
547
|
+
lines.push(
|
|
548
|
+
truncateToWidth(
|
|
549
|
+
theme.fg("dim", "$ ") + theme.fg("text", oneLine(snap.command)),
|
|
550
|
+
width,
|
|
551
|
+
),
|
|
552
|
+
);
|
|
553
|
+
lines.push(border);
|
|
554
|
+
|
|
555
|
+
// Stream tab line: which stream is active, both sizes.
|
|
556
|
+
const active = this.stream;
|
|
557
|
+
const viewData = active === "stdout" ? snap.stdout : snap.stderr;
|
|
558
|
+
const tab = (name: "stdout" | "stderr", size: number) =>
|
|
559
|
+
name === active
|
|
560
|
+
? theme.fg("accent", theme.bold(`${name} (${formatSize(size)})`))
|
|
561
|
+
: theme.fg("dim", `${name} (${formatSize(size)})`);
|
|
562
|
+
lines.push(
|
|
563
|
+
truncateToWidth(
|
|
564
|
+
` ${tab("stdout", snap.stdout.totalBytes)}${theme.fg("dim", " | ")}${tab("stderr", snap.stderr.totalBytes)}${theme.fg("dim", " — t to switch")}`,
|
|
565
|
+
width,
|
|
566
|
+
),
|
|
567
|
+
);
|
|
568
|
+
|
|
569
|
+
// Fixed-height output viewport. Notes and scroll status consume rows
|
|
570
|
+
// inside the viewport so streaming/scrolling never changes overlay height.
|
|
571
|
+
const buffer = viewData;
|
|
572
|
+
const version =
|
|
573
|
+
// The cached view text identity changes with the buffer; totalBytes is a
|
|
574
|
+
// monotonically increasing proxy for a version counter.
|
|
575
|
+
buffer.totalBytes;
|
|
576
|
+
const output = this.lineCache.get(buffer.text, version, width - 2);
|
|
577
|
+
const viewport = this.viewportHeight();
|
|
578
|
+
|
|
579
|
+
const noteRows: string[] = [];
|
|
580
|
+
if (snap.errorText) {
|
|
581
|
+
noteRows.push(
|
|
582
|
+
truncateToWidth(
|
|
583
|
+
theme.fg("error", `error: ${oneLine(snap.errorText)}`),
|
|
584
|
+
width,
|
|
585
|
+
),
|
|
586
|
+
);
|
|
587
|
+
}
|
|
588
|
+
if (buffer.truncatedBytes > 0) {
|
|
589
|
+
noteRows.push(
|
|
590
|
+
truncateToWidth(
|
|
591
|
+
theme.fg(
|
|
592
|
+
"dim",
|
|
593
|
+
`first ${formatSize(buffer.truncatedBytes)} dropped from view — full log: ${buffer.spillPath ?? "(unavailable)"}`,
|
|
594
|
+
),
|
|
595
|
+
width,
|
|
596
|
+
),
|
|
597
|
+
);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
const body: string[] = [...noteRows];
|
|
601
|
+
const scrollRows = this.scrollOffset > 0 ? 1 : 0;
|
|
602
|
+
const capacity = Math.max(1, viewport - body.length - scrollRows);
|
|
603
|
+
const maxOffset = Math.max(0, output.length - capacity);
|
|
604
|
+
if (this.scrollOffset > maxOffset) this.scrollOffset = maxOffset;
|
|
605
|
+
|
|
606
|
+
const end = output.length - this.scrollOffset;
|
|
607
|
+
const visible = output.slice(Math.max(0, end - capacity), end);
|
|
608
|
+
if (visible.length === 0) {
|
|
609
|
+
body.push(theme.fg("dim", `(no ${active} yet)`));
|
|
610
|
+
} else {
|
|
611
|
+
for (const line of visible) {
|
|
612
|
+
body.push(truncateToWidth(` ${line}`, width));
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
if (this.scrollOffset > 0) {
|
|
617
|
+
body.push(
|
|
618
|
+
truncateToWidth(
|
|
619
|
+
theme.fg("dim", `... ${this.scrollOffset} lines below · ↓/pgdn`),
|
|
620
|
+
width,
|
|
621
|
+
),
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
while (body.length < viewport) body.push("");
|
|
625
|
+
lines.push(...body.slice(0, viewport));
|
|
626
|
+
|
|
627
|
+
lines.push(border);
|
|
628
|
+
lines.push(
|
|
629
|
+
truncateToWidth(
|
|
630
|
+
theme.fg(
|
|
631
|
+
"dim",
|
|
632
|
+
`${configuredKeys(this.keybindings, "tui.select.cancel")} back · t stdout/stderr · x kill · ${configuredKeys(this.keybindings, "tui.editor.cursorUp")}/${configuredKeys(this.keybindings, "tui.editor.cursorDown")}/jk scroll · ${configuredKeys(this.keybindings, "tui.editor.pageUp")}/${configuredKeys(this.keybindings, "tui.editor.pageDown")} page · g/G top/bottom`,
|
|
633
|
+
),
|
|
634
|
+
width,
|
|
635
|
+
),
|
|
636
|
+
);
|
|
637
|
+
lines.push(border);
|
|
638
|
+
return lines;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
invalidate(): void {}
|
|
642
|
+
}
|