pi-polish 0.1.3 → 0.1.4
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 +4 -4
- package/extensions/chrome.ts +17 -3
- package/extensions/command.ts +22 -0
- package/extensions/tools.ts +68 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,16 +13,16 @@ pi install npm:pi-polish
|
|
|
13
13
|
|
|
14
14
|
## Features
|
|
15
15
|
|
|
16
|
-
- Shell commands, file reads and
|
|
16
|
+
- Shell commands, file reads, searches and MCP tool calls in a row fold into one line that updates while they run (`Running 2 shell commands… · 5s`) and then sums them up (`Ran 3 shell commands, read 2 files` or `Called 2 MCP tools`). `ctrl+o` shows the full output.
|
|
17
17
|
- Edits and new files show as diffs with syntax highlighting and the changed words marked, in colors that follow your Pi theme, dark or light.
|
|
18
|
-
-
|
|
19
|
-
- An orange status line shows what Pi is doing, how long it has been at it and how many tokens it has written.
|
|
18
|
+
- Reads, searches, shell commands and MCP calls show a pulsing dot in your theme's accent color while they run, then turn into a quiet grey row once done, with a red dot if one failed. Edits and writes keep a green dot with their diff.
|
|
19
|
+
- An orange status line shows what Pi is doing, how long it has been at it and how many tokens it has written. When Pi finishes, a dim line says how long it worked and when it was done.
|
|
20
20
|
- `❯` marks your messages and `●` marks Pi's replies.
|
|
21
21
|
- A short instruction to the model to change files with Pi's edit and write tools instead of shell commands like `sed -i`, so you see the changes as diffs.
|
|
22
22
|
|
|
23
23
|
## Compatibility
|
|
24
24
|
|
|
25
|
-
pi-polish only changes how Pi's built-in tools are drawn; the tools themselves run exactly as before. The chat styling (the markers and the status line) reaches into Pi's internal UI, so a new Pi release can break that part of the look until pi-polish is updated. Your sessions are never affected. Tested with Pi 1.
|
|
25
|
+
pi-polish only changes how Pi's built-in tools and MCP tools are drawn; the tools themselves run exactly as before. The chat styling (the markers and the status line) reaches into Pi's internal UI, so a new Pi release can break that part of the look until pi-polish is updated. Your sessions are never affected. Tested with Pi 1.4.
|
|
26
26
|
|
|
27
27
|
## License
|
|
28
28
|
|
package/extensions/chrome.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AssistantMessageComponent, CustomEditor, type ExtensionAPI, UserMessageComponent } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import { Markdown, MouseRegion, Spacer, Text } from "@earendil-works/pi-tui";
|
|
3
|
+
import { shortCommand } from "./command.ts";
|
|
3
4
|
|
|
4
5
|
const PROMPT = "❯";
|
|
5
6
|
const REPLY = "●";
|
|
@@ -93,7 +94,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
93
94
|
const path = String(args?.path ?? "").split("/").pop();
|
|
94
95
|
switch (name) {
|
|
95
96
|
case "bash":
|
|
96
|
-
return `Running ${String(args?.command ?? "").
|
|
97
|
+
return `Running ${shortCommand(String(args?.command ?? ""), process.cwd()).slice(0, 60)}`;
|
|
97
98
|
case "read":
|
|
98
99
|
return `Reading ${path}`;
|
|
99
100
|
case "edit":
|
|
@@ -107,8 +108,10 @@ export default function (pi: ExtensionAPI) {
|
|
|
107
108
|
return "Starting a background job";
|
|
108
109
|
case "questionnaire":
|
|
109
110
|
return "Waiting for your answer";
|
|
111
|
+
case "tool_search":
|
|
112
|
+
return "Searching tools";
|
|
110
113
|
default:
|
|
111
|
-
return `Running ${name}`;
|
|
114
|
+
return name.startsWith("mcp__") ? `Calling ${name.split("__")[1]}/${name.split("__").slice(2).join("__")}` : `Running ${name}`;
|
|
112
115
|
}
|
|
113
116
|
};
|
|
114
117
|
pi.on("session_start", async (_event, ctx) => {
|
|
@@ -116,6 +119,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
116
119
|
});
|
|
117
120
|
pi.on("agent_start", async (_event, ctx) => {
|
|
118
121
|
if (!ctx.hasUI) return;
|
|
122
|
+
ctx.ui.setWidget("pi-polish-turn", undefined);
|
|
119
123
|
started = Date.now();
|
|
120
124
|
tokens = 0;
|
|
121
125
|
streamed = 0;
|
|
@@ -147,7 +151,17 @@ export default function (pi: ExtensionAPI) {
|
|
|
147
151
|
pi.on("agent_end", async (_event, ctx) => {
|
|
148
152
|
clearInterval(timer);
|
|
149
153
|
timer = undefined;
|
|
150
|
-
if (ctx.hasUI)
|
|
154
|
+
if (!ctx.hasUI) return;
|
|
155
|
+
ctx.ui.setWorkingMessage();
|
|
156
|
+
// How long the turn took and when it ended, until the next one starts
|
|
157
|
+
const secs = Math.round((Date.now() - started) / 1000);
|
|
158
|
+
const took = secs < 60 ? `${secs}s` : `${Math.floor(secs / 60)}m ${secs % 60}s`;
|
|
159
|
+
const done = new Date().toLocaleTimeString([], { hour: "numeric", minute: "2-digit" });
|
|
160
|
+
ctx.ui.setWidget("pi-polish-turn", (_tui, theme) => new Text(theme.fg("dim", `✻ Worked for ${took} · done ${done}`), 0, 0));
|
|
161
|
+
});
|
|
162
|
+
// A jump in the session tree leaves the turn behind, so its summary goes too
|
|
163
|
+
pi.on("session_tree", async (_event, ctx) => {
|
|
164
|
+
if (ctx.hasUI) ctx.ui.setWidget("pi-polish-turn", undefined);
|
|
151
165
|
});
|
|
152
166
|
pi.on("session_shutdown", async () => clearInterval(timer));
|
|
153
167
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
2
|
+
import { basename, isAbsolute, resolve } from "node:path";
|
|
3
|
+
|
|
4
|
+
// Real paths, so a cd through a symlink such as /tmp still counts as cwd
|
|
5
|
+
const real = (path: string) => {
|
|
6
|
+
try {
|
|
7
|
+
return realpathSync(path);
|
|
8
|
+
} catch {
|
|
9
|
+
return path;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/** A shell command's first line, without leading cds into cwd; another folder shows as its last part when the path is absolute. */
|
|
14
|
+
export function shortCommand(command: string, cwd: string): string {
|
|
15
|
+
const rest = command.trim().replace(/^(?:cd\s+(?:"[^"]*"|'[^']*'|\S+)[ \t]*(?:&&|;|\n)\s*)+/, (prefix) => {
|
|
16
|
+
const [, quoted, separator] = [...prefix.matchAll(/cd\s+("[^"]*"|'[^']*'|\S+)[ \t]*(&&|;|\n)/g)].at(-1)!;
|
|
17
|
+
const dir = quoted.replace(/^["']|["']$/g, "");
|
|
18
|
+
if (real(resolve(cwd, dir)) === real(resolve(cwd))) return "";
|
|
19
|
+
return `cd ${isAbsolute(dir) ? `…/${basename(dir)}` : quoted}${separator === ";" ? ";" : " &&"} `;
|
|
20
|
+
});
|
|
21
|
+
return rest.split("\n")[0];
|
|
22
|
+
}
|
package/extensions/tools.ts
CHANGED
|
@@ -7,7 +7,8 @@ import {
|
|
|
7
7
|
type ExtensionAPI,
|
|
8
8
|
type ToolRenderers,
|
|
9
9
|
} from "@earendil-works/pi-coding-agent";
|
|
10
|
-
import { type Component, Container, Text, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
10
|
+
import { type Component, Container, Text, truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
11
|
+
import { shortCommand } from "./command.ts";
|
|
11
12
|
import bash from "@shikijs/langs/shellscript";
|
|
12
13
|
import css from "@shikijs/langs/css";
|
|
13
14
|
import c from "@shikijs/langs/c";
|
|
@@ -56,13 +57,17 @@ const DIFF_PREVIEW_LINES = 16;
|
|
|
56
57
|
const BG_RESET = "\x1b[49m";
|
|
57
58
|
const FG_RESET = "\x1b[39m";
|
|
58
59
|
|
|
59
|
-
type Kind = "bash" | "read" | "search" | "list";
|
|
60
|
-
const KIND_OF: Record<string, Kind> = { bash: "bash", read: "read", grep: "search", find: "search", ls: "list" };
|
|
60
|
+
type Kind = "bash" | "read" | "search" | "list" | "tools" | "mcp";
|
|
61
|
+
const KIND_OF: Record<string, Kind> = { bash: "bash", read: "read", grep: "search", find: "search", ls: "list", tool_search: "tools" };
|
|
62
|
+
// Pi names MCP tools mcp__<server>__<tool>
|
|
63
|
+
const kindOf = (toolName: string): Kind | undefined => KIND_OF[toolName] ?? (toolName.startsWith("mcp__") ? "mcp" : undefined);
|
|
61
64
|
const VERBS: Record<Kind, [done: string, running: string, one: string, many: string]> = {
|
|
62
65
|
bash: ["Ran", "Running", "shell command", "shell commands"],
|
|
63
66
|
read: ["Read", "Reading", "file", "files"],
|
|
64
67
|
search: ["Searched", "Searching", "pattern", "patterns"],
|
|
65
68
|
list: ["Listed", "Listing", "directory", "directories"],
|
|
69
|
+
tools: ["Ran", "Running", "tool search", "tool searches"],
|
|
70
|
+
mcp: ["Called", "Calling", "MCP tool", "MCP tools"],
|
|
66
71
|
};
|
|
67
72
|
|
|
68
73
|
interface Member {
|
|
@@ -98,15 +103,23 @@ function shortPath(path: unknown, cwd: string): string {
|
|
|
98
103
|
function label(toolName: string, args: any, cwd: string): string {
|
|
99
104
|
switch (toolName) {
|
|
100
105
|
case "bash":
|
|
101
|
-
return `$ ${String(args?.command ?? "")
|
|
106
|
+
return `$ ${shortCommand(String(args?.command ?? ""), cwd)}`;
|
|
102
107
|
case "read":
|
|
103
108
|
return `Read ${shortPath(args?.path, cwd)}`;
|
|
104
109
|
case "grep":
|
|
105
110
|
return `Search "${args?.pattern ?? ""}"${args?.path ? ` in ${shortPath(args.path, cwd)}` : ""}`;
|
|
106
111
|
case "find":
|
|
107
112
|
return `Find "${args?.pattern ?? ""}"${args?.path ? ` in ${shortPath(args.path, cwd)}` : ""}`;
|
|
108
|
-
|
|
113
|
+
case "tool_search":
|
|
114
|
+
return `Search tools "${args?.query ?? ""}"`;
|
|
115
|
+
case "ls":
|
|
109
116
|
return `List ${args?.path ? shortPath(args.path, cwd) : "."}`;
|
|
117
|
+
default: {
|
|
118
|
+
// server/tool like pi's own MCP rows, with the first text argument, which is usually the query
|
|
119
|
+
const [, server, ...tool] = toolName.split("__");
|
|
120
|
+
const first = Object.values(args ?? {}).find((v) => typeof v === "string") as string | undefined;
|
|
121
|
+
return `${server}/${tool.join("__")}${first ? ` "${first.split("\n")[0]}"` : ""}`;
|
|
122
|
+
}
|
|
110
123
|
}
|
|
111
124
|
}
|
|
112
125
|
|
|
@@ -412,6 +425,14 @@ function empty() {
|
|
|
412
425
|
return new Container();
|
|
413
426
|
}
|
|
414
427
|
|
|
428
|
+
/** Lines cut to the screen width instead of wrapping; the first line keeps its failure note in view. */
|
|
429
|
+
function oneLine(lines: string[], suffix = ""): Component {
|
|
430
|
+
return {
|
|
431
|
+
invalidate() {},
|
|
432
|
+
render: (width: number) => lines.map((line, i) => (i === 0 ? truncateToWidth(line, Math.max(10, width - visibleWidth(suffix))) + suffix : truncateToWidth(line, width))),
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
|
|
415
436
|
export default function (pi: ExtensionAPI) {
|
|
416
437
|
const cwd = process.cwd();
|
|
417
438
|
const groupOf = new Map<string, Group>();
|
|
@@ -427,7 +448,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
427
448
|
|
|
428
449
|
const tick = (group: Group) => {
|
|
429
450
|
const running = group.members.some((m) => !m.done);
|
|
430
|
-
if (running && !group.timer) group.timer = setInterval(() => rowInvalidate.get(group.members[0].id)?.(),
|
|
451
|
+
if (running && !group.timer) group.timer = setInterval(() => rowInvalidate.get(group.members[0].id)?.(), 500);
|
|
431
452
|
if (!running && group.timer) {
|
|
432
453
|
clearInterval(group.timer);
|
|
433
454
|
group.timer = undefined;
|
|
@@ -437,7 +458,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
437
458
|
|
|
438
459
|
const assign = (id: string, toolName: string, args: unknown, done = false, error = false): Group => {
|
|
439
460
|
current ??= { members: [], started: Date.now() };
|
|
440
|
-
current.members.push({ id, kind:
|
|
461
|
+
current.members.push({ id, kind: kindOf(toolName)!, label: label(toolName, args, cwd), done, error });
|
|
441
462
|
groupOf.set(id, current);
|
|
442
463
|
return current;
|
|
443
464
|
};
|
|
@@ -460,7 +481,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
460
481
|
for (const part of msg.content ?? []) {
|
|
461
482
|
if (part.type === "text" && part.text.trim()) current = undefined;
|
|
462
483
|
if (part.type !== "toolCall") continue;
|
|
463
|
-
if (
|
|
484
|
+
if (kindOf(part.name)) assign(part.id, part.name, part.arguments, true);
|
|
464
485
|
else current = undefined;
|
|
465
486
|
}
|
|
466
487
|
}
|
|
@@ -492,7 +513,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
492
513
|
if (msg.role === "assistant" && (msg.content ?? []).some((c: any) => c.type === "text" && c.text.trim())) current = undefined;
|
|
493
514
|
});
|
|
494
515
|
pi.on("tool_execution_start", async (event) => {
|
|
495
|
-
if (!
|
|
516
|
+
if (!kindOf(event.toolName)) {
|
|
496
517
|
current = undefined;
|
|
497
518
|
return;
|
|
498
519
|
}
|
|
@@ -526,31 +547,44 @@ export default function (pi: ExtensionAPI) {
|
|
|
526
547
|
if (running) {
|
|
527
548
|
const secs = Math.round((Date.now() - group.started) / 1000);
|
|
528
549
|
const active = [...group.members].reverse().find((m) => !m.done) ?? group.members[group.members.length - 1];
|
|
529
|
-
|
|
530
|
-
|
|
550
|
+
// The dot pulses every half second while the group runs, like a cursor blink
|
|
551
|
+
const pulse = Math.floor(Date.now() / 500) % 2 ? "dim" : "accent";
|
|
552
|
+
const head = theme.fg(pulse, `${MARK} `) + theme.fg("toolTitle", `${summary(group, true)}…`) + theme.fg("dim", ` · ${secs}s`) + failNote;
|
|
553
|
+
return oneLine([head, theme.fg("dim", ` └ ${active.label}`)]);
|
|
531
554
|
}
|
|
532
|
-
|
|
533
|
-
const mark = theme.fg(
|
|
555
|
+
// Finished rows go quiet, like Claude Code's: dim text, with a red ● only when something failed
|
|
556
|
+
const mark = failed ? theme.fg("error", `${MARK} `) : " ";
|
|
534
557
|
const text = group.members.length === 1 ? group.members[0].label : summary(group, false);
|
|
535
|
-
return
|
|
558
|
+
return oneLine([mark + theme.fg("dim", text)], failNote);
|
|
536
559
|
};
|
|
537
560
|
|
|
538
561
|
// The ● carries the status, like Claude Code: dim while arguments stream, accent while running, green or red when done.
|
|
539
562
|
// History rows redrawn after /reload have no execution state, so they count as done.
|
|
540
|
-
const
|
|
541
|
-
if (context.isError) return
|
|
542
|
-
if (context.executionStarted && context.isPartial) return
|
|
543
|
-
if (!context.executionStarted && live) return
|
|
544
|
-
return
|
|
563
|
+
const status = (context: { isPartial?: boolean; isError?: boolean; executionStarted?: boolean }) => {
|
|
564
|
+
if (context.isError) return "error";
|
|
565
|
+
if (context.executionStarted && context.isPartial) return "accent";
|
|
566
|
+
if (!context.executionStarted && live) return "dim";
|
|
567
|
+
return "success";
|
|
545
568
|
};
|
|
569
|
+
const dot = (theme: any, context: any) => theme.fg(status(context), `${MARK} `);
|
|
546
570
|
|
|
547
571
|
const groupedTool = (toolName: string): ToolRenderers => ({
|
|
548
572
|
renderShell: "self",
|
|
549
573
|
renderCall(args: any, theme, context) {
|
|
550
574
|
const grouped = renderGrouped(context.toolCallId, theme, context);
|
|
551
575
|
if (grouped) return grouped;
|
|
552
|
-
|
|
553
|
-
|
|
576
|
+
// Expanded, a shell command shows in full, with its cd and every line; collapsed rows keep the short label
|
|
577
|
+
const command = context.expanded && toolName === "bash" ? String(args?.command ?? "").trim().split("\n") : undefined;
|
|
578
|
+
const text = command ? `$ ${command.slice(0, 60).join("\n")}${command.length > 60 ? `\n... ${command.length - 60} more lines` : ""}` : truncateToWidth(label(toolName, args, cwd), 400);
|
|
579
|
+
// Finished rows are dim without a dot; running and failed ones keep it
|
|
580
|
+
const done = status(context) === "success";
|
|
581
|
+
const mark = done ? " " : dot(theme, context);
|
|
582
|
+
// Wrapped lines continue under the text, past the dot, instead of at the left edge
|
|
583
|
+
return {
|
|
584
|
+
invalidate() {},
|
|
585
|
+
render: (width: number) =>
|
|
586
|
+
wrapTextWithAnsi(text, Math.max(10, width - 2)).map((line, i) => (i === 0 ? mark : " ") + theme.fg(done ? "dim" : "toolTitle", line)),
|
|
587
|
+
};
|
|
554
588
|
},
|
|
555
589
|
renderResult(result, { expanded }, theme) {
|
|
556
590
|
if (!expanded) return empty();
|
|
@@ -599,7 +633,19 @@ export default function (pi: ExtensionAPI) {
|
|
|
599
633
|
}),
|
|
600
634
|
};
|
|
601
635
|
// Only the drawing changes: pi's own tools keep running, with their own schema and on/off state
|
|
602
|
-
pi.registerToolRenderer((toolName, next) =>
|
|
636
|
+
pi.registerToolRenderer((toolName, next) => {
|
|
637
|
+
if (renderers[toolName] || !kindOf(toolName)) return renderers[toolName] ?? next();
|
|
638
|
+
const own = groupedTool(toolName);
|
|
639
|
+
if (kindOf(toolName) !== "mcp") return own;
|
|
640
|
+
// Expanded MCP results keep pi's own view, which shows images and limits long one-line JSON. It gets
|
|
641
|
+
// no lastComponent, since that may be the empty component drawn while the row was collapsed.
|
|
642
|
+
const pis = next()?.renderResult;
|
|
643
|
+
return {
|
|
644
|
+
...own,
|
|
645
|
+
renderResult: (result, options, theme, context) =>
|
|
646
|
+
options.expanded && pis ? pis(result, options, theme, { ...context, lastComponent: undefined }) : own.renderResult!(result, options, theme, context),
|
|
647
|
+
};
|
|
648
|
+
});
|
|
603
649
|
|
|
604
650
|
// pi only lists a tool's guidelines while the tool is on
|
|
605
651
|
pi.on("before_agent_start", async (event) => {
|