@ftrdotdev/pi-tui 0.5.1 → 0.6.0

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.
@@ -1,7 +1,7 @@
1
1
  import { assert, it } from "@effect/vitest";
2
2
  import type {
3
3
  ExtensionAPI,
4
- ExtensionContext,
4
+ ExtensionToolContext,
5
5
  Theme,
6
6
  ToolDefinition,
7
7
  } from "@earendil-works/pi-coding-agent";
@@ -45,6 +45,7 @@ const encode = (raw: string) => new TextEncoder().encode(raw);
45
45
  * `getPngDimensions` only inspects the signature and the width/height words at
46
46
  * offsets 16..23, so this suffices to exercise the dimension detection path.
47
47
  */
48
+ // prettier-ignore
48
49
  const PNG_BYTES = new Uint8Array([
49
50
  0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, // PNG signature
50
51
  0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, // IHDR chunk header
@@ -242,9 +243,7 @@ function makeMemFs(
242
243
  );
243
244
  const fs: Partial<FileSystem.FileSystem> = {
244
245
  readFile: (path) =>
245
- files.has(path)
246
- ? Effect.succeed(files.get(path)!)
247
- : notFound("readFile")(path),
246
+ files.has(path) ? Effect.succeed(files.get(path)!) : notFound("readFile")(path),
248
247
  readFileString: (path) =>
249
248
  files.has(path)
250
249
  ? Effect.succeed(new TextDecoder().decode(files.get(path)!))
@@ -343,7 +342,11 @@ it("mimeFromPath maps supported image extensions to their MIME type", () => {
343
342
 
344
343
  it("bytesToBase64 round-trips through atob for ASCII and binary bytes", () => {
345
344
  assert.strictEqual(bytesToBase64(encode("abc")), "YWJj");
346
- const round = new Uint8Array(atob(bytesToBase64(PNG_BYTES)).split("").map((c) => c.charCodeAt(0)));
345
+ const round = new Uint8Array(
346
+ atob(bytesToBase64(PNG_BYTES))
347
+ .split("")
348
+ .map((c) => c.charCodeAt(0)),
349
+ );
347
350
  assert.deepStrictEqual(round, PNG_BYTES);
348
351
  assert.strictEqual(bytesToBase64(PNG_BYTES), PNG_BASE64);
349
352
  });
@@ -385,10 +388,12 @@ it.effect("read returns an AVIF image record with parsed dimensions", () =>
385
388
  }),
386
389
  );
387
390
 
388
-
389
391
  it.effect("read routes jpeg/gif/webp extensions to image records", () =>
390
392
  Effect.gen(function* () {
391
- const mem = makeMemFs({}, { "/a/x.jpg": PNG_BYTES, "/a/y.gif": PNG_BYTES, "/a/z.webp": PNG_BYTES });
393
+ const mem = makeMemFs(
394
+ {},
395
+ { "/a/x.jpg": PNG_BYTES, "/a/y.gif": PNG_BYTES, "/a/z.webp": PNG_BYTES },
396
+ );
392
397
  const jpeg = yield* runRead(mem, "/a/x.jpg");
393
398
  const gif = yield* runRead(mem, "/a/y.gif");
394
399
  const webp = yield* runRead(mem, "/a/z.webp");
@@ -491,6 +496,7 @@ const theme = {
491
496
  fg: (_color: string, text: string) => text,
492
497
  bg: (_color: string, text: string) => text,
493
498
  bold: (text: string) => text,
499
+ style: (text: string) => text,
494
500
  } as unknown as Theme;
495
501
 
496
502
  class FakePi {
@@ -548,7 +554,7 @@ it("the preview tool registers with the preview name and a path parameter", () =
548
554
 
549
555
  it("tool execute returns the exact confirmation stub and the display record for a readable file", async () => {
550
556
  const tool = registerTool(makeMemFs({ "/cwd/src/file.md": "# Title" }));
551
- const ctx = { cwd: "/cwd" } as ExtensionContext;
557
+ const ctx = { cwd: "/cwd" } as ExtensionToolContext;
552
558
  const result = await tool.execute("call-1", { path: "src/file.md" }, undefined, undefined, ctx);
553
559
  assert.strictEqual(
554
560
  (result.content[0] as { type: "text"; text: string }).text,
@@ -565,7 +571,7 @@ it("tool execute returns the exact confirmation stub and the display record for
565
571
 
566
572
  it("tool execute keeps the model stub for images and returns an image detail record", async () => {
567
573
  const tool = registerTool(makeMemFs({}, { "/cwd/pic.png": PNG_BYTES }));
568
- const ctx = { cwd: "/cwd" } as ExtensionContext;
574
+ const ctx = { cwd: "/cwd" } as ExtensionToolContext;
569
575
  const result = await tool.execute("call-1", { path: "pic.png" }, undefined, undefined, ctx);
570
576
  assert.strictEqual(
571
577
  (result.content[0] as { type: "text"; text: string }).text,
@@ -586,7 +592,7 @@ it("tool execute keeps the model stub for images and returns an image detail rec
586
592
 
587
593
  it("tool execute returns only the text stub for text files (no image block)", async () => {
588
594
  const tool = registerTool(makeMemFs({ "/cwd/file.md": "# Title" }));
589
- const ctx = { cwd: "/cwd" } as ExtensionContext;
595
+ const ctx = { cwd: "/cwd" } as ExtensionToolContext;
590
596
  const result = await tool.execute("call-1", { path: "file.md" }, undefined, undefined, ctx);
591
597
  assert.strictEqual(result.content.length, 1);
592
598
  assert.strictEqual((result.content[0] as { type: string }).type, "text");
@@ -594,14 +600,14 @@ it("tool execute returns only the text stub for text files (no image block)", as
594
600
 
595
601
  it("tool execute resolves relative paths against the tool context cwd", async () => {
596
602
  const tool = registerTool(makeMemFs({ "/cwd/src/file.md": "# Title" }));
597
- const ctx = { cwd: "/cwd" } as ExtensionContext;
603
+ const ctx = { cwd: "/cwd" } as ExtensionToolContext;
598
604
  const result = await tool.execute("call-1", { path: "src/file.md" }, undefined, undefined, ctx);
599
605
  assert.strictEqual(result.details.path, "/cwd/src/file.md");
600
606
  });
601
607
 
602
608
  it("tool execute returns the exact failure stub and error details when the read fails", async () => {
603
609
  const tool = registerTool();
604
- const ctx = { cwd: "/cwd" } as ExtensionContext;
610
+ const ctx = { cwd: "/cwd" } as ExtensionToolContext;
605
611
  const result = await tool.execute("call-1", { path: "missing.md" }, undefined, undefined, ctx);
606
612
  const expectedMessage = PlatformError.systemError({
607
613
  _tag: "NotFound",
@@ -626,7 +632,7 @@ it("tool execute returns the exact failure stub and error details when the read
626
632
 
627
633
  it("tool execute reports the failure stub when the reference is unresolvable", async () => {
628
634
  const tool = registerTool();
629
- const ctx = { cwd: "/cwd" } as ExtensionContext;
635
+ const ctx = { cwd: "/cwd" } as ExtensionToolContext;
630
636
  const result = await tool.execute(
631
637
  "call-1",
632
638
  { path: "file://host/path" },
@@ -645,20 +651,38 @@ it("tool execute reports the failure stub when the reference is unresolvable", a
645
651
  });
646
652
 
647
653
  it("renderBodyComponent renders markdown content with the Markdown component when the language hint is markdown", () => {
648
- const data = { kind: "text" as const, path: "/a.md", content: "# Hi", lang: "markdown", truncated: false };
654
+ const data = {
655
+ kind: "text" as const,
656
+ path: "/a.md",
657
+ content: "# Hi",
658
+ lang: "markdown",
659
+ truncated: false,
660
+ };
649
661
  const out = renderBodyComponent(data, true, theme);
650
662
  assert.instanceOf(out, Markdown);
651
663
  });
652
664
 
653
665
  it("renderBodyComponent renders code content with the Text component when the language hint is not markdown", () => {
654
- const data = { kind: "text" as const, path: "/a.ts", content: "const x = 1;", lang: "typescript", truncated: false };
666
+ const data = {
667
+ kind: "text" as const,
668
+ path: "/a.ts",
669
+ content: "const x = 1;",
670
+ lang: "typescript",
671
+ truncated: false,
672
+ };
655
673
  const out = renderBodyComponent(data, true, theme);
656
674
  assert.instanceOf(out, Text);
657
675
  });
658
676
 
659
677
  it("renderBodyComponent collapses code content to the collapsed line count when not expanded", () => {
660
678
  const content = Array.from({ length: 45 }, (_, i) => `line ${i + 1}`).join("\n");
661
- const data = { kind: "text" as const, path: "/a.ts", content, lang: "typescript", truncated: false };
679
+ const data = {
680
+ kind: "text" as const,
681
+ path: "/a.ts",
682
+ content,
683
+ lang: "typescript",
684
+ truncated: false,
685
+ };
662
686
  const out = renderBodyComponent(data, false, theme);
663
687
  assert.instanceOf(out, Text);
664
688
  assert.strictEqual(
@@ -668,7 +692,13 @@ it("renderBodyComponent collapses code content to the collapsed line count when
668
692
  });
669
693
 
670
694
  it("renderBodyComponent appends the truncated note for a truncated expanded record", () => {
671
- const data = { kind: "text" as const, path: "/a.md", content: "# Hi", lang: "markdown", truncated: true };
695
+ const data = {
696
+ kind: "text" as const,
697
+ path: "/a.md",
698
+ content: "# Hi",
699
+ lang: "markdown",
700
+ truncated: true,
701
+ };
672
702
  const out = renderBodyComponent(data, true, theme);
673
703
  assert.instanceOf(out, Markdown);
674
704
  assert.strictEqual(out.render(200).join("\n").includes("... (truncated preview)"), true);
@@ -676,7 +706,13 @@ it("renderBodyComponent appends the truncated note for a truncated expanded reco
676
706
 
677
707
  it("renderBodyComponent appends the more-lines note for a collapsed markdown record", () => {
678
708
  const content = Array.from({ length: 41 }, (_, i) => `line ${i + 1}`).join("\n");
679
- const data = { kind: "text" as const, path: "/a.md", content, lang: "markdown", truncated: false };
709
+ const data = {
710
+ kind: "text" as const,
711
+ path: "/a.md",
712
+ content,
713
+ lang: "markdown",
714
+ truncated: false,
715
+ };
680
716
  const out = renderBodyComponent(data, false, theme);
681
717
  assert.instanceOf(out, Markdown);
682
718
  assert.strictEqual(
@@ -777,7 +813,14 @@ it("renderResult renders the warning box for an error result", () => {
777
813
  const tool = registerTool();
778
814
  const result = {
779
815
  content: [],
780
- details: { kind: "text", path: "/x", content: "", lang: undefined, truncated: false, error: "boom" },
816
+ details: {
817
+ kind: "text",
818
+ path: "/x",
819
+ content: "",
820
+ lang: undefined,
821
+ truncated: false,
822
+ error: "boom",
823
+ },
781
824
  } as any;
782
825
  const out = tool.renderResult!(result, { expanded: true, isPartial: false }, theme, {} as any);
783
826
  assert.ok(isComponent(out));
@@ -787,7 +830,13 @@ it("renderResult renders the warning box for an error result", () => {
787
830
 
788
831
  it("renderResult renders the body in a bordered box for a success result", () => {
789
832
  const tool = registerTool();
790
- const record = { kind: "text", path: "/a.md", content: "# Hi", lang: "markdown", truncated: false };
833
+ const record = {
834
+ kind: "text",
835
+ path: "/a.md",
836
+ content: "# Hi",
837
+ lang: "markdown",
838
+ truncated: false,
839
+ };
791
840
  const out = tool.renderResult!(
792
841
  { content: [], details: record } as any,
793
842
  { expanded: true, isPartial: false },
@@ -877,7 +926,13 @@ it("the preview command appends the display record as a custom entry on success"
877
926
  assert.deepStrictEqual(pi.entries, [
878
927
  {
879
928
  type: "preview",
880
- data: { kind: "text", path: "/cwd/file.md", content: "# Hi", lang: "markdown", truncated: false },
929
+ data: {
930
+ kind: "text",
931
+ path: "/cwd/file.md",
932
+ content: "# Hi",
933
+ lang: "markdown",
934
+ truncated: false,
935
+ },
881
936
  },
882
937
  ]);
883
938
  assert.strictEqual(calls.length, 0);
@@ -932,7 +987,15 @@ it("the entry renderer renders the path header and the body inside a bordered bo
932
987
  registerPreview(pi as unknown as ExtensionAPI, makePreviewContext(makeMemFs()));
933
988
  const renderer = pi.renderers.find((r) => r.type === "preview")!.renderer;
934
989
  const out = renderer(
935
- { data: { kind: "text", path: "/abs/a.md", content: "# Hi", lang: "markdown", truncated: false } },
990
+ {
991
+ data: {
992
+ kind: "text",
993
+ path: "/abs/a.md",
994
+ content: "# Hi",
995
+ lang: "markdown",
996
+ truncated: false,
997
+ },
998
+ },
936
999
  { expanded: true },
937
1000
  theme,
938
1001
  );
@@ -973,9 +1036,14 @@ it("the entry renderer draws image entries as caption box + spacer + bare kitty
973
1036
  const boxRows = rendered.split("\n");
974
1037
  assert.strictEqual(boxRows.includes(""), true);
975
1038
  // …then the kitty transmission on its own line (no rail prefix, no box glyph before \x1b_G).
976
- assert.strictEqual(lines.some((l: string) => l.startsWith("\x1b_G")), true);
977
1039
  assert.strictEqual(
978
- lines.some((l: string) => l.includes("│ \x1b_G") || l.includes("╭ \x1b_G") || /[╭─╰╯]\x1b_G/.test(l)),
1040
+ lines.some((l: string) => l.startsWith("\x1b_G")),
1041
+ true,
1042
+ );
1043
+ assert.strictEqual(
1044
+ lines.some(
1045
+ (l: string) => l.includes("│ \x1b_G") || l.includes("╭ \x1b_G") || /[╭─╰╯]\x1b_G/.test(l),
1046
+ ),
979
1047
  false,
980
1048
  );
981
1049
  } finally {
package/src/ui/preview.ts CHANGED
@@ -431,13 +431,13 @@ function makePreviewComponent(data: PreviewData, expanded: boolean, theme: Theme
431
431
  ? `${data.path} (${data.dimensions.widthPx}x${data.dimensions.heightPx})`
432
432
  : data.path;
433
433
  return makeBorderedBox(new Text(theme.fg("accent", caption), 0, 0), theme, {
434
- label: theme.fg("accent", theme.bold("preview")),
435
- bg: (s) => theme.bg("customMessageBg", s),
434
+ label: theme.style("preview", { fg: "accent", bold: true }),
435
+ bg: "customMessageBg",
436
436
  });
437
437
  }
438
438
  return makeBorderedBox(renderBodyComponent(data, expanded, theme), theme, {
439
- label: theme.fg("accent", theme.bold("preview")),
440
- bg: (s) => theme.bg("customMessageBg", s),
439
+ label: theme.style("preview", { fg: "accent", bold: true }),
440
+ bg: "customMessageBg",
441
441
  });
442
442
  }
443
443
 
@@ -527,8 +527,8 @@ export function registerPreview(
527
527
 
528
528
  renderCall(args, theme) {
529
529
  return makeBorderedBox(new Text(args.path, 0, 0), theme, {
530
- label: theme.fg("toolTitle", theme.bold("preview")),
531
- bg: (s) => theme.bg("customMessageBg", s),
530
+ label: theme.style("preview", { fg: "toolTitle", bold: true }),
531
+ bg: "customMessageBg",
532
532
  });
533
533
  },
534
534
 
@@ -538,9 +538,9 @@ export function registerPreview(
538
538
  if (!data) return context.lastComponent ?? new Text("", 0, 0);
539
539
  if (data.error) {
540
540
  return makeBorderedBox(new Text(`preview failed: ${data.error}`, 0, 0), theme, {
541
- label: theme.fg("warning", theme.bold("preview")),
541
+ label: theme.style("preview", { fg: "warning", bold: true }),
542
542
  color: "warning",
543
- bg: (s) => theme.bg("customMessageBg", s),
543
+ bg: "customMessageBg",
544
544
  });
545
545
  }
546
546
  return makePreviewComponent(data, options.expanded, theme);
@@ -556,9 +556,9 @@ export function registerPreview(
556
556
  const noData = new Container();
557
557
  noData.addChild(new Text(theme.fg("warning", "[preview] no data"), 0, 0));
558
558
  return makeBorderedBox(noData, theme, {
559
- label: theme.fg("warning", theme.bold("preview")),
559
+ label: theme.style("preview", { fg: "warning", bold: true }),
560
560
  color: "warning",
561
- bg: (s) => theme.bg("customMessageBg", s),
561
+ bg: "customMessageBg",
562
562
  });
563
563
  }
564
564
  // Image entries mirror pi's native tool-image layout exactly: the bordered
@@ -585,8 +585,8 @@ export function registerPreview(
585
585
  body.addChild(new Text(theme.fg("accent", data.path), 0, 0));
586
586
  body.addChild(renderBodyComponent(data, expanded, theme));
587
587
  return makeBorderedBox(body, theme, {
588
- label: theme.fg("accent", theme.bold("preview")),
589
- bg: (s) => theme.bg("customMessageBg", s),
588
+ label: theme.style("preview", { fg: "accent", bold: true }),
589
+ bg: "customMessageBg",
590
590
  });
591
591
  });
592
592
 
@@ -0,0 +1,25 @@
1
+ import { assert, it } from "@effect/vitest";
2
+ import type { Theme } from "@earendil-works/pi-coding-agent";
3
+ import { renderBar, renderRuntimeSegment } from "./render-helpers.ts";
4
+
5
+ // Identity theme: styling is a no-op so assertions read the plain text.
6
+ const theme = {
7
+ fg: (_color: string, text: string) => text,
8
+ bg: (_color: string, text: string) => text,
9
+ bold: (text: string) => text,
10
+ } as unknown as Theme;
11
+
12
+ it("renderBar fills by rounded percentage in ascii and unicode", () => {
13
+ assert.strictEqual(renderBar(theme, 50, 10, true), "[#####-----]");
14
+ assert.strictEqual(renderBar(theme, 0, 4, false), "[░░░░]");
15
+ assert.strictEqual(renderBar(theme, 100, 4, true), "[####]");
16
+ assert.strictEqual(renderBar(theme, 150, 4, true), "[####]");
17
+ });
18
+
19
+ it("renderRuntimeSegment joins every runtime with a dim separator", () => {
20
+ assert.strictEqual(renderRuntimeSegment(theme, [], "ascii"), "");
21
+ assert.strictEqual(
22
+ renderRuntimeSegment(theme, [{ name: "nodejs", version: "1.2.3" }, { name: "rust" }], "ascii"),
23
+ "node 1.2.3 · rs",
24
+ );
25
+ });
@@ -159,19 +159,22 @@ export function renderGitSegment(
159
159
  }
160
160
 
161
161
  /**
162
- * The runtime footer segment: a runtime symbol (ascii or nerd per the
163
- * icon mode) plus its version when known. Returns `""` for no runtime.
162
+ * The runtime footer segment: one symbol (ascii or nerd per the icon mode)
163
+ * per matching runtime, each with its version when known. Multiple runtimes
164
+ * are joined with a dim separator. Returns `""` for no runtime.
164
165
  */
165
166
  export function renderRuntimeSegment(
166
167
  theme: Theme,
167
- runtime: RuntimeInfo | null,
168
+ runtimes: readonly RuntimeInfo[],
168
169
  iconMode: TuiConfig["icons"]["mode"],
169
170
  ): string {
170
- if (!runtime) return "";
171
- const symbol = theme.fg("success", runtimeSymbol(runtime.name, iconMode));
172
- const version = runtime.version ? theme.fg("muted", runtime.version) : "";
173
- const label = [symbol, version].filter(Boolean).join(" ");
174
- return label;
171
+ return runtimes
172
+ .map((runtime) => {
173
+ const symbol = theme.fg("success", runtimeSymbol(runtime.name, iconMode));
174
+ const version = runtime.version ? theme.fg("muted", runtime.version) : "";
175
+ return [symbol, version].filter(Boolean).join(" ");
176
+ })
177
+ .join(theme.fg("dim", " · "));
175
178
  }
176
179
 
177
180
  /**
@@ -189,7 +192,9 @@ export interface TimerSegmentParts {
189
192
  function renderSplit(theme: Theme, glyphs: IconGlyphs, inference: number, tool: number): string {
190
193
  const buckets: string[] = [];
191
194
  if (inference > 0) {
192
- buckets.push(`${theme.fg("dim", glyphs.inference)} ${theme.fg("accent", formatDuration(inference))}`);
195
+ buckets.push(
196
+ `${theme.fg("dim", glyphs.inference)} ${theme.fg("accent", formatDuration(inference))}`,
197
+ );
193
198
  }
194
199
  if (tool > 0) {
195
200
  buckets.push(`${theme.fg("dim", glyphs.tool)} ${theme.fg("accent", formatDuration(tool))}`);