pi-stereo-view 0.1.22 → 0.1.24

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/src/layout.ts CHANGED
@@ -2,16 +2,18 @@ import { setImmediate } from "node:timers/promises";
2
2
  import type { Theme, ToolInfo } from "@earendil-works/pi-coding-agent";
3
3
  import {
4
4
  Container, HStack, isViewportTUI, mixColors, MouseRegion, rgbColor, ScrollView, Text, truncateToWidth, visibleWidth, VStack,
5
- type Color, type Component, type ScrollViewOptions, type StackChild, type TUI, type TuiMouseEvent,
5
+ type Color, type Component, type RgbColor, type ScrollViewOptions, type StackChild, type TUI, type TuiMouseEvent,
6
6
  } from "@earendil-works/pi-tui";
7
- import { NativeDocument, type Pane } from "./native.ts";
7
+ import { NativeDocument, type DocumentTurns, type Pane } from "./native.ts";
8
+ import { parseHex, turnColor, pointerMotionAvailable, type TurnMix } from "./turns.ts";
8
9
  import { NativeContentIndex } from "./content.ts";
9
10
  import { NestedCalls } from "./nested-calls.ts";
10
- import type { Preferences } from "./preferences.ts";
11
+ import type { TurnMarkSetting, Preferences } from "./preferences.ts";
11
12
  import type { TranscriptTimestamps } from "./timestamps.ts";
12
13
  import { PANEL_SEARCH_ROWS, PanelSearch, PanelSearchShortcut, type SearchHit } from "./search.ts";
13
14
  import { SplitSurface } from "./surface.ts";
14
15
  import { sameValues } from "./render-cache.ts";
16
+ import type { Problem } from "./report.ts";
15
17
 
16
18
  type NativeTui = TUI & {
17
19
  layoutRoot?: Container;
@@ -238,6 +240,14 @@ export class TranscriptLayout {
238
240
  private automaticHeight = MIN_ROWS;
239
241
  private readonly rejectedRoots = new WeakSet<Component>();
240
242
  private rebuild = (): void => {};
243
+ private index?: NativeContentIndex;
244
+ /** The turn under the pointer; a pointer move collects the turn its document reports. */
245
+ private hoverAct?: number;
246
+ private moving?: { turn: number };
247
+ /** The terminal's reported background, which turn colors are mixed into. */
248
+ private terminalBackground?: RgbColor;
249
+ /** Set by `markTerminalStarted()`; terminal queries wait for it. */
250
+ private terminalStarted = false;
241
251
 
242
252
  constructor(
243
253
  readonly preferences: Preferences,
@@ -250,8 +260,32 @@ export class TranscriptLayout {
250
260
  private readonly getTools: () => readonly ToolInfo[] = () => [],
251
261
  private readonly nested = new NestedCalls(),
252
262
  private readonly getEditor: () => Component | undefined = () => undefined,
263
+ /** Show a problem that stops the split, with a way to report it. */
264
+ private readonly report: (problem: Problem) => void = problem => this.warn("pi-stereo-view " + problem.summary +
265
+ (problem.error === undefined ? "" : ": " + (problem.error instanceof Error ? problem.error.message : String(problem.error)))),
253
266
  ) {}
254
267
 
268
+ /** Refuse a Pi layout the split cannot use; Pi keeps its own history view. */
269
+ private refuse(reason: string): void {
270
+ this.report({ summary: "split is unavailable: " + reason });
271
+ }
272
+
273
+ /**
274
+ * Stop the split after an unexpected error instead of letting it reach Pi's render loop, which would end Pi.
275
+ * The next layout pass shows Pi's own history and dock.
276
+ */
277
+ private fail(error: unknown): void {
278
+ if (this.failed) return;
279
+ this.failed = true;
280
+ this.searchShortcut.dispose();
281
+ this.disposeDocuments?.();
282
+ queueMicrotask(() => {
283
+ this.closeSearch(false);
284
+ this.report({ summary: "split disabled after an error", error });
285
+ this.host?.requestRender();
286
+ });
287
+ }
288
+
255
289
  private button(label: () => string, action: () => void, enabled = (): boolean => true, background?: () => Color): Component {
256
290
  const button = new MouseRegion(memoized(width => [width, this.theme(), this.preferences.buttonStyle, label(), enabled(), background?.()],
257
291
  width => new Text(this.theme().style((this.preferences.buttonStyle === "icons" ? "[ " + label() + " ]" : "[" + label() + "]").padEnd(width), {
@@ -276,7 +310,7 @@ export class TranscriptLayout {
276
310
  }
277
311
  const host = tui as unknown as NativeTui;
278
312
  if (typeof host.getFocusedComponent !== "function") {
279
- this.warn("pi-stereo-view split is unavailable: Pi's focus API is not supported.");
313
+ this.refuse("Pi's focus API is not supported.");
280
314
  return;
281
315
  }
282
316
  if (host.layoutRoot === this.mounted && this.host) {
@@ -286,7 +320,7 @@ export class TranscriptLayout {
286
320
  mounted.preserveForeignChildren(original as VStack);
287
321
  this.dispose();
288
322
  this.rejectedRoots.add(original);
289
- this.warn("pi-stereo-view split is unavailable: Pi's fullscreen layout is not supported.");
323
+ this.refuse("Pi's fullscreen layout is not supported.");
290
324
  return;
291
325
  }
292
326
  const root = host.layoutRoot;
@@ -294,7 +328,7 @@ export class TranscriptLayout {
294
328
  if (root && this.rejectedRoots.has(root)) return;
295
329
  if (!root || root.constructor.name !== "VStack" || root.children.length !== 2) {
296
330
  if (root) this.rejectedRoots.add(root);
297
- this.warn("pi-stereo-view split is unavailable: Pi's fullscreen layout is not supported.");
331
+ this.refuse("Pi's fullscreen layout is not supported.");
298
332
  return;
299
333
  }
300
334
  const [transcript, dock] = root.children;
@@ -305,7 +339,7 @@ export class TranscriptLayout {
305
339
  document?.constructor.name !== "Container" || owner.child !== document || document.children.length !== 3 ||
306
340
  !Array.isArray((document.children[2] as Container)?.children)) {
307
341
  this.rejectedRoots.add(root);
308
- this.warn("pi-stereo-view split is unavailable: Pi's history document is not supported.");
342
+ this.refuse("Pi's history document is not supported.");
309
343
  return;
310
344
  }
311
345
  // Route only the known native dock shape. Keep the original untouched for fallback.
@@ -344,15 +378,33 @@ export class TranscriptLayout {
344
378
  const showTimestamps = (): boolean => this.preferences.showTimestamps;
345
379
  const showCopyButtons = (): boolean => this.preferences.showCopyButtons;
346
380
  const index = new NativeContentIndex(document, this.nested, () => this.preferences.nestedTree);
381
+ this.index = index;
382
+ // With turns off, every turn setting keeps its value but nothing marks, highlights or jumps.
383
+ const on = (): boolean => this.preferences.turns;
384
+ const turns: DocumentTurns = {
385
+ clock: () => on() && this.preferences.turnClock,
386
+ stripe: () => on() ? this.preferences.turnStripe : "off",
387
+ background: () => on() && this.preferences.turnBackground,
388
+ separator: () => on() && this.preferences.turnSeparator,
389
+ mix: () => this.turnMix(),
390
+ color: (turn, layers) => turnColor(this.theme(), turn, this.terminalBackground, this.turnMix(), layers),
391
+ hovered: () => this.hoverEnabled() ? this.hoverAct : undefined,
392
+ hover: turn => { if (this.moving) this.moving.turn = turn; },
393
+ goTo: () => on() ? this.preferences.goTo : "off",
394
+ jump: (from, source) => {
395
+ void this.goTo(from, source).catch(error => this.warn("Could not open the other panel: " + String(error)));
396
+ },
397
+ };
347
398
  const activityDocument = new NativeDocument(document, "activity", this.timestamps, this.theme, timestampGutter, showTimestamps,
348
399
  () => this.activate("activity"), false, index, this.copy, showCopyButtons,
349
400
  () => ({ compact: this.preferences.compactTools, group: this.preferences.groupTools }), this.getTools,
350
401
  () => { if (this.activity) this.activity.scrollTo(this.activity.scrollTop, { disableFollow: true }); }, () => this.preferences.buttonStyle, {
351
402
  enabled: () => this.preferences.streamTools, delay: () => this.preferences.streamDelay * 1000,
352
403
  changed: () => this.host?.requestRender(),
353
- });
404
+ }, turns);
354
405
  const chatDocument = new NativeDocument(document, "chat", this.timestamps, this.theme, timestampGutter, showTimestamps,
355
- () => this.activate("chat"), false, index, this.copy, showCopyButtons, undefined, undefined, undefined, () => this.preferences.buttonStyle);
406
+ () => this.activate("chat"), false, index, this.copy, showCopyButtons, undefined, undefined, undefined, () => this.preferences.buttonStyle,
407
+ undefined, turns);
356
408
  this.documents = { activity: activityDocument, chat: chatDocument };
357
409
  this.disposeDocuments = () => {
358
410
  activityDocument.dispose();
@@ -443,10 +495,7 @@ export class TranscriptLayout {
443
495
  return true;
444
496
  } catch (error) {
445
497
  projectSurface = false;
446
- this.failed = true;
447
- this.searchShortcut.dispose();
448
- this.disposeDocuments?.();
449
- queueMicrotask(() => this.warn("pi-stereo-view split disabled: " + (error as Error).message));
498
+ this.fail(error);
450
499
  return false;
451
500
  }
452
501
  };
@@ -605,6 +654,16 @@ export class TranscriptLayout {
605
654
  const pane = this.searchPane();
606
655
  return pane === "chat" ? this.chat : pane === "activity" ? this.activity : undefined;
607
656
  },
657
+ failed: error => this.fail(error),
658
+ pointer: {
659
+ start: () => { this.moving = { turn: 0 }; },
660
+ end: () => {
661
+ const turn = this.moving?.turn ?? 0;
662
+ this.moving = undefined;
663
+ this.setHover(turn);
664
+ },
665
+ leave: () => this.setHover(0),
666
+ },
608
667
  });
609
668
  surface.setModels([columns, rows, activityPane, chatPane]);
610
669
  this.surface = surface;
@@ -612,10 +671,19 @@ export class TranscriptLayout {
612
671
  { component: toolbar, basis: "auto", shrink: 0, visible: () => {
613
672
  // Root visibility is evaluated once at the start of each native layout pass.
614
673
  pass = undefined;
615
- this.timestamps.beginFrame();
616
- index.beginFrame();
617
- activityDocument.beginFrame(true);
618
- chatDocument.beginFrame(true);
674
+ // An error here would reach Pi's render loop and end Pi; stop the split instead.
675
+ try {
676
+ this.timestamps.beginFrame();
677
+ // Combined history draws Pi's own document, so the shared index waits until the split is shown again.
678
+ // Pi-Bolt can then keep releasing the settled messages it draws itself.
679
+ if (this.preferences.mode === "split" && !this.failed) {
680
+ index.beginFrame();
681
+ activityDocument.beginFrame(true);
682
+ chatDocument.beginFrame(true);
683
+ }
684
+ } catch (error) {
685
+ this.fail(error);
686
+ }
619
687
  return true;
620
688
  } },
621
689
  { component: {
@@ -633,6 +701,66 @@ export class TranscriptLayout {
633
701
  this.mounted = mounted;
634
702
  host.setLayoutRoot(mounted);
635
703
  host.requestRender();
704
+ if (this.terminalStarted) this.queryBackground(host);
705
+ }
706
+
707
+ /**
708
+ * Pi's terminal has started, so terminal queries can be sent. An early layout mounts before that start, and a
709
+ * query sent then answers ahead of Pi's own start queries: Pi takes that answer for its own and misses the
710
+ * terminal's support for program status (OSC 7501) and keyboard protocols.
711
+ */
712
+ markTerminalStarted(): void {
713
+ this.terminalStarted = true;
714
+ if (this.host) this.queryBackground(this.host);
715
+ }
716
+
717
+ /** Ask the terminal for its background once; turn colors use a guess from the theme until it answers. */
718
+ private queryBackground(host: NativeTui): void {
719
+ if (this.terminalBackground || typeof host.queryTerminalColors !== "function") return;
720
+ void host.queryTerminalColors({ timeoutMs: 1000 }).then(colors => {
721
+ if (!colors.background || this.host !== host) return;
722
+ this.terminalBackground = colors.background;
723
+ host.requestRender();
724
+ }, () => {});
725
+ }
726
+
727
+ /** Pi reports pointer motion for hover only outside terminal multiplexers and with its mouse support on. */
728
+ hoverAvailable(): boolean {
729
+ return pointerMotionAvailable(this.host);
730
+ }
731
+
732
+ /** Hover highlighting is on, with turns, and the terminal reports pointer motion. */
733
+ private hoverEnabled(): boolean {
734
+ return this.preferences.turns && this.preferences.turnHover && this.hoverAvailable();
735
+ }
736
+
737
+ /** Highlight another turn; pointer moves within one turn render nothing. */
738
+ private setHover(turn: number): void {
739
+ const next = turn || undefined;
740
+ if (next === this.hoverAct) return;
741
+ this.hoverAct = next;
742
+ if (this.hoverEnabled()) this.host?.requestRender();
743
+ }
744
+
745
+ /** Scroll the other panel to the same point of history as a block, returning from a zoomed panel first. */
746
+ async goTo(from: Pane, source: Component): Promise<void> {
747
+ const target = this.index?.counterpart(source, from);
748
+ if (!target || !this.searchPane()) return;
749
+ const pane: Pane = from === "chat" ? "activity" : "chat";
750
+ if (this.preferences.zoom) {
751
+ this.preferences.zoom = null;
752
+ this.changed(false);
753
+ // The restored panel needs a layout pass for its viewport.
754
+ await setImmediate();
755
+ }
756
+ const document = this.documents?.[pane];
757
+ const view = pane === "chat" ? this.chat : this.activity;
758
+ if (!document || !view || !this.searchPane()) return;
759
+ this.activate(pane);
760
+ const row = document.locate(target);
761
+ if (row === undefined) return;
762
+ view.scrollTo(Math.max(0, row - 1), { disableFollow: true });
763
+ this.host?.requestRender();
636
764
  }
637
765
 
638
766
  private activate(pane: Pane): void {
@@ -766,6 +894,31 @@ export class TranscriptLayout {
766
894
  if (persist) this.save();
767
895
  }
768
896
 
897
+ /** The turn marks' mix strengths, as the palette reads them. */
898
+ private turnMix(): TurnMix {
899
+ const { turnClockMix: clock, turnStripeMix: stripe, turnBackgroundMix: background, turnHoverMix: hover, turnHoverColor } = this.preferences;
900
+ // One hover color for every turn, or each turn's own hue.
901
+ return { clock, stripe, background, hover, hoverColor: parseHex(turnHoverColor) };
902
+ }
903
+
904
+ /** Turn the whole turn feature on or off, or change one of its marks or strengths. */
905
+ setTurnMark<Key extends TurnMarkSetting>(key: Key, value: Preferences[Key]): void {
906
+ this.preferences[key] = value;
907
+ this.changed();
908
+ }
909
+
910
+ /** Highlight the turn under the pointer in both panels. */
911
+ setTurnHover(hover: boolean): void {
912
+ this.preferences.turnHover = hover;
913
+ this.changed();
914
+ }
915
+
916
+ /** Open the other panel from a block's clock, its button, both or neither. */
917
+ setGoTo(goTo: Preferences["goTo"]): void {
918
+ this.preferences.goTo = goTo;
919
+ this.changed();
920
+ }
921
+
769
922
  /** Show or hide clock rows without changing their recorded source times. */
770
923
  setTimestamps(show: boolean): void {
771
924
  this.preferences.showTimestamps = show;
@@ -912,6 +1065,9 @@ export class TranscriptLayout {
912
1065
  this.activity = undefined;
913
1066
  this.chat = undefined;
914
1067
  this.documents = undefined;
1068
+ this.index = undefined;
1069
+ this.hoverAct = undefined;
1070
+ this.moving = undefined;
915
1071
  this.drag = undefined;
916
1072
  this.rebuild = () => {};
917
1073
  }