pi-polish 0.1.10 → 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 and MCP tool calls 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.
@@ -57,8 +57,8 @@ const DIFF_PREVIEW_LINES = 16;
57
57
  const BG_RESET = "\x1b[49m";
58
58
  const FG_RESET = "\x1b[39m";
59
59
 
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" };
60
+ type Kind = "bash" | "read" | "search" | "list" | "tools" | "mcp" | "code";
61
+ const KIND_OF: Record<string, Kind> = { bash: "bash", read: "read", grep: "search", find: "search", ls: "list", tool_search: "tools", codemode: "code" };
62
62
  // Pi names MCP tools mcp__<server>__<tool>
63
63
  const kindOf = (toolName: string): Kind | undefined => KIND_OF[toolName] ?? (toolName.startsWith("mcp__") ? "mcp" : undefined);
64
64
  const VERBS: Record<Kind, [done: string, running: string, one: string, many: string]> = {
@@ -68,16 +68,20 @@ const VERBS: Record<Kind, [done: string, running: string, one: string, many: str
68
68
  list: ["Listed", "Listing", "directory", "directories"],
69
69
  tools: ["Ran", "Running", "tool search", "tool searches"],
70
70
  mcp: ["Called", "Calling", "MCP tool", "MCP tools"],
71
+ code: ["Ran", "Running", "codemode script", "codemode scripts"],
71
72
  };
72
73
 
73
74
  interface Member {
74
75
  id: string;
75
76
  kind: Kind;
77
+ toolName: string;
78
+ args: any;
76
79
  label: string;
77
80
  done: boolean;
78
81
  error: boolean;
79
82
  // The latest output line of a running shell command
80
83
  latest?: string;
84
+ result?: Result;
81
85
  }
82
86
 
83
87
  interface Group {
@@ -118,6 +122,11 @@ function label(toolName: string, args: any, cwd: string): string {
118
122
  return `Search tools "${args?.query ?? ""}"`;
119
123
  case "ls":
120
124
  return `List ${args?.path ? shortPath(args.path, cwd) : "."}`;
125
+ case "codemode": {
126
+ // The script's first line of code, which usually names the tool it calls
127
+ const first = String(args?.code ?? "").split("\n").map((l) => l.trim()).find(Boolean);
128
+ return `codemode${first ? ` ${first}` : ""}`;
129
+ }
121
130
  default: {
122
131
  // server/tool like pi's own MCP rows, with the first text argument, which is usually the query
123
132
  const [, server, ...tool] = toolName.split("__");
@@ -462,9 +471,9 @@ export default function (pi: ExtensionAPI) {
462
471
  }
463
472
  };
464
473
 
465
- 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 => {
466
475
  current ??= { members: [], started: Date.now() };
467
- 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 });
468
477
  groupOf.set(id, current);
469
478
  return current;
470
479
  };
@@ -480,8 +489,8 @@ export default function (pi: ExtensionAPI) {
480
489
  pi.on("session_start", async (_event, ctx) => {
481
490
  reset();
482
491
  const branch = ctx.sessionManager.getBranch();
483
- // Tool results come after their calls, so collect the failures first
484
- 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]] : [])));
485
494
  for (const entry of branch) {
486
495
  if (entry.type !== "message") continue;
487
496
  const msg = entry.message as any;
@@ -490,7 +499,8 @@ export default function (pi: ExtensionAPI) {
490
499
  for (const part of msg.content ?? []) {
491
500
  if (part.type === "text" && part.text.trim()) current = undefined;
492
501
  if (part.type !== "toolCall") continue;
493
- 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);
494
504
  else current = undefined;
495
505
  }
496
506
  }
@@ -522,6 +532,8 @@ export default function (pi: ExtensionAPI) {
522
532
  if (msg.role === "assistant" && (msg.content ?? []).some((c: any) => c.type === "text" && c.text.trim())) current = undefined;
523
533
  });
524
534
  pi.on("tool_execution_start", async (event) => {
535
+ // Calls a codemode script makes show inside its own row, so they don't join or end a group
536
+ if (event.parentToolCallId) return;
525
537
  if (!kindOf(event.toolName)) {
526
538
  current = undefined;
527
539
  return;
@@ -548,19 +560,65 @@ export default function (pi: ExtensionAPI) {
548
560
  if (!group || !member) return;
549
561
  member.done = true;
550
562
  member.error = event.isError;
563
+ member.result = event.result;
551
564
  tick(group);
552
565
  refresh(group);
553
566
  });
554
567
  pi.on("session_shutdown", async () => reset());
555
568
 
556
- /** 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. */
557
615
  const renderGrouped = (toolCallId: string, theme: any, context: any): Component | undefined => {
558
616
  rowInvalidate.set(toolCallId, context.invalidate);
559
617
  const group = groupOf.get(toolCallId);
560
- if (context.expanded || group?.open) return undefined;
561
618
  // Live rows wait for tool_execution_start to place them; history rows without a group render alone
562
619
  if (!group) return live || !ready ? empty() : undefined;
563
620
  if (group.members[0].id !== toolCallId) return empty();
621
+ if (context.expanded || group.open) return clickable(group, tree(group, theme));
564
622
 
565
623
  const running = group.members.some((m) => !m.done);
566
624
  const failed = group.members.filter((m) => m.error).length;
@@ -608,9 +666,11 @@ export default function (pi: ExtensionAPI) {
608
666
  renderCall(args: any, theme, context) {
609
667
  const grouped = renderGrouped(context.toolCallId, theme, context);
610
668
  if (grouped) return grouped;
611
- // Expanded, a shell command shows in full, with its cd and every line; collapsed rows keep the short label
612
- const command = isOpen(context) && toolName === "bash" ? String(args?.command ?? "").trim().split("\n") : undefined;
613
- const text = command ? `$ ${command.slice(0, 60).join("\n")}${command.length > 60 ? `\n... ${command.length - 60} more lines` : ""}` : truncateToWidth(label(toolName, args, cwd), 400);
669
+ // Expanded, a shell command or a codemode script shows in full, with every line; collapsed rows keep the short label
670
+ const source = toolName === "bash" ? args?.command : toolName === "codemode" ? args?.code : undefined;
671
+ const lines = isOpen(context) && source !== undefined ? String(source ?? "").trim().split("\n") : undefined;
672
+ const prefix = toolName === "bash" ? "$ " : "codemode\n";
673
+ const text = lines ? `${prefix}${lines.slice(0, 60).join("\n")}${lines.length > 60 ? `\n... ${lines.length - 60} more lines` : ""}` : truncateToWidth(label(toolName, args, cwd), 400);
614
674
  // Finished rows are dim without a dot; running and failed ones keep it
615
675
  const done = status(context) === "success";
616
676
  const mark = done ? " " : dot(theme, context);
@@ -625,7 +685,8 @@ export default function (pi: ExtensionAPI) {
625
685
  return group?.open ? clickable(group, row) : row;
626
686
  },
627
687
  renderResult(result, { expanded }, theme, context) {
628
- 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();
629
690
  return new Text(expandedBody(theme, resultText(result as Result)), 0, 0);
630
691
  },
631
692
  });
@@ -703,14 +764,16 @@ export default function (pi: ExtensionAPI) {
703
764
  return own && own.renderShell !== "self" ? otherTool(toolName, own) : own;
704
765
  }
705
766
  const own = groupedTool(toolName);
706
- if (kindOf(toolName) !== "mcp") return own;
707
- // Expanded MCP results keep pi's own view, which shows images and limits long one-line JSON. It gets
708
- // no lastComponent, since that may be the empty component drawn while the row was collapsed.
767
+ if (kindOf(toolName) !== "mcp" && kindOf(toolName) !== "code") return own;
768
+ // Expanded MCP and codemode results keep pi's own view, which shows images, limits long one-line JSON and
769
+ // lists a script's nested calls. It gets no lastComponent, since that may be the empty component drawn
770
+ // while the row was collapsed.
709
771
  const pis = next()?.renderResult;
772
+ if (pis) pisResult.set(toolName, pis);
710
773
  return {
711
774
  ...own,
712
775
  renderResult: (result, options, theme, context) =>
713
- 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),
714
777
  };
715
778
  });
716
779
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-polish",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
4
4
  "license": "MIT",
5
5
  "author": "Thomas van Dongen",
6
6
  "type": "module",