pi-render-btw 0.6.16 → 0.6.18

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.6.18 — 2026-11-20
4
+
5
+ - Fix contextual BTW sub-sessions losing the current main-session context before their first model request. Seed messages now enter the sub-session `SessionManager`, which Pi uses as the authoritative source for model context.
6
+
7
+ ## 0.6.17 — 2026-09-18
8
+
9
+ - Add clickable `[preview]` actions beside `[Esc]` in BTW and BTW Peek for opening the focused tool's read-only Tool Output.
10
+
3
11
  ## 0.6.16 — 2026-09-18
4
12
 
5
13
  - Extract textual tool-result content for readable Tool Output instead of rendering the raw `{ content: [...] }` envelope.
@@ -0,0 +1,23 @@
1
+ import { buildSessionContext } from "@earendil-works/pi-coding-agent";
2
+ import type { Message } from "@earendil-works/pi-ai";
3
+ import { describe, expect, it } from "vitest";
4
+ import { createBtwSessionManager } from "./btw.ts";
5
+
6
+ function userMessage(text: string): Message {
7
+ return {
8
+ role: "user",
9
+ content: [{ type: "text", text }],
10
+ timestamp: 0,
11
+ };
12
+ }
13
+
14
+ describe("BTW contextual seed", () => {
15
+ it("persists the main-session seed in the sub-session manager", () => {
16
+ const seed = [userMessage("MAIN_SESSION_CONTEXT_SENTINEL")];
17
+ const sessionManager = createBtwSessionManager(seed);
18
+
19
+ const context = buildSessionContext(sessionManager.getEntries(), sessionManager.getLeafId());
20
+
21
+ expect(context.messages).toEqual(seed);
22
+ });
23
+ });
@@ -573,6 +573,10 @@ describe("BTW transcript message layout", () => {
573
573
  component.handleMouse({ type: "click", button: "left", x: 5, y: hostToolRow } as any);
574
574
  expect((component as any).liveFocusedToolCallId).toBe("call-1");
575
575
  expect((component as any).liveExpandedToolCallId).toBe("call-1");
576
+ component.handleMouse({ type: "click", button: "left", x: 64, y: 1 } as any);
577
+ expect((component as any).toolPreviewParent).toBe("live");
578
+ component.handleInput("\x1b");
579
+ expect((component as any).toolPreviewParent).toBeNull();
576
580
  (component as any).followTranscript = false;
577
581
  (component as any).transcriptScrollOffset = 0;
578
582
  expect(component.render(80).join("\n")).toContain("line 0");
@@ -721,8 +725,18 @@ describe("BTW transcript message layout", () => {
721
725
  );
722
726
 
723
727
  const lines = component.render(24);
728
+ expect(lines[1]).toContain("[preview]");
729
+ expect(lines[1].indexOf("[preview]")).toBeLessThan(lines[1].indexOf("[Esc]"));
724
730
  expect(lines[1]).toMatch(/\[Esc\]│$/);
725
731
 
732
+ const previewHover = component.handleMouse({ type: "move", button: "none", x: 10, y: 1 } as any);
733
+ expect((component as any).hoveredPreviewHint).toBe(true);
734
+ expect(previewHover).toEqual({ handled: true, render: true });
735
+ const noPreview = component.handleMouse({ type: "click", button: "left", x: 10, y: 1 } as any);
736
+ expect(noPreview).toEqual({ handled: true, render: false });
737
+ expect((component as any).toolPreviewParent).toBeNull();
738
+ expect(dismissed).toBe(0);
739
+
726
740
  const hover = component.handleMouse({ type: "move", button: "none", x: 20, y: 1 } as any);
727
741
  expect((component as any).hoveredEscapeHint).toBe(true);
728
742
  expect(hover).toEqual({ handled: true, render: true });
@@ -754,6 +768,31 @@ describe("BTW transcript message layout", () => {
754
768
  expect(dismissed).toBe(0);
755
769
  });
756
770
 
771
+ it("opens the selected Peek tool from the title [preview] button", () => {
772
+ const entries = [
773
+ { id: 1, turnId: 1, type: "tool-call", toolCallId: "call-peek", toolName: "read", args: "{\"path\":\"src/index.ts\"}" },
774
+ { id: 2, turnId: 1, type: "tool-result", toolCallId: "call-peek", toolName: "read", content: "preview text", fullContent: "preview text", truncated: false, isError: false, streaming: false },
775
+ ] as any;
776
+ const component = new BtwOverlayComponent(
777
+ tui,
778
+ theme,
779
+ keybindings,
780
+ () => entries,
781
+ () => null,
782
+ () => "contextual",
783
+ () => {},
784
+ () => {},
785
+ () => {},
786
+ );
787
+
788
+ component.handleInput("\x10");
789
+ component.render(80);
790
+ component.handleMouse({ type: "click", button: "left", x: 64, y: 1 } as any);
791
+
792
+ expect((component as any).peekState.active).toBe(true);
793
+ expect((component as any).toolPreviewParent).toBe("peek");
794
+ });
795
+
757
796
  it("closes from the title [Esc] button through the raw SGR fallback", () => {
758
797
  let dismissed = 0;
759
798
  const component = new BtwOverlayComponent(
package/extensions/btw.ts CHANGED
@@ -74,6 +74,15 @@ const BTW_MODEL_OVERRIDE_TYPE = "btw-model-override";
74
74
  const BTW_THINKING_OVERRIDE_TYPE = "btw-thinking-override";
75
75
  const BTW_FOCUS_SHORTCUTS = [Key.alt("/"), Key.ctrlAlt("w")] as const;
76
76
  const BTW_TOOL_OUTPUT_SHORTCUT = Key.ctrl("o");
77
+ const BTW_PREVIEW_LABEL = "[preview]";
78
+ const BTW_ESCAPE_LABEL = "[Esc]";
79
+
80
+ type BtwTitleActionGeometry = {
81
+ previewStart: number | null;
82
+ previewEnd: number | null;
83
+ escapeStart: number;
84
+ escapeEnd: number;
85
+ };
77
86
 
78
87
  function matchesBtwFocusShortcut(data: string): boolean {
79
88
  return BTW_FOCUS_SHORTCUTS.some((shortcut) => matchesKey(data, shortcut));
@@ -435,6 +444,18 @@ function buildBtwSeedState(
435
444
  };
436
445
  }
437
446
 
447
+ /**
448
+ * Own the BTW seed in its SessionManager so Pi can reconstruct it for the
449
+ * next model request. Kept as a small seam for context-related regressions.
450
+ */
451
+ export function createBtwSessionManager(seedMessages: readonly Message[]): SessionManager {
452
+ const sessionManager = SessionManager.inMemory();
453
+ for (const message of seedMessages) {
454
+ sessionManager.appendMessage(message);
455
+ }
456
+ return sessionManager;
457
+ }
458
+
438
459
  function extractToolText(value: unknown): string | undefined {
439
460
  if (Array.isArray(value)) {
440
461
  const text = value
@@ -1222,7 +1243,9 @@ export class BtwOverlayComponent extends Container implements Focusable {
1222
1243
  private peekToolPreviewScrollOffset = 0;
1223
1244
  private expandedToolId: number | null = null;
1224
1245
  private hoveredEscapeHint = false;
1246
+ private hoveredPreviewHint = false;
1225
1247
  private rawEscapePress = false;
1248
+ private rawPreviewPress = false;
1226
1249
  /** The same bounded preview is used by Peek and by the live BTW panel. */
1227
1250
  private toolPreviewParent: "peek" | "live" | null = null;
1228
1251
  private liveToolPreviewBlock: PeekBlock | null = null;
@@ -1430,18 +1453,42 @@ export class BtwOverlayComponent extends Container implements Focusable {
1430
1453
  return this.theme.fg("border", `${left}${"─".repeat(innerWidth)}${right}`);
1431
1454
  }
1432
1455
 
1433
- private titleLine(title: string, innerWidth: number): string {
1434
- const escapeLabel = "[Esc]";
1456
+ private titleActionGeometry(innerWidth: number, showPreview: boolean): BtwTitleActionGeometry {
1457
+ const escapeWidth = visibleWidth(BTW_ESCAPE_LABEL);
1458
+ const previewWidth = showPreview ? visibleWidth(BTW_PREVIEW_LABEL) : 0;
1459
+ const actionGap = showPreview ? 1 : 0;
1460
+ const actionsWidth = previewWidth + actionGap + escapeWidth;
1461
+ const actionStart = innerWidth - actionsWidth + 1;
1462
+ const escapeStart = innerWidth - escapeWidth + 1;
1463
+
1464
+ return {
1465
+ previewStart: showPreview ? actionStart : null,
1466
+ previewEnd: showPreview ? actionStart + previewWidth - 1 : null,
1467
+ escapeStart,
1468
+ escapeEnd: escapeStart + escapeWidth - 1,
1469
+ };
1470
+ }
1471
+
1472
+ private titleLine(title: string, innerWidth: number, showPreview = false): string {
1473
+ const geometry = this.titleActionGeometry(innerWidth, showPreview);
1474
+ const actionWidth = innerWidth - geometry.escapeStart + 1 +
1475
+ (geometry.previewStart === null ? 0 : geometry.escapeStart - geometry.previewStart);
1435
1476
  const escapeHint = this.hoveredEscapeHint
1436
- ? this.theme.fg("text", this.theme.bold(escapeLabel))
1437
- : this.theme.fg("dim", escapeLabel);
1438
- const escapeWidth = visibleWidth(escapeLabel);
1439
- // Reserve the close label before truncating the title so a long tool name
1440
- // can never hide the only visible mouse/keyboard dismissal affordance.
1441
- const titleWidth = Math.max(0, innerWidth - escapeWidth - 1);
1477
+ ? this.theme.fg("text", this.theme.bold(BTW_ESCAPE_LABEL))
1478
+ : this.theme.fg("dim", BTW_ESCAPE_LABEL);
1479
+ const previewEnabled = showPreview && this.canEnterPreview();
1480
+ const previewHint = showPreview
1481
+ ? this.hoveredPreviewHint && previewEnabled
1482
+ ? this.theme.fg("text", this.theme.bold(BTW_PREVIEW_LABEL))
1483
+ : this.theme.fg("dim", BTW_PREVIEW_LABEL)
1484
+ : "";
1485
+ // Reserve the title actions before truncating the title so a long title
1486
+ // can never hide either visible mouse/keyboard affordance.
1487
+ const titleWidth = Math.max(0, (geometry.previewStart ?? geometry.escapeStart) - 2);
1442
1488
  const clippedTitle = truncateToWidth(title, titleWidth, "");
1443
- const gap = Math.max(1, innerWidth - visibleWidth(clippedTitle) - escapeWidth);
1444
- return this.frameLine(`${clippedTitle}${" ".repeat(gap)}${escapeHint}`, innerWidth);
1489
+ const gap = Math.max(1, innerWidth - visibleWidth(clippedTitle) - actionWidth);
1490
+ const actions = showPreview ? `${previewHint} ${escapeHint}` : escapeHint;
1491
+ return this.frameLine(`${clippedTitle}${" ".repeat(gap)}${actions}`, innerWidth);
1445
1492
  }
1446
1493
 
1447
1494
  private resetDraftRecall(): void {
@@ -1720,11 +1767,48 @@ export class BtwOverlayComponent extends Container implements Focusable {
1720
1767
  return null;
1721
1768
  }
1722
1769
 
1770
+ private titleShowsPreviewAction(): boolean {
1771
+ return this.toolPreviewParent === null;
1772
+ }
1773
+
1774
+ private currentTitleActionGeometry(): BtwTitleActionGeometry {
1775
+ const dialogWidth = Math.max(24, this.lastRenderWidth);
1776
+ const innerWidth = Math.max(22, dialogWidth - 2);
1777
+ const local = this.titleActionGeometry(innerWidth, this.titleShowsPreviewAction());
1778
+ return {
1779
+ previewStart: local.previewStart === null ? null : local.previewStart + 1,
1780
+ previewEnd: local.previewEnd === null ? null : local.previewEnd + 1,
1781
+ escapeStart: local.escapeStart + 1,
1782
+ escapeEnd: local.escapeEnd + 1,
1783
+ };
1784
+ }
1785
+
1786
+ private hasFocusedLiveToolPreview(): boolean {
1787
+ if (this.peekState.active || this.toolPreviewParent !== null || !this.liveFocusedToolCallId) return false;
1788
+ const block = buildPeekBlocks(
1789
+ this.readTranscriptEntries() as unknown as PeekEntry[],
1790
+ true,
1791
+ ).find((candidate) => candidate.toolCallId === this.liveFocusedToolCallId);
1792
+ return hasToolPreview(block);
1793
+ }
1794
+
1795
+ private canEnterPreview(): boolean {
1796
+ if (!this.titleShowsPreviewAction()) return false;
1797
+ return this.peekState.active
1798
+ ? hasToolPreview(this.getSelectedPeekBlock())
1799
+ : this.hasFocusedLiveToolPreview();
1800
+ }
1801
+
1723
1802
  /** The title row is line 2 in one-based local coordinates. */
1724
1803
  private isEscapeHintHit(x: number, y: number): boolean {
1725
- const labelWidth = visibleWidth("[Esc]");
1726
- const labelStart = this.lastRenderWidth - labelWidth;
1727
- return y === 2 && x >= labelStart && x < this.lastRenderWidth;
1804
+ const geometry = this.currentTitleActionGeometry();
1805
+ return y === 2 && x >= geometry.escapeStart && x <= geometry.escapeEnd;
1806
+ }
1807
+
1808
+ private isPreviewHintHit(x: number, y: number): boolean {
1809
+ const geometry = this.currentTitleActionGeometry();
1810
+ return y === 2 && geometry.previewStart !== null && geometry.previewEnd !== null &&
1811
+ x >= geometry.previewStart && x <= geometry.previewEnd;
1728
1812
  }
1729
1813
 
1730
1814
  private closeEscapeTarget(): void {
@@ -1780,19 +1864,35 @@ export class BtwOverlayComponent extends Container implements Focusable {
1780
1864
  delta: event.wheelDelta,
1781
1865
  };
1782
1866
 
1867
+ const overPreviewHint = this.isPreviewHintHit(normalized.x, normalized.y);
1783
1868
  const overEscapeHint = this.isEscapeHintHit(normalized.x, normalized.y);
1784
1869
  if (normalized.action === "move" || normalized.action === "drag") {
1785
- const hoverChanged = overEscapeHint !== this.hoveredEscapeHint;
1870
+ const previewHoverChanged = overPreviewHint !== this.hoveredPreviewHint;
1871
+ const escapeHoverChanged = overEscapeHint !== this.hoveredEscapeHint;
1872
+ this.hoveredPreviewHint = overPreviewHint;
1786
1873
  this.hoveredEscapeHint = overEscapeHint;
1787
- if (overEscapeHint) {
1788
- return { handled: true, render: hoverChanged };
1874
+ if (overPreviewHint || overEscapeHint) {
1875
+ return { handled: true, render: previewHoverChanged || escapeHoverChanged };
1789
1876
  }
1790
1877
  // Continue routing a move outside the title to update transcript/block
1791
1878
  // hover state. In preview mode there is no other hover target.
1792
- if (hoverChanged && this.toolPreviewParent !== null) {
1879
+ if ((previewHoverChanged || escapeHoverChanged) && this.toolPreviewParent !== null) {
1793
1880
  return { handled: true, render: true };
1794
1881
  }
1795
1882
  }
1883
+ if (overPreviewHint && normalized.button === 0) {
1884
+ if (normalized.action === "press") {
1885
+ return { handled: true, capture: true, render: false };
1886
+ }
1887
+ if (normalized.action === "click") {
1888
+ if (this.peekState.active) this.enterToolPreview();
1889
+ else this.enterLiveToolPreview();
1890
+ return { handled: true, render: false };
1891
+ }
1892
+ if (normalized.action === "release") {
1893
+ return { handled: true, render: false };
1894
+ }
1895
+ }
1796
1896
  if (overEscapeHint && normalized.button === 0) {
1797
1897
  if (normalized.action === "press") {
1798
1898
  return { handled: true, capture: true, render: false };
@@ -1935,21 +2035,31 @@ export class BtwOverlayComponent extends Container implements Focusable {
1935
2035
  }
1936
2036
  return;
1937
2037
  }
1938
- // Consume bounded SGR button events locally. Older hosts do not expose
1939
- // normalized click dispatch, so retain the close-button press/release
1940
- // pair as the one actionable fallback; transcript clicks remain read-only.
2038
+ // Consume bounded SGR button events locally. Older hosts do not expose
2039
+ // normalized click dispatch, so retain preview/close press/release pairs
2040
+ // as the actionable fallback; transcript clicks remain read-only.
1941
2041
  if (mouseEvent) {
2042
+ const overPreviewHint = this.isPreviewHintHit(mouseEvent.x, mouseEvent.y);
1942
2043
  const overEscapeHint = this.isEscapeHintHit(mouseEvent.x, mouseEvent.y);
1943
2044
  this.handleMouse(mouseEvent);
1944
2045
  if (mouseEvent.button === 0 && mouseEvent.action === "press") {
2046
+ this.rawPreviewPress = overPreviewHint;
1945
2047
  this.rawEscapePress = overEscapeHint;
1946
2048
  } else if (mouseEvent.button === 0 && mouseEvent.action === "release") {
2049
+ const preview = this.rawPreviewPress && overPreviewHint;
1947
2050
  const close = this.rawEscapePress && overEscapeHint;
2051
+ this.rawPreviewPress = false;
1948
2052
  this.rawEscapePress = false;
1949
- if (close) this.closeEscapeTarget();
2053
+ if (preview) {
2054
+ if (this.peekState.active) this.enterToolPreview();
2055
+ else this.enterLiveToolPreview();
2056
+ } else if (close) {
2057
+ this.closeEscapeTarget();
2058
+ }
1950
2059
  }
1951
2060
  return;
1952
2061
  }
2062
+ this.rawPreviewPress = false;
1953
2063
  this.rawEscapePress = false;
1954
2064
 
1955
2065
  // Peek mode active → route all input to peek handler
@@ -2038,7 +2148,7 @@ export class BtwOverlayComponent extends Container implements Focusable {
2038
2148
 
2039
2149
  const lines = [this.borderLine(innerWidth, "top")];
2040
2150
 
2041
- lines.push(this.titleLine(this.theme.fg("accent", this.theme.bold(this.modeTextValue.trim())), innerWidth));
2151
+ lines.push(this.titleLine(this.theme.fg("accent", this.theme.bold(this.modeTextValue.trim())), innerWidth, true));
2042
2152
  lines.push(this.frameLine(this.theme.fg("dim", summary), innerWidth));
2043
2153
  lines.push(this.scrollRule(innerWidth, "top", hiddenAbove));
2044
2154
 
@@ -2417,7 +2527,7 @@ export class BtwOverlayComponent extends Container implements Focusable {
2417
2527
  ? ` search ${this.peekState.currentMatchIndex + 1}/${this.peekState.searchMatches.length}`
2418
2528
  : "");
2419
2529
  lines.push(this.borderLine(innerWidth, "top"));
2420
- lines.push(this.titleLine(this.theme.fg("accent", this.theme.bold(title)), innerWidth));
2530
+ lines.push(this.titleLine(this.theme.fg("accent", this.theme.bold(title)), innerWidth, true));
2421
2531
  lines.push(this.ruleLine(innerWidth));
2422
2532
 
2423
2533
  // ── Search bar ───────────────────────────────────────────────────
@@ -3421,11 +3531,13 @@ export default function (pi: ExtensionAPI) {
3421
3531
  }
3422
3532
 
3423
3533
  const parentModelRuntime = (ctx.modelRegistry as unknown as { runtime?: ModelRuntime | null }).runtime ?? undefined;
3534
+ const { messages: seedMessages, sideThreadStartIndex } = buildBtwSeedState(ctx, pendingThread, mode, settings.model);
3535
+ const sessionManager = createBtwSessionManager(seedMessages);
3424
3536
  const sessionOptions: Parameters<typeof createAgentSession>[0] & {
3425
3537
  modelRegistry?: ExtensionContext["modelRegistry"];
3426
3538
  modelRuntime?: ModelRuntime;
3427
3539
  } = {
3428
- sessionManager: SessionManager.inMemory(),
3540
+ sessionManager,
3429
3541
  model: settings.model,
3430
3542
  ...(parentModelRuntime !== undefined && { modelRuntime: parentModelRuntime }),
3431
3543
  thinkingLevel: settings.thinkingLevel,
@@ -3437,11 +3549,6 @@ export default function (pi: ExtensionAPI) {
3437
3549
  };
3438
3550
  const { session } = await createAgentSession(sessionOptions);
3439
3551
 
3440
- const { messages: seedMessages, sideThreadStartIndex } = buildBtwSeedState(ctx, pendingThread, mode, settings.model);
3441
- if (seedMessages.length > 0) {
3442
- session.agent.state.messages = seedMessages as typeof session.state.messages;
3443
- }
3444
-
3445
3552
  return { session, mode, subscriptions: new Set(), sideThreadStartIndex };
3446
3553
  }
3447
3554
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-render-btw",
3
- "version": "0.6.16",
3
+ "version": "0.6.18",
4
4
  "description": "A pi extension for parallel side conversations with /btw — with markdown rendering in the floating window",
5
5
  "type": "module",
6
6
  "license": "MIT",