@xynogen/pix-pretty 2.0.1 → 2.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/package.json +5 -4
- package/src/chips.ts +5 -8
- package/src/index.ts +1 -3
- package/src/renderers.ts +15 -16
- package/src/shell-tool.ts +6 -22
- package/src/tool-card.ts +111 -0
- package/src/tui.ts +52 -0
- package/src/types.ts +23 -22
- package/src/utils.ts +60 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xynogen/pix-pretty",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Enhanced tool output rendering with syntax highlighting, file icons, tree views, diff rendering, and FFF search",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -34,17 +34,18 @@
|
|
|
34
34
|
"./shell-tool": "./src/shell-tool.ts",
|
|
35
35
|
"./command-preview": "./src/command-preview.ts",
|
|
36
36
|
"./tool-status": "./src/tool-status.ts",
|
|
37
|
+
"./tool-card": "./src/tool-card.ts",
|
|
37
38
|
"./test-utils": "./src/test-utils.ts",
|
|
38
39
|
"./transient-error": "./src/transient-error.ts",
|
|
39
40
|
"./chips": "./src/chips.ts",
|
|
40
|
-
"./grammar-consent": "./src/grammar-consent.ts"
|
|
41
|
+
"./grammar-consent": "./src/grammar-consent.ts",
|
|
42
|
+
"./tui": "./src/tui.ts"
|
|
41
43
|
},
|
|
42
44
|
"scripts": {
|
|
43
45
|
"test": "bun test"
|
|
44
46
|
},
|
|
45
47
|
"files": [
|
|
46
48
|
"src",
|
|
47
|
-
"!src/**/*.test.*",
|
|
48
49
|
"README.md",
|
|
49
50
|
"LICENSE"
|
|
50
51
|
],
|
|
@@ -72,7 +73,7 @@
|
|
|
72
73
|
"access": "public"
|
|
73
74
|
},
|
|
74
75
|
"dependencies": {
|
|
75
|
-
"@xynogen/pix-runtime": "^0.
|
|
76
|
+
"@xynogen/pix-runtime": "^0.20.0",
|
|
76
77
|
"chalk": "^4.1.2",
|
|
77
78
|
"cli-highlight": "^2.1.11",
|
|
78
79
|
"@ff-labs/fff-node": "^0.10.6",
|
package/src/chips.ts
CHANGED
|
@@ -283,14 +283,11 @@ export function installChips(editor: CustomEditor): void {
|
|
|
283
283
|
|
|
284
284
|
/** Activate chips: sent-message transformer + editor patch. pix-display calls this. */
|
|
285
285
|
export function registerChips(pi: ExtensionAPI): void {
|
|
286
|
-
//
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
};
|
|
292
|
-
markdownPi.registerMarkdownTransformer?.((markdown, { messageType }) =>
|
|
293
|
-
messageType === "user" ? renderHistoryChips(markdown) : markdown,
|
|
286
|
+
// `messageTypes` skips Pi's assistant stream; `?.` and the `user` check cover older Pi runtimes (peer `*`).
|
|
287
|
+
pi.registerMarkdownTransformer?.(
|
|
288
|
+
(markdown, { messageType }) =>
|
|
289
|
+
messageType === "user" ? renderHistoryChips(markdown) : markdown,
|
|
290
|
+
{ messageTypes: ["user"] },
|
|
294
291
|
);
|
|
295
292
|
|
|
296
293
|
pi.on("session_start", (_event, ctx) => {
|
package/src/index.ts
CHANGED
|
@@ -16,7 +16,6 @@ import { clearHighlightCache } from "./highlight.ts";
|
|
|
16
16
|
import { initIconMode } from "./icon-persist.ts";
|
|
17
17
|
import { installOrderedWidgets } from "./ordered-widgets.ts";
|
|
18
18
|
import { installOverlayImages } from "./overlay-images.ts";
|
|
19
|
-
import type { PiPrettyApi } from "./types.ts";
|
|
20
19
|
|
|
21
20
|
export default function piPrettyExtension(pi: ExtensionAPI): void {
|
|
22
21
|
installOverlayImages();
|
|
@@ -30,7 +29,6 @@ export default function piPrettyExtension(pi: ExtensionAPI): void {
|
|
|
30
29
|
restore = undefined;
|
|
31
30
|
});
|
|
32
31
|
});
|
|
33
|
-
const prettyPi = pi as unknown as PiPrettyApi;
|
|
34
32
|
clearHighlightCache();
|
|
35
33
|
|
|
36
34
|
// ── Icon mode ───────────────────────────────────────────
|
|
@@ -41,5 +39,5 @@ export default function piPrettyExtension(pi: ExtensionAPI): void {
|
|
|
41
39
|
// ── FFF slash commands ──────────────────────────────────────────────
|
|
42
40
|
// fffState is a module-level singleton shared with pix-grep/pix-find.
|
|
43
41
|
// Commands become available once pix-grep initialises the finder.
|
|
44
|
-
registerFffCommands(
|
|
42
|
+
registerFffCommands(pi, fffState);
|
|
45
43
|
}
|
package/src/renderers.ts
CHANGED
|
@@ -49,6 +49,9 @@ export async function renderFileContent(
|
|
|
49
49
|
const show = lines.slice(0, maxLines);
|
|
50
50
|
const lg = lang(filePath);
|
|
51
51
|
const hl = await hlBlock(show.join("\n"), lg, theme);
|
|
52
|
+
// A cut preview fades its last code rows. Only the code fades; the gutter stays solid.
|
|
53
|
+
const codeRows = show.map((line, i) => hl[i] ?? line);
|
|
54
|
+
const body = theme && total > maxLines ? fadeTail(codeRows, theme) : codeRows;
|
|
52
55
|
|
|
53
56
|
const tw = options.width ?? termW();
|
|
54
57
|
const startLine = offset;
|
|
@@ -60,9 +63,9 @@ export async function renderFileContent(
|
|
|
60
63
|
const out: string[] = [];
|
|
61
64
|
out.push(rule(tw));
|
|
62
65
|
|
|
63
|
-
for (let i = 0; i <
|
|
66
|
+
for (let i = 0; i < body.length; i++) {
|
|
64
67
|
const ln = startLine + i;
|
|
65
|
-
const code =
|
|
68
|
+
const code = body[i] ?? "";
|
|
66
69
|
if (options.wrapLongLines && visibleWidth(code) > cw) {
|
|
67
70
|
// Expanded view: no tail is hidden. First row carries the line number,
|
|
68
71
|
// continuation rows indent under the gutter so the code column aligns.
|
|
@@ -78,8 +81,6 @@ export async function renderFileContent(
|
|
|
78
81
|
out.push(`${lnum(ln, nw)} ${FG_RULE}│${RST} ${display}${RST}`);
|
|
79
82
|
}
|
|
80
83
|
|
|
81
|
-
// A cut preview fades its last code rows (rule row 0 stays as is).
|
|
82
|
-
if (theme && total > maxLines) out.splice(1, Infinity, ...fadeTail(out.slice(1), theme));
|
|
83
84
|
out.push(rule(tw));
|
|
84
85
|
if (total > maxLines) {
|
|
85
86
|
const more = `${BODY_PAD}… ${pluralize(total - maxLines, "more line")} (${total} total) `;
|
|
@@ -151,21 +152,19 @@ function renderLsTree(
|
|
|
151
152
|
const max = ctx?.expanded ? total : MAX_PREVIEW_LINES;
|
|
152
153
|
const show = lines.slice(0, max);
|
|
153
154
|
|
|
154
|
-
|
|
155
|
-
const entry =
|
|
156
|
-
const isLast = i === show.length - 1 && total <= max;
|
|
157
|
-
const prefix = isLast ? "└── " : "├── ";
|
|
158
|
-
const connector = `${FG_RULE}${prefix}${RST}`;
|
|
159
|
-
|
|
155
|
+
const names = show.map((line) => {
|
|
156
|
+
const entry = line.trim();
|
|
160
157
|
const isDir = entry.endsWith("/");
|
|
161
158
|
const name = isDir ? entry.slice(0, -1) : entry;
|
|
162
159
|
const icon = isDir ? dirIcon(theme) : fileIcon(name, theme);
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
160
|
+
return `${icon}${entryColor(isDir, name, theme)}`;
|
|
161
|
+
});
|
|
162
|
+
// A cut preview fades its last entries. Only the names fade; the tree connectors stay solid.
|
|
163
|
+
const body = theme && total > max ? fadeTail(names, theme) : names;
|
|
164
|
+
body.forEach((name, i) => {
|
|
165
|
+
const isLast = i === show.length - 1 && total <= max;
|
|
166
|
+
out.push(`${FG_RULE}${isLast ? "└── " : "├── "}${RST}${name}`);
|
|
167
|
+
});
|
|
169
168
|
if (total > max) {
|
|
170
169
|
out.push(
|
|
171
170
|
`${FG_RULE}└── ${RST}${FG_DIM}… ${pluralize(total - max, "more entry", "more entries")} ${RST}${theme ? expandHint(theme, ctx) : ""}`,
|
package/src/shell-tool.ts
CHANGED
|
@@ -12,9 +12,7 @@ import type { AgentToolUpdateCallback, ExtensionContext } from "@earendil-works/
|
|
|
12
12
|
import { type CollapseState, tickCollapse } from "@xynogen/pix-runtime/collapse";
|
|
13
13
|
import { resolveBaseBackground } from "./ansi.ts";
|
|
14
14
|
import { collapsedCommandRow, commandPreview } from "./command-preview.ts";
|
|
15
|
-
import { MAX_PREVIEW_LINES } from "./config.ts";
|
|
16
15
|
import { renderBashOutput } from "./renderers.ts";
|
|
17
|
-
import { fadeRowsOf } from "./text.ts";
|
|
18
16
|
import type { ToolContext } from "./tools/context.ts";
|
|
19
17
|
import type {
|
|
20
18
|
BashParams,
|
|
@@ -28,21 +26,18 @@ import {
|
|
|
28
26
|
BODY_PAD,
|
|
29
27
|
bodyLine,
|
|
30
28
|
dotJoin,
|
|
31
|
-
fadeRulePaint,
|
|
32
29
|
fillToolBackground,
|
|
33
30
|
frameToolResult,
|
|
34
31
|
getErrorMessage,
|
|
35
32
|
getTextContent,
|
|
36
33
|
hideCollapsedToolCall,
|
|
37
34
|
isTextContent,
|
|
38
|
-
|
|
35
|
+
outputBodyRows,
|
|
39
36
|
renderRow,
|
|
40
37
|
renderTitle,
|
|
41
38
|
renderToolError,
|
|
42
|
-
|
|
39
|
+
reuseText,
|
|
43
40
|
setResultDetails,
|
|
44
|
-
termW,
|
|
45
|
-
unframeToolResult,
|
|
46
41
|
} from "./utils.ts";
|
|
47
42
|
import { formatDuration } from "./widget-format.ts";
|
|
48
43
|
|
|
@@ -149,7 +144,7 @@ export function registerShellTool(
|
|
|
149
144
|
renderCall(args: BashParams, theme: ThemeLike, renderCtx: RenderContextLike) {
|
|
150
145
|
resolveBaseBackground(theme);
|
|
151
146
|
const displayCmdRaw = (args.command ?? "").trim();
|
|
152
|
-
const text = renderCtx.lastComponent
|
|
147
|
+
const text = reuseText(renderCtx.lastComponent, TextComponent);
|
|
153
148
|
const label = renderTitle(theme, name, renderCtx);
|
|
154
149
|
const collapseState = renderCtx.state as CollapseState;
|
|
155
150
|
if (hideCollapsedToolCall(collapseState, renderCtx.expanded, (value) => text.setText(value)))
|
|
@@ -175,7 +170,7 @@ export function registerShellTool(
|
|
|
175
170
|
renderCtx: RenderContextLike,
|
|
176
171
|
) {
|
|
177
172
|
resolveBaseBackground(theme);
|
|
178
|
-
const text =
|
|
173
|
+
const text = reuseText(renderCtx.lastComponent, TextComponent);
|
|
179
174
|
const d = result.details as Record<string, unknown> | undefined;
|
|
180
175
|
const isPartial = (_opt as { isPartial?: boolean } | undefined)?.isPartial === true;
|
|
181
176
|
const completed = () => frameToolResult(text, theme, renderCtx.isError);
|
|
@@ -221,29 +216,18 @@ export function registerShellTool(
|
|
|
221
216
|
const normalizedText = normalizeShellText(d.text as string);
|
|
222
217
|
const { summary } = renderBashOutput(normalizedText, d.exitCode as number | null, theme);
|
|
223
218
|
const lines = normalizedText ? normalizedText.split("\n") : [];
|
|
224
|
-
const lineCount = lines.length;
|
|
225
219
|
|
|
226
220
|
if (!normalizedText) {
|
|
227
221
|
text.setText(fillToolBackground(summary));
|
|
228
222
|
return isPartial ? text : completed();
|
|
229
223
|
}
|
|
230
224
|
|
|
231
|
-
const maxShow = renderCtx.expanded ? lineCount : MAX_PREVIEW_LINES;
|
|
232
|
-
const footer =
|
|
233
|
-
lineCount > maxShow ? [moreLines(lineCount - maxShow, theme, renderCtx)] : [];
|
|
234
225
|
// Every result (including single-line) is framed; the rules follow exit
|
|
235
226
|
// status: green ok, red failure. The `✓ exit N` header is dropped — the
|
|
236
227
|
// collapsed row already carries status.
|
|
237
228
|
const exitCode = d.exitCode as number | null;
|
|
238
|
-
const
|
|
239
|
-
const
|
|
240
|
-
// Fade the last preview rows instead of a hard cut before the "… N more" row.
|
|
241
|
-
const body = fadeRowsOf(
|
|
242
|
-
lines.slice(0, maxShow + 1).map((line) => bodyLine(line, theme)),
|
|
243
|
-
theme,
|
|
244
|
-
maxShow,
|
|
245
|
-
);
|
|
246
|
-
const out = isPartial ? [...body, ...footer] : ruleFrame(body, footer, termW(), paint);
|
|
229
|
+
const status = exitCode === null || exitCode === 0 ? "success" : "error";
|
|
230
|
+
const out = outputBodyRows(lines, theme, renderCtx, status, isPartial);
|
|
247
231
|
text.setText(fillToolBackground(out.join("\n")));
|
|
248
232
|
return text;
|
|
249
233
|
}
|
package/src/tool-card.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tool-card — the call row and result card every self-rendered tool shares.
|
|
3
|
+
*
|
|
4
|
+
* One place owns the order: a bare or partial error first, then the auto-collapsed
|
|
5
|
+
* row, then a structured error, then the tool's own body inside the result frame.
|
|
6
|
+
* A tool supplies only what differs: which details are its own, the collapsed row
|
|
7
|
+
* text, and the body.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { type CollapseState, tickCollapse } from "@xynogen/pix-runtime/collapse";
|
|
11
|
+
import { resolveBaseBackground } from "./ansi.ts";
|
|
12
|
+
import type {
|
|
13
|
+
RenderContextLike,
|
|
14
|
+
TextComponentCtor,
|
|
15
|
+
TextComponentLike,
|
|
16
|
+
ThemeLike,
|
|
17
|
+
ToolResultLike,
|
|
18
|
+
} from "./types.ts";
|
|
19
|
+
import {
|
|
20
|
+
frameToolResult,
|
|
21
|
+
getTextContent,
|
|
22
|
+
hideCollapsedToolCall,
|
|
23
|
+
renderCollapsed,
|
|
24
|
+
renderToolError,
|
|
25
|
+
reuseText,
|
|
26
|
+
} from "./utils.ts";
|
|
27
|
+
|
|
28
|
+
type RenderState = Record<string, string | undefined>;
|
|
29
|
+
|
|
30
|
+
export interface ToolCardBody<D, S extends RenderState> {
|
|
31
|
+
d: D | undefined;
|
|
32
|
+
result: ToolResultLike<D>;
|
|
33
|
+
text: TextComponentLike;
|
|
34
|
+
theme: ThemeLike;
|
|
35
|
+
ctx: RenderContextLike<S>;
|
|
36
|
+
isPartial: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ToolCardSpec<D, S extends RenderState> {
|
|
40
|
+
/** Tool name: the collapse key and the collapsed row title. */
|
|
41
|
+
name: string;
|
|
42
|
+
/** True for details this tool produced. An error that carries them still renders as a card. */
|
|
43
|
+
owns(d: D | undefined): boolean;
|
|
44
|
+
/** Collapsed row target and success summary. A failed call shows "failed" instead. */
|
|
45
|
+
collapsed(d: D | undefined): { target: string; summary: string };
|
|
46
|
+
/** Set the expanded body. Return "framed" when the body drew its own frame. */
|
|
47
|
+
body(input: ToolCardBody<D, S>): "framed" | undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** `renderResult` for a self-rendered tool card. */
|
|
51
|
+
export function toolCardRenderer<D = Record<string, unknown>, S extends RenderState = RenderState>(
|
|
52
|
+
TextComponent: TextComponentCtor,
|
|
53
|
+
spec: ToolCardSpec<D, S>,
|
|
54
|
+
) {
|
|
55
|
+
return (
|
|
56
|
+
result: ToolResultLike<D>,
|
|
57
|
+
options: { isPartial?: boolean } | undefined,
|
|
58
|
+
theme: ThemeLike,
|
|
59
|
+
ctx: RenderContextLike<S>,
|
|
60
|
+
) => {
|
|
61
|
+
resolveBaseBackground(theme);
|
|
62
|
+
const text = reuseText(ctx.lastComponent, TextComponent);
|
|
63
|
+
const d = result.details ?? undefined;
|
|
64
|
+
const isPartial = options?.isPartial === true;
|
|
65
|
+
const completed = () => frameToolResult(text, theme, ctx.isError);
|
|
66
|
+
const showError = () => text.setText(renderToolError(getTextContent(result) || "Error", theme));
|
|
67
|
+
|
|
68
|
+
if (ctx.isError && (!spec.owns(d) || isPartial)) {
|
|
69
|
+
showError();
|
|
70
|
+
return isPartial ? text : completed();
|
|
71
|
+
}
|
|
72
|
+
if (
|
|
73
|
+
!isPartial &&
|
|
74
|
+
tickCollapse(spec.name, ctx.state as CollapseState, ctx.invalidate, ctx.expanded)
|
|
75
|
+
) {
|
|
76
|
+
const { target, summary } = spec.collapsed(d);
|
|
77
|
+
const failed = ctx.isError;
|
|
78
|
+
text.setText(
|
|
79
|
+
renderCollapsed(
|
|
80
|
+
theme,
|
|
81
|
+
spec.name,
|
|
82
|
+
target,
|
|
83
|
+
failed ? "failed" : summary,
|
|
84
|
+
failed ? "error" : "success",
|
|
85
|
+
),
|
|
86
|
+
);
|
|
87
|
+
return text;
|
|
88
|
+
}
|
|
89
|
+
if (ctx.isError) {
|
|
90
|
+
showError();
|
|
91
|
+
return completed();
|
|
92
|
+
}
|
|
93
|
+
const framed = spec.body({ d, result, text, theme, ctx, isPartial });
|
|
94
|
+
return framed === "framed" || isPartial ? text : completed();
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** `renderCall` for a self-rendered tool: hidden once its result collapsed, else `line`. */
|
|
99
|
+
export function toolCallRenderer<A, S extends RenderState = RenderState>(
|
|
100
|
+
TextComponent: TextComponentCtor,
|
|
101
|
+
line: (args: A, theme: ThemeLike, ctx: RenderContextLike<S>) => string,
|
|
102
|
+
) {
|
|
103
|
+
return (args: A, theme: ThemeLike, ctx: RenderContextLike<S>) => {
|
|
104
|
+
resolveBaseBackground(theme);
|
|
105
|
+
const text = reuseText(ctx.lastComponent, TextComponent);
|
|
106
|
+
if (hideCollapsedToolCall(ctx.state as CollapseState, ctx.expanded, (v) => text.setText(v)))
|
|
107
|
+
return text;
|
|
108
|
+
text.setText(line(args, theme, ctx));
|
|
109
|
+
return text;
|
|
110
|
+
};
|
|
111
|
+
}
|
package/src/tui.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tui.ts — the terminal UI surface for feature packages: pi-tui components and
|
|
3
|
+
* helpers plus Pi's UI types. Named re-exports only, so the host's classes keep
|
|
4
|
+
* their identity. Non-UI Pi API lives in `@xynogen/pix-runtime/host`.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export type {
|
|
8
|
+
ThemeColor,
|
|
9
|
+
ToolRenderers,
|
|
10
|
+
ToolRenderResultOptions,
|
|
11
|
+
} from "@earendil-works/pi-coding-agent";
|
|
12
|
+
export {
|
|
13
|
+
AssistantMessageComponent,
|
|
14
|
+
CustomEditor,
|
|
15
|
+
getMarkdownTheme,
|
|
16
|
+
Theme,
|
|
17
|
+
} from "@earendil-works/pi-coding-agent";
|
|
18
|
+
export type {
|
|
19
|
+
Component,
|
|
20
|
+
EditorTheme,
|
|
21
|
+
KeybindingsManager,
|
|
22
|
+
KeyId,
|
|
23
|
+
MarkdownTheme,
|
|
24
|
+
SelectItem,
|
|
25
|
+
TextStyle,
|
|
26
|
+
TUI,
|
|
27
|
+
} from "@earendil-works/pi-tui";
|
|
28
|
+
export {
|
|
29
|
+
Box,
|
|
30
|
+
Container,
|
|
31
|
+
decodeKittyPrintable,
|
|
32
|
+
Editor,
|
|
33
|
+
fuzzyFilter,
|
|
34
|
+
Input,
|
|
35
|
+
isKeyRelease,
|
|
36
|
+
isKeyRepeat,
|
|
37
|
+
isKittyProtocolActive,
|
|
38
|
+
Key,
|
|
39
|
+
Markdown,
|
|
40
|
+
matchesKey,
|
|
41
|
+
mixColors,
|
|
42
|
+
parseColor,
|
|
43
|
+
parseKey,
|
|
44
|
+
SelectList,
|
|
45
|
+
Spacer,
|
|
46
|
+
sliceByColumn,
|
|
47
|
+
stripTerminalSequences,
|
|
48
|
+
Text,
|
|
49
|
+
truncateToWidth,
|
|
50
|
+
visibleWidth,
|
|
51
|
+
wrapTextWithAnsi,
|
|
52
|
+
} from "@earendil-works/pi-tui";
|
package/src/types.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
ReadToolInput,
|
|
13
13
|
WriteToolInput,
|
|
14
14
|
} from "@earendil-works/pi-coding-agent";
|
|
15
|
+
import type { Component } from "@earendil-works/pi-tui";
|
|
15
16
|
import type { FileFinder } from "@ff-labs/fff-node";
|
|
16
17
|
|
|
17
18
|
// We keep the original shiki-shaped types as plain string aliases so the
|
|
@@ -25,16 +26,18 @@ export type BundledTheme = string;
|
|
|
25
26
|
// ---------------------------------------------------------------------------
|
|
26
27
|
|
|
27
28
|
export type BgTheme = {
|
|
28
|
-
|
|
29
|
+
// Method syntax: Pi's Theme takes narrower ThemeBg keys, and method params are bivariant.
|
|
30
|
+
getBgAnsi?(key: string): string;
|
|
29
31
|
// Semantic background wrapper exposed by Pi's theme (e.g. modal fills). Optional
|
|
30
32
|
// so mock/partial themes remain assignable; real runtime themes provide it.
|
|
31
|
-
bg
|
|
33
|
+
bg?(key: string, text: string): string;
|
|
32
34
|
};
|
|
33
35
|
|
|
34
36
|
export type FgTheme = {
|
|
35
|
-
|
|
37
|
+
// Method syntax: Pi's Theme takes a narrower ThemeColor key, and method params are bivariant.
|
|
38
|
+
fg(key: string, text: string): string;
|
|
36
39
|
// Optional raw-ANSI accessor exposed by Pi's theme for semantic foregrounds.
|
|
37
|
-
getFgAnsi
|
|
40
|
+
getFgAnsi?(key: string): string;
|
|
38
41
|
};
|
|
39
42
|
|
|
40
43
|
export type ImageProtocol = "iterm2" | "kitty" | "none";
|
|
@@ -47,11 +50,9 @@ export type ToolContent = TextContent | ImageContent;
|
|
|
47
50
|
|
|
48
51
|
export type ToolResultLike<TDetails = unknown> = AgentToolResult<TDetails | undefined>;
|
|
49
52
|
|
|
50
|
-
export type TextComponentLike = {
|
|
53
|
+
export type TextComponentLike = Component & {
|
|
51
54
|
setText(value: string): void;
|
|
52
55
|
getText?: () => string;
|
|
53
|
-
render?: (width: number) => string[];
|
|
54
|
-
invalidate?: () => void;
|
|
55
56
|
};
|
|
56
57
|
|
|
57
58
|
export type TextComponentCtor = new (text?: string, x?: number, y?: number) => TextComponentLike;
|
|
@@ -61,7 +62,7 @@ export type ThemeLike = BgTheme & FgTheme & { bold: (text: string) => string };
|
|
|
61
62
|
export type RenderContextLike<
|
|
62
63
|
TState extends Record<string, string | undefined> = Record<string, string | undefined>,
|
|
63
64
|
> = {
|
|
64
|
-
lastComponent?:
|
|
65
|
+
lastComponent?: Component;
|
|
65
66
|
state: TState;
|
|
66
67
|
expanded: boolean;
|
|
67
68
|
isError: boolean;
|
|
@@ -82,20 +83,19 @@ type SessionContextLike = ExtensionContext;
|
|
|
82
83
|
|
|
83
84
|
export type CommandContextLike = ExtensionCommandContext;
|
|
84
85
|
|
|
85
|
-
type ToolExecutor<TParams, TDetails = unknown> = (
|
|
86
|
-
toolCallId: string,
|
|
87
|
-
params: TParams,
|
|
88
|
-
signal?: AbortSignal,
|
|
89
|
-
onUpdate?: AgentToolUpdateCallback<TDetails | undefined>,
|
|
90
|
-
ctx?: ExtensionContext,
|
|
91
|
-
) => Promise<ToolResultLike<TDetails>>;
|
|
92
|
-
|
|
93
86
|
export type ToolFactory<TParams, TDetails = unknown> = (cwd: string) => {
|
|
94
87
|
name?: string;
|
|
95
88
|
description?: string;
|
|
96
89
|
label?: string;
|
|
97
90
|
parameters?: unknown;
|
|
98
|
-
|
|
91
|
+
// Method syntax: Pi's execute requires the narrower ExtensionToolContext; params stay bivariant.
|
|
92
|
+
execute(
|
|
93
|
+
toolCallId: string,
|
|
94
|
+
params: TParams,
|
|
95
|
+
signal?: AbortSignal,
|
|
96
|
+
onUpdate?: AgentToolUpdateCallback<TDetails | undefined>,
|
|
97
|
+
ctx?: ExtensionContext,
|
|
98
|
+
): Promise<ToolResultLike<TDetails>>;
|
|
99
99
|
};
|
|
100
100
|
|
|
101
101
|
export type PiPrettySdk = {
|
|
@@ -116,19 +116,20 @@ export type PiPrettySdk = {
|
|
|
116
116
|
getAgentDir?: () => string;
|
|
117
117
|
};
|
|
118
118
|
|
|
119
|
+
// Method syntax keeps params bivariant, so Pi's generic/overloaded ExtensionAPI is assignable.
|
|
119
120
|
export type PiPrettyApi = {
|
|
120
|
-
registerTool
|
|
121
|
-
registerCommand
|
|
121
|
+
registerTool(tool: unknown): void;
|
|
122
|
+
registerCommand(
|
|
122
123
|
name: string,
|
|
123
124
|
command: {
|
|
124
125
|
description?: string;
|
|
125
126
|
handler: (args: string, ctx: CommandContextLike) => Promise<void> | void;
|
|
126
127
|
},
|
|
127
|
-
)
|
|
128
|
-
on
|
|
128
|
+
): void;
|
|
129
|
+
on(
|
|
129
130
|
event: string,
|
|
130
131
|
handler: (event: unknown, ctx: SessionContextLike) => Promise<void> | void,
|
|
131
|
-
)
|
|
132
|
+
): void;
|
|
132
133
|
};
|
|
133
134
|
|
|
134
135
|
export type OptionalFffModule = { FileFinder: typeof FileFinder };
|
package/src/utils.ts
CHANGED
|
@@ -865,6 +865,19 @@ export function unframeToolResult<T extends FramableComponent>(component: T): T
|
|
|
865
865
|
return (framed[RESULT_FRAME]?.component ?? component) as T;
|
|
866
866
|
}
|
|
867
867
|
|
|
868
|
+
function isTextComponent(component: Component): component is TextComponentLike {
|
|
869
|
+
return typeof (component as Partial<TextComponentLike>).setText === "function";
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
/** The prior render's text component (unframed) when it has one, else a fresh one. */
|
|
873
|
+
export function reuseText(
|
|
874
|
+
last: Component | undefined,
|
|
875
|
+
TextComponent: TextComponentCtor = Text,
|
|
876
|
+
): TextComponentLike {
|
|
877
|
+
const prior = last && unframeToolResult(last);
|
|
878
|
+
return prior && isTextComponent(prior) ? prior : new TextComponent("", 0, 0);
|
|
879
|
+
}
|
|
880
|
+
|
|
868
881
|
/**
|
|
869
882
|
* Default completed-tool chrome: unchanged body, full-width solid close, then
|
|
870
883
|
* footer lines. Existing callers inherit this distro-wide presentation.
|
|
@@ -878,6 +891,31 @@ export function ruleFrame(
|
|
|
878
891
|
return [...bodyLines, rule(width ?? termW(), paint), ...footerLines];
|
|
879
892
|
}
|
|
880
893
|
|
|
894
|
+
/**
|
|
895
|
+
* Command-output body: the first MAX_PREVIEW_LINES rows (all when expanded) with a
|
|
896
|
+
* fading tail and an "… N more" footer. Completed output closes with a status rule;
|
|
897
|
+
* partial output stays unframed. Returns the rows; the caller fills the background.
|
|
898
|
+
*/
|
|
899
|
+
export function outputBodyRows(
|
|
900
|
+
lines: string[],
|
|
901
|
+
theme: StatusRuleTheme & MutedTheme & Parameters<typeof fadeRowsOf>[1],
|
|
902
|
+
ctx: ExpandHintCtx,
|
|
903
|
+
status: "success" | "error",
|
|
904
|
+
partial: boolean,
|
|
905
|
+
): string[] {
|
|
906
|
+
const maxShow = ctx.expanded ? lines.length : MAX_PREVIEW_LINES;
|
|
907
|
+
const footer = lines.length > maxShow ? [moreLines(lines.length - maxShow, theme, ctx)] : [];
|
|
908
|
+
// Fade the last preview rows instead of a hard cut before the "… N more" row.
|
|
909
|
+
const body = fadeRowsOf(
|
|
910
|
+
lines.slice(0, maxShow + 1).map((line) => bodyLine(line, theme)),
|
|
911
|
+
theme,
|
|
912
|
+
maxShow,
|
|
913
|
+
);
|
|
914
|
+
return partial
|
|
915
|
+
? [...body, ...footer]
|
|
916
|
+
: ruleFrame(body, footer, termW(), fadeRulePaint(theme, status));
|
|
917
|
+
}
|
|
918
|
+
|
|
881
919
|
/** Explicit solid top/bottom frame for sections that need stronger separation. */
|
|
882
920
|
export function sectionFrame(
|
|
883
921
|
bodyLines: string[],
|
|
@@ -948,6 +986,28 @@ export function makeTextResult<TDetails>(
|
|
|
948
986
|
};
|
|
949
987
|
}
|
|
950
988
|
|
|
989
|
+
/** Details of an action tool (download, proc): an outcome plus model-facing lines or an error. */
|
|
990
|
+
export interface ActionResultDetails {
|
|
991
|
+
action: string;
|
|
992
|
+
ok: boolean;
|
|
993
|
+
lines: string[];
|
|
994
|
+
error?: string;
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
/** Tool result for an action: the error, else the lines, else `<action> ok`. */
|
|
998
|
+
export function makeActionResult<TDetails extends ActionResultDetails>(details: TDetails) {
|
|
999
|
+
return {
|
|
1000
|
+
content: [
|
|
1001
|
+
{
|
|
1002
|
+
type: "text" as const,
|
|
1003
|
+
text: details.error ?? (details.lines.join("\n") || `${details.action} ok`),
|
|
1004
|
+
},
|
|
1005
|
+
],
|
|
1006
|
+
details,
|
|
1007
|
+
isError: !details.ok,
|
|
1008
|
+
};
|
|
1009
|
+
}
|
|
1010
|
+
|
|
951
1011
|
export function appendNotices(text: string, notices: string[]): string {
|
|
952
1012
|
return notices.length ? `${text}\n\n[${notices.join(". ")}]` : text;
|
|
953
1013
|
}
|