pi-stereo-view 0.1.18 → 0.1.20

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
@@ -10,6 +10,8 @@ import { NestedCalls } from "./nested-calls.ts";
10
10
  import type { Preferences } from "./preferences.ts";
11
11
  import type { TranscriptTimestamps } from "./timestamps.ts";
12
12
  import { PANEL_SEARCH_ROWS, PanelSearch, PanelSearchShortcut, type SearchHit } from "./search.ts";
13
+ import { SplitSurface } from "./surface.ts";
14
+ import { sameValues } from "./render-cache.ts";
13
15
 
14
16
  type NativeTui = TUI & {
15
17
  layoutRoot?: Container;
@@ -21,6 +23,23 @@ const MIN_ACTIVITY = 40;
21
23
  const MIN_ROWS = 4;
22
24
  const TOOLBAR_BUTTON_WIDTH = 6;
23
25
 
26
+ /**
27
+ * A control that renders again only when one of its inputs changes. Every spinner frame lays out
28
+ * all toolbar and panel controls; restyling unchanged labels would cost more than the panels themselves.
29
+ */
30
+ function memoized(inputs: (width: number) => unknown[], render: (width: number) => string[]): Component {
31
+ let last: { inputs: unknown[]; lines: string[] } | undefined;
32
+ return {
33
+ render: width => {
34
+ const current = inputs(width);
35
+ if (last && sameValues(last.inputs, current)) return last.lines;
36
+ last = { inputs: current, lines: render(width) };
37
+ return last.lines;
38
+ },
39
+ invalidate: () => { last = undefined; },
40
+ };
41
+ }
42
+
24
43
  class PanelScrollView extends ScrollView {
25
44
  constructor(component: Component, options: ScrollViewOptions, private readonly interact: () => void, active: () => boolean) {
26
45
  super(component, options);
@@ -111,7 +130,9 @@ class ButtonStack extends VStack {
111
130
  flush();
112
131
  }
113
132
  }
114
- return super.render(width);
133
+ // Pi and the split surface lay out these rows themselves and read render only for the height.
134
+ // Rendering the rows here would composite every button into a full-width row on every frame.
135
+ return Array<string>(this.children.length).fill("");
115
136
  }
116
137
  }
117
138
  class TranscriptColumns extends HStack {
@@ -137,11 +158,17 @@ class TranscriptRows extends VStack {
137
158
  class TranscriptRoot extends VStack {
138
159
  private readonly owned: Component[];
139
160
 
140
- constructor(children: StackChild[]) {
161
+ constructor(children: StackChild[], private readonly unhandled: (event: TuiMouseEvent) => void) {
141
162
  super(children);
142
163
  this.owned = [...this.children];
143
164
  }
144
165
 
166
+ override handleMouse(event: TuiMouseEvent): ReturnType<Container["handleMouse"]> {
167
+ // Pi offers the root only events no component handled, such as pointer moves outside the split panels.
168
+ this.unhandled(event);
169
+ return undefined;
170
+ }
171
+
145
172
  unchanged(): boolean {
146
173
  return this.children.length === this.owned.length && this.children.every((child, index) => child === this.owned[index]);
147
174
  }
@@ -194,6 +221,7 @@ export class TranscriptLayout {
194
221
  private activity?: ScrollView;
195
222
  private chat?: ScrollView;
196
223
  private documents?: Record<Pane, NativeDocument>;
224
+ private surface?: SplitSurface;
197
225
  private disposeDocuments?: () => void;
198
226
  private activePane: Pane = "chat";
199
227
  private readonly searchShortcut = new PanelSearchShortcut();
@@ -225,12 +253,10 @@ export class TranscriptLayout {
225
253
  ) {}
226
254
 
227
255
  private button(label: () => string, action: () => void, enabled = (): boolean => true, background?: () => Color): Component {
228
- const button = new MouseRegion({
229
- render: width => new Text(this.theme().style((this.preferences.buttonStyle === "icons" ? "[ " + label() + " ]" : "[" + label() + "]").padEnd(width), {
256
+ const button = new MouseRegion(memoized(width => [width, this.theme(), this.preferences.buttonStyle, label(), enabled(), background?.()],
257
+ width => new Text(this.theme().style((this.preferences.buttonStyle === "icons" ? "[ " + label() + " ]" : "[" + label() + "]").padEnd(width), {
230
258
  fg: enabled() ? "accent" : "dim", bg: background?.(),
231
- }), 0, 0).render(width),
232
- invalidate() {},
233
- }, event => {
259
+ }), 0, 0).render(width)), event => {
234
260
  if (event.button !== "left") return;
235
261
  if (event.type === "press") return { capture: true };
236
262
  if (event.type === "click" && enabled() && event.x >= 0 && event.x < event.width && event.y === 0) {
@@ -317,9 +343,10 @@ export class TranscriptLayout {
317
343
  const timestampGutter = (): number => scroll.scrollbar === "auto" ? 1 : 0;
318
344
  const showTimestamps = (): boolean => this.preferences.showTimestamps;
319
345
  const showCopyButtons = (): boolean => this.preferences.showCopyButtons;
320
- const index = new NativeContentIndex(document, this.nested);
346
+ const index = new NativeContentIndex(document, this.nested, () => this.preferences.nestedTree);
321
347
  const activityDocument = new NativeDocument(document, "activity", this.timestamps, this.theme, timestampGutter, showTimestamps,
322
- () => this.activate("activity"), false, index, this.copy, showCopyButtons, () => this.preferences.compactTools, this.getTools,
348
+ () => this.activate("activity"), false, index, this.copy, showCopyButtons,
349
+ () => ({ compact: this.preferences.compactTools, group: this.preferences.groupTools }), this.getTools,
323
350
  () => { if (this.activity) this.activity.scrollTo(this.activity.scrollTop, { disableFollow: true }); }, () => this.preferences.buttonStyle, {
324
351
  enabled: () => this.preferences.streamTools, delay: () => this.preferences.streamDelay * 1000,
325
352
  changed: () => this.host?.requestRender(),
@@ -342,6 +369,11 @@ export class TranscriptLayout {
342
369
  this.activity = new PanelScrollView(activityDocument, scrollOptions, () => this.activate("activity"),
343
370
  () => this.searchPane() === "activity");
344
371
  this.chat = new PanelScrollView(chatDocument, scrollOptions, () => this.activate("chat"), () => this.searchPane() === "chat");
372
+ // Each document renders only the history near its panel's viewport from the previous layout.
373
+ for (const [document, view] of [[activityDocument, this.activity], [chatDocument, this.chat]] as const) {
374
+ document.setViewport(() => view.viewportHeight ?
375
+ { top: view.scrollTop, height: view.viewportHeight, following: view.isFollowingEnd } : undefined, () => this.host?.requestRender());
376
+ }
345
377
  let historyHeight = 0;
346
378
  let chatHeader: Component;
347
379
  let activityHeader: Component;
@@ -439,25 +471,25 @@ export class TranscriptLayout {
439
471
  ]);
440
472
  const pane = (title: string, target: Pane, view: ScrollView): Component => {
441
473
  const hue = target === "chat" ? rgbColor(104, 158, 115) : rgbColor(107, 152, 190);
474
+ let colors: { inputs: unknown[]; palette: { fill: Color; text: Color; border: Color } } | undefined;
475
+ // Mixed colors keep their identity while the theme is unchanged, so memoized controls can compare them.
442
476
  const palette = () => {
443
477
  const theme = this.theme();
444
- return {
478
+ const inputs = [theme, theme.colors.customMessageBg, theme.colors.text, theme.colors.border];
479
+ if (!colors || !sameValues(colors.inputs, inputs)) colors = { inputs, palette: {
445
480
  fill: mixColors(theme.colors.customMessageBg, hue, 0.15, "srgb"),
446
481
  text: mixColors(theme.colors.text, hue, 0.25),
447
482
  border: mixColors(theme.colors.border, hue, 0.3),
448
- };
449
- };
450
- const line: Component = {
451
- render: width => [this.theme().style("─".repeat(width), { fg: palette().border })],
452
- invalidate() {},
483
+ } };
484
+ return colors.palette;
453
485
  };
486
+ const line = memoized(width => [width, this.theme(), palette()],
487
+ width => [this.theme().style("─".repeat(width), { fg: palette().border })]);
454
488
  const label = new ButtonStack([
455
- { component: new MouseRegion({
456
- render: width => new Text(this.theme().style(truncateToWidth(width < title.length + 3 ? title : " " + (this.searchPane() === target ? "● " : " ") + title, width, "").padEnd(width), {
489
+ { component: new MouseRegion(memoized(width => [width, this.theme(), palette(), this.searchPane() === target],
490
+ width => new Text(this.theme().style(truncateToWidth(width < title.length + 3 ? title : " " + (this.searchPane() === target ? "● " : " ") + title, width, "").padEnd(width), {
457
491
  fg: this.searchPane() === target ? "accent" : palette().text, bg: palette().fill,
458
- }), 0, 0).render(width),
459
- invalidate() {},
460
- }, event => {
492
+ }), 0, 0).render(width)), event => {
461
493
  if (event.button === "left" && (event.type === "press" || event.type === "click")) {
462
494
  this.activate(target);
463
495
  return { handled: true, render: true };
@@ -486,10 +518,8 @@ export class TranscriptLayout {
486
518
  ] : [
487
519
  { index: 1, icon: 6 },
488
520
  { index: 2, icon: 5 },
489
- ], title.length, {
490
- render: width => [this.theme().style(" ".repeat(width), { bg: palette().fill })],
491
- invalidate() {},
492
- });
521
+ ], title.length, memoized(width => [width, this.theme(), palette()],
522
+ width => [this.theme().style(" ".repeat(width), { bg: palette().fill })]));
493
523
  headers.push(label);
494
524
  const header = new VStack([
495
525
  { component: line, basis: 1, shrink: 0, visible: () =>
@@ -507,12 +537,10 @@ export class TranscriptLayout {
507
537
  };
508
538
  const activityPane = pane("Activity", "activity", this.activity);
509
539
  const chatPane = pane("Conversation", "chat", this.chat);
510
- const divider = (axis: "columns" | "rows"): Component => new MouseRegion({
511
- render: width => axis === "columns" ?
540
+ const divider = (axis: "columns" | "rows"): Component => new MouseRegion(memoized(width => [width, host.terminal.rows, this.theme()],
541
+ width => axis === "columns" ?
512
542
  Array.from({ length: host.terminal.rows }, () => this.theme().fg("dim", "║")) :
513
- [this.theme().fg("dim", "═".repeat(width))],
514
- invalidate() {},
515
- }, event => {
543
+ [this.theme().fg("dim", "═".repeat(width))]), event => {
516
544
  if (event.button !== "left" && event.type !== "release") return;
517
545
  const position = axis === "columns" ? event.screenX : event.screenY;
518
546
  if (event.type === "press") {
@@ -564,32 +592,42 @@ export class TranscriptLayout {
564
592
  }
565
593
  };
566
594
  this.rebuild();
595
+ const surface = new SplitSurface(host, {
596
+ model: () => {
597
+ const zoom = this.preferences.zoom;
598
+ if (zoom) return { component: zoom === "chat" ? chatPane : activityPane, fill: false, minSize: MIN_ROWS };
599
+ return this.preferences.layout === "columns" ? { component: columns, fill: true, minSize: 1 } : { component: rows, fill: false, minSize: 1 };
600
+ },
601
+ height: () => historyHeight,
602
+ active: () => {
603
+ const pane = this.searchPane();
604
+ return pane === "chat" ? this.chat : pane === "activity" ? this.activity : undefined;
605
+ },
606
+ });
607
+ surface.setModels([columns, rows, activityPane, chatPane]);
608
+ this.surface = surface;
567
609
  const mounted = new TranscriptRoot([
568
610
  { component: toolbar, basis: "auto", shrink: 0, visible: () => {
569
611
  // Root visibility is evaluated once at the start of each native layout pass.
570
612
  pass = undefined;
571
613
  this.timestamps.beginFrame();
572
- activityDocument.beginFrame();
573
- chatDocument.beginFrame();
614
+ index.beginFrame();
615
+ activityDocument.beginFrame(true);
616
+ chatDocument.beginFrame(true);
574
617
  return true;
575
618
  } },
576
619
  { component: {
577
620
  render: width => [this.theme().style("─".repeat(width), { fg: this.theme().colors.border })],
578
621
  invalidate() {},
579
622
  }, basis: 1, shrink: 0, visible: viewport => !shown(viewport) },
580
- { component: columns, basis: 0, grow: 1, minSize: 1,
581
- visible: viewport => shown(viewport) && !this.preferences.zoom && this.preferences.layout === "columns" },
582
- { component: rows, basis: "auto", minSize: 1,
583
- visible: viewport => shown(viewport) && !this.preferences.zoom && this.preferences.layout === "rows" },
584
- { component: activityPane, basis: "auto", minSize: MIN_ROWS,
585
- visible: viewport => shown(viewport) && this.preferences.zoom === "activity" },
586
- { component: chatPane, basis: "auto", minSize: MIN_ROWS,
587
- visible: viewport => shown(viewport) && this.preferences.zoom === "chat" },
623
+ // The split paints as one full-width row block; the proxy gives Pi's keyboard scrolling the active panel.
624
+ { component: surface, basis: "auto", minSize: 1, visible: viewport => shown(viewport) },
625
+ { component: surface.proxy, basis: 0, shrink: 0, visible: viewport => shown(viewport) },
588
626
  { component: transcript, basis: 0, grow: 1, minSize: 1, visible: viewport => !shown(viewport) },
589
627
  ...(projectedDock ? [{ component: projectedDock, basis: "auto" as const, shrink: 0, minSize: 1,
590
628
  visible: () => projectSurface }] : []),
591
629
  { component: dock, basis: "auto", shrink: 0, minSize: 1, visible: () => !projectSurface },
592
- ]);
630
+ ], event => surface.unhandled(event));
593
631
  this.mounted = mounted;
594
632
  host.setLayoutRoot(mounted);
595
633
  host.requestRender();
@@ -749,7 +787,20 @@ export class TranscriptLayout {
749
787
  this.preferences.compactTools = compact;
750
788
  this.changed();
751
789
  }
752
- /** Show the latest compact tool calls in full until they have been finished for the delay. */
790
+
791
+ /** Merge adjacent calls of one tool in one state under a counted header, with or without compact rows. */
792
+ setGroupTools(group: boolean): void {
793
+ this.preferences.groupTools = group;
794
+ this.changed();
795
+ }
796
+
797
+ /** Show nested calls as a tree, or keep the parent's native output panel. */
798
+ setNestedTree(tree: boolean): void {
799
+ this.preferences.nestedTree = tree;
800
+ this.changed();
801
+ }
802
+
803
+ /** Show the latest tool calls in full while Compact or Group would hide them, until they have been finished for the delay. */
753
804
  setStreamTools(stream: boolean): void {
754
805
  this.preferences.streamTools = stream;
755
806
  this.changed();
@@ -846,6 +897,8 @@ export class TranscriptLayout {
846
897
  this.searchShortcut.dispose();
847
898
  this.disposeDocuments?.();
848
899
  this.disposeDocuments = undefined;
900
+ this.surface?.dispose();
901
+ this.surface = undefined;
849
902
  const host = this.host;
850
903
  this.activity?.setScrollbar("hidden");
851
904
  this.chat?.setScrollbar("hidden");