@bismawy/pi-arnative 0.9.1 → 0.10.1

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.
Files changed (47) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/README.md +3 -3
  3. package/extensions/animations.ts +1 -1
  4. package/extensions/arnative.ts +23 -11
  5. package/extensions/autocompact.ts +0 -0
  6. package/extensions/footer.ts +0 -0
  7. package/extensions/model-heal.ts +93 -18
  8. package/extensions/section-headers.ts +0 -0
  9. package/extensions/timestamps.ts +0 -0
  10. package/extensions/todos.ts +43 -15
  11. package/extensions/tools.ts +128 -11
  12. package/extensions/ui-render-tweaks.ts +5 -5
  13. package/extensions/usage.ts +309 -19
  14. package/lib/ansi.ts +22 -12
  15. package/lib/box.ts +1 -1
  16. package/lib/check.ts +0 -0
  17. package/lib/compat.ts +13 -64
  18. package/lib/format.ts +0 -0
  19. package/lib/list-picker.ts +117 -22
  20. package/lib/model-heal.ts +78 -13
  21. package/lib/patch.ts +0 -0
  22. package/lib/preset-store.ts +0 -0
  23. package/lib/provider.ts +153 -66
  24. package/lib/shortcuts.ts +36 -20
  25. package/lib/spinner.ts +0 -0
  26. package/lib/status-widget.ts +14 -0
  27. package/lib/todos.ts +0 -0
  28. package/lib/usage-store.ts +227 -24
  29. package/package.json +1 -1
  30. package/themes/arnative-blossom.json +89 -0
  31. package/themes/arnative-crimson.json +89 -0
  32. package/themes/arnative-cyberpunk.json +28 -28
  33. package/themes/arnative-dracula.json +40 -39
  34. package/themes/arnative-earthy.json +89 -0
  35. package/themes/arnative-garden.json +89 -0
  36. package/themes/arnative-gruvbox.json +40 -39
  37. package/themes/arnative-lemonade.json +89 -0
  38. package/themes/{arnative-synthwave.json → arnative-magical.json} +32 -31
  39. package/themes/arnative-matrix.json +37 -36
  40. package/themes/arnative-neutral.json +89 -0
  41. package/themes/arnative-nord.json +43 -42
  42. package/themes/arnative-zinc.json +43 -42
  43. package/themes/arnative.json +32 -31
  44. package/themes/gen-themes.mjs +376 -129
  45. package/themes/arnative-emerald.json +0 -88
  46. package/themes/arnative-sun.json +0 -88
  47. package/themes/arnative-violet.json +0 -88
@@ -141,6 +141,18 @@ export function resHead(theme: Theme): string {
141
141
  return theme.fg("dim", "󱞩");
142
142
  }
143
143
 
144
+ /** Rows a block of text takes once painted at `width`, blank lines dropped: one long JSON
145
+ * string is a single newline but twenty-five rows on screen. */
146
+ const drawnRows = (text: string, width: number): number =>
147
+ text
148
+ .split("\n")
149
+ .filter((l) => l.trim())
150
+ .reduce((n, l) => n + Math.max(1, Math.ceil(visibleWidth(l) / Math.max(1, width))), 0);
151
+
152
+ /** A summary is one row by definition: a single-line JSON payload of 40k chars would
153
+ * otherwise wrap into a full screen and the box would look expanded while folded. */
154
+ const oneRow = (text: string, inner: number): string => truncateToWidth(text, inner, "…");
155
+
144
156
  // Title line: duration right-aligned and space-aware (title truncated when it
145
157
  // doesn't fit); expanded = full title wrapped. All lines stay <= inner (overflow bug).
146
158
  export function titleRow(
@@ -412,6 +424,17 @@ const componentLines = (component: any, width: number): string[] => {
412
424
  }
413
425
  };
414
426
 
427
+ /** Right-align a label on the first row of `rows`, for a row pi did not build: the fold
428
+ * hint and the runtime still land where every other box puts them. */
429
+ const rightAlign = (th: Theme, rows: string[], inner: number, right: string): string[] => {
430
+ if (!right || rows.length === 0) return rows;
431
+ const label = th.fg("dim", right);
432
+ const avail = Math.max(4, inner - visibleWidth(label) - 1);
433
+ const head = truncateToWidth(rows[0]!, avail, "…");
434
+ const pad = Math.max(1, avail - visibleWidth(head));
435
+ return [`${head}${" ".repeat(pad)}${label}`, ...rows.slice(1)];
436
+ };
437
+
415
438
  // Status icon + third-party lines -> our box content lines.
416
439
  const boxedRows = (icon: string, lines: string[]): string[] => {
417
440
  const body = lines.filter((l) => !isBlankLine(l));
@@ -505,21 +528,50 @@ if (ToolExecutionComponent?.prototype?.render) {
505
528
  // MCP dumps the whole output on error (1-line summary instead), Chrome
506
529
  // DevTools is a bare line, and errors take our own path (pi-web-access'
507
530
  // own error box would nest a box inside a box).
508
- const theirs =
509
- own && !isErr && !cdp && !MCP_TOOL(name)
510
- ? safeCall(() => own.call(this, { content: result.content, details: result.details }, opts, th, ctx))
511
- : null;
512
531
  return new Lines((width) => {
513
532
  const soft = softSecondaryOf(th);
514
533
  const inner = Math.max(8, width - 4);
515
534
  const icon = isErr ? th.fg("error", "x") : th.fg("success", "✓");
516
- const boxed2 = componentLines(theirs, width - 6);
535
+ // Their renderer already answers `expanded`, so both states are measured: the fold
536
+ // label appears only when the other state really shows a different number of rows.
537
+ const theirsAt = (exp: boolean) =>
538
+ own && !isErr && !cdp && !MCP_TOOL(name)
539
+ ? componentLines(
540
+ safeCall(() =>
541
+ own.call(this, { content: result.content, details: result.details }, { ...opts, expanded: exp }, th, ctx),
542
+ ),
543
+ width - 6,
544
+ )
545
+ : [];
546
+ const boxed2 = theirsAt(opts.expanded);
547
+ // Only rows on screen decide: a renderer that answers `expanded` with the same visible
548
+ // height (a trailing newline, a blank row) has nothing to fold.
549
+ const rowsShown = (rows: string[]) => rows.filter((l) => !isBlankLine(l)).length;
550
+ const foldHint = rowsShown(theirsAt(!opts.expanded)) === rowsShown(boxed2) ? "" : opts.expanded ? HINT_COLLAPSE : HINT_EXPAND;
517
551
  if (fff && boxed2.length) {
518
552
  const callLines = componentLines(safeCall(() => origCall.call(this)?.call(this, ctx.args, th, ctx)), width - 6);
519
- return box(th, width, boxedRows(icon, [callLines[0]?.trim() || displayName(name), ...boxed2]));
553
+ const head = `${icon} ${callLines[0]?.trim() || th.fg("accent", displayName(name))}`;
554
+ const right = [durationOf(ctx), foldHint].filter(Boolean).join(" · ");
555
+ return box(th, width, rightAlign(th, [head, ...boxed2], inner, right));
520
556
  }
521
- if (boxed2.length && !cdp) return box(th, width, boxedRows(icon, boxed2));
522
- const more = countLines(text) > 1;
557
+ // Third-party content keeps its own template but not its own chrome: without this the
558
+ // box had no title row, so the tool name, the runtime and the fold label were all lost.
559
+ if (boxed2.length && !cdp) {
560
+ const rows = titleRow(
561
+ th,
562
+ icon,
563
+ th.fg("accent", displayName(name)),
564
+ mcpInfo(name, ctx.args, th),
565
+ inner,
566
+ durationOf(ctx),
567
+ opts.expanded,
568
+ foldHint,
569
+ );
570
+ return box(th, width, [...rows, ...boxed2]);
571
+ }
572
+ // A payload with no newline can still be a full screen of wrapped text, so the row
573
+ // count is measured at this width: a 40k-char JSON line folds like any other box.
574
+ const more = drawnRows(text, inner) > 1;
523
575
  const rows = titleRow(
524
576
  th,
525
577
  icon,
@@ -533,7 +585,11 @@ if (ToolExecutionComponent?.prototype?.render) {
533
585
  more ? (opts.expanded ? HINT_COLLAPSE : HINT_EXPAND) : "",
534
586
  );
535
587
  const summary = toolSummary(name, text, ctx.args);
536
- rows.push(`${resHead(th)}${summary ? ` ${paintLinks(summary, soft)}` : ""}`);
588
+ const summaryRow = oneRow(
589
+ `${resHead(th)}${summary ? ` ${paintLinks(summary, soft)}` : ""}`,
590
+ inner,
591
+ );
592
+ rows.push(summaryRow);
537
593
  if (opts.expanded && more) {
538
594
  // MCP args are gone once the call box is replaced by the result.
539
595
  if (MCP_TOOL(name)) rows.push(th.fg("dim", `args ${JSON.stringify(ctx.args ?? {})}`));
@@ -697,7 +753,8 @@ function minimal(
697
753
  const inner = Math.max(8, width - 4);
698
754
  // Anything the collapsed summary hides gets the standard hint; without it a
699
755
  // two-line box (edit: `+1 / -1`) looks complete while the diff sits behind it.
700
- const hasDetail = countLines(text) > 1 || Boolean((result.details as { diff?: string } | undefined)?.diff);
756
+ const hasDetail =
757
+ drawnRows(text, inner) > 1 || Boolean((result.details as { diff?: string } | undefined)?.diff);
701
758
  const label = hasDetail ? (expanded ? HINT_COLLAPSE : HINT_EXPAND) : "";
702
759
  const rows = titleRow(
703
760
  theme,
@@ -711,7 +768,7 @@ function minimal(
711
768
  );
712
769
  const resLine = res(result, theme, soft, isErr, expanded);
713
770
  if (expanded) rows.push(...full(result, theme));
714
- else if (resLine) rows.push(resLine);
771
+ else if (resLine) rows.push(oneRow(resLine, inner));
715
772
  return box(theme, width, rows);
716
773
  });
717
774
  },
@@ -1680,5 +1737,65 @@ if (isMain(import.meta.url)) {
1680
1737
  );
1681
1738
  }
1682
1739
 
1740
+
1741
+ // --- Third-party boxes carry a title row, so they can be folded like any other ----
1742
+ // pi-web-access paints its own template; without our title row the box lost the tool
1743
+ // name, the runtime and the [show] label, so the only way to expand it was ctrl+o.
1744
+ const paFolding = {
1745
+ toolName: "web_search",
1746
+ toolDefinition: {
1747
+ renderResult: (_r: unknown, o: { expanded?: boolean }) => ({
1748
+ render: () => (o.expanded ? ["a", "b", "c", "d"] : ["a"]),
1749
+ }),
1750
+ },
1751
+ };
1752
+ const paFold = (expanded: boolean) =>
1753
+ (ToolExecutionComponent.prototype.getResultRenderer.call(paFolding) as any)(
1754
+ { content: [{ type: "text", text: "raw" }], details: {} },
1755
+ { expanded, isPartial: false },
1756
+ th,
1757
+ { args: {}, state: {} },
1758
+ ).render(80) as string[];
1759
+ const paFolded = paFold(false).map(stripAnsi);
1760
+ const paOpen = paFold(true).map(stripAnsi);
1761
+ assert(paFolded[1]!.includes("search"), "third-party box: the title row names the tool");
1762
+ assert(paFolded[1]!.includes("[show]"), "third-party box: collapsed offers [show]");
1763
+ assert(paOpen[1]!.includes("[hide]"), "third-party box: expanded offers [hide]");
1764
+ assert(paOpen.length > paFolded.length, "third-party box: expanding really shows more rows");
1765
+ // A renderer that ignores `expanded` has nothing to fold, so it gets no label.
1766
+ const flat = { toolName: "web_search", toolDefinition: { renderResult: () => ({ render: () => ["only line"] }) } };
1767
+ for (const expanded of [false, true]) {
1768
+ const rows = (ToolExecutionComponent.prototype.getResultRenderer.call(flat) as any)(
1769
+ { content: [{ type: "text", text: "raw" }], details: {} },
1770
+ { expanded, isPartial: false },
1771
+ th,
1772
+ { args: {}, state: {} },
1773
+ ).render(80) as string[];
1774
+ assert(rows.every((l: string) => !l.includes("[show]") && !l.includes("[hide]")), `third-party box: nothing to fold, no label (expanded=${expanded})`);
1775
+ }
1776
+
1777
+ // --- A one-line payload that fills the screen still folds ---------------------
1778
+ // A JSON reply has no newline, so it used to count as one row: no [show], and the
1779
+ // summary row wrapped into 30 rows because nothing clipped it.
1780
+ const longJson = JSON.stringify({ results: [{ url: "https://example.com", text: "x".repeat(4000) }] });
1781
+ const longBox = resRenderer(
1782
+ { content: [{ type: "text", text: longJson }] },
1783
+ { expanded: false, isPartial: false },
1784
+ th,
1785
+ { args: {}, state: {} },
1786
+ ).render(80) as string[];
1787
+ assert(longBox.some((l: string) => l.includes("[show]")), "a wrapped one-line payload offers [show]");
1788
+ assert(longBox.length <= 6, `a folded box stays short whatever the payload (${longBox.length} rows)`);
1789
+ assert(
1790
+ longBox.every((l: string) => !l.includes("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")),
1791
+ "the summary row is one row: the payload is clipped, not wrapped",
1792
+ );
1793
+ const shortBox = resRenderer(
1794
+ { content: [{ type: "text", text: "done" }] },
1795
+ { expanded: false, isPartial: false },
1796
+ th,
1797
+ { args: {}, state: {} },
1798
+ ).render(80) as string[];
1799
+ assert(shortBox.every((l: string) => !l.includes("[show]")), "a one-row answer has nothing to fold, so no label");
1683
1800
  console.log("OK");
1684
1801
  }
@@ -429,10 +429,10 @@ if (isMain(import.meta.url)) {
429
429
  // Mirrors `soften()` (themes/gen-themes.mjs) and `softenColor` (lib/ansi.ts): the soft
430
430
  // tier is the base color at L 0.82 with a fifth of its chroma. Both sides read the
431
431
  // declared OKLCH string (not the 8-bit hex), so the comparison is not rounded twice.
432
- const softenToHex = (base: string) => {
432
+ const softenToHex = (base: string, scale = 0.35, l = 0.90) => {
433
433
  const o = parseOklch(base);
434
434
  assert(o !== null, `cannot soften a non-oklch color: ${base}`);
435
- return toHex(`oklch(82% ${oklchToRgb(0.82, o.C * 0.18, o.H).chroma} ${o.H})`);
435
+ return toHex(`oklch(${+(l * 100).toFixed(1)}% ${oklchToRgb(l, o.C * scale, o.H).chroma} ${o.H})`);
436
436
  };
437
437
  // The built-in theme adds fallbacks (scrollbarTrack<-muted, thinkingMax<-thinkingXhigh),
438
438
  // so the fg map must be complete; take the whole palette like pi's loader does.
@@ -477,10 +477,10 @@ if (isMain(import.meta.url)) {
477
477
  // Non-text keys (backgrounds/borders/scrollbar) — contrast is not measured.
478
478
  const nonText = new Set([
479
479
  "selectedBg", "userMessageBg", "customMessageBg", "toolPendingBg", "toolSuccessBg", "toolErrorBg",
480
- "searchMatchBg", "border", "borderAccent", "borderMuted", "mdCodeBlockBorder", "mdQuoteBorder",
480
+ "searchMatchBg", "border", "borderAccent", "borderMuted", "mdCodeBlockBorder", "mdQuoteBorder", "mdHr",
481
481
  "scrollbarTrack", "scrollbarThumb",
482
482
  ]);
483
- const secondary = new Set(["muted", "dim", "mdQuote", "mdHr", "mdLinkUrl", "toolOutput", "toolDiffContext", "thinkingText", "syntaxComment"]);
483
+ const secondary = new Set(["muted", "dim", "mdQuote", "mdLinkUrl", "toolOutput", "toolDiffContext", "thinkingText", "syntaxComment"]);
484
484
  // Thinking ramp: the higher the more readable; thinkingMax = accent (measured on the pill path).
485
485
  const ramp: Record<string, number> = {
486
486
  thinkingOff: 1.3, thinkingMinimal: 1.3, thinkingLow: 2.0,
@@ -552,7 +552,7 @@ if (isMain(import.meta.url)) {
552
552
  // The declared soft vars must equal what the extensions derive at runtime, or the
553
553
  // footer label colour drifts from the theme file. `softenToHex` mirrors
554
554
  // `soften()` in gen-themes.mjs and `softenColor` in lib/ansi.ts.
555
- for (const [softName, baseName] of [["accentSoft", "accent"], ["secondarySoft", "borderAccent"]] as const) {
555
+ for (const [softName, baseName] of [["accentSoft", "accent"], ["secondarySoft", "secondary"]] as const) {
556
556
  const base = d.vars[d.colors[baseName] ?? baseName]!;
557
557
  const declared = toHex(d.vars[softName]!);
558
558
  const derived = softenToHex(base);