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 +8 -0
- package/README.md +17 -1
- package/extensions/btw/peek.test.ts +33 -0
- package/extensions/btw/peek.ts +157 -7
- package/extensions/btw/transcript-store.ts +423 -0
- package/extensions/btw-transcript.test.ts +244 -2
- package/extensions/btw.ts +957 -117
- package/package.json +2 -1
package/extensions/btw.ts
CHANGED
|
@@ -40,23 +40,42 @@ import {
|
|
|
40
40
|
findNextSearchMatch,
|
|
41
41
|
findSearchMatches,
|
|
42
42
|
getBlockText,
|
|
43
|
-
|
|
43
|
+
getSemanticBlockLabel,
|
|
44
|
+
getToolPreviewText,
|
|
45
|
+
getToolResultSummary,
|
|
46
|
+
hasToolPreview,
|
|
47
|
+
parsePeekMouseInput,
|
|
44
48
|
type PeekBlock,
|
|
49
|
+
type PeekMouseEvent,
|
|
45
50
|
type PeekEntry,
|
|
46
51
|
type PeekModeState,
|
|
47
52
|
} from "./btw/peek.ts";
|
|
53
|
+
import {
|
|
54
|
+
BTW_TRANSCRIPT_SCHEMA_VERSION,
|
|
55
|
+
BtwTranscriptStore,
|
|
56
|
+
isTranscriptLocator,
|
|
57
|
+
makeTranscriptLocator,
|
|
58
|
+
type DurableTranscriptSnapshot,
|
|
59
|
+
type ToolArtifact,
|
|
60
|
+
} from "./btw/transcript-store.ts";
|
|
48
61
|
|
|
49
62
|
const BTW_MESSAGE_TYPE = "btw-note";
|
|
50
63
|
const BTW_ENTRY_TYPE = "btw-thread-entry";
|
|
51
64
|
const BTW_RESET_TYPE = "btw-thread-reset";
|
|
65
|
+
const BTW_TRANSCRIPT_TYPE = "btw-transcript";
|
|
52
66
|
const BTW_MODEL_OVERRIDE_TYPE = "btw-model-override";
|
|
53
67
|
const BTW_THINKING_OVERRIDE_TYPE = "btw-thinking-override";
|
|
54
68
|
const BTW_FOCUS_SHORTCUTS = [Key.alt("/"), Key.ctrlAlt("w")] as const;
|
|
69
|
+
const BTW_TOOL_OUTPUT_SHORTCUT = Key.ctrl("o");
|
|
55
70
|
|
|
56
71
|
function matchesBtwFocusShortcut(data: string): boolean {
|
|
57
72
|
return BTW_FOCUS_SHORTCUTS.some((shortcut) => matchesKey(data, shortcut));
|
|
58
73
|
}
|
|
59
74
|
|
|
75
|
+
function matchesPageKey(data: string, page: 5 | 6): boolean {
|
|
76
|
+
return new RegExp(`^\\x1b\\[${page}(?:;\\d+)?~$`).test(data);
|
|
77
|
+
}
|
|
78
|
+
|
|
60
79
|
function findFdPath(): string | null {
|
|
61
80
|
const lookup = process.platform === "win32" ? "where" : "which";
|
|
62
81
|
for (const candidate of process.platform === "win32" ? ["fd.exe", "fd"] : ["fd", "fdfind"]) {
|
|
@@ -93,6 +112,8 @@ type SessionModel = NonNullable<ExtensionCommandContext["model"]>;
|
|
|
93
112
|
type BtwModelRef = Pick<SessionModel, "provider" | "id" | "api">;
|
|
94
113
|
|
|
95
114
|
type BtwDetails = {
|
|
115
|
+
/** Versioned so old btw-thread-entry details remain readable on restore. */
|
|
116
|
+
schemaVersion: typeof BTW_TRANSCRIPT_SCHEMA_VERSION;
|
|
96
117
|
question: string;
|
|
97
118
|
thinking: string;
|
|
98
119
|
answer: string;
|
|
@@ -116,6 +137,20 @@ type BtwResetDetails = {
|
|
|
116
137
|
mode?: BtwThreadMode;
|
|
117
138
|
};
|
|
118
139
|
|
|
140
|
+
const VALID_THINKING_LEVELS: readonly SessionThinkingLevel[] = ["off", "minimal", "low", "medium", "high", "xhigh"];
|
|
141
|
+
|
|
142
|
+
function isBtwThreadMode(value: unknown): value is BtwThreadMode {
|
|
143
|
+
return value === "contextual" || value === "tangent";
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function isSessionThinkingLevel(value: unknown): value is SessionThinkingLevel {
|
|
147
|
+
return typeof value === "string" && VALID_THINKING_LEVELS.includes(value as SessionThinkingLevel);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
151
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
152
|
+
}
|
|
153
|
+
|
|
119
154
|
type BtwModelOverrideDetails =
|
|
120
155
|
| ({ timestamp: number; action: "set" } & Pick<SessionModel, "provider" | "id" | "api">)
|
|
121
156
|
| { timestamp: number; action: "clear" };
|
|
@@ -152,7 +187,10 @@ type BtwTranscriptEntry =
|
|
|
152
187
|
type: "tool-result";
|
|
153
188
|
toolCallId: string;
|
|
154
189
|
toolName: string;
|
|
190
|
+
/** Compact text used by the normal transcript row. */
|
|
155
191
|
content: string;
|
|
192
|
+
/** Full result retained for the read-only Peek preview. */
|
|
193
|
+
fullContent?: string;
|
|
156
194
|
truncated: boolean;
|
|
157
195
|
isError: boolean;
|
|
158
196
|
streaming: boolean;
|
|
@@ -397,19 +435,15 @@ function formatToolPreview(value: unknown): string {
|
|
|
397
435
|
return value;
|
|
398
436
|
}
|
|
399
437
|
|
|
400
|
-
if (value && typeof value === "object") {
|
|
401
|
-
const path = (value as { path?: unknown }).path;
|
|
402
|
-
if (typeof path === "string") {
|
|
403
|
-
return path;
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
|
|
407
438
|
try {
|
|
408
439
|
const preview = JSON.stringify(value);
|
|
409
440
|
if (!preview || preview === "{}") {
|
|
410
441
|
return "";
|
|
411
442
|
}
|
|
412
|
-
|
|
443
|
+
// Keep the immutable input for `w`; normal rendering clips it to the row
|
|
444
|
+
// width, so storing a pre-truncated preview would make the read-only view
|
|
445
|
+
// incomplete.
|
|
446
|
+
return preview;
|
|
413
447
|
} catch {
|
|
414
448
|
return "";
|
|
415
449
|
}
|
|
@@ -571,7 +605,7 @@ function upsertTranscriptTextEntry(
|
|
|
571
605
|
appendTranscriptEntry(state, { type, turnId, text, streaming } as Omit<Extract<BtwTranscriptEntry, { type: "thinking" | "assistant-text" }>, "id">);
|
|
572
606
|
}
|
|
573
607
|
|
|
574
|
-
function summarizeToolResult(value: unknown, maxLength = 400): { content: string; truncated: boolean } {
|
|
608
|
+
function summarizeToolResult(value: unknown, maxLength = 400): { content: string; fullContent: string; truncated: boolean } {
|
|
575
609
|
let content = "";
|
|
576
610
|
|
|
577
611
|
if (value && typeof value === "object") {
|
|
@@ -617,6 +651,7 @@ function summarizeToolResult(value: unknown, maxLength = 400): { content: string
|
|
|
617
651
|
const truncated = content.length > maxLength;
|
|
618
652
|
return {
|
|
619
653
|
content: truncated ? `${content.slice(0, maxLength - 3)}...` : content,
|
|
654
|
+
fullContent: content,
|
|
620
655
|
truncated,
|
|
621
656
|
};
|
|
622
657
|
}
|
|
@@ -651,6 +686,7 @@ function upsertToolResultEntry(
|
|
|
651
686
|
toolCallId: string,
|
|
652
687
|
toolName: string,
|
|
653
688
|
content: string,
|
|
689
|
+
fullContent: string,
|
|
654
690
|
truncated: boolean,
|
|
655
691
|
isError: boolean,
|
|
656
692
|
streaming: boolean,
|
|
@@ -663,6 +699,7 @@ function upsertToolResultEntry(
|
|
|
663
699
|
|
|
664
700
|
if (existing && existing.type === "tool-result") {
|
|
665
701
|
existing.content = content;
|
|
702
|
+
existing.fullContent = fullContent;
|
|
666
703
|
existing.truncated = truncated;
|
|
667
704
|
existing.isError = isError;
|
|
668
705
|
existing.streaming = streaming;
|
|
@@ -675,6 +712,7 @@ function upsertToolResultEntry(
|
|
|
675
712
|
toolCallId,
|
|
676
713
|
toolName,
|
|
677
714
|
content,
|
|
715
|
+
fullContent,
|
|
678
716
|
truncated,
|
|
679
717
|
isError,
|
|
680
718
|
streaming,
|
|
@@ -756,6 +794,7 @@ function applyTranscriptEvent(state: BtwTranscriptState, event: AgentSessionEven
|
|
|
756
794
|
event.toolCallId,
|
|
757
795
|
event.toolName,
|
|
758
796
|
result.content,
|
|
797
|
+
result.fullContent,
|
|
759
798
|
result.truncated,
|
|
760
799
|
false,
|
|
761
800
|
true,
|
|
@@ -771,6 +810,7 @@ function applyTranscriptEvent(state: BtwTranscriptState, event: AgentSessionEven
|
|
|
771
810
|
event.toolCallId,
|
|
772
811
|
event.toolName,
|
|
773
812
|
result.content,
|
|
813
|
+
result.fullContent,
|
|
774
814
|
result.truncated,
|
|
775
815
|
event.isError,
|
|
776
816
|
false,
|
|
@@ -796,6 +836,40 @@ function appendPersistedTranscriptTurn(state: BtwTranscriptState, details: BtwDe
|
|
|
796
836
|
finishTranscriptTurn(state, turnId);
|
|
797
837
|
}
|
|
798
838
|
|
|
839
|
+
/**
|
|
840
|
+
* A process can terminate while a BTW sub-session is streaming. There is no
|
|
841
|
+
* live producer after reload, so those entries must become a closed historical
|
|
842
|
+
* turn before the next composer submission is accepted.
|
|
843
|
+
*/
|
|
844
|
+
function closeRestoredStreamingTurns(state: BtwTranscriptState): boolean {
|
|
845
|
+
const staleTurns = new Set<number>();
|
|
846
|
+
for (const entry of state.entries) {
|
|
847
|
+
if ((entry.type === "thinking" || entry.type === "assistant-text" || entry.type === "tool-result") && entry.streaming) {
|
|
848
|
+
staleTurns.add(entry.turnId);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
if (staleTurns.size === 0) return false;
|
|
852
|
+
|
|
853
|
+
for (const turnId of staleTurns) {
|
|
854
|
+
for (const entry of state.entries) {
|
|
855
|
+
if (entry.turnId === turnId &&
|
|
856
|
+
(entry.type === "thinking" || entry.type === "assistant-text" || entry.type === "tool-result")) {
|
|
857
|
+
entry.streaming = false;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
const hasEndBoundary = state.entries.some(
|
|
861
|
+
(entry) => entry.turnId === turnId && entry.type === "turn-boundary" && entry.phase === "end",
|
|
862
|
+
);
|
|
863
|
+
if (!hasEndBoundary) {
|
|
864
|
+
appendTranscriptEntry(state, { type: "turn-boundary", turnId, phase: "end" } as Omit<Extract<BtwTranscriptEntry, { type: "turn-boundary" }>, "id">);
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
state.currentTurnId = null;
|
|
869
|
+
state.lastTurnId = Math.max(state.lastTurnId ?? 0, ...staleTurns);
|
|
870
|
+
return true;
|
|
871
|
+
}
|
|
872
|
+
|
|
799
873
|
function setTranscriptFailure(state: BtwTranscriptState, message: string): void {
|
|
800
874
|
const turnId = state.currentTurnId ?? state.lastTurnId ?? ensureTranscriptTurn(state);
|
|
801
875
|
upsertTranscriptTextEntry(state, turnId, "assistant-text", `❌ ${message}`, false);
|
|
@@ -1064,13 +1138,20 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1064
1138
|
private readonly onDismissCallback: () => void;
|
|
1065
1139
|
private readonly onUnfocusCallback: () => void;
|
|
1066
1140
|
private readonly onExternalEditorCallback?: () => void;
|
|
1141
|
+
private readonly readToolArtifact?: (toolCallId: string) => ToolArtifact | null;
|
|
1067
1142
|
private externalEditorActive = false;
|
|
1068
1143
|
private readonly tui: TUI;
|
|
1069
1144
|
private readonly theme: ExtensionContext["ui"]["theme"];
|
|
1070
1145
|
private transcriptLines: string[] = [];
|
|
1146
|
+
/** Rendered transcript line -> source entry id; separators/padding are null. */
|
|
1147
|
+
private transcriptLineEntryIds: Array<number | null> = [];
|
|
1071
1148
|
private transcriptScrollOffset = 0;
|
|
1072
1149
|
private transcriptViewportHeight = 8;
|
|
1073
1150
|
private followTranscript = true;
|
|
1151
|
+
private liveHoveredEntryId: number | null = null;
|
|
1152
|
+
private liveFocusedEntryId: number | null = null;
|
|
1153
|
+
private liveFocusedToolCallId: string | null = null;
|
|
1154
|
+
private liveExpandedToolCallId: string | null = null;
|
|
1074
1155
|
private _focused = false;
|
|
1075
1156
|
private modeTextValue = "";
|
|
1076
1157
|
private summaryTextValue = "";
|
|
@@ -1082,8 +1163,24 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1082
1163
|
private peekBlocks: PeekBlock[] = [];
|
|
1083
1164
|
private peekRenderedLines: string[] = [];
|
|
1084
1165
|
private peekBlockStarts: number[] = [];
|
|
1166
|
+
private peekBlockEnds: number[] = [];
|
|
1085
1167
|
private peekHeaderLines: string[] = [];
|
|
1168
|
+
private peekStickyHeaderIndex: number | null = null;
|
|
1086
1169
|
private peekSearchBuffer = "";
|
|
1170
|
+
private peekHoveredIndex: number | null = null;
|
|
1171
|
+
private peekToolPreviewIndex: number | null = null;
|
|
1172
|
+
private peekToolPreviewBlockId: number | null = null;
|
|
1173
|
+
private peekToolPreviewLines: string[] = [];
|
|
1174
|
+
private peekToolPreviewScrollOffset = 0;
|
|
1175
|
+
private expandedToolId: number | null = null;
|
|
1176
|
+
/** The same bounded preview is used by Peek and by the live BTW panel. */
|
|
1177
|
+
private toolPreviewParent: "peek" | "live" | null = null;
|
|
1178
|
+
private liveToolPreviewBlock: PeekBlock | null = null;
|
|
1179
|
+
private lastRenderWidth = 80;
|
|
1180
|
+
// SGR mouse coordinates are one-based. Peek content starts at row 4;
|
|
1181
|
+
// live transcript content starts at row 5 after its title/rules.
|
|
1182
|
+
private peekContentTop = 4;
|
|
1183
|
+
private transcriptContentTop = 5;
|
|
1087
1184
|
|
|
1088
1185
|
get focused(): boolean {
|
|
1089
1186
|
return this._focused;
|
|
@@ -1106,6 +1203,7 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1106
1203
|
onUnfocus: () => void,
|
|
1107
1204
|
onExternalEditor?: () => void,
|
|
1108
1205
|
cwd?: string,
|
|
1206
|
+
readToolArtifact?: (toolCallId: string) => ToolArtifact | null,
|
|
1109
1207
|
) {
|
|
1110
1208
|
super();
|
|
1111
1209
|
this.tui = tui;
|
|
@@ -1117,6 +1215,7 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1117
1215
|
this.onDismissCallback = onDismiss;
|
|
1118
1216
|
this.onUnfocusCallback = onUnfocus;
|
|
1119
1217
|
this.onExternalEditorCallback = onExternalEditor;
|
|
1218
|
+
this.readToolArtifact = readToolArtifact;
|
|
1120
1219
|
|
|
1121
1220
|
this.modeText = new Text("", 1, 0);
|
|
1122
1221
|
this.summaryText = new Text("", 1, 0);
|
|
@@ -1155,9 +1254,6 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1155
1254
|
this.hintsText = new Text("", 1, 0);
|
|
1156
1255
|
this.mdTheme = getMarkdownTheme();
|
|
1157
1256
|
|
|
1158
|
-
// Enable SGR mouse reporting so wheel/touchpad events reach handleInput().
|
|
1159
|
-
this.tui.terminal?.write?.("\x1b[?1000h\x1b[?1006h");
|
|
1160
|
-
|
|
1161
1257
|
const originalHandleInput = this.input.handleInput.bind(this.input);
|
|
1162
1258
|
this.input.handleInput = (data: string) => {
|
|
1163
1259
|
if (keybindings.matches(data, "app.editor.external")) {
|
|
@@ -1235,6 +1331,12 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1235
1331
|
return this.theme.fg("border", `${left}${"─".repeat(innerWidth)}${right}`);
|
|
1236
1332
|
}
|
|
1237
1333
|
|
|
1334
|
+
private titleLine(title: string, innerWidth: number): string {
|
|
1335
|
+
const escapeHint = this.theme.fg("dim", "[Esc]");
|
|
1336
|
+
const gap = Math.max(1, innerWidth - visibleWidth(title) - visibleWidth(escapeHint));
|
|
1337
|
+
return this.frameLine(`${title}${" ".repeat(gap)}${escapeHint}`, innerWidth);
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1238
1340
|
private wrapTranscript(innerWidth: number): string[] {
|
|
1239
1341
|
const wrapped: string[] = [];
|
|
1240
1342
|
for (const line of this.transcriptLines) {
|
|
@@ -1250,17 +1352,26 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1250
1352
|
private buildTranscriptLines(innerWidth: number): string[] {
|
|
1251
1353
|
const entries = this.readTranscriptEntries();
|
|
1252
1354
|
if (entries.length === 0) {
|
|
1355
|
+
this.transcriptLineEntryIds = [null];
|
|
1253
1356
|
return [this.theme.fg("dim", "No BTW thread yet. Ask a side question to start one.")];
|
|
1254
1357
|
}
|
|
1255
1358
|
|
|
1256
1359
|
const allLines: string[] = [];
|
|
1257
|
-
const
|
|
1258
|
-
const
|
|
1259
|
-
|
|
1260
|
-
|
|
1360
|
+
const lineEntryIds: Array<number | null> = [];
|
|
1361
|
+
const markBlock = (entryId: number, start: number): void => {
|
|
1362
|
+
const plain = (line: string) => line.replace(/\x1b\[[0-9;?]*[ -\/]*[@-~]/g, "");
|
|
1363
|
+
for (let index = start; index < allLines.length; index++) {
|
|
1364
|
+
const value = plain(allLines[index]);
|
|
1365
|
+
lineEntryIds[index] = value.trim() && !/^─+$/.test(value.trim()) ? entryId : null;
|
|
1366
|
+
}
|
|
1367
|
+
};
|
|
1368
|
+
const focusRail = (entryId: number): string =>
|
|
1369
|
+
this.liveFocusedEntryId === entryId ? "▎ " : this.liveHoveredEntryId === entryId ? "│ " : "";
|
|
1370
|
+
const userBadge = buildTranscriptBadge(this.theme, "USER", "userMessageBg", "accent");
|
|
1371
|
+
const thinkingBadge = buildTranscriptBadge(this.theme, "THINKING", "toolPendingBg", "warning");
|
|
1372
|
+
const assistantBadge = buildTranscriptBadge(this.theme, "ASSISTANT", "customMessageBg", "success");
|
|
1261
1373
|
const separator = this.theme.fg("borderMuted", "─".repeat(40));
|
|
1262
1374
|
const blockIndent = " ";
|
|
1263
|
-
const resultIndent = blockIndent;
|
|
1264
1375
|
const mdContentWidth = Math.max(1, innerWidth - visibleWidth(blockIndent));
|
|
1265
1376
|
|
|
1266
1377
|
const pushBlank = (): void => {
|
|
@@ -1270,6 +1381,7 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1270
1381
|
};
|
|
1271
1382
|
|
|
1272
1383
|
for (const entry of entries) {
|
|
1384
|
+
const blockStart = allLines.length;
|
|
1273
1385
|
if (entry.type === "turn-boundary") {
|
|
1274
1386
|
if (entry.phase === "start" && allLines.length > 0) {
|
|
1275
1387
|
pushBlank();
|
|
@@ -1280,67 +1392,81 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1280
1392
|
|
|
1281
1393
|
if (entry.type === "user-message") {
|
|
1282
1394
|
pushBlank();
|
|
1283
|
-
allLines.push(userBadge);
|
|
1284
|
-
const
|
|
1285
|
-
|
|
1286
|
-
|
|
1395
|
+
allLines.push(`${focusRail(entry.id)}${userBadge}`);
|
|
1396
|
+
const boundedText = entry.text.length > 16_000 ? `${entry.text.slice(0, 15_970)}\n… [truncated]` : entry.text;
|
|
1397
|
+
const mdLines = new Markdown(boundedText || "∅", 0, 0, this.mdTheme).render(mdContentWidth);
|
|
1398
|
+
for (const line of mdLines) {
|
|
1399
|
+
allLines.push(`${blockIndent}${line.replace(/[ \t]+$/u, "")}`);
|
|
1287
1400
|
}
|
|
1401
|
+
markBlock(entry.id, blockStart);
|
|
1288
1402
|
continue;
|
|
1289
1403
|
}
|
|
1290
1404
|
|
|
1291
1405
|
if (entry.type === "thinking") {
|
|
1292
1406
|
const thinkingHeader = entry.streaming ? `${thinkingBadge} ${this.theme.fg("warning", "▍")}` : thinkingBadge;
|
|
1293
1407
|
pushBlank();
|
|
1294
|
-
allLines.push(thinkingHeader);
|
|
1408
|
+
allLines.push(`${focusRail(entry.id)}${thinkingHeader}`);
|
|
1295
1409
|
const styledLines = wrapTextWithAnsi(entry.text, mdContentWidth);
|
|
1296
1410
|
for (const line of styledLines) {
|
|
1297
1411
|
allLines.push(`${blockIndent}${this.theme.fg("warning", this.theme.italic(line))}`);
|
|
1298
1412
|
}
|
|
1413
|
+
markBlock(entry.id, blockStart);
|
|
1299
1414
|
continue;
|
|
1300
1415
|
}
|
|
1301
1416
|
|
|
1302
1417
|
if (entry.type === "tool-call") {
|
|
1303
|
-
const toolLabel = this.theme.fg("
|
|
1304
|
-
const argsLabel = entry.args ? this.theme.fg("dim", `
|
|
1418
|
+
const toolLabel = this.theme.fg("toolTitle", this.theme.bold(`· TOOL ${entry.toolName}`));
|
|
1419
|
+
const argsLabel = entry.args ? this.theme.fg("dim", ` → ${entry.args}`) : "";
|
|
1420
|
+
const isExpanded = this.liveExpandedToolCallId === entry.toolCallId;
|
|
1421
|
+
const moreLabel = this.theme.fg("dim", isExpanded ? " · [click less]" : " · [click more]");
|
|
1305
1422
|
pushBlank();
|
|
1306
|
-
allLines.push(`${
|
|
1423
|
+
allLines.push(`${focusRail(entry.id)}${toolLabel}${argsLabel}${moreLabel}`);
|
|
1424
|
+
if (isExpanded && entry.args) {
|
|
1425
|
+
for (const line of wrapTextWithAnsi(entry.args, mdContentWidth)) {
|
|
1426
|
+
allLines.push(`${blockIndent}${this.theme.fg("dim", line)}`);
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
markBlock(entry.id, blockStart);
|
|
1307
1430
|
continue;
|
|
1308
1431
|
}
|
|
1309
1432
|
|
|
1310
1433
|
if (entry.type === "tool-result") {
|
|
1311
|
-
const
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
const
|
|
1434
|
+
const resultIcon = entry.isError ? "✗" : "✓";
|
|
1435
|
+
const resultColor = entry.isError ? "error" : entry.streaming ? "warning" : "dim";
|
|
1436
|
+
const resultLabel = this.theme.fg(resultColor, ` ↳ ${resultIcon} ${entry.toolName}`);
|
|
1437
|
+
const summary = this.theme.fg(resultColor, ` · ${getToolResultSummary(entry.content)}`);
|
|
1438
|
+
const truncLabel = entry.truncated ? this.theme.fg("dim", " · truncated") : "";
|
|
1439
|
+
const isExpanded = this.liveExpandedToolCallId === entry.toolCallId;
|
|
1440
|
+
const moreLabel = this.theme.fg("dim", isExpanded ? " · [click less]" : " · [click more]");
|
|
1317
1441
|
pushBlank();
|
|
1318
|
-
allLines.push(`${
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1442
|
+
allLines.push(`${focusRail(entry.id)}${resultLabel}${summary}${truncLabel}${moreLabel}`);
|
|
1443
|
+
if (isExpanded) {
|
|
1444
|
+
for (const line of wrapTextWithAnsi(entry.fullContent ?? entry.content, mdContentWidth)) {
|
|
1445
|
+
allLines.push(`${blockIndent}${this.theme.fg(entry.isError ? "error" : "dim", line)}`);
|
|
1446
|
+
}
|
|
1322
1447
|
}
|
|
1448
|
+
markBlock(entry.id, blockStart);
|
|
1323
1449
|
continue;
|
|
1324
1450
|
}
|
|
1325
1451
|
|
|
1326
1452
|
if (entry.type === "assistant-text") {
|
|
1327
1453
|
pushBlank();
|
|
1328
1454
|
const header = entry.streaming ? `${assistantBadge} ${this.theme.fg("warning", "▍")}` : assistantBadge;
|
|
1329
|
-
allLines.push(header);
|
|
1455
|
+
allLines.push(`${focusRail(entry.id)}${header}`);
|
|
1330
1456
|
|
|
1331
1457
|
if (!entry.streaming) {
|
|
1332
1458
|
// Render markdown for completed text — use cached result when available
|
|
1333
1459
|
const cached = this.mdLineCache.get(entry.id);
|
|
1334
1460
|
if (cached && cached.renderedAtWidth === mdContentWidth) {
|
|
1335
1461
|
for (const line of cached.lines) {
|
|
1336
|
-
allLines.push(`${blockIndent}${line}`);
|
|
1462
|
+
allLines.push(`${blockIndent}${line.replace(/[ \t]+$/u, "")}`);
|
|
1337
1463
|
}
|
|
1338
1464
|
} else {
|
|
1339
1465
|
const md = new Markdown(entry.text, 0, 0, this.mdTheme);
|
|
1340
1466
|
const mdLines = md.render(mdContentWidth);
|
|
1341
1467
|
this.mdLineCache.set(entry.id, { lines: mdLines, renderedAtWidth: mdContentWidth });
|
|
1342
1468
|
for (const line of mdLines) {
|
|
1343
|
-
allLines.push(`${blockIndent}${line}`);
|
|
1469
|
+
allLines.push(`${blockIndent}${line.replace(/[ \t]+$/u, "")}`);
|
|
1344
1470
|
}
|
|
1345
1471
|
}
|
|
1346
1472
|
} else {
|
|
@@ -1350,44 +1476,229 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1350
1476
|
allLines.push(`${blockIndent}${line}`);
|
|
1351
1477
|
}
|
|
1352
1478
|
}
|
|
1479
|
+
markBlock(entry.id, blockStart);
|
|
1353
1480
|
continue;
|
|
1354
1481
|
}
|
|
1355
1482
|
}
|
|
1356
1483
|
|
|
1484
|
+
this.transcriptLineEntryIds = lineEntryIds;
|
|
1357
1485
|
return allLines;
|
|
1358
1486
|
}
|
|
1359
1487
|
|
|
1360
1488
|
private getDialogHeight(inputLines: number): number {
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
const
|
|
1364
|
-
|
|
1489
|
+
// Prefer the host TUI's measured viewport. Falling back to stdout keeps
|
|
1490
|
+
// this component usable in lightweight tests and non-interactive hosts.
|
|
1491
|
+
const terminalRows = Math.max(
|
|
1492
|
+
1,
|
|
1493
|
+
this.tui.terminal?.rows ?? process.stdout.rows ?? 30,
|
|
1494
|
+
);
|
|
1495
|
+
const preferred = Math.floor(terminalRows * 0.78);
|
|
1496
|
+
const maxHeight = Math.max(1, Math.min(40, terminalRows));
|
|
1497
|
+
const minHeight = Math.min(maxHeight, Math.max(8, 8 + inputLines + 3));
|
|
1498
|
+
return Math.min(maxHeight, Math.max(minHeight, preferred));
|
|
1365
1499
|
}
|
|
1366
1500
|
|
|
1367
1501
|
private scrollTranscript(delta: number): void {
|
|
1502
|
+
// Wheel input can arrive before the first overlay render. Build the same
|
|
1503
|
+
// line cache used by rendering so the first wheel tick is not lost.
|
|
1504
|
+
if (this.transcriptLines.length === 0 && this.readTranscriptEntries().length > 0) {
|
|
1505
|
+
this.render(this.lastRenderWidth);
|
|
1506
|
+
}
|
|
1368
1507
|
if (delta < 0) {
|
|
1369
1508
|
this.followTranscript = false;
|
|
1370
1509
|
}
|
|
1371
|
-
|
|
1510
|
+
const maxScroll = Math.max(0, this.transcriptLines.length - this.transcriptViewportHeight);
|
|
1511
|
+
this.transcriptScrollOffset = Math.max(
|
|
1512
|
+
0,
|
|
1513
|
+
Math.min(maxScroll, this.transcriptScrollOffset + delta),
|
|
1514
|
+
);
|
|
1515
|
+
if (this.transcriptScrollOffset >= maxScroll) {
|
|
1516
|
+
this.followTranscript = true;
|
|
1517
|
+
}
|
|
1372
1518
|
this.tui.requestRender();
|
|
1373
1519
|
}
|
|
1374
1520
|
|
|
1375
1521
|
dispose(): void {
|
|
1376
|
-
|
|
1522
|
+
// Mouse reporting is owned by the host TUI. This component only handles
|
|
1523
|
+
// normalized events (or passively parses an event already delivered to it).
|
|
1377
1524
|
}
|
|
1378
1525
|
|
|
1379
1526
|
private getMouseScrollDelta(data: string): number | null {
|
|
1380
|
-
const
|
|
1381
|
-
if (!
|
|
1382
|
-
|
|
1527
|
+
const event = parsePeekMouseInput(data);
|
|
1528
|
+
if (!event || event.action !== "wheel") return null;
|
|
1529
|
+
// Match the normalized TUI path and the subagent viewer: one logical
|
|
1530
|
+
// wheel unit means one rendered line. Do not apply a second multiplier.
|
|
1531
|
+
return Math.trunc(event.delta ?? 0);
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
private transcriptEntryAtLine(y: number): number | null {
|
|
1535
|
+
const localLine = y - this.transcriptContentTop;
|
|
1536
|
+
if (localLine < 0 || localLine >= this.transcriptViewportHeight) return null;
|
|
1537
|
+
const line = this.transcriptScrollOffset + localLine;
|
|
1538
|
+
return this.transcriptLineEntryIds[line] ?? null;
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
private focusLiveTranscriptEntry(entryId: number): boolean {
|
|
1542
|
+
const entry = this.readTranscriptEntries().find((candidate) => candidate.id === entryId);
|
|
1543
|
+
if (!entry || entry.type === "turn-boundary") return false;
|
|
1544
|
+
this.liveFocusedEntryId = entryId;
|
|
1545
|
+
this.liveHoveredEntryId = entryId;
|
|
1546
|
+
this.liveFocusedToolCallId = entry.type === "tool-call" || entry.type === "tool-result"
|
|
1547
|
+
? entry.toolCallId ?? null
|
|
1548
|
+
: null;
|
|
1549
|
+
this.tui.requestRender();
|
|
1550
|
+
return true;
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
private peekIndexAtLine(y: number): number | null {
|
|
1554
|
+
if (this.peekBlockStarts.length !== this.peekBlocks.length) return null;
|
|
1555
|
+
const localLine = y - this.peekContentTop;
|
|
1556
|
+
if (localLine < 0 || localLine >= this.peekState.viewportHeight) return null;
|
|
1557
|
+
if (localLine === 0 && this.peekStickyHeaderIndex !== null) {
|
|
1558
|
+
return this.peekStickyHeaderIndex;
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
const line = this.peekState.scrollOffset + localLine;
|
|
1562
|
+
for (let index = 0; index < this.peekBlocks.length; index++) {
|
|
1563
|
+
const start = this.peekBlockStarts[index];
|
|
1564
|
+
const end = this.peekBlockEnds[index] ?? start;
|
|
1565
|
+
if (line >= start && line < end) return index;
|
|
1566
|
+
}
|
|
1567
|
+
return null;
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
/**
|
|
1571
|
+
* Viewer-local mouse seam. Newer Pi TUI hosts call `handleMouse` with
|
|
1572
|
+
* zero-based `{ type, button, x, y, wheelDelta }` events; older hosts and
|
|
1573
|
+
* tests can pass the one-based normalized SGR shape from `peek.ts`.
|
|
1574
|
+
*
|
|
1575
|
+
* Only an explicit left click is actionable. Press/release are consumed
|
|
1576
|
+
* without changing selection, focus, expansion, or the composer draft.
|
|
1577
|
+
*/
|
|
1578
|
+
handleMouse(event: PeekMouseEvent | {
|
|
1579
|
+
type: "press" | "release" | "move" | "drag" | "click" | "wheel";
|
|
1580
|
+
button: "left" | "middle" | "right" | "none" | number;
|
|
1581
|
+
x: number;
|
|
1582
|
+
y: number;
|
|
1583
|
+
clickCount?: number;
|
|
1584
|
+
wheelDelta?: number;
|
|
1585
|
+
}): any {
|
|
1586
|
+
const normalized: PeekMouseEvent = "action" in event
|
|
1587
|
+
? event
|
|
1588
|
+
: {
|
|
1589
|
+
action: event.type === "wheel"
|
|
1590
|
+
? "wheel"
|
|
1591
|
+
: event.type === "click"
|
|
1592
|
+
? "click"
|
|
1593
|
+
: event.type === "release"
|
|
1594
|
+
? "release"
|
|
1595
|
+
: event.type,
|
|
1596
|
+
button: typeof event.button === "number"
|
|
1597
|
+
? event.button
|
|
1598
|
+
: event.button === "left"
|
|
1599
|
+
? 0
|
|
1600
|
+
: event.button === "middle"
|
|
1601
|
+
? 1
|
|
1602
|
+
: event.button === "right"
|
|
1603
|
+
? 2
|
|
1604
|
+
: -1,
|
|
1605
|
+
// Pi TUI coordinates are zero-based local cells; the SGR adapter
|
|
1606
|
+
// and rendered spans below intentionally use one-based rows.
|
|
1607
|
+
x: event.x + 1,
|
|
1608
|
+
y: event.y + 1,
|
|
1609
|
+
clickCount: event.clickCount,
|
|
1610
|
+
delta: event.wheelDelta,
|
|
1611
|
+
};
|
|
1612
|
+
|
|
1613
|
+
if (normalized.action === "wheel") {
|
|
1614
|
+
const delta = Math.trunc(normalized.delta ?? 0);
|
|
1615
|
+
if (this.toolPreviewParent !== null) {
|
|
1616
|
+
this.scrollToolPreview(delta);
|
|
1617
|
+
} else if (this.peekState.active) {
|
|
1618
|
+
this.scrollPeek(delta);
|
|
1619
|
+
} else {
|
|
1620
|
+
this.scrollTranscript(delta);
|
|
1621
|
+
}
|
|
1622
|
+
return { handled: true, render: true };
|
|
1623
|
+
}
|
|
1624
|
+
if (this.toolPreviewParent !== null) {
|
|
1625
|
+
return { handled: true, render: false };
|
|
1383
1626
|
}
|
|
1384
1627
|
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1628
|
+
if (!this.peekState.active) {
|
|
1629
|
+
if (this.transcriptLineEntryIds.length === 0 && this.readTranscriptEntries().length > 0) {
|
|
1630
|
+
this.render(this.lastRenderWidth);
|
|
1631
|
+
}
|
|
1632
|
+
if (normalized.action === "move" || normalized.action === "drag") {
|
|
1633
|
+
const hoveredEntryId = this.transcriptEntryAtLine(normalized.y);
|
|
1634
|
+
if (hoveredEntryId === this.liveHoveredEntryId) return { handled: true, render: false };
|
|
1635
|
+
this.liveHoveredEntryId = hoveredEntryId;
|
|
1636
|
+
this.tui.requestRender();
|
|
1637
|
+
return { handled: true, render: true };
|
|
1638
|
+
}
|
|
1639
|
+
if (normalized.action === "press" || normalized.action === "release") {
|
|
1640
|
+
return { handled: true, render: false };
|
|
1641
|
+
}
|
|
1642
|
+
if (normalized.action !== "click" || normalized.button !== 0) {
|
|
1643
|
+
return { handled: true, render: false };
|
|
1644
|
+
}
|
|
1645
|
+
if (normalized.x < 1 || normalized.x > this.lastRenderWidth) {
|
|
1646
|
+
return { handled: true, render: false };
|
|
1647
|
+
}
|
|
1648
|
+
const entryId = this.transcriptEntryAtLine(normalized.y);
|
|
1649
|
+
if (entryId === null) return { handled: true, render: false };
|
|
1650
|
+
const entry = this.readTranscriptEntries().find((candidate) => candidate.id === entryId);
|
|
1651
|
+
const focused = this.focusLiveTranscriptEntry(entryId);
|
|
1652
|
+
if (focused && (entry?.type === "tool-call" || entry?.type === "tool-result") && entry.toolCallId) {
|
|
1653
|
+
// A single click both focuses the tool row and toggles its compact
|
|
1654
|
+
// details. A second click collapses it, matching the Peek surface.
|
|
1655
|
+
if ((normalized.clickCount ?? 1) >= 2) {
|
|
1656
|
+
this.liveExpandedToolCallId = null;
|
|
1657
|
+
} else {
|
|
1658
|
+
this.liveExpandedToolCallId = this.liveExpandedToolCallId === entry.toolCallId
|
|
1659
|
+
? null
|
|
1660
|
+
: entry.toolCallId;
|
|
1661
|
+
}
|
|
1662
|
+
this.tui.requestRender();
|
|
1663
|
+
}
|
|
1664
|
+
return { handled: true, focus: focused, render: focused };
|
|
1388
1665
|
}
|
|
1389
1666
|
|
|
1390
|
-
|
|
1667
|
+
if (normalized.action === "move" || normalized.action === "drag") {
|
|
1668
|
+
const hoveredIndex = this.peekIndexAtLine(normalized.y);
|
|
1669
|
+
if (hoveredIndex === this.peekHoveredIndex) {
|
|
1670
|
+
return { handled: true, render: false };
|
|
1671
|
+
}
|
|
1672
|
+
this.peekHoveredIndex = hoveredIndex;
|
|
1673
|
+
this.tui.requestRender();
|
|
1674
|
+
return { handled: true, render: true };
|
|
1675
|
+
}
|
|
1676
|
+
if (normalized.action === "press" || normalized.action === "release") {
|
|
1677
|
+
return { handled: true, render: false };
|
|
1678
|
+
}
|
|
1679
|
+
if (normalized.action !== "click" || normalized.button !== 0) return;
|
|
1680
|
+
if (normalized.x < 1 || normalized.x > this.lastRenderWidth || this.peekBlockStarts.length !== this.peekBlocks.length) {
|
|
1681
|
+
return { handled: true, render: false };
|
|
1682
|
+
}
|
|
1683
|
+
|
|
1684
|
+
const targetIndex = this.peekIndexAtLine(normalized.y);
|
|
1685
|
+
if (targetIndex === null) return { handled: true, render: false };
|
|
1686
|
+
this.peekHoveredIndex = targetIndex;
|
|
1687
|
+
this.peekState.selectedIndex = targetIndex;
|
|
1688
|
+
const target = this.peekBlocks[targetIndex];
|
|
1689
|
+
const clickCount = normalized.clickCount ?? 1;
|
|
1690
|
+
const shouldCollapse = clickCount >= 2 &&
|
|
1691
|
+
(target?.type === "tool-call" || target?.type === "tool-result") &&
|
|
1692
|
+
this.expandedToolId === target.id;
|
|
1693
|
+
if (shouldCollapse) {
|
|
1694
|
+
this.expandedToolId = null;
|
|
1695
|
+
this.peekState.scrollOffset = Math.max(0, Math.min(
|
|
1696
|
+
this.peekState.scrollOffset,
|
|
1697
|
+
Math.max(0, this.peekRenderedLines.length - Math.max(1, this.peekState.viewportHeight)),
|
|
1698
|
+
));
|
|
1699
|
+
}
|
|
1700
|
+
this.tui.requestRender();
|
|
1701
|
+
return { handled: true, focus: true, render: true };
|
|
1391
1702
|
}
|
|
1392
1703
|
|
|
1393
1704
|
handleInput(data: string): void {
|
|
@@ -1396,15 +1707,40 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1396
1707
|
return;
|
|
1397
1708
|
}
|
|
1398
1709
|
|
|
1710
|
+
// Older hosts may deliver raw SGR/X10 wheel bytes through handleInput
|
|
1711
|
+
// instead of dispatching a normalized mouse event.
|
|
1712
|
+
const earlyMouseScrollDelta = this.getMouseScrollDelta(data);
|
|
1713
|
+
if (earlyMouseScrollDelta !== null && this.toolPreviewParent !== null) {
|
|
1714
|
+
this.scrollToolPreview(earlyMouseScrollDelta);
|
|
1715
|
+
return;
|
|
1716
|
+
}
|
|
1717
|
+
|
|
1718
|
+
if (this.toolPreviewParent !== null && this.handleToolPreviewInput(data)) {
|
|
1719
|
+
return;
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
// Ctrl+O is live-only: it opens the complete immutable input/output of
|
|
1723
|
+
// the focused tool without interfering with the composer.
|
|
1724
|
+
if (matchesKey(data, BTW_TOOL_OUTPUT_SHORTCUT)) {
|
|
1725
|
+
this.enterLiveToolPreview();
|
|
1726
|
+
if (this.toolPreviewParent === "live") return;
|
|
1727
|
+
}
|
|
1728
|
+
|
|
1729
|
+
const mouseEvent = parsePeekMouseInput(data);
|
|
1399
1730
|
const mouseScrollDelta = this.getMouseScrollDelta(data);
|
|
1400
1731
|
if (mouseScrollDelta !== null) {
|
|
1401
|
-
if (this.
|
|
1732
|
+
if (this.toolPreviewParent !== null) {
|
|
1733
|
+
this.scrollToolPreview(mouseScrollDelta);
|
|
1734
|
+
} else if (this.peekState.active) {
|
|
1402
1735
|
this.scrollPeek(mouseScrollDelta);
|
|
1403
1736
|
} else {
|
|
1404
1737
|
this.scrollTranscript(mouseScrollDelta);
|
|
1405
1738
|
}
|
|
1406
1739
|
return;
|
|
1407
1740
|
}
|
|
1741
|
+
// Consume bounded SGR button events locally. Without an explicit host
|
|
1742
|
+
// click dispatch, press/release are deliberately no-ops rather than input.
|
|
1743
|
+
if (mouseEvent) return;
|
|
1408
1744
|
|
|
1409
1745
|
// Peek mode active → route all input to peek handler
|
|
1410
1746
|
if (this.peekState.active) {
|
|
@@ -1421,8 +1757,12 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1421
1757
|
this.input.handleInput(data);
|
|
1422
1758
|
}
|
|
1423
1759
|
|
|
1424
|
-
private inputFrameLines(innerWidth: number): string[] {
|
|
1425
|
-
const
|
|
1760
|
+
private inputFrameLines(innerWidth: number, maxLines = 6): string[] {
|
|
1761
|
+
const rendered = this.input.render(innerWidth);
|
|
1762
|
+
// CustomEditor owns cursor/history state; only clip its visual viewport,
|
|
1763
|
+
// keeping the newest lines visible instead of allowing the overlay to grow.
|
|
1764
|
+
const lines = rendered.slice(Math.max(0, rendered.length - maxLines));
|
|
1765
|
+
if (lines.length === 0) lines.push("");
|
|
1426
1766
|
return lines.map((l) => {
|
|
1427
1767
|
const pad = Math.max(0, innerWidth - visibleWidth(l));
|
|
1428
1768
|
return `${this.theme.fg("border", "│")}${l}${" ".repeat(pad)}${this.theme.fg("border", "│")}`;
|
|
@@ -1436,20 +1776,33 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1436
1776
|
override render(width: number): string[] {
|
|
1437
1777
|
const dialogWidth = Math.max(24, width);
|
|
1438
1778
|
const innerWidth = Math.max(22, dialogWidth - 2);
|
|
1779
|
+
this.lastRenderWidth = dialogWidth;
|
|
1780
|
+
|
|
1781
|
+
// Tool Output is a read-only view over the currently focused live tool.
|
|
1782
|
+
if (this.toolPreviewParent === "live") {
|
|
1783
|
+
return this.renderLiveToolPreview(innerWidth, width);
|
|
1784
|
+
}
|
|
1439
1785
|
|
|
1440
1786
|
// Peek mode has its own full-content render
|
|
1441
1787
|
if (this.peekState.active) {
|
|
1442
1788
|
return this.renderPeekMode(innerWidth, width);
|
|
1443
1789
|
}
|
|
1444
1790
|
|
|
1445
|
-
// Compute input lines (cap at 6 for layout stability)
|
|
1791
|
+
// Compute input lines (cap at 6 for layout stability). On very short
|
|
1792
|
+
// terminals, reserve the composer a single visible row rather than letting
|
|
1793
|
+
// CustomEditor output push the bottom border out of the viewport.
|
|
1446
1794
|
const rawInputLines = this.input.render(innerWidth);
|
|
1447
|
-
const
|
|
1795
|
+
const terminalRows = Math.max(1, this.tui.terminal?.rows ?? process.stdout.rows ?? 30);
|
|
1796
|
+
const inputBudget = Math.max(1, terminalRows - BTW_OVERLAY_CHROME_BASE - 1);
|
|
1797
|
+
const inputLineCount = Math.max(1, Math.min(rawInputLines.length, 6, inputBudget));
|
|
1448
1798
|
|
|
1449
1799
|
const transcriptLines = this.buildTranscriptLines(innerWidth);
|
|
1800
|
+
this.transcriptLines = transcriptLines;
|
|
1450
1801
|
const chromeHeight = BTW_OVERLAY_CHROME_BASE + inputLineCount;
|
|
1451
1802
|
const dialogHeight = this.getDialogHeight(inputLineCount);
|
|
1452
|
-
|
|
1803
|
+
// On short terminals the viewport yields before the composer/chrome. Do
|
|
1804
|
+
// not force a six-line transcript that pushes the bottom border away.
|
|
1805
|
+
const transcriptHeight = Math.max(1, dialogHeight - chromeHeight);
|
|
1453
1806
|
this.transcriptViewportHeight = transcriptHeight;
|
|
1454
1807
|
|
|
1455
1808
|
const maxScroll = Math.max(0, transcriptLines.length - transcriptHeight);
|
|
@@ -1476,7 +1829,7 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1476
1829
|
|
|
1477
1830
|
const lines = [this.borderLine(innerWidth, "top")];
|
|
1478
1831
|
|
|
1479
|
-
lines.push(this.
|
|
1832
|
+
lines.push(this.titleLine(this.theme.fg("accent", this.theme.bold(this.modeTextValue.trim())), innerWidth));
|
|
1480
1833
|
lines.push(this.frameLine(this.theme.fg("dim", summary), innerWidth));
|
|
1481
1834
|
lines.push(this.ruleLine(innerWidth));
|
|
1482
1835
|
|
|
@@ -1489,7 +1842,7 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1489
1842
|
|
|
1490
1843
|
lines.push(this.ruleLine(innerWidth));
|
|
1491
1844
|
lines.push(this.frameLine(this.theme.fg("warning", this.statusTextValue.trim()), innerWidth));
|
|
1492
|
-
lines.push(...this.inputFrameLines(innerWidth));
|
|
1845
|
+
lines.push(...this.inputFrameLines(innerWidth, inputLineCount));
|
|
1493
1846
|
lines.push(this.frameLine(this.theme.fg("dim", this.hintsTextValue.trim()), innerWidth));
|
|
1494
1847
|
lines.push(this.borderLine(innerWidth, "bottom"));
|
|
1495
1848
|
|
|
@@ -1540,13 +1893,277 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1540
1893
|
|
|
1541
1894
|
// ── Peek Mode ──────────────────────────────────────────────────────
|
|
1542
1895
|
|
|
1896
|
+
private getSelectedPeekBlock(): PeekBlock | undefined {
|
|
1897
|
+
if (this.peekBlocks.length === 0) return undefined;
|
|
1898
|
+
this.peekState.selectedIndex = Math.max(
|
|
1899
|
+
0,
|
|
1900
|
+
Math.min(this.peekState.selectedIndex, this.peekBlocks.length - 1),
|
|
1901
|
+
);
|
|
1902
|
+
return this.peekBlocks[this.peekState.selectedIndex];
|
|
1903
|
+
}
|
|
1904
|
+
|
|
1905
|
+
private buildToolPreviewLines(block: PeekBlock, innerWidth: number): string[] {
|
|
1906
|
+
// Keep the preview read-only and complete. The viewport, not the data,
|
|
1907
|
+
// supplies the bound: only the visible slice is emitted below.
|
|
1908
|
+
const text = getToolPreviewText(block);
|
|
1909
|
+
const lines: string[] = [];
|
|
1910
|
+
for (const line of text.split("\n")) {
|
|
1911
|
+
lines.push(...wrapTextWithAnsi(line, Math.max(1, innerWidth - 4)));
|
|
1912
|
+
}
|
|
1913
|
+
return lines.length > 0 ? lines : ["(no tool data)"];
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
private hydrateToolBlock(block: PeekBlock): PeekBlock {
|
|
1917
|
+
if (!block.toolCallId || !this.readToolArtifact) return block;
|
|
1918
|
+
const artifact = this.readToolArtifact(block.toolCallId);
|
|
1919
|
+
if (!artifact) return block;
|
|
1920
|
+
|
|
1921
|
+
const hydrated = { ...block };
|
|
1922
|
+
const input = formatToolPreview(artifact.input);
|
|
1923
|
+
const output = formatToolPreview(artifact.output);
|
|
1924
|
+
const details = formatToolPreview(artifact.details);
|
|
1925
|
+
if (input) hydrated.toolInput = input;
|
|
1926
|
+
if (output) hydrated.toolOutput = output;
|
|
1927
|
+
if (details) {
|
|
1928
|
+
hydrated.toolOutput = `${hydrated.toolOutput || "(no output)"}\n\nDetails:\n${details}`;
|
|
1929
|
+
}
|
|
1930
|
+
if (artifact.diff) {
|
|
1931
|
+
hydrated.toolOutput = `${hydrated.toolOutput || "(no output)"}\n\nDiff:\n${artifact.diff}`;
|
|
1932
|
+
}
|
|
1933
|
+
hydrated.toolName = artifact.toolName || hydrated.toolName;
|
|
1934
|
+
hydrated.isError = hydrated.isError || artifact.isError;
|
|
1935
|
+
return hydrated;
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
private hydrateToolBlocks(blocks: PeekBlock[]): PeekBlock[] {
|
|
1939
|
+
return blocks.map((block) =>
|
|
1940
|
+
block.type === "tool-call" || block.type === "tool-result"
|
|
1941
|
+
? this.hydrateToolBlock(block)
|
|
1942
|
+
: block,
|
|
1943
|
+
);
|
|
1944
|
+
}
|
|
1945
|
+
|
|
1946
|
+
private findLiveToolPreviewBlock(): PeekBlock | null {
|
|
1947
|
+
const selected = this.liveToolPreviewBlock;
|
|
1948
|
+
if (!selected) return null;
|
|
1949
|
+
const blocks = this.hydrateToolBlocks(buildPeekBlocks(
|
|
1950
|
+
this.readTranscriptEntries() as unknown as PeekEntry[],
|
|
1951
|
+
true,
|
|
1952
|
+
));
|
|
1953
|
+
return blocks.find((block) => block.entryId === selected.entryId) ??
|
|
1954
|
+
blocks.find((block) => block.toolCallId === selected.toolCallId &&
|
|
1955
|
+
(block.type === "tool-call" || block.type === "tool-result")) ?? null;
|
|
1956
|
+
}
|
|
1957
|
+
|
|
1958
|
+
private scrollToolPreview(delta: number): void {
|
|
1959
|
+
// Opening a preview schedules the next render. A fast wheel event can
|
|
1960
|
+
// arrive first, so initialize its wrapped lines before calculating bounds.
|
|
1961
|
+
if (this.toolPreviewParent !== null && this.peekToolPreviewLines.length === 0) {
|
|
1962
|
+
this.render(this.lastRenderWidth);
|
|
1963
|
+
}
|
|
1964
|
+
if (this.toolPreviewParent === null) return;
|
|
1965
|
+
const viewportHeight = Math.max(1, this.peekState.viewportHeight);
|
|
1966
|
+
const maxScroll = Math.max(0, this.peekToolPreviewLines.length - viewportHeight);
|
|
1967
|
+
this.peekToolPreviewScrollOffset = Math.max(
|
|
1968
|
+
0,
|
|
1969
|
+
Math.min(maxScroll, this.peekToolPreviewScrollOffset + delta),
|
|
1970
|
+
);
|
|
1971
|
+
this.tui.requestRender();
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1974
|
+
private renderLiveToolPreview(innerWidth: number, fullWidth: number): string[] {
|
|
1975
|
+
const block = this.findLiveToolPreviewBlock();
|
|
1976
|
+
if (!block) {
|
|
1977
|
+
this.toolPreviewParent = null;
|
|
1978
|
+
this.liveToolPreviewBlock = null;
|
|
1979
|
+
this.peekToolPreviewLines = [];
|
|
1980
|
+
this.peekToolPreviewScrollOffset = 0;
|
|
1981
|
+
return this.render(fullWidth);
|
|
1982
|
+
}
|
|
1983
|
+
|
|
1984
|
+
const dialogHeight = this.getDialogHeight(1);
|
|
1985
|
+
const viewportHeight = Math.max(1, dialogHeight - 6);
|
|
1986
|
+
this.peekState.viewportHeight = viewportHeight;
|
|
1987
|
+
this.peekToolPreviewLines = this.buildToolPreviewLines(block, innerWidth);
|
|
1988
|
+
const maxScroll = Math.max(0, this.peekToolPreviewLines.length - viewportHeight);
|
|
1989
|
+
this.peekToolPreviewScrollOffset = Math.max(
|
|
1990
|
+
0,
|
|
1991
|
+
Math.min(this.peekToolPreviewScrollOffset, maxScroll),
|
|
1992
|
+
);
|
|
1993
|
+
|
|
1994
|
+
const title = `BTW Tool Output · ${getSemanticBlockLabel(block)} · read-only`;
|
|
1995
|
+
const lines = [
|
|
1996
|
+
this.borderLine(innerWidth, "top"),
|
|
1997
|
+
this.titleLine(this.theme.fg("accent", this.theme.bold(title)), innerWidth),
|
|
1998
|
+
this.ruleLine(innerWidth),
|
|
1999
|
+
];
|
|
2000
|
+
const visible = this.peekToolPreviewLines.slice(
|
|
2001
|
+
this.peekToolPreviewScrollOffset,
|
|
2002
|
+
this.peekToolPreviewScrollOffset + viewportHeight,
|
|
2003
|
+
);
|
|
2004
|
+
for (const line of visible) lines.push(this.frameLine(line, innerWidth));
|
|
2005
|
+
for (let i = visible.length; i < viewportHeight; i++) lines.push(this.frameLine("", innerWidth));
|
|
2006
|
+
const hiddenAbove = this.peekToolPreviewScrollOffset;
|
|
2007
|
+
const hiddenBelow = Math.max(0, maxScroll - this.peekToolPreviewScrollOffset);
|
|
2008
|
+
const counts = hiddenAbove || hiddenBelow ? ` · ↑${hiddenAbove} ↓${hiddenBelow}` : "";
|
|
2009
|
+
lines.push(this.ruleLine(innerWidth));
|
|
2010
|
+
lines.push(this.frameLine(this.theme.fg("dim", `j/k ↑↓ g/G PgUp/PgDn Esc:back${counts}`), innerWidth));
|
|
2011
|
+
lines.push(this.borderLine(innerWidth, "bottom"));
|
|
2012
|
+
return lines.map((line) => this.fitRenderedLine(line, fullWidth));
|
|
2013
|
+
}
|
|
2014
|
+
|
|
2015
|
+
private renderToolPreview(innerWidth: number, fullWidth: number): string[] {
|
|
2016
|
+
if (this.peekToolPreviewBlockId !== null) {
|
|
2017
|
+
const refreshedIndex = this.peekBlocks.findIndex((candidate) => candidate.id === this.peekToolPreviewBlockId);
|
|
2018
|
+
this.peekToolPreviewIndex = refreshedIndex >= 0 ? refreshedIndex : null;
|
|
2019
|
+
}
|
|
2020
|
+
const block = this.peekToolPreviewIndex === null ? undefined : this.peekBlocks[this.peekToolPreviewIndex];
|
|
2021
|
+
if (!block || !hasToolPreview(block)) {
|
|
2022
|
+
this.peekToolPreviewIndex = null;
|
|
2023
|
+
this.peekToolPreviewBlockId = null;
|
|
2024
|
+
this.peekToolPreviewLines = [];
|
|
2025
|
+
this.peekToolPreviewScrollOffset = 0;
|
|
2026
|
+
this.toolPreviewParent = null;
|
|
2027
|
+
return this.renderPeekMode(innerWidth, fullWidth);
|
|
2028
|
+
}
|
|
2029
|
+
|
|
2030
|
+
const dialogHeight = this.getDialogHeight(1);
|
|
2031
|
+
const viewportHeight = Math.max(1, dialogHeight - 6);
|
|
2032
|
+
this.peekState.viewportHeight = viewportHeight;
|
|
2033
|
+
this.peekToolPreviewLines = this.buildToolPreviewLines(block, innerWidth);
|
|
2034
|
+
const maxScroll = Math.max(0, this.peekToolPreviewLines.length - viewportHeight);
|
|
2035
|
+
this.peekToolPreviewScrollOffset = Math.max(
|
|
2036
|
+
0,
|
|
2037
|
+
Math.min(this.peekToolPreviewScrollOffset, maxScroll),
|
|
2038
|
+
);
|
|
2039
|
+
|
|
2040
|
+
const title = `BTW Peek · ${getSemanticBlockLabel(block)} · read-only`;
|
|
2041
|
+
const lines = [
|
|
2042
|
+
this.borderLine(innerWidth, "top"),
|
|
2043
|
+
this.titleLine(this.theme.fg("accent", this.theme.bold(title)), innerWidth),
|
|
2044
|
+
this.ruleLine(innerWidth),
|
|
2045
|
+
];
|
|
2046
|
+
const visible = this.peekToolPreviewLines.slice(
|
|
2047
|
+
this.peekToolPreviewScrollOffset,
|
|
2048
|
+
this.peekToolPreviewScrollOffset + viewportHeight,
|
|
2049
|
+
);
|
|
2050
|
+
for (const line of visible) lines.push(this.frameLine(line, innerWidth));
|
|
2051
|
+
while (visible.length < viewportHeight) {
|
|
2052
|
+
visible.push("");
|
|
2053
|
+
lines.push(this.frameLine("", innerWidth));
|
|
2054
|
+
}
|
|
2055
|
+
const hiddenAbove = this.peekToolPreviewScrollOffset;
|
|
2056
|
+
const hiddenBelow = Math.max(0, maxScroll - this.peekToolPreviewScrollOffset);
|
|
2057
|
+
const counts = hiddenAbove || hiddenBelow ? ` · ↑${hiddenAbove} ↓${hiddenBelow}` : "";
|
|
2058
|
+
lines.push(this.ruleLine(innerWidth));
|
|
2059
|
+
lines.push(this.frameLine(this.theme.fg("dim", `j/k ↑↓ g/G PgUp/PgDn q/Esc:back${counts}`), innerWidth));
|
|
2060
|
+
lines.push(this.borderLine(innerWidth, "bottom"));
|
|
2061
|
+
return lines.map((line) => this.fitRenderedLine(line, fullWidth));
|
|
2062
|
+
}
|
|
2063
|
+
|
|
2064
|
+
private enterToolPreview(): void {
|
|
2065
|
+
const block = this.getSelectedPeekBlock();
|
|
2066
|
+
if (!hasToolPreview(block)) return;
|
|
2067
|
+
this.peekToolPreviewIndex = this.peekState.selectedIndex;
|
|
2068
|
+
this.peekToolPreviewBlockId = block?.id ?? null;
|
|
2069
|
+
this.toolPreviewParent = "peek";
|
|
2070
|
+
this.peekToolPreviewScrollOffset = 0;
|
|
2071
|
+
this.peekToolPreviewLines = [];
|
|
2072
|
+
this.tui.requestRender();
|
|
2073
|
+
}
|
|
2074
|
+
|
|
2075
|
+
private enterLiveToolPreview(): void {
|
|
2076
|
+
if (this.peekState.active || !this.liveFocusedToolCallId) return;
|
|
2077
|
+
const block = buildPeekBlocks(
|
|
2078
|
+
this.readTranscriptEntries() as unknown as PeekEntry[],
|
|
2079
|
+
true,
|
|
2080
|
+
).find((candidate) => candidate.toolCallId === this.liveFocusedToolCallId);
|
|
2081
|
+
if (!block) return;
|
|
2082
|
+
this.liveToolPreviewBlock = block;
|
|
2083
|
+
this.toolPreviewParent = "live";
|
|
2084
|
+
this.peekToolPreviewScrollOffset = 0;
|
|
2085
|
+
this.peekToolPreviewLines = [];
|
|
2086
|
+
this.tui.requestRender();
|
|
2087
|
+
}
|
|
2088
|
+
|
|
2089
|
+
private exitToolPreview(): void {
|
|
2090
|
+
this.peekToolPreviewIndex = null;
|
|
2091
|
+
this.peekToolPreviewBlockId = null;
|
|
2092
|
+
this.peekToolPreviewLines = [];
|
|
2093
|
+
this.peekToolPreviewScrollOffset = 0;
|
|
2094
|
+
this.liveToolPreviewBlock = null;
|
|
2095
|
+
this.toolPreviewParent = null;
|
|
2096
|
+
this.tui.requestRender();
|
|
2097
|
+
}
|
|
2098
|
+
|
|
2099
|
+
private toggleExpandedTool(): boolean {
|
|
2100
|
+
const block = this.getSelectedPeekBlock();
|
|
2101
|
+
if (!hasToolPreview(block)) return false;
|
|
2102
|
+
this.expandedToolId = this.expandedToolId === block?.id ? null : block?.id ?? null;
|
|
2103
|
+
this.peekState.scrollOffset = 0;
|
|
2104
|
+
this.tui.requestRender();
|
|
2105
|
+
return true;
|
|
2106
|
+
}
|
|
2107
|
+
|
|
2108
|
+
private handleToolPreviewInput(data: string): boolean {
|
|
2109
|
+
if (this.toolPreviewParent === null) return false;
|
|
2110
|
+
if (data === "q" || data === "\x1b" || data === "\x03") {
|
|
2111
|
+
this.exitToolPreview();
|
|
2112
|
+
return true;
|
|
2113
|
+
}
|
|
2114
|
+
const viewportHeight = Math.max(1, this.peekState.viewportHeight);
|
|
2115
|
+
const maxScroll = Math.max(0, this.peekToolPreviewLines.length - viewportHeight);
|
|
2116
|
+
if (data === "j" || data === "\x1b[B" || data === "\x1b[OB") {
|
|
2117
|
+
this.peekToolPreviewScrollOffset = Math.min(maxScroll, this.peekToolPreviewScrollOffset + 1);
|
|
2118
|
+
this.tui.requestRender();
|
|
2119
|
+
return true;
|
|
2120
|
+
}
|
|
2121
|
+
if (data === "k" || data === "\x1b[A" || data === "\x1b[OA") {
|
|
2122
|
+
this.peekToolPreviewScrollOffset = Math.max(0, this.peekToolPreviewScrollOffset - 1);
|
|
2123
|
+
this.tui.requestRender();
|
|
2124
|
+
return true;
|
|
2125
|
+
}
|
|
2126
|
+
if (data === "g") {
|
|
2127
|
+
this.peekToolPreviewScrollOffset = 0;
|
|
2128
|
+
this.tui.requestRender();
|
|
2129
|
+
return true;
|
|
2130
|
+
}
|
|
2131
|
+
if (data === "G") {
|
|
2132
|
+
this.peekToolPreviewScrollOffset = maxScroll;
|
|
2133
|
+
this.tui.requestRender();
|
|
2134
|
+
return true;
|
|
2135
|
+
}
|
|
2136
|
+
if (matchesPageKey(data, 5)) {
|
|
2137
|
+
this.peekToolPreviewScrollOffset = Math.max(0, this.peekToolPreviewScrollOffset - Math.max(1, viewportHeight - 2));
|
|
2138
|
+
this.tui.requestRender();
|
|
2139
|
+
return true;
|
|
2140
|
+
}
|
|
2141
|
+
if (matchesPageKey(data, 6)) {
|
|
2142
|
+
this.peekToolPreviewScrollOffset = Math.min(maxScroll, this.peekToolPreviewScrollOffset + Math.max(1, viewportHeight - 2));
|
|
2143
|
+
this.tui.requestRender();
|
|
2144
|
+
return true;
|
|
2145
|
+
}
|
|
2146
|
+
return true;
|
|
2147
|
+
}
|
|
2148
|
+
|
|
1543
2149
|
private renderPeekMode(innerWidth: number, fullWidth: number): string[] {
|
|
1544
|
-
|
|
2150
|
+
if (this.peekToolPreviewIndex !== null) {
|
|
2151
|
+
return this.renderToolPreview(innerWidth, fullWidth);
|
|
2152
|
+
}
|
|
2153
|
+
const dialogHeight = this.getDialogHeight(1);
|
|
2154
|
+
// Normal Peek chrome is six rows: top/title/rule + rule/status/bottom.
|
|
2155
|
+
this.peekState.viewportHeight = Math.max(1, dialogHeight - 6);
|
|
1545
2156
|
const vh = this.peekState.viewportHeight;
|
|
1546
2157
|
const lines: string[] = [];
|
|
1547
2158
|
|
|
1548
2159
|
// ── Header ───────────────────────────────────────────────────────
|
|
1549
2160
|
const blockCount = this.peekBlocks.length;
|
|
2161
|
+
this.peekState.selectedIndex = blockCount === 0
|
|
2162
|
+
? 0
|
|
2163
|
+
: Math.max(0, Math.min(this.peekState.selectedIndex, blockCount - 1));
|
|
2164
|
+
if (this.expandedToolId !== null && !this.peekBlocks.some((block) => block.id === this.expandedToolId && hasToolPreview(block))) {
|
|
2165
|
+
this.expandedToolId = null;
|
|
2166
|
+
}
|
|
1550
2167
|
const pct =
|
|
1551
2168
|
blockCount > 0
|
|
1552
2169
|
? `${Math.round((this.peekState.selectedIndex / Math.max(1, blockCount - 1)) * 100)}%`
|
|
@@ -1557,16 +2174,17 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1557
2174
|
? ` search ${this.peekState.currentMatchIndex + 1}/${this.peekState.searchMatches.length}`
|
|
1558
2175
|
: "");
|
|
1559
2176
|
lines.push(this.borderLine(innerWidth, "top"));
|
|
1560
|
-
lines.push(this.
|
|
2177
|
+
lines.push(this.titleLine(this.theme.fg("accent", this.theme.bold(title)), innerWidth));
|
|
1561
2178
|
lines.push(this.ruleLine(innerWidth));
|
|
1562
2179
|
|
|
1563
2180
|
// ── Search bar ───────────────────────────────────────────────────
|
|
1564
2181
|
if (this.peekState.searchActive) {
|
|
1565
2182
|
const searchLine = `/ ${this.peekSearchBuffer}${this.theme.fg("warning", "█")}`;
|
|
2183
|
+
const searchViewportHeight = Math.max(1, dialogHeight - 8);
|
|
1566
2184
|
lines.push(this.frameLine(this.theme.fg("accent", searchLine), innerWidth));
|
|
1567
2185
|
lines.push(this.ruleLine(innerWidth));
|
|
1568
|
-
// Fill rest with blank lines
|
|
1569
|
-
for (let i = 0; i <
|
|
2186
|
+
// Fill rest with blank lines while keeping the footer and border visible.
|
|
2187
|
+
for (let i = 0; i < searchViewportHeight; i++) {
|
|
1570
2188
|
lines.push(this.frameLine("", innerWidth));
|
|
1571
2189
|
}
|
|
1572
2190
|
lines.push(this.ruleLine(innerWidth));
|
|
@@ -1577,58 +2195,62 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1577
2195
|
|
|
1578
2196
|
// ── Content ──────────────────────────────────────────────────────
|
|
1579
2197
|
const mdContentWidth = Math.max(1, innerWidth - 6);
|
|
2198
|
+
this.peekContentTop = 4;
|
|
1580
2199
|
// Build rendered lines and remember each block's starting line so j/k
|
|
1581
2200
|
// can scroll by line while J/K still jump by message.
|
|
1582
2201
|
const blockRenderLines: string[][] = [];
|
|
1583
2202
|
this.peekBlockStarts = [];
|
|
2203
|
+
this.peekBlockEnds = [];
|
|
1584
2204
|
this.peekHeaderLines = [];
|
|
1585
2205
|
let renderedLineCount = 0;
|
|
1586
2206
|
|
|
1587
2207
|
for (const block of this.peekBlocks) {
|
|
1588
2208
|
const blockLines: string[] = [];
|
|
1589
2209
|
this.peekBlockStarts.push(renderedLineCount);
|
|
1590
|
-
const label =
|
|
2210
|
+
const label = getSemanticBlockLabel(block);
|
|
1591
2211
|
const isCurrent = block.id === this.peekBlocks[this.peekState.selectedIndex]?.id;
|
|
2212
|
+
const isHovered = block.id === this.peekBlocks[this.peekHoveredIndex ?? -1]?.id;
|
|
1592
2213
|
|
|
1593
|
-
//
|
|
1594
|
-
|
|
2214
|
+
// The rail is the sole selection indicator. Hover is a quieter cue and
|
|
2215
|
+
// never changes the selected target until an explicit click arrives.
|
|
2216
|
+
const prefix = isCurrent ? "▎ " : isHovered ? "│ " : " ";
|
|
1595
2217
|
if (block.type === "thinking") {
|
|
1596
|
-
blockLines.push(`${prefix}${this.theme.fg("warning", this.theme.bold(this.theme.italic(`── ${label} ──`)))}`);
|
|
1597
|
-
} else if (block.type === "tool-call") {
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
const
|
|
1601
|
-
|
|
2218
|
+
blockLines.push(`${prefix}${this.theme.fg(isHovered ? "text" : "warning", this.theme.bold(this.theme.italic(`── ${label} ──`)))}`);
|
|
2219
|
+
} else if (block.type === "tool-call" || block.type === "tool-result") {
|
|
2220
|
+
const resultIcon = block.type === "tool-result" ? (block.isError ? "✗" : "✓") : "→";
|
|
2221
|
+
const toolColor = block.isError ? "error" : isHovered ? "text" : "toolTitle";
|
|
2222
|
+
const detail = block.type === "tool-result"
|
|
2223
|
+
? getToolResultSummary(block.text)
|
|
2224
|
+
: block.toolInput || "(no input)";
|
|
2225
|
+
const detailColor = block.isError ? "error" : isHovered ? "text" : "dim";
|
|
2226
|
+
blockLines.push(`${prefix}${this.theme.fg(toolColor, this.theme.bold(`· ${block.toolName ?? "?"} ${resultIcon}`))} ${this.theme.fg(detailColor, detail)}`);
|
|
1602
2227
|
} else if (block.type === "user") {
|
|
1603
|
-
blockLines.push(`${prefix}${this.theme.fg("accent", this.theme.bold(`── ${label} ──`))}`);
|
|
2228
|
+
blockLines.push(`${prefix}${this.theme.fg(isHovered ? "text" : "accent", this.theme.bold(`── ${label} ──`))}`);
|
|
1604
2229
|
} else {
|
|
1605
|
-
blockLines.push(`${prefix}${this.theme.fg("success", this.theme.bold(`── ${label} ──`))}`);
|
|
2230
|
+
blockLines.push(`${prefix}${this.theme.fg(isHovered ? "text" : "success", this.theme.bold(`── ${label} ──`))}`);
|
|
1606
2231
|
}
|
|
1607
2232
|
|
|
1608
|
-
//
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
2233
|
+
// Tool rows stay compact by default. Enter is an optional safe
|
|
2234
|
+
// expansion; the full read-only view is also available through `w`.
|
|
2235
|
+
if (block.type === "tool-call" || block.type === "tool-result") {
|
|
2236
|
+
if (this.expandedToolId === block.id && hasToolPreview(block)) {
|
|
2237
|
+
const toolLines = this.buildToolPreviewLines(block, innerWidth);
|
|
2238
|
+
for (const toolLine of toolLines) {
|
|
2239
|
+
blockLines.push(`${isCurrent ? "▎ " : " "}${this.theme.fg(block.isError ? "error" : "dim", toolLine)}`);
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
} else if (block.text) {
|
|
2243
|
+
if (block.type === "assistant-text" || block.type === "user") {
|
|
1612
2244
|
const md = new Markdown(block.text, 0, 0, this.mdTheme);
|
|
1613
2245
|
const mdLines = md.render(mdContentWidth);
|
|
1614
2246
|
for (const mdLine of mdLines) {
|
|
1615
|
-
blockLines.push(`${isCurrent ? "▎ " : " "}${mdLine}`);
|
|
1616
|
-
}
|
|
1617
|
-
} else if (block.type === "tool-result" && block.isError) {
|
|
1618
|
-
const styledLines = wrapTextWithAnsi(block.text, mdContentWidth);
|
|
1619
|
-
for (const sl of styledLines) {
|
|
1620
|
-
blockLines.push(`${isCurrent ? "▎ " : " "}${this.theme.fg("error", sl)}`);
|
|
2247
|
+
blockLines.push(`${isCurrent ? "▎ " : " "}${mdLine.replace(/[ \t]+$/u, "")}`);
|
|
1621
2248
|
}
|
|
1622
2249
|
} else if (block.type === "thinking") {
|
|
1623
2250
|
const styledLines = wrapTextWithAnsi(block.text, mdContentWidth);
|
|
1624
2251
|
for (const sl of styledLines) {
|
|
1625
2252
|
blockLines.push(`${isCurrent ? "▎ " : " "}${this.theme.fg("warning", this.theme.italic(sl))}`);
|
|
1626
2253
|
}
|
|
1627
|
-
} else {
|
|
1628
|
-
const styledLines = wrapTextWithAnsi(block.text, mdContentWidth);
|
|
1629
|
-
for (const sl of styledLines) {
|
|
1630
|
-
blockLines.push(`${isCurrent ? "▎ " : " "}${sl}`);
|
|
1631
|
-
}
|
|
1632
2254
|
}
|
|
1633
2255
|
}
|
|
1634
2256
|
|
|
@@ -1638,6 +2260,9 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1638
2260
|
blockRenderLines.push(blockLines);
|
|
1639
2261
|
this.peekHeaderLines.push(blockLines[0] ?? "");
|
|
1640
2262
|
renderedLineCount += blockLines.length;
|
|
2263
|
+
// The final blank is visual separation, not part of the block hitbox.
|
|
2264
|
+
// This keeps clicks on separators/padding from changing focus.
|
|
2265
|
+
this.peekBlockEnds.push(Math.max(this.peekBlockStarts[this.peekBlockStarts.length - 1] + 1, renderedLineCount - 1));
|
|
1641
2266
|
}
|
|
1642
2267
|
|
|
1643
2268
|
this.peekRenderedLines = blockRenderLines.flat();
|
|
@@ -1645,12 +2270,14 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1645
2270
|
this.peekState.scrollOffset = Math.max(0, Math.min(this.peekState.scrollOffset, maxScroll));
|
|
1646
2271
|
|
|
1647
2272
|
const visibleStart = this.peekState.scrollOffset;
|
|
2273
|
+
this.peekStickyHeaderIndex = null;
|
|
1648
2274
|
const displayLines = this.peekRenderedLines.slice(visibleStart, visibleStart + vh);
|
|
1649
2275
|
if (blockCount === 0) {
|
|
1650
2276
|
displayLines.push(this.theme.fg("dim", "No BTW thread yet."));
|
|
1651
2277
|
}
|
|
1652
2278
|
const selectedStart = this.peekBlockStarts[this.peekState.selectedIndex];
|
|
1653
2279
|
if (selectedStart !== undefined && selectedStart < visibleStart && displayLines.length > 0) {
|
|
2280
|
+
this.peekStickyHeaderIndex = this.peekState.selectedIndex;
|
|
1654
2281
|
displayLines[0] = this.peekHeaderLines[this.peekState.selectedIndex] ?? displayLines[0];
|
|
1655
2282
|
}
|
|
1656
2283
|
|
|
@@ -1668,7 +2295,10 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1668
2295
|
? ` ${this.peekState.currentMatchIndex + 1}/${this.peekState.searchMatches.length}`
|
|
1669
2296
|
: "";
|
|
1670
2297
|
const toolsHint = this.peekState.showTools ? "t:hide" : "t:show";
|
|
1671
|
-
const
|
|
2298
|
+
const selected = this.getSelectedPeekBlock();
|
|
2299
|
+
const previewHint = hasToolPreview(selected) ? " w:preview" : "";
|
|
2300
|
+
const expandHint = hasToolPreview(selected) ? " Enter:expand" : "";
|
|
2301
|
+
const statusText = `j/k ↑↓ J/K msg g/G /search${searchHint} ${toolsHint}${previewHint} M:copy o:nvim${expandHint} q:exit`;
|
|
1672
2302
|
lines.push(this.ruleLine(innerWidth));
|
|
1673
2303
|
lines.push(this.frameLine(this.theme.fg("dim", statusText), innerWidth));
|
|
1674
2304
|
lines.push(this.borderLine(innerWidth, "bottom"));
|
|
@@ -1681,32 +2311,72 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1681
2311
|
this.peekState.active = true;
|
|
1682
2312
|
const entries = this.readTranscriptEntries();
|
|
1683
2313
|
this.peekState.showTools = true;
|
|
1684
|
-
this.peekBlocks = buildPeekBlocks(
|
|
2314
|
+
this.peekBlocks = this.hydrateToolBlocks(buildPeekBlocks(
|
|
1685
2315
|
entries as unknown as PeekEntry[],
|
|
1686
2316
|
this.peekState.showTools,
|
|
1687
|
-
);
|
|
2317
|
+
));
|
|
2318
|
+
this.peekState.blocks = this.peekBlocks;
|
|
1688
2319
|
this.peekState.selectedIndex = 0;
|
|
1689
2320
|
this.peekState.scrollOffset = 0;
|
|
2321
|
+
this.peekHoveredIndex = null;
|
|
2322
|
+
this.peekToolPreviewIndex = null;
|
|
2323
|
+
this.peekToolPreviewBlockId = null;
|
|
2324
|
+
this.peekToolPreviewLines = [];
|
|
2325
|
+
this.peekToolPreviewScrollOffset = 0;
|
|
2326
|
+
this.expandedToolId = null;
|
|
2327
|
+
this.peekBlockStarts = [];
|
|
2328
|
+
this.peekBlockEnds = [];
|
|
1690
2329
|
this.tui.requestRender();
|
|
1691
2330
|
}
|
|
1692
2331
|
|
|
1693
2332
|
exitPeekMode(): void {
|
|
1694
2333
|
this.peekState.active = false;
|
|
1695
2334
|
this.peekState.searchActive = false;
|
|
2335
|
+
this.peekHoveredIndex = null;
|
|
1696
2336
|
this.peekSearchBuffer = "";
|
|
2337
|
+
this.peekToolPreviewIndex = null;
|
|
2338
|
+
this.peekToolPreviewBlockId = null;
|
|
2339
|
+
this.peekToolPreviewLines = [];
|
|
2340
|
+
this.peekToolPreviewScrollOffset = 0;
|
|
2341
|
+
this.liveToolPreviewBlock = null;
|
|
2342
|
+
this.toolPreviewParent = null;
|
|
2343
|
+
this.expandedToolId = null;
|
|
1697
2344
|
this.tui.requestRender();
|
|
1698
2345
|
}
|
|
1699
2346
|
|
|
1700
2347
|
private rebuildPeekBlocks(): void {
|
|
1701
2348
|
const entries = this.readTranscriptEntries();
|
|
1702
|
-
this.peekBlocks = buildPeekBlocks(
|
|
2349
|
+
this.peekBlocks = this.hydrateToolBlocks(buildPeekBlocks(
|
|
1703
2350
|
entries as unknown as PeekEntry[],
|
|
1704
2351
|
this.peekState.showTools,
|
|
1705
|
-
);
|
|
2352
|
+
));
|
|
2353
|
+
this.peekState.blocks = this.peekBlocks;
|
|
1706
2354
|
|
|
1707
2355
|
const maxIndex = Math.max(0, this.peekBlocks.length - 1);
|
|
1708
|
-
this.peekState.selectedIndex =
|
|
2356
|
+
this.peekState.selectedIndex = this.peekBlocks.length === 0
|
|
2357
|
+
? 0
|
|
2358
|
+
: Math.max(0, Math.min(this.peekState.selectedIndex, maxIndex));
|
|
1709
2359
|
this.peekState.scrollOffset = Math.max(0, this.peekState.scrollOffset);
|
|
2360
|
+
this.peekHoveredIndex = null;
|
|
2361
|
+
// Existing line spans belong to the old transcript snapshot.
|
|
2362
|
+
this.peekBlockStarts = [];
|
|
2363
|
+
this.peekBlockEnds = [];
|
|
2364
|
+
this.peekRenderedLines = [];
|
|
2365
|
+
this.peekHeaderLines = [];
|
|
2366
|
+
if (this.expandedToolId !== null && !this.peekBlocks.some((block) => block.id === this.expandedToolId && hasToolPreview(block))) {
|
|
2367
|
+
this.expandedToolId = null;
|
|
2368
|
+
}
|
|
2369
|
+
if (this.peekToolPreviewBlockId !== null) {
|
|
2370
|
+
const previewIndex = this.peekBlocks.findIndex((block) => block.id === this.peekToolPreviewBlockId);
|
|
2371
|
+
if (previewIndex < 0 || !hasToolPreview(this.peekBlocks[previewIndex])) {
|
|
2372
|
+
this.peekToolPreviewIndex = null;
|
|
2373
|
+
this.peekToolPreviewBlockId = null;
|
|
2374
|
+
this.peekToolPreviewLines = [];
|
|
2375
|
+
this.peekToolPreviewScrollOffset = 0;
|
|
2376
|
+
} else {
|
|
2377
|
+
this.peekToolPreviewIndex = previewIndex;
|
|
2378
|
+
}
|
|
2379
|
+
}
|
|
1710
2380
|
if (this.peekState.searchQuery) {
|
|
1711
2381
|
this.peekState.searchMatches = findSearchMatches(
|
|
1712
2382
|
this.peekBlocks,
|
|
@@ -1721,22 +2391,33 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1721
2391
|
}
|
|
1722
2392
|
|
|
1723
2393
|
private scrollPeek(delta: number): void {
|
|
2394
|
+
// A key can arrive before the first post-enter render. Build the line
|
|
2395
|
+
// spans once so scrolling and mouse selection use the same coordinates.
|
|
2396
|
+
if (this.peekBlocks.length > 0 && this.peekBlockStarts.length !== this.peekBlocks.length) {
|
|
2397
|
+
this.render(this.lastRenderWidth);
|
|
2398
|
+
}
|
|
2399
|
+
|
|
1724
2400
|
const maxScroll = Math.max(0, this.peekRenderedLines.length - this.peekState.viewportHeight);
|
|
1725
2401
|
this.peekState.scrollOffset = Math.max(
|
|
1726
2402
|
0,
|
|
1727
2403
|
Math.min(maxScroll, this.peekState.scrollOffset + delta),
|
|
1728
2404
|
);
|
|
1729
2405
|
|
|
1730
|
-
//
|
|
1731
|
-
//
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
2406
|
+
// The block containing the current top line is the one selected. Include
|
|
2407
|
+
// tool blocks: there is one target, not a second hidden tool focus.
|
|
2408
|
+
if (this.peekBlocks.length > 0 && this.peekBlockStarts.length === this.peekBlocks.length) {
|
|
2409
|
+
let selected = 0;
|
|
2410
|
+
for (let i = 0; i < this.peekBlockStarts.length; i++) {
|
|
2411
|
+
if (this.peekBlockStarts[i] > this.peekState.scrollOffset) break;
|
|
1736
2412
|
selected = i;
|
|
1737
2413
|
}
|
|
2414
|
+
this.peekState.selectedIndex = selected;
|
|
2415
|
+
} else {
|
|
2416
|
+
this.peekState.selectedIndex = Math.max(
|
|
2417
|
+
0,
|
|
2418
|
+
Math.min(this.peekState.selectedIndex, Math.max(0, this.peekBlocks.length - 1)),
|
|
2419
|
+
);
|
|
1738
2420
|
}
|
|
1739
|
-
this.peekState.selectedIndex = selected;
|
|
1740
2421
|
this.tui.requestRender();
|
|
1741
2422
|
}
|
|
1742
2423
|
|
|
@@ -1746,6 +2427,9 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1746
2427
|
}
|
|
1747
2428
|
|
|
1748
2429
|
private jumpPeekMessage(direction: 1 | -1): void {
|
|
2430
|
+
if (this.peekBlocks.length > 0 && this.peekBlockStarts.length !== this.peekBlocks.length) {
|
|
2431
|
+
this.render(this.lastRenderWidth);
|
|
2432
|
+
}
|
|
1749
2433
|
const isMessage = (block: PeekBlock): boolean =>
|
|
1750
2434
|
block.type !== "tool-call" && block.type !== "tool-result";
|
|
1751
2435
|
let target = this.peekState.selectedIndex + direction;
|
|
@@ -1761,32 +2445,40 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1761
2445
|
}
|
|
1762
2446
|
|
|
1763
2447
|
private ensurePeekVisible(): void {
|
|
2448
|
+
if (this.peekBlocks.length === 0) {
|
|
2449
|
+
this.peekState.selectedIndex = 0;
|
|
2450
|
+
this.peekState.scrollOffset = 0;
|
|
2451
|
+
return;
|
|
2452
|
+
}
|
|
2453
|
+
this.peekState.selectedIndex = Math.max(
|
|
2454
|
+
0,
|
|
2455
|
+
Math.min(this.peekState.selectedIndex, this.peekBlocks.length - 1),
|
|
2456
|
+
);
|
|
1764
2457
|
const start = this.peekBlockStarts[this.peekState.selectedIndex];
|
|
1765
2458
|
if (start === undefined) return;
|
|
1766
2459
|
|
|
1767
|
-
const vh = this.peekState.viewportHeight;
|
|
2460
|
+
const vh = Math.max(1, this.peekState.viewportHeight);
|
|
2461
|
+
const maxScroll = Math.max(0, this.peekRenderedLines.length - vh);
|
|
1768
2462
|
if (start < this.peekState.scrollOffset) {
|
|
1769
2463
|
this.peekState.scrollOffset = start;
|
|
1770
2464
|
} else if (start >= this.peekState.scrollOffset + vh) {
|
|
1771
2465
|
this.peekState.scrollOffset = start - vh + 1;
|
|
1772
2466
|
}
|
|
2467
|
+
this.peekState.scrollOffset = Math.max(0, Math.min(this.peekState.scrollOffset, maxScroll));
|
|
1773
2468
|
}
|
|
1774
2469
|
|
|
1775
2470
|
private peekGoToFirst(): void {
|
|
1776
|
-
this.peekState.selectedIndex = 0;
|
|
2471
|
+
this.peekState.selectedIndex = this.peekBlocks.length === 0 ? 0 : 0;
|
|
1777
2472
|
this.peekState.scrollOffset = 0;
|
|
1778
2473
|
this.tui.requestRender();
|
|
1779
2474
|
}
|
|
1780
2475
|
|
|
1781
2476
|
private peekGoToLast(): void {
|
|
1782
|
-
|
|
1783
|
-
while (last > 0 && (this.peekBlocks[last]?.type === "tool-call" || this.peekBlocks[last]?.type === "tool-result")) {
|
|
1784
|
-
last--;
|
|
1785
|
-
}
|
|
2477
|
+
const last = Math.max(0, this.peekBlocks.length - 1);
|
|
1786
2478
|
this.peekState.selectedIndex = last;
|
|
1787
2479
|
this.peekState.scrollOffset = Math.max(
|
|
1788
2480
|
0,
|
|
1789
|
-
this.peekRenderedLines.length - this.peekState.viewportHeight,
|
|
2481
|
+
this.peekRenderedLines.length - Math.max(1, this.peekState.viewportHeight),
|
|
1790
2482
|
);
|
|
1791
2483
|
this.tui.requestRender();
|
|
1792
2484
|
}
|
|
@@ -1794,8 +2486,16 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1794
2486
|
private peekToggleTools(): void {
|
|
1795
2487
|
this.peekState.showTools = !this.peekState.showTools;
|
|
1796
2488
|
this.rebuildPeekBlocks();
|
|
1797
|
-
this.peekState.selectedIndex =
|
|
2489
|
+
this.peekState.selectedIndex = this.peekBlocks.length === 0
|
|
2490
|
+
? 0
|
|
2491
|
+
: Math.max(0, Math.min(this.peekState.selectedIndex, this.peekBlocks.length - 1));
|
|
1798
2492
|
this.peekState.scrollOffset = 0;
|
|
2493
|
+
this.peekHoveredIndex = null;
|
|
2494
|
+
this.expandedToolId = null;
|
|
2495
|
+
this.peekToolPreviewIndex = null;
|
|
2496
|
+
this.peekToolPreviewBlockId = null;
|
|
2497
|
+
this.peekToolPreviewLines = [];
|
|
2498
|
+
this.peekToolPreviewScrollOffset = 0;
|
|
1799
2499
|
this.peekState.searchMatches = [];
|
|
1800
2500
|
this.peekState.currentMatchIndex = -1;
|
|
1801
2501
|
this.tui.requestRender();
|
|
@@ -1911,6 +2611,10 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1911
2611
|
}
|
|
1912
2612
|
|
|
1913
2613
|
private handlePeekInput(data: string): void {
|
|
2614
|
+
if (this.peekToolPreviewIndex !== null && this.handleToolPreviewInput(data)) {
|
|
2615
|
+
return;
|
|
2616
|
+
}
|
|
2617
|
+
|
|
1914
2618
|
// If in search mode, capture input for search query
|
|
1915
2619
|
if (this.peekState.searchActive) {
|
|
1916
2620
|
if (data === "\r" || data === "\n") {
|
|
@@ -1982,6 +2686,19 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1982
2686
|
return;
|
|
1983
2687
|
}
|
|
1984
2688
|
|
|
2689
|
+
// Full tool input/output preview. It is intentionally a no-op for text
|
|
2690
|
+
// blocks and tools whose BTW data model has no input/output.
|
|
2691
|
+
if (data === "w") {
|
|
2692
|
+
this.enterToolPreview();
|
|
2693
|
+
return;
|
|
2694
|
+
}
|
|
2695
|
+
|
|
2696
|
+
// Enter can expand a tool card, but must never submit while Peek owns input.
|
|
2697
|
+
if (data === "\r" || data === "\n") {
|
|
2698
|
+
this.toggleExpandedTool();
|
|
2699
|
+
return;
|
|
2700
|
+
}
|
|
2701
|
+
|
|
1985
2702
|
// Copy current message
|
|
1986
2703
|
if (data === "M") {
|
|
1987
2704
|
void this.peekCopyCurrent();
|
|
@@ -1995,12 +2712,12 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1995
2712
|
}
|
|
1996
2713
|
|
|
1997
2714
|
// Page up/down
|
|
1998
|
-
if (data
|
|
2715
|
+
if (matchesPageKey(data, 5)) {
|
|
1999
2716
|
// PageUp
|
|
2000
2717
|
this.scrollPeekPage(-1);
|
|
2001
2718
|
return;
|
|
2002
2719
|
}
|
|
2003
|
-
if (data
|
|
2720
|
+
if (matchesPageKey(data, 6)) {
|
|
2004
2721
|
// PageDown
|
|
2005
2722
|
this.scrollPeekPage(1);
|
|
2006
2723
|
return;
|
|
@@ -2008,7 +2725,7 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
2008
2725
|
|
|
2009
2726
|
// Leaving Peek always returns to the BTW composer. The composer itself
|
|
2010
2727
|
// remains responsible for dismissing the BTW overlay.
|
|
2011
|
-
if (data === "q" || data === "\x03" || data === "\
|
|
2728
|
+
if (data === "q" || data === "\x03" || data === "\x1b") {
|
|
2012
2729
|
this.exitPeekMode();
|
|
2013
2730
|
return;
|
|
2014
2731
|
}
|
|
@@ -2036,6 +2753,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
2036
2753
|
let overlayRuntime: OverlayRuntime | null = null;
|
|
2037
2754
|
let lastUiContext: ExtensionContext | ExtensionCommandContext | null = null;
|
|
2038
2755
|
let activeBtwSession: BtwSessionRuntime | null = null;
|
|
2756
|
+
let btwStore: BtwTranscriptStore | null = null;
|
|
2039
2757
|
|
|
2040
2758
|
function syncUi(ctx?: ExtensionContext | ExtensionCommandContext): void {
|
|
2041
2759
|
const activeCtx = ctx ?? lastUiContext;
|
|
@@ -2050,6 +2768,68 @@ export default function (pi: ExtensionAPI) {
|
|
|
2050
2768
|
syncUi(ctx);
|
|
2051
2769
|
}
|
|
2052
2770
|
|
|
2771
|
+
function ensureBtwStore(ctx: ExtensionContext | ExtensionCommandContext): BtwTranscriptStore {
|
|
2772
|
+
const sessionId = ctx.sessionManager.getSessionId();
|
|
2773
|
+
if (!btwStore || btwStore.threadId !== sessionId) {
|
|
2774
|
+
btwStore = new BtwTranscriptStore(ctx.cwd, sessionId);
|
|
2775
|
+
}
|
|
2776
|
+
return btwStore;
|
|
2777
|
+
}
|
|
2778
|
+
|
|
2779
|
+
function persistTranscriptState(): void {
|
|
2780
|
+
if (!btwStore) return;
|
|
2781
|
+
try {
|
|
2782
|
+
const snapshot = btwStore.appendSnapshot({
|
|
2783
|
+
mode: pendingMode,
|
|
2784
|
+
entries: transcriptState.entries as unknown as DurableTranscriptSnapshot["entries"],
|
|
2785
|
+
nextEntryId: transcriptState.nextEntryId,
|
|
2786
|
+
nextTurnId: transcriptState.nextTurnId,
|
|
2787
|
+
currentTurnId: transcriptState.currentTurnId,
|
|
2788
|
+
lastTurnId: transcriptState.lastTurnId,
|
|
2789
|
+
});
|
|
2790
|
+
// Keep only a small, immutable locator in the Pi session. Large tool
|
|
2791
|
+
// payloads remain in .pi-subagents/btw and never enter LLM context.
|
|
2792
|
+
pi.appendEntry(BTW_TRANSCRIPT_TYPE, makeTranscriptLocator(btwStore, snapshot));
|
|
2793
|
+
} catch {
|
|
2794
|
+
// Durable BTW storage is best effort. The in-memory transcript remains
|
|
2795
|
+
// authoritative for the current overlay when a filesystem is unavailable.
|
|
2796
|
+
}
|
|
2797
|
+
}
|
|
2798
|
+
|
|
2799
|
+
function persistToolArtifact(event: AgentSessionEvent): void {
|
|
2800
|
+
if (!btwStore) return;
|
|
2801
|
+
try {
|
|
2802
|
+
if (event.type === "tool_execution_start") {
|
|
2803
|
+
btwStore.writeToolArtifact({
|
|
2804
|
+
toolCallId: event.toolCallId,
|
|
2805
|
+
toolName: event.toolName,
|
|
2806
|
+
phase: "start",
|
|
2807
|
+
input: event.args,
|
|
2808
|
+
isError: false,
|
|
2809
|
+
});
|
|
2810
|
+
} else if (event.type === "tool_execution_update") {
|
|
2811
|
+
btwStore.writeToolArtifact({
|
|
2812
|
+
toolCallId: event.toolCallId,
|
|
2813
|
+
toolName: event.toolName,
|
|
2814
|
+
phase: "update",
|
|
2815
|
+
output: event.partialResult,
|
|
2816
|
+
isError: false,
|
|
2817
|
+
});
|
|
2818
|
+
} else if (event.type === "tool_execution_end") {
|
|
2819
|
+
btwStore.writeToolArtifact({
|
|
2820
|
+
toolCallId: event.toolCallId,
|
|
2821
|
+
toolName: event.toolName,
|
|
2822
|
+
phase: "end",
|
|
2823
|
+
output: event.result,
|
|
2824
|
+
isError: event.isError,
|
|
2825
|
+
});
|
|
2826
|
+
}
|
|
2827
|
+
} catch {
|
|
2828
|
+
// Tool artifacts are diagnostics; a failed artifact write must not abort
|
|
2829
|
+
// the side conversation or change its visible transcript.
|
|
2830
|
+
}
|
|
2831
|
+
}
|
|
2832
|
+
|
|
2053
2833
|
function setOverlayDraft(value: string): void {
|
|
2054
2834
|
overlayDraft = value;
|
|
2055
2835
|
overlayRuntime?.setDraft?.(value);
|
|
@@ -2109,11 +2889,17 @@ export default function (pi: ExtensionAPI) {
|
|
|
2109
2889
|
event: AgentSessionEvent,
|
|
2110
2890
|
ctx?: ExtensionContext | ExtensionCommandContext,
|
|
2111
2891
|
): void {
|
|
2112
|
-
if (activeBtwSession?.session !== sessionRuntime.session
|
|
2892
|
+
if (activeBtwSession?.session !== sessionRuntime.session) {
|
|
2113
2893
|
return;
|
|
2114
2894
|
}
|
|
2115
2895
|
|
|
2896
|
+
persistToolArtifact(event);
|
|
2116
2897
|
applyTranscriptEvent(transcriptState, event);
|
|
2898
|
+
persistTranscriptState();
|
|
2899
|
+
|
|
2900
|
+
if (!overlayRuntime) {
|
|
2901
|
+
return;
|
|
2902
|
+
}
|
|
2117
2903
|
|
|
2118
2904
|
if (event.type === "tool_execution_start") {
|
|
2119
2905
|
setOverlayStatus(`⏳ running tool: ${event.toolName}`, ctx);
|
|
@@ -2400,6 +3186,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
2400
3186
|
},
|
|
2401
3187
|
undefined,
|
|
2402
3188
|
ctx.cwd,
|
|
3189
|
+
(toolCallId) => btwStore?.readToolArtifact(toolCallId) ?? null,
|
|
2403
3190
|
);
|
|
2404
3191
|
|
|
2405
3192
|
overlay.focused = runtime.handle?.isFocused() ?? true;
|
|
@@ -2677,6 +3464,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
2677
3464
|
pendingThread = [];
|
|
2678
3465
|
pendingMode = mode;
|
|
2679
3466
|
transcriptState = createEmptyTranscriptState();
|
|
3467
|
+
ensureBtwStore(ctx);
|
|
3468
|
+
persistTranscriptState();
|
|
2680
3469
|
setOverlayDraft("");
|
|
2681
3470
|
setOverlayStatus(null, ctx);
|
|
2682
3471
|
if (persist) {
|
|
@@ -2696,9 +3485,11 @@ export default function (pi: ExtensionAPI) {
|
|
|
2696
3485
|
overlayDraft = "";
|
|
2697
3486
|
lastUiContext = ctx;
|
|
2698
3487
|
overlayStatus = null;
|
|
3488
|
+
btwStore = new BtwTranscriptStore(ctx.cwd, ctx.sessionManager.getSessionId());
|
|
2699
3489
|
|
|
2700
3490
|
const branch = ctx.sessionManager.getBranch();
|
|
2701
3491
|
let lastResetIndex = -1;
|
|
3492
|
+
let transcriptLocator: { schemaVersion: typeof BTW_TRANSCRIPT_SCHEMA_VERSION; threadId: string; recordId: string } | null = null;
|
|
2702
3493
|
|
|
2703
3494
|
for (let i = 0; i < branch.length; i++) {
|
|
2704
3495
|
if (isCustomEntry(branch[i], BTW_MODEL_OVERRIDE_TYPE)) {
|
|
@@ -2720,7 +3511,9 @@ export default function (pi: ExtensionAPI) {
|
|
|
2720
3511
|
const details = (branch[i] as unknown as { data?: BtwThinkingOverrideDetails }).data;
|
|
2721
3512
|
btwThinkingOverride =
|
|
2722
3513
|
details?.action === "set"
|
|
2723
|
-
? details.thinkingLevel
|
|
3514
|
+
? isSessionThinkingLevel(details.thinkingLevel)
|
|
3515
|
+
? details.thinkingLevel
|
|
3516
|
+
: null
|
|
2724
3517
|
: details?.action === "clear"
|
|
2725
3518
|
? null
|
|
2726
3519
|
: btwThinkingOverride;
|
|
@@ -2728,9 +3521,41 @@ export default function (pi: ExtensionAPI) {
|
|
|
2728
3521
|
|
|
2729
3522
|
if (isCustomEntry(branch[i], BTW_RESET_TYPE)) {
|
|
2730
3523
|
lastResetIndex = i;
|
|
3524
|
+
transcriptLocator = null;
|
|
2731
3525
|
const details = (branch[i] as unknown as { data?: BtwResetDetails }).data;
|
|
2732
3526
|
pendingMode = details?.mode ?? "contextual";
|
|
2733
3527
|
}
|
|
3528
|
+
|
|
3529
|
+
if (i > lastResetIndex && isCustomEntry(branch[i], BTW_TRANSCRIPT_TYPE)) {
|
|
3530
|
+
const details = (branch[i] as unknown as { data?: unknown }).data;
|
|
3531
|
+
if (isTranscriptLocator(details)) {
|
|
3532
|
+
transcriptLocator = details;
|
|
3533
|
+
}
|
|
3534
|
+
}
|
|
3535
|
+
}
|
|
3536
|
+
|
|
3537
|
+
// A branch must opt into a persisted snapshot through its locator. Never
|
|
3538
|
+
// fall back to the newest file record here: the same session can have
|
|
3539
|
+
// divergent branches and that would leak one branch's BTW transcript into
|
|
3540
|
+
// another branch.
|
|
3541
|
+
const restored = transcriptLocator && transcriptLocator.threadId === btwStore.threadId
|
|
3542
|
+
? btwStore.readSnapshot(transcriptLocator.recordId)
|
|
3543
|
+
: null;
|
|
3544
|
+
if (restored) {
|
|
3545
|
+
transcriptState = {
|
|
3546
|
+
entries: restored.snapshot.entries as unknown as BtwTranscript,
|
|
3547
|
+
nextEntryId: restored.snapshot.nextEntryId,
|
|
3548
|
+
nextTurnId: restored.snapshot.nextTurnId,
|
|
3549
|
+
currentTurnId: restored.snapshot.currentTurnId,
|
|
3550
|
+
lastTurnId: restored.snapshot.lastTurnId,
|
|
3551
|
+
toolCalls: restored.toolCalls,
|
|
3552
|
+
};
|
|
3553
|
+
pendingMode = restored.snapshot.mode;
|
|
3554
|
+
if (closeRestoredStreamingTurns(transcriptState)) {
|
|
3555
|
+
// Make the recovery durable immediately so another reload observes the
|
|
3556
|
+
// same closed historical turn instead of reviving a dead stream.
|
|
3557
|
+
persistTranscriptState();
|
|
3558
|
+
}
|
|
2734
3559
|
}
|
|
2735
3560
|
|
|
2736
3561
|
for (const entry of branch.slice(lastResetIndex + 1)) {
|
|
@@ -2745,13 +3570,23 @@ export default function (pi: ExtensionAPI) {
|
|
|
2745
3570
|
|
|
2746
3571
|
const normalizedDetails: BtwDetails = {
|
|
2747
3572
|
...details,
|
|
3573
|
+
schemaVersion: details.schemaVersion === BTW_TRANSCRIPT_SCHEMA_VERSION
|
|
3574
|
+
? details.schemaVersion
|
|
3575
|
+
: BTW_TRANSCRIPT_SCHEMA_VERSION,
|
|
2748
3576
|
api: details.api || ctx.model?.api || "openai-responses",
|
|
2749
3577
|
};
|
|
2750
3578
|
|
|
2751
3579
|
pendingThread.push(normalizedDetails);
|
|
2752
|
-
|
|
3580
|
+
if (!restored) {
|
|
3581
|
+
appendPersistedTranscriptTurn(transcriptState, normalizedDetails);
|
|
3582
|
+
}
|
|
2753
3583
|
}
|
|
2754
3584
|
|
|
3585
|
+
// Sessions created before durable BTW storage still get a first snapshot
|
|
3586
|
+
// after restore, while current sessions retain the exact persisted state.
|
|
3587
|
+
if (!restored && pendingThread.length > 0) {
|
|
3588
|
+
persistTranscriptState();
|
|
3589
|
+
}
|
|
2755
3590
|
syncUi(ctx);
|
|
2756
3591
|
}
|
|
2757
3592
|
|
|
@@ -2762,6 +3597,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
2762
3597
|
mode: BtwThreadMode,
|
|
2763
3598
|
): Promise<void> {
|
|
2764
3599
|
lastUiContext = ctx;
|
|
3600
|
+
ensureBtwStore(ctx);
|
|
2765
3601
|
const settings = await resolveBtwSettings(ctx);
|
|
2766
3602
|
const model = settings.model;
|
|
2767
3603
|
if (!model) {
|
|
@@ -2804,6 +3640,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
2804
3640
|
}
|
|
2805
3641
|
if (response.stopReason === "aborted") {
|
|
2806
3642
|
removeTranscriptTurn(transcriptState, transcriptState.lastTurnId ?? transcriptState.currentTurnId);
|
|
3643
|
+
persistTranscriptState();
|
|
2807
3644
|
setOverlayStatus("Request aborted.", ctx);
|
|
2808
3645
|
return;
|
|
2809
3646
|
}
|
|
@@ -2818,6 +3655,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
2818
3655
|
const thinking = extractThinking(response) || streamedThinking || "";
|
|
2819
3656
|
|
|
2820
3657
|
const details: BtwDetails = {
|
|
3658
|
+
schemaVersion: BTW_TRANSCRIPT_SCHEMA_VERSION,
|
|
2821
3659
|
question,
|
|
2822
3660
|
thinking,
|
|
2823
3661
|
answer,
|
|
@@ -2831,6 +3669,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
2831
3669
|
|
|
2832
3670
|
pendingThread.push(details);
|
|
2833
3671
|
pi.appendEntry(BTW_ENTRY_TYPE, details);
|
|
3672
|
+
persistTranscriptState();
|
|
2834
3673
|
|
|
2835
3674
|
const saveState = saveVisibleBtwNote(pi, details, saveRequested, wasBusy);
|
|
2836
3675
|
if (saveState === "saved") {
|
|
@@ -2845,6 +3684,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
2845
3684
|
} catch (error) {
|
|
2846
3685
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
2847
3686
|
setTranscriptFailure(transcriptState, errorMessage);
|
|
3687
|
+
persistTranscriptState();
|
|
2848
3688
|
setOverlayStatus("Request failed. Thread preserved for retry or follow-up.", ctx);
|
|
2849
3689
|
notify(ctx, errorMessage, "error");
|
|
2850
3690
|
await disposeBtwSession();
|