@narumitw/pi-btw 0.53.0 → 0.54.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 CHANGED
@@ -96,10 +96,12 @@ Type another question and press `Enter` to queue it as `Steering`; queued questi
96
96
  submission order and answered one at a time after the active response completes.
97
97
  A queued question uses the side thread's thinking level when its turn begins.
98
98
  A failed active response is shown in the transcript and does not discard later steering questions.
99
- The footer shows `PgUp`/`PgDn` only when history can scroll; press `Ctrl+C` to cancel the active
100
- response and discard the ephemeral side-thread draft and steering queue. Completed questions,
101
- answers, and visible errors remain available through Resume until the current extension instance
102
- ends. Steering remains entirely inside pi-btw and never appends to the main conversation or editor.
99
+ Use the mouse wheel or trackpad to scroll transcript history like Pi's main thread.
100
+ Keyboard `PgUp`/`PgDn` history navigation remains available.
101
+ It appears in the footer only when the transcript can scroll.
102
+ Press `Ctrl+C` to cancel the active response and discard the ephemeral side-thread draft and steering queue.
103
+ Completed questions, answers, and visible errors remain available through Resume until the current extension instance ends.
104
+ Steering remains entirely inside pi-btw and never appends to the main conversation or editor.
103
105
 
104
106
  After at least one successful answer, press `Ctrl+R` to bring selected context to the main
105
107
  editor. The scope menu shows the size of the latest question and answer and the entire side
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narumitw/pi-btw",
3
- "version": "0.53.0",
3
+ "version": "0.54.0",
4
4
  "description": "Pi extension that adds a /btw side-question command.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -21,7 +21,14 @@ type BtwCustomFactory<T> = (
21
21
  done: (result: T) => void,
22
22
  ) => (Component & { dispose?(): void }) | Promise<Component & { dispose?(): void }>;
23
23
 
24
- type BtwFullscreenTui = TUI & { flash?: (message: string, durationMs?: number) => void };
24
+ type BtwFullscreenTui = TUI & {
25
+ flash?: (message: string, durationMs?: number) => void;
26
+ setLayoutRoot(component: Component | undefined): void;
27
+ };
28
+
29
+ export interface BtwFullscreenLayoutComponent extends Component {
30
+ getFullscreenLayout(): Component;
31
+ }
25
32
 
26
33
  export type BtwFullscreenTuiFactory = (parent: TUI) => BtwFullscreenTui;
27
34
 
@@ -223,6 +230,7 @@ class BtwFullscreenHost<T> implements Component {
223
230
  let component: (Component & { dispose?(): void }) | undefined;
224
231
  let overlay: OverlayHandle | undefined;
225
232
  let mounted = false;
233
+ let layoutMounted = false;
226
234
  let factorySettled = false;
227
235
  let closed = false;
228
236
  let promiseSettled = false;
@@ -243,6 +251,7 @@ class BtwFullscreenHost<T> implements Component {
243
251
  let cleanupError: unknown;
244
252
  try {
245
253
  if (overlay) overlay.hide();
254
+ else if (mounted && layoutMounted) fullscreen.setLayoutRoot(undefined);
246
255
  else if (mounted && component) fullscreen.removeChild(component);
247
256
  } catch (error) {
248
257
  cleanupError = error;
@@ -327,8 +336,13 @@ class BtwFullscreenHost<T> implements Component {
327
336
  options.onHandle?.(overlay);
328
337
  } else {
329
338
  fullscreen.clear();
330
- fullscreen.addChild(component);
331
339
  mounted = true;
340
+ if (isFullscreenLayoutComponent(component)) {
341
+ layoutMounted = true;
342
+ fullscreen.setLayoutRoot(component.getFullscreenLayout());
343
+ } else {
344
+ fullscreen.addChild(component);
345
+ }
332
346
  fullscreen.setFocus(component);
333
347
  fullscreen.requestRender();
334
348
  }
@@ -337,3 +351,9 @@ class BtwFullscreenHost<T> implements Component {
337
351
  });
338
352
  }
339
353
  }
354
+
355
+ function isFullscreenLayoutComponent(
356
+ component: Component,
357
+ ): component is BtwFullscreenLayoutComponent {
358
+ return "getFullscreenLayout" in component && typeof component.getFullscreenLayout === "function";
359
+ }
@@ -16,10 +16,13 @@ import {
16
16
  Loader,
17
17
  Markdown,
18
18
  matchesKey,
19
+ ScrollView,
19
20
  type TUI,
20
21
  truncateToWidth,
22
+ VStack,
21
23
  visibleWidth,
22
24
  } from "@earendil-works/pi-tui";
25
+ import type { BtwFullscreenLayoutComponent } from "./fullscreen-ui.js";
23
26
  import type { BtwThinkingLevel, SideThreadTurn } from "./side-thread.js";
24
27
  import { sanitizeSingleLine } from "./text.js";
25
28
 
@@ -29,6 +32,21 @@ const OSC133_MARKERS = ["\u001b]133;A\u0007", "\u001b]133;B\u0007", "\u001b]133;
29
32
  // Pi renders a spacer above the custom component and a two-line built-in footer below it.
30
33
  const RESERVED_APP_LINES = 3;
31
34
 
35
+ // A temporary fit after manual scrolling must not silently resume following new output.
36
+ class PreservingScrollView extends ScrollView {
37
+ override updateLayout(
38
+ contentHeight: number,
39
+ viewportHeight: number,
40
+ requestRender: () => void,
41
+ ): void {
42
+ const preserveManualPosition = !this.isFollowingEnd;
43
+ super.updateLayout(contentHeight, viewportHeight, requestRender);
44
+ if (preserveManualPosition && this.isFollowingEnd) {
45
+ this.scrollTo(this.scrollTop, { disableFollow: true });
46
+ }
47
+ }
48
+ }
49
+
32
50
  export type TranscriptPagerAction =
33
51
  | { kind: "submit"; question: string }
34
52
  | { kind: "bringToMain"; questionDraft: string }
@@ -49,14 +67,13 @@ export interface BtwAnsweringViewOptions {
49
67
  };
50
68
  }
51
69
 
52
- export class BtwTranscriptPager implements Component, Focusable {
70
+ export class BtwTranscriptPager implements BtwFullscreenLayoutComponent, Focusable {
53
71
  private readonly transcriptComponents: Component[];
54
72
  private readonly editor: Editor;
55
73
  private readonly canBringToMain: boolean;
56
- private scrollOffset = 0;
74
+ private readonly scrollView: ScrollView;
75
+ private readonly layoutRoot: VStack;
57
76
  private lastContentLineCount = 0;
58
- private lastViewportHeight = 1;
59
- private followBottom: boolean;
60
77
  private warning: string | undefined;
61
78
  private finished = false;
62
79
  private isFocused = false;
@@ -75,7 +92,6 @@ export class BtwTranscriptPager implements Component, Focusable {
75
92
  ) {
76
93
  this.transcriptComponents = buildTranscriptComponents(turns, this.theme);
77
94
  this.canBringToMain = turns.some((turn) => turn.kind === "answered");
78
- this.followBottom = options.startAtBottom ?? false;
79
95
  this.thinkingLevel = options.thinking?.level;
80
96
  const editorTheme: EditorTheme = {
81
97
  borderColor: (text) => this.theme.fg("accent", text),
@@ -101,6 +117,17 @@ export class BtwTranscriptPager implements Component, Focusable {
101
117
  this.finished = true;
102
118
  this.onAction({ kind: "submit", question });
103
119
  };
120
+ const transcript = this.createTranscriptComponent();
121
+ this.scrollView = new PreservingScrollView(transcript, {
122
+ follow: options.startAtBottom ? "end" : "none",
123
+ primary: true,
124
+ });
125
+ this.layoutRoot = new VStack([
126
+ { component: this.createHeaderComponent(), basis: 1, shrink: 0, minSize: 1 },
127
+ { component: this.scrollView, basis: 0, grow: 1, minSize: 0 },
128
+ { component: this.createFooterComponent(), basis: 1, shrink: 0, minSize: 1 },
129
+ { component: this.editor, basis: "auto", shrink: 1, minSize: 0 },
130
+ ]);
104
131
  }
105
132
 
106
133
  get focused(): boolean {
@@ -112,6 +139,10 @@ export class BtwTranscriptPager implements Component, Focusable {
112
139
  this.editor.focused = value;
113
140
  }
114
141
 
142
+ getFullscreenLayout(): Component {
143
+ return this.layoutRoot;
144
+ }
145
+
115
146
  render(width: number): string[] {
116
147
  const safeWidth = Math.max(1, width);
117
148
  const editorLines = this.editor.render(safeWidth);
@@ -122,13 +153,13 @@ export class BtwTranscriptPager implements Component, Focusable {
122
153
  );
123
154
  const contentLines = renderTranscriptLines(this.transcriptComponents, safeWidth);
124
155
  this.lastContentLineCount = contentLines.length;
125
- this.lastViewportHeight = viewportHeight;
126
- if (this.followBottom) this.scrollOffset = this.getMaxScrollOffset();
127
- this.clampScrollOffset();
156
+ this.scrollView.updateLayout(contentLines.length, viewportHeight, () =>
157
+ this.tui.requestRender(),
158
+ );
128
159
 
129
160
  return fitComposerLayout(
130
161
  renderSideThreadHeader(safeWidth, this.theme, this.thinkingLevel),
131
- contentLines.slice(this.scrollOffset, this.scrollOffset + viewportHeight),
162
+ contentLines.slice(this.scrollView.scrollTop, this.scrollView.scrollTop + viewportHeight),
132
163
  this.renderFooter(safeWidth),
133
164
  editorLines,
134
165
  availableRows,
@@ -164,15 +195,12 @@ export class BtwTranscriptPager implements Component, Focusable {
164
195
  return;
165
196
  }
166
197
  if (matchesKey(data, Key.pageUp)) {
167
- const previousOffset = this.scrollOffset;
168
- this.scrollBy(-this.lastViewportHeight);
169
- if (this.scrollOffset < previousOffset) this.followBottom = false;
198
+ this.scrollView.scrollBy(-Math.max(1, this.scrollView.viewportHeight));
170
199
  this.tui.requestRender();
171
200
  return;
172
201
  }
173
202
  if (matchesKey(data, Key.pageDown)) {
174
- this.scrollBy(this.lastViewportHeight);
175
- this.followBottom = this.scrollOffset >= this.getMaxScrollOffset();
203
+ this.scrollView.scrollBy(Math.max(1, this.scrollView.viewportHeight));
176
204
  this.tui.requestRender();
177
205
  return;
178
206
  }
@@ -181,8 +209,7 @@ export class BtwTranscriptPager implements Component, Focusable {
181
209
  }
182
210
 
183
211
  invalidate(): void {
184
- for (const component of this.transcriptComponents) component.invalidate();
185
- this.editor.invalidate();
212
+ this.layoutRoot.invalidate();
186
213
  }
187
214
 
188
215
  dispose(): void {
@@ -218,7 +245,7 @@ export class BtwTranscriptPager implements Component, Focusable {
218
245
  ? compactBase
219
246
  : fallbackBase;
220
247
  if (scrollable) {
221
- const history = ` • ${this.scrollOffset > 0 ? "↑ older" : "↓ newer"} • PgUp/PgDn history`;
248
+ const history = ` • ${this.scrollView.scrollTop > 0 ? "↑ older" : "↓ newer"} • PgUp/PgDn history`;
222
249
  const compactHistory = " • PgUp/PgDn";
223
250
  const compactScrollable = this.canBringToMain
224
251
  ? "Enter • Ctrl+R • Ctrl+C • PgUp/PgDn"
@@ -238,29 +265,46 @@ export class BtwTranscriptPager implements Component, Focusable {
238
265
  return truncateToWidth(this.theme.fg("muted", hints), width);
239
266
  }
240
267
 
241
- private scrollBy(delta: number): void {
242
- this.scrollOffset += delta;
243
- this.clampScrollOffset();
268
+ private createHeaderComponent(): Component {
269
+ return {
270
+ render: (width) => [renderSideThreadHeader(width, this.theme, this.thinkingLevel)],
271
+ invalidate() {},
272
+ };
244
273
  }
245
274
 
246
- private clampScrollOffset(): void {
247
- this.scrollOffset = Math.max(0, Math.min(this.scrollOffset, this.getMaxScrollOffset()));
275
+ private createTranscriptComponent(): Component {
276
+ return {
277
+ render: (width) => {
278
+ const lines = renderTranscriptLines(this.transcriptComponents, width);
279
+ this.lastContentLineCount = lines.length;
280
+ return lines;
281
+ },
282
+ invalidate: () => {
283
+ for (const component of this.transcriptComponents) component.invalidate();
284
+ },
285
+ };
286
+ }
287
+
288
+ private createFooterComponent(): Component {
289
+ return {
290
+ render: (width) => [this.renderFooter(width)],
291
+ invalidate() {},
292
+ };
248
293
  }
249
294
 
250
295
  private getMaxScrollOffset(): number {
251
- return Math.max(0, this.lastContentLineCount - this.lastViewportHeight);
296
+ return Math.max(0, this.lastContentLineCount - this.scrollView.viewportHeight);
252
297
  }
253
298
  }
254
299
 
255
- export class BtwAnsweringView implements Component, Focusable {
300
+ export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable {
256
301
  private readonly transcriptComponents: Component[];
257
302
  private readonly loader: Loader;
258
303
  private readonly editor: Editor | undefined;
259
304
  private readonly controller = new AbortController();
260
- private scrollOffset = 0;
305
+ private readonly scrollView: ScrollView;
306
+ private readonly layoutRoot: VStack;
261
307
  private lastContentLineCount = 0;
262
- private lastViewportHeight = 1;
263
- private followBottom = true;
264
308
  private warning: string | undefined;
265
309
  private finished = false;
266
310
  private isFocused = false;
@@ -308,6 +352,23 @@ export class BtwAnsweringView implements Component, Focusable {
308
352
  this.warning = undefined;
309
353
  };
310
354
  }
355
+ const transcript = this.createTranscriptComponent();
356
+ this.scrollView = new PreservingScrollView(transcript, { follow: "end", primary: true });
357
+ this.layoutRoot = new VStack([
358
+ { component: this.createHeaderComponent(), basis: 1, shrink: 0, minSize: 1 },
359
+ { component: this.scrollView, basis: 0, grow: 1, minSize: 0 },
360
+ {
361
+ component: this.createSteeringComponent(),
362
+ basis: "auto",
363
+ shrink: 1,
364
+ minSize: 0,
365
+ maxSize: MAX_STEERING_DISPLAY_LINES,
366
+ },
367
+ { component: this.createFooterComponent(), basis: 1, shrink: 0, minSize: 1 },
368
+ ...(this.editor
369
+ ? [{ component: this.editor, basis: "auto" as const, shrink: 1, minSize: 0 }]
370
+ : []),
371
+ ]);
311
372
  }
312
373
 
313
374
  get focused(): boolean {
@@ -323,6 +384,10 @@ export class BtwAnsweringView implements Component, Focusable {
323
384
  return this.controller.signal;
324
385
  }
325
386
 
387
+ getFullscreenLayout(): Component {
388
+ return this.layoutRoot;
389
+ }
390
+
326
391
  render(width: number): string[] {
327
392
  const safeWidth = Math.max(1, width);
328
393
  const availableRows = Math.max(1, this.tui.terminal.rows - RESERVED_APP_LINES);
@@ -343,13 +408,13 @@ export class BtwAnsweringView implements Component, Focusable {
343
408
  );
344
409
  const contentLines = renderTranscriptLines(this.transcriptComponents, safeWidth);
345
410
  this.lastContentLineCount = contentLines.length;
346
- this.lastViewportHeight = viewportHeight;
347
- if (this.followBottom) this.scrollOffset = this.getMaxScrollOffset();
348
- this.clampScrollOffset();
411
+ this.scrollView.updateLayout(contentLines.length, viewportHeight, () =>
412
+ this.tui.requestRender(),
413
+ );
349
414
 
350
415
  return fitComposerLayout(
351
416
  renderSideThreadHeader(safeWidth, this.theme, this.thinkingLevel),
352
- contentLines.slice(this.scrollOffset, this.scrollOffset + viewportHeight),
417
+ contentLines.slice(this.scrollView.scrollTop, this.scrollView.scrollTop + viewportHeight),
353
418
  this.renderFooter(safeWidth),
354
419
  editorLines,
355
420
  availableRows,
@@ -383,15 +448,12 @@ export class BtwAnsweringView implements Component, Focusable {
383
448
  return;
384
449
  }
385
450
  if (matchesKey(data, Key.pageUp)) {
386
- const previousOffset = this.scrollOffset;
387
- this.scrollBy(-this.lastViewportHeight);
388
- if (this.scrollOffset < previousOffset) this.followBottom = false;
451
+ this.scrollView.scrollBy(-Math.max(1, this.scrollView.viewportHeight));
389
452
  this.tui.requestRender();
390
453
  return;
391
454
  }
392
455
  if (matchesKey(data, Key.pageDown)) {
393
- this.scrollBy(this.lastViewportHeight);
394
- this.followBottom = this.scrollOffset >= this.getMaxScrollOffset();
456
+ this.scrollView.scrollBy(Math.max(1, this.scrollView.viewportHeight));
395
457
  this.tui.requestRender();
396
458
  return;
397
459
  }
@@ -400,9 +462,7 @@ export class BtwAnsweringView implements Component, Focusable {
400
462
  }
401
463
 
402
464
  invalidate(): void {
403
- for (const component of this.transcriptComponents) component.invalidate();
404
- this.loader.invalidate();
405
- this.editor?.invalidate();
465
+ this.layoutRoot.invalidate();
406
466
  }
407
467
 
408
468
  finish(): void {
@@ -442,17 +502,48 @@ export class BtwAnsweringView implements Component, Focusable {
442
502
  return truncateToWidth(`${loaderLine} • ${this.theme.fg("muted", selectedHints)}`, width);
443
503
  }
444
504
 
445
- private scrollBy(delta: number): void {
446
- this.scrollOffset += delta;
447
- this.clampScrollOffset();
505
+ private createHeaderComponent(): Component {
506
+ return {
507
+ render: (width) => [renderSideThreadHeader(width, this.theme, this.thinkingLevel)],
508
+ invalidate() {},
509
+ };
510
+ }
511
+
512
+ private createTranscriptComponent(): Component {
513
+ return {
514
+ render: (width) => {
515
+ const lines = renderTranscriptLines(this.transcriptComponents, width);
516
+ this.lastContentLineCount = lines.length;
517
+ return lines;
518
+ },
519
+ invalidate: () => {
520
+ for (const component of this.transcriptComponents) component.invalidate();
521
+ },
522
+ };
523
+ }
524
+
525
+ private createSteeringComponent(): Component {
526
+ return {
527
+ render: (width) =>
528
+ renderSteeringLines(
529
+ this.options.steering?.questions ?? [],
530
+ width,
531
+ this.theme,
532
+ MAX_STEERING_DISPLAY_LINES,
533
+ ),
534
+ invalidate() {},
535
+ };
448
536
  }
449
537
 
450
- private clampScrollOffset(): void {
451
- this.scrollOffset = Math.max(0, Math.min(this.scrollOffset, this.getMaxScrollOffset()));
538
+ private createFooterComponent(): Component {
539
+ return {
540
+ render: (width) => [this.renderFooter(width)],
541
+ invalidate: () => this.loader.invalidate(),
542
+ };
452
543
  }
453
544
 
454
545
  private getMaxScrollOffset(): number {
455
- return Math.max(0, this.lastContentLineCount - this.lastViewportHeight);
546
+ return Math.max(0, this.lastContentLineCount - this.scrollView.viewportHeight);
456
547
  }
457
548
  }
458
549