@jaggerxtrm/pi-extensions 0.7.17 → 0.7.21
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 +7 -0
- package/extensions/README.md +12 -0
- package/extensions/sp-terminal-overlay/index.ts +495 -0
- package/extensions/sp-terminal-overlay/package.json +15 -0
- package/extensions/xtrm-ui/format.ts +7 -2
- package/extensions/xtrm-ui/index.ts +422 -11
- package/package.json +1 -1
- package/src/extensions/sp-terminal-overlay.ts +3 -0
- package/src/registry.ts +2 -0
package/README.md
CHANGED
|
@@ -41,3 +41,10 @@ Pi discovers this package through:
|
|
|
41
41
|
- `pi.extensions: ["./src/index.ts"]`
|
|
42
42
|
|
|
43
43
|
After install, keep `.pi/settings.json` package wiring pointed at `npm:@jaggerxtrm/pi-extensions`.
|
|
44
|
+
|
|
45
|
+
## Managed extensions
|
|
46
|
+
|
|
47
|
+
Notable bundled extensions include:
|
|
48
|
+
|
|
49
|
+
- `xtrm-ui` — XTRM Pi chrome, native tool summaries, selectable external tool chrome (`/xtrm-ui chrome background|box`).
|
|
50
|
+
- `sp-terminal-overlay` — `/sp-feed`, `/sp-ps`, and `/xtrm-terminal` streaming overlays for specialist monitoring.
|
package/extensions/README.md
CHANGED
|
@@ -3,3 +3,15 @@
|
|
|
3
3
|
This directory is the canonical source for managed Pi extension entrypoints.
|
|
4
4
|
|
|
5
5
|
Runtime delivery is package-based via `npm:@jaggerxtrm/pi-extensions`.
|
|
6
|
+
|
|
7
|
+
## sp-terminal-overlay
|
|
8
|
+
|
|
9
|
+
Streaming terminal-style overlay for specialist/process monitoring commands.
|
|
10
|
+
|
|
11
|
+
Commands:
|
|
12
|
+
|
|
13
|
+
- `/sp-feed [args]` — opens `sp feed -f [args]` in an overlay.
|
|
14
|
+
- `/sp-ps [args]` / `/xtrm-ps [args]` — opens `sp ps [args]` (defaults to `sp ps --follow`) in an overlay.
|
|
15
|
+
- `/xtrm-terminal <command>` — opens an arbitrary shell command in an overlay.
|
|
16
|
+
|
|
17
|
+
Keys: `Esc`/`q` close, `r` restart, arrows/page keys scroll.
|
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
|
|
2
|
+
import type { ExtensionAPI, ExtensionCommandContext, Theme } from "@mariozechner/pi-coding-agent";
|
|
3
|
+
import { matchesKey, truncateToWidth, visibleWidth } from "@mariozechner/pi-tui";
|
|
4
|
+
|
|
5
|
+
const MAX_BUFFER_LINES = 2000;
|
|
6
|
+
const DEFAULT_VISIBLE_LINES = 24;
|
|
7
|
+
const RENDER_THROTTLE_MS = 100;
|
|
8
|
+
const ANSI_SGR_PATTERN = /^\x1b\[[0-9;]*m$/u;
|
|
9
|
+
const DISALLOWED_SGR_CODES = new Set([5, 6, 8]);
|
|
10
|
+
|
|
11
|
+
function padVisible(text: string, width: number): string {
|
|
12
|
+
return text + " ".repeat(Math.max(0, width - visibleWidth(text)));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function resetAnsi(text: string): string {
|
|
16
|
+
return text.includes("\x1b") ? `${text}\x1b[0m` : text;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function shellQuote(value: string): string {
|
|
20
|
+
return `'${value.replace(/'/g, `'"'"'`)}'`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function resolveSpFeedCommand(args: string): string {
|
|
24
|
+
const trimmed = args.trim();
|
|
25
|
+
return trimmed ? `sp feed -f ${trimmed}` : "sp feed -f";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function resolveSpPsCommand(args: string): string {
|
|
29
|
+
const trimmed = args.trim();
|
|
30
|
+
return trimmed ? `sp ps ${trimmed}` : "sp ps --follow";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function openTerminalOverlay(ctx: ExtensionCommandContext, title: string, command: string): Promise<void> {
|
|
34
|
+
return ctx.ui.custom<void>(
|
|
35
|
+
(tui, theme, _keybindings, done) => {
|
|
36
|
+
const terminal = new StreamingTerminalOverlay({
|
|
37
|
+
title,
|
|
38
|
+
command,
|
|
39
|
+
cwd: process.cwd(),
|
|
40
|
+
theme,
|
|
41
|
+
requestRender: () => {
|
|
42
|
+
tui.requestRender();
|
|
43
|
+
},
|
|
44
|
+
close: () => {
|
|
45
|
+
terminal.dispose();
|
|
46
|
+
done(undefined);
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
return terminal;
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
overlay: true,
|
|
53
|
+
overlayOptions: {
|
|
54
|
+
anchor: "center",
|
|
55
|
+
width: "80%",
|
|
56
|
+
minWidth: 72,
|
|
57
|
+
maxHeight: "80%",
|
|
58
|
+
margin: 1,
|
|
59
|
+
},
|
|
60
|
+
} as Parameters<ExtensionCommandContext["ui"]["custom"]>[1],
|
|
61
|
+
).then(() => undefined);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type StreamingTerminalOverlayOptions = {
|
|
65
|
+
title: string;
|
|
66
|
+
command: string;
|
|
67
|
+
cwd: string;
|
|
68
|
+
theme: Theme;
|
|
69
|
+
requestRender: () => void;
|
|
70
|
+
close: () => void;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
class StreamingTerminalOverlay {
|
|
74
|
+
private child: ChildProcessWithoutNullStreams | undefined;
|
|
75
|
+
private lines: string[] = [];
|
|
76
|
+
private currentLine = "";
|
|
77
|
+
private screenLines: string[] = [];
|
|
78
|
+
private cursorRow = 0;
|
|
79
|
+
private cursorCol = 0;
|
|
80
|
+
private terminalMode = false;
|
|
81
|
+
private scrollOffset = 0;
|
|
82
|
+
private status = "starting";
|
|
83
|
+
private closed = false;
|
|
84
|
+
private renderTimer: ReturnType<typeof setTimeout> | undefined;
|
|
85
|
+
private lastRenderAt = 0;
|
|
86
|
+
|
|
87
|
+
constructor(private readonly options: StreamingTerminalOverlayOptions) {
|
|
88
|
+
this.start();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
handleInput(data: string): void {
|
|
92
|
+
if (matchesKey(data, "escape") || matchesKey(data, "q") || matchesKey(data, "ctrl+c")) {
|
|
93
|
+
this.options.close();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (matchesKey(data, "r")) {
|
|
97
|
+
this.restart();
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (matchesKey(data, "up")) {
|
|
101
|
+
this.scrollOffset = Math.min(this.scrollOffset + 1, Math.max(0, this.renderSourceLines().length - 1));
|
|
102
|
+
this.options.requestRender();
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (matchesKey(data, "down")) {
|
|
106
|
+
this.scrollOffset = Math.max(0, this.scrollOffset - 1);
|
|
107
|
+
this.options.requestRender();
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (matchesKey(data, "pageup")) {
|
|
111
|
+
this.scrollOffset = Math.min(this.scrollOffset + DEFAULT_VISIBLE_LINES, Math.max(0, this.renderSourceLines().length - 1));
|
|
112
|
+
this.options.requestRender();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (matchesKey(data, "pagedown")) {
|
|
116
|
+
this.scrollOffset = Math.max(0, this.scrollOffset - DEFAULT_VISIBLE_LINES);
|
|
117
|
+
this.options.requestRender();
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (matchesKey(data, "home")) {
|
|
121
|
+
this.scrollOffset = Math.max(0, this.renderSourceLines().length - 1);
|
|
122
|
+
this.options.requestRender();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (matchesKey(data, "end")) {
|
|
126
|
+
this.scrollOffset = 0;
|
|
127
|
+
this.options.requestRender();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
render(width: number): string[] {
|
|
132
|
+
const theme = this.options.theme;
|
|
133
|
+
const overlayWidth = Math.max(40, width);
|
|
134
|
+
const innerWidth = overlayWidth - 2;
|
|
135
|
+
const contentWidth = Math.max(1, innerWidth - 2);
|
|
136
|
+
const allLines = this.renderSourceLines();
|
|
137
|
+
const visibleCount = DEFAULT_VISIBLE_LINES;
|
|
138
|
+
const end = Math.max(0, allLines.length - this.scrollOffset);
|
|
139
|
+
const start = Math.max(0, end - visibleCount);
|
|
140
|
+
const visible = allLines.slice(start, end);
|
|
141
|
+
const hiddenAbove = start;
|
|
142
|
+
const hiddenBelow = Math.max(0, allLines.length - end);
|
|
143
|
+
|
|
144
|
+
const border = (text: string) => theme.fg("border", text);
|
|
145
|
+
const title = ` ${theme.fg("accent", theme.bold(this.options.title))} ${theme.fg("dim", this.status)} `;
|
|
146
|
+
const top = border("╭") + truncateToWidth(title, Math.max(0, innerWidth), "") + border("─".repeat(Math.max(0, innerWidth - visibleWidth(title)))) + border("╮");
|
|
147
|
+
const row = (content: string) => {
|
|
148
|
+
const truncated = resetAnsi(truncateToWidth(content, contentWidth));
|
|
149
|
+
return `${border("│")} ${padVisible(truncated, contentWidth)} ${border("│")}`;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const output = [top];
|
|
153
|
+
output.push(row(theme.fg("dim", `$ ${this.options.command}`)));
|
|
154
|
+
output.push(row(theme.fg("dim", "Esc/q close • r restart • ↑↓ scroll • PgUp/PgDn page")));
|
|
155
|
+
output.push(row(""));
|
|
156
|
+
const bodyRows: string[] = [];
|
|
157
|
+
if (hiddenAbove > 0) bodyRows.push(theme.fg("dim", `… ${hiddenAbove} lines above`));
|
|
158
|
+
bodyRows.push(...visible);
|
|
159
|
+
if (visible.length === 0) bodyRows.push(theme.fg("dim", "waiting for output…"));
|
|
160
|
+
if (hiddenBelow > 0) bodyRows.push(theme.fg("dim", `… ${hiddenBelow} lines below`));
|
|
161
|
+
|
|
162
|
+
for (let index = 0; index < visibleCount; index++) {
|
|
163
|
+
output.push(row(bodyRows[index] ?? ""));
|
|
164
|
+
}
|
|
165
|
+
output.push(border("╰" + "─".repeat(innerWidth) + "╯"));
|
|
166
|
+
return output;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
invalidate(): void {}
|
|
170
|
+
|
|
171
|
+
dispose(): void {
|
|
172
|
+
this.closed = true;
|
|
173
|
+
if (this.renderTimer) clearTimeout(this.renderTimer);
|
|
174
|
+
this.renderTimer = undefined;
|
|
175
|
+
this.stop();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private start(): void {
|
|
179
|
+
this.stop();
|
|
180
|
+
this.status = "running";
|
|
181
|
+
this.lines = [];
|
|
182
|
+
this.currentLine = "";
|
|
183
|
+
this.screenLines = [];
|
|
184
|
+
this.cursorRow = 0;
|
|
185
|
+
this.cursorCol = 0;
|
|
186
|
+
this.terminalMode = false;
|
|
187
|
+
this.scrollOffset = 0;
|
|
188
|
+
|
|
189
|
+
const shell = process.env.SHELL || "/bin/sh";
|
|
190
|
+
this.child = spawn(shell, ["-lc", this.options.command], {
|
|
191
|
+
cwd: this.options.cwd,
|
|
192
|
+
env: {
|
|
193
|
+
...process.env,
|
|
194
|
+
FORCE_COLOR: process.env.FORCE_COLOR ?? "1",
|
|
195
|
+
TERM: process.env.TERM ?? "xterm-256color",
|
|
196
|
+
COLUMNS: process.env.COLUMNS ?? "120",
|
|
197
|
+
LINES: process.env.LINES ?? "40",
|
|
198
|
+
},
|
|
199
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
this.child.stdout.on("data", (chunk) => this.append(String(chunk)));
|
|
203
|
+
this.child.stderr.on("data", (chunk) => this.append(String(chunk)));
|
|
204
|
+
this.child.on("error", (error) => {
|
|
205
|
+
this.status = "error";
|
|
206
|
+
this.append(`\n[error] ${error.message}\n`);
|
|
207
|
+
});
|
|
208
|
+
this.child.on("close", (code, signal) => {
|
|
209
|
+
this.flushCurrentLine();
|
|
210
|
+
this.status = signal ? `stopped (${signal})` : `exited ${code ?? "unknown"}`;
|
|
211
|
+
this.requestRenderSoon(true);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
this.requestRenderSoon(true);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private restart(): void {
|
|
218
|
+
this.start();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
private stop(): void {
|
|
222
|
+
const child = this.child;
|
|
223
|
+
this.child = undefined;
|
|
224
|
+
if (child && !child.killed) {
|
|
225
|
+
child.kill("SIGTERM");
|
|
226
|
+
setTimeout(() => {
|
|
227
|
+
if (!child.killed) child.kill("SIGKILL");
|
|
228
|
+
}, 750).unref?.();
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private requestRenderSoon(immediate = false): void {
|
|
233
|
+
if (this.closed) return;
|
|
234
|
+
if (immediate) {
|
|
235
|
+
if (this.renderTimer) clearTimeout(this.renderTimer);
|
|
236
|
+
this.renderTimer = undefined;
|
|
237
|
+
this.lastRenderAt = Date.now();
|
|
238
|
+
this.options.requestRender();
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const now = Date.now();
|
|
243
|
+
const elapsed = now - this.lastRenderAt;
|
|
244
|
+
if (elapsed >= RENDER_THROTTLE_MS) {
|
|
245
|
+
this.lastRenderAt = now;
|
|
246
|
+
this.options.requestRender();
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (this.renderTimer) return;
|
|
251
|
+
this.renderTimer = setTimeout(() => {
|
|
252
|
+
this.renderTimer = undefined;
|
|
253
|
+
this.lastRenderAt = Date.now();
|
|
254
|
+
this.options.requestRender();
|
|
255
|
+
}, RENDER_THROTTLE_MS - elapsed);
|
|
256
|
+
this.renderTimer.unref?.();
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private renderSourceLines(): string[] {
|
|
260
|
+
if (this.terminalMode) {
|
|
261
|
+
return this.screenLines.map((line) => line.replace(/\s+$/u, ""));
|
|
262
|
+
}
|
|
263
|
+
return this.currentLine ? [...this.lines, this.currentLine] : this.lines;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private append(text: string): void {
|
|
267
|
+
if (this.closed) return;
|
|
268
|
+
for (let index = 0; index < text.length; index++) {
|
|
269
|
+
const char = text[index]!;
|
|
270
|
+
if (char === "\x1b") {
|
|
271
|
+
const sgrMatch = text.slice(index).match(/^\x1b\[[0-9;]*m/u);
|
|
272
|
+
if (sgrMatch?.[0]) {
|
|
273
|
+
this.appendSafeSgr(sgrMatch[0]);
|
|
274
|
+
index += sgrMatch[0].length - 1;
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
const nextIndex = this.consumeEscape(text, index);
|
|
278
|
+
if (nextIndex !== index) {
|
|
279
|
+
index = nextIndex;
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
this.appendChar(char);
|
|
284
|
+
}
|
|
285
|
+
this.trimBuffer();
|
|
286
|
+
this.requestRenderSoon();
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private appendSafeSgr(sequence: string): void {
|
|
290
|
+
if (!ANSI_SGR_PATTERN.test(sequence)) return;
|
|
291
|
+
const params = sequence.slice(2, -1).split(";").filter(Boolean).map((part) => Number.parseInt(part, 10));
|
|
292
|
+
if (params.some((param) => Number.isNaN(param) || DISALLOWED_SGR_CODES.has(param))) return;
|
|
293
|
+
|
|
294
|
+
// Cursor-addressed dashboards need cell-aware mutation. Keeping raw SGR inside
|
|
295
|
+
// screenLines makes cursorCol slicing unsafe, so preserve colors only for
|
|
296
|
+
// append-only feed output.
|
|
297
|
+
if (this.terminalMode) return;
|
|
298
|
+
this.currentLine += sequence;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
private appendChar(char: string): void {
|
|
302
|
+
if (char === "\r") {
|
|
303
|
+
if (this.terminalMode) this.cursorCol = 0;
|
|
304
|
+
else this.currentLine = "";
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
if (char === "\n") {
|
|
308
|
+
if (this.terminalMode) {
|
|
309
|
+
this.cursorRow++;
|
|
310
|
+
this.cursorCol = 0;
|
|
311
|
+
this.ensureScreenLine(this.cursorRow);
|
|
312
|
+
} else {
|
|
313
|
+
this.flushCurrentLine();
|
|
314
|
+
}
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
if (char === "\b" || char === "\x7f") {
|
|
318
|
+
if (this.terminalMode) this.cursorCol = Math.max(0, this.cursorCol - 1);
|
|
319
|
+
else this.currentLine = this.currentLine.slice(0, -1);
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (char < " " && char !== "\t") return;
|
|
323
|
+
|
|
324
|
+
if (this.terminalMode) {
|
|
325
|
+
this.writeScreenChar(char === "\t" ? " " : char);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
this.currentLine += char;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
private consumeEscape(text: string, start: number): number {
|
|
332
|
+
const introducer = text[start + 1];
|
|
333
|
+
if (introducer === "[") {
|
|
334
|
+
let end = start + 2;
|
|
335
|
+
while (end < text.length && !/[A-Za-z~]/.test(text[end]!)) end++;
|
|
336
|
+
if (end >= text.length) return start;
|
|
337
|
+
this.handleCsi(text.slice(start + 2, end), text[end]!);
|
|
338
|
+
return end;
|
|
339
|
+
}
|
|
340
|
+
if (introducer === "]") {
|
|
341
|
+
let end = start + 2;
|
|
342
|
+
while (end < text.length) {
|
|
343
|
+
if (text[end] === "\x07") return end;
|
|
344
|
+
if (text[end] === "\x1b" && text[end + 1] === "\\") return end + 1;
|
|
345
|
+
end++;
|
|
346
|
+
}
|
|
347
|
+
return start;
|
|
348
|
+
}
|
|
349
|
+
if (introducer) return start + 1;
|
|
350
|
+
return start;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
private handleCsi(params: string, final: string): void {
|
|
354
|
+
const numbers = params
|
|
355
|
+
.replace(/[?>!]/g, "")
|
|
356
|
+
.split(";")
|
|
357
|
+
.map((part) => Number.parseInt(part || "0", 10));
|
|
358
|
+
const first = numbers[0] ?? 0;
|
|
359
|
+
|
|
360
|
+
if (final === "m") return;
|
|
361
|
+
if (final === "H" || final === "f") {
|
|
362
|
+
this.enterTerminalMode();
|
|
363
|
+
this.cursorRow = Math.max(0, (numbers[0] || 1) - 1);
|
|
364
|
+
this.cursorCol = Math.max(0, (numbers[1] || 1) - 1);
|
|
365
|
+
this.ensureScreenLine(this.cursorRow);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
if (final === "J") {
|
|
369
|
+
this.enterTerminalMode();
|
|
370
|
+
if (first === 2 || first === 3) {
|
|
371
|
+
this.screenLines = [];
|
|
372
|
+
this.cursorRow = 0;
|
|
373
|
+
this.cursorCol = 0;
|
|
374
|
+
this.ensureScreenLine(0);
|
|
375
|
+
} else if (first === 0) {
|
|
376
|
+
this.screenLines = this.screenLines.slice(0, this.cursorRow + 1);
|
|
377
|
+
this.clearScreenLineFromCursor();
|
|
378
|
+
}
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
if (final === "K") {
|
|
382
|
+
this.enterTerminalMode();
|
|
383
|
+
if (first === 2) this.screenLines[this.cursorRow] = "";
|
|
384
|
+
else this.clearScreenLineFromCursor();
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
if (final === "A") {
|
|
388
|
+
this.enterTerminalMode();
|
|
389
|
+
this.cursorRow = Math.max(0, this.cursorRow - Math.max(1, first));
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
if (final === "B") {
|
|
393
|
+
this.enterTerminalMode();
|
|
394
|
+
this.cursorRow += Math.max(1, first);
|
|
395
|
+
this.ensureScreenLine(this.cursorRow);
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
if (final === "C") {
|
|
399
|
+
this.enterTerminalMode();
|
|
400
|
+
this.cursorCol += Math.max(1, first);
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
if (final === "D") {
|
|
404
|
+
this.enterTerminalMode();
|
|
405
|
+
this.cursorCol = Math.max(0, this.cursorCol - Math.max(1, first));
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
private enterTerminalMode(): void {
|
|
410
|
+
if (this.terminalMode) return;
|
|
411
|
+
this.terminalMode = true;
|
|
412
|
+
this.screenLines = this.currentLine ? [...this.lines, this.currentLine] : [...this.lines];
|
|
413
|
+
if (this.screenLines.length === 0) this.screenLines.push("");
|
|
414
|
+
this.cursorRow = Math.max(0, this.screenLines.length - 1);
|
|
415
|
+
this.cursorCol = visibleWidth(this.screenLines[this.cursorRow] ?? "");
|
|
416
|
+
this.currentLine = "";
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
private ensureScreenLine(row: number): void {
|
|
420
|
+
while (this.screenLines.length <= row) this.screenLines.push("");
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
private writeScreenChar(char: string): void {
|
|
424
|
+
this.ensureScreenLine(this.cursorRow);
|
|
425
|
+
const line = this.screenLines[this.cursorRow] ?? "";
|
|
426
|
+
const padded = line.length < this.cursorCol ? line + " ".repeat(this.cursorCol - line.length) : line;
|
|
427
|
+
this.screenLines[this.cursorRow] = padded.slice(0, this.cursorCol) + char + padded.slice(this.cursorCol + 1);
|
|
428
|
+
this.cursorCol++;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
private clearScreenLineFromCursor(): void {
|
|
432
|
+
this.ensureScreenLine(this.cursorRow);
|
|
433
|
+
this.screenLines[this.cursorRow] = (this.screenLines[this.cursorRow] ?? "").slice(0, this.cursorCol);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
private flushCurrentLine(): void {
|
|
437
|
+
if (this.terminalMode) return;
|
|
438
|
+
this.lines.push(this.currentLine);
|
|
439
|
+
this.currentLine = "";
|
|
440
|
+
this.trimBuffer();
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
private trimBuffer(): void {
|
|
444
|
+
if (this.lines.length > MAX_BUFFER_LINES) this.lines.splice(0, this.lines.length - MAX_BUFFER_LINES);
|
|
445
|
+
if (this.screenLines.length > MAX_BUFFER_LINES) this.screenLines.splice(0, this.screenLines.length - MAX_BUFFER_LINES);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
export default function spTerminalOverlayExtension(pi: ExtensionAPI): void {
|
|
450
|
+
pi.registerCommand("sp-feed", {
|
|
451
|
+
description: "Open a streaming overlay for `sp feed -f`",
|
|
452
|
+
handler: async (args, ctx) => {
|
|
453
|
+
await openTerminalOverlay(ctx, "sp feed", resolveSpFeedCommand(args));
|
|
454
|
+
},
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
pi.registerCommand("sp-ps", {
|
|
458
|
+
description: "Open a streaming overlay for `sp ps --follow`",
|
|
459
|
+
handler: async (args, ctx) => {
|
|
460
|
+
await openTerminalOverlay(ctx, "sp ps", resolveSpPsCommand(args));
|
|
461
|
+
},
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
pi.registerCommand("xtrm-ps", {
|
|
465
|
+
description: "Alias for /sp-ps",
|
|
466
|
+
handler: async (args, ctx) => {
|
|
467
|
+
await openTerminalOverlay(ctx, "sp ps", resolveSpPsCommand(args));
|
|
468
|
+
},
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
pi.registerCommand("xtrm-terminal", {
|
|
472
|
+
description: "Open a streaming terminal overlay for an arbitrary shell command",
|
|
473
|
+
handler: async (args, ctx) => {
|
|
474
|
+
const command = args.trim();
|
|
475
|
+
if (!command) {
|
|
476
|
+
ctx.ui.notify("Usage: /xtrm-terminal <command>", "warning");
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
await openTerminalOverlay(ctx, command, command);
|
|
480
|
+
},
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
pi.registerCommand("xtrm-terminal-file", {
|
|
484
|
+
description: "Open a streaming overlay for a command with one shell-quoted file/path argument",
|
|
485
|
+
handler: async (args, ctx) => {
|
|
486
|
+
const [commandName, ...rest] = args.trim().split(/\s+/u);
|
|
487
|
+
const fileArg = rest.join(" ");
|
|
488
|
+
if (!commandName || !fileArg) {
|
|
489
|
+
ctx.ui.notify("Usage: /xtrm-terminal-file <command> <path>", "warning");
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
await openTerminalOverlay(ctx, commandName, `${commandName} ${shellQuote(fileArg)}`);
|
|
493
|
+
},
|
|
494
|
+
});
|
|
495
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xtrm/pi-sp-terminal-overlay",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "XTRM Pi overlay for streaming sp feed and terminal command output",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./index.ts"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"pi": {
|
|
11
|
+
"extensions": [
|
|
12
|
+
"./index.ts"
|
|
13
|
+
]
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -258,6 +258,9 @@ export function formatLineLabel(count: number, noun: string): string {
|
|
|
258
258
|
return `${count} ${noun}${count === 1 ? "" : "s"}`;
|
|
259
259
|
}
|
|
260
260
|
|
|
261
|
+
const TOOL_SUMMARY_SUBJECT_MAX = 34;
|
|
262
|
+
const TOOL_SUMMARY_META_MAX = 34;
|
|
263
|
+
|
|
261
264
|
export function renderToolSummary(
|
|
262
265
|
theme: { fg(color: string, text: string): string; bold(text: string): string },
|
|
263
266
|
status: "pending" | "success" | "error" | "muted",
|
|
@@ -270,9 +273,11 @@ export function renderToolSummary(
|
|
|
270
273
|
: status === "error" ? "error"
|
|
271
274
|
: status === "success" ? "success"
|
|
272
275
|
: "muted";
|
|
276
|
+
const compactSubject = subject ? shortenCommand(subject, TOOL_SUMMARY_SUBJECT_MAX) : undefined;
|
|
277
|
+
const compactMeta = meta ? shortenCommand(meta, TOOL_SUMMARY_META_MAX) : undefined;
|
|
273
278
|
let text = `${theme.fg(color, "•")} ${theme.fg("toolTitle", theme.bold(label))}`;
|
|
274
|
-
if (
|
|
275
|
-
if (
|
|
279
|
+
if (compactSubject) text += ` ${theme.fg("accent", compactSubject)}`;
|
|
280
|
+
if (compactMeta) text += theme.fg("muted", ` · ${compactMeta}`);
|
|
276
281
|
return text;
|
|
277
282
|
}
|
|
278
283
|
|
|
@@ -32,7 +32,7 @@ import {
|
|
|
32
32
|
createWriteTool,
|
|
33
33
|
} from "@mariozechner/pi-coding-agent";
|
|
34
34
|
import { Box, Text, truncateToWidth, visibleWidth } from "@mariozechner/pi-tui";
|
|
35
|
-
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
35
|
+
import { existsSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
|
|
36
36
|
import { basename, dirname, join } from "node:path";
|
|
37
37
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
38
38
|
import {
|
|
@@ -57,6 +57,7 @@ import {
|
|
|
57
57
|
|
|
58
58
|
export type XtrmThemeName = "pidex-dark" | "pidex-light" | "pidex-dark-flattools" | "pidex-light-flattools";
|
|
59
59
|
export type XtrmDensity = "compact" | "comfortable";
|
|
60
|
+
export type XtrmExternalToolChrome = "background" | "box";
|
|
60
61
|
|
|
61
62
|
export interface XtrmUiPrefs {
|
|
62
63
|
themeName: XtrmThemeName;
|
|
@@ -67,6 +68,7 @@ export interface XtrmUiPrefs {
|
|
|
67
68
|
forceTheme: boolean; // When false, skip setTheme (allow external theme override)
|
|
68
69
|
toolRowBg: boolean; // Subtle background behind tool text rows (no padding)
|
|
69
70
|
compactExternalToolResults: boolean; // Compact extension tool results (disables full expand output)
|
|
71
|
+
externalToolChrome: XtrmExternalToolChrome; // Visual treatment for non-native tool rows
|
|
70
72
|
hideThinkingPlaceholder: boolean; // When false, hidden thinking blocks render no placeholder text
|
|
71
73
|
}
|
|
72
74
|
|
|
@@ -85,9 +87,17 @@ export const DEFAULT_PREFS: XtrmUiPrefs = {
|
|
|
85
87
|
forceTheme: true,
|
|
86
88
|
toolRowBg: false,
|
|
87
89
|
compactExternalToolResults: true,
|
|
90
|
+
externalToolChrome: "background",
|
|
88
91
|
hideThinkingPlaceholder: false,
|
|
89
92
|
};
|
|
90
93
|
|
|
94
|
+
let activeExternalToolChrome: XtrmExternalToolChrome = DEFAULT_PREFS.externalToolChrome;
|
|
95
|
+
|
|
96
|
+
function setActiveExternalToolChrome(chrome: XtrmExternalToolChrome): void {
|
|
97
|
+
activeExternalToolChrome = chrome;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
91
101
|
// ============================================================================
|
|
92
102
|
// Preferences
|
|
93
103
|
// ============================================================================
|
|
@@ -111,6 +121,7 @@ function normalizePrefs(input: unknown): XtrmUiPrefs {
|
|
|
111
121
|
toolRowBg: source.toolRowBg ?? DEFAULT_PREFS.toolRowBg,
|
|
112
122
|
compactExternalToolResults:
|
|
113
123
|
source.compactExternalToolResults ?? DEFAULT_PREFS.compactExternalToolResults,
|
|
124
|
+
externalToolChrome: source.externalToolChrome === "box" ? "box" : "background",
|
|
114
125
|
hideThinkingPlaceholder: source.hideThinkingPlaceholder ?? DEFAULT_PREFS.hideThinkingPlaceholder,
|
|
115
126
|
};
|
|
116
127
|
}
|
|
@@ -153,8 +164,39 @@ type PatchableAssistantMessage = {
|
|
|
153
164
|
|
|
154
165
|
const PATCHED_ASSISTANT_MESSAGE = "__xtrmUiSilentHiddenThinking";
|
|
155
166
|
|
|
167
|
+
function maybeFileUrlToPath(value: string): string {
|
|
168
|
+
return value.startsWith("file:") ? fileURLToPath(value) : value;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function resolvePiCodingAgentEntryPath(): string {
|
|
172
|
+
const candidates: string[] = [];
|
|
173
|
+
|
|
174
|
+
const argvPath = process.argv[1];
|
|
175
|
+
if (argvPath && existsSync(argvPath)) {
|
|
176
|
+
const realArgvPath = realpathSync(argvPath);
|
|
177
|
+
if (realArgvPath.endsWith("/dist/cli.js")) {
|
|
178
|
+
candidates.push(join(dirname(realArgvPath), "index.js"));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
candidates.push(
|
|
183
|
+
join(dirname(process.execPath), "..", "lib", "node_modules", "@earendil-works", "pi-coding-agent", "dist", "index.js"),
|
|
184
|
+
join(dirname(process.execPath), "..", "lib", "node_modules", "@mariozechner", "pi-coding-agent", "dist", "index.js"),
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
for (const packageName of ["@earendil-works/pi-coding-agent", "@mariozechner/pi-coding-agent"]) {
|
|
188
|
+
try {
|
|
189
|
+
candidates.push(maybeFileUrlToPath(import.meta.resolve(packageName)));
|
|
190
|
+
} catch {}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const entryPath = candidates.find((candidate) => existsSync(candidate));
|
|
194
|
+
if (!entryPath) throw new Error("Could not resolve pi-coding-agent entry path");
|
|
195
|
+
return entryPath;
|
|
196
|
+
}
|
|
197
|
+
|
|
156
198
|
async function installSilentHiddenThinkingPatch(): Promise<void> {
|
|
157
|
-
const entryPath =
|
|
199
|
+
const entryPath = resolvePiCodingAgentEntryPath();
|
|
158
200
|
const componentPath = join(dirname(entryPath), "modes", "interactive", "components", "assistant-message.js");
|
|
159
201
|
const mod = await import(pathToFileURL(componentPath).href) as {
|
|
160
202
|
AssistantMessageComponent?: AssistantMessageComponentCtor;
|
|
@@ -191,6 +233,222 @@ async function installSilentHiddenThinkingPatch(): Promise<void> {
|
|
|
191
233
|
proto[PATCHED_ASSISTANT_MESSAGE] = true;
|
|
192
234
|
}
|
|
193
235
|
|
|
236
|
+
type ToolExecutionComponentCtor = {
|
|
237
|
+
prototype: {
|
|
238
|
+
getRenderShell?: () => "default" | "self";
|
|
239
|
+
hasRendererDefinition?: () => boolean;
|
|
240
|
+
render?: (width: number) => string[];
|
|
241
|
+
};
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
type PatchableToolExecutionComponent = {
|
|
245
|
+
toolName?: string;
|
|
246
|
+
args?: unknown;
|
|
247
|
+
result?: { content?: Array<{ type: string; text?: string }>; details?: unknown; isError?: boolean };
|
|
248
|
+
expanded?: boolean;
|
|
249
|
+
hasRendererDefinition?: () => boolean;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
type ExternalToolFrameKind = "serena" | "gitnexus" | "structured" | "process" | "external";
|
|
253
|
+
|
|
254
|
+
const PATCHED_EXTERNAL_TOOL_FRAME = "__xtrmUiExternalToolFrame";
|
|
255
|
+
const EXTERNAL_TOOL_FRAME_PATCH_VERSION = 11;
|
|
256
|
+
const ANSI_PATTERN = /\x1b\[[0-9;?]*[ -/]*[@-~]/g;
|
|
257
|
+
|
|
258
|
+
function stripAnsi(text: string): string {
|
|
259
|
+
return text.replace(ANSI_PATTERN, "");
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function isBlankRenderedLine(line: string): boolean {
|
|
263
|
+
return stripAnsi(line).trim().length === 0;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function externalToolFrameKind(toolName: string | undefined): ExternalToolFrameKind | undefined {
|
|
267
|
+
if (!toolName || XTRM_BUILTIN_TOOLS.has(toolName)) return undefined;
|
|
268
|
+
if (toolName === "structured_return") return "structured";
|
|
269
|
+
if (toolName === "process") return "process";
|
|
270
|
+
if (toolName.startsWith("gitnexus_")) return "gitnexus";
|
|
271
|
+
if (SERENA_COMPACT_TOOLS.has(toolName)) return "serena";
|
|
272
|
+
return "external";
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function padVisible(text: string, width: number): string {
|
|
276
|
+
const visible = visibleWidth(text);
|
|
277
|
+
return text + " ".repeat(Math.max(0, width - visible));
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function getXtrmOriginalText(details: unknown): string | undefined {
|
|
281
|
+
const record = asRecord(details);
|
|
282
|
+
return typeof record?.xtrmOriginalText === "string" ? record.xtrmOriginalText : undefined;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function getToolArgs(component: PatchableToolExecutionComponent): Record<string, unknown> {
|
|
286
|
+
return component.args && typeof component.args === "object" && !Array.isArray(component.args)
|
|
287
|
+
? component.args as Record<string, unknown>
|
|
288
|
+
: {};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function summarizeExternalToolPending(toolName: string | undefined, input: Record<string, unknown>): string {
|
|
292
|
+
const name = toolName ?? "tool";
|
|
293
|
+
if (name === "structured_return") {
|
|
294
|
+
return `• structured_return ${shortenCommand(String(input.command ?? "running"), 38)}`;
|
|
295
|
+
}
|
|
296
|
+
if (name === "process") {
|
|
297
|
+
return `• process ${String(input.action ?? "running")}`;
|
|
298
|
+
}
|
|
299
|
+
if (name.startsWith("gitnexus_")) {
|
|
300
|
+
const subject = summarizeSerenaSubject(name, input) ?? summarizeToolSubject(name, input);
|
|
301
|
+
return `• ${normalizeToolLabel(name)}${subject ? ` ${subject}` : ""}`;
|
|
302
|
+
}
|
|
303
|
+
if (SERENA_COMPACT_TOOLS.has(name)) {
|
|
304
|
+
const subject = summarizeSerenaSubject(name, input);
|
|
305
|
+
return `• serena ${name}${subject ? ` ${subject}` : ""}`;
|
|
306
|
+
}
|
|
307
|
+
const subject = summarizeToolSubject(name, input) ?? summarizeSerenaSubject(name, input);
|
|
308
|
+
return `• ${normalizeToolLabel(name)}${subject ? ` ${subject}` : ""}`;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function extractResultTextLines(component: PatchableToolExecutionComponent): string[] | undefined {
|
|
312
|
+
const originalText = component.expanded ? getXtrmOriginalText(component.result?.details) : undefined;
|
|
313
|
+
if (originalText) return originalText.split("\n");
|
|
314
|
+
|
|
315
|
+
const text = component.result?.content?.find((content) => content.type === "text")?.text;
|
|
316
|
+
if (text) return text.split("\n");
|
|
317
|
+
|
|
318
|
+
return [summarizeExternalToolPending(component.toolName, getToolArgs(component))];
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function trimRenderedToolLines(lines: string[]): string[] {
|
|
322
|
+
let start = 0;
|
|
323
|
+
let end = lines.length;
|
|
324
|
+
while (start < end && isBlankRenderedLine(lines[start] ?? "")) start++;
|
|
325
|
+
while (end > start && isBlankRenderedLine(lines[end - 1] ?? "")) end--;
|
|
326
|
+
return lines.slice(start, end).map((line) => line.replace(/\s+$/u, ""));
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function externalToolBadgeColor(kind: ExternalToolFrameKind, text: string): string {
|
|
330
|
+
const bgColors: Record<ExternalToolFrameKind, [number, number, number]> = {
|
|
331
|
+
serena: [82, 210, 255],
|
|
332
|
+
gitnexus: [178, 154, 255],
|
|
333
|
+
structured: [205, 166, 255],
|
|
334
|
+
process: [92, 226, 255],
|
|
335
|
+
external: [178, 190, 210],
|
|
336
|
+
};
|
|
337
|
+
const [badgeR, badgeG, badgeB] = bgColors[kind];
|
|
338
|
+
return `\x1b[38;2;3;8;12m\x1b[48;2;${badgeR};${badgeG};${badgeB}m${text}\x1b[39m\x1b[49m`;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function highlightExternalToolBadge(kind: ExternalToolFrameKind, line: string): string {
|
|
342
|
+
const match = line.match(/^(•\s+)(\S+)/u);
|
|
343
|
+
if (!match?.[1] || !match[2]) return line;
|
|
344
|
+
return match[1] + externalToolBadgeColor(kind, match[2]) + line.slice(match[1].length + match[2].length);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function externalToolBorderColor(kind: ExternalToolFrameKind, text: string): string {
|
|
348
|
+
const colors: Record<ExternalToolFrameKind, [number, number, number]> = {
|
|
349
|
+
serena: [150, 210, 255],
|
|
350
|
+
gitnexus: [185, 168, 255],
|
|
351
|
+
structured: [205, 166, 255],
|
|
352
|
+
process: [145, 231, 255],
|
|
353
|
+
external: [168, 181, 199],
|
|
354
|
+
};
|
|
355
|
+
const [r, g, b] = colors[kind];
|
|
356
|
+
return `[2m[38;2;${r};${g};${b}m${text}[39m[22m`;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function collapsedExternalToolLines(contentLines: string[], expanded: boolean): string[] {
|
|
360
|
+
return expanded ? contentLines : [contentLines.join(" · ")];
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function renderExternalToolBackgroundLines(
|
|
364
|
+
contentLines: string[],
|
|
365
|
+
width: number,
|
|
366
|
+
kind: ExternalToolFrameKind,
|
|
367
|
+
expanded: boolean,
|
|
368
|
+
): string[] {
|
|
369
|
+
const availableWidth = Math.max(8, width);
|
|
370
|
+
const renderWidth = availableWidth;
|
|
371
|
+
const visibleLines = collapsedExternalToolLines(contentLines, expanded);
|
|
372
|
+
|
|
373
|
+
return visibleLines.map((rawLine) => {
|
|
374
|
+
const line = truncateToWidth(rawLine, Math.max(1, renderWidth));
|
|
375
|
+
return highlightExternalToolBadge(kind, line);
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function renderExternalToolBoxLines(
|
|
380
|
+
contentLines: string[],
|
|
381
|
+
width: number,
|
|
382
|
+
kind: ExternalToolFrameKind,
|
|
383
|
+
expanded: boolean,
|
|
384
|
+
): string[] {
|
|
385
|
+
const availableWidth = Math.max(8, width - 4);
|
|
386
|
+
const maxContentWidth = expanded ? availableWidth : Math.min(availableWidth, 34);
|
|
387
|
+
const visibleLines = collapsedExternalToolLines(contentLines, expanded);
|
|
388
|
+
const contentWidth = Math.max(
|
|
389
|
+
1,
|
|
390
|
+
Math.min(maxContentWidth, ...visibleLines.map((line) => visibleWidth(line))),
|
|
391
|
+
);
|
|
392
|
+
const innerWidth = contentWidth + 2;
|
|
393
|
+
|
|
394
|
+
const framed = [externalToolBorderColor(kind, `╭${"─".repeat(innerWidth)}╮`)];
|
|
395
|
+
for (const rawLine of visibleLines) {
|
|
396
|
+
const line = truncateToWidth(rawLine, contentWidth);
|
|
397
|
+
framed.push(`${externalToolBorderColor(kind, "│")} ${padVisible(line, contentWidth)} ${externalToolBorderColor(kind, "│")}`);
|
|
398
|
+
}
|
|
399
|
+
framed.push(externalToolBorderColor(kind, `╰${"─".repeat(innerWidth)}╯`));
|
|
400
|
+
return framed;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function renderExternalToolLines(
|
|
404
|
+
lines: string[],
|
|
405
|
+
width: number,
|
|
406
|
+
kind: ExternalToolFrameKind,
|
|
407
|
+
expanded = false,
|
|
408
|
+
): string[] {
|
|
409
|
+
const contentLines = trimRenderedToolLines(lines).filter((line) => !isBlankRenderedLine(line));
|
|
410
|
+
if (contentLines.length === 0) return [];
|
|
411
|
+
|
|
412
|
+
return activeExternalToolChrome === "box"
|
|
413
|
+
? renderExternalToolBoxLines(contentLines, width, kind, expanded)
|
|
414
|
+
: renderExternalToolBackgroundLines(contentLines, width, kind, expanded);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
async function installExternalToolFramePatch(): Promise<void> {
|
|
418
|
+
const entryPath = resolvePiCodingAgentEntryPath();
|
|
419
|
+
const componentPath = join(dirname(entryPath), "modes", "interactive", "components", "tool-execution.js");
|
|
420
|
+
const mod = await import(pathToFileURL(componentPath).href) as {
|
|
421
|
+
ToolExecutionComponent?: ToolExecutionComponentCtor;
|
|
422
|
+
};
|
|
423
|
+
const proto = mod.ToolExecutionComponent?.prototype as
|
|
424
|
+
| (ToolExecutionComponentCtor["prototype"] & { [PATCHED_EXTERNAL_TOOL_FRAME]?: boolean })
|
|
425
|
+
| undefined;
|
|
426
|
+
if (!proto?.render || proto[PATCHED_EXTERNAL_TOOL_FRAME] === EXTERNAL_TOOL_FRAME_PATCH_VERSION) return;
|
|
427
|
+
|
|
428
|
+
const getRenderShell = proto.getRenderShell;
|
|
429
|
+
const render = proto.render;
|
|
430
|
+
|
|
431
|
+
proto.getRenderShell = function patchedGetRenderShell(this: PatchableToolExecutionComponent) {
|
|
432
|
+
const kind = externalToolFrameKind(this.toolName);
|
|
433
|
+
if (kind) return "self";
|
|
434
|
+
return getRenderShell?.call(this) ?? "default";
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
proto.render = function patchedRender(this: PatchableToolExecutionComponent, width: number) {
|
|
438
|
+
const rendered = render.call(this, width);
|
|
439
|
+
const kind = externalToolFrameKind(this.toolName);
|
|
440
|
+
if (!kind || rendered.length === 0) return rendered;
|
|
441
|
+
|
|
442
|
+
const firstContentIndex = rendered.findIndex((line) => !isBlankRenderedLine(line));
|
|
443
|
+
const leading = firstContentIndex > 0 ? rendered.slice(0, firstContentIndex) : [];
|
|
444
|
+
const content = extractResultTextLines(this) ?? rendered;
|
|
445
|
+
const styled = renderExternalToolLines(content, width, kind, Boolean(this.expanded));
|
|
446
|
+
return styled.length > 0 ? [...leading, ...styled] : rendered;
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
proto[PATCHED_EXTERNAL_TOOL_FRAME] = EXTERNAL_TOOL_FRAME_PATCH_VERSION;
|
|
450
|
+
}
|
|
451
|
+
|
|
194
452
|
function applyThinkingChrome(ctx: ExtensionContext, prefs: XtrmUiPrefs): void {
|
|
195
453
|
(ctx.ui as { setHiddenThinkingLabel?: (label?: string) => void }).setHiddenThinkingLabel?.(
|
|
196
454
|
prefs.hideThinkingPlaceholder ? undefined : "",
|
|
@@ -396,6 +654,13 @@ function parseDensityArg(arg: string): XtrmDensity | undefined {
|
|
|
396
654
|
return undefined;
|
|
397
655
|
}
|
|
398
656
|
|
|
657
|
+
function parseExternalToolChromeArg(arg: string): XtrmExternalToolChrome | undefined {
|
|
658
|
+
const normalized = arg.trim().toLowerCase();
|
|
659
|
+
if (normalized === "background" || normalized === "bg" || normalized === "row") return "background";
|
|
660
|
+
if (normalized === "box" || normalized === "frame" || normalized === "border") return "box";
|
|
661
|
+
return undefined;
|
|
662
|
+
}
|
|
663
|
+
|
|
399
664
|
function registerCommands(pi: ExtensionAPI, getPrefs: () => XtrmUiPrefs, setPrefs: (p: XtrmUiPrefs) => void, getThinkingLevel: () => string) {
|
|
400
665
|
pi.registerMessageRenderer("xtrm-ui-info", (message, _options, theme) => {
|
|
401
666
|
const title = (message.details as { title?: string } | undefined)?.title ?? "XTRM UI";
|
|
@@ -407,7 +672,26 @@ function registerCommands(pi: ExtensionAPI, getPrefs: () => XtrmUiPrefs, setPref
|
|
|
407
672
|
|
|
408
673
|
pi.registerCommand("xtrm-ui", {
|
|
409
674
|
description: "Show XTRM UI status and active preferences",
|
|
410
|
-
handler: async (
|
|
675
|
+
handler: async (args, ctx) => {
|
|
676
|
+
const trimmedArgs = args.trim();
|
|
677
|
+
if (trimmedArgs) {
|
|
678
|
+
const [subcommand, ...rest] = trimmedArgs.split(/\s+/u);
|
|
679
|
+
if (subcommand === "chrome" || subcommand === "external-chrome" || subcommand === "tool-chrome") {
|
|
680
|
+
const externalToolChrome = parseExternalToolChromeArg(rest.join(" "));
|
|
681
|
+
if (!externalToolChrome) {
|
|
682
|
+
ctx.ui.notify("Usage: /xtrm-ui chrome background|box", "warning");
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
const prefs = { ...getPrefs(), externalToolChrome };
|
|
686
|
+
setPrefs(prefs);
|
|
687
|
+
persistPrefs(pi, prefs);
|
|
688
|
+
ctx.ui.notify(`External tool chrome set to ${externalToolChrome}.`, "info");
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
ctx.ui.notify("Usage: /xtrm-ui [chrome background|box]", "warning");
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
|
|
411
695
|
const prefs = getPrefs();
|
|
412
696
|
const contextUsage = ctx.getContextUsage();
|
|
413
697
|
const lines = [
|
|
@@ -419,6 +703,7 @@ function registerCommands(pi: ExtensionAPI, getPrefs: () => XtrmUiPrefs, setPref
|
|
|
419
703
|
`Show footer: ${prefs.showFooter ? "yes" : "no"} (custom-footer handles this)`,
|
|
420
704
|
`Tool row background: ${prefs.toolRowBg ? "on" : "off"}`,
|
|
421
705
|
`Compact external tool results: ${prefs.compactExternalToolResults ? "on" : "off"}`,
|
|
706
|
+
`External tool chrome: ${prefs.externalToolChrome}`,
|
|
422
707
|
`Model: ${ctx.model?.id ?? "none"}`,
|
|
423
708
|
`Context: ${contextUsage?.tokens ?? "unknown"}/${contextUsage?.contextWindow ?? "unknown"}`,
|
|
424
709
|
];
|
|
@@ -549,6 +834,25 @@ function registerCommands(pi: ExtensionAPI, getPrefs: () => XtrmUiPrefs, setPref
|
|
|
549
834
|
},
|
|
550
835
|
});
|
|
551
836
|
|
|
837
|
+
pi.registerCommand("xtrm-ui-external-chrome", {
|
|
838
|
+
description: "Choose non-native tool chrome: background|box",
|
|
839
|
+
getArgumentCompletions: (prefix) => {
|
|
840
|
+
const values = ["background", "box"].filter((item) => item.startsWith(prefix));
|
|
841
|
+
return values.length > 0 ? values.map((value) => ({ value, label: value })) : null;
|
|
842
|
+
},
|
|
843
|
+
handler: async (args, ctx) => {
|
|
844
|
+
const externalToolChrome = parseExternalToolChromeArg(args);
|
|
845
|
+
if (!externalToolChrome) {
|
|
846
|
+
ctx.ui.notify("Usage: /xtrm-ui-external-chrome background|box", "warning");
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
const prefs = { ...getPrefs(), externalToolChrome };
|
|
850
|
+
setPrefs(prefs);
|
|
851
|
+
persistPrefs(pi, prefs);
|
|
852
|
+
ctx.ui.notify(`External tool chrome set to ${externalToolChrome}.`, "info");
|
|
853
|
+
},
|
|
854
|
+
});
|
|
855
|
+
|
|
552
856
|
pi.registerCommand("xtrm-ui-reset", {
|
|
553
857
|
description: "Restore XTRM UI defaults",
|
|
554
858
|
handler: async (_args, ctx) => {
|
|
@@ -956,6 +1260,101 @@ function summarizeGenericToolResult(
|
|
|
956
1260
|
return `• ${normalized}${subject ? ` ${subject}` : ""}${joined ? ` · ${joined}` : ""}`;
|
|
957
1261
|
}
|
|
958
1262
|
|
|
1263
|
+
function summarizeStructuredReturnToolResult(
|
|
1264
|
+
input: Record<string, unknown>,
|
|
1265
|
+
text: string,
|
|
1266
|
+
details: unknown,
|
|
1267
|
+
durationMs: number | undefined,
|
|
1268
|
+
): string {
|
|
1269
|
+
const record = asRecord(details);
|
|
1270
|
+
const command = shortenCommand(String(input.command ?? text.split("→")[0] ?? "command"), 52);
|
|
1271
|
+
const resultText = text.includes("→") ? text.split("→").slice(1).join("→").trim() : text.trim();
|
|
1272
|
+
const resultLines = resultText.split("\n").map((line) => line.trim()).filter(Boolean);
|
|
1273
|
+
const summary = resultLines.find((line) => !line.startsWith("cwd:"));
|
|
1274
|
+
const parser = typeof record?.parser === "string" ? record.parser : undefined;
|
|
1275
|
+
const exitCode = typeof record?.exitCode === "number" ? `exit ${record.exitCode}` : undefined;
|
|
1276
|
+
const duration = formatDuration(durationMs);
|
|
1277
|
+
const meta = joinMeta([summary ? shortenCommand(summary, 72) : undefined, parser, exitCode, duration]);
|
|
1278
|
+
return `• structured_return ${command}${meta ? ` · ${meta}` : ""}`;
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
function summarizeProcessToolResult(
|
|
1282
|
+
input: Record<string, unknown>,
|
|
1283
|
+
text: string,
|
|
1284
|
+
details: unknown,
|
|
1285
|
+
durationMs: number | undefined,
|
|
1286
|
+
): string {
|
|
1287
|
+
const record = asRecord(details);
|
|
1288
|
+
const action = String(record?.action ?? input.action ?? "action");
|
|
1289
|
+
const duration = formatDuration(durationMs);
|
|
1290
|
+
const meta = (...parts: Array<string | undefined>) => {
|
|
1291
|
+
const joined = joinMeta([...parts, duration]);
|
|
1292
|
+
return joined ? ` · ${joined}` : "";
|
|
1293
|
+
};
|
|
1294
|
+
|
|
1295
|
+
if (action === "start") {
|
|
1296
|
+
const proc = asRecord(record?.process);
|
|
1297
|
+
const name = String(proc?.name ?? input.name ?? "process");
|
|
1298
|
+
const id = proc?.id ? String(proc.id) : undefined;
|
|
1299
|
+
const pid = proc?.pid != null ? `pid ${String(proc.pid)}` : undefined;
|
|
1300
|
+
return `• process start "${name}"${meta(id, pid)}`;
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
if (action === "list") {
|
|
1304
|
+
const processes = Array.isArray(record?.processes) ? record.processes : [];
|
|
1305
|
+
const running = processes.filter((item) => {
|
|
1306
|
+
const proc = asRecord(item);
|
|
1307
|
+
return proc?.status === "running" || proc?.status === "terminating";
|
|
1308
|
+
}).length;
|
|
1309
|
+
return `• process list${meta(`${processes.length} ${processes.length === 1 ? "process" : "processes"}`, `${running} running`)}`;
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
if (action === "output") {
|
|
1313
|
+
const output = asRecord(record?.output);
|
|
1314
|
+
const stdout = Array.isArray(output?.stdout) ? output.stdout.length : undefined;
|
|
1315
|
+
const stderr = Array.isArray(output?.stderr) ? output.stderr.length : undefined;
|
|
1316
|
+
return `• process output ${String(input.id ?? "process")}${meta(
|
|
1317
|
+
stdout != null ? `${stdout} stdout` : undefined,
|
|
1318
|
+
stderr != null ? `${stderr} stderr` : undefined,
|
|
1319
|
+
)}`;
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
if (action === "logs") {
|
|
1323
|
+
return `• process logs ${String(input.id ?? "process")}${meta("log paths")}`;
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
const message = typeof record?.message === "string" ? record.message : text.split("\n")[0];
|
|
1327
|
+
return `• process ${action}${message ? ` · ${shortenCommand(message, 38)}` : ""}${duration ? ` · ${duration}` : ""}`;
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
function summarizeExternalToolResult(
|
|
1331
|
+
toolName: string,
|
|
1332
|
+
input: Record<string, unknown>,
|
|
1333
|
+
text: string,
|
|
1334
|
+
details: unknown,
|
|
1335
|
+
durationMs: number | undefined,
|
|
1336
|
+
): string {
|
|
1337
|
+
if (SERENA_COMPACT_TOOLS.has(toolName)) {
|
|
1338
|
+
return summarizeSerenaToolResult(toolName, input, text, durationMs);
|
|
1339
|
+
}
|
|
1340
|
+
if (toolName === "structured_return") {
|
|
1341
|
+
return summarizeStructuredReturnToolResult(input, text, details, durationMs);
|
|
1342
|
+
}
|
|
1343
|
+
if (toolName === "process") {
|
|
1344
|
+
return summarizeProcessToolResult(input, text, details, durationMs);
|
|
1345
|
+
}
|
|
1346
|
+
return summarizeGenericToolResult(toolName, input, text, durationMs);
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
function withXtrmToolDetails(details: unknown, sourceText: string, toolName: string): unknown {
|
|
1350
|
+
const record = asRecord(details);
|
|
1351
|
+
return {
|
|
1352
|
+
...(record ?? {}),
|
|
1353
|
+
xtrmOriginalText: sourceText,
|
|
1354
|
+
xtrmToolFrame: externalToolFrameKind(toolName),
|
|
1355
|
+
};
|
|
1356
|
+
}
|
|
1357
|
+
|
|
959
1358
|
const XTRM_BUILTIN_TOOLS = new Set(["bash", "read", "edit", "write", "find", "grep", "ls"]);
|
|
960
1359
|
|
|
961
1360
|
function registerXtrmUiTools(pi: ExtensionAPI, getPrefs: () => XtrmUiPrefs): void {
|
|
@@ -1004,7 +1403,11 @@ function registerXtrmUiTools(pi: ExtensionAPI, getPrefs: () => XtrmUiPrefs): voi
|
|
|
1004
1403
|
|
|
1005
1404
|
pi.on("tool_result", async (event: ToolResultEvent, _ctx) => {
|
|
1006
1405
|
if (event.isError) return undefined;
|
|
1007
|
-
if (XTRM_BUILTIN_TOOLS.has(event.toolName))
|
|
1406
|
+
if (XTRM_BUILTIN_TOOLS.has(event.toolName)) {
|
|
1407
|
+
trackToolCallEnd(event.toolCallId);
|
|
1408
|
+
return undefined;
|
|
1409
|
+
}
|
|
1410
|
+
if (!getPrefs().compactExternalToolResults) return undefined;
|
|
1008
1411
|
|
|
1009
1412
|
const text = getTextContent({ content: event.content as Array<{ type: string; text?: string }> });
|
|
1010
1413
|
const startedAt = toolCallStartTimes.get(event.toolCallId);
|
|
@@ -1018,13 +1421,17 @@ function registerXtrmUiTools(pi: ExtensionAPI, getPrefs: () => XtrmUiPrefs): voi
|
|
|
1018
1421
|
? (event.input as Record<string, unknown>)
|
|
1019
1422
|
: {};
|
|
1020
1423
|
|
|
1021
|
-
const compactText =
|
|
1022
|
-
|
|
1023
|
-
|
|
1424
|
+
const compactText = summarizeExternalToolResult(
|
|
1425
|
+
event.toolName,
|
|
1426
|
+
safeInput,
|
|
1427
|
+
sourceText,
|
|
1428
|
+
event.details,
|
|
1429
|
+
durationMs,
|
|
1430
|
+
);
|
|
1024
1431
|
|
|
1025
1432
|
return {
|
|
1026
1433
|
content: [{ type: "text", text: formatHierarchyText(compactText) }],
|
|
1027
|
-
details: event.details,
|
|
1434
|
+
details: withXtrmToolDetails(event.details, sourceText, event.toolName),
|
|
1028
1435
|
};
|
|
1029
1436
|
});
|
|
1030
1437
|
|
|
@@ -1043,7 +1450,7 @@ function registerXtrmUiTools(pi: ExtensionAPI, getPrefs: () => XtrmUiPrefs): voi
|
|
|
1043
1450
|
renderResult(result, { expanded, isPartial }, theme) {
|
|
1044
1451
|
const details = (result.details ?? {}) as DetailsWithXtrmMeta<BashToolDetails, Record<string, unknown>>;
|
|
1045
1452
|
const meta = getXtrmMeta<BashToolDetails, Record<string, unknown>>(details);
|
|
1046
|
-
const command = shortenCommand(String(meta?.args.command ?? ""));
|
|
1453
|
+
const command = shortenCommand(String(meta?.args.command ?? ""), 38);
|
|
1047
1454
|
if (isPartial) {
|
|
1048
1455
|
return toolRowText(theme, `${theme.fg("accent", "•")} ${theme.fg("toolTitle", "Running ")}${theme.fg("accent", command)}${theme.fg("toolTitle", " in bash")}`);
|
|
1049
1456
|
}
|
|
@@ -1263,13 +1670,17 @@ function isXtrmTheme(name: string | undefined): boolean {
|
|
|
1263
1670
|
|
|
1264
1671
|
export default function xtrmUiExtension(pi: ExtensionAPI): void {
|
|
1265
1672
|
void installSilentHiddenThinkingPatch().catch(() => undefined);
|
|
1673
|
+
void installExternalToolFramePatch().catch(() => undefined);
|
|
1266
1674
|
|
|
1267
1675
|
let prefs: XtrmUiPrefs = { ...DEFAULT_PREFS };
|
|
1268
1676
|
let previousThemeName: string | null = null;
|
|
1269
1677
|
const extensionThemeDir = join(__dirname, "../../themes/xtrm-ui");
|
|
1270
1678
|
|
|
1271
1679
|
const getPrefs = () => prefs;
|
|
1272
|
-
const setPrefs = (p: XtrmUiPrefs) => {
|
|
1680
|
+
const setPrefs = (p: XtrmUiPrefs) => {
|
|
1681
|
+
prefs = p;
|
|
1682
|
+
setActiveExternalToolChrome(p.externalToolChrome);
|
|
1683
|
+
};
|
|
1273
1684
|
const getThinkingLevel = () => formatThinking(pi.getThinkingLevel());
|
|
1274
1685
|
|
|
1275
1686
|
registerXtrmUiTools(pi, getPrefs);
|
|
@@ -1285,7 +1696,7 @@ export default function xtrmUiExtension(pi: ExtensionAPI): void {
|
|
|
1285
1696
|
}));
|
|
1286
1697
|
|
|
1287
1698
|
pi.on("session_start", async (_event, ctx) => {
|
|
1288
|
-
|
|
1699
|
+
setPrefs(loadPrefs(ctx.sessionManager.getEntries() as Array<MaybeCustomEntry>));
|
|
1289
1700
|
if (!previousThemeName && !isXtrmTheme(ctx.ui.theme.name)) {
|
|
1290
1701
|
previousThemeName = ctx.ui.theme.name ?? null;
|
|
1291
1702
|
}
|
package/package.json
CHANGED
package/src/registry.ts
CHANGED
|
@@ -12,6 +12,7 @@ import piSerenaCompactExtension from "./extensions/pi-serena-compact.ts";
|
|
|
12
12
|
import qualityGatesExtension from "./extensions/quality-gates.ts";
|
|
13
13
|
import serviceSkillsExtension from "./extensions/service-skills.ts";
|
|
14
14
|
import sessionFlowExtension from "./extensions/session-flow.ts";
|
|
15
|
+
import spTerminalOverlayExtension from "./extensions/sp-terminal-overlay.ts";
|
|
15
16
|
import xtrmLoaderExtension from "./extensions/xtrm-loader.ts";
|
|
16
17
|
import xtrmUiExtension from "./extensions/xtrm-ui.ts";
|
|
17
18
|
|
|
@@ -33,6 +34,7 @@ export const managedPiExtensions: readonly ManagedPiExtension[] = [
|
|
|
33
34
|
{ id: "quality-gates", register: qualityGatesExtension },
|
|
34
35
|
{ id: "service-skills", register: serviceSkillsExtension },
|
|
35
36
|
{ id: "session-flow", register: sessionFlowExtension },
|
|
37
|
+
{ id: "sp-terminal-overlay", register: spTerminalOverlayExtension },
|
|
36
38
|
{ id: "xtrm-loader", register: xtrmLoaderExtension },
|
|
37
39
|
{ id: "xtrm-ui", register: xtrmUiExtension },
|
|
38
40
|
];
|