pi-render-btw 0.6.10 → 0.6.11

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 ADDED
@@ -0,0 +1,8 @@
1
+ # Changelog
2
+
3
+ ## 0.6.11 — 2026-09-13
4
+
5
+ - Persist BTW transcript snapshots and complete tool artifacts under `.pi-subagents/btw/`.
6
+ - Rehydrate the active branch's BTW transcript after reload without leaking data across branches.
7
+ - Add live tool focus, click-to-expand/double-click collapse, and read-only `Ctrl+O` Tool Output.
8
+ - Keep wheel, Escape, Peek, and composer interactions scoped to the active BTW surface.
package/README.md CHANGED
@@ -96,6 +96,8 @@ pi install /path/to/pi-render-btw
96
96
  - `Ctrl+G` opens the current BTW draft in `$VISUAL`/`$EDITOR` (falling back to `nano`) and reads it back when the editor exits successfully
97
97
  - the composer uses Pi's native editor behavior for undo, paste, prompt history, and Tab file-path completion (including `@` prefixes) relative to the BTW working directory
98
98
  - `Esc` still dismisses BTW immediately while the overlay is focused
99
+ - hovering and left-clicking a transcript row focuses one shared USER/ASSISTANT/Tool target; clicking a Tool row toggles its compact details and double-clicking collapses it
100
+ - with a focused Tool row, `Ctrl+O` opens a read-only full Input/Output viewer; its wheel, `j/k`, arrows, `g/G`, PageUp/PageDown, and `Esc` controls stay inside the viewer
99
101
  - BTW opens top-centered so the main session remains visible underneath it
100
102
 
101
103
  ### `/btw:new [question]`
@@ -117,11 +119,15 @@ pi install /path/to/pi-render-btw
117
119
 
118
120
  - switches the BTW overlay into Peek mode for the current side thread
119
121
  - `Ctrl+P` enters Peek mode from the BTW composer, even when it contains a draft
120
- - `j/k` scroll one message, `J/K` jump to the next/previous message, and `g/G` go to the top/bottom
122
+ - `j/k` scroll one rendered line, `J/K` jump between USER/THINKING/ASSISTANT messages, and `g/G` go to the top/bottom
123
+ - arrow keys and PageUp/PageDown provide the corresponding line/page scrolling
121
124
  - `/` searches with smartcase; `n/N` moves between matches
122
125
  - `t` toggles tool messages
126
+ - `w` opens a read-only tool Input/Output preview when the selected tool has data; `Enter` expands a tool card and a host double-click collapses it
127
+ - normalized mouse hover highlights a block and a left click selects it; press/release events do not change selection
123
128
  - `M` copies the selected message; `o` opens it read-only in nvim
124
129
  - `Esc`, `q`, or `Ctrl+C` returns to the BTW composer; press `Esc` again in the composer to dismiss BTW
130
+ - the Peek and Tool Output headers show `[Esc]`; wheel input is handled locally without changing the main viewport
125
131
 
126
132
  ### `/btw:clear`
127
133
 
@@ -190,6 +196,16 @@ BTW exchanges are persisted in the session as hidden custom entries so they:
190
196
  - preserve the current BTW-only model and thinking overrides for that session history
191
197
  - stay out of the main agent's LLM context
192
198
 
199
+ ### Durable BTW transcript and tool artifacts
200
+
201
+ In addition to the small hidden session locators, BTW stores its immutable transcript snapshots and complete tool payloads under:
202
+
203
+ ```text
204
+ <project>/.pi-subagents/btw/threads/<session-id>/
205
+ ```
206
+
207
+ Tool input, output, details, and available unified diffs are kept outside the main LLM context. On reload, the active branch locator rehydrates the transcript; missing or corrupt records are ignored safely rather than replaced with fabricated output.
208
+
193
209
  ### Visible saved notes
194
210
 
195
211
  If you use `--save`, that one BTW exchange is also written as a visible custom message in the session transcript.
@@ -6,6 +6,9 @@ import {
6
6
  findNextSearchMatch,
7
7
  getBlockText,
8
8
  getBlockTypeLabel,
9
+ getToolPreviewText,
10
+ hasToolPreview,
11
+ parsePeekMouseInput,
9
12
  type PeekBlock,
10
13
  type PeekEntry,
11
14
  type PeekModeState,
@@ -22,7 +25,10 @@ function makeEntry(
22
25
  type: overrides.type,
23
26
  text: overrides.text,
24
27
  content: overrides.content,
28
+ toolCallId: overrides.toolCallId,
25
29
  toolName: overrides.toolName,
30
+ args: overrides.args,
31
+ fullContent: overrides.fullContent,
26
32
  isError: overrides.isError,
27
33
  truncated: overrides.truncated,
28
34
  streaming: overrides.streaming,
@@ -102,6 +108,18 @@ describe("buildPeekBlocks", () => {
102
108
  expect(blocks).toHaveLength(1);
103
109
  });
104
110
 
111
+ it("pairs tool input and output for a safe read-only preview", () => {
112
+ const blocks = buildPeekBlocks([
113
+ makeEntry({ id: 1, type: "tool-call", toolCallId: "call-1", toolName: "read", args: "src/index.ts" }),
114
+ makeEntry({ id: 2, type: "tool-result", toolCallId: "call-1", toolName: "read", content: "summary", fullContent: "complete contents" }),
115
+ ], true);
116
+
117
+ expect(hasToolPreview(blocks[0])).toBe(true);
118
+ expect(getToolPreviewText(blocks[0])).toContain("src/index.ts");
119
+ expect(blocks[0].toolOutput).toBe("complete contents");
120
+ expect(getToolPreviewText(blocks[0])).toContain("complete contents");
121
+ });
122
+
105
123
  it("produces sequential blocks from transcript entries in order", () => {
106
124
  const entries: PeekEntry[] = [
107
125
  makeEntry({ id: 1, type: "user-message", text: "Q1" }),
@@ -261,6 +279,21 @@ describe("getBlockText", () => {
261
279
  });
262
280
  });
263
281
 
282
+ describe("parsePeekMouseInput", () => {
283
+ it("normalizes bounded SGR wheel and button events without terminal side effects", () => {
284
+ expect(parsePeekMouseInput("\x1b[<64;12;8M")).toMatchObject({ action: "wheel", x: 12, y: 8, delta: -1 });
285
+ expect(parsePeekMouseInput("\x1b[<0;12;8M")).toMatchObject({ action: "press", button: 0, x: 12, y: 8 });
286
+ expect(parsePeekMouseInput("\x1b[<0;12;8m")).toMatchObject({ action: "release", button: 0, x: 12, y: 8 });
287
+ expect(parsePeekMouseInput("\x1b[<0;10001;1M")).toBeNull();
288
+ expect(parsePeekMouseInput("not mouse")).toBeNull();
289
+ });
290
+
291
+ it("normalizes legacy X10 wheel events", () => {
292
+ const x10WheelDown = "\x1b[M" + String.fromCharCode(32 + 65, 32 + 12, 32 + 8);
293
+ expect(parsePeekMouseInput(x10WheelDown)).toMatchObject({ action: "wheel", x: 12, y: 8, delta: 1 });
294
+ });
295
+ });
296
+
264
297
  describe("getBlockTypeLabel", () => {
265
298
  it("returns 'You' for user", () => {
266
299
  expect(getBlockTypeLabel({ id: 0, entryId: 1, turnId: 1, type: "user", text: "" })).toBe("You");
@@ -13,7 +13,12 @@ export interface PeekEntry {
13
13
  turnId: number;
14
14
  text?: string;
15
15
  content?: string;
16
+ toolCallId?: string;
16
17
  toolName?: string;
18
+ /** Serialized tool input retained by BTW's transcript model. */
19
+ args?: string;
20
+ /** Untruncated result retained for the read-only preview when available. */
21
+ fullContent?: string;
17
22
  isError?: boolean;
18
23
  truncated?: boolean;
19
24
  streaming?: boolean;
@@ -34,7 +39,11 @@ export interface PeekBlock {
34
39
  turnId: number;
35
40
  type: PeekBlockType;
36
41
  text: string;
42
+ toolCallId?: string;
37
43
  toolName?: string;
44
+ /** Serialized input/output used only for the read-only tool preview. */
45
+ toolInput?: string;
46
+ toolOutput?: string;
38
47
  isError?: boolean;
39
48
  truncated?: boolean;
40
49
  }
@@ -87,8 +96,26 @@ export function buildPeekBlocks(
87
96
  showTools: boolean,
88
97
  ): PeekBlock[] {
89
98
  const blocks: PeekBlock[] = [];
99
+ const toolData = new Map<string, { input?: string; output?: string; isError?: boolean; toolName?: string }>();
90
100
  let id = 0;
91
101
 
102
+ // Tool calls and results are separate transcript entries, but a preview should
103
+ // present their paired input/output together. Keep this lookup local to the
104
+ // renderer so the persisted BTW thread remains unchanged.
105
+ for (const entry of entries) {
106
+ if (!entry.toolCallId || (entry.type !== "tool-call" && entry.type !== "tool-result")) continue;
107
+ const current = toolData.get(entry.toolCallId) ?? {};
108
+ if (entry.type === "tool-call") {
109
+ current.input = entry.args ?? entry.text ?? current.input;
110
+ current.toolName = entry.toolName ?? current.toolName;
111
+ } else {
112
+ current.output = entry.fullContent ?? entry.content ?? entry.text ?? current.output;
113
+ current.isError = entry.isError;
114
+ current.toolName = entry.toolName ?? current.toolName;
115
+ }
116
+ toolData.set(entry.toolCallId, current);
117
+ }
118
+
92
119
  for (const entry of entries) {
93
120
  if (entry.type === "turn-boundary") continue;
94
121
 
@@ -118,23 +145,32 @@ export function buildPeekBlocks(
118
145
  });
119
146
  } else if (entry.type === "tool-call") {
120
147
  if (!showTools) continue;
148
+ const paired = entry.toolCallId ? toolData.get(entry.toolCallId) : undefined;
121
149
  blocks.push({
122
150
  id: id++,
123
151
  entryId: entry.id,
124
152
  turnId: entry.turnId,
125
153
  type: "tool-call",
126
- text: "",
127
- toolName: entry.toolName,
154
+ text: entry.args ?? entry.text ?? "",
155
+ toolCallId: entry.toolCallId,
156
+ toolName: entry.toolName ?? paired?.toolName,
157
+ toolInput: entry.args ?? entry.text ?? paired?.input,
158
+ toolOutput: paired?.output,
159
+ isError: paired?.isError,
128
160
  });
129
161
  } else if (entry.type === "tool-result") {
130
162
  if (!showTools) continue;
163
+ const paired = entry.toolCallId ? toolData.get(entry.toolCallId) : undefined;
131
164
  blocks.push({
132
165
  id: id++,
133
166
  entryId: entry.id,
134
167
  turnId: entry.turnId,
135
168
  type: "tool-result",
136
- text: entry.content ?? entry.text ?? "",
137
- toolName: entry.toolName,
169
+ text: entry.fullContent ?? entry.content ?? entry.text ?? "",
170
+ toolCallId: entry.toolCallId,
171
+ toolName: entry.toolName ?? paired?.toolName,
172
+ toolInput: paired?.input,
173
+ toolOutput: entry.fullContent ?? entry.content ?? entry.text ?? paired?.output,
138
174
  isError: entry.isError,
139
175
  truncated: entry.truncated,
140
176
  });
@@ -163,7 +199,13 @@ export function findSearchMatches(
163
199
 
164
200
  for (let i = 0; i < blocks.length; i++) {
165
201
  const block = blocks[i];
166
- const haystack = caseSensitive ? block.text : block.text.toLowerCase();
202
+ const searchable = [
203
+ getBlockTypeLabel(block),
204
+ block.text,
205
+ block.toolInput ?? "",
206
+ block.toolOutput ?? "",
207
+ ].join("\n");
208
+ const haystack = caseSensitive ? searchable : searchable.toLowerCase();
167
209
  const needle = caseSensitive ? query : query.toLowerCase();
168
210
  if (haystack.includes(needle)) {
169
211
  matches.push(i);
@@ -193,6 +235,20 @@ export function findNextSearchMatch(
193
235
 
194
236
  // ── Block text for copy/nvim ────────────────────────────────────────
195
237
 
238
+ export function hasToolPreview(block: PeekBlock | undefined): boolean {
239
+ return Boolean(
240
+ block &&
241
+ (block.type === "tool-call" || block.type === "tool-result") &&
242
+ (block.toolInput || block.toolOutput || (block.type === "tool-result" && block.text)),
243
+ );
244
+ }
245
+
246
+ export function getToolPreviewText(block: PeekBlock): string {
247
+ const input = block.toolInput || (block.type === "tool-call" ? block.text : "") || "(no input)";
248
+ const output = block.toolOutput || (block.type === "tool-result" ? block.text : "") || "(no output)";
249
+ return `Input:\n${input}\n\nOutput${block.isError ? " (error)" : ""}:\n${output}`;
250
+ }
251
+
196
252
  export function getBlockText(block: PeekBlock): string {
197
253
  switch (block.type) {
198
254
  case "user":
@@ -202,9 +258,9 @@ export function getBlockText(block: PeekBlock): string {
202
258
  case "assistant-text":
203
259
  return `## Assistant\n\n${block.text}`;
204
260
  case "tool-call":
205
- return `## Tool Call: ${block.toolName ?? "unknown"}`;
261
+ return `## Tool Call: ${block.toolName ?? "unknown"}\n\n${getToolPreviewText(block)}`;
206
262
  case "tool-result":
207
- return `## Tool Result${block.isError ? " (error)" : ""}: ${block.toolName ?? "unknown"}\n\n${block.text}`;
263
+ return `## Tool Result${block.isError ? " (error)" : ""}: ${block.toolName ?? "unknown"}\n\n${getToolPreviewText(block)}`;
208
264
  }
209
265
  }
210
266
 
@@ -224,3 +280,97 @@ export function getBlockTypeLabel(block: PeekBlock): string {
224
280
  return `Result${block.isError ? " (error)" : ""}: ${block.toolName ?? "?"}`;
225
281
  }
226
282
  }
283
+
284
+ /** Semantic labels used by the viewer surface; copy labels above stay stable. */
285
+ export function getSemanticBlockLabel(block: PeekBlock): string {
286
+ switch (block.type) {
287
+ case "user":
288
+ return "USER";
289
+ case "thinking":
290
+ return "THINKING";
291
+ case "assistant-text":
292
+ return "ASSISTANT";
293
+ case "tool-call":
294
+ return `TOOL ${block.toolName ?? "?"}`;
295
+ case "tool-result":
296
+ return `TOOL RESULT ${block.toolName ?? "?"}`;
297
+ }
298
+ }
299
+
300
+ export function getToolResultSummary(text: string): string {
301
+ const normalized = text.replace(/\r\n?/g, "\n");
302
+ const firstLine = normalized.split("\n").find((line) => line.trim().length > 0)?.trim() || "(no output)";
303
+ const lineCount = normalized ? normalized.split("\n").length : 0;
304
+ return `${firstLine} · ${lineCount}L/${normalized.length}c`;
305
+ }
306
+
307
+ // A normalized mouse event is intentionally independent of Pi's optional host
308
+ // mouse dispatch. Raw SGR parsing is bounded and does not enable terminal modes.
309
+ export type PeekMouseAction = "press" | "release" | "wheel" | "move" | "drag";
310
+ export interface PeekMouseEvent {
311
+ action: PeekMouseAction | "click";
312
+ button: number;
313
+ x: number;
314
+ y: number;
315
+ /** Consecutive click count when the host supplies one. */
316
+ clickCount?: number;
317
+ /** For wheel events, -1 is up and +1 is down. */
318
+ delta?: number;
319
+ }
320
+
321
+ export function parsePeekMouseInput(data: string): PeekMouseEvent | null {
322
+ const match = data.match(/^\x1b\[<(\d+);(\d+);(\d+)([mM])$/);
323
+ if (match) {
324
+ const encodedButton = Number(match[1]);
325
+ const x = Number(match[2]);
326
+ const y = Number(match[3]);
327
+ if (!Number.isSafeInteger(encodedButton) || !Number.isSafeInteger(x) || !Number.isSafeInteger(y)) return null;
328
+ if (x < 1 || y < 1 || x > 10000 || y > 10000) return null;
329
+
330
+ if ((encodedButton & 64) !== 0) {
331
+ return {
332
+ action: "wheel",
333
+ button: encodedButton & 3,
334
+ x,
335
+ y,
336
+ delta: (encodedButton & 1) === 0 ? -1 : 1,
337
+ };
338
+ }
339
+
340
+ return {
341
+ action: match[4] === "M" ? "press" : "release",
342
+ button: encodedButton & 3,
343
+ x,
344
+ y,
345
+ };
346
+ }
347
+
348
+ // Some terminals/multiplexers still emit the six-byte X10 mouse form.
349
+ // Pi's newer TUI normalizes this before dispatch, but accepting it here
350
+ // keeps the fallback handleInput path from dropping wheel events.
351
+ if (data.length !== 6 || !data.startsWith("\x1b[M")) return null;
352
+ const encodedButton = data.charCodeAt(3) - 32;
353
+ const x = data.charCodeAt(4) - 32;
354
+ const y = data.charCodeAt(5) - 32;
355
+ if (encodedButton < 0 || x < 1 || y < 1 || x > 223 || y > 223) return null;
356
+
357
+ if ((encodedButton & 64) !== 0) {
358
+ const direction = encodedButton & 3;
359
+ if (direction !== 0 && direction !== 1) return null;
360
+ return {
361
+ action: "wheel",
362
+ button: encodedButton & 3,
363
+ x,
364
+ y,
365
+ delta: direction === 0 ? -1 : 1,
366
+ };
367
+ }
368
+
369
+ const button = encodedButton & 3;
370
+ return {
371
+ action: button === 3 ? "release" : (encodedButton & 32) !== 0 ? "move" : "press",
372
+ button,
373
+ x,
374
+ y,
375
+ };
376
+ }