pi-render-btw 0.6.1 → 0.6.3
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/README.md +12 -0
- package/extensions/btw/peek.test.ts +276 -0
- package/extensions/btw/peek.ts +226 -0
- package/extensions/btw.ts +563 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,6 +21,7 @@ with **full markdown rendering** (syntax highlighting, bold, code blocks, lists,
|
|
|
21
21
|
- supports BTW-only model and thinking overrides without changing the main thread settings
|
|
22
22
|
- lets you inject the full thread, or a summary of it, back into the main agent
|
|
23
23
|
- optionally saves an individual BTW exchange as a visible session note with `--save`
|
|
24
|
+
- includes an internal Peek mode for browsing the current BTW side thread
|
|
24
25
|
|
|
25
26
|
## ✨ Markdown rendering
|
|
26
27
|
|
|
@@ -71,6 +72,7 @@ pi install /path/to/pi-render-btw
|
|
|
71
72
|
/btw:thinking low
|
|
72
73
|
/btw:inject implement the plan we just discussed
|
|
73
74
|
/btw:summarize turn that side thread into a short handoff
|
|
75
|
+
/btw:peek browse the current BTW side thread
|
|
74
76
|
/btw:clear
|
|
75
77
|
```
|
|
76
78
|
|
|
@@ -109,6 +111,16 @@ pi install /path/to/pi-render-btw
|
|
|
109
111
|
- opens or refreshes the same focused BTW modal shell
|
|
110
112
|
- with `--save`, also saves that single exchange as a visible session note
|
|
111
113
|
|
|
114
|
+
### `/btw:peek`
|
|
115
|
+
|
|
116
|
+
- switches the BTW overlay into Peek mode for the current side thread
|
|
117
|
+
- `p` also enters Peek mode when the BTW composer is empty
|
|
118
|
+
- `j/k` scroll one message, `J/K` jump to the next/previous message, and `g/G` go to the top/bottom
|
|
119
|
+
- `/` searches with smartcase; `n/N` moves between matches
|
|
120
|
+
- `t` toggles tool messages
|
|
121
|
+
- `M` copies the selected message; `o` opens it read-only in nvim
|
|
122
|
+
- `Esc` returns to the BTW composer
|
|
123
|
+
|
|
112
124
|
### `/btw:clear`
|
|
113
125
|
|
|
114
126
|
- dismisses the BTW modal/widget
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
buildPeekBlocks,
|
|
4
|
+
createPeekState,
|
|
5
|
+
findSearchMatches,
|
|
6
|
+
findNextSearchMatch,
|
|
7
|
+
getBlockText,
|
|
8
|
+
getBlockTypeLabel,
|
|
9
|
+
type PeekBlock,
|
|
10
|
+
type PeekEntry,
|
|
11
|
+
type PeekModeState,
|
|
12
|
+
} from "./peek.ts";
|
|
13
|
+
|
|
14
|
+
// ── Helpers ──────────────────────────────────────────────────────────
|
|
15
|
+
|
|
16
|
+
function makeEntry(
|
|
17
|
+
overrides: Partial<PeekEntry> & { type: PeekEntry["type"] },
|
|
18
|
+
): PeekEntry {
|
|
19
|
+
return {
|
|
20
|
+
id: overrides.id ?? 1,
|
|
21
|
+
turnId: overrides.turnId ?? 1,
|
|
22
|
+
type: overrides.type,
|
|
23
|
+
text: overrides.text,
|
|
24
|
+
content: overrides.content,
|
|
25
|
+
toolName: overrides.toolName,
|
|
26
|
+
isError: overrides.isError,
|
|
27
|
+
truncated: overrides.truncated,
|
|
28
|
+
streaming: overrides.streaming,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ── Tests ────────────────────────────────────────────────────────────
|
|
33
|
+
|
|
34
|
+
describe("buildPeekBlocks", () => {
|
|
35
|
+
it("converts user-message entries to user blocks", () => {
|
|
36
|
+
const entries: PeekEntry[] = [
|
|
37
|
+
makeEntry({ id: 1, type: "user-message", text: "Hello" }),
|
|
38
|
+
];
|
|
39
|
+
const blocks = buildPeekBlocks(entries, true);
|
|
40
|
+
expect(blocks).toHaveLength(1);
|
|
41
|
+
expect(blocks[0]).toMatchObject({ type: "user", text: "Hello" });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("converts assistant-text entries to assistant-text blocks", () => {
|
|
45
|
+
const entries: PeekEntry[] = [
|
|
46
|
+
makeEntry({ id: 1, type: "assistant-text", text: "Response", streaming: false }),
|
|
47
|
+
];
|
|
48
|
+
const blocks = buildPeekBlocks(entries, true);
|
|
49
|
+
expect(blocks).toHaveLength(1);
|
|
50
|
+
expect(blocks[0]).toMatchObject({ type: "assistant-text", text: "Response" });
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("converts thinking entries", () => {
|
|
54
|
+
const entries: PeekEntry[] = [
|
|
55
|
+
makeEntry({ id: 1, type: "thinking", text: "deep thoughts", streaming: false }),
|
|
56
|
+
];
|
|
57
|
+
const blocks = buildPeekBlocks(entries, true);
|
|
58
|
+
expect(blocks).toHaveLength(1);
|
|
59
|
+
expect(blocks[0]).toMatchObject({ type: "thinking", text: "deep thoughts" });
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("converts tool-call entries when showTools=true", () => {
|
|
63
|
+
const entries: PeekEntry[] = [
|
|
64
|
+
makeEntry({ id: 1, type: "tool-call", toolName: "read" }),
|
|
65
|
+
];
|
|
66
|
+
const blocks = buildPeekBlocks(entries, true);
|
|
67
|
+
expect(blocks).toHaveLength(1);
|
|
68
|
+
expect(blocks[0]).toMatchObject({ type: "tool-call", toolName: "read" });
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("filters out tool-call entries when showTools=false", () => {
|
|
72
|
+
const entries: PeekEntry[] = [
|
|
73
|
+
makeEntry({ id: 1, type: "tool-call", toolName: "read" }),
|
|
74
|
+
];
|
|
75
|
+
const blocks = buildPeekBlocks(entries, false);
|
|
76
|
+
expect(blocks).toHaveLength(0);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("converts tool-result entries when showTools=true", () => {
|
|
80
|
+
const entries: PeekEntry[] = [
|
|
81
|
+
makeEntry({ id: 1, type: "tool-result", toolName: "read", content: "file content", isError: false }),
|
|
82
|
+
];
|
|
83
|
+
const blocks = buildPeekBlocks(entries, true);
|
|
84
|
+
expect(blocks).toHaveLength(1);
|
|
85
|
+
expect(blocks[0]).toMatchObject({ type: "tool-result", toolName: "read", text: "file content" });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("filters out tool-result entries when showTools=false", () => {
|
|
89
|
+
const entries: PeekEntry[] = [
|
|
90
|
+
makeEntry({ id: 1, type: "tool-result", toolName: "read", content: "file content", isError: false }),
|
|
91
|
+
];
|
|
92
|
+
const blocks = buildPeekBlocks(entries, false);
|
|
93
|
+
expect(blocks).toHaveLength(0);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("skips turn-boundary entries", () => {
|
|
97
|
+
const entries: PeekEntry[] = [
|
|
98
|
+
makeEntry({ id: 1, type: "turn-boundary" }),
|
|
99
|
+
makeEntry({ id: 2, type: "user-message", text: "Hi" }),
|
|
100
|
+
];
|
|
101
|
+
const blocks = buildPeekBlocks(entries, true);
|
|
102
|
+
expect(blocks).toHaveLength(1);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("produces sequential blocks from transcript entries in order", () => {
|
|
106
|
+
const entries: PeekEntry[] = [
|
|
107
|
+
makeEntry({ id: 1, type: "user-message", text: "Q1" }),
|
|
108
|
+
makeEntry({ id: 2, type: "thinking", text: "thinking..." }),
|
|
109
|
+
makeEntry({ id: 3, type: "tool-call", toolName: "read" }),
|
|
110
|
+
makeEntry({ id: 4, type: "tool-result", toolName: "read", content: "data" }),
|
|
111
|
+
makeEntry({ id: 5, type: "assistant-text", text: "A1" }),
|
|
112
|
+
];
|
|
113
|
+
const blocks = buildPeekBlocks(entries, true);
|
|
114
|
+
expect(blocks).toHaveLength(5);
|
|
115
|
+
expect(blocks.map((b) => b.type)).toEqual([
|
|
116
|
+
"user",
|
|
117
|
+
"thinking",
|
|
118
|
+
"tool-call",
|
|
119
|
+
"tool-result",
|
|
120
|
+
"assistant-text",
|
|
121
|
+
]);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("createPeekState", () => {
|
|
126
|
+
it("returns inactive state with defaults", () => {
|
|
127
|
+
const state = createPeekState();
|
|
128
|
+
expect(state.active).toBe(false);
|
|
129
|
+
expect(state.blocks).toEqual([]);
|
|
130
|
+
expect(state.selectedIndex).toBe(0);
|
|
131
|
+
expect(state.scrollOffset).toBe(0);
|
|
132
|
+
expect(state.showTools).toBe(true);
|
|
133
|
+
expect(state.smartCase).toBe(true);
|
|
134
|
+
expect(state.searchActive).toBe(false);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe("findSearchMatches", () => {
|
|
139
|
+
const blocks: PeekBlock[] = [
|
|
140
|
+
{ id: 0, entryId: 1, turnId: 1, type: "user", text: "Hello World" },
|
|
141
|
+
{ id: 1, entryId: 2, turnId: 1, type: "assistant-text", text: "Hello there" },
|
|
142
|
+
{ id: 2, entryId: 3, turnId: 2, type: "user", text: "How are you?" },
|
|
143
|
+
{ id: 3, entryId: 4, turnId: 2, type: "assistant-text", text: "I'm fine, thanks!" },
|
|
144
|
+
{ id: 4, entryId: 5, turnId: 3, type: "user", text: "Hello Hello" },
|
|
145
|
+
];
|
|
146
|
+
|
|
147
|
+
it("finds all blocks containing query text", () => {
|
|
148
|
+
const matches = findSearchMatches(blocks, "Hello", true);
|
|
149
|
+
expect(matches).toEqual([0, 1, 4]);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("is smartcase: case insensitive when query is lowercase", () => {
|
|
153
|
+
const matches = findSearchMatches(blocks, "hello", true);
|
|
154
|
+
expect(matches).toEqual([0, 1, 4]);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("is case sensitive when query contains uppercase (smartcase)", () => {
|
|
158
|
+
const matches = findSearchMatches(blocks, "Hello", true);
|
|
159
|
+
expect(matches).toEqual([0, 1, 4]);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("returns empty array for no matches", () => {
|
|
163
|
+
const matches = findSearchMatches(blocks, "xyz", true);
|
|
164
|
+
expect(matches).toEqual([]);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("returns empty array for empty query", () => {
|
|
168
|
+
const matches = findSearchMatches(blocks, "", true);
|
|
169
|
+
expect(matches).toEqual([]);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("matches in result blocks too", () => {
|
|
173
|
+
const blocksWithResult: PeekBlock[] = [
|
|
174
|
+
{ id: 0, entryId: 1, turnId: 1, type: "user", text: "Search for this" },
|
|
175
|
+
{ id: 1, entryId: 2, turnId: 1, type: "tool-result", text: "result data here", toolName: "read" },
|
|
176
|
+
];
|
|
177
|
+
const matches = findSearchMatches(blocksWithResult, "data", true);
|
|
178
|
+
expect(matches).toEqual([1]);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe("findNextSearchMatch", () => {
|
|
183
|
+
const blocks: PeekBlock[] = [
|
|
184
|
+
{ id: 0, entryId: 1, turnId: 1, type: "user", text: "Hello" },
|
|
185
|
+
{ id: 1, entryId: 2, turnId: 1, type: "assistant-text", text: "Hi" },
|
|
186
|
+
{ id: 2, entryId: 3, turnId: 2, type: "user", text: "Hello again" },
|
|
187
|
+
];
|
|
188
|
+
const matches = [0, 2];
|
|
189
|
+
|
|
190
|
+
it("finds first match when no current match", () => {
|
|
191
|
+
const result = findNextSearchMatch(blocks, matches, -1, true);
|
|
192
|
+
expect(result).toEqual({ matchIndex: 0, blockIndex: 0 });
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("finds next match forward", () => {
|
|
196
|
+
const result = findNextSearchMatch(blocks, matches, 0, true);
|
|
197
|
+
expect(result).toEqual({ matchIndex: 1, blockIndex: 2 });
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("wraps to the first match when going forward from the last match", () => {
|
|
201
|
+
const result = findNextSearchMatch(blocks, matches, 1, true);
|
|
202
|
+
expect(result).toEqual({ matchIndex: 0, blockIndex: 0 });
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("finds first match from end when going backward with no current", () => {
|
|
206
|
+
const result = findNextSearchMatch(blocks, matches, -1, false);
|
|
207
|
+
expect(result).toEqual({ matchIndex: 1, blockIndex: 2 });
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("finds previous match backward", () => {
|
|
211
|
+
const result = findNextSearchMatch(blocks, matches, 1, false);
|
|
212
|
+
expect(result).toEqual({ matchIndex: 0, blockIndex: 0 });
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("wraps to the last match when going backward from the first match", () => {
|
|
216
|
+
const result = findNextSearchMatch(blocks, matches, 0, false);
|
|
217
|
+
expect(result).toEqual({ matchIndex: 1, blockIndex: 2 });
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("returns null for empty matches", () => {
|
|
221
|
+
const result = findNextSearchMatch(blocks, [], -1, true);
|
|
222
|
+
expect(result).toBeNull();
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe("getBlockText", () => {
|
|
227
|
+
it("formats user block", () => {
|
|
228
|
+
const text = getBlockText({ id: 0, entryId: 1, turnId: 1, type: "user", text: "Hello" });
|
|
229
|
+
expect(text).toContain("## You");
|
|
230
|
+
expect(text).toContain("Hello");
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("formats assistant-text block", () => {
|
|
234
|
+
const text = getBlockText({ id: 0, entryId: 1, turnId: 1, type: "assistant-text", text: "Response" });
|
|
235
|
+
expect(text).toContain("## Assistant");
|
|
236
|
+
expect(text).toContain("Response");
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("formats thinking block", () => {
|
|
240
|
+
const text = getBlockText({ id: 0, entryId: 1, turnId: 1, type: "thinking", text: "thinking..." });
|
|
241
|
+
expect(text).toContain("## Thinking");
|
|
242
|
+
expect(text).toContain("thinking...");
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("formats tool-call block", () => {
|
|
246
|
+
const text = getBlockText({ id: 0, entryId: 1, turnId: 1, type: "tool-call", text: "", toolName: "read" });
|
|
247
|
+
expect(text).toContain("## Tool Call");
|
|
248
|
+
expect(text).toContain("read");
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("formats tool-result block", () => {
|
|
252
|
+
const text = getBlockText({ id: 0, entryId: 1, turnId: 1, type: "tool-result", text: "content", toolName: "bash" });
|
|
253
|
+
expect(text).toContain("## Tool Result");
|
|
254
|
+
expect(text).toContain("bash");
|
|
255
|
+
expect(text).toContain("content");
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("marks error on tool-result when isError", () => {
|
|
259
|
+
const text = getBlockText({ id: 0, entryId: 1, turnId: 1, type: "tool-result", text: "error", toolName: "bash", isError: true });
|
|
260
|
+
expect(text).toContain("error");
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
describe("getBlockTypeLabel", () => {
|
|
265
|
+
it("returns 'You' for user", () => {
|
|
266
|
+
expect(getBlockTypeLabel({ id: 0, entryId: 1, turnId: 1, type: "user", text: "" })).toBe("You");
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it("returns 'Assistant' for assistant-text", () => {
|
|
270
|
+
expect(getBlockTypeLabel({ id: 0, entryId: 1, turnId: 1, type: "assistant-text", text: "" })).toBe("Assistant");
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it("includes toolName in tool-call label", () => {
|
|
274
|
+
expect(getBlockTypeLabel({ id: 0, entryId: 1, turnId: 1, type: "tool-call", text: "", toolName: "read" })).toBe("Tool: read");
|
|
275
|
+
});
|
|
276
|
+
});
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BTW Peek Mode — standalone peek/navigation over a BTW transcript.
|
|
3
|
+
*
|
|
4
|
+
* Provides state management, key handling, and rendering helpers.
|
|
5
|
+
* The actual render integration lives in BtwOverlayComponent (btw.ts).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// ── Minimal entry shape (subset of BtwTranscriptEntry) ──────────────
|
|
9
|
+
|
|
10
|
+
export interface PeekEntry {
|
|
11
|
+
id: number;
|
|
12
|
+
type: "user-message" | "thinking" | "assistant-text" | "tool-call" | "tool-result" | "turn-boundary";
|
|
13
|
+
turnId: number;
|
|
14
|
+
text?: string;
|
|
15
|
+
content?: string;
|
|
16
|
+
toolName?: string;
|
|
17
|
+
isError?: boolean;
|
|
18
|
+
truncated?: boolean;
|
|
19
|
+
streaming?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// ── Peek Block (one logical "message" unit) ─────────────────────────
|
|
23
|
+
|
|
24
|
+
export type PeekBlockType =
|
|
25
|
+
| "user"
|
|
26
|
+
| "thinking"
|
|
27
|
+
| "assistant-text"
|
|
28
|
+
| "tool-call"
|
|
29
|
+
| "tool-result";
|
|
30
|
+
|
|
31
|
+
export interface PeekBlock {
|
|
32
|
+
id: number;
|
|
33
|
+
entryId: number;
|
|
34
|
+
turnId: number;
|
|
35
|
+
type: PeekBlockType;
|
|
36
|
+
text: string;
|
|
37
|
+
toolName?: string;
|
|
38
|
+
isError?: boolean;
|
|
39
|
+
truncated?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// ── Peek Mode State ─────────────────────────────────────────────────
|
|
43
|
+
|
|
44
|
+
export interface PeekModeState {
|
|
45
|
+
active: boolean;
|
|
46
|
+
blocks: PeekBlock[];
|
|
47
|
+
/** Index into `blocks` that the cursor is on */
|
|
48
|
+
selectedIndex: number;
|
|
49
|
+
/** Scroll offset in rendered lines */
|
|
50
|
+
scrollOffset: number;
|
|
51
|
+
/** Viewport height in lines at last render */
|
|
52
|
+
viewportHeight: number;
|
|
53
|
+
/** Search state */
|
|
54
|
+
searchQuery: string;
|
|
55
|
+
/** True while user is typing a search query */
|
|
56
|
+
searchActive: boolean;
|
|
57
|
+
/** Indices into `blocks` that match the search */
|
|
58
|
+
searchMatches: number[];
|
|
59
|
+
/** Index into searchMatches[] for the current match */
|
|
60
|
+
currentMatchIndex: number;
|
|
61
|
+
/** Case-insensitive search when query is all-lowercase */
|
|
62
|
+
smartCase: boolean;
|
|
63
|
+
/** Show/hide tool-call and tool-result blocks */
|
|
64
|
+
showTools: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function createPeekState(): PeekModeState {
|
|
68
|
+
return {
|
|
69
|
+
active: false,
|
|
70
|
+
blocks: [],
|
|
71
|
+
selectedIndex: 0,
|
|
72
|
+
scrollOffset: 0,
|
|
73
|
+
viewportHeight: 10,
|
|
74
|
+
searchQuery: "",
|
|
75
|
+
searchActive: false,
|
|
76
|
+
searchMatches: [],
|
|
77
|
+
currentMatchIndex: -1,
|
|
78
|
+
smartCase: true,
|
|
79
|
+
showTools: true,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// ── Block Builder ───────────────────────────────────────────────────
|
|
84
|
+
|
|
85
|
+
export function buildPeekBlocks(
|
|
86
|
+
entries: PeekEntry[],
|
|
87
|
+
showTools: boolean,
|
|
88
|
+
): PeekBlock[] {
|
|
89
|
+
const blocks: PeekBlock[] = [];
|
|
90
|
+
let id = 0;
|
|
91
|
+
|
|
92
|
+
for (const entry of entries) {
|
|
93
|
+
if (entry.type === "turn-boundary") continue;
|
|
94
|
+
|
|
95
|
+
if (entry.type === "user-message") {
|
|
96
|
+
blocks.push({
|
|
97
|
+
id: id++,
|
|
98
|
+
entryId: entry.id,
|
|
99
|
+
turnId: entry.turnId,
|
|
100
|
+
type: "user",
|
|
101
|
+
text: entry.text ?? "",
|
|
102
|
+
});
|
|
103
|
+
} else if (entry.type === "thinking") {
|
|
104
|
+
blocks.push({
|
|
105
|
+
id: id++,
|
|
106
|
+
entryId: entry.id,
|
|
107
|
+
turnId: entry.turnId,
|
|
108
|
+
type: "thinking",
|
|
109
|
+
text: entry.text ?? "",
|
|
110
|
+
});
|
|
111
|
+
} else if (entry.type === "assistant-text") {
|
|
112
|
+
blocks.push({
|
|
113
|
+
id: id++,
|
|
114
|
+
entryId: entry.id,
|
|
115
|
+
turnId: entry.turnId,
|
|
116
|
+
type: "assistant-text",
|
|
117
|
+
text: entry.text ?? "",
|
|
118
|
+
});
|
|
119
|
+
} else if (entry.type === "tool-call") {
|
|
120
|
+
if (!showTools) continue;
|
|
121
|
+
blocks.push({
|
|
122
|
+
id: id++,
|
|
123
|
+
entryId: entry.id,
|
|
124
|
+
turnId: entry.turnId,
|
|
125
|
+
type: "tool-call",
|
|
126
|
+
text: "",
|
|
127
|
+
toolName: entry.toolName,
|
|
128
|
+
});
|
|
129
|
+
} else if (entry.type === "tool-result") {
|
|
130
|
+
if (!showTools) continue;
|
|
131
|
+
blocks.push({
|
|
132
|
+
id: id++,
|
|
133
|
+
entryId: entry.id,
|
|
134
|
+
turnId: entry.turnId,
|
|
135
|
+
type: "tool-result",
|
|
136
|
+
text: entry.content ?? entry.text ?? "",
|
|
137
|
+
toolName: entry.toolName,
|
|
138
|
+
isError: entry.isError,
|
|
139
|
+
truncated: entry.truncated,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return blocks;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ── Search ──────────────────────────────────────────────────────────
|
|
148
|
+
|
|
149
|
+
function isSmartCaseQuery(query: string): boolean {
|
|
150
|
+
// smartcase: case-insensitive when query is all-lowercase
|
|
151
|
+
return query === query.toLowerCase();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function findSearchMatches(
|
|
155
|
+
blocks: PeekBlock[],
|
|
156
|
+
query: string,
|
|
157
|
+
smartCase: boolean,
|
|
158
|
+
): number[] {
|
|
159
|
+
if (!query) return [];
|
|
160
|
+
|
|
161
|
+
const caseSensitive = smartCase && !isSmartCaseQuery(query);
|
|
162
|
+
const matches: number[] = [];
|
|
163
|
+
|
|
164
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
165
|
+
const block = blocks[i];
|
|
166
|
+
const haystack = caseSensitive ? block.text : block.text.toLowerCase();
|
|
167
|
+
const needle = caseSensitive ? query : query.toLowerCase();
|
|
168
|
+
if (haystack.includes(needle)) {
|
|
169
|
+
matches.push(i);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return matches;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function findNextSearchMatch(
|
|
177
|
+
blocks: PeekBlock[],
|
|
178
|
+
matches: number[],
|
|
179
|
+
currentIndex: number,
|
|
180
|
+
forward: boolean,
|
|
181
|
+
): { matchIndex: number; blockIndex: number } | null {
|
|
182
|
+
if (matches.length === 0) return null;
|
|
183
|
+
|
|
184
|
+
if (currentIndex < 0 || currentIndex >= matches.length) {
|
|
185
|
+
const idx = forward ? 0 : matches.length - 1;
|
|
186
|
+
return { matchIndex: idx, blockIndex: matches[idx] };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const next = currentIndex + (forward ? 1 : -1);
|
|
190
|
+
const wrapped = (next + matches.length) % matches.length;
|
|
191
|
+
return { matchIndex: wrapped, blockIndex: matches[wrapped] };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// ── Block text for copy/nvim ────────────────────────────────────────
|
|
195
|
+
|
|
196
|
+
export function getBlockText(block: PeekBlock): string {
|
|
197
|
+
switch (block.type) {
|
|
198
|
+
case "user":
|
|
199
|
+
return `## You\n\n${block.text}`;
|
|
200
|
+
case "thinking":
|
|
201
|
+
return `## Thinking\n\n${block.text}`;
|
|
202
|
+
case "assistant-text":
|
|
203
|
+
return `## Assistant\n\n${block.text}`;
|
|
204
|
+
case "tool-call":
|
|
205
|
+
return `## Tool Call: ${block.toolName ?? "unknown"}`;
|
|
206
|
+
case "tool-result":
|
|
207
|
+
return `## Tool Result${block.isError ? " (error)" : ""}: ${block.toolName ?? "unknown"}\n\n${block.text}`;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// ── Block type label (for display) ──────────────────────────────────
|
|
212
|
+
|
|
213
|
+
export function getBlockTypeLabel(block: PeekBlock): string {
|
|
214
|
+
switch (block.type) {
|
|
215
|
+
case "user":
|
|
216
|
+
return "You";
|
|
217
|
+
case "thinking":
|
|
218
|
+
return "Thinking";
|
|
219
|
+
case "assistant-text":
|
|
220
|
+
return "Assistant";
|
|
221
|
+
case "tool-call":
|
|
222
|
+
return `Tool: ${block.toolName ?? "?"}`;
|
|
223
|
+
case "tool-result":
|
|
224
|
+
return `Result${block.isError ? " (error)" : ""}: ${block.toolName ?? "?"}`;
|
|
225
|
+
}
|
|
226
|
+
}
|
package/extensions/btw.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
1
5
|
import {
|
|
2
6
|
buildSessionContext,
|
|
7
|
+
copyToClipboard,
|
|
3
8
|
createAgentSession,
|
|
4
9
|
createExtensionRuntime,
|
|
5
10
|
getMarkdownTheme,
|
|
@@ -28,6 +33,17 @@ import {
|
|
|
28
33
|
type TUI,
|
|
29
34
|
} from "@earendil-works/pi-tui";
|
|
30
35
|
import { TextArea } from "./text-area.ts";
|
|
36
|
+
import {
|
|
37
|
+
buildPeekBlocks,
|
|
38
|
+
createPeekState,
|
|
39
|
+
findNextSearchMatch,
|
|
40
|
+
findSearchMatches,
|
|
41
|
+
getBlockText,
|
|
42
|
+
getBlockTypeLabel,
|
|
43
|
+
type PeekBlock,
|
|
44
|
+
type PeekEntry,
|
|
45
|
+
type PeekModeState,
|
|
46
|
+
} from "./btw/peek.ts";
|
|
31
47
|
|
|
32
48
|
const BTW_MESSAGE_TYPE = "btw-note";
|
|
33
49
|
const BTW_ENTRY_TYPE = "btw-thread-entry";
|
|
@@ -153,6 +169,7 @@ type OverlayRuntime = {
|
|
|
153
169
|
close?: () => void;
|
|
154
170
|
finish?: () => void;
|
|
155
171
|
setDraft?: (value: string) => void;
|
|
172
|
+
peek?: () => void;
|
|
156
173
|
closed?: boolean;
|
|
157
174
|
};
|
|
158
175
|
|
|
@@ -1046,6 +1063,12 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1046
1063
|
private hintsTextValue = "";
|
|
1047
1064
|
private mdTheme: ReturnType<typeof getMarkdownTheme>;
|
|
1048
1065
|
private mdLineCache: Map<number, { lines: string[]; renderedAtWidth: number }> = new Map();
|
|
1066
|
+
private peekState: PeekModeState;
|
|
1067
|
+
private peekBlocks: PeekBlock[] = [];
|
|
1068
|
+
private peekRenderedLines: string[] = [];
|
|
1069
|
+
private peekBlockStarts: number[] = [];
|
|
1070
|
+
private peekHeaderLines: string[] = [];
|
|
1071
|
+
private peekSearchBuffer = "";
|
|
1049
1072
|
|
|
1050
1073
|
get focused(): boolean {
|
|
1051
1074
|
return this._focused;
|
|
@@ -1091,6 +1114,7 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1091
1114
|
this.onDismissCallback();
|
|
1092
1115
|
};
|
|
1093
1116
|
|
|
1117
|
+
this.peekState = createPeekState();
|
|
1094
1118
|
this.hintsText = new Text("", 1, 0);
|
|
1095
1119
|
this.mdTheme = getMarkdownTheme();
|
|
1096
1120
|
|
|
@@ -1297,7 +1321,23 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1297
1321
|
|
|
1298
1322
|
const mouseScrollDelta = this.getMouseScrollDelta(data);
|
|
1299
1323
|
if (mouseScrollDelta !== null) {
|
|
1300
|
-
this.
|
|
1324
|
+
if (this.peekState.active) {
|
|
1325
|
+
this.scrollPeek(mouseScrollDelta);
|
|
1326
|
+
} else {
|
|
1327
|
+
this.scrollTranscript(mouseScrollDelta);
|
|
1328
|
+
}
|
|
1329
|
+
return;
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
// Peek mode active → route all input to peek handler
|
|
1333
|
+
if (this.peekState.active) {
|
|
1334
|
+
this.handlePeekInput(data);
|
|
1335
|
+
return;
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
// Chat mode: 'p' with empty input enters peek mode
|
|
1339
|
+
if ((data === "p" || data === "P") && this.input.getValue().length === 0) {
|
|
1340
|
+
this.enterPeekMode();
|
|
1301
1341
|
return;
|
|
1302
1342
|
}
|
|
1303
1343
|
|
|
@@ -1320,6 +1360,11 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1320
1360
|
const dialogWidth = Math.max(24, width);
|
|
1321
1361
|
const innerWidth = Math.max(22, dialogWidth - 2);
|
|
1322
1362
|
|
|
1363
|
+
// Peek mode has its own full-content render
|
|
1364
|
+
if (this.peekState.active) {
|
|
1365
|
+
return this.renderPeekMode(innerWidth, width);
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1323
1368
|
// Compute input lines (cap at 6 for layout stability)
|
|
1324
1369
|
const rawInputLines = this.input.render(innerWidth);
|
|
1325
1370
|
const inputLineCount = Math.max(1, Math.min(rawInputLines.length, 6));
|
|
@@ -1393,6 +1438,11 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1393
1438
|
}
|
|
1394
1439
|
|
|
1395
1440
|
refresh(): void {
|
|
1441
|
+
// Rebuild peek blocks if in peek mode
|
|
1442
|
+
if (this.peekState.active) {
|
|
1443
|
+
this.rebuildPeekBlocks();
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1396
1446
|
this.modeTextValue = `${getOverlayTitle(this.getMode())} · hidden thread preserved`;
|
|
1397
1447
|
this.modeText.setText(this.modeTextValue);
|
|
1398
1448
|
const entries = this.readTranscriptEntries();
|
|
@@ -1410,6 +1460,495 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1410
1460
|
this.hintsText.setText(this.hintsTextValue);
|
|
1411
1461
|
this.tui.requestRender();
|
|
1412
1462
|
}
|
|
1463
|
+
|
|
1464
|
+
// ── Peek Mode ──────────────────────────────────────────────────────
|
|
1465
|
+
|
|
1466
|
+
private renderPeekMode(innerWidth: number, fullWidth: number): string[] {
|
|
1467
|
+
this.peekState.viewportHeight = Math.max(6, this.getDialogHeight(1) - 8);
|
|
1468
|
+
const vh = this.peekState.viewportHeight;
|
|
1469
|
+
const lines: string[] = [];
|
|
1470
|
+
|
|
1471
|
+
// ── Header ───────────────────────────────────────────────────────
|
|
1472
|
+
const blockCount = this.peekBlocks.length;
|
|
1473
|
+
const pct =
|
|
1474
|
+
blockCount > 0
|
|
1475
|
+
? `${Math.round((this.peekState.selectedIndex / Math.max(1, blockCount - 1)) * 100)}%`
|
|
1476
|
+
: "";
|
|
1477
|
+
const position = blockCount > 0 ? `${this.peekState.selectedIndex + 1}/${blockCount}` : "0/0";
|
|
1478
|
+
const title = `BTW Peek ${position} ${pct}` +
|
|
1479
|
+
(this.peekState.searchMatches.length > 0
|
|
1480
|
+
? ` search ${this.peekState.currentMatchIndex + 1}/${this.peekState.searchMatches.length}`
|
|
1481
|
+
: "");
|
|
1482
|
+
lines.push(this.borderLine(innerWidth, "top"));
|
|
1483
|
+
lines.push(this.frameLine(this.theme.fg("accent", this.theme.bold(title)), innerWidth));
|
|
1484
|
+
lines.push(this.ruleLine(innerWidth));
|
|
1485
|
+
|
|
1486
|
+
// ── Search bar ───────────────────────────────────────────────────
|
|
1487
|
+
if (this.peekState.searchActive) {
|
|
1488
|
+
const searchLine = `/ ${this.peekSearchBuffer}${this.theme.fg("warning", "█")}`;
|
|
1489
|
+
lines.push(this.frameLine(this.theme.fg("accent", searchLine), innerWidth));
|
|
1490
|
+
lines.push(this.ruleLine(innerWidth));
|
|
1491
|
+
// Fill rest with blank lines
|
|
1492
|
+
for (let i = 0; i < vh; i++) {
|
|
1493
|
+
lines.push(this.frameLine("", innerWidth));
|
|
1494
|
+
}
|
|
1495
|
+
lines.push(this.ruleLine(innerWidth));
|
|
1496
|
+
lines.push(this.frameLine(this.theme.fg("dim", "Enter: search · Esc: cancel"), innerWidth));
|
|
1497
|
+
lines.push(this.borderLine(innerWidth, "bottom"));
|
|
1498
|
+
return lines.map((l) => this.fitRenderedLine(l, fullWidth));
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
// ── Content ──────────────────────────────────────────────────────
|
|
1502
|
+
const mdContentWidth = Math.max(1, innerWidth - 6);
|
|
1503
|
+
// Build rendered lines and remember each block's starting line so j/k
|
|
1504
|
+
// can scroll by line while J/K still jump by message.
|
|
1505
|
+
const blockRenderLines: string[][] = [];
|
|
1506
|
+
this.peekBlockStarts = [];
|
|
1507
|
+
this.peekHeaderLines = [];
|
|
1508
|
+
let renderedLineCount = 0;
|
|
1509
|
+
|
|
1510
|
+
for (const block of this.peekBlocks) {
|
|
1511
|
+
const blockLines: string[] = [];
|
|
1512
|
+
this.peekBlockStarts.push(renderedLineCount);
|
|
1513
|
+
const label = getBlockTypeLabel(block);
|
|
1514
|
+
const isCurrent = block.id === this.peekBlocks[this.peekState.selectedIndex]?.id;
|
|
1515
|
+
|
|
1516
|
+
// Block header line
|
|
1517
|
+
const prefix = isCurrent ? "▎ " : " ";
|
|
1518
|
+
if (block.type === "thinking") {
|
|
1519
|
+
blockLines.push(`${prefix}${this.theme.fg("warning", this.theme.bold(this.theme.italic(`── ${label} ──`)))}`);
|
|
1520
|
+
} else if (block.type === "tool-call") {
|
|
1521
|
+
blockLines.push(`${prefix}${this.theme.fg("toolTitle", this.theme.bold(`── ${label} ──`))}`);
|
|
1522
|
+
} else if (block.type === "tool-result") {
|
|
1523
|
+
const errPrefix = block.isError ? this.theme.fg("error", " ⚠") : "";
|
|
1524
|
+
blockLines.push(`${prefix}${this.theme.fg("dim", this.theme.bold(`── ${label} ──`))}${errPrefix}`);
|
|
1525
|
+
} else if (block.type === "user") {
|
|
1526
|
+
blockLines.push(`${prefix}${this.theme.fg("accent", this.theme.bold(`── ${label} ──`))}`);
|
|
1527
|
+
} else {
|
|
1528
|
+
blockLines.push(`${prefix}${this.theme.fg("success", this.theme.bold(`── ${label} ──`))}`);
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1531
|
+
// Block content
|
|
1532
|
+
if (block.text) {
|
|
1533
|
+
if (block.type === "assistant-text") {
|
|
1534
|
+
// Use Markdown for assistant text
|
|
1535
|
+
const md = new Markdown(block.text, 0, 0, this.mdTheme);
|
|
1536
|
+
const mdLines = md.render(mdContentWidth);
|
|
1537
|
+
for (const mdLine of mdLines) {
|
|
1538
|
+
blockLines.push(`${isCurrent ? "▎ " : " "}${mdLine}`);
|
|
1539
|
+
}
|
|
1540
|
+
} else if (block.type === "tool-result" && block.isError) {
|
|
1541
|
+
const styledLines = wrapTextWithAnsi(block.text, mdContentWidth);
|
|
1542
|
+
for (const sl of styledLines) {
|
|
1543
|
+
blockLines.push(`${isCurrent ? "▎ " : " "}${this.theme.fg("error", sl)}`);
|
|
1544
|
+
}
|
|
1545
|
+
} else if (block.type === "thinking") {
|
|
1546
|
+
const styledLines = wrapTextWithAnsi(block.text, mdContentWidth);
|
|
1547
|
+
for (const sl of styledLines) {
|
|
1548
|
+
blockLines.push(`${isCurrent ? "▎ " : " "}${this.theme.fg("warning", this.theme.italic(sl))}`);
|
|
1549
|
+
}
|
|
1550
|
+
} else {
|
|
1551
|
+
const styledLines = wrapTextWithAnsi(block.text, mdContentWidth);
|
|
1552
|
+
for (const sl of styledLines) {
|
|
1553
|
+
blockLines.push(`${isCurrent ? "▎ " : " "}${sl}`);
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
// Separator between blocks
|
|
1559
|
+
blockLines.push("");
|
|
1560
|
+
|
|
1561
|
+
blockRenderLines.push(blockLines);
|
|
1562
|
+
this.peekHeaderLines.push(blockLines[0] ?? "");
|
|
1563
|
+
renderedLineCount += blockLines.length;
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
this.peekRenderedLines = blockRenderLines.flat();
|
|
1567
|
+
const maxScroll = Math.max(0, this.peekRenderedLines.length - vh);
|
|
1568
|
+
this.peekState.scrollOffset = Math.max(0, Math.min(this.peekState.scrollOffset, maxScroll));
|
|
1569
|
+
|
|
1570
|
+
const visibleStart = this.peekState.scrollOffset;
|
|
1571
|
+
const displayLines = this.peekRenderedLines.slice(visibleStart, visibleStart + vh);
|
|
1572
|
+
if (blockCount === 0) {
|
|
1573
|
+
displayLines.push(this.theme.fg("dim", "No BTW thread yet."));
|
|
1574
|
+
}
|
|
1575
|
+
const selectedStart = this.peekBlockStarts[this.peekState.selectedIndex];
|
|
1576
|
+
if (selectedStart !== undefined && selectedStart < visibleStart && displayLines.length > 0) {
|
|
1577
|
+
displayLines[0] = this.peekHeaderLines[this.peekState.selectedIndex] ?? displayLines[0];
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
while (displayLines.length < vh) {
|
|
1581
|
+
displayLines.push("");
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1584
|
+
// Frame each line
|
|
1585
|
+
for (const line of displayLines) {
|
|
1586
|
+
lines.push(this.frameLine(line, innerWidth));
|
|
1587
|
+
}
|
|
1588
|
+
|
|
1589
|
+
// ── Status bar ───────────────────────────────────────────────────
|
|
1590
|
+
const searchHint = this.peekState.searchMatches.length > 0
|
|
1591
|
+
? ` ${this.peekState.currentMatchIndex + 1}/${this.peekState.searchMatches.length}`
|
|
1592
|
+
: "";
|
|
1593
|
+
const toolsHint = this.peekState.showTools ? "t:hide" : "t:show";
|
|
1594
|
+
const statusText = `j/k ↑↓ g/G /search${searchHint} ${toolsHint} M:copy o:nvim Esc`;
|
|
1595
|
+
lines.push(this.ruleLine(innerWidth));
|
|
1596
|
+
lines.push(this.frameLine(this.theme.fg("dim", statusText), innerWidth));
|
|
1597
|
+
lines.push(this.borderLine(innerWidth, "bottom"));
|
|
1598
|
+
|
|
1599
|
+
return lines.map((l) => this.fitRenderedLine(l, fullWidth));
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
enterPeekMode(): void {
|
|
1603
|
+
this.peekState = createPeekState();
|
|
1604
|
+
this.peekState.active = true;
|
|
1605
|
+
const entries = this.readTranscriptEntries();
|
|
1606
|
+
this.peekState.showTools = true;
|
|
1607
|
+
this.peekBlocks = buildPeekBlocks(
|
|
1608
|
+
entries as unknown as PeekEntry[],
|
|
1609
|
+
this.peekState.showTools,
|
|
1610
|
+
);
|
|
1611
|
+
this.peekState.selectedIndex = 0;
|
|
1612
|
+
this.peekState.scrollOffset = 0;
|
|
1613
|
+
this.tui.requestRender();
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
exitPeekMode(): void {
|
|
1617
|
+
this.peekState.active = false;
|
|
1618
|
+
this.peekState.searchActive = false;
|
|
1619
|
+
this.peekSearchBuffer = "";
|
|
1620
|
+
this.tui.requestRender();
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1623
|
+
private rebuildPeekBlocks(): void {
|
|
1624
|
+
const entries = this.readTranscriptEntries();
|
|
1625
|
+
this.peekBlocks = buildPeekBlocks(
|
|
1626
|
+
entries as unknown as PeekEntry[],
|
|
1627
|
+
this.peekState.showTools,
|
|
1628
|
+
);
|
|
1629
|
+
|
|
1630
|
+
const maxIndex = Math.max(0, this.peekBlocks.length - 1);
|
|
1631
|
+
this.peekState.selectedIndex = Math.min(this.peekState.selectedIndex, maxIndex);
|
|
1632
|
+
this.peekState.scrollOffset = Math.max(0, this.peekState.scrollOffset);
|
|
1633
|
+
if (this.peekState.searchQuery) {
|
|
1634
|
+
this.peekState.searchMatches = findSearchMatches(
|
|
1635
|
+
this.peekBlocks,
|
|
1636
|
+
this.peekState.searchQuery,
|
|
1637
|
+
this.peekState.smartCase,
|
|
1638
|
+
);
|
|
1639
|
+
this.peekState.currentMatchIndex = Math.min(
|
|
1640
|
+
this.peekState.currentMatchIndex,
|
|
1641
|
+
this.peekState.searchMatches.length - 1,
|
|
1642
|
+
);
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
|
|
1646
|
+
private scrollPeek(delta: number): void {
|
|
1647
|
+
const maxScroll = Math.max(0, this.peekRenderedLines.length - this.peekState.viewportHeight);
|
|
1648
|
+
this.peekState.scrollOffset = Math.max(
|
|
1649
|
+
0,
|
|
1650
|
+
Math.min(maxScroll, this.peekState.scrollOffset + delta),
|
|
1651
|
+
);
|
|
1652
|
+
|
|
1653
|
+
// Keep the selected message aligned with the first message at or before
|
|
1654
|
+
// the viewport, while preserving line-level scrolling within a block.
|
|
1655
|
+
let selected = this.peekState.selectedIndex;
|
|
1656
|
+
for (let i = 0; i < this.peekBlockStarts.length; i++) {
|
|
1657
|
+
if (this.peekBlockStarts[i] > this.peekState.scrollOffset) break;
|
|
1658
|
+
if (this.peekBlocks[i]?.type !== "tool-call" && this.peekBlocks[i]?.type !== "tool-result") {
|
|
1659
|
+
selected = i;
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
this.peekState.selectedIndex = selected;
|
|
1663
|
+
this.tui.requestRender();
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
private scrollPeekPage(direction: 1 | -1): void {
|
|
1667
|
+
const page = Math.max(1, this.peekState.viewportHeight - 2);
|
|
1668
|
+
this.scrollPeek(direction * page);
|
|
1669
|
+
}
|
|
1670
|
+
|
|
1671
|
+
private jumpPeekMessage(direction: 1 | -1): void {
|
|
1672
|
+
const isMessage = (block: PeekBlock): boolean =>
|
|
1673
|
+
block.type !== "tool-call" && block.type !== "tool-result";
|
|
1674
|
+
let target = this.peekState.selectedIndex + direction;
|
|
1675
|
+
while (target >= 0 && target < this.peekBlocks.length) {
|
|
1676
|
+
if (isMessage(this.peekBlocks[target])) {
|
|
1677
|
+
this.peekState.selectedIndex = target;
|
|
1678
|
+
this.ensurePeekVisible();
|
|
1679
|
+
this.tui.requestRender();
|
|
1680
|
+
return;
|
|
1681
|
+
}
|
|
1682
|
+
target += direction;
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
private ensurePeekVisible(): void {
|
|
1687
|
+
const start = this.peekBlockStarts[this.peekState.selectedIndex];
|
|
1688
|
+
if (start === undefined) return;
|
|
1689
|
+
|
|
1690
|
+
const vh = this.peekState.viewportHeight;
|
|
1691
|
+
if (start < this.peekState.scrollOffset) {
|
|
1692
|
+
this.peekState.scrollOffset = start;
|
|
1693
|
+
} else if (start >= this.peekState.scrollOffset + vh) {
|
|
1694
|
+
this.peekState.scrollOffset = start - vh + 1;
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
|
|
1698
|
+
private peekGoToFirst(): void {
|
|
1699
|
+
this.peekState.selectedIndex = 0;
|
|
1700
|
+
this.peekState.scrollOffset = 0;
|
|
1701
|
+
this.tui.requestRender();
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
private peekGoToLast(): void {
|
|
1705
|
+
let last = Math.max(0, this.peekBlocks.length - 1);
|
|
1706
|
+
while (last > 0 && (this.peekBlocks[last]?.type === "tool-call" || this.peekBlocks[last]?.type === "tool-result")) {
|
|
1707
|
+
last--;
|
|
1708
|
+
}
|
|
1709
|
+
this.peekState.selectedIndex = last;
|
|
1710
|
+
this.peekState.scrollOffset = Math.max(
|
|
1711
|
+
0,
|
|
1712
|
+
this.peekRenderedLines.length - this.peekState.viewportHeight,
|
|
1713
|
+
);
|
|
1714
|
+
this.tui.requestRender();
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
private peekToggleTools(): void {
|
|
1718
|
+
this.peekState.showTools = !this.peekState.showTools;
|
|
1719
|
+
this.rebuildPeekBlocks();
|
|
1720
|
+
this.peekState.selectedIndex = Math.min(this.peekState.selectedIndex, Math.max(0, this.peekBlocks.length - 1));
|
|
1721
|
+
this.peekState.scrollOffset = 0;
|
|
1722
|
+
this.peekState.searchMatches = [];
|
|
1723
|
+
this.peekState.currentMatchIndex = -1;
|
|
1724
|
+
this.tui.requestRender();
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
private async peekCopyCurrent(): Promise<void> {
|
|
1728
|
+
const block = this.peekBlocks[this.peekState.selectedIndex];
|
|
1729
|
+
if (!block) return;
|
|
1730
|
+
|
|
1731
|
+
try {
|
|
1732
|
+
await copyToClipboard(getBlockText(block));
|
|
1733
|
+
this.statusTextValue = "Copied current BTW message.";
|
|
1734
|
+
} catch {
|
|
1735
|
+
this.statusTextValue = "Copy failed.";
|
|
1736
|
+
}
|
|
1737
|
+
this.tui.requestRender();
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
private peekOpenInNvim(): void {
|
|
1741
|
+
const block = this.peekBlocks[this.peekState.selectedIndex];
|
|
1742
|
+
if (!block) return;
|
|
1743
|
+
|
|
1744
|
+
const editorCommand = process.env.VISUAL || process.env.EDITOR || "nvim";
|
|
1745
|
+
const [editor, ...editorArgs] = editorCommand.split(/\s+/);
|
|
1746
|
+
const dir = mkdtempSync(path.join(tmpdir(), "pi-btw-peek-"));
|
|
1747
|
+
const filePath = path.join(dir, "message.md");
|
|
1748
|
+
|
|
1749
|
+
try {
|
|
1750
|
+
writeFileSync(filePath, `${getBlockText(block)}\n`, "utf8");
|
|
1751
|
+
this.tui.stop();
|
|
1752
|
+
process.stdout.write(`Launching external editor: ${editorCommand}\n`);
|
|
1753
|
+
spawnSync(editor, ["-R", ...editorArgs, filePath], {
|
|
1754
|
+
cwd: process.cwd(),
|
|
1755
|
+
env: process.env,
|
|
1756
|
+
stdio: "inherit",
|
|
1757
|
+
});
|
|
1758
|
+
} finally {
|
|
1759
|
+
rmSync(dir, { recursive: true, force: true });
|
|
1760
|
+
this.tui.start();
|
|
1761
|
+
this.tui.requestRender(true);
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
private startPeekSearch(): void {
|
|
1766
|
+
this.peekState.searchActive = true;
|
|
1767
|
+
this.peekSearchBuffer = "";
|
|
1768
|
+
this.peekState.searchMatches = [];
|
|
1769
|
+
this.peekState.currentMatchIndex = -1;
|
|
1770
|
+
this.tui.requestRender();
|
|
1771
|
+
}
|
|
1772
|
+
|
|
1773
|
+
private finishPeekSearch(): void {
|
|
1774
|
+
if (!this.peekSearchBuffer) {
|
|
1775
|
+
this.peekState.searchActive = false;
|
|
1776
|
+
this.tui.requestRender();
|
|
1777
|
+
return;
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
this.peekState.searchQuery = this.peekSearchBuffer;
|
|
1781
|
+
this.peekState.searchActive = false;
|
|
1782
|
+
this.peekState.searchMatches = findSearchMatches(
|
|
1783
|
+
this.peekBlocks,
|
|
1784
|
+
this.peekState.searchQuery,
|
|
1785
|
+
this.peekState.smartCase,
|
|
1786
|
+
);
|
|
1787
|
+
this.peekState.currentMatchIndex = -1;
|
|
1788
|
+
|
|
1789
|
+
// Jump to first match
|
|
1790
|
+
if (this.peekState.searchMatches.length > 0) {
|
|
1791
|
+
this.peekState.selectedIndex = this.peekState.searchMatches[0];
|
|
1792
|
+
this.peekState.currentMatchIndex = 0;
|
|
1793
|
+
this.ensurePeekVisible();
|
|
1794
|
+
}
|
|
1795
|
+
this.tui.requestRender();
|
|
1796
|
+
}
|
|
1797
|
+
|
|
1798
|
+
private cancelPeekSearch(): void {
|
|
1799
|
+
this.peekState.searchActive = false;
|
|
1800
|
+
this.peekSearchBuffer = "";
|
|
1801
|
+
this.tui.requestRender();
|
|
1802
|
+
}
|
|
1803
|
+
|
|
1804
|
+
private peekNextMatch(): void {
|
|
1805
|
+
if (this.peekState.searchMatches.length === 0) return;
|
|
1806
|
+
const result = findNextSearchMatch(
|
|
1807
|
+
this.peekBlocks,
|
|
1808
|
+
this.peekState.searchMatches,
|
|
1809
|
+
this.peekState.currentMatchIndex,
|
|
1810
|
+
true,
|
|
1811
|
+
);
|
|
1812
|
+
if (result) {
|
|
1813
|
+
this.peekState.selectedIndex = result.blockIndex;
|
|
1814
|
+
this.peekState.currentMatchIndex = result.matchIndex;
|
|
1815
|
+
this.ensurePeekVisible();
|
|
1816
|
+
this.tui.requestRender();
|
|
1817
|
+
}
|
|
1818
|
+
}
|
|
1819
|
+
|
|
1820
|
+
private peekPrevMatch(): void {
|
|
1821
|
+
if (this.peekState.searchMatches.length === 0) return;
|
|
1822
|
+
const result = findNextSearchMatch(
|
|
1823
|
+
this.peekBlocks,
|
|
1824
|
+
this.peekState.searchMatches,
|
|
1825
|
+
this.peekState.currentMatchIndex,
|
|
1826
|
+
false,
|
|
1827
|
+
);
|
|
1828
|
+
if (result) {
|
|
1829
|
+
this.peekState.selectedIndex = result.blockIndex;
|
|
1830
|
+
this.peekState.currentMatchIndex = result.matchIndex;
|
|
1831
|
+
this.ensurePeekVisible();
|
|
1832
|
+
this.tui.requestRender();
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
private handlePeekInput(data: string): void {
|
|
1837
|
+
// If in search mode, capture input for search query
|
|
1838
|
+
if (this.peekState.searchActive) {
|
|
1839
|
+
if (data === "\r" || data === "\n") {
|
|
1840
|
+
this.finishPeekSearch();
|
|
1841
|
+
return;
|
|
1842
|
+
}
|
|
1843
|
+
if (data === "\x1b" || data === "\x03") {
|
|
1844
|
+
this.cancelPeekSearch();
|
|
1845
|
+
return;
|
|
1846
|
+
}
|
|
1847
|
+
if (data === "\x7f" || data === "\b") {
|
|
1848
|
+
this.peekSearchBuffer = this.peekSearchBuffer.slice(0, -1);
|
|
1849
|
+
this.tui.requestRender();
|
|
1850
|
+
return;
|
|
1851
|
+
}
|
|
1852
|
+
// Printable characters
|
|
1853
|
+
if (data.length === 1 && data.charCodeAt(0) >= 32 && data.charCodeAt(0) <= 126) {
|
|
1854
|
+
this.peekSearchBuffer += data;
|
|
1855
|
+
this.tui.requestRender();
|
|
1856
|
+
return;
|
|
1857
|
+
}
|
|
1858
|
+
return;
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
// Navigation
|
|
1862
|
+
if (data === "j") {
|
|
1863
|
+
this.scrollPeek(1);
|
|
1864
|
+
return;
|
|
1865
|
+
}
|
|
1866
|
+
if (data === "k") {
|
|
1867
|
+
this.scrollPeek(-1);
|
|
1868
|
+
return;
|
|
1869
|
+
}
|
|
1870
|
+
// Match pi-peek: J/K jump to the next/previous message block.
|
|
1871
|
+
if (data === "J") {
|
|
1872
|
+
this.jumpPeekMessage(1);
|
|
1873
|
+
return;
|
|
1874
|
+
}
|
|
1875
|
+
if (data === "K") {
|
|
1876
|
+
this.jumpPeekMessage(-1);
|
|
1877
|
+
return;
|
|
1878
|
+
}
|
|
1879
|
+
if (data === "g") {
|
|
1880
|
+
this.peekGoToFirst();
|
|
1881
|
+
return;
|
|
1882
|
+
}
|
|
1883
|
+
if (data === "G") {
|
|
1884
|
+
this.peekGoToLast();
|
|
1885
|
+
return;
|
|
1886
|
+
}
|
|
1887
|
+
|
|
1888
|
+
// Search
|
|
1889
|
+
if (data === "/") {
|
|
1890
|
+
this.startPeekSearch();
|
|
1891
|
+
return;
|
|
1892
|
+
}
|
|
1893
|
+
if (data === "n") {
|
|
1894
|
+
this.peekNextMatch();
|
|
1895
|
+
return;
|
|
1896
|
+
}
|
|
1897
|
+
if (data === "N") {
|
|
1898
|
+
this.peekPrevMatch();
|
|
1899
|
+
return;
|
|
1900
|
+
}
|
|
1901
|
+
|
|
1902
|
+
// Toggle tools
|
|
1903
|
+
if (data === "t") {
|
|
1904
|
+
this.peekToggleTools();
|
|
1905
|
+
return;
|
|
1906
|
+
}
|
|
1907
|
+
|
|
1908
|
+
// Copy current message
|
|
1909
|
+
if (data === "M") {
|
|
1910
|
+
void this.peekCopyCurrent();
|
|
1911
|
+
return;
|
|
1912
|
+
}
|
|
1913
|
+
|
|
1914
|
+
// Open in nvim
|
|
1915
|
+
if (data === "o") {
|
|
1916
|
+
this.peekOpenInNvim();
|
|
1917
|
+
return;
|
|
1918
|
+
}
|
|
1919
|
+
|
|
1920
|
+
// Page up/down
|
|
1921
|
+
if (data === "\x1b[5~" || data === "\x1b[5;*~") {
|
|
1922
|
+
// PageUp
|
|
1923
|
+
this.scrollPeekPage(-1);
|
|
1924
|
+
return;
|
|
1925
|
+
}
|
|
1926
|
+
if (data === "\x1b[6~" || data === "\x1b[6;*~") {
|
|
1927
|
+
// PageDown
|
|
1928
|
+
this.scrollPeekPage(1);
|
|
1929
|
+
return;
|
|
1930
|
+
}
|
|
1931
|
+
|
|
1932
|
+
// q/Ctrl+C close BTW; Enter/Escape return to the BTW composer.
|
|
1933
|
+
if (data === "q" || data === "\x03") {
|
|
1934
|
+
this.onDismissCallback();
|
|
1935
|
+
return;
|
|
1936
|
+
}
|
|
1937
|
+
if (data === "\r" || data === "\n" || data === "\x1b") {
|
|
1938
|
+
this.exitPeekMode();
|
|
1939
|
+
return;
|
|
1940
|
+
}
|
|
1941
|
+
|
|
1942
|
+
// Arrow keys
|
|
1943
|
+
if (data === "\x1b[A" || data === "\x1bOA") {
|
|
1944
|
+
this.scrollPeek(-1);
|
|
1945
|
+
return;
|
|
1946
|
+
}
|
|
1947
|
+
if (data === "\x1b[B" || data === "\x1bOB") {
|
|
1948
|
+
this.scrollPeek(1);
|
|
1949
|
+
return;
|
|
1950
|
+
}
|
|
1951
|
+
}
|
|
1413
1952
|
}
|
|
1414
1953
|
|
|
1415
1954
|
export default function (pi: ExtensionAPI) {
|
|
@@ -1792,6 +2331,9 @@ export default function (pi: ExtensionAPI) {
|
|
|
1792
2331
|
runtime.setDraft = (value) => {
|
|
1793
2332
|
overlay.setDraft(value);
|
|
1794
2333
|
};
|
|
2334
|
+
runtime.peek = () => {
|
|
2335
|
+
overlay.enterPeekMode();
|
|
2336
|
+
};
|
|
1795
2337
|
runtime.refresh = () => {
|
|
1796
2338
|
overlay.focused = runtime.handle?.isFocused() ?? false;
|
|
1797
2339
|
overlay.refresh();
|
|
@@ -1996,12 +2538,24 @@ export default function (pi: ExtensionAPI) {
|
|
|
1996
2538
|
return true;
|
|
1997
2539
|
}
|
|
1998
2540
|
|
|
2541
|
+
if (name === "btw:peek") {
|
|
2542
|
+
await ensureOverlay(ctx);
|
|
2543
|
+
overlayRuntime?.peek?.();
|
|
2544
|
+
if (overlayRuntime?.handle) {
|
|
2545
|
+
overlayRuntime.handle.setHidden(false);
|
|
2546
|
+
overlayRuntime.handle.focus();
|
|
2547
|
+
}
|
|
2548
|
+
setOverlayStatus("Peek mode — browse the BTW thread. Esc to return.", ctx);
|
|
2549
|
+
overlayRuntime?.refresh?.();
|
|
2550
|
+
return true;
|
|
2551
|
+
}
|
|
2552
|
+
|
|
1999
2553
|
return false;
|
|
2000
2554
|
}
|
|
2001
2555
|
|
|
2002
2556
|
function parseOverlayBtwCommand(value: string): { name: string; args: string } | null {
|
|
2003
2557
|
const trimmed = value.trim();
|
|
2004
|
-
const match = trimmed.match(/^\/(btw:(?:new|tangent|clear|inject|summarize|model|thinking))(?:\s+(.*))?$/);
|
|
2558
|
+
const match = trimmed.match(/^\/(btw:(?:new|tangent|clear|inject|summarize|model|thinking|peek))(?:\s+(.*))?$/);
|
|
2005
2559
|
if (!match) {
|
|
2006
2560
|
return null;
|
|
2007
2561
|
}
|
|
@@ -2400,6 +2954,13 @@ export default function (pi: ExtensionAPI) {
|
|
|
2400
2954
|
},
|
|
2401
2955
|
});
|
|
2402
2956
|
|
|
2957
|
+
pi.registerCommand("btw:peek", {
|
|
2958
|
+
description: "Enter peek mode to browse the BTW thread transcript.",
|
|
2959
|
+
handler: async (args, ctx) => {
|
|
2960
|
+
await dispatchBtwCommand("btw:peek", args, ctx);
|
|
2961
|
+
},
|
|
2962
|
+
});
|
|
2963
|
+
|
|
2403
2964
|
pi.registerCommand("btw:thinking", {
|
|
2404
2965
|
description: "Show, set, or clear the BTW-only thinking override.",
|
|
2405
2966
|
handler: async (args, ctx) => {
|