@xynogen/pix-pretty 1.27.0 → 1.30.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 +25 -11
- package/package.json +7 -3
- package/src/chips.ts +307 -0
- package/src/command-preview.ts +77 -0
- package/src/commands/fff.ts +2 -2
- package/src/confirm.ts +1 -1
- package/src/diff-render.ts +47 -16
- package/src/fff.ts +11 -9
- package/src/gate-overlay.ts +1 -1
- package/src/highlight.ts +3 -3
- package/src/icon-catalog.ts +47 -9
- package/src/icon-persist.ts +1 -1
- package/src/icons.ts +1 -1
- package/src/index.ts +5 -5
- package/src/lang.ts +1 -1
- package/src/progress.ts +2 -2
- package/src/provider-picker.ts +16 -5
- package/src/renderers.ts +10 -8
- package/src/shell-tool.ts +259 -0
- package/src/test-utils.ts +6 -3
- package/src/tool-status.ts +85 -0
- package/src/tools/context.ts +2 -2
- package/src/types.ts +4 -0
- package/src/utils.ts +130 -73
package/src/icon-catalog.ts
CHANGED
|
@@ -1,21 +1,59 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* icon-catalog.ts —
|
|
2
|
+
* icon-catalog.ts — the public icon surface of pix-pretty.
|
|
3
3
|
*
|
|
4
|
-
* The
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
4
|
+
* The shared catalog lives in `@xynogen/pix-runtime` (a lower layer) so that
|
|
5
|
+
* pix-runtime's own UI — the `/pix` settings overlay — can resolve icons
|
|
6
|
+
* without a circular dependency on pix-pretty. This file re-exports that
|
|
7
|
+
* surface, and adds the display-only icons that pix-pretty owns
|
|
8
|
+
* (`PRETTY_CATALOG`). Both follow the same global icon mode.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
import {
|
|
12
|
+
getIconMode,
|
|
13
|
+
type IconMode,
|
|
14
|
+
type IconKey as RuntimeIconKey,
|
|
15
|
+
icon as runtimeIcon,
|
|
16
|
+
iconFor as runtimeIconFor,
|
|
17
|
+
} from "@xynogen/pix-runtime/icon-catalog";
|
|
18
|
+
|
|
11
19
|
export {
|
|
12
20
|
getIconMode,
|
|
13
21
|
ICON_KEYS,
|
|
14
22
|
ICON_MODES,
|
|
15
|
-
type IconKey,
|
|
16
23
|
type IconMode,
|
|
17
|
-
icon,
|
|
18
|
-
iconFor,
|
|
19
24
|
onIconModeChange,
|
|
20
25
|
setIconMode,
|
|
21
26
|
} from "@xynogen/pix-runtime/icon-catalog";
|
|
27
|
+
|
|
28
|
+
const VS = "\uFE0E"; // force text presentation
|
|
29
|
+
|
|
30
|
+
/** Icons that pix-pretty owns. Same shape as the runtime catalog. */
|
|
31
|
+
const PRETTY_CATALOG = {
|
|
32
|
+
// ── agent mode (pix-core plan mode, footer) ───────────────────────────
|
|
33
|
+
"mode.plan": { nerd: "\u{F034D}", unicode: `\u2630${VS}`, ascii: "P" },
|
|
34
|
+
"mode.normal": { nerd: "\u{F0174}", unicode: `\u276F${VS}`, ascii: ">" },
|
|
35
|
+
// ── model kind (pix-models picker) ─────────────────────────────────────
|
|
36
|
+
"model.classifier": { nerd: "\u{F05D1}", unicode: `\u2696${VS}`, ascii: "C" },
|
|
37
|
+
} as const satisfies Record<string, Record<IconMode, string>>;
|
|
38
|
+
|
|
39
|
+
type PrettyIconKey = keyof typeof PRETTY_CATALOG;
|
|
40
|
+
|
|
41
|
+
/** Every valid semantic icon key: runtime catalog + pix-pretty catalog. */
|
|
42
|
+
export type IconKey = RuntimeIconKey | PrettyIconKey;
|
|
43
|
+
|
|
44
|
+
/** pix-pretty's own keys (the runtime keys are in `ICON_KEYS`). */
|
|
45
|
+
export const PRETTY_ICON_KEYS = Object.keys(PRETTY_CATALOG) as PrettyIconKey[];
|
|
46
|
+
|
|
47
|
+
function isPrettyKey(key: IconKey): key is PrettyIconKey {
|
|
48
|
+
return Object.hasOwn(PRETTY_CATALOG, key);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Resolve a semantic icon key to its glyph for the active mode. Unknown keys return "". */
|
|
52
|
+
export function icon(key: IconKey): string {
|
|
53
|
+
return isPrettyKey(key) ? PRETTY_CATALOG[key][getIconMode()] : runtimeIcon(key);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Resolve a key for an explicit mode (used by /pix previews + tests). */
|
|
57
|
+
export function iconFor(key: IconKey, mode: IconMode): string {
|
|
58
|
+
return isPrettyKey(key) ? PRETTY_CATALOG[key][mode] : runtimeIconFor(key, mode);
|
|
59
|
+
}
|
package/src/icon-persist.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import { config, onConfigChange, updateConfig } from "@xynogen/pix-runtime/config";
|
|
13
13
|
import { prettySection } from "@xynogen/pix-runtime/sections";
|
|
14
|
-
import { ICON_MODES, type IconMode, setIconMode } from "./icon-catalog.
|
|
14
|
+
import { ICON_MODES, type IconMode, setIconMode } from "./icon-catalog.ts";
|
|
15
15
|
|
|
16
16
|
function isIconMode(m: string): m is IconMode {
|
|
17
17
|
return (ICON_MODES as readonly string[]).includes(m);
|
package/src/icons.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { basename, extname } from "node:path";
|
|
2
|
-
import type { FgTheme } from "./types.
|
|
2
|
+
import type { FgTheme } from "./types.ts";
|
|
3
3
|
|
|
4
4
|
const ICONS_MODE = (process.env.PRETTY_ICONS ?? "nerd").toLowerCase();
|
|
5
5
|
const USE_ICONS = ICONS_MODE !== "none" && ICONS_MODE !== "off";
|
package/src/index.ts
CHANGED
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
12
|
-
import { registerFffCommands } from "./commands/fff.
|
|
13
|
-
import { fffState } from "./fff.
|
|
14
|
-
import { clearHighlightCache } from "./highlight.
|
|
15
|
-
import { initIconMode } from "./icon-persist.
|
|
16
|
-
import type { PiPrettyApi } from "./types.
|
|
12
|
+
import { registerFffCommands } from "./commands/fff.ts";
|
|
13
|
+
import { fffState } from "./fff.ts";
|
|
14
|
+
import { clearHighlightCache } from "./highlight.ts";
|
|
15
|
+
import { initIconMode } from "./icon-persist.ts";
|
|
16
|
+
import type { PiPrettyApi } from "./types.ts";
|
|
17
17
|
|
|
18
18
|
export default function piPrettyExtension(pi: ExtensionAPI): void {
|
|
19
19
|
const prettyPi = pi as unknown as PiPrettyApi;
|
package/src/lang.ts
CHANGED
package/src/progress.ts
CHANGED
|
@@ -19,8 +19,8 @@ import {
|
|
|
19
19
|
modalOverlayOptions,
|
|
20
20
|
modalWidth,
|
|
21
21
|
terminalModalHeight,
|
|
22
|
-
} from "./modal-frame.
|
|
23
|
-
import { SPINNER } from "./widget-format.
|
|
22
|
+
} from "./modal-frame.ts";
|
|
23
|
+
import { SPINNER } from "./widget-format.ts";
|
|
24
24
|
|
|
25
25
|
interface ProgressTheme {
|
|
26
26
|
fg(color: string, text: string): string;
|
package/src/provider-picker.ts
CHANGED
|
@@ -14,6 +14,8 @@ import {
|
|
|
14
14
|
Key,
|
|
15
15
|
type KeybindingsManager,
|
|
16
16
|
matchesKey,
|
|
17
|
+
truncateToWidth,
|
|
18
|
+
visibleWidth,
|
|
17
19
|
} from "@earendil-works/pi-tui";
|
|
18
20
|
import {
|
|
19
21
|
frameModal,
|
|
@@ -22,7 +24,8 @@ import {
|
|
|
22
24
|
modalOverlayOptions,
|
|
23
25
|
modalWidth,
|
|
24
26
|
terminalModalHeight,
|
|
25
|
-
} from "./modal-frame.
|
|
27
|
+
} from "./modal-frame.ts";
|
|
28
|
+
import { getErrorMessage } from "./utils.ts";
|
|
26
29
|
|
|
27
30
|
export interface ProviderPickerRow {
|
|
28
31
|
id: string;
|
|
@@ -212,10 +215,12 @@ export function renderSettingsRows(
|
|
|
212
215
|
rowLines[index] = lines.length;
|
|
213
216
|
const label = `${active ? theme.fg("accent", "→") : " "} ${theme.fg(active ? "accent" : "text", row.label.padEnd(labelWidth))} `;
|
|
214
217
|
const editing = active ? field : undefined;
|
|
218
|
+
const valueWidth = Math.max(10, width - labelWidth - 4);
|
|
215
219
|
lines.push(
|
|
216
220
|
editing
|
|
217
|
-
? `${label}${editing.render(
|
|
218
|
-
:
|
|
221
|
+
? `${label}${editing.render(valueWidth)[0] ?? ""}`
|
|
222
|
+
: // One line per row. A long value (a device name) wraps and breaks the cursor math.
|
|
223
|
+
`${label}${theme.fg(row.tone ?? "success", truncateToWidth(row.value, valueWidth, "…"))}`,
|
|
219
224
|
);
|
|
220
225
|
if (!active || !list) return;
|
|
221
226
|
// ponytail: a window of `max` items around the cursor. Add a scrollbar if lists grow past ~50.
|
|
@@ -229,10 +234,16 @@ export function renderSettingsRows(
|
|
|
229
234
|
list.choices.slice(start, start + max).forEach((choice, offset) => {
|
|
230
235
|
const on = start + offset === list.cursor;
|
|
231
236
|
if (on) listLine = lines.length;
|
|
237
|
+
// One line per choice, like the row value. A long device name must not wrap.
|
|
238
|
+
// Cut plain text before the color, so the cut never splits a style.
|
|
239
|
+
const room = Math.max(10, width - pad.length - 2);
|
|
240
|
+
const name = truncateToWidth(choice.label ?? choice.value, room, "…");
|
|
241
|
+
const left = room - visibleWidth(name);
|
|
242
|
+
const hint = choice.hint && left > 3 ? truncateToWidth(` ${choice.hint}`, left, "…") : "";
|
|
232
243
|
const text =
|
|
233
244
|
choice.value === CUSTOM_CHOICE
|
|
234
245
|
? theme.fg(on ? "accent" : "dim", "type a value…")
|
|
235
|
-
: `${theme.fg(on ? "accent" : "text",
|
|
246
|
+
: `${theme.fg(on ? "accent" : "text", name)}${hint ? theme.fg("muted", hint) : ""}`;
|
|
236
247
|
lines.push(`${pad}${on ? theme.fg("accent", "▸") : " "} ${text}`);
|
|
237
248
|
});
|
|
238
249
|
if (list.choices.length === 0) lines.push(`${pad} ${theme.fg("muted", "no match")}`);
|
|
@@ -286,7 +297,7 @@ export async function showSettingsPicker(
|
|
|
286
297
|
try {
|
|
287
298
|
if ((await opts.onAction(action)) === "close") return close(action);
|
|
288
299
|
} catch (cause) {
|
|
289
|
-
error =
|
|
300
|
+
error = getErrorMessage(cause);
|
|
290
301
|
}
|
|
291
302
|
busy = false;
|
|
292
303
|
rows = opts.rows();
|
package/src/renderers.ts
CHANGED
|
@@ -2,13 +2,13 @@ import { truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works
|
|
|
2
2
|
import { config } from "@xynogen/pix-runtime/config";
|
|
3
3
|
import { prettySection } from "@xynogen/pix-runtime/sections";
|
|
4
4
|
|
|
5
|
-
import { FG_DIM, FG_LNUM, FG_RULE, RST } from "./ansi.
|
|
6
|
-
import { MAX_PREVIEW_LINES } from "./config.
|
|
7
|
-
import { hlBlock } from "./highlight.
|
|
8
|
-
import { dirIcon, fileColor, fileIcon } from "./icons.
|
|
9
|
-
import { lang } from "./lang.
|
|
10
|
-
import type { FgTheme } from "./types.
|
|
11
|
-
import { lnum, normalizeLineEndings, pluralize, rule, termW } from "./utils.
|
|
5
|
+
import { FG_DIM, FG_LNUM, FG_RULE, RST } from "./ansi.ts";
|
|
6
|
+
import { MAX_PREVIEW_LINES } from "./config.ts";
|
|
7
|
+
import { hlBlock } from "./highlight.ts";
|
|
8
|
+
import { dirIcon, fileColor, fileIcon } from "./icons.ts";
|
|
9
|
+
import { lang } from "./lang.ts";
|
|
10
|
+
import type { FgTheme } from "./types.ts";
|
|
11
|
+
import { BODY_PAD, lnum, normalizeLineEndings, pluralize, rule, termW } from "./utils.ts";
|
|
12
12
|
|
|
13
13
|
/** Layout controls for {@link renderFileContent}. */
|
|
14
14
|
export interface RenderFileContentOptions {
|
|
@@ -68,7 +68,9 @@ export async function renderFileContent(
|
|
|
68
68
|
|
|
69
69
|
out.push(rule(tw));
|
|
70
70
|
if (total > maxLines) {
|
|
71
|
-
out.push(
|
|
71
|
+
out.push(
|
|
72
|
+
`${FG_DIM}${BODY_PAD}… ${pluralize(total - maxLines, "more line")} (${total} total)${RST}`,
|
|
73
|
+
);
|
|
72
74
|
}
|
|
73
75
|
return out.join("\n");
|
|
74
76
|
}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* shell-tool — shared registrar + renderer for command-shell tools.
|
|
3
|
+
*
|
|
4
|
+
* pix-bash (`bash`) and pix-powershell (`powershell`) wrap Pi's built-in
|
|
5
|
+
* shell tool definitions (both built on Pi's `createShellToolDefinition`) and
|
|
6
|
+
* render them identically: a compact call line, a status-colored framed body,
|
|
7
|
+
* a live five-line tail while streaming, and an auto-collapsed one-line row.
|
|
8
|
+
* Only the tool name and the command summarizer differ per shell.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { AgentToolUpdateCallback, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
12
|
+
import { type CollapseState, tickCollapse } from "@xynogen/pix-runtime/collapse";
|
|
13
|
+
import { resolveBaseBackground } from "./ansi.ts";
|
|
14
|
+
import { collapsedCommandRow, commandPreview } from "./command-preview.ts";
|
|
15
|
+
import { MAX_PREVIEW_LINES } from "./config.ts";
|
|
16
|
+
import { renderBashOutput } from "./renderers.ts";
|
|
17
|
+
import type { ToolContext } from "./tools/context.ts";
|
|
18
|
+
import type {
|
|
19
|
+
BashParams,
|
|
20
|
+
PiPrettyApi,
|
|
21
|
+
RenderContextLike,
|
|
22
|
+
ThemeLike,
|
|
23
|
+
ToolFactory,
|
|
24
|
+
ToolResultLike,
|
|
25
|
+
} from "./types.ts";
|
|
26
|
+
import {
|
|
27
|
+
BODY_PAD,
|
|
28
|
+
bodyLine,
|
|
29
|
+
dotJoin,
|
|
30
|
+
fillToolBackground,
|
|
31
|
+
formatCollapsedToolRow,
|
|
32
|
+
formatToolCallTitle,
|
|
33
|
+
frameToolResult,
|
|
34
|
+
getErrorMessage,
|
|
35
|
+
getTextContent,
|
|
36
|
+
hideCollapsedToolCall,
|
|
37
|
+
isTextContent,
|
|
38
|
+
moreLines,
|
|
39
|
+
renderToolError,
|
|
40
|
+
ruleFrame,
|
|
41
|
+
setResultDetails,
|
|
42
|
+
termW,
|
|
43
|
+
unframeToolResult,
|
|
44
|
+
} from "./utils.ts";
|
|
45
|
+
import { formatDuration } from "./widget-format.ts";
|
|
46
|
+
|
|
47
|
+
export interface ShellToolOptions {
|
|
48
|
+
/** Tool name to register (and collapse-config key), e.g. `"bash"`, `"powershell"`. */
|
|
49
|
+
name: string;
|
|
50
|
+
/** One-line command summary used by the auto-collapsed row. */
|
|
51
|
+
summarize: (command: string) => string;
|
|
52
|
+
/** Output that implies failure when no explicit exit code is reported. */
|
|
53
|
+
failurePattern?: RegExp;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const EXIT_CODE_RE = /(?:exit code|exited with(?: code)?|exit status)[:\s]*(\d+)/i;
|
|
57
|
+
|
|
58
|
+
/** Drop cursor/erase CSI + OSC sequences (keep SGR colors) so progress-bar
|
|
59
|
+
* control codes don't leak into the TUI as literal escape junk. */
|
|
60
|
+
function stripNonSgrCsi(text: string): string {
|
|
61
|
+
return text
|
|
62
|
+
.replace(/\x1b\][\s\S]*?(?:\x07|\x1b\\)/g, "") // OSC …BEL/ST
|
|
63
|
+
.replace(/\x1b\[[?][0-9;]*[A-Za-z]/g, "") // private-mode CSI (?25l etc.)
|
|
64
|
+
.replace(/\x1b\[[0-9;]*[A-HJKSTfhl]/g, ""); // cursor move / erase CSI
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Progress bars rewrite one line with CR (and often ESC[K). Treat each CR as
|
|
69
|
+
* "overwrite this line", not a newline — otherwise every tick dumps a new row
|
|
70
|
+
* and the card scrolls with dozens of near-identical frames.
|
|
71
|
+
*
|
|
72
|
+
* Split on LF first: `.` does not match CR, so a single `/^.*$/gm` pass would
|
|
73
|
+
* miss CR-only progress streams entirely.
|
|
74
|
+
*/
|
|
75
|
+
export function collapseProgressFrames(text: string): string {
|
|
76
|
+
return text
|
|
77
|
+
.replace(/\r\n/g, "\n")
|
|
78
|
+
.split("\n")
|
|
79
|
+
.map((line) => {
|
|
80
|
+
const cr = line.lastIndexOf("\r");
|
|
81
|
+
return stripNonSgrCsi(cr >= 0 ? line.slice(cr + 1) : line);
|
|
82
|
+
})
|
|
83
|
+
.join("\n");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Canonical shell output normalization: collapse CR progress frames, then
|
|
87
|
+
* squeeze blank runs and trim. */
|
|
88
|
+
export function normalizeShellText(text: string): string {
|
|
89
|
+
return collapseProgressFrames(text)
|
|
90
|
+
.replace(/\n{3,}/g, "\n\n")
|
|
91
|
+
.replace(/^\n+|\n+$/g, "");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Register `options.name` as a pretty-rendered wrapper around a Pi shell tool. */
|
|
95
|
+
export function registerShellTool(
|
|
96
|
+
pi: PiPrettyApi,
|
|
97
|
+
createTool: ToolFactory<BashParams>,
|
|
98
|
+
ctx: ToolContext,
|
|
99
|
+
options: ShellToolOptions,
|
|
100
|
+
): void {
|
|
101
|
+
const { cwd, TextComponent } = ctx;
|
|
102
|
+
const { name, failurePattern } = options;
|
|
103
|
+
const origTool = createTool(cwd);
|
|
104
|
+
|
|
105
|
+
pi.registerTool({
|
|
106
|
+
...origTool,
|
|
107
|
+
name,
|
|
108
|
+
// Full-width framing (rules + bg fill) baked at termW(); the default
|
|
109
|
+
// Box shell pads x by 1 and re-wraps at width-2, splitting every line.
|
|
110
|
+
renderShell: "self",
|
|
111
|
+
|
|
112
|
+
async execute(
|
|
113
|
+
tid: string,
|
|
114
|
+
params: BashParams,
|
|
115
|
+
sig: AbortSignal | undefined,
|
|
116
|
+
upd: AgentToolUpdateCallback<unknown> | undefined,
|
|
117
|
+
toolCtx: ExtensionContext,
|
|
118
|
+
) {
|
|
119
|
+
const startedAt = Date.now();
|
|
120
|
+
const details = (text: string) => {
|
|
121
|
+
const exitMatch = text.match(EXIT_CODE_RE);
|
|
122
|
+
const exitCode = exitMatch ? Number(exitMatch[1]) : failurePattern?.test(text) ? 1 : 0;
|
|
123
|
+
return {
|
|
124
|
+
_type: "bashResult" as const,
|
|
125
|
+
text,
|
|
126
|
+
exitCode,
|
|
127
|
+
command: params.command ?? "",
|
|
128
|
+
durationMs: Date.now() - startedAt,
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
const result = (await origTool.execute(tid, params, sig, upd, toolCtx)) as ToolResultLike;
|
|
134
|
+
setResultDetails(result, details(getTextContent(result)));
|
|
135
|
+
return result;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
const text = getErrorMessage(error);
|
|
138
|
+
if (!EXIT_CODE_RE.test(text)) throw error;
|
|
139
|
+
return {
|
|
140
|
+
content: [{ type: "text" as const, text }],
|
|
141
|
+
details: details(text),
|
|
142
|
+
isError: true,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
renderCall(args: BashParams, theme: ThemeLike, renderCtx: RenderContextLike) {
|
|
148
|
+
resolveBaseBackground(theme);
|
|
149
|
+
const displayCmdRaw = (args.command ?? "").trim();
|
|
150
|
+
const text = renderCtx.lastComponent ?? new TextComponent("", 0, 0);
|
|
151
|
+
const label = formatToolCallTitle(theme, name, renderCtx);
|
|
152
|
+
const collapseState = renderCtx.state as CollapseState;
|
|
153
|
+
if (hideCollapsedToolCall(collapseState, renderCtx.expanded, (value) => text.setText(value)))
|
|
154
|
+
return text;
|
|
155
|
+
const timeout = args.timeout ? ` ${theme.fg("muted", `(${args.timeout}s timeout)`)}` : "";
|
|
156
|
+
return commandPreview(
|
|
157
|
+
label + timeout,
|
|
158
|
+
displayCmdRaw,
|
|
159
|
+
name === "powershell" ? "powershell" : "bash",
|
|
160
|
+
theme,
|
|
161
|
+
renderCtx.state,
|
|
162
|
+
renderCtx.invalidate,
|
|
163
|
+
renderCtx.expanded,
|
|
164
|
+
3,
|
|
165
|
+
);
|
|
166
|
+
},
|
|
167
|
+
|
|
168
|
+
renderResult(
|
|
169
|
+
result: ToolResultLike,
|
|
170
|
+
_opt: unknown,
|
|
171
|
+
theme: ThemeLike,
|
|
172
|
+
renderCtx: RenderContextLike,
|
|
173
|
+
) {
|
|
174
|
+
resolveBaseBackground(theme);
|
|
175
|
+
const text = unframeToolResult(renderCtx.lastComponent ?? new TextComponent("", 0, 0));
|
|
176
|
+
const d = result.details as Record<string, unknown> | undefined;
|
|
177
|
+
const isPartial = (_opt as { isPartial?: boolean } | undefined)?.isPartial === true;
|
|
178
|
+
const completed = () => frameToolResult(text, theme, renderCtx.isError);
|
|
179
|
+
const structuredError = renderCtx.isError && d?._type === "bashResult";
|
|
180
|
+
|
|
181
|
+
if (renderCtx.isError && (!structuredError || isPartial)) {
|
|
182
|
+
text.setText(renderToolError(getTextContent(result) || "Error", theme));
|
|
183
|
+
return isPartial ? text : completed();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Auto-collapse: show summary line after delay
|
|
187
|
+
const cs = renderCtx.state as CollapseState;
|
|
188
|
+
if (!isPartial && tickCollapse(name, cs, renderCtx.invalidate, renderCtx.expanded)) {
|
|
189
|
+
if (d?._type === "bashResult") {
|
|
190
|
+
const normalizedText = normalizeShellText(d.text as string);
|
|
191
|
+
const lc = normalizedText ? normalizedText.split("\n").length : 0;
|
|
192
|
+
const durationMs = Number(d.durationMs ?? 0);
|
|
193
|
+
const exitCode = d.exitCode as number | null;
|
|
194
|
+
const status = exitCode === null ? "warning" : exitCode === 0 ? "success" : "error";
|
|
195
|
+
const meta = dotJoin([
|
|
196
|
+
exitCode !== null && exitCode !== 0 && `exit ${exitCode}`,
|
|
197
|
+
lc > 0 && `${lc} ${lc === 1 ? "line" : "lines"}`,
|
|
198
|
+
durationMs > 0 && formatDuration(durationMs, "bash"),
|
|
199
|
+
]);
|
|
200
|
+
return collapsedCommandRow(
|
|
201
|
+
formatCollapsedToolRow(theme, name, "", meta, status).trimEnd(),
|
|
202
|
+
String(d.command ?? ""),
|
|
203
|
+
theme,
|
|
204
|
+
);
|
|
205
|
+
} else {
|
|
206
|
+
text.setText(fillToolBackground(`${BODY_PAD}${theme.fg("muted", "done")}`));
|
|
207
|
+
}
|
|
208
|
+
return text;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (renderCtx.isError) {
|
|
212
|
+
text.setText(renderToolError(getTextContent(result) || "Error", theme));
|
|
213
|
+
return completed();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (d?._type === "bashResult") {
|
|
217
|
+
const normalizedText = normalizeShellText(d.text as string);
|
|
218
|
+
const { summary } = renderBashOutput(normalizedText, d.exitCode as number | null, theme);
|
|
219
|
+
const lines = normalizedText ? normalizedText.split("\n") : [];
|
|
220
|
+
const lineCount = lines.length;
|
|
221
|
+
|
|
222
|
+
if (!normalizedText) {
|
|
223
|
+
text.setText(fillToolBackground(summary));
|
|
224
|
+
return isPartial ? text : completed();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const maxShow = renderCtx.expanded ? lineCount : MAX_PREVIEW_LINES;
|
|
228
|
+
const show = lines.slice(0, maxShow);
|
|
229
|
+
const footer = lineCount > maxShow ? [moreLines(lineCount - maxShow, theme)] : [];
|
|
230
|
+
// Every result (including single-line) is framed; the rules follow exit
|
|
231
|
+
// status: green ok, red failure. The `✓ exit N` header is dropped — the
|
|
232
|
+
// collapsed row already carries status.
|
|
233
|
+
const exitCode = d.exitCode as number | null;
|
|
234
|
+
const statusKey = exitCode === null || exitCode === 0 ? "success" : "error";
|
|
235
|
+
const paint = (s: string) => theme.fg(statusKey, s);
|
|
236
|
+
const body = show.map((line) => bodyLine(line, theme));
|
|
237
|
+
const out = isPartial ? [...body, ...footer] : ruleFrame(body, footer, termW(), paint);
|
|
238
|
+
text.setText(fillToolBackground(out.join("\n")));
|
|
239
|
+
return text;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const fallback = result.content?.[0];
|
|
243
|
+
const fallbackText = fallback && isTextContent(fallback) ? fallback.text : "done";
|
|
244
|
+
if (isPartial) {
|
|
245
|
+
const liveLines = normalizeShellText(String(fallbackText)).split("\n").slice(-5);
|
|
246
|
+
text.setText(
|
|
247
|
+
fillToolBackground(
|
|
248
|
+
liveLines.map((line) => bodyLine(line, theme, (l) => theme.fg("dim", l))).join("\n"),
|
|
249
|
+
),
|
|
250
|
+
);
|
|
251
|
+
return text;
|
|
252
|
+
}
|
|
253
|
+
text.setText(
|
|
254
|
+
fillToolBackground(`${BODY_PAD}${theme.fg("dim", String(fallbackText).slice(0, 120))}`),
|
|
255
|
+
);
|
|
256
|
+
return completed();
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
}
|
package/src/test-utils.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
// rebuilt these same mocks (MockTextComponent, capture-pi, theme, render ctx)
|
|
3
3
|
// by hand; this collapses them to one import. Pure and Pi-host-agnostic.
|
|
4
4
|
|
|
5
|
-
import type { CursorStore, FffState } from "./fff.
|
|
6
|
-
import type { ToolContext } from "./tools/context.
|
|
7
|
-
import type { PiPrettyApi, RenderContextLike, TextComponentCtor, ThemeLike } from "./types.
|
|
5
|
+
import type { CursorStore, FffState } from "./fff.ts";
|
|
6
|
+
import type { ToolContext } from "./tools/context.ts";
|
|
7
|
+
import type { PiPrettyApi, RenderContextLike, TextComponentCtor, ThemeLike } from "./types.ts";
|
|
8
8
|
|
|
9
9
|
/** In-memory TextComponent: stores text, splits on render. */
|
|
10
10
|
export class MockTextComponent {
|
|
@@ -93,6 +93,9 @@ export function makeRenderCtx(
|
|
|
93
93
|
return {
|
|
94
94
|
expanded: false,
|
|
95
95
|
isError: false,
|
|
96
|
+
executionStarted: true,
|
|
97
|
+
argsComplete: true,
|
|
98
|
+
isPartial: true,
|
|
96
99
|
invalidate: () => {},
|
|
97
100
|
state: {},
|
|
98
101
|
...overrides,
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tool-status.ts — one wording + surface for pix-runtime binary downloads.
|
|
3
|
+
*
|
|
4
|
+
* `ensureTool()` (pix-runtime/binaries) reports progress through `onStatus`;
|
|
5
|
+
* runtime cannot import UI. Consumers pass this adapter so every package shows
|
|
6
|
+
* downloads the same way: a footer status while downloading (live activity),
|
|
7
|
+
* then one transient line when installed or failed (AGENTS.md UI surfaces).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { icon } from "./icon-catalog.ts";
|
|
11
|
+
import { showTransientMessage, type TransientErrorUI } from "./transient-error.ts";
|
|
12
|
+
|
|
13
|
+
/** Structural mirror of pix-runtime's ToolStatus (kept local: pretty depends on runtime, not its types). */
|
|
14
|
+
export type ToolStatusEvent =
|
|
15
|
+
| { kind: "downloading"; name: string; version: string; url: string; size?: string }
|
|
16
|
+
| { kind: "installed"; name: string; path: string; version: string; verified: boolean }
|
|
17
|
+
| { kind: "failed"; name: string; error: string; hint: string };
|
|
18
|
+
|
|
19
|
+
export type ToolStatusUI = TransientErrorUI & {
|
|
20
|
+
setStatus?(key: string, text: string | undefined): void;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const STATUS_KEY = "pix-binaries";
|
|
24
|
+
|
|
25
|
+
/** Human wording for one status event. */
|
|
26
|
+
export function formatToolStatus(s: ToolStatusEvent): string {
|
|
27
|
+
if (s.kind === "downloading") {
|
|
28
|
+
const size = s.size ? ` (${s.size})` : "";
|
|
29
|
+
return `downloading ${s.name} ${s.version}${size} from ${s.url}`;
|
|
30
|
+
}
|
|
31
|
+
if (s.kind === "installed") {
|
|
32
|
+
return `installed ${s.name} ${s.version} → ${s.path}${s.verified ? " (sha256 verified)" : ""}`;
|
|
33
|
+
}
|
|
34
|
+
return `${s.name} unavailable: ${s.error}${s.hint ? ` — install: ${s.hint}` : ""}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Structural mirror of pix-runtime's BinaryMissingError (no runtime import needed). */
|
|
38
|
+
export interface BinaryMissingLike {
|
|
39
|
+
name: "BinaryMissingError";
|
|
40
|
+
tool: string;
|
|
41
|
+
message: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function isBinaryMissing(err: unknown): err is BinaryMissingLike {
|
|
45
|
+
return (
|
|
46
|
+
err instanceof Error &&
|
|
47
|
+
err.name === "BinaryMissingError" &&
|
|
48
|
+
typeof (err as { tool?: unknown }).tool === "string"
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const warned = new Set<string>();
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Background features (footer branch, welcome, recency) call this when a
|
|
56
|
+
* binary is missing: one transient warning per tool per process, pointing at
|
|
57
|
+
* the /pix Binaries tab, instead of failing silently or spamming. Returns true
|
|
58
|
+
* when `err` was a missing-binary error (handled), false otherwise.
|
|
59
|
+
*/
|
|
60
|
+
export function warnBinaryMissing(ui: TransientErrorUI | undefined, err: unknown): boolean {
|
|
61
|
+
if (!isBinaryMissing(err)) return false;
|
|
62
|
+
if (!ui || warned.has(err.tool)) return true;
|
|
63
|
+
warned.add(err.tool);
|
|
64
|
+
showTransientMessage(ui, `${err.message} · set a path in /pix → Binaries`, "warning");
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Test seam: forget which tools already warned. */
|
|
69
|
+
export function resetBinaryWarnings(): void {
|
|
70
|
+
warned.clear();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Build an `onStatus` callback that routes events to the standard surfaces. */
|
|
74
|
+
export function reportToolStatus(ui: ToolStatusUI | undefined): (s: ToolStatusEvent) => void {
|
|
75
|
+
return (s) => {
|
|
76
|
+
if (!ui) return;
|
|
77
|
+
if (s.kind === "downloading") {
|
|
78
|
+
ui.setStatus?.(STATUS_KEY, `${icon("status.running")} ${s.name} ${s.version}`);
|
|
79
|
+
showTransientMessage(ui, formatToolStatus(s), "info");
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
ui.setStatus?.(STATUS_KEY, undefined);
|
|
83
|
+
showTransientMessage(ui, formatToolStatus(s), s.kind === "installed" ? "info" : "warning");
|
|
84
|
+
};
|
|
85
|
+
}
|
package/src/tools/context.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { CursorStore, FffState } from "../fff.
|
|
2
|
-
import type { TextComponentCtor } from "../types.
|
|
1
|
+
import type { CursorStore, FffState } from "../fff.ts";
|
|
2
|
+
import type { TextComponentCtor } from "../types.ts";
|
|
3
3
|
|
|
4
4
|
// ── Shared context passed to each tool registrar ───────────────────────
|
|
5
5
|
|
package/src/types.ts
CHANGED
|
@@ -65,6 +65,10 @@ export type RenderContextLike<
|
|
|
65
65
|
state: TState;
|
|
66
66
|
expanded: boolean;
|
|
67
67
|
isError: boolean;
|
|
68
|
+
/** Pi sets this to false when the tool returns its final result. */
|
|
69
|
+
isPartial?: boolean;
|
|
70
|
+
executionStarted?: boolean;
|
|
71
|
+
argsComplete?: boolean;
|
|
68
72
|
invalidate: () => void;
|
|
69
73
|
/** Stable id for this tool execution — used to key resize invalidators. */
|
|
70
74
|
toolCallId?: string;
|