pi-polish 0.1.11 → 0.1.12

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 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 their full output, and click again to fold it; `ctrl+o` does this for every row.
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.
@@ -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 {
@@ -468,9 +471,9 @@ export default function (pi: ExtensionAPI) {
468
471
  }
469
472
  };
470
473
 
471
- const assign = (id: string, toolName: string, args: unknown, done = false, error = false): Group => {
474
+ const assign = (id: string, toolName: string, args: unknown, done = false, error = false, result?: Result): Group => {
472
475
  current ??= { members: [], started: Date.now() };
473
- current.members.push({ id, kind: kindOf(toolName)!, label: label(toolName, args, cwd), done, error });
476
+ current.members.push({ id, kind: kindOf(toolName)!, toolName, args, label: label(toolName, args, cwd), done, error, result });
474
477
  groupOf.set(id, current);
475
478
  return current;
476
479
  };
@@ -486,8 +489,8 @@ export default function (pi: ExtensionAPI) {
486
489
  pi.on("session_start", async (_event, ctx) => {
487
490
  reset();
488
491
  const branch = ctx.sessionManager.getBranch();
489
- // Tool results come after their calls, so collect the failures first
490
- const failed = new Set(branch.flatMap((e: any) => (e.type === "message" && e.message.role === "toolResult" && e.message.isError ? [e.message.toolCallId] : [])));
492
+ // Tool results come after their calls, so collect them first
493
+ const results = new Map<string, any>(branch.flatMap((e: any) => (e.type === "message" && e.message.role === "toolResult" ? [[e.message.toolCallId, e.message]] : [])));
491
494
  for (const entry of branch) {
492
495
  if (entry.type !== "message") continue;
493
496
  const msg = entry.message as any;
@@ -496,7 +499,8 @@ export default function (pi: ExtensionAPI) {
496
499
  for (const part of msg.content ?? []) {
497
500
  if (part.type === "text" && part.text.trim()) current = undefined;
498
501
  if (part.type !== "toolCall") continue;
499
- if (kindOf(part.name)) assign(part.id, part.name, part.arguments, true, failed.has(part.id));
502
+ const result = results.get(part.id);
503
+ if (kindOf(part.name)) assign(part.id, part.name, part.arguments, true, Boolean(result?.isError), result);
500
504
  else current = undefined;
501
505
  }
502
506
  }
@@ -556,19 +560,65 @@ export default function (pi: ExtensionAPI) {
556
560
  if (!group || !member) return;
557
561
  member.done = true;
558
562
  member.error = event.isError;
563
+ member.result = event.result;
559
564
  tick(group);
560
565
  refresh(group);
561
566
  });
562
567
  pi.on("session_shutdown", async () => reset());
563
568
 
564
- /** Collapsed rendering for a grouped tool row; the group's first row draws the whole group. */
569
+ // Pi's own result view per tool, which MCP and codemode calls keep inside the expanded tree
570
+ const pisResult = new Map<string, ToolRenderers["renderResult"]>();
571
+
572
+ /** A call's command and output in the expanded tree, as lines that fit width. */
573
+ const memberLines = (m: Member, theme: any, width: number): string[] => {
574
+ const source = m.toolName === "bash" ? m.args?.command : m.toolName === "codemode" ? m.args?.code : undefined;
575
+ const command = source === undefined ? m.label : `${m.toolName === "bash" ? "$ " : ""}${String(source).trim()}`;
576
+ const commandLines = command.split("\n");
577
+ const shown = commandLines.slice(0, 60).join("\n") + (commandLines.length > 60 ? `\n... ${commandLines.length - 60} more lines` : "");
578
+ const lines = wrapTextWithAnsi(theme.fg("toolTitle", shown), width);
579
+ if (!m.result) return m.latest ? [...lines, ...wrapTextWithAnsi(theme.fg("dim", m.latest), width)] : lines;
580
+ const pis = pisResult.get(m.toolName);
581
+ if (pis) {
582
+ 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 };
583
+ const own = pis(m.result as any, { expanded: true, isPartial: false }, theme, context as any).render(width);
584
+ // Pi's view starts with a blank line that would leave an empty branch row
585
+ return [...lines, ...own.slice(own.findIndex((line) => visibleWidth(line.trim()) > 0))];
586
+ }
587
+ // Runs of blank lines, such as before a failed command's exit code, take one line
588
+ const output = resultText(m.result).trimEnd().replace(/\n{3,}/g, "\n\n").split("\n");
589
+ // All of it: the tool already cut long output, and expanding asks to see everything
590
+ return [...lines, ...output.flatMap((line) => wrapTextWithAnsi(theme.fg(m.error ? "error" : "toolOutput", line), width))];
591
+ };
592
+
593
+ /** The expanded group: its summary, then each call on a branch with its own dot, command and output. */
594
+ const tree = (group: Group, theme: any): Component => ({
595
+ invalidate() {},
596
+ render(width: number) {
597
+ const running = group.members.some((m) => !m.done);
598
+ const failed = group.members.filter((m) => m.error).length;
599
+ const elapsed = running ? theme.fg("dim", ` · ${Math.round((Date.now() - group.started) / 1000)}s`) : "";
600
+ const head = theme.fg(failed ? "error" : running ? "accent" : "success", `${MARK} `) + theme.fg("toolTitle", `${summary(group, running)}${running ? "…" : ""}`) + elapsed + (failed ? theme.fg("error", ` · ${failed} failed`) : "");
601
+ const lines = [truncateToWidth(head, width)];
602
+ group.members.forEach((m, i) => {
603
+ const last = i === group.members.length - 1;
604
+ const mark = theme.fg(m.error ? "error" : m.done ? "success" : "accent", `${MARK} `);
605
+ memberLines(m, theme, Math.max(10, width - 6)).forEach((line, j) => {
606
+ const branch = j === 0 ? (last ? "└ " : "├ ") : last ? " " : "│ ";
607
+ lines.push(` ${theme.fg("dim", branch)}${j === 0 ? mark : " "}${line}`);
608
+ });
609
+ });
610
+ return lines;
611
+ },
612
+ });
613
+
614
+ /** Rendering for a grouped tool row; the group's first row draws the whole group, folded or as a tree. */
565
615
  const renderGrouped = (toolCallId: string, theme: any, context: any): Component | undefined => {
566
616
  rowInvalidate.set(toolCallId, context.invalidate);
567
617
  const group = groupOf.get(toolCallId);
568
- if (context.expanded || group?.open) return undefined;
569
618
  // Live rows wait for tool_execution_start to place them; history rows without a group render alone
570
619
  if (!group) return live || !ready ? empty() : undefined;
571
620
  if (group.members[0].id !== toolCallId) return empty();
621
+ if (context.expanded || group.open) return clickable(group, tree(group, theme));
572
622
 
573
623
  const running = group.members.some((m) => !m.done);
574
624
  const failed = group.members.filter((m) => m.error).length;
@@ -635,7 +685,8 @@ export default function (pi: ExtensionAPI) {
635
685
  return group?.open ? clickable(group, row) : row;
636
686
  },
637
687
  renderResult(result, { expanded }, theme, context) {
638
- if (!expanded && !isOpen(context)) return empty();
688
+ // A group's first row draws every call's output in its tree
689
+ if (groupOf.has(context.toolCallId) || (!expanded && !isOpen(context))) return empty();
639
690
  return new Text(expandedBody(theme, resultText(result as Result)), 0, 0);
640
691
  },
641
692
  });
@@ -718,10 +769,11 @@ export default function (pi: ExtensionAPI) {
718
769
  // lists a script's nested calls. It gets no lastComponent, since that may be the empty component drawn
719
770
  // while the row was collapsed.
720
771
  const pis = next()?.renderResult;
772
+ if (pis) pisResult.set(toolName, pis);
721
773
  return {
722
774
  ...own,
723
775
  renderResult: (result, options, theme, context) =>
724
- isOpen({ expanded: options.expanded, toolCallId: context.toolCallId }) && pis ? pis(result, { ...options, expanded: true }, theme, { ...context, lastComponent: undefined }) : own.renderResult!(result, options, theme, context),
776
+ !groupOf.has(context.toolCallId) && options.expanded && pis ? pis(result, { ...options, expanded: true }, theme, { ...context, lastComponent: undefined }) : own.renderResult!(result, options, theme, context),
725
777
  };
726
778
  });
727
779
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-polish",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "license": "MIT",
5
5
  "author": "Thomas van Dongen",
6
6
  "type": "module",