pi-polish 0.1.11 → 0.1.13
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 +1 -1
- package/extensions/tools.ts +74 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ pi install npm:pi-polish
|
|
|
13
13
|
|
|
14
14
|
## Features
|
|
15
15
|
|
|
16
|
-
- Shell commands, file reads, searches, MCP tool calls and codemode scripts in a row fold into one line that updates while they run (`Running 2 shell commands… · 5s`, with the command and its latest output line below) and then sums them up (`Ran 3 shell commands, read 2 files` or `Called 2 MCP tools`). Click a row to see its commands and
|
|
16
|
+
- Shell commands, file reads, searches, MCP tool calls and codemode scripts in a row fold into one line that updates while they run (`Running 2 shell commands… · 5s`, with the command and its latest output line below) and then sums them up (`Ran 3 shell commands, read 2 files` or `Called 2 MCP tools`). Click a row to see its commands under it, each with its own status dot and full output, and click again to fold it; `ctrl+o` does this for every row.
|
|
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
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
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.
|
package/extensions/tools.ts
CHANGED
|
@@ -74,11 +74,14 @@ const VERBS: Record<Kind, [done: string, running: string, one: string, many: str
|
|
|
74
74
|
interface Member {
|
|
75
75
|
id: string;
|
|
76
76
|
kind: Kind;
|
|
77
|
+
toolName: string;
|
|
78
|
+
args: any;
|
|
77
79
|
label: string;
|
|
78
80
|
done: boolean;
|
|
79
81
|
error: boolean;
|
|
80
82
|
// The latest output line of a running shell command
|
|
81
83
|
latest?: string;
|
|
84
|
+
result?: Result;
|
|
82
85
|
}
|
|
83
86
|
|
|
84
87
|
interface Group {
|
|
@@ -86,8 +89,9 @@ interface Group {
|
|
|
86
89
|
started: number;
|
|
87
90
|
ended?: number;
|
|
88
91
|
timer?: NodeJS.Timeout;
|
|
89
|
-
// Opened by a click,
|
|
92
|
+
// Opened or folded by a click, overriding ctrl+o for this group until ctrl+o is toggled
|
|
90
93
|
open?: boolean;
|
|
94
|
+
openUnder?: boolean;
|
|
91
95
|
}
|
|
92
96
|
|
|
93
97
|
type Result = { content: Array<{ type: string; text?: string }>; details?: unknown };
|
|
@@ -468,9 +472,9 @@ export default function (pi: ExtensionAPI) {
|
|
|
468
472
|
}
|
|
469
473
|
};
|
|
470
474
|
|
|
471
|
-
const assign = (id: string, toolName: string, args: unknown, done = false, error = false): Group => {
|
|
475
|
+
const assign = (id: string, toolName: string, args: unknown, done = false, error = false, result?: Result): Group => {
|
|
472
476
|
current ??= { members: [], started: Date.now() };
|
|
473
|
-
current.members.push({ id, kind: kindOf(toolName)!, label: label(toolName, args, cwd), done, error });
|
|
477
|
+
current.members.push({ id, kind: kindOf(toolName)!, toolName, args, label: label(toolName, args, cwd), done, error, result });
|
|
474
478
|
groupOf.set(id, current);
|
|
475
479
|
return current;
|
|
476
480
|
};
|
|
@@ -486,8 +490,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
486
490
|
pi.on("session_start", async (_event, ctx) => {
|
|
487
491
|
reset();
|
|
488
492
|
const branch = ctx.sessionManager.getBranch();
|
|
489
|
-
// Tool results come after their calls, so collect
|
|
490
|
-
const
|
|
493
|
+
// Tool results come after their calls, so collect them first
|
|
494
|
+
const results = new Map<string, any>(branch.flatMap((e: any) => (e.type === "message" && e.message.role === "toolResult" ? [[e.message.toolCallId, e.message]] : [])));
|
|
491
495
|
for (const entry of branch) {
|
|
492
496
|
if (entry.type !== "message") continue;
|
|
493
497
|
const msg = entry.message as any;
|
|
@@ -496,7 +500,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
496
500
|
for (const part of msg.content ?? []) {
|
|
497
501
|
if (part.type === "text" && part.text.trim()) current = undefined;
|
|
498
502
|
if (part.type !== "toolCall") continue;
|
|
499
|
-
|
|
503
|
+
const result = results.get(part.id);
|
|
504
|
+
if (kindOf(part.name)) assign(part.id, part.name, part.arguments, true, Boolean(result?.isError), result);
|
|
500
505
|
else current = undefined;
|
|
501
506
|
}
|
|
502
507
|
}
|
|
@@ -556,19 +561,65 @@ export default function (pi: ExtensionAPI) {
|
|
|
556
561
|
if (!group || !member) return;
|
|
557
562
|
member.done = true;
|
|
558
563
|
member.error = event.isError;
|
|
564
|
+
member.result = event.result;
|
|
559
565
|
tick(group);
|
|
560
566
|
refresh(group);
|
|
561
567
|
});
|
|
562
568
|
pi.on("session_shutdown", async () => reset());
|
|
563
569
|
|
|
564
|
-
|
|
570
|
+
// Pi's own result view per tool, which MCP and codemode calls keep inside the expanded tree
|
|
571
|
+
const pisResult = new Map<string, ToolRenderers["renderResult"]>();
|
|
572
|
+
|
|
573
|
+
/** A call's command and output in the expanded tree, as lines that fit width. */
|
|
574
|
+
const memberLines = (m: Member, theme: any, width: number): string[] => {
|
|
575
|
+
const source = m.toolName === "bash" ? m.args?.command : m.toolName === "codemode" ? m.args?.code : undefined;
|
|
576
|
+
const command = source === undefined ? m.label : `${m.toolName === "bash" ? "$ " : ""}${String(source).trim()}`;
|
|
577
|
+
const commandLines = command.split("\n");
|
|
578
|
+
const shown = commandLines.slice(0, 60).join("\n") + (commandLines.length > 60 ? `\n... ${commandLines.length - 60} more lines` : "");
|
|
579
|
+
const lines = wrapTextWithAnsi(theme.fg("toolTitle", shown), width);
|
|
580
|
+
if (!m.result) return m.latest ? [...lines, ...wrapTextWithAnsi(theme.fg("dim", m.latest), width)] : lines;
|
|
581
|
+
const pis = pisResult.get(m.toolName);
|
|
582
|
+
if (pis) {
|
|
583
|
+
const context = { args: m.args, toolCallId: m.id, invalidate() {}, lastComponent: undefined, state: {}, cwd, executionStarted: true, argsComplete: true, isPartial: false, expanded: true, showImages: true, isError: m.error, outputPad: 0 };
|
|
584
|
+
const own = pis(m.result as any, { expanded: true, isPartial: false }, theme, context as any).render(width);
|
|
585
|
+
// Pi's view starts with a blank line that would leave an empty branch row
|
|
586
|
+
return [...lines, ...own.slice(own.findIndex((line) => visibleWidth(line.trim()) > 0))];
|
|
587
|
+
}
|
|
588
|
+
// Runs of blank lines, such as before a failed command's exit code, take one line
|
|
589
|
+
const output = resultText(m.result).trimEnd().replace(/\n{3,}/g, "\n\n").split("\n");
|
|
590
|
+
// All of it: the tool already cut long output, and expanding asks to see everything
|
|
591
|
+
return [...lines, ...output.flatMap((line) => wrapTextWithAnsi(theme.fg(m.error ? "error" : "toolOutput", line), width))];
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
/** The expanded group: its summary, then each call on a branch with its own dot, command and output. */
|
|
595
|
+
const tree = (group: Group, theme: any): Component => ({
|
|
596
|
+
invalidate() {},
|
|
597
|
+
render(width: number) {
|
|
598
|
+
const running = group.members.some((m) => !m.done);
|
|
599
|
+
const failed = group.members.filter((m) => m.error).length;
|
|
600
|
+
const elapsed = running ? theme.fg("dim", ` · ${Math.round((Date.now() - group.started) / 1000)}s`) : "";
|
|
601
|
+
const head = theme.fg(failed ? "error" : running ? "accent" : "success", `${MARK} `) + theme.fg("toolTitle", `${summary(group, running)}${running ? "…" : ""}`) + elapsed + (failed ? theme.fg("error", ` · ${failed} failed`) : "");
|
|
602
|
+
const lines = [truncateToWidth(head, width)];
|
|
603
|
+
group.members.forEach((m, i) => {
|
|
604
|
+
const last = i === group.members.length - 1;
|
|
605
|
+
const mark = theme.fg(m.error ? "error" : m.done ? "success" : "accent", `${MARK} `);
|
|
606
|
+
memberLines(m, theme, Math.max(10, width - 6)).forEach((line, j) => {
|
|
607
|
+
const branch = j === 0 ? (last ? "└ " : "├ ") : last ? " " : "│ ";
|
|
608
|
+
lines.push(` ${theme.fg("dim", branch)}${j === 0 ? mark : " "}${line}`);
|
|
609
|
+
});
|
|
610
|
+
});
|
|
611
|
+
return lines;
|
|
612
|
+
},
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
/** Rendering for a grouped tool row; the group's first row draws the whole group, folded or as a tree. */
|
|
565
616
|
const renderGrouped = (toolCallId: string, theme: any, context: any): Component | undefined => {
|
|
566
617
|
rowInvalidate.set(toolCallId, context.invalidate);
|
|
567
618
|
const group = groupOf.get(toolCallId);
|
|
568
|
-
if (context.expanded || group?.open) return undefined;
|
|
569
619
|
// Live rows wait for tool_execution_start to place them; history rows without a group render alone
|
|
570
620
|
if (!group) return live || !ready ? empty() : undefined;
|
|
571
621
|
if (group.members[0].id !== toolCallId) return empty();
|
|
622
|
+
if (shown(group, context.expanded)) return clickable(group, tree(group, theme), context.expanded);
|
|
572
623
|
|
|
573
624
|
const running = group.members.some((m) => !m.done);
|
|
574
625
|
const failed = group.members.filter((m) => m.error).length;
|
|
@@ -588,18 +639,24 @@ export default function (pi: ExtensionAPI) {
|
|
|
588
639
|
const mark = failed ? theme.fg("error", `${MARK} `) : " ";
|
|
589
640
|
// Always the summary, even for one call ("Ran 1 shell command"), like Claude Code; a click or ctrl+o shows each one
|
|
590
641
|
const text = summary(group, false);
|
|
591
|
-
return clickable(group, oneLine([mark + theme.fg("dim", text)], failNote));
|
|
642
|
+
return clickable(group, oneLine([mark + theme.fg("dim", text)], failNote), context.expanded);
|
|
592
643
|
};
|
|
593
644
|
|
|
645
|
+
/** Whether a group shows as a tree: ctrl+o's state, unless a click changed it since ctrl+o was last toggled. */
|
|
646
|
+
const shown = (group: Group, expanded?: boolean) => (group.open !== undefined && group.openUnder === Boolean(expanded) ? group.open : Boolean(expanded));
|
|
594
647
|
/** A row that opens or closes its group when clicked. */
|
|
595
|
-
const clickable = (group: Group, row: Component): Component =>
|
|
648
|
+
const clickable = (group: Group, row: Component, expanded?: boolean): Component =>
|
|
596
649
|
new MouseRegion(row, (event) => {
|
|
597
650
|
if (event.type !== "click" || event.button !== "left") return undefined;
|
|
598
|
-
group.open = !group
|
|
651
|
+
group.open = !shown(group, expanded);
|
|
652
|
+
group.openUnder = Boolean(expanded);
|
|
599
653
|
refresh(group);
|
|
600
654
|
return { handled: true };
|
|
601
655
|
});
|
|
602
|
-
const isOpen = (context: { expanded?: boolean; toolCallId?: string }) =>
|
|
656
|
+
const isOpen = (context: { expanded?: boolean; toolCallId?: string }) => {
|
|
657
|
+
const group = groupOf.get(context.toolCallId ?? "");
|
|
658
|
+
return group ? shown(group, context.expanded) : Boolean(context.expanded);
|
|
659
|
+
};
|
|
603
660
|
|
|
604
661
|
// The ● carries the status, like Claude Code: dim while arguments stream, accent while running, green or red when done.
|
|
605
662
|
// History rows redrawn after /reload have no execution state, so they count as done.
|
|
@@ -632,10 +689,11 @@ export default function (pi: ExtensionAPI) {
|
|
|
632
689
|
};
|
|
633
690
|
// A row of a group opened by a click closes it again
|
|
634
691
|
const group = groupOf.get(context.toolCallId);
|
|
635
|
-
return group
|
|
692
|
+
return group && shown(group, context.expanded) ? clickable(group, row, context.expanded) : row;
|
|
636
693
|
},
|
|
637
694
|
renderResult(result, { expanded }, theme, context) {
|
|
638
|
-
|
|
695
|
+
// A group's first row draws every call's output in its tree
|
|
696
|
+
if (groupOf.has(context.toolCallId) || (!expanded && !isOpen(context))) return empty();
|
|
639
697
|
return new Text(expandedBody(theme, resultText(result as Result)), 0, 0);
|
|
640
698
|
},
|
|
641
699
|
});
|
|
@@ -718,10 +776,11 @@ export default function (pi: ExtensionAPI) {
|
|
|
718
776
|
// lists a script's nested calls. It gets no lastComponent, since that may be the empty component drawn
|
|
719
777
|
// while the row was collapsed.
|
|
720
778
|
const pis = next()?.renderResult;
|
|
779
|
+
if (pis) pisResult.set(toolName, pis);
|
|
721
780
|
return {
|
|
722
781
|
...own,
|
|
723
782
|
renderResult: (result, options, theme, context) =>
|
|
724
|
-
|
|
783
|
+
!groupOf.has(context.toolCallId) && options.expanded && pis ? pis(result, { ...options, expanded: true }, theme, { ...context, lastComponent: undefined }) : own.renderResult!(result, options, theme, context),
|
|
725
784
|
};
|
|
726
785
|
});
|
|
727
786
|
|