@ftrdotdev/pi-tui 0.8.0 → 0.9.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.
Files changed (76) hide show
  1. package/CHANGELOG.md +225 -0
  2. package/README.md +5 -0
  3. package/package.json +18 -15
  4. package/src/activity-service.test.ts +97 -0
  5. package/src/activity.test.ts +1 -1
  6. package/src/activity.ts +38 -0
  7. package/src/chrome/budget.test.ts +345 -0
  8. package/src/chrome/budget.ts +220 -0
  9. package/src/chrome/declarations.ts +303 -0
  10. package/src/chrome/facts.test.ts +165 -0
  11. package/src/chrome/facts.ts +267 -0
  12. package/src/chrome/frame.ts +45 -0
  13. package/src/chrome/laws.test.ts +231 -0
  14. package/src/chrome/port.test.ts +164 -0
  15. package/src/chrome/port.ts +67 -0
  16. package/src/chrome/service-provider.test.ts +383 -0
  17. package/src/chrome/service-provider.ts +55 -0
  18. package/src/commands/git-execution.test.ts +5 -2
  19. package/src/commands/git-execution.ts +27 -15
  20. package/src/commands/git-status.test.ts +47 -15
  21. package/src/commands/git-status.ts +83 -31
  22. package/src/components/bordered-box.test.ts +12 -15
  23. package/src/components/bordered-box.ts +11 -7
  24. package/src/components/define-component.ts +1 -1
  25. package/src/components/index.ts +7 -0
  26. package/src/components/row-list.test.ts +310 -0
  27. package/src/components/row-list.ts +272 -0
  28. package/src/config.test.ts +118 -3
  29. package/src/config.ts +46 -40
  30. package/src/frame/box-frame.test.ts +53 -7
  31. package/src/harness-policy.test.ts +47 -0
  32. package/src/icons.test.ts +19 -0
  33. package/src/icons.ts +50 -31
  34. package/src/index.test.ts +135 -0
  35. package/src/index.ts +118 -119
  36. package/src/layers.test.ts +94 -0
  37. package/src/layers.ts +92 -0
  38. package/src/runtime-service.test.ts +218 -0
  39. package/src/runtime-service.ts +103 -0
  40. package/src/runtime.test.ts +165 -66
  41. package/src/runtime.ts +127 -93
  42. package/src/session-lifecycle-service.test.ts +95 -0
  43. package/src/session-lifecycle.test.ts +77 -0
  44. package/src/session-lifecycle.ts +21 -0
  45. package/src/state.test.ts +132 -3
  46. package/src/state.ts +43 -13
  47. package/src/telemetry-service.test.ts +250 -0
  48. package/src/telemetry.test.ts +322 -0
  49. package/src/telemetry.ts +130 -23
  50. package/src/ui/editor-tint.test.ts +58 -45
  51. package/src/ui/editor-tint.ts +1 -1
  52. package/src/ui/editor.ts +117 -53
  53. package/src/ui/footer.test.ts +349 -120
  54. package/src/ui/footer.ts +147 -124
  55. package/src/ui/header.test.ts +203 -223
  56. package/src/ui/header.ts +32 -96
  57. package/src/ui/preview.test.ts +73 -70
  58. package/src/ui/preview.ts +48 -33
  59. package/src/ui/render-helpers.test.ts +82 -13
  60. package/src/ui/render-helpers.ts +110 -122
  61. package/src/ui/settings-command.test.ts +70 -11
  62. package/src/ui/settings-command.ts +30 -45
  63. package/src/ui/theme-color.test.ts +4 -3
  64. package/src/usage-totals-service.test.ts +230 -0
  65. package/src/usage-totals-service.ts +59 -0
  66. package/src/utils.test.ts +46 -50
  67. package/src/utils.ts +41 -239
  68. package/src/vim/editor-host.test.ts +201 -75
  69. package/src/vim/editor-state.ts +2 -2
  70. package/src/vim/mode-indicator.test.ts +6 -5
  71. package/src/vim/mode-indicator.ts +13 -9
  72. package/src/vim/selection-render.ts +8 -4
  73. package/src/vim/text-model.test.ts +52 -14
  74. package/src/vim/text-model.ts +1 -1
  75. package/src/vim/vim-router.test.ts +3 -0
  76. package/src/vim/vim-router.ts +1 -1
package/src/ui/header.ts CHANGED
@@ -1,23 +1,10 @@
1
- import {
2
- VERSION,
3
- type ExtensionAPI,
4
- type ExtensionContext,
5
- type Theme,
6
- } from "@earendil-works/pi-coding-agent";
1
+ import { VERSION, type ExtensionContext, type Theme } from "@earendil-works/pi-coding-agent";
7
2
  import { Context, Effect, Layer } from "effect";
8
3
  import { makeBorderedBox } from "../components/bordered-box.ts";
9
4
  import { defineComponent, type DisposableComponent } from "../components/define-component.ts";
10
- import { renderLogo, twoColumn } from "./render-helpers.ts";
11
- import {
12
- center,
13
- collectPiCommandNames,
14
- formatCwd,
15
- formatModelLabel,
16
- formatThinkingLabel,
17
- headerColumnWidths,
18
- padRight,
19
- pickSlashCommandTips,
20
- } from "../utils.ts";
5
+ import { renderLogo } from "./render-helpers.ts";
6
+ import { center } from "../utils.ts";
7
+ import type { ChromePort } from "../chrome/port.ts";
21
8
 
22
9
  /**
23
10
  * Process-wide flag: the raw screen clear runs at most once per launch.
@@ -46,7 +33,7 @@ export function resetScreenClearMemory(): void {
46
33
  function clearVisibleScreenOnce(): void {
47
34
  if (!process.stdout.isTTY) return;
48
35
  const globals = globalThis as ScreenClearedGlobal;
49
- if (globals[SCREEN_CLEARED_KEY]) return;
36
+ if (globals[SCREEN_CLEARED_KEY] ?? false) return;
50
37
  globals[SCREEN_CLEARED_KEY] = true;
51
38
  process.stdout.write("\x1b[2J\x1b[H");
52
39
  }
@@ -54,96 +41,47 @@ function clearVisibleScreenOnce(): void {
54
41
  /**
55
42
  * Pi header widget (`setHeader`), framed in the house rounded box.
56
43
  *
57
- * The frame (top border with the `Pi vVERSION` label, the `│` rails, the
58
- * bottom border) is rendered by {@link makeBorderedBox} so every view in
59
- * this package shares one border implementation. Only the body — logo, title,
60
- * model/effort/cwd column, and the slash-command tips — is composed here.
61
- * The header keeps the body rail-flush (`paddingX: 0`) to preserve a
62
- * zero-gap look between content and rails.
44
+ * The header is a deterministic brand box: the logo and the title inside the
45
+ * shared frame, with `Pi vVERSION` in the border label. Model identity,
46
+ * thinking level and cwd belong to the footer; the header declares only its
47
+ * version fact, which `buildBoxFrame` omits when it cannot fit.
63
48
  *
64
49
  * Construction follows the house service pattern (preview.ts): the service
65
50
  * builds through a `Layer.effect` factory, its render is a named synchronous
66
- * total effect, tips are computed once at service build from the command pool
67
- * and never re-read, and the closure component handed to pi runs the render
68
- * effect with `Effect.runSync` per invocation.
51
+ * total effect, and the closure component handed to pi runs the render effect
52
+ * with `Effect.runSync` per invocation.
69
53
  */
70
54
  export class HeaderRenderService extends Context.Service<
71
55
  HeaderRenderService,
72
- { readonly render: (width: number) => Effect.Effect<string[]> }
73
- >()("tui/header/HeaderRenderService") {
74
- static make(pi: ExtensionAPI, ctx: ExtensionContext): Layer.Layer<HeaderRenderService> {
56
+ { readonly render: (width: number) => Effect.Effect<readonly string[]> }
57
+ >()("@ftrdotdev/pi-tui/ui/header/HeaderRenderService") {
58
+ static make(port: ChromePort): Layer.Layer<HeaderRenderService> {
75
59
  return Layer.effect(
76
60
  HeaderRenderService,
77
61
  Effect.gen(function* () {
78
- // Construction-once tips (preserves the former constructor semantics):
79
- // computed from `pi.getCommands()` at service build, never re-read.
80
- const tipCommands = yield* pickSlashCommandTips(collectPiCommandNames(pi.getCommands()), {
81
- fixed: ["tui"],
82
- count: 3,
83
- });
84
-
85
- // Named synchronous total step: the body column composition, rendered
86
- // at `innerWidth` because the frame owns the two rail columns.
87
- const renderBody = (
88
- theme: Theme,
89
- _innerWidth: number,
90
- leftWidth: number,
91
- rightWidth: number,
92
- useTips: boolean,
93
- ) =>
62
+ // Keep the factory generator-shaped; the layer shape is plan 2's seam.
63
+ yield* Effect.void;
64
+ // Named synchronous total step: the centered brand body, rendered at
65
+ // `innerWidth` because the frame owns the two rail columns.
66
+ const renderBody = (theme: Theme, innerWidth: number) =>
94
67
  Effect.sync(function () {
95
68
  const paint = (s: string) => theme.fg("accent", s);
96
- const muted = (s: string) => theme.fg("muted", s);
97
- const dim = (s: string) => theme.fg("dim", s);
98
69
  const bold = (s: string) => theme.bold(s);
99
-
100
- const leftLines = [
101
- ...renderLogo(paint).map((line) => center(line, leftWidth)),
102
- center(bold("Let's make some Pi(e)"), leftWidth),
103
- center(
104
- muted(
105
- `${formatModelLabel(ctx.model)} · ${formatThinkingLabel(pi.getThinkingLevel())}`,
106
- ),
107
- leftWidth,
108
- ),
109
- center(dim(formatCwd(ctx.cwd)), leftWidth),
70
+ return [
71
+ ...renderLogo(paint).map((line) => center(line, innerWidth)),
72
+ center(bold("Let's make some Pi(e)"), innerWidth),
110
73
  ];
111
-
112
- const tipDivider = paint("─".repeat(Math.max(8, Math.min(rightWidth, 22))));
113
- const [cmd0 = "", cmd1 = "", cmd2 = "", cmd3 = ""] = tipCommands;
114
- const tipLines = [
115
- "",
116
- paint(bold("Welcome")),
117
- muted("Ask Pi anything"),
118
- tipDivider,
119
- paint(bold("Commands")),
120
- muted(cmd0),
121
- muted(cmd1),
122
- muted(cmd2),
123
- muted(cmd3),
124
- "",
125
- ];
126
-
127
- const rows: string[] = [];
128
- for (let i = 0; i < leftLines.length; i++) {
129
- const content = useTips
130
- ? twoColumn(leftLines[i] ?? "", tipLines[i] ?? "", leftWidth, rightWidth, paint)
131
- : padRight(leftLines[i] ?? "", leftWidth);
132
- rows.push(content);
133
- }
134
- return rows;
135
74
  });
136
75
 
137
76
  const render = Effect.fn("HeaderRenderService.render")(function* (width: number) {
138
- const theme = ctx.ui.theme; // per-render live read
77
+ const theme = port.facts.theme(); // per-render live read
139
78
  const paint = (s: string) => theme.fg("accent", s);
140
79
 
141
80
  if (width < 24) return [paint(`Pi v${VERSION}`)]; // short-circuit
142
81
 
143
82
  const innerWidth = width - 2;
144
- const { leftWidth, rightWidth, useTips } = headerColumnWidths(innerWidth);
145
- const bodyLines = yield* renderBody(theme, innerWidth, leftWidth, rightWidth, useTips);
146
- // The body component renders the precomputed column lines;
83
+ const bodyLines = yield* renderBody(theme, innerWidth);
84
+ // The body component renders the precomputed centered lines;
147
85
  // makeBorderedBox pads them into the frame rails.
148
86
  const body = defineComponent({
149
87
  render: () => bodyLines,
@@ -164,11 +102,11 @@ export class HeaderRenderService extends Context.Service<
164
102
  /**
165
103
  * Register the header widget with `ctx.ui.setHeader` and return a cleanup
166
104
  * that unregisters it. The mount factory builds the service layer
167
- * synchronously at mount (tips drawn once from `pi.getCommands()`),
168
- * disposes the previous mount on re-mount, and hands pi a closure
169
- * component whose `render` runs the service's render effect synchronously.
105
+ * synchronously at mount, disposes the previous mount on re-mount, and hands
106
+ * pi a closure component whose `render` runs the service's render effect
107
+ * synchronously.
170
108
  */
171
- export function installHeader(pi: ExtensionAPI, ctx: ExtensionContext): () => void {
109
+ export function installHeader(ctx: ExtensionContext, port: ChromePort): () => void {
172
110
  let header: DisposableComponent | undefined;
173
111
  ctx.ui.setHeader((tui) => {
174
112
  // Regular mode keeps the terminal's scrollback, so the first mount clears
@@ -177,13 +115,11 @@ export function installHeader(pi: ExtensionAPI, ctx: ExtensionContext): () => vo
177
115
  // re-mount from desyncing pi-tui's differential renderer.
178
116
  if (tui.mode === "regular") clearVisibleScreenOnce();
179
117
  // Re-mount: dispose the previous instance before building the new one.
180
- if (header?.dispose) header.dispose();
118
+ if (header?.dispose !== undefined) header.dispose();
181
119
  // Build the service layer and run construction synchronously at mount.
182
120
  const svc = Context.get(
183
121
  Effect.runSync(
184
- Effect.context<HeaderRenderService>().pipe(
185
- Effect.provide(HeaderRenderService.make(pi, ctx)),
186
- ),
122
+ Effect.context<HeaderRenderService>().pipe(Effect.provide(HeaderRenderService.make(port))),
187
123
  ),
188
124
  HeaderRenderService,
189
125
  );
@@ -195,7 +131,7 @@ export function installHeader(pi: ExtensionAPI, ctx: ExtensionContext): () => vo
195
131
  return header;
196
132
  });
197
133
  return () => {
198
- if (header?.dispose) header.dispose();
134
+ if (header?.dispose !== undefined) header.dispose();
199
135
  header = undefined;
200
136
  ctx.ui.setHeader(undefined);
201
137
  };
@@ -1,10 +1,10 @@
1
+ // Each test is its own entry point: it builds a fresh layer for one scenario and
2
+ // runs its effects inside it, so providing there is the pattern the rule allows.
3
+ /* oxlint-disable effecttsgo/strict-effect-provide */
1
4
  import { assert, it } from "@effect/vitest";
2
- import type {
3
- ExtensionAPI,
4
- ExtensionToolContext,
5
- Theme,
6
- ToolDefinition,
7
- } from "@earendil-works/pi-coding-agent";
5
+ import { themeDouble } from "@ftrdotdev/pi-test-kit";
6
+ import { extensionApiDouble } from "../../test-support/doubles.ts";
7
+ import type { ExtensionToolContext, ToolDefinition } from "@earendil-works/pi-coding-agent";
8
8
  import { initTheme } from "@earendil-works/pi-coding-agent";
9
9
  import {
10
10
  Image,
@@ -281,7 +281,8 @@ it.effect("read returns the display record for a readable file", () =>
281
281
  it.effect("read computes a code language hint from the path", () =>
282
282
  Effect.gen(function* () {
283
283
  const mem = makeMemFs({ "/abs/main.ts": "const x = 1;\n" });
284
- const data = asText(yield* runRead(mem, "/abs/main.ts"));
284
+ const mainRead = yield* runRead(mem, "/abs/main.ts");
285
+ const data = asText(mainRead);
285
286
  assert.strictEqual(data.lang, "typescript");
286
287
  assert.strictEqual(data.truncated, false);
287
288
  }),
@@ -290,7 +291,8 @@ it.effect("read computes a code language hint from the path", () =>
290
291
  it.effect("read truncates content at the injected byte cap and reports truncated", () =>
291
292
  Effect.gen(function* () {
292
293
  const mem = makeMemFs({ "/abs/big.txt": "abcdef" });
293
- const data = asText(yield* runRead(mem, "/abs/big.txt", 5));
294
+ const bigRead = yield* runRead(mem, "/abs/big.txt", 5);
295
+ const data = asText(bigRead);
294
296
  assert.strictEqual(data.content, "abcde");
295
297
  assert.strictEqual(data.truncated, true);
296
298
  }),
@@ -299,7 +301,8 @@ it.effect("read truncates content at the injected byte cap and reports truncated
299
301
  it.effect("read does not split a multi-byte character at the injected byte cap", () =>
300
302
  Effect.gen(function* () {
301
303
  const mem = makeMemFs({ "/abs/uni.txt": "€€€" });
302
- const data = asText(yield* runRead(mem, "/abs/uni.txt", 4));
304
+ const uniRead = yield* runRead(mem, "/abs/uni.txt", 4);
305
+ const data = asText(uniRead);
303
306
  assert.strictEqual(data.content, "€");
304
307
  assert.strictEqual(data.truncated, true);
305
308
  }),
@@ -308,7 +311,8 @@ it.effect("read does not split a multi-byte character at the injected byte cap",
308
311
  it.effect("read truncates content at the injected line cap and reports truncated", () =>
309
312
  Effect.gen(function* () {
310
313
  const mem = makeMemFs({ "/abs/lines.txt": "a\nb\nc\nd" });
311
- const data = asText(yield* runRead(mem, "/abs/lines.txt", 100, 3));
314
+ const linesRead = yield* runRead(mem, "/abs/lines.txt", 100, 3);
315
+ const data = asText(linesRead);
312
316
  assert.strictEqual(data.content, "a\nb\nc");
313
317
  assert.strictEqual(data.truncated, true);
314
318
  }),
@@ -317,7 +321,8 @@ it.effect("read truncates content at the injected line cap and reports truncated
317
321
  it.effect("read leaves content untruncated at the exact byte and line caps", () =>
318
322
  Effect.gen(function* () {
319
323
  const mem = makeMemFs({ "/abs/at.txt": "abcde" });
320
- const data = asText(yield* runRead(mem, "/abs/at.txt", 5, 2000));
324
+ const atRead = yield* runRead(mem, "/abs/at.txt", 5, 2000);
325
+ const data = asText(atRead);
321
326
  assert.strictEqual(data.content, "abcde");
322
327
  assert.strictEqual(data.truncated, false);
323
328
  }),
@@ -481,7 +486,7 @@ it.effect("read fails with PreviewReadError on a permission error", () =>
481
486
  );
482
487
 
483
488
  it("PreviewReadError carries the expected tag and fields", () => {
484
- const err = new PreviewReadError({ path: "/x", message: "boom" });
489
+ const err = PreviewReadError.make({ path: "/x", message: "boom" });
485
490
  assert.strictEqual(err._tag, "PreviewReadError");
486
491
  assert.instanceOf(err, PreviewReadError);
487
492
  assert.strictEqual(err.path, "/x");
@@ -492,12 +497,7 @@ it("PreviewReadError carries the expected tag and fields", () => {
492
497
  // Slice 4 — preview tool registration behavior
493
498
  // ---------------------------------------------------------------------------
494
499
 
495
- const theme = {
496
- fg: (_color: string, text: string) => text,
497
- bg: (_color: string, text: string) => text,
498
- bold: (text: string) => text,
499
- style: (text: string) => text,
500
- } as unknown as Theme;
500
+ const theme = themeDouble();
501
501
 
502
502
  class FakePi {
503
503
  readonly tools: Array<ToolDefinition<any, any, any>> = [];
@@ -535,27 +535,27 @@ const makePreviewContext = (
535
535
  mem: MemFs,
536
536
  maxBytes?: number,
537
537
  maxLines?: number,
538
- ): Context.Context<PreviewService> =>
539
- Effect.runSync(
538
+ ): Promise<Context.Context<PreviewService>> =>
539
+ Effect.runPromise(
540
540
  Effect.context<PreviewService>().pipe(
541
541
  Effect.provide(PreviewService.layerTest(mem.fs, testHomedir, maxBytes, maxLines)),
542
542
  ),
543
543
  );
544
544
 
545
- const registerTool = (mem: MemFs = makeMemFs()) => {
545
+ const registerTool = async (mem: MemFs = makeMemFs()) => {
546
546
  const pi = new FakePi();
547
- registerPreview(pi as unknown as ExtensionAPI, makePreviewContext(mem));
547
+ registerPreview(extensionApiDouble(pi), await makePreviewContext(mem));
548
548
  return pi.tools[0]!;
549
549
  };
550
550
 
551
- it("the preview tool registers with the preview name and a path parameter", () => {
552
- const tool = registerTool();
551
+ it("the preview tool registers with the preview name and a path parameter", async () => {
552
+ const tool = await registerTool();
553
553
  assert.strictEqual(tool.name, "preview");
554
554
  assert.strictEqual(tool.parameters.properties.path.type, "string");
555
555
  });
556
556
 
557
557
  it("tool execute returns the exact confirmation stub and the display record for a readable file", async () => {
558
- const tool = registerTool(makeMemFs({ "/cwd/src/file.md": "# Title" }));
558
+ const tool = await registerTool(makeMemFs({ "/cwd/src/file.md": "# Title" }));
559
559
  const ctx = { cwd: "/cwd" } as ExtensionToolContext;
560
560
  const result = await tool.execute("call-1", { path: "src/file.md" }, undefined, undefined, ctx);
561
561
  assert.strictEqual(
@@ -572,7 +572,7 @@ it("tool execute returns the exact confirmation stub and the display record for
572
572
  });
573
573
 
574
574
  it("tool execute keeps the model stub for images and returns an image detail record", async () => {
575
- const tool = registerTool(makeMemFs({}, { "/cwd/pic.png": PNG_BYTES }));
575
+ const tool = await registerTool(makeMemFs({}, { "/cwd/pic.png": PNG_BYTES }));
576
576
  const ctx = { cwd: "/cwd" } as ExtensionToolContext;
577
577
  const result = await tool.execute("call-1", { path: "pic.png" }, undefined, undefined, ctx);
578
578
  assert.strictEqual(
@@ -593,7 +593,7 @@ it("tool execute keeps the model stub for images and returns an image detail rec
593
593
  });
594
594
 
595
595
  it("tool execute returns only the text stub for text files (no image block)", async () => {
596
- const tool = registerTool(makeMemFs({ "/cwd/file.md": "# Title" }));
596
+ const tool = await registerTool(makeMemFs({ "/cwd/file.md": "# Title" }));
597
597
  const ctx = { cwd: "/cwd" } as ExtensionToolContext;
598
598
  const result = await tool.execute("call-1", { path: "file.md" }, undefined, undefined, ctx);
599
599
  assert.strictEqual(result.content.length, 1);
@@ -601,14 +601,14 @@ it("tool execute returns only the text stub for text files (no image block)", as
601
601
  });
602
602
 
603
603
  it("tool execute resolves relative paths against the tool context cwd", async () => {
604
- const tool = registerTool(makeMemFs({ "/cwd/src/file.md": "# Title" }));
604
+ const tool = await registerTool(makeMemFs({ "/cwd/src/file.md": "# Title" }));
605
605
  const ctx = { cwd: "/cwd" } as ExtensionToolContext;
606
606
  const result = await tool.execute("call-1", { path: "src/file.md" }, undefined, undefined, ctx);
607
607
  assert.strictEqual(result.details.path, "/cwd/src/file.md");
608
608
  });
609
609
 
610
610
  it("tool execute returns the exact failure stub and error details when the read fails", async () => {
611
- const tool = registerTool();
611
+ const tool = await registerTool();
612
612
  const ctx = { cwd: "/cwd" } as ExtensionToolContext;
613
613
  const result = await tool.execute("call-1", { path: "missing.md" }, undefined, undefined, ctx);
614
614
  const expectedMessage = PlatformError.systemError({
@@ -626,14 +626,13 @@ it("tool execute returns the exact failure stub and error details when the read
626
626
  path: "missing.md",
627
627
  kind: "text",
628
628
  content: "",
629
- lang: undefined,
630
629
  truncated: false,
631
630
  error: expectedMessage,
632
631
  });
633
632
  });
634
633
 
635
634
  it("tool execute reports the failure stub when the reference is unresolvable", async () => {
636
- const tool = registerTool();
635
+ const tool = await registerTool();
637
636
  const ctx = { cwd: "/cwd" } as ExtensionToolContext;
638
637
  const result = await tool.execute(
639
638
  "call-1",
@@ -779,13 +778,13 @@ it("renderBodyComponent image emits a kitty sequence when the terminal supports
779
778
  }
780
779
  });
781
780
 
782
- it("the preview tool renders its own shell instead of pi's standard tool box", () => {
783
- const tool = registerTool();
781
+ it("the preview tool renders its own shell instead of pi's standard tool box", async () => {
782
+ const tool = await registerTool();
784
783
  assert.strictEqual(tool.renderShell, "self");
785
784
  });
786
785
 
787
- it("renderCall renders the preview tool title and the path argument", () => {
788
- const tool = registerTool();
786
+ it("renderCall renders the preview tool title and the path argument", async () => {
787
+ const tool = await registerTool();
789
788
  const out = tool.renderCall!({ path: "/abs/x.ts" }, theme, {} as any);
790
789
  assert.ok(isComponent(out));
791
790
  const rendered = out.render(200).join("\n");
@@ -794,8 +793,8 @@ it("renderCall renders the preview tool title and the path argument", () => {
794
793
  assert.strictEqual(rendered.includes("/abs/x.ts"), true);
795
794
  });
796
795
 
797
- it("renderResult falls back to the last component when the details are missing", () => {
798
- const tool = registerTool();
796
+ it("renderResult falls back to the last component when the details are missing", async () => {
797
+ const tool = await registerTool();
799
798
  const result = { content: [], details: undefined } as any;
800
799
  const last = new Text("last", 0, 0);
801
800
  const out = tool.renderResult!(result, { expanded: true, isPartial: false }, theme, {
@@ -811,8 +810,8 @@ it("renderResult falls back to the last component when the details are missing",
811
810
  assert.instanceOf(fallback, Text);
812
811
  });
813
812
 
814
- it("renderResult renders the warning box for an error result", () => {
815
- const tool = registerTool();
813
+ it("renderResult renders the warning box for an error result", async () => {
814
+ const tool = await registerTool();
816
815
  const result = {
817
816
  content: [],
818
817
  details: {
@@ -830,8 +829,8 @@ it("renderResult renders the warning box for an error result", () => {
830
829
  assert.strictEqual(out.render(200).join("\n").includes("preview failed: boom"), true);
831
830
  });
832
831
 
833
- it("renderResult renders the body in a bordered box for a success result", () => {
834
- const tool = registerTool();
832
+ it("renderResult renders the body in a bordered box for a success result", async () => {
833
+ const tool = await registerTool();
835
834
  const record = {
836
835
  kind: "text",
837
836
  path: "/a.md",
@@ -850,8 +849,8 @@ it("renderResult renders the body in a bordered box for a success result", () =>
850
849
  assert.strictEqual(out.render(200).join("\n").includes("Hi"), true);
851
850
  });
852
851
 
853
- it("renderResult draws an image caption and leaves pixel drawing to pi's native content blocks", () => {
854
- const tool = registerTool();
852
+ it("renderResult draws an image caption and leaves pixel drawing to pi's native content blocks", async () => {
853
+ const tool = await registerTool();
855
854
  const record = {
856
855
  kind: "image" as const,
857
856
  path: "/a/pic.png",
@@ -879,7 +878,7 @@ it("the model stub and image block both reach the model (image stays in content)
879
878
  // The image block is intentional: it is what makes the TUI render the picture
880
879
  // through pi's native pipeline, and pi auto-resizes it for provider requests.
881
880
  const pi = new FakePi();
882
- registerPreview(pi as unknown as ExtensionAPI, makePreviewContext(makeMemFs()));
881
+ registerPreview(extensionApiDouble(pi), await makePreviewContext(makeMemFs()));
883
882
  assert.strictEqual(pi.toolResultHandlers.length, 0, "no tool_result hook registered");
884
883
  });
885
884
 
@@ -905,14 +904,14 @@ const makeCommandCtx = (): NotifySpy => {
905
904
  return { calls, ctx };
906
905
  };
907
906
 
908
- const registerCommand = (mem: MemFs = makeMemFs()) => {
907
+ const registerCommand = async (mem: MemFs = makeMemFs()) => {
909
908
  const pi = new FakePi();
910
- registerPreview(pi as unknown as ExtensionAPI, makePreviewContext(mem));
909
+ registerPreview(extensionApiDouble(pi), await makePreviewContext(mem));
911
910
  return { pi, handler: pi.commands.find((c) => c.name === "preview")!.handler };
912
911
  };
913
912
 
914
913
  it("the preview command notifies the usage warning when the argument is empty", async () => {
915
- const { pi, handler } = registerCommand();
914
+ const { pi, handler } = await registerCommand();
916
915
  const { calls, ctx } = makeCommandCtx();
917
916
  await handler(" ", ctx);
918
917
  assert.strictEqual(calls.length, 1);
@@ -922,7 +921,7 @@ it("the preview command notifies the usage warning when the argument is empty",
922
921
 
923
922
  it("the preview command appends the display record as a custom entry on success", async () => {
924
923
  const mem = makeMemFs({ "/cwd/file.md": "# Hi" });
925
- const { pi, handler } = registerCommand(mem);
924
+ const { pi, handler } = await registerCommand(mem);
926
925
  const { calls, ctx } = makeCommandCtx();
927
926
  await handler("/cwd/file.md", ctx);
928
927
  assert.deepStrictEqual(pi.entries, [
@@ -942,14 +941,14 @@ it("the preview command appends the display record as a custom entry on success"
942
941
 
943
942
  it("the preview command resolves relative paths before reading", async () => {
944
943
  const mem = makeMemFs({ "/cwd/file.md": "# Hi" });
945
- const { pi, handler } = registerCommand(mem);
944
+ const { pi, handler } = await registerCommand(mem);
946
945
  const { ctx } = makeCommandCtx();
947
946
  await handler("file.md", ctx);
948
947
  assert.strictEqual((pi.entries[0]!.data as { path: string }).path, "/cwd/file.md");
949
948
  });
950
949
 
951
950
  it("the preview command notifies the error message when the read fails", async () => {
952
- const { pi, handler } = registerCommand();
951
+ const { pi, handler } = await registerCommand();
953
952
  const { calls, ctx } = makeCommandCtx();
954
953
  await handler("missing.md", ctx);
955
954
  const expectedMessage = PlatformError.systemError({
@@ -965,7 +964,7 @@ it("the preview command notifies the error message when the read fails", async (
965
964
  });
966
965
 
967
966
  it("the preview command notifies the error message when the reference is unresolvable", async () => {
968
- const { pi, handler } = registerCommand();
967
+ const { pi, handler } = await registerCommand();
969
968
  const { calls, ctx } = makeCommandCtx();
970
969
  await handler("file://host/path", ctx);
971
970
  assert.strictEqual(calls.length, 1);
@@ -974,9 +973,9 @@ it("the preview command notifies the error message when the reference is unresol
974
973
  assert.strictEqual(pi.entries.length, 0);
975
974
  });
976
975
 
977
- it("the entry renderer renders a warning bordered box when the entry has no data", () => {
976
+ it("the entry renderer renders a warning bordered box when the entry has no data", async () => {
978
977
  const pi = new FakePi();
979
- registerPreview(pi as unknown as ExtensionAPI, makePreviewContext(makeMemFs()));
978
+ registerPreview(extensionApiDouble(pi), await makePreviewContext(makeMemFs()));
980
979
  const renderer = pi.renderers.find((r) => r.type === "preview")!.renderer;
981
980
  const out = renderer({ data: undefined }, { expanded: true }, theme);
982
981
  assert.ok(isComponent(out));
@@ -984,9 +983,9 @@ it("the entry renderer renders a warning bordered box when the entry has no data
984
983
  assert.strictEqual(out.render(200).join("\n").includes("[preview] no data"), true);
985
984
  });
986
985
 
987
- it("the entry renderer renders the path header and the body inside a bordered box when the entry has data", () => {
986
+ it("the entry renderer renders the path header and the body inside a bordered box when the entry has data", async () => {
988
987
  const pi = new FakePi();
989
- registerPreview(pi as unknown as ExtensionAPI, makePreviewContext(makeMemFs()));
988
+ registerPreview(extensionApiDouble(pi), await makePreviewContext(makeMemFs()));
990
989
  const renderer = pi.renderers.find((r) => r.type === "preview")!.renderer;
991
990
  const out = renderer(
992
991
  {
@@ -1008,11 +1007,11 @@ it("the entry renderer renders the path header and the body inside a bordered bo
1008
1007
  assert.strictEqual(rendered.includes("Hi"), true);
1009
1008
  });
1010
1009
 
1011
- it("the entry renderer draws image entries as caption box + spacer + bare kitty transmission", () => {
1010
+ it("the entry renderer draws image entries as caption box + spacer + bare kitty transmission", async () => {
1012
1011
  setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true });
1013
1012
  try {
1014
1013
  const pi = new FakePi();
1015
- registerPreview(pi as unknown as ExtensionAPI, makePreviewContext(makeMemFs()));
1014
+ registerPreview(extensionApiDouble(pi), await makePreviewContext(makeMemFs()));
1016
1015
  const renderer = pi.renderers.find((r) => r.type === "preview")!.renderer;
1017
1016
  const out = renderer(
1018
1017
  {
@@ -1044,7 +1043,11 @@ it("the entry renderer draws image entries as caption box + spacer + bare kitty
1044
1043
  );
1045
1044
  assert.strictEqual(
1046
1045
  lines.some(
1047
- (l: string) => l.includes("│ \x1b_G") || l.includes("╭ \x1b_G") || /[╭─╰╯]\x1b_G/.test(l),
1046
+ (l: string) =>
1047
+ l.includes("│ \x1b_G") ||
1048
+ l.includes("╭ \x1b_G") ||
1049
+ // eslint-disable-next-line no-control-regex -- asserts an OSC introducer
1050
+ /[╭─╰╯]\x1b_G/.test(l),
1048
1051
  ),
1049
1052
  false,
1050
1053
  );
@@ -1075,39 +1078,39 @@ const nestedEndEvent = (overrides: Record<string, unknown> = {}) => ({
1075
1078
  ...overrides,
1076
1079
  });
1077
1080
 
1078
- const registerEndHook = () => {
1081
+ const registerEndHook = async () => {
1079
1082
  const pi = new FakePi();
1080
- registerPreview(pi as unknown as ExtensionAPI, makePreviewContext(makeMemFs()));
1083
+ registerPreview(extensionApiDouble(pi), await makePreviewContext(makeMemFs()));
1081
1084
  return { pi, handler: pi.toolExecutionEndHandlers[0]! };
1082
1085
  };
1083
1086
 
1084
- it("the nested-preview hook appends the display record so the entry renderer can draw it", () => {
1085
- const { pi, handler } = registerEndHook();
1087
+ it("the nested-preview hook appends the display record so the entry renderer can draw it", async () => {
1088
+ const { pi, handler } = await registerEndHook();
1086
1089
  assert.strictEqual(pi.toolExecutionEndHandlers.length, 1);
1087
1090
  handler(nestedEndEvent(), { mode: "tui" });
1088
1091
  assert.deepStrictEqual(pi.entries, [{ type: "preview", data: TEXT_RECORD }]);
1089
1092
  });
1090
1093
 
1091
- it("the nested-preview hook ignores model-issued (top-level) preview ends", () => {
1092
- const { pi, handler } = registerEndHook();
1094
+ it("the nested-preview hook ignores model-issued (top-level) preview ends", async () => {
1095
+ const { pi, handler } = await registerEndHook();
1093
1096
  handler(nestedEndEvent({ parentToolCallId: undefined }), { mode: "tui" });
1094
1097
  assert.strictEqual(pi.entries.length, 0);
1095
1098
  });
1096
1099
 
1097
- it("the nested-preview hook ignores nested ends for other tools", () => {
1098
- const { pi, handler } = registerEndHook();
1100
+ it("the nested-preview hook ignores nested ends for other tools", async () => {
1101
+ const { pi, handler } = await registerEndHook();
1099
1102
  handler(nestedEndEvent({ toolName: "read" }), { mode: "tui" });
1100
1103
  assert.strictEqual(pi.entries.length, 0);
1101
1104
  });
1102
1105
 
1103
- it("the nested-preview hook ignores non-tui modes", () => {
1104
- const { pi, handler } = registerEndHook();
1106
+ it("the nested-preview hook ignores non-tui modes", async () => {
1107
+ const { pi, handler } = await registerEndHook();
1105
1108
  handler(nestedEndEvent(), { mode: "rpc" });
1106
1109
  assert.strictEqual(pi.entries.length, 0);
1107
1110
  });
1108
1111
 
1109
- it("the nested-preview hook ignores failed previews", () => {
1110
- const { pi, handler } = registerEndHook();
1112
+ it("the nested-preview hook ignores failed previews", async () => {
1113
+ const { pi, handler } = await registerEndHook();
1111
1114
  handler(
1112
1115
  nestedEndEvent({
1113
1116
  result: { content: [], details: { ...TEXT_RECORD, content: "", error: "boom" } },