@giladbarnea/pi-pretty-codemode 0.2.0 → 0.2.1
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 +5 -3
- package/package.json +32 -9
- package/src/capture.ts +1 -1
- package/src/previews.ts +12 -3
- package/src/render.ts +45 -32
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
last_updated: 2026-11-
|
|
2
|
+
last_updated: 2026-11-30T14-38
|
|
3
3
|
---
|
|
4
4
|
|
|
5
5
|
# Pretty Codemode
|
|
@@ -40,10 +40,10 @@ This extension does not replace tool definitions, execution or model-visible res
|
|
|
40
40
|
## Current limits
|
|
41
41
|
|
|
42
42
|
- Captures live in memory for the latest 32 Code Mode runs, up to 256 child calls per run. These limits count records, not payload bytes.
|
|
43
|
-
- Reload, session changes and tree navigation clear captures.
|
|
43
|
+
- Reload, session changes and tree navigation clear live captures. Expanded history still shows the formatted Script output box and Pi's saved nested-call summaries. Pi does not save nested tool output, so that output is only available from live captures.
|
|
44
44
|
- Nested images show a placeholder. Images printed by the script remain Pi-owned.
|
|
45
45
|
- Expansion applies to the Code Mode call, not independently to each child.
|
|
46
|
-
- Expanded views format JavaScript only for the newest 256
|
|
46
|
+
- Expanded views format JavaScript only for the newest 256 Code Mode calls, including saved calls. Older calls show the JavaScript as written. Formatting is display-only.
|
|
47
47
|
|
|
48
48
|
The accepted prototype is now a first-class package. Visual hierarchy and repeated-input feedback remain to be addressed. Persistence, payload-byte budgets and additional interaction behavior are separate decisions.
|
|
49
49
|
|
|
@@ -56,6 +56,8 @@ npm run check
|
|
|
56
56
|
|
|
57
57
|
Tests require Bun. Unit/rendering fixtures are self-contained. The suite exercises actual SDK execution, structured child results, partial output, cancellation, expansion, padding and intent comments.
|
|
58
58
|
|
|
59
|
+
To test reload and resume in real Pi, run `uv run -p python3 tests/saved-history.py regular` and repeat with `fullscreen`. This uses a private tmux server and an offline fixture provider.
|
|
60
|
+
|
|
59
61
|
- `index.ts`: registers the guideline, capture invalidation and Code Mode renderer.
|
|
60
62
|
- `src/capture.ts`: observes nested execution events.
|
|
61
63
|
- `src/render.ts`: owns the Code Mode display.
|
package/package.json
CHANGED
|
@@ -1,17 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@giladbarnea/pi-pretty-codemode",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Intent summaries and syntax-highlighted nested tool results for Pi Code Mode.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"keywords": [
|
|
8
|
-
|
|
7
|
+
"keywords": [
|
|
8
|
+
"pi-package"
|
|
9
|
+
],
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/giladbarnea/pi-pretty-codemode.git"
|
|
13
|
+
},
|
|
9
14
|
"homepage": "https://github.com/giladbarnea/pi-pretty-codemode#readme",
|
|
10
|
-
"bugs": {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/giladbarnea/pi-pretty-codemode/issues"
|
|
17
|
+
},
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=24"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"index.ts",
|
|
23
|
+
"src/",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"pi": {
|
|
31
|
+
"extensions": [
|
|
32
|
+
"./index.ts"
|
|
33
|
+
]
|
|
34
|
+
},
|
|
15
35
|
"scripts": {
|
|
16
36
|
"test": "for file in tests/*.test.ts; do bun test --timeout 30000 \"$file\" || exit; done",
|
|
17
37
|
"typecheck": "tsc --project tsconfig.json",
|
|
@@ -23,7 +43,10 @@
|
|
|
23
43
|
"@dprint/typescript": "0.101.0",
|
|
24
44
|
"@giladbarnea/pi-pretty-bash": "1.0.15"
|
|
25
45
|
},
|
|
26
|
-
"peerDependencies": {
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@earendil-works/pi-coding-agent": "1.5.* || 1.6.*",
|
|
48
|
+
"@earendil-works/pi-tui": "1.5.* || 1.6.*"
|
|
49
|
+
},
|
|
27
50
|
"devDependencies": {
|
|
28
51
|
"@earendil-works/pi-coding-agent": "1.5.2",
|
|
29
52
|
"@earendil-works/pi-tui": "1.5.2",
|
package/src/capture.ts
CHANGED
|
@@ -65,7 +65,7 @@ export const installCapture: InstallCapture = (pi, invalidate) => {
|
|
|
65
65
|
const run = runs.get(root)!;
|
|
66
66
|
if (event.toolCallId === root) {
|
|
67
67
|
const details = event.result.details as CodemodeToolDetails | undefined;
|
|
68
|
-
const statuses = new Map(details?.calls
|
|
68
|
+
const statuses = new Map(details?.calls?.map(call => [call.id, call.status]));
|
|
69
69
|
for (const call of run.calls) {
|
|
70
70
|
call.status = statuses.get(call.toolCallId) ?? (call.status === "running" ? "cancelled" : call.status);
|
|
71
71
|
}
|
package/src/previews.ts
CHANGED
|
@@ -5,8 +5,7 @@ type SavedEntry = { type: string; message?: { role?: string; content?: unknown }
|
|
|
5
5
|
type ToolCallBlock = { type?: unknown; name?: unknown; id?: unknown };
|
|
6
6
|
|
|
7
7
|
/** The newest Code Mode calls in the chat, in the order Pi draws them: its rebuilt history, then live calls.
|
|
8
|
-
* Only Pi's chat events change the order; rendering only asks.
|
|
9
|
-
* before session_start), so a call the window has not seen is never a preview and never moves another call. */
|
|
8
|
+
* Only Pi's chat events change the order; rendering only asks. A call outside the window never moves another call. */
|
|
10
9
|
export type PreviewWindow = {
|
|
11
10
|
/** Replace the order with a rebuilt chat's Code Mode call IDs, oldest first. Pi redraws the chat afterwards. */
|
|
12
11
|
sync(toolCallIds: Iterable<string>): void;
|
|
@@ -37,12 +36,18 @@ export function codemodeCallIds(entries: Iterable<SavedEntry>): string[] {
|
|
|
37
36
|
export function createPreviewWindow(limit = PREVIEW_CALLS): PreviewWindow {
|
|
38
37
|
let positions = new Map<string, number>();
|
|
39
38
|
const previews = new Map<string, Map<object, () => void>>();
|
|
39
|
+
let beforeFirstSync: Map<object, { toolCallId: string; invalidate: () => void }> | undefined = new Map();
|
|
40
40
|
const outside = (toolCallId: string): boolean => !positions.has(toolCallId) || positions.size - positions.get(toolCallId)! > limit;
|
|
41
41
|
return {
|
|
42
42
|
sync(toolCallIds) {
|
|
43
43
|
positions = new Map();
|
|
44
44
|
previews.clear();
|
|
45
45
|
for (const toolCallId of toolCallIds) positions.set(toolCallId, positions.size);
|
|
46
|
+
const waiting = beforeFirstSync;
|
|
47
|
+
beforeFirstSync = undefined;
|
|
48
|
+
for (const { toolCallId, invalidate } of waiting?.values() ?? []) {
|
|
49
|
+
if (!outside(toolCallId)) invalidate();
|
|
50
|
+
}
|
|
46
51
|
},
|
|
47
52
|
add(toolCallId) {
|
|
48
53
|
if (positions.has(toolCallId)) return;
|
|
@@ -54,7 +59,11 @@ export function createPreviewWindow(limit = PREVIEW_CALLS): PreviewWindow {
|
|
|
54
59
|
}
|
|
55
60
|
},
|
|
56
61
|
includes(toolCallId, owner, invalidate) {
|
|
57
|
-
if (outside(toolCallId))
|
|
62
|
+
if (outside(toolCallId)) {
|
|
63
|
+
// In-process reload draws expanded saved rows before session_start supplies their chat order.
|
|
64
|
+
beforeFirstSync?.set(owner, { toolCallId, invalidate });
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
58
67
|
previews.set(toolCallId, (previews.get(toolCallId) ?? new Map()).set(owner, invalidate));
|
|
59
68
|
return true;
|
|
60
69
|
},
|
package/src/render.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { highlightCode, type CodemodeToolDetails, type ToolRenderers } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { Box, Text, wrapTextWithAnsi, type Component } from "@earendil-works/pi-tui";
|
|
2
|
+
import { Box, getCapabilities, Text, wrapTextWithAnsi, type Component } from "@earendil-works/pi-tui";
|
|
3
3
|
import type { CreateCodemodeRenderers } from "./contract.ts";
|
|
4
4
|
import { renderChild } from "./renderers/children.ts";
|
|
5
5
|
import { commandMemo, displayColors, type CommandMemo } from "./renderers/bash.ts";
|
|
@@ -9,11 +9,13 @@ import { javascriptPreview } from "./renderers/code-preview.ts";
|
|
|
9
9
|
import { jsonPreview, memoizedPreview } from "./renderers/preview.ts";
|
|
10
10
|
|
|
11
11
|
const SCRIPT_HEADER = /^Script (completed|failed)\nWall time [\d.]+ seconds\nOutput:\n$/;
|
|
12
|
+
const SAVED_RESULT_COLORS = ["toolOutput", "dim", "success", "warning", "error"] as const;
|
|
12
13
|
type RenderState = {
|
|
13
14
|
nativeResult?: ReturnType<NonNullable<ToolRenderers["renderResult"]>>;
|
|
14
15
|
startedAt?: number;
|
|
15
16
|
commands?: CommandMemo["entries"];
|
|
16
17
|
scriptOutput?: typeof jsonPreview;
|
|
18
|
+
savedResultRows?: RowMemo;
|
|
17
19
|
javascript?: typeof javascriptPreview;
|
|
18
20
|
javascriptRows?: RowMemo;
|
|
19
21
|
javascriptColors?: { source: string; colors: string; lines: readonly string[] };
|
|
@@ -47,8 +49,8 @@ export const createCodemodeRenderers: CreateCodemodeRenderers = (getRun, origina
|
|
|
47
49
|
if (!context.expanded && intent) {
|
|
48
50
|
return padded(new Text(`${theme.fg("toolTitle", theme.bold("Code Mode"))} ${theme.fg("accent", intent)}`, 0, 0), context.outputPad);
|
|
49
51
|
}
|
|
50
|
-
//
|
|
51
|
-
const formatted = typeof source === "string" && context.expanded &&
|
|
52
|
+
// A saved result has complete arguments even though Pi only sets argsComplete on live rows.
|
|
53
|
+
const formatted = typeof source === "string" && context.expanded && !context.isPartial && !context.isError
|
|
52
54
|
&& previews.includes(context.toolCallId, state, context.invalidate)
|
|
53
55
|
? (state.javascript ??= memoizedPreview(displayedJavascript))(source)
|
|
54
56
|
: code;
|
|
@@ -74,14 +76,15 @@ export const createCodemodeRenderers: CreateCodemodeRenderers = (getRun, origina
|
|
|
74
76
|
},
|
|
75
77
|
renderResult(result, options, theme, context) {
|
|
76
78
|
const run = getRun(context.toolCallId);
|
|
79
|
+
const details = result.details as CodemodeToolDetails | undefined;
|
|
80
|
+
const state = context.state as RenderState;
|
|
77
81
|
const source = (context.args as { code?: unknown } | undefined)?.code;
|
|
78
82
|
const intent = intentFromCode(typeof source === "string" ? source : "", context.argsComplete);
|
|
79
83
|
if (!options.expanded && !context.isError && intent) {
|
|
80
|
-
const details = result.details as CodemodeToolDetails | undefined;
|
|
81
84
|
const calls = details?.calls ?? run?.calls ?? [];
|
|
82
85
|
const counts = new Map<string, number>();
|
|
83
86
|
for (const call of calls) counts.set(call.status, (counts.get(call.status) ?? 0) + 1);
|
|
84
|
-
const startedAt =
|
|
87
|
+
const startedAt = state.startedAt;
|
|
85
88
|
const timing = options.isPartial
|
|
86
89
|
? startedAt === undefined ? "running" : `running ${Math.round((Date.now() - startedAt) / 1000)}s`
|
|
87
90
|
: context.durationMs === undefined ? "done" : `took ${(context.durationMs / 1000).toFixed(1)}s`;
|
|
@@ -89,38 +92,44 @@ export const createCodemodeRenderers: CreateCodemodeRenderers = (getRun, origina
|
|
|
89
92
|
const text = details?.fullOutputPath ? `${summary}\nFull output: ${details.fullOutputPath}` : summary;
|
|
90
93
|
return padded(new Text(theme.fg("muted", displayText(text)), 0, 0), context.outputPad);
|
|
91
94
|
}
|
|
92
|
-
if (!run
|
|
93
|
-
|
|
94
|
-
state.commands = undefined;
|
|
95
|
-
state.scriptOutput = undefined;
|
|
95
|
+
if (!run) state.commands = undefined;
|
|
96
|
+
if (!run && (!options.expanded || options.isPartial) && original.renderResult) {
|
|
96
97
|
const component = original.renderResult(result, options, theme, { ...context, lastComponent: state.nativeResult });
|
|
97
98
|
state.nativeResult = component;
|
|
98
99
|
return padded(component, context.outputPad);
|
|
99
100
|
}
|
|
100
|
-
const calls = run?.calls ?? [];
|
|
101
101
|
const completed = options.expanded && !options.isPartial && !context.isError;
|
|
102
|
-
const
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const sourceOutput = textOutput({ ...result, content });
|
|
108
|
-
const output = displayText(sourceOutput.replace(/\r/g, "")).trimEnd();
|
|
109
|
-
const fullOutputPath = (result.details as CodemodeToolDetails | undefined)?.fullOutputPath;
|
|
110
|
-
const preview = completed && !fullOutputPath
|
|
111
|
-
? (state.scriptOutput ??= memoizedPreview(jsonPreview))(sourceOutput)
|
|
112
|
-
: undefined;
|
|
113
|
-
const styled = preview
|
|
114
|
-
? preview.map((part, index) => {
|
|
115
|
-
const text = displayText(part.text.replace(/\r/g, ""));
|
|
116
|
-
return part.json ? highlightCode(text, "json").join("\n") : styledOutput(index === preview.length - 1 ? text.trimEnd() : text, theme);
|
|
117
|
-
}).join("\n")
|
|
118
|
-
: styledOutput(output, theme, context.isError);
|
|
119
|
-
const failures = calls.filter(call => call.status === "error").length;
|
|
120
|
-
const summary = `${calls.length} call${calls.length === 1 ? "" : "s"}${failures ? ` · ${failures} failed` : ""}`;
|
|
121
|
-
return padded(new Lines(width => {
|
|
102
|
+
const fullOutputPath = details?.fullOutputPath;
|
|
103
|
+
const draw = new Lines(width => {
|
|
104
|
+
const calls = run?.calls ?? details?.calls ?? [];
|
|
105
|
+
const failures = calls.filter(call => call.status === "error").length;
|
|
106
|
+
const summary = `${calls.length} call${calls.length === 1 ? "" : "s"}${failures ? ` · ${failures} failed` : ""}`;
|
|
122
107
|
const rows: string[] = ["", theme.fg("muted", theme.bold(`Nested tools · ${summary}`))];
|
|
123
|
-
|
|
108
|
+
if (run) {
|
|
109
|
+
const commands = commandMemo(state.commands ??= new Map(), theme, highlightBash, completed);
|
|
110
|
+
for (const call of run.calls) rows.push("", ...renderChild(call, theme, options.expanded, commands).render(width));
|
|
111
|
+
} else {
|
|
112
|
+
// Reuse Pi's saved summaries and image indicators, but not its script output or timer.
|
|
113
|
+
const savedCalls = (details?.calls ?? []).map(call => ({
|
|
114
|
+
...call, name: displayText(call.name), args: displayText(call.args), error: call.error && displayText(call.error),
|
|
115
|
+
}));
|
|
116
|
+
const summaries = original.renderResult?.({ content: result.content.filter(block => block.type === "image"), details: { calls: savedCalls } },
|
|
117
|
+
options, theme, { ...context, durationMs: undefined, state: {}, lastComponent: undefined });
|
|
118
|
+
if (summaries) rows.push(...summaries.render(width));
|
|
119
|
+
}
|
|
120
|
+
const [first, ...rest] = result.content;
|
|
121
|
+
const content = first?.type === "text" && SCRIPT_HEADER.test(first.text) ? rest : result.content;
|
|
122
|
+
const sourceOutput = textOutput({ ...result, content });
|
|
123
|
+
const output = displayText(sourceOutput.replace(/\r/g, "")).trimEnd();
|
|
124
|
+
const preview = completed && !fullOutputPath
|
|
125
|
+
? (state.scriptOutput ??= memoizedPreview(jsonPreview))(sourceOutput)
|
|
126
|
+
: undefined;
|
|
127
|
+
const styled = preview
|
|
128
|
+
? preview.map((part, index) => {
|
|
129
|
+
const text = displayText(part.text.replace(/\r/g, ""));
|
|
130
|
+
return part.json ? highlightCode(text, "json").join("\n") : styledOutput(index === preview.length - 1 ? text.trimEnd() : text, theme);
|
|
131
|
+
}).join("\n")
|
|
132
|
+
: styledOutput(output, theme, context.isError);
|
|
124
133
|
if (run?.incomplete) rows.push(...wrapTextWithAnsi(theme.fg("warning", "Nested call capture is incomplete or truncated"), width));
|
|
125
134
|
rows.push("", theme.fg("muted", theme.bold("Script output")));
|
|
126
135
|
const innerWidth = Math.max(1, width - 4);
|
|
@@ -132,6 +141,10 @@ export const createCodemodeRenderers: CreateCodemodeRenderers = (getRun, origina
|
|
|
132
141
|
if (fullOutputPath) rows.push(...wrapTextWithAnsi(theme.fg("muted", `Full output: ${displayText(fullOutputPath)}`), width));
|
|
133
142
|
if (context.durationMs !== undefined) rows.push(theme.fg("muted", `Took ${(context.durationMs / 1000).toFixed(1)}s`));
|
|
134
143
|
return rows;
|
|
135
|
-
})
|
|
144
|
+
});
|
|
145
|
+
if (run) return padded(draw, context.outputPad);
|
|
146
|
+
const colors = displayColors(theme) + SAVED_RESULT_COLORS.map(color => theme.fg(color, "")).join("");
|
|
147
|
+
const keys = [result.content, details, colors, options.expanded, options.isPartial, context.isError, context.durationMs, context.showImages, getCapabilities().images];
|
|
148
|
+
return padded(new KeptLines(state.savedResultRows ??= {}, keys, width => draw.render(width)), context.outputPad);
|
|
136
149
|
},
|
|
137
150
|
});
|