@wierdbytes/pi-facelift 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 huynhgiabuu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # `@wierdbytes/pi-facelift`
2
+
3
+ Cosmetic facelift for built-in [pi coding agent](https://github.com/earendil-works/pi/tree/main/packages/coding-agent)
4
+ tool output.
5
+
6
+ > inspired by [`pi-pretty`](https://github.com/buddingnewinsights/pi-pretty)
7
+
8
+ ![read / bash / ls / find / grep rendered with the open-right rounded frame](./media/demo.png)
9
+
10
+ ## What you get
11
+
12
+ - **`read`** — syntax-highlighted file content with line numbers, plus
13
+ inline image rendering (Kitty / iTerm2 protocols, with tmux passthrough).
14
+ - **`bash`** — open-right frame with the duration + status pinned to the
15
+ bottom border
16
+
17
+ Status color follows pi: **green** on success, **yellow** while still
18
+ running, **red** on non-zero / timeout / abort. The exit summary is
19
+ consistent across statuses (`✓ exit 0`, `✗ exit N`, `⚡ timed out`,
20
+ `⚡ aborted`). Duration is shown as `3.3s` / `1m3s` / `2h5m`.
21
+ - **`ls`** — Nerd Font file icons + tree-oriented rendering.
22
+ - **`find` / `grep`** — grouped / highlighted rendering on top of pi's
23
+ built-in tool implementations (no extra search backend, no extra
24
+ dependencies).
25
+
26
+ ## Install
27
+
28
+ ```bash
29
+ pi install npm:@wierdbytes/pi-facelift
30
+ ```
31
+
32
+ ## Terminal support for inline images
33
+
34
+ Inline image previews work in **Ghostty**, **Kitty**, **iTerm2**, and
35
+ **WezTerm**. Inside `tmux`, pi-facelift uses passthrough escape
36
+ sequences:
37
+
38
+ ```tmux
39
+ set -g allow-passthrough on
40
+ ```
41
+
42
+ (or run once in a session: `tmux set -g allow-passthrough on`)
43
+
44
+ ## Configuration
45
+
46
+ All configuration is via environment variables — no settings file:
47
+
48
+ | Variable | Default | Notes |
49
+ | ------------------------------ | --------------- | ----------------------------------------------------------------------------- |
50
+ | `FACELIFT_THEME` | `github-dark` | Shiki theme. Falls back to `~/.pi/agent/settings.json` `theme` if unset. |
51
+ | `FACELIFT_MAX_HL_CHARS` | `80000` | Files larger than this skip syntax highlighting (still get line numbers). |
52
+ | `FACELIFT_MAX_PREVIEW_LINES` | `80` | Body preview length for collapsed renderers. |
53
+ | `FACELIFT_CACHE_LIMIT` | `128` | Max number of highlighted blocks held in memory. |
54
+ | `FACELIFT_ICONS` | `nerd` | Set to `none` / `off` to disable Nerd Font icons in `ls`/`find`/`grep`. |
55
+ | `FACELIFT_IMAGE_PROTOCOL` | auto | Force `kitty` / `iterm2` / `none`. Auto-detected from `$TERM_PROGRAM` etc. |
56
+
57
+ ## Development
58
+
59
+ This package lives in the [`pi-wierd-stuff`](../../README.md) monorepo.
60
+
61
+ ```bash
62
+ bun install # from the repo root
63
+ cd packages/facelift
64
+ bun run test # 11 tests
65
+ bun run test:watch # vitest watch mode
66
+ bun run demo # render every tool block to your terminal
67
+ ```
68
+
69
+ The `demo` script (`scripts/demo.ts`) is a visual harness — it boots the
70
+ extension with mocked SDK tools and prints every renderer (read with
71
+ live shiki highlighting, bash in success / non-zero / streaming /
72
+ timeout states, ls tree, find groups, grep matches) so you can eyeball
73
+ the open-right rounded frame, status-aware coloring, and the duration /
74
+ exit summary in the bottom border.
75
+
76
+ Typecheck (uses the global `tsc`):
77
+
78
+ ```bash
79
+ bunx tsc --noEmit --target esnext --module nodenext \
80
+ --moduleResolution nodenext --strict --skipLibCheck \
81
+ --allowImportingTsExtensions \
82
+ index.ts
83
+ ```
84
+
85
+ ## License
86
+
87
+ MIT — see [LICENSE](./LICENSE).
@@ -0,0 +1,169 @@
1
+ import { visibleWidth } from "@earendil-works/pi-tui";
2
+ import { describe, expect, it } from "vitest";
3
+
4
+ import piFaceliftExtension from "./index.ts";
5
+
6
+ class MockText {
7
+ private text = "";
8
+ constructor(_text = "", _x = 0, _y = 0) {}
9
+ setText(value: string) {
10
+ this.text = value;
11
+ }
12
+ getText() {
13
+ return this.text;
14
+ }
15
+ }
16
+
17
+ const mockTheme = {
18
+ fg: (_key: string, text: string) => text,
19
+ bold: (text: string) => text,
20
+ };
21
+
22
+ const ansiMockTheme = {
23
+ fg: (_key: string, text: string) => `\x1b[31m${text}\x1b[0m`,
24
+ bold: (text: string) => `\x1b[1m${text}\x1b[22m`,
25
+ };
26
+
27
+ function mockToolFactory(exec: any) {
28
+ return (_cwd: string) => ({
29
+ name: "mock",
30
+ description: "mock",
31
+ parameters: { type: "object", properties: {} },
32
+ execute: exec,
33
+ });
34
+ }
35
+
36
+ function withStdoutColumns<T>(columns: number, fn: () => T): T {
37
+ const descriptor = Object.getOwnPropertyDescriptor(process.stdout, "columns");
38
+ Object.defineProperty(process.stdout, "columns", { configurable: true, value: columns });
39
+ try {
40
+ return fn();
41
+ } finally {
42
+ if (descriptor) {
43
+ Object.defineProperty(process.stdout, "columns", descriptor);
44
+ } else {
45
+ delete (process.stdout as NodeJS.WriteStream & { columns?: number }).columns;
46
+ }
47
+ }
48
+ }
49
+
50
+ function loadBashTool() {
51
+ const noopExec = async () => ({ content: [{ type: "text", text: "" }] });
52
+ const tools = new Map<string, any>();
53
+ const pi = {
54
+ registerTool: (tool: any) => tools.set(tool.name, tool),
55
+ registerCommand: () => {},
56
+ on: () => {},
57
+ };
58
+
59
+ piFaceliftExtension(pi, {
60
+ sdk: {
61
+ createReadToolDefinition: mockToolFactory(noopExec),
62
+ createBashToolDefinition: mockToolFactory(noopExec),
63
+ createLsToolDefinition: mockToolFactory(noopExec),
64
+ createFindToolDefinition: mockToolFactory(noopExec),
65
+ createGrepToolDefinition: mockToolFactory(noopExec),
66
+ getAgentDir: () => "/tmp/pi-facelift-test",
67
+ },
68
+ TextComponent: MockText,
69
+ });
70
+
71
+ return tools.get("bash");
72
+ }
73
+
74
+ describe("bash renderCall expansion", () => {
75
+ it("truncates long commands when collapsed", () => {
76
+ const bashTool = loadBashTool();
77
+ const command = `printf '${"x".repeat(120)}'`;
78
+
79
+ const rendered = bashTool.renderCall({ command }, mockTheme, {
80
+ lastComponent: new MockText(),
81
+ isError: false,
82
+ state: {},
83
+ expanded: false,
84
+ invalidate: () => {},
85
+ });
86
+
87
+ expect(rendered.getText()).toContain("bash");
88
+ expect(rendered.getText()).toContain("…");
89
+ expect(rendered.getText()).not.toContain(command);
90
+ });
91
+
92
+ it("shows the full command when expanded", () => {
93
+ const bashTool = loadBashTool();
94
+ const command = `printf '${"x".repeat(120)}'`;
95
+
96
+ const rendered = bashTool.renderCall({ command }, mockTheme, {
97
+ lastComponent: new MockText(),
98
+ isError: false,
99
+ state: {},
100
+ expanded: true,
101
+ invalidate: () => {},
102
+ });
103
+
104
+ expect(rendered.getText()).toContain(command);
105
+ });
106
+
107
+ it("preserves timeout text in both collapsed and expanded states", () => {
108
+ const bashTool = loadBashTool();
109
+ const command = `printf '${"x".repeat(120)}'`;
110
+
111
+ const collapsed = bashTool.renderCall({ command, timeout: 5 }, mockTheme, {
112
+ lastComponent: new MockText(),
113
+ isError: false,
114
+ state: {},
115
+ expanded: false,
116
+ invalidate: () => {},
117
+ });
118
+ const expanded = bashTool.renderCall({ command, timeout: 5 }, mockTheme, {
119
+ lastComponent: new MockText(),
120
+ isError: false,
121
+ state: {},
122
+ expanded: true,
123
+ invalidate: () => {},
124
+ });
125
+
126
+ expect(collapsed.getText()).toContain("5s timeout");
127
+ expect(expanded.getText()).toContain("5s timeout");
128
+ });
129
+
130
+ it("truncates expanded ANSI tool headers to fit the terminal width", () => {
131
+ withStdoutColumns(84, () => {
132
+ const bashTool = loadBashTool();
133
+ const command = `printf '${"界".repeat(120)}'`;
134
+
135
+ const rendered = bashTool.renderCall({ command }, ansiMockTheme, {
136
+ lastComponent: new MockText(),
137
+ isError: false,
138
+ state: {},
139
+ expanded: true,
140
+ invalidate: () => {},
141
+ });
142
+
143
+ // pi-facelift draws its own frame and pi-tui's `Text.render(width)` is
144
+ // called with `terminal.columns`, so frame lines should be ≤ cols.
145
+ for (const line of rendered.getText().split("\n")) {
146
+ expect(visibleWidth(line)).toBeLessThanOrEqual(84);
147
+ }
148
+ });
149
+ });
150
+
151
+ it("does not exceed narrow terminal widths", () => {
152
+ withStdoutColumns(24, () => {
153
+ const bashTool = loadBashTool();
154
+ const command = `printf '${"x".repeat(120)}'`;
155
+
156
+ const rendered = bashTool.renderCall({ command }, ansiMockTheme, {
157
+ lastComponent: new MockText(),
158
+ isError: false,
159
+ state: {},
160
+ expanded: true,
161
+ invalidate: () => {},
162
+ });
163
+
164
+ for (const line of rendered.getText().split("\n")) {
165
+ expect(visibleWidth(line)).toBeLessThanOrEqual(24);
166
+ }
167
+ });
168
+ });
169
+ });
@@ -0,0 +1,153 @@
1
+ import { mkdirSync, rmSync } from "node:fs";
2
+
3
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
4
+
5
+ import piFaceliftExtension, { __imageInternals } from "./index.ts";
6
+
7
+ const ENV_KEYS = [
8
+ "TMUX",
9
+ "TERM",
10
+ "TERM_PROGRAM",
11
+ "LC_TERMINAL",
12
+ "GHOSTTY_RESOURCES_DIR",
13
+ "KITTY_WINDOW_ID",
14
+ "KITTY_PID",
15
+ "WEZTERM_EXECUTABLE",
16
+ "WEZTERM_CONFIG_DIR",
17
+ "WEZTERM_CONFIG_FILE",
18
+ "COLORTERM",
19
+ "FACELIFT_IMAGE_PROTOCOL",
20
+ ] as const;
21
+
22
+ class MockText {
23
+ private text = "";
24
+ constructor(_text = "", _x = 0, _y = 0) {}
25
+ setText(value: string) {
26
+ this.text = value;
27
+ }
28
+ getText() {
29
+ return this.text;
30
+ }
31
+ }
32
+
33
+ function mockToolFactory(exec: any) {
34
+ return (_cwd: string) => ({
35
+ name: "mock",
36
+ description: "mock",
37
+ parameters: { type: "object", properties: {} },
38
+ execute: exec,
39
+ });
40
+ }
41
+
42
+ function loadReadTool(readExec: any) {
43
+ const noopExec = async () => ({ content: [{ type: "text", text: "" }] });
44
+ const tools = new Map<string, any>();
45
+ const pi = {
46
+ registerTool: (tool: any) => tools.set(tool.name, tool),
47
+ registerCommand: () => {},
48
+ on: () => {},
49
+ };
50
+
51
+ piFaceliftExtension(pi, {
52
+ sdk: {
53
+ createReadToolDefinition: mockToolFactory(readExec),
54
+ createBashToolDefinition: mockToolFactory(noopExec),
55
+ createLsToolDefinition: mockToolFactory(noopExec),
56
+ createFindToolDefinition: mockToolFactory(noopExec),
57
+ createGrepToolDefinition: mockToolFactory(noopExec),
58
+ getAgentDir: () => "/tmp/pi-facelift-test",
59
+ },
60
+ TextComponent: MockText,
61
+ });
62
+
63
+ return tools.get("read");
64
+ }
65
+
66
+ describe("image rendering terminal detection", () => {
67
+ const envSnapshot = new Map<string, string | undefined>();
68
+ const agentDir = "/tmp/pi-facelift-test";
69
+
70
+ beforeEach(() => {
71
+ rmSync(agentDir, { recursive: true, force: true });
72
+ mkdirSync(agentDir, { recursive: true });
73
+ for (const key of ENV_KEYS) {
74
+ envSnapshot.set(key, process.env[key]);
75
+ delete process.env[key];
76
+ }
77
+ __imageInternals.resetCachesForTests();
78
+ });
79
+
80
+ afterEach(() => {
81
+ rmSync(agentDir, { recursive: true, force: true });
82
+ for (const key of ENV_KEYS) {
83
+ const value = envSnapshot.get(key);
84
+ if (value === undefined) delete process.env[key];
85
+ else process.env[key] = value;
86
+ }
87
+ __imageInternals.resetCachesForTests();
88
+ });
89
+
90
+ it("detects kitty protocol inside tmux via KITTY_WINDOW_ID", () => {
91
+ process.env.TMUX = "/tmp/tmux-1000/default,123,0";
92
+ process.env.TERM_PROGRAM = "tmux";
93
+ process.env.KITTY_WINDOW_ID = "1";
94
+
95
+ expect(__imageInternals.getOuterTerminal()).toBe("kitty");
96
+ expect(__imageInternals.detectImageProtocol()).toBe("kitty");
97
+ });
98
+
99
+ it("detects wezterm protocol inside tmux via WEZTERM_EXECUTABLE", () => {
100
+ process.env.TMUX = "/tmp/tmux-1000/default,123,0";
101
+ process.env.TERM_PROGRAM = "tmux";
102
+ process.env.WEZTERM_EXECUTABLE = "/Applications/WezTerm.app/Contents/MacOS/wezterm";
103
+
104
+ expect(__imageInternals.getOuterTerminal()).toBe("WezTerm");
105
+ expect(__imageInternals.detectImageProtocol()).toBe("iterm2");
106
+ });
107
+
108
+ it("falls back to tmux client term for outer terminal detection", () => {
109
+ process.env.TMUX = "/tmp/tmux-1000/default,123,0";
110
+ process.env.TERM_PROGRAM = "tmux";
111
+ __imageInternals.setTmuxClientTermOverrideForTests("xterm-kitty");
112
+
113
+ expect(__imageInternals.getOuterTerminal()).toBe("kitty");
114
+ expect(__imageInternals.detectImageProtocol()).toBe("kitty");
115
+ });
116
+
117
+ it("reports warning when tmux allow-passthrough is off", () => {
118
+ process.env.TMUX = "/tmp/tmux-1000/default,123,0";
119
+ __imageInternals.setTmuxAllowPassthroughOverrideForTests(false);
120
+
121
+ expect(__imageInternals.getTmuxPassthroughWarning("kitty")).toContain("allow-passthrough is off");
122
+ });
123
+
124
+ it("does not warn when tmux allow-passthrough is enabled", () => {
125
+ process.env.TMUX = "/tmp/tmux-1000/default,123,0";
126
+ __imageInternals.setTmuxAllowPassthroughOverrideForTests(true);
127
+
128
+ expect(__imageInternals.getTmuxPassthroughWarning("kitty")).toBeNull();
129
+ });
130
+
131
+ it("renders image metadata without a second inline preview", async () => {
132
+ process.env.TERM_PROGRAM = "kitty";
133
+
134
+ const readTool = loadReadTool(async () => ({
135
+ content: [{ type: "image", data: Buffer.from("fake").toString("base64"), mimeType: "image/png" }],
136
+ }));
137
+
138
+ const result = await readTool.execute("t1", { path: "media/inline-image.png" }, null, null, {});
139
+ const rendered = readTool.renderResult(result, {}, {}, {
140
+ lastComponent: new MockText(),
141
+ isError: false,
142
+ state: {},
143
+ expanded: false,
144
+ invalidate: () => {},
145
+ });
146
+
147
+ expect(rendered.getText()).toContain("image/png");
148
+ expect(rendered.getText()).not.toContain("\x1b_G");
149
+ expect(result.content).toEqual([
150
+ { type: "image", data: Buffer.from("fake").toString("base64"), mimeType: "image/png" },
151
+ ]);
152
+ });
153
+ });