@narumitw/pi-btw 0.61.2 → 0.63.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -7
- package/dist/index.ts +1486 -726
- package/dist/index.ts.map +4 -4
- package/docs/core-alignment.md +277 -0
- package/docs/main-context-runtime.md +45 -0
- package/docs/native-appearance.md +857 -0
- package/docs/workflows.md +80 -16
- package/package.json +4 -4
- package/src/btw.ts +185 -116
- package/src/conversation-chrome.ts +93 -0
- package/src/fullscreen-renderer.ts +329 -0
- package/src/fullscreen-ui.ts +109 -315
- package/src/import-main-updates.ts +84 -0
- package/src/keybindings.ts +125 -22
- package/src/main-context-snapshot.ts +125 -0
- package/src/menu.ts +43 -27
- package/src/session-lifetime.ts +66 -0
- package/src/side-thread.ts +66 -17
- package/src/transcript-markdown.ts +5 -5
- package/src/transcript-pager.ts +412 -169
- package/src/workspace-layout.ts +96 -36
package/src/transcript-pager.ts
CHANGED
|
@@ -2,7 +2,6 @@ import type { AssistantMessage } from "@earendil-works/pi-ai";
|
|
|
2
2
|
import {
|
|
3
3
|
AssistantMessageComponent,
|
|
4
4
|
getMarkdownTheme,
|
|
5
|
-
type KeybindingsManager,
|
|
6
5
|
type MarkdownTransformer,
|
|
7
6
|
type Theme,
|
|
8
7
|
UserMessageComponent,
|
|
@@ -13,27 +12,198 @@ import {
|
|
|
13
12
|
Editor,
|
|
14
13
|
type EditorTheme,
|
|
15
14
|
type Focusable,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
isKeyRelease,
|
|
16
|
+
type Keybindings,
|
|
17
|
+
type KeybindingsManager,
|
|
18
|
+
renderFakeCursor,
|
|
20
19
|
ScrollView,
|
|
20
|
+
Spacer,
|
|
21
|
+
Text,
|
|
21
22
|
type TUI,
|
|
22
23
|
truncateToWidth,
|
|
23
24
|
VStack,
|
|
24
25
|
visibleWidth,
|
|
25
26
|
} from "@earendil-works/pi-tui";
|
|
26
|
-
import {
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
import {
|
|
28
|
+
type BtwDisplayModel,
|
|
29
|
+
BtwWorkingIndicator,
|
|
30
|
+
type BtwWorkspaceFocus,
|
|
31
|
+
renderConversationFooter,
|
|
32
|
+
renderWorkingBorder,
|
|
33
|
+
} from "./conversation-chrome.js";
|
|
34
|
+
import {
|
|
35
|
+
BtwPasteGuard,
|
|
36
|
+
type BtwShortcuts,
|
|
37
|
+
btwKeyHasReachableInput,
|
|
38
|
+
getBtwHostInputBlockers,
|
|
39
|
+
getBtwKeybindings,
|
|
40
|
+
getBtwShortcuts,
|
|
41
|
+
normalizeBtwKey,
|
|
42
|
+
} from "./keybindings.js";
|
|
43
|
+
import type { BtwThinkingLevel, SideTranscriptItem } from "./side-thread.js";
|
|
44
|
+
import { formatKeyLabel, sanitizeSingleLine } from "./text.js";
|
|
29
45
|
import type { BtwFullscreenLayoutComponent } from "./workspace-layout.js";
|
|
30
46
|
|
|
31
|
-
const TRANSCRIPT_CHROME_LINES = 2;
|
|
32
47
|
const MAX_STEERING_DISPLAY_LINES = 3;
|
|
33
48
|
const OSC133_MARKERS = ["\u001b]133;A\u0007", "\u001b]133;B\u0007", "\u001b]133;C\u0007"];
|
|
34
49
|
// Pi renders a spacer above the custom component and a two-line built-in footer below it.
|
|
35
50
|
const RESERVED_APP_LINES = 3;
|
|
36
51
|
|
|
52
|
+
// Native mouse focus can land directly on this Editor. Route keyboard input
|
|
53
|
+
// through its logical BTW owner without replacing native mouse/cursor handling.
|
|
54
|
+
class BtwComposer extends Editor {
|
|
55
|
+
constructor(
|
|
56
|
+
tui: TUI,
|
|
57
|
+
theme: EditorTheme,
|
|
58
|
+
private readonly dispatch: (data: string) => void,
|
|
59
|
+
private readonly loader?: BtwWorkingIndicator,
|
|
60
|
+
) {
|
|
61
|
+
super(tui, theme);
|
|
62
|
+
}
|
|
63
|
+
override handleInput(data: string): void {
|
|
64
|
+
this.dispatch(data);
|
|
65
|
+
}
|
|
66
|
+
handleEditorInput(data: string): void {
|
|
67
|
+
super.handleInput(data);
|
|
68
|
+
}
|
|
69
|
+
override render(width: number): string[] {
|
|
70
|
+
if (width <= 0) return [];
|
|
71
|
+
// Core word wrapping cannot fit a two-cell grapheme into layoutWidth=1.
|
|
72
|
+
// Measure at least two text cells, then clip only the display boundary.
|
|
73
|
+
return super.render(Math.max(3, width)).map((line) => {
|
|
74
|
+
// A wide grapheme cannot fit a one-cell dock. Keep its cursor cell usable.
|
|
75
|
+
if (width <= 2 && line.includes(CURSOR_MARKER))
|
|
76
|
+
return CURSOR_MARKER + renderFakeCursor(" ") + " ".repeat(width - 1);
|
|
77
|
+
return truncateToWidth(line, width, "");
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
protected override renderTopBorder(width: number, hiddenLineCount: number): string {
|
|
81
|
+
if (!this.loader) return super.renderTopBorder(width, hiddenLineCount);
|
|
82
|
+
return (
|
|
83
|
+
renderWorkingBorder(width, hiddenLineCount, this.borderColor, this.loader) ??
|
|
84
|
+
super.renderTopBorder(width, hiddenLineCount)
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const VIEWPORT_ACTIONS = [
|
|
90
|
+
"tui.altScreen.search",
|
|
91
|
+
"tui.altScreen.pageUp",
|
|
92
|
+
"tui.altScreen.pageDown",
|
|
93
|
+
"tui.altScreen.halfPageUp",
|
|
94
|
+
"tui.altScreen.halfPageDown",
|
|
95
|
+
"tui.altScreen.lineUp",
|
|
96
|
+
"tui.altScreen.lineDown",
|
|
97
|
+
"tui.altScreen.previousPrompt",
|
|
98
|
+
"tui.altScreen.nextPrompt",
|
|
99
|
+
"tui.altScreen.top",
|
|
100
|
+
"tui.altScreen.bottom",
|
|
101
|
+
] as const;
|
|
102
|
+
// Editor's actual order before submit, with no autocomplete provider installed.
|
|
103
|
+
const BEFORE_SUBMIT = [
|
|
104
|
+
"tui.input.copy",
|
|
105
|
+
"tui.editor.undo",
|
|
106
|
+
"tui.input.tab",
|
|
107
|
+
"tui.editor.deleteToLineEnd",
|
|
108
|
+
"tui.editor.deleteToLineStart",
|
|
109
|
+
"tui.editor.deleteWordBackward",
|
|
110
|
+
"tui.editor.deleteWordForward",
|
|
111
|
+
"tui.editor.deleteCharBackward",
|
|
112
|
+
"tui.editor.deleteCharForward",
|
|
113
|
+
"tui.editor.yank",
|
|
114
|
+
"tui.editor.yankPop",
|
|
115
|
+
"tui.editor.historyPrevious",
|
|
116
|
+
"tui.editor.historyNext",
|
|
117
|
+
"tui.editor.cursorLineStart",
|
|
118
|
+
"tui.editor.cursorLineEnd",
|
|
119
|
+
"tui.editor.cursorWordLeft",
|
|
120
|
+
"tui.editor.cursorWordRight",
|
|
121
|
+
"tui.input.newLine",
|
|
122
|
+
] as const;
|
|
123
|
+
|
|
124
|
+
function usableKeys(keybindings: KeybindingsManager, action: keyof Keybindings, blockers: readonly string[]): string[] {
|
|
125
|
+
return keybindings.getKeys(action).flatMap((candidate) => {
|
|
126
|
+
const key = normalizeBtwKey(candidate);
|
|
127
|
+
return key && btwKeyHasReachableInput(key, blockers) ? [key] : [];
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function submitHint(tui: TUI, keybindings: KeybindingsManager, verb: string): string {
|
|
132
|
+
const earlier =
|
|
133
|
+
tui.mode === "fullscreen" ? VIEWPORT_ACTIONS : (["tui.altScreen.pageUp", "tui.altScreen.pageDown"] as const);
|
|
134
|
+
const blockers = [
|
|
135
|
+
...getBtwHostInputBlockers(tui),
|
|
136
|
+
"ctrl+c",
|
|
137
|
+
"shift+backspace",
|
|
138
|
+
"shift+delete",
|
|
139
|
+
...[...earlier, ...BEFORE_SUBMIT].flatMap((id) => keybindings.getKeys(id)),
|
|
140
|
+
];
|
|
141
|
+
const key = usableKeys(keybindings, "tui.input.submit", blockers)[0];
|
|
142
|
+
return key ? `${formatKeyLabel(key)} ${verb}` : "";
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function pagingHint(tui: TUI, keybindings: KeybindingsManager): string {
|
|
146
|
+
const actions = ["tui.altScreen.pageUp", "tui.altScreen.pageDown"] as const;
|
|
147
|
+
const labels = actions.flatMap((action) => {
|
|
148
|
+
const earlier =
|
|
149
|
+
tui.mode === "fullscreen"
|
|
150
|
+
? VIEWPORT_ACTIONS.slice(0, VIEWPORT_ACTIONS.indexOf(action))
|
|
151
|
+
: action === "tui.altScreen.pageDown"
|
|
152
|
+
? (["tui.altScreen.pageUp"] as const)
|
|
153
|
+
: [];
|
|
154
|
+
const blockers = [...getBtwHostInputBlockers(tui), "ctrl+c", ...earlier.flatMap((id) => keybindings.getKeys(id))];
|
|
155
|
+
const key = usableKeys(keybindings, action, blockers)[0];
|
|
156
|
+
return key
|
|
157
|
+
? [
|
|
158
|
+
formatKeyLabel(key)
|
|
159
|
+
.replace(/Pageup$/u, "PgUp")
|
|
160
|
+
.replace(/Pagedown$/u, "PgDn"),
|
|
161
|
+
]
|
|
162
|
+
: [];
|
|
163
|
+
});
|
|
164
|
+
return labels.length ? `${labels.join("/")} history` : "";
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function handleFallbackPaging(
|
|
168
|
+
data: string,
|
|
169
|
+
tui: TUI,
|
|
170
|
+
keybindings: KeybindingsManager,
|
|
171
|
+
scrollView: ScrollView,
|
|
172
|
+
): boolean {
|
|
173
|
+
// The native renderer already dispatched these before the focused component.
|
|
174
|
+
if (tui.mode === "fullscreen" || isKeyRelease(data)) return false;
|
|
175
|
+
for (const [action, direction] of [
|
|
176
|
+
["tui.altScreen.pageUp", -1],
|
|
177
|
+
["tui.altScreen.pageDown", 1],
|
|
178
|
+
] as const) {
|
|
179
|
+
if (keybindings.matches(data, action)) {
|
|
180
|
+
scrollView.scrollBy(direction * Math.max(1, scrollView.viewportHeight - 4));
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function fittingHint(width: number, variants: readonly string[]): string {
|
|
188
|
+
return variants.find((hint) => visibleWidth(hint) <= Math.max(0, width)) ?? "";
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function compactHints(parts: readonly string[]): string {
|
|
192
|
+
return parts.filter(Boolean).join(" • ");
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function releaseScrollbar(scrollView: ScrollView): void {
|
|
196
|
+
scrollView.setScrollbarActive(false);
|
|
197
|
+
// setScrollbar owns cancellation of its private native hide timer.
|
|
198
|
+
scrollView.setScrollbar("hidden");
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function thinkingBorder(theme: Theme, level: BtwThinkingLevel | undefined, text: string): string {
|
|
202
|
+
return level && theme.getThinkingBorderColor
|
|
203
|
+
? theme.getThinkingBorderColor(level)(text)
|
|
204
|
+
: theme.fg("borderMuted", text);
|
|
205
|
+
}
|
|
206
|
+
|
|
37
207
|
// A temporary fit after manual scrolling must not silently resume following new output.
|
|
38
208
|
class PreservingScrollView extends ScrollView {
|
|
39
209
|
override updateLayout(contentHeight: number, viewportHeight: number, requestRender: () => void): void {
|
|
@@ -48,6 +218,7 @@ class PreservingScrollView extends ScrollView {
|
|
|
48
218
|
export type TranscriptPagerAction =
|
|
49
219
|
| { kind: "submit"; question: string }
|
|
50
220
|
| { kind: "bringToMain"; questionDraft: string }
|
|
221
|
+
| { kind: "importMainUpdates"; questionDraft: string }
|
|
51
222
|
| { kind: "close" };
|
|
52
223
|
|
|
53
224
|
export interface BtwThinkingControl {
|
|
@@ -58,6 +229,8 @@ export interface BtwThinkingControl {
|
|
|
58
229
|
}
|
|
59
230
|
|
|
60
231
|
export interface BtwAnsweringViewOptions {
|
|
232
|
+
model?: BtwDisplayModel;
|
|
233
|
+
keybindings?: KeybindingsManager;
|
|
61
234
|
markdownTransformers?: readonly MarkdownTransformer[];
|
|
62
235
|
steering?: {
|
|
63
236
|
questions: readonly string[];
|
|
@@ -67,10 +240,18 @@ export interface BtwAnsweringViewOptions {
|
|
|
67
240
|
}
|
|
68
241
|
|
|
69
242
|
export class BtwTranscriptPager implements BtwFullscreenLayoutComponent, Focusable {
|
|
243
|
+
private workspaceFocus: BtwWorkspaceFocus | undefined;
|
|
244
|
+
private get keybindings(): KeybindingsManager {
|
|
245
|
+
return getBtwKeybindings(this.tui, this.options.keybindings ?? this.options.thinking?.keybindings);
|
|
246
|
+
}
|
|
247
|
+
setWorkspaceFocus(pane: "side" | "main", shortcutLabel: string | undefined): void {
|
|
248
|
+
this.workspaceFocus = { pane, shortcutLabel: shortcutLabel ? sanitizeSingleLine(shortcutLabel) : undefined };
|
|
249
|
+
this.tui.requestRender();
|
|
250
|
+
}
|
|
70
251
|
private readonly shortcuts: BtwShortcuts;
|
|
71
252
|
private readonly pasteGuard = new BtwPasteGuard();
|
|
72
253
|
private readonly transcriptComponents: Component[];
|
|
73
|
-
private readonly editor:
|
|
254
|
+
private readonly editor: BtwComposer;
|
|
74
255
|
private readonly canBringToMain: boolean;
|
|
75
256
|
private readonly scrollView: ScrollView;
|
|
76
257
|
private readonly layoutRoot: VStack;
|
|
@@ -83,21 +264,24 @@ export class BtwTranscriptPager implements BtwFullscreenLayoutComponent, Focusab
|
|
|
83
264
|
constructor(
|
|
84
265
|
private readonly tui: TUI,
|
|
85
266
|
private readonly theme: Theme,
|
|
86
|
-
turns: readonly
|
|
267
|
+
turns: readonly SideTranscriptItem[],
|
|
87
268
|
private readonly onAction: (action: TranscriptPagerAction) => void,
|
|
88
269
|
private readonly options: {
|
|
270
|
+
model?: BtwDisplayModel;
|
|
271
|
+
keybindings?: KeybindingsManager;
|
|
272
|
+
mainUpdates?: () => boolean;
|
|
89
273
|
startAtBottom?: boolean;
|
|
90
274
|
initialQuestion?: string;
|
|
91
275
|
markdownTransformers?: readonly MarkdownTransformer[];
|
|
92
276
|
thinking?: BtwThinkingControl;
|
|
93
277
|
} = {},
|
|
94
278
|
) {
|
|
95
|
-
this.shortcuts = getBtwShortcuts(tui,
|
|
279
|
+
this.shortcuts = getBtwShortcuts(tui, this.keybindings);
|
|
96
280
|
this.transcriptComponents = buildTranscriptComponents(turns, this.theme, undefined, options.markdownTransformers);
|
|
97
281
|
this.canBringToMain = turns.some((turn) => turn.kind === "answered");
|
|
98
282
|
this.thinkingLevel = options.thinking?.level;
|
|
99
283
|
const editorTheme: EditorTheme = {
|
|
100
|
-
borderColor: (text) => this.theme.
|
|
284
|
+
borderColor: (text) => thinkingBorder(this.theme, this.thinkingLevel, text),
|
|
101
285
|
selectList: {
|
|
102
286
|
selectedPrefix: (text) => this.theme.fg("accent", text),
|
|
103
287
|
selectedText: (text) => this.theme.fg("accent", text),
|
|
@@ -106,7 +290,7 @@ export class BtwTranscriptPager implements BtwFullscreenLayoutComponent, Focusab
|
|
|
106
290
|
noMatch: (text) => this.theme.fg("warning", text),
|
|
107
291
|
},
|
|
108
292
|
};
|
|
109
|
-
this.editor = new
|
|
293
|
+
this.editor = new BtwComposer(this.tui, editorTheme, (data) => this.handleInput(data));
|
|
110
294
|
if (options.initialQuestion) this.editor.setText(options.initialQuestion);
|
|
111
295
|
this.editor.onChange = () => {
|
|
112
296
|
this.warning = undefined;
|
|
@@ -122,14 +306,20 @@ export class BtwTranscriptPager implements BtwFullscreenLayoutComponent, Focusab
|
|
|
122
306
|
};
|
|
123
307
|
const transcript = this.createTranscriptComponent();
|
|
124
308
|
this.scrollView = new PreservingScrollView(transcript, {
|
|
125
|
-
follow:
|
|
309
|
+
follow: "end",
|
|
126
310
|
primary: true,
|
|
311
|
+
scrollbar: "auto",
|
|
312
|
+
scrollbarTrackStyle: (text) => this.theme.fg("scrollbarTrack", text),
|
|
313
|
+
scrollbarThumbStyle: (text) => this.theme.fg("scrollbarThumb", text),
|
|
127
314
|
});
|
|
315
|
+
if (!options.startAtBottom) this.scrollView.scrollTo(0, { disableFollow: true });
|
|
316
|
+
const dock = new VStack([
|
|
317
|
+
{ component: this.editor, basis: "auto", shrink: 1, minSize: 1 },
|
|
318
|
+
{ component: this.createFooterComponent(), basis: "auto", shrink: 1, minSize: 0 },
|
|
319
|
+
]);
|
|
128
320
|
this.layoutRoot = new VStack([
|
|
129
|
-
{ component: this.createHeaderComponent(), basis: 1, shrink: 0, minSize: 1 },
|
|
130
321
|
{ component: this.scrollView, basis: 0, grow: 1, minSize: 0 },
|
|
131
|
-
{ component:
|
|
132
|
-
{ component: this.editor, basis: "auto", shrink: 1, minSize: 0 },
|
|
322
|
+
{ component: dock, basis: "auto", shrink: 1, minSize: 1 },
|
|
133
323
|
]);
|
|
134
324
|
}
|
|
135
325
|
|
|
@@ -151,17 +341,16 @@ export class BtwTranscriptPager implements BtwFullscreenLayoutComponent, Focusab
|
|
|
151
341
|
}
|
|
152
342
|
|
|
153
343
|
render(width: number): string[] {
|
|
154
|
-
if (width <= 0) return [];
|
|
344
|
+
if (width <= 0 || this.tui.terminal.rows <= 0) return [];
|
|
155
345
|
const safeWidth = Math.max(1, width);
|
|
156
346
|
const editorLines = this.editor.render(safeWidth);
|
|
157
347
|
const availableRows = Math.max(1, this.tui.terminal.rows - RESERVED_APP_LINES);
|
|
158
|
-
const viewportHeight = Math.max(0, availableRows - editorLines.length - TRANSCRIPT_CHROME_LINES);
|
|
159
348
|
const contentLines = renderTranscriptLines(this.transcriptComponents, safeWidth);
|
|
160
349
|
this.lastContentLineCount = contentLines.length;
|
|
350
|
+
const viewportHeight = Math.max(0, availableRows - editorLines.length - this.renderFooter(safeWidth).length);
|
|
161
351
|
this.scrollView.updateLayout(contentLines.length, viewportHeight, () => this.tui.requestRender());
|
|
162
352
|
|
|
163
353
|
return fitComposerLayout(
|
|
164
|
-
renderSideThreadHeader(safeWidth, this.theme, this.thinkingLevel),
|
|
165
354
|
contentLines.slice(this.scrollView.scrollTop, this.scrollView.scrollTop + viewportHeight),
|
|
166
355
|
this.renderFooter(safeWidth),
|
|
167
356
|
editorLines,
|
|
@@ -172,7 +361,7 @@ export class BtwTranscriptPager implements BtwFullscreenLayoutComponent, Focusab
|
|
|
172
361
|
handleInput(data: string): void {
|
|
173
362
|
if (this.finished) return;
|
|
174
363
|
if (this.pasteGuard.consume(data)) {
|
|
175
|
-
this.editor.
|
|
364
|
+
this.editor.handleEditorInput(data);
|
|
176
365
|
this.tui.requestRender();
|
|
177
366
|
return;
|
|
178
367
|
}
|
|
@@ -181,6 +370,11 @@ export class BtwTranscriptPager implements BtwFullscreenLayoutComponent, Focusab
|
|
|
181
370
|
this.onAction({ kind: "close" });
|
|
182
371
|
return;
|
|
183
372
|
}
|
|
373
|
+
if (this.options.mainUpdates && this.shortcuts.matches(data, "importMainUpdates")) {
|
|
374
|
+
this.finished = true;
|
|
375
|
+
this.onAction({ kind: "importMainUpdates", questionDraft: this.editor.getExpandedText() });
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
184
378
|
if (this.canBringToMain && this.shortcuts.matches(data, "bringToMain")) {
|
|
185
379
|
this.finished = true;
|
|
186
380
|
this.onAction({ kind: "bringToMain", questionDraft: this.editor.getExpandedText() });
|
|
@@ -198,17 +392,9 @@ export class BtwTranscriptPager implements BtwFullscreenLayoutComponent, Focusab
|
|
|
198
392
|
}
|
|
199
393
|
return;
|
|
200
394
|
}
|
|
201
|
-
if (
|
|
202
|
-
this.
|
|
203
|
-
this.tui.requestRender();
|
|
204
|
-
return;
|
|
395
|
+
if (!handleFallbackPaging(data, this.tui, this.keybindings, this.scrollView)) {
|
|
396
|
+
this.editor.handleEditorInput(data);
|
|
205
397
|
}
|
|
206
|
-
if (matchesKey(data, Key.pageDown)) {
|
|
207
|
-
this.scrollView.scrollBy(Math.max(1, this.scrollView.viewportHeight));
|
|
208
|
-
this.tui.requestRender();
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
this.editor.handleInput(data);
|
|
212
398
|
if (!this.finished) this.tui.requestRender();
|
|
213
399
|
}
|
|
214
400
|
|
|
@@ -217,72 +403,72 @@ export class BtwTranscriptPager implements BtwFullscreenLayoutComponent, Focusab
|
|
|
217
403
|
}
|
|
218
404
|
|
|
219
405
|
dispose(): void {
|
|
406
|
+
releaseScrollbar(this.scrollView);
|
|
220
407
|
if (this.finished) return;
|
|
221
408
|
this.finished = true;
|
|
222
409
|
this.onAction({ kind: "close" });
|
|
223
410
|
}
|
|
224
411
|
|
|
225
|
-
private renderFooter(width: number): string {
|
|
412
|
+
private renderFooter(width: number): string[] {
|
|
413
|
+
return renderConversationFooter(width, this.theme, {
|
|
414
|
+
model: this.options.model,
|
|
415
|
+
thinkingLevel: this.thinkingLevel,
|
|
416
|
+
focus: this.workspaceFocus,
|
|
417
|
+
hint: this.renderHints(width),
|
|
418
|
+
warning: this.warning !== undefined,
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
private renderHints(width: number): string {
|
|
226
423
|
const exit = this.shortcuts.label("exit");
|
|
227
424
|
const bring = this.canBringToMain && this.shortcuts.keys.bringToMain.length > 0;
|
|
228
425
|
const bringKey = this.shortcuts.label("bringToMain");
|
|
229
|
-
if (this.warning) {
|
|
230
|
-
const warning = width < 32 ? `Empty • ${exit}` : `${this.warning} • ${exit} exit`;
|
|
231
|
-
return truncateToWidth(this.theme.fg("warning", warning), width);
|
|
232
|
-
}
|
|
233
|
-
const scrollable = this.getMaxScrollOffset() > 0;
|
|
426
|
+
if (this.warning) return fittingHint(width, [`${this.warning} • ${exit} exit`, `Empty • ${exit}`, exit]);
|
|
234
427
|
const thinking = this.options.thinking;
|
|
235
|
-
const
|
|
236
|
-
thinking && thinking.levels.length > 1 && this.
|
|
237
|
-
?
|
|
428
|
+
const cycle =
|
|
429
|
+
thinking && thinking.levels.length > 1 && this.shortcuts.keys.cycleThinkingLevel.length
|
|
430
|
+
? `${this.shortcuts.label("cycleThinkingLevel")} thinking`
|
|
238
431
|
: "";
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
const
|
|
244
|
-
const
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
private createHeaderComponent(): Component {
|
|
276
|
-
return {
|
|
277
|
-
render: (width) => [renderSideThreadHeader(width, this.theme, this.thinkingLevel)],
|
|
278
|
-
invalidate() {},
|
|
279
|
-
};
|
|
432
|
+
const importKey =
|
|
433
|
+
this.options.mainUpdates && this.shortcuts.keys.importMainUpdates.length
|
|
434
|
+
? this.shortcuts.label("importMainUpdates")
|
|
435
|
+
: "";
|
|
436
|
+
const newMainUpdates = importKey ? this.options.mainUpdates?.() : false;
|
|
437
|
+
const importHint = importKey ? `${importKey} import main${newMainUpdates ? " (new)" : ""}` : "";
|
|
438
|
+
const compactImportHint = importKey ? `${importKey} import${newMainUpdates ? " (new)" : ""}` : "";
|
|
439
|
+
const submit = submitHint(this.tui, this.keybindings, "send");
|
|
440
|
+
const history = this.getMaxScrollOffset() > 0 ? pagingHint(this.tui, this.keybindings) : "";
|
|
441
|
+
const switchPane = this.workspaceFocus?.shortcutLabel ? `${this.workspaceFocus.shortcutLabel} switch` : "";
|
|
442
|
+
const submitKey = submit.split(" ")[0] ?? "";
|
|
443
|
+
return fittingHint(width, [
|
|
444
|
+
compactHints([
|
|
445
|
+
submit,
|
|
446
|
+
bring ? `${bringKey} bring to main` : "",
|
|
447
|
+
`${exit} exit`,
|
|
448
|
+
cycle,
|
|
449
|
+
history,
|
|
450
|
+
switchPane,
|
|
451
|
+
importHint,
|
|
452
|
+
]),
|
|
453
|
+
...(compactImportHint
|
|
454
|
+
? [
|
|
455
|
+
compactHints([submit, bring ? bringKey : "", exit, cycle, history, switchPane, compactImportHint]),
|
|
456
|
+
compactHints([submitKey, exit, compactImportHint]),
|
|
457
|
+
]
|
|
458
|
+
: []),
|
|
459
|
+
compactHints([submit, bring ? bringKey : "", exit, cycle, history, switchPane]),
|
|
460
|
+
compactHints([submitKey, bring ? bringKey : "", exit, cycle, history.replace(/ history$/u, ""), switchPane]),
|
|
461
|
+
compactHints([submitKey, bring ? bringKey : "", exit, history.replace(/ history$/u, "")]),
|
|
462
|
+
compactHints([submitKey, bring ? bringKey : "", exit]),
|
|
463
|
+
compactHints([submitKey, exit]),
|
|
464
|
+
exit,
|
|
465
|
+
]);
|
|
280
466
|
}
|
|
281
467
|
|
|
282
468
|
private createTranscriptComponent(): Component {
|
|
283
469
|
return {
|
|
284
470
|
render: (width) => {
|
|
285
|
-
const lines = renderTranscriptLines(this.transcriptComponents, width);
|
|
471
|
+
const lines = renderTranscriptLines(this.transcriptComponents, width, true);
|
|
286
472
|
this.lastContentLineCount = lines.length;
|
|
287
473
|
return lines;
|
|
288
474
|
},
|
|
@@ -294,7 +480,7 @@ export class BtwTranscriptPager implements BtwFullscreenLayoutComponent, Focusab
|
|
|
294
480
|
|
|
295
481
|
private createFooterComponent(): Component {
|
|
296
482
|
return {
|
|
297
|
-
render: (width) =>
|
|
483
|
+
render: (width) => this.renderFooter(width),
|
|
298
484
|
invalidate() {},
|
|
299
485
|
};
|
|
300
486
|
}
|
|
@@ -305,11 +491,19 @@ export class BtwTranscriptPager implements BtwFullscreenLayoutComponent, Focusab
|
|
|
305
491
|
}
|
|
306
492
|
|
|
307
493
|
export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable {
|
|
494
|
+
private workspaceFocus: BtwWorkspaceFocus | undefined;
|
|
495
|
+
private get keybindings(): KeybindingsManager {
|
|
496
|
+
return getBtwKeybindings(this.tui, this.options.keybindings ?? this.options.steering?.thinking?.keybindings);
|
|
497
|
+
}
|
|
498
|
+
setWorkspaceFocus(pane: "side" | "main", shortcutLabel: string | undefined): void {
|
|
499
|
+
this.workspaceFocus = { pane, shortcutLabel: shortcutLabel ? sanitizeSingleLine(shortcutLabel) : undefined };
|
|
500
|
+
this.tui.requestRender();
|
|
501
|
+
}
|
|
308
502
|
private readonly shortcuts: BtwShortcuts;
|
|
309
503
|
private readonly pasteGuard = new BtwPasteGuard();
|
|
310
504
|
private readonly transcriptComponents: Component[];
|
|
311
|
-
private readonly loader:
|
|
312
|
-
private readonly editor:
|
|
505
|
+
private readonly loader: BtwWorkingIndicator;
|
|
506
|
+
private readonly editor: BtwComposer | undefined;
|
|
313
507
|
private readonly controller = new AbortController();
|
|
314
508
|
private readonly scrollView: ScrollView;
|
|
315
509
|
private readonly layoutRoot: VStack;
|
|
@@ -322,13 +516,13 @@ export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable
|
|
|
322
516
|
constructor(
|
|
323
517
|
private readonly tui: TUI,
|
|
324
518
|
private readonly theme: Theme,
|
|
325
|
-
turns: readonly
|
|
519
|
+
turns: readonly SideTranscriptItem[],
|
|
326
520
|
pendingQuestion: string,
|
|
327
521
|
private readonly onCancel: () => void,
|
|
328
522
|
thinkingLevel?: BtwThinkingLevel,
|
|
329
523
|
private readonly options: BtwAnsweringViewOptions = {},
|
|
330
524
|
) {
|
|
331
|
-
this.shortcuts = getBtwShortcuts(tui,
|
|
525
|
+
this.shortcuts = getBtwShortcuts(tui, this.keybindings);
|
|
332
526
|
this.transcriptComponents = buildTranscriptComponents(
|
|
333
527
|
turns,
|
|
334
528
|
this.theme,
|
|
@@ -336,7 +530,7 @@ export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable
|
|
|
336
530
|
options.markdownTransformers,
|
|
337
531
|
);
|
|
338
532
|
this.thinkingLevel = options.steering?.thinking?.level ?? thinkingLevel;
|
|
339
|
-
this.loader = new
|
|
533
|
+
this.loader = new BtwWorkingIndicator(
|
|
340
534
|
this.tui,
|
|
341
535
|
(text) => this.theme.fg("accent", text),
|
|
342
536
|
(text) => this.theme.fg("muted", text),
|
|
@@ -344,7 +538,7 @@ export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable
|
|
|
344
538
|
);
|
|
345
539
|
if (options.steering) {
|
|
346
540
|
const editorTheme: EditorTheme = {
|
|
347
|
-
borderColor: (text) => this.theme.
|
|
541
|
+
borderColor: (text) => thinkingBorder(this.theme, this.thinkingLevel, text),
|
|
348
542
|
selectList: {
|
|
349
543
|
selectedPrefix: (text) => this.theme.fg("accent", text),
|
|
350
544
|
selectedText: (text) => this.theme.fg("accent", text),
|
|
@@ -353,7 +547,7 @@ export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable
|
|
|
353
547
|
noMatch: (text) => this.theme.fg("warning", text),
|
|
354
548
|
},
|
|
355
549
|
};
|
|
356
|
-
this.editor = new
|
|
550
|
+
this.editor = new BtwComposer(this.tui, editorTheme, (data) => this.handleInput(data), this.loader);
|
|
357
551
|
this.editor.onChange = () => {
|
|
358
552
|
this.warning = undefined;
|
|
359
553
|
};
|
|
@@ -368,10 +562,14 @@ export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable
|
|
|
368
562
|
};
|
|
369
563
|
}
|
|
370
564
|
const transcript = this.createTranscriptComponent();
|
|
371
|
-
this.scrollView = new PreservingScrollView(transcript, {
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
565
|
+
this.scrollView = new PreservingScrollView(transcript, {
|
|
566
|
+
follow: "end",
|
|
567
|
+
primary: true,
|
|
568
|
+
scrollbar: "auto",
|
|
569
|
+
scrollbarTrackStyle: (text) => this.theme.fg("scrollbarTrack", text),
|
|
570
|
+
scrollbarThumbStyle: (text) => this.theme.fg("scrollbarThumb", text),
|
|
571
|
+
});
|
|
572
|
+
const dock = new VStack([
|
|
375
573
|
{
|
|
376
574
|
component: this.createSteeringComponent(),
|
|
377
575
|
basis: "auto",
|
|
@@ -379,8 +577,12 @@ export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable
|
|
|
379
577
|
minSize: 0,
|
|
380
578
|
maxSize: MAX_STEERING_DISPLAY_LINES,
|
|
381
579
|
},
|
|
382
|
-
{ component: this.
|
|
383
|
-
|
|
580
|
+
{ component: this.editor ?? this.createWorkingComponent(), basis: "auto", shrink: 1, minSize: 1 },
|
|
581
|
+
{ component: this.createFooterComponent(), basis: "auto", shrink: 1, minSize: 0 },
|
|
582
|
+
]);
|
|
583
|
+
this.layoutRoot = new VStack([
|
|
584
|
+
{ component: this.scrollView, basis: 0, grow: 1, minSize: 0 },
|
|
585
|
+
{ component: dock, basis: "auto", shrink: 1, minSize: 1 },
|
|
384
586
|
]);
|
|
385
587
|
}
|
|
386
588
|
|
|
@@ -406,27 +608,24 @@ export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable
|
|
|
406
608
|
}
|
|
407
609
|
|
|
408
610
|
render(width: number): string[] {
|
|
409
|
-
if (width <= 0) return [];
|
|
611
|
+
if (width <= 0 || this.tui.terminal.rows <= 0) return [];
|
|
410
612
|
const safeWidth = Math.max(1, width);
|
|
411
613
|
const availableRows = Math.max(1, this.tui.terminal.rows - RESERVED_APP_LINES);
|
|
412
|
-
const editorLines = this.editor?.render(safeWidth) ??
|
|
413
|
-
const
|
|
614
|
+
const editorLines = this.editor?.render(safeWidth) ?? this.renderWorking(safeWidth);
|
|
615
|
+
const contentLines = renderTranscriptLines(this.transcriptComponents, safeWidth);
|
|
616
|
+
this.lastContentLineCount = contentLines.length;
|
|
617
|
+
const footerLines = this.renderFooter(safeWidth);
|
|
618
|
+
const steeringCapacity = Math.max(0, availableRows - editorLines.length - footerLines.length);
|
|
414
619
|
const steeringLines = renderSteeringLines(
|
|
415
620
|
this.options.steering?.questions ?? [],
|
|
416
621
|
safeWidth,
|
|
417
622
|
this.theme,
|
|
418
623
|
Math.min(MAX_STEERING_DISPLAY_LINES, steeringCapacity),
|
|
419
624
|
);
|
|
420
|
-
const viewportHeight = Math.max(
|
|
421
|
-
0,
|
|
422
|
-
availableRows - editorLines.length - TRANSCRIPT_CHROME_LINES - steeringLines.length,
|
|
423
|
-
);
|
|
424
|
-
const contentLines = renderTranscriptLines(this.transcriptComponents, safeWidth);
|
|
425
|
-
this.lastContentLineCount = contentLines.length;
|
|
625
|
+
const viewportHeight = Math.max(0, availableRows - editorLines.length - footerLines.length - steeringLines.length);
|
|
426
626
|
this.scrollView.updateLayout(contentLines.length, viewportHeight, () => this.tui.requestRender());
|
|
427
627
|
|
|
428
628
|
return fitComposerLayout(
|
|
429
|
-
renderSideThreadHeader(safeWidth, this.theme, this.thinkingLevel),
|
|
430
629
|
contentLines.slice(this.scrollView.scrollTop, this.scrollView.scrollTop + viewportHeight),
|
|
431
630
|
this.renderFooter(safeWidth),
|
|
432
631
|
editorLines,
|
|
@@ -438,7 +637,7 @@ export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable
|
|
|
438
637
|
handleInput(data: string): void {
|
|
439
638
|
if (this.finished) return;
|
|
440
639
|
if (this.pasteGuard.consume(data)) {
|
|
441
|
-
this.editor?.
|
|
640
|
+
this.editor?.handleEditorInput(data);
|
|
442
641
|
this.tui.requestRender();
|
|
443
642
|
return;
|
|
444
643
|
}
|
|
@@ -461,17 +660,9 @@ export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable
|
|
|
461
660
|
}
|
|
462
661
|
return;
|
|
463
662
|
}
|
|
464
|
-
if (
|
|
465
|
-
this.
|
|
466
|
-
this.tui.requestRender();
|
|
467
|
-
return;
|
|
663
|
+
if (!handleFallbackPaging(data, this.tui, this.keybindings, this.scrollView)) {
|
|
664
|
+
this.editor?.handleEditorInput(data);
|
|
468
665
|
}
|
|
469
|
-
if (matchesKey(data, Key.pageDown)) {
|
|
470
|
-
this.scrollView.scrollBy(Math.max(1, this.scrollView.viewportHeight));
|
|
471
|
-
this.tui.requestRender();
|
|
472
|
-
return;
|
|
473
|
-
}
|
|
474
|
-
this.editor?.handleInput(data);
|
|
475
666
|
this.tui.requestRender();
|
|
476
667
|
}
|
|
477
668
|
|
|
@@ -481,10 +672,12 @@ export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable
|
|
|
481
672
|
|
|
482
673
|
finish(): void {
|
|
483
674
|
this.finished = true;
|
|
675
|
+
releaseScrollbar(this.scrollView);
|
|
484
676
|
this.loader.stop();
|
|
485
677
|
}
|
|
486
678
|
|
|
487
679
|
dispose(): void {
|
|
680
|
+
releaseScrollbar(this.scrollView);
|
|
488
681
|
if (this.finished) {
|
|
489
682
|
this.loader.stop();
|
|
490
683
|
this.controller.abort();
|
|
@@ -496,38 +689,50 @@ export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable
|
|
|
496
689
|
this.onCancel();
|
|
497
690
|
}
|
|
498
691
|
|
|
499
|
-
private renderFooter(width: number): string {
|
|
692
|
+
private renderFooter(width: number): string[] {
|
|
693
|
+
return renderConversationFooter(width, this.theme, {
|
|
694
|
+
model: this.options.model,
|
|
695
|
+
thinkingLevel: this.thinkingLevel,
|
|
696
|
+
focus: this.workspaceFocus,
|
|
697
|
+
hint: this.renderHints(width),
|
|
698
|
+
warning: this.warning !== undefined,
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
private renderHints(width: number): string {
|
|
500
703
|
const exit = this.shortcuts.label("exit");
|
|
501
|
-
if (this.warning) {
|
|
502
|
-
|
|
503
|
-
return truncateToWidth(this.theme.fg("warning", warning), width);
|
|
504
|
-
}
|
|
505
|
-
const baseHint = this.editor ? `Enter steer • ${exit} cancel` : `${exit} cancel`;
|
|
704
|
+
if (this.warning) return fittingHint(width, [`${this.warning} • ${exit} cancel`, `Empty • ${exit}`, exit]);
|
|
705
|
+
const submit = this.editor ? submitHint(this.tui, this.keybindings, "queue next") : "";
|
|
506
706
|
const thinking = this.options.steering?.thinking;
|
|
507
|
-
const
|
|
508
|
-
thinking && thinking.levels.length > 1 && this.
|
|
509
|
-
?
|
|
707
|
+
const cycle =
|
|
708
|
+
thinking && thinking.levels.length > 1 && this.shortcuts.keys.cycleThinkingLevel.length
|
|
709
|
+
? `${this.shortcuts.label("cycleThinkingLevel")} thinking`
|
|
510
710
|
: "";
|
|
511
|
-
const
|
|
512
|
-
const
|
|
513
|
-
const
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
711
|
+
const history = this.getMaxScrollOffset() > 0 ? pagingHint(this.tui, this.keybindings) : "";
|
|
712
|
+
const switchPane = this.workspaceFocus?.shortcutLabel ? `${this.workspaceFocus.shortcutLabel} switch` : "";
|
|
713
|
+
const submitKey = submit.split(" ")[0] ?? "";
|
|
714
|
+
return fittingHint(width, [
|
|
715
|
+
compactHints([submit, `${exit} cancel`, cycle, history, switchPane]),
|
|
716
|
+
compactHints([submit, exit, cycle, history, switchPane]),
|
|
717
|
+
compactHints([submitKey, exit, cycle, history.replace(/ history$/u, ""), switchPane]),
|
|
718
|
+
compactHints([submitKey, exit, history.replace(/ history$/u, "")]),
|
|
719
|
+
compactHints([submitKey, exit]),
|
|
720
|
+
exit,
|
|
721
|
+
]);
|
|
518
722
|
}
|
|
519
723
|
|
|
520
|
-
private
|
|
521
|
-
return
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
724
|
+
private renderWorking(width: number): string[] {
|
|
725
|
+
return [this.loader.renderInBorder(Math.max(0, width))];
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
private createWorkingComponent(): Component {
|
|
729
|
+
return { render: (width) => this.renderWorking(width), invalidate: () => this.loader.invalidate() };
|
|
525
730
|
}
|
|
526
731
|
|
|
527
732
|
private createTranscriptComponent(): Component {
|
|
528
733
|
return {
|
|
529
734
|
render: (width) => {
|
|
530
|
-
const lines = renderTranscriptLines(this.transcriptComponents, width);
|
|
735
|
+
const lines = renderTranscriptLines(this.transcriptComponents, width, true);
|
|
531
736
|
this.lastContentLineCount = lines.length;
|
|
532
737
|
return lines;
|
|
533
738
|
},
|
|
@@ -547,7 +752,7 @@ export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable
|
|
|
547
752
|
|
|
548
753
|
private createFooterComponent(): Component {
|
|
549
754
|
return {
|
|
550
|
-
render: (width) =>
|
|
755
|
+
render: (width) => this.renderFooter(width),
|
|
551
756
|
invalidate: () => this.loader.invalidate(),
|
|
552
757
|
};
|
|
553
758
|
}
|
|
@@ -557,9 +762,10 @@ export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable
|
|
|
557
762
|
}
|
|
558
763
|
}
|
|
559
764
|
|
|
560
|
-
export function formatSideTranscript(turns: readonly
|
|
765
|
+
export function formatSideTranscript(turns: readonly SideTranscriptItem[]): string {
|
|
561
766
|
return turns
|
|
562
767
|
.map((turn) => {
|
|
768
|
+
if (turn.kind === "mainContext") return `Main context update\n\n${escapeTerminalControls(turn.snapshot.text)}`;
|
|
563
769
|
const question = escapeTerminalControls(turn.question);
|
|
564
770
|
const rawAnswer = escapeTerminalControls(turn.answer);
|
|
565
771
|
const answer = turn.kind === "error" ? `Error: ${rawAnswer}` : rawAnswer;
|
|
@@ -569,12 +775,27 @@ export function formatSideTranscript(turns: readonly SideThreadTurn[]): string {
|
|
|
569
775
|
}
|
|
570
776
|
|
|
571
777
|
function buildTranscriptComponents(
|
|
572
|
-
turns: readonly
|
|
778
|
+
turns: readonly SideTranscriptItem[],
|
|
573
779
|
theme: Theme,
|
|
574
780
|
pendingQuestion?: string,
|
|
575
781
|
markdownTransformers: readonly MarkdownTransformer[] = [],
|
|
576
782
|
): Component[] {
|
|
577
|
-
const components = turns.flatMap((turn): Component[] => {
|
|
783
|
+
const components = turns.flatMap((turn, index): Component[] => {
|
|
784
|
+
// Pi inserts a spacer before a later user message, outside that message's
|
|
785
|
+
// own Markdown padding. Keep the same separation between side exchanges.
|
|
786
|
+
const preceding = index > 0 ? [new Spacer(1)] : [];
|
|
787
|
+
if (turn.kind === "mainContext") {
|
|
788
|
+
const update: Component = {
|
|
789
|
+
render: (width) =>
|
|
790
|
+
new Text(
|
|
791
|
+
theme.fg("muted", `Main context update\n\n${escapeTerminalControls(turn.snapshot.text)}`),
|
|
792
|
+
1,
|
|
793
|
+
0,
|
|
794
|
+
).render(width),
|
|
795
|
+
invalidate() {},
|
|
796
|
+
};
|
|
797
|
+
return [...preceding, update];
|
|
798
|
+
}
|
|
578
799
|
const question = new UserMessageComponent(
|
|
579
800
|
escapeTerminalControls(turn.question),
|
|
580
801
|
getMarkdownTheme(),
|
|
@@ -582,10 +803,12 @@ function buildTranscriptComponents(
|
|
|
582
803
|
markdownTransformers,
|
|
583
804
|
);
|
|
584
805
|
if (turn.kind === "error") {
|
|
585
|
-
const error
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
806
|
+
const error: Component = {
|
|
807
|
+
render: (width) =>
|
|
808
|
+
new Text(theme.fg("error", `Error: ${escapeTerminalControls(turn.answer)}`), 1, 0).render(width),
|
|
809
|
+
invalidate() {},
|
|
810
|
+
};
|
|
811
|
+
return [...preceding, question, new Spacer(1), error];
|
|
589
812
|
}
|
|
590
813
|
const response: AssistantMessage = {
|
|
591
814
|
...turn.response,
|
|
@@ -593,9 +816,14 @@ function buildTranscriptComponents(
|
|
|
593
816
|
stopReason: "stop",
|
|
594
817
|
errorMessage: undefined,
|
|
595
818
|
};
|
|
596
|
-
return [
|
|
819
|
+
return [
|
|
820
|
+
...preceding,
|
|
821
|
+
question,
|
|
822
|
+
new AssistantMessageComponent(response, true, getMarkdownTheme(), "", 1, markdownTransformers),
|
|
823
|
+
];
|
|
597
824
|
});
|
|
598
825
|
if (pendingQuestion) {
|
|
826
|
+
if (components.length > 0) components.push(new Spacer(1));
|
|
599
827
|
components.push(
|
|
600
828
|
new UserMessageComponent(escapeTerminalControls(pendingQuestion), getMarkdownTheme(), 1, markdownTransformers),
|
|
601
829
|
);
|
|
@@ -603,30 +831,44 @@ function buildTranscriptComponents(
|
|
|
603
831
|
return components;
|
|
604
832
|
}
|
|
605
833
|
|
|
606
|
-
function renderTranscriptLines(components: readonly Component[], width: number): string[] {
|
|
607
|
-
return components.flatMap((component) =>
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
834
|
+
function renderTranscriptLines(components: readonly Component[], width: number, native = false): string[] {
|
|
835
|
+
return components.flatMap((component) => {
|
|
836
|
+
const lines = component.render(width);
|
|
837
|
+
// Pi adds these exact prefixes outside Markdown. Never trust payload/transformer
|
|
838
|
+
// markers, even when they happen to use the same OSC number.
|
|
839
|
+
const trusted = component instanceof UserMessageComponent || component instanceof AssistantMessageComponent;
|
|
840
|
+
return lines.map((line, index) => {
|
|
841
|
+
let prefix = "";
|
|
842
|
+
if (native && trusted) {
|
|
843
|
+
if (index === 0 && line.startsWith(OSC133_MARKERS[0] ?? "")) prefix += OSC133_MARKERS[0];
|
|
844
|
+
const end = `${OSC133_MARKERS[1]}${OSC133_MARKERS[2]}`;
|
|
845
|
+
if (index === lines.length - 1 && line.slice(prefix.length).startsWith(end)) prefix += end;
|
|
846
|
+
}
|
|
847
|
+
return prefix + truncateToWidth(stripShellIntegrationMarkers(line), Math.max(0, width), "");
|
|
848
|
+
});
|
|
849
|
+
});
|
|
615
850
|
}
|
|
616
851
|
|
|
617
852
|
function fitComposerLayout(
|
|
618
|
-
header: string,
|
|
619
853
|
contentLines: string[],
|
|
620
|
-
|
|
854
|
+
footerLines: string[],
|
|
621
855
|
editorLines: string[],
|
|
622
856
|
availableRows: number,
|
|
623
857
|
statusLines: string[] = [],
|
|
624
858
|
): string[] {
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
const editorBudget = Math.
|
|
629
|
-
|
|
859
|
+
if (availableRows <= 0) return [];
|
|
860
|
+
// Keep the active input first in the row budget, while retaining native
|
|
861
|
+
// conversation order: transcript, pending questions, editor, then footer.
|
|
862
|
+
const editorBudget = Math.min(editorLines.length, availableRows);
|
|
863
|
+
const footerBudget = Math.min(footerLines.length, availableRows - editorBudget);
|
|
864
|
+
const statusBudget = Math.min(statusLines.length, availableRows - editorBudget - footerBudget);
|
|
865
|
+
const transcriptBudget = availableRows - editorBudget - footerBudget - statusBudget;
|
|
866
|
+
return [
|
|
867
|
+
...contentLines.slice(0, transcriptBudget),
|
|
868
|
+
...statusLines.slice(0, statusBudget),
|
|
869
|
+
...fitEditorLines(editorLines, editorBudget),
|
|
870
|
+
...footerLines.slice(0, footerBudget),
|
|
871
|
+
];
|
|
630
872
|
}
|
|
631
873
|
|
|
632
874
|
function fitEditorLines(editorLines: string[], budget: number): string[] {
|
|
@@ -644,7 +886,7 @@ function renderSteeringLines(questions: readonly string[], width: number, theme:
|
|
|
644
886
|
if (maxLines === 1 && questions.length > 1) {
|
|
645
887
|
return [
|
|
646
888
|
truncateToWidth(
|
|
647
|
-
theme.fg("dim", `Steering (+${questions.length - 1} more): ${formatQuestion(questions[0] ?? "")}`),
|
|
889
|
+
theme.fg("dim", ` Steering next (+${questions.length - 1} more): ${formatQuestion(questions[0] ?? "")}`),
|
|
648
890
|
width,
|
|
649
891
|
),
|
|
650
892
|
];
|
|
@@ -653,15 +895,16 @@ function renderSteeringLines(questions: readonly string[], width: number, theme:
|
|
|
653
895
|
const questionLimit = hasOverflow ? Math.max(1, maxLines - 1) : maxLines;
|
|
654
896
|
const lines = questions
|
|
655
897
|
.slice(0, questionLimit)
|
|
656
|
-
.map((question) => truncateToWidth(theme.fg("dim", `Steering: ${formatQuestion(question)}`), width));
|
|
898
|
+
.map((question) => truncateToWidth(theme.fg("dim", ` Steering next: ${formatQuestion(question)}`), width));
|
|
657
899
|
if (hasOverflow) {
|
|
658
|
-
lines.push(truncateToWidth(theme.fg("dim", `Steering: … +${questions.length - questionLimit} more`), width));
|
|
900
|
+
lines.push(truncateToWidth(theme.fg("dim", ` Steering next: … +${questions.length - questionLimit} more`), width));
|
|
659
901
|
}
|
|
660
902
|
return lines;
|
|
661
903
|
}
|
|
662
904
|
|
|
663
905
|
function stripShellIntegrationMarkers(line: string): string {
|
|
664
|
-
|
|
906
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: remove the exact OSC terminal protocol, not printable text.
|
|
907
|
+
return line.replace(/\u001b\]133;[^\u0007\u001b]*(?:\u0007|\u001b\\)/gu, "");
|
|
665
908
|
}
|
|
666
909
|
|
|
667
910
|
function escapeTerminalControls(text: string): string {
|