pi-stereo-view 0.1.18 → 0.1.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/package.json +5 -3
- package/src/content.ts +132 -63
- package/src/layout.ts +77 -38
- package/src/native.ts +371 -46
- package/src/render-cache.ts +19 -5
- package/src/surface.ts +875 -0
- package/src/timestamps.ts +41 -5
- package/src/tool-streaming.ts +31 -13
package/src/native.ts
CHANGED
|
@@ -3,13 +3,14 @@ import { Container, Spacer, Text, type Component, type TuiMouseEvent } from "@ea
|
|
|
3
3
|
import { searchText, type SearchEntry, type SearchHit } from "./search.ts";
|
|
4
4
|
import { highlightSearchRows, searchHitRow, type SearchClock } from "./search-highlight.ts";
|
|
5
5
|
import { CompactMessage, type TranscriptTimestamps } from "./timestamps.ts";
|
|
6
|
-
import { NativeContentIndex, type ContentEntry, type Pane } from "./content.ts";
|
|
6
|
+
import { NativeContentIndex, callStatus, type ContentEntry, type Pane } from "./content.ts";
|
|
7
7
|
import { CompactTool } from "./compact-tools.ts";
|
|
8
8
|
import { toolPresentations, type ToolPresentation } from "./tool-presentation.ts";
|
|
9
9
|
import { ToolStreaming } from "./tool-streaming.ts";
|
|
10
10
|
import { toolGroups, groupSummary, type ToolGroup } from "./tool-groups.ts";
|
|
11
11
|
import { ToolIndent } from "./tool-indent.ts";
|
|
12
12
|
import { NestedTree, type NativeTreeBody } from "./nested-tree.ts";
|
|
13
|
+
import { pushShellInputs, sameValues } from "./render-cache.ts";
|
|
13
14
|
export type { Pane } from "./content.ts";
|
|
14
15
|
|
|
15
16
|
/** Decorate one selected block without changing native text or mouse coordinates. */
|
|
@@ -44,6 +45,52 @@ class SearchHighlight implements Component {
|
|
|
44
45
|
}
|
|
45
46
|
}
|
|
46
47
|
|
|
48
|
+
/** Scroll state of the panel showing a document, read before the panel's layout in a frame. */
|
|
49
|
+
export interface DocumentViewport { top: number; height: number; following: boolean }
|
|
50
|
+
|
|
51
|
+
/** Time per frame for measuring offscreen entries that are new, changed or stale. */
|
|
52
|
+
const LAZY_BUDGET_MS = 4;
|
|
53
|
+
/** Offscreen entries checked for native changes per frame, in rotation. */
|
|
54
|
+
const SWEEP_ENTRIES = 64;
|
|
55
|
+
|
|
56
|
+
/** One projected entry's rows, the components that produced them, and the inputs they depend on. */
|
|
57
|
+
interface Segment {
|
|
58
|
+
components: Component[];
|
|
59
|
+
heights: number[];
|
|
60
|
+
lines: string[];
|
|
61
|
+
width: number;
|
|
62
|
+
epoch: number;
|
|
63
|
+
/** Rows are blank placeholders with the entry's last known height. */
|
|
64
|
+
estimate: boolean;
|
|
65
|
+
visibleIn: boolean;
|
|
66
|
+
toolRunIn: boolean;
|
|
67
|
+
visibleOut: boolean;
|
|
68
|
+
toolRunOut: boolean;
|
|
69
|
+
/** Cheap fingerprint of native state, compared while the entry stays offscreen. */
|
|
70
|
+
print: unknown[];
|
|
71
|
+
/** First row and build of the last document this segment was part of. */
|
|
72
|
+
start: number;
|
|
73
|
+
build: number;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The last document: segment order with the row and child offsets of each segment. */
|
|
77
|
+
interface Built {
|
|
78
|
+
order: Segment[];
|
|
79
|
+
lines: string[][];
|
|
80
|
+
components: Component[][];
|
|
81
|
+
rows: number[];
|
|
82
|
+
childRows: number[];
|
|
83
|
+
document: string[];
|
|
84
|
+
children: Component[];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const blankLines = (height: number): string[] => Array<string>(height).fill("");
|
|
88
|
+
|
|
89
|
+
/** The status of every call, in entry order. */
|
|
90
|
+
function pushCallStates(entries: readonly ContentEntry[], into: unknown[]): void {
|
|
91
|
+
for (const entry of entries) if (entry.tool && entry.owner === entry.source) into.push(callStatus(entry.source));
|
|
92
|
+
}
|
|
93
|
+
|
|
47
94
|
/**
|
|
48
95
|
* Project the live native component tree, not messages or renderer strings.
|
|
49
96
|
* Assistant shells keep the native render method and original content components.
|
|
@@ -60,6 +107,11 @@ export class NativeDocument extends Container {
|
|
|
60
107
|
private readonly openTools = new WeakSet<Component>();
|
|
61
108
|
private groups = new Map<Component, ToolGroup>();
|
|
62
109
|
private present: (name: string) => ToolPresentation = name => ({ name, key: name });
|
|
110
|
+
private toolsKey?: unknown[];
|
|
111
|
+
/** Inputs of the current grouping, and the status of every call when it was made. */
|
|
112
|
+
private groupInputs?: unknown[];
|
|
113
|
+
private readonly callStates: unknown[] = [];
|
|
114
|
+
private trees?: { inputs: unknown[]; map: Map<Component, NestedTree> };
|
|
63
115
|
private searchTool?: Component;
|
|
64
116
|
private readonly clockOwners = new WeakMap<Component, Component>();
|
|
65
117
|
private readonly compactBlocks = new Map<Component, CompactMessage>();
|
|
@@ -76,6 +128,22 @@ export class NativeDocument extends Container {
|
|
|
76
128
|
private checked = false;
|
|
77
129
|
private releasedFor?: readonly ContentEntry[];
|
|
78
130
|
private disposed = false;
|
|
131
|
+
// Virtualized history: one cached segment per projected entry, reused while it stays offscreen.
|
|
132
|
+
private readonly segments = new Map<Component, Segment>();
|
|
133
|
+
private built?: Built;
|
|
134
|
+
private segmentsFor?: readonly ContentEntry[];
|
|
135
|
+
private buildId = 0;
|
|
136
|
+
private epoch = 0;
|
|
137
|
+
private presentation: unknown[] = [];
|
|
138
|
+
private invalidations = 0;
|
|
139
|
+
private scrollShift = 0;
|
|
140
|
+
private sweep = 0;
|
|
141
|
+
/** Entries whose native nodes invalidated since they last rendered. */
|
|
142
|
+
private readonly dirty = new Set<Component>();
|
|
143
|
+
private viewport?: () => DocumentViewport | undefined;
|
|
144
|
+
private requestRender?: () => void;
|
|
145
|
+
private followUp = false;
|
|
146
|
+
private readonly print: unknown[] = [];
|
|
79
147
|
|
|
80
148
|
constructor(
|
|
81
149
|
private readonly source: Container,
|
|
@@ -103,11 +171,15 @@ export class NativeDocument extends Container {
|
|
|
103
171
|
private live(source: Component): boolean { return this.compactActive && this.streamer?.isLive(source) === true; }
|
|
104
172
|
private compactActive = false;
|
|
105
173
|
|
|
106
|
-
/**
|
|
107
|
-
|
|
174
|
+
/**
|
|
175
|
+
* Start a native frame; only repeated measurements within that frame may reuse routing.
|
|
176
|
+
* Documents sharing one index pass `indexStarted` after the owner started the index's frame,
|
|
177
|
+
* so the shared history is verified once per frame, not once per panel.
|
|
178
|
+
*/
|
|
179
|
+
beginFrame(indexStarted = false): void {
|
|
108
180
|
this.frameActive = true;
|
|
109
181
|
this.frameWidth = undefined;
|
|
110
|
-
this.index.beginFrame();
|
|
182
|
+
if (!indexStarted) this.index.beginFrame();
|
|
111
183
|
// Blocks are only added from the current entries, so an unchanged index has nothing to release.
|
|
112
184
|
const entries = this.index.entries(this.pane);
|
|
113
185
|
if (this.compactBlocks.size && entries !== this.releasedFor) {
|
|
@@ -124,13 +196,33 @@ export class NativeDocument extends Container {
|
|
|
124
196
|
if (this.frameActive && this.checked) return;
|
|
125
197
|
const chat = this.source.children[2] as Container;
|
|
126
198
|
if (!Array.isArray(chat?.children)) throw new Error("Pi's history document is not supported");
|
|
127
|
-
|
|
128
|
-
this.timestamps.check(component);
|
|
129
|
-
}
|
|
199
|
+
this.timestamps.checkHistory(chat.children);
|
|
130
200
|
this.index.entries(this.pane);
|
|
131
201
|
this.checked = true;
|
|
132
202
|
}
|
|
133
203
|
|
|
204
|
+
/**
|
|
205
|
+
* Render only entries near the panel's viewport; other entries keep their rows from earlier frames.
|
|
206
|
+
* Without a provider, or before the panel has a height, every entry renders each frame.
|
|
207
|
+
*/
|
|
208
|
+
setViewport(provider: (() => DocumentViewport | undefined) | undefined, requestRender?: () => void): void {
|
|
209
|
+
this.viewport = provider;
|
|
210
|
+
this.requestRender = requestRender;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Rows entries above the viewport grew by since the last call; the panel adds them to keep its content in place. */
|
|
214
|
+
takeScrollShift(): number {
|
|
215
|
+
const shift = this.scrollShift;
|
|
216
|
+
this.scrollShift = 0;
|
|
217
|
+
return shift;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Render a segment again on its next frame, even offscreen. */
|
|
221
|
+
private stale(source: Component | undefined): void {
|
|
222
|
+
const segment = source && this.segments.get(source);
|
|
223
|
+
if (segment) segment.epoch = -1;
|
|
224
|
+
}
|
|
225
|
+
|
|
134
226
|
/** Read full saved text from the current native branch, including collapsed blocks. */
|
|
135
227
|
searchEntries(): SearchEntry[] {
|
|
136
228
|
return this.index.entries(this.pane).flatMap(entry => {
|
|
@@ -170,6 +262,9 @@ export class NativeDocument extends Container {
|
|
|
170
262
|
|
|
171
263
|
/** Clear search-only fragments, measurements and highlighting without invalidating native blocks. */
|
|
172
264
|
clearSearch(): void {
|
|
265
|
+
// Highlights and excerpts live in these entries' cached rows.
|
|
266
|
+
this.stale(this.searchAnchor?.entry.source);
|
|
267
|
+
this.stale(this.excerpt?.entry.source);
|
|
173
268
|
this.searchTool = undefined;
|
|
174
269
|
this.searchAnchor = undefined;
|
|
175
270
|
this.excerpt = undefined;
|
|
@@ -214,12 +309,31 @@ export class NativeDocument extends Container {
|
|
|
214
309
|
this.streamer?.dispose();
|
|
215
310
|
for (const block of this.compactBlocks.values()) block.dispose();
|
|
216
311
|
this.compactBlocks.clear();
|
|
312
|
+
this.segments.clear();
|
|
313
|
+
this.dirty.clear();
|
|
314
|
+
this.built = undefined;
|
|
217
315
|
this.frameWidth = undefined;
|
|
218
316
|
this.frameLines = undefined;
|
|
219
317
|
}
|
|
220
318
|
override handleMouse(event: TuiMouseEvent): ReturnType<Container["handleMouse"]> {
|
|
221
319
|
if (event.type === "wheel" || (event.type === "press" && event.button === "left")) this.interact();
|
|
222
|
-
|
|
320
|
+
const built = this.built;
|
|
321
|
+
if (!built || event.y < 0) return undefined;
|
|
322
|
+
// Find the entry under the pointer, then route like a container of that entry's components.
|
|
323
|
+
let low = 0;
|
|
324
|
+
let high = built.order.length - 1;
|
|
325
|
+
while (low < high) {
|
|
326
|
+
const middle = (low + high + 1) >> 1;
|
|
327
|
+
if (built.rows[middle] <= event.y) low = middle; else high = middle - 1;
|
|
328
|
+
}
|
|
329
|
+
const segment = built.order[low];
|
|
330
|
+
const row = event.y - (built.rows[low] ?? 0);
|
|
331
|
+
if (!segment || segment.estimate || row >= segment.lines.length) return undefined;
|
|
332
|
+
const entry = {
|
|
333
|
+
children: segment.components,
|
|
334
|
+
mouseLayout: { width: event.width, children: segment.components.map((component, index) => ({ component, height: segment.heights[index] })) },
|
|
335
|
+
};
|
|
336
|
+
return Container.prototype.handleMouse.call(entry, { ...event, y: row, height: segment.lines.length });
|
|
223
337
|
}
|
|
224
338
|
private toolOpen(source: Component): boolean {
|
|
225
339
|
return this.openTools.has(source) || this.searchTool === source;
|
|
@@ -286,7 +400,10 @@ export class NativeDocument extends Container {
|
|
|
286
400
|
if (!stamped) {
|
|
287
401
|
stamped = new CompactMessage(routed, () => this.timestamps.label(this.clockOwners.get(component) ?? component),
|
|
288
402
|
this.theme, this.timestampGutter, () => this.showTimestamps() && !(this.pane === "activity" && this.compactTools() && entry.kind === "tool" && !this.live(entry.owner)),
|
|
289
|
-
this.copy ? () => this.copyBlock(component) : undefined, this.showCopy, this.buttonStyle)
|
|
403
|
+
this.copy ? () => this.copyBlock(component) : undefined, this.showCopy, this.buttonStyle, () => {
|
|
404
|
+
// Nested panels render inside their owner's segment.
|
|
405
|
+
this.dirty.add(component).add(entry.owner);
|
|
406
|
+
});
|
|
290
407
|
this.stamped.set(routed, stamped);
|
|
291
408
|
}
|
|
292
409
|
this.compactBlocks.set(component, stamped);
|
|
@@ -301,25 +418,27 @@ export class NativeDocument extends Container {
|
|
|
301
418
|
clock: () => block.searchClock(),
|
|
302
419
|
excerpt: this.excerpt?.entry.source === entry.source && entry.owner !== entry.source ? this.searchExcerpt() : undefined };
|
|
303
420
|
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
const
|
|
307
|
-
|
|
308
|
-
this.
|
|
309
|
-
this.
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
if (this.
|
|
321
|
-
|
|
322
|
-
|
|
421
|
+
/** Pi lists its tools as new objects on every call; keep the resolver while their presented fields are the same. */
|
|
422
|
+
private updatePresentations(tools: readonly ToolInfo[]): void {
|
|
423
|
+
const key: unknown[] = [];
|
|
424
|
+
for (const tool of tools) key.push(tool.name, tool.description, tool.parameters, tool.exposure, tool.namespace?.name, tool.sourceInfo);
|
|
425
|
+
if (sameValues(this.toolsKey, key)) return;
|
|
426
|
+
this.toolsKey = key;
|
|
427
|
+
this.present = toolPresentations(tools);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Regroup calls only when the entries, presentation, live calls or a call's status change. Pi updates a call's
|
|
432
|
+
* result without invalidating it, so the status is read from its native fields on every frame.
|
|
433
|
+
*/
|
|
434
|
+
private updateGroups(compactTools: boolean): void {
|
|
435
|
+
const all = this.index.entries();
|
|
436
|
+
const inputs = [compactTools, all, this.present, this.compactActive, this.streamer?.revision];
|
|
437
|
+
if (sameValues(this.groupInputs, inputs) && this.callStatesUnchanged(all)) return;
|
|
438
|
+
this.groupInputs = inputs;
|
|
439
|
+
this.callStates.length = 0;
|
|
440
|
+
pushCallStates(all, this.callStates);
|
|
441
|
+
const groups = compactTools ? toolGroups(all, this.present, source => this.live(source)) : new Map<Component, ToolGroup>();
|
|
323
442
|
// Carry visible expansion through state-driven splits and merges, without
|
|
324
443
|
// reopening explicitly collapsed runs. Read the old state before adding heads.
|
|
325
444
|
const opening: Component[] = [];
|
|
@@ -337,9 +456,22 @@ export class NativeDocument extends Container {
|
|
|
337
456
|
}
|
|
338
457
|
for (const source of opening) this.openGroups.add(source);
|
|
339
458
|
this.groups = groups;
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
private callStatesUnchanged(entries: readonly ContentEntry[]): boolean {
|
|
462
|
+
const states = this.callStates;
|
|
463
|
+
let position = 0;
|
|
464
|
+
for (const entry of entries) {
|
|
465
|
+
if (!entry.tool || entry.owner !== entry.source) continue;
|
|
466
|
+
if (states[position++] !== callStatus(entry.source)) return false;
|
|
467
|
+
}
|
|
468
|
+
return position === states.length;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/** Nested call trees of Activity calls, rebuilt only when the entries or nested calls change. */
|
|
472
|
+
private treesFor(entries: readonly ContentEntry[]): Map<Component, NestedTree> {
|
|
473
|
+
const inputs = [entries, this.index.nested.revision];
|
|
474
|
+
if (this.trees && sameValues(this.trees.inputs, inputs)) return this.trees.map;
|
|
343
475
|
const trees = new Map<Component, NestedTree>();
|
|
344
476
|
if (this.pane === "activity") for (const entry of entries) {
|
|
345
477
|
if (!entry.tool) continue;
|
|
@@ -357,17 +489,67 @@ export class NativeDocument extends Container {
|
|
|
357
489
|
}
|
|
358
490
|
trees.set(entry.source, tree);
|
|
359
491
|
}
|
|
360
|
-
|
|
361
|
-
|
|
492
|
+
this.trees = { inputs, map: trees };
|
|
493
|
+
return trees;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
override render(width: number): string[] {
|
|
497
|
+
if (this.frameActive && this.frameWidth === width && this.frameLines) return this.frameLines;
|
|
498
|
+
const chat = this.source.children[2] as Container;
|
|
499
|
+
this.timestamps.prepare(chat.children);
|
|
500
|
+
this.lastWidth = width;
|
|
501
|
+
this.positions.clear();
|
|
502
|
+
if (this.excerpt && !this.index.contains(this.excerpt.entry.source, this.pane)) this.excerpt = undefined;
|
|
503
|
+
if (this.searchAnchor && !this.index.contains(this.searchAnchor.entry.source, this.pane)) this.searchAnchor = undefined;
|
|
504
|
+
const locateMatch = this.searchAnchor !== undefined || this.excerpt !== undefined;
|
|
505
|
+
let row = 0;
|
|
506
|
+
let visibleBlock = false;
|
|
507
|
+
let toolRun = false;
|
|
508
|
+
const compactTools = this.pane === "activity" && this.compactTools();
|
|
509
|
+
this.updatePresentations(compactTools ? this.getTools() : []);
|
|
510
|
+
this.compactActive = compactTools && this.streaming?.enabled() === true;
|
|
511
|
+
if (this.compactActive) this.streamer!.observe(this.index.entries());
|
|
512
|
+
else this.streamer?.clear();
|
|
513
|
+
this.updateGroups(compactTools);
|
|
514
|
+
const searched = this.searchAnchor ? this.index.entries(this.pane).find(entry => entry.source === this.searchAnchor!.entry.source) : undefined;
|
|
515
|
+
this.searchTool = searched?.owner;
|
|
516
|
+
const entries = this.index.entries(this.pane);
|
|
517
|
+
const trees = this.treesFor(entries);
|
|
518
|
+
const view = locateMatch ? undefined : this.viewport?.();
|
|
519
|
+
// The first frame and search measure every entry; later frames reuse offscreen rows.
|
|
520
|
+
const virtual = view !== undefined && view.height > 0 && this.built !== undefined;
|
|
521
|
+
const presentation = [this.theme(), compactTools, this.compactActive, this.showTimestamps(), this.showCopy(), this.buttonStyle(),
|
|
522
|
+
this.timestampGutter(), this.invalidations];
|
|
523
|
+
if (!sameValues(presentation, this.presentation)) {
|
|
524
|
+
this.presentation = presentation;
|
|
525
|
+
this.epoch++;
|
|
526
|
+
}
|
|
527
|
+
const build = ++this.buildId;
|
|
528
|
+
const started = performance.now();
|
|
529
|
+
// Render the viewport plus half a viewport on each side; scrolling moves the window before layout.
|
|
530
|
+
const margin = virtual ? Math.ceil(view.height / 2) : 0;
|
|
531
|
+
// A following panel shows the end; find the entries of its last rows from cached heights.
|
|
532
|
+
let tail = 0;
|
|
533
|
+
if (virtual && view.following) {
|
|
534
|
+
let rows = 0;
|
|
535
|
+
tail = entries.length;
|
|
536
|
+
while (tail > 0 && rows < view.height + margin) rows += this.segments.get(entries[--tail].source)?.lines.length ?? 1;
|
|
537
|
+
}
|
|
538
|
+
const sweep = entries.length ? this.sweep % entries.length : 0;
|
|
539
|
+
this.sweep = sweep + SWEEP_ENTRIES;
|
|
540
|
+
const top = virtual && !view.following ? view.top : 0;
|
|
541
|
+
let shift = 0;
|
|
542
|
+
let pending = false;
|
|
543
|
+
const order: Segment[] = [];
|
|
544
|
+
const renderEntry = (entry: ContentEntry, compactTool: boolean, tree: NestedTree | undefined): Segment => {
|
|
362
545
|
const component = entry.source;
|
|
363
546
|
const routed = entry.component;
|
|
364
|
-
|
|
365
|
-
const
|
|
366
|
-
const
|
|
547
|
+
const result: Component[] = [];
|
|
548
|
+
const visibleIn = visibleBlock;
|
|
549
|
+
const toolRunIn = toolRun;
|
|
367
550
|
let clock: SearchClock | undefined;
|
|
368
551
|
let nativeBody = true;
|
|
369
552
|
let bodyDepth = 0;
|
|
370
|
-
const compactTool = compactTools && entry.kind === "tool" && !this.live(entry.owner);
|
|
371
553
|
if (compactTool) {
|
|
372
554
|
const group = this.groups.get(entry.owner);
|
|
373
555
|
const grouped = group !== undefined && group.calls.length > 1;
|
|
@@ -382,7 +564,6 @@ export class NativeDocument extends Container {
|
|
|
382
564
|
}
|
|
383
565
|
nativeBody = members && this.toolOpen(entry.owner);
|
|
384
566
|
}
|
|
385
|
-
const tree = trees.get(component);
|
|
386
567
|
const contentStart = result.length;
|
|
387
568
|
const bodyWidth = Math.max(0, width - (bodyDepth ? bodyDepth * 2 + 1 : 0));
|
|
388
569
|
if (nativeBody && tree) {
|
|
@@ -400,16 +581,16 @@ export class NativeDocument extends Container {
|
|
|
400
581
|
result.push(stamped);
|
|
401
582
|
clock = stamped.searchClock();
|
|
402
583
|
}
|
|
403
|
-
if (!tree && this.searchAnchor?.entry.source === component && !this.excerpt && result.length >
|
|
584
|
+
if (!tree && this.searchAnchor?.entry.source === component && !this.excerpt && result.length > 0) {
|
|
404
585
|
const last = result.length - 1;
|
|
405
586
|
result[last] = this.highlighted(result[last]);
|
|
406
587
|
}
|
|
407
588
|
if (locateMatch) {
|
|
408
|
-
const prefix = result.slice(
|
|
589
|
+
const prefix = result.slice(0, contentStart).flatMap(child => child.render(width));
|
|
409
590
|
const lines = [...prefix, ...result.slice(contentStart).flatMap(child => child.render(bodyWidth))];
|
|
410
591
|
let excerptRow: number | undefined;
|
|
411
592
|
if (this.excerpt?.entry.source === component) {
|
|
412
|
-
if (!lines.length &&
|
|
593
|
+
if (!lines.length && visibleIn) {
|
|
413
594
|
result.push(new Spacer(1));
|
|
414
595
|
lines.push("");
|
|
415
596
|
}
|
|
@@ -428,20 +609,164 @@ export class NativeDocument extends Container {
|
|
|
428
609
|
});
|
|
429
610
|
if (excerptRow !== undefined) this.positions.set(component, { row, lines: [], excerptRow });
|
|
430
611
|
} else this.positions.set(component, { row: row + prefixRows, lines: lines.slice(prefixRows), clock, excerptRow });
|
|
431
|
-
row += lines.length;
|
|
432
612
|
}
|
|
433
613
|
for (let child = contentStart; child < result.length; child++) result[child] = this.indent(result[child], bodyDepth, true);
|
|
614
|
+
const heights: number[] = [];
|
|
615
|
+
// Copy rows: native renderers may reuse and later mutate their row arrays.
|
|
616
|
+
const lines: string[] = [];
|
|
617
|
+
for (const child of result) {
|
|
618
|
+
const childLines = child.render(width);
|
|
619
|
+
heights.push(childLines.length);
|
|
620
|
+
for (const line of childLines) lines.push(line);
|
|
621
|
+
}
|
|
622
|
+
const print: unknown[] = [];
|
|
623
|
+
this.fingerprint(entry, compactTool, tree, print);
|
|
624
|
+
let segment = this.segments.get(component);
|
|
625
|
+
if (!segment) {
|
|
626
|
+
segment = { components: result, heights, lines, width, epoch: this.epoch, estimate: false, visibleIn, toolRunIn,
|
|
627
|
+
visibleOut: visibleBlock, toolRunOut: toolRun, print, start: row, build };
|
|
628
|
+
this.segments.set(component, segment);
|
|
629
|
+
return segment;
|
|
630
|
+
}
|
|
631
|
+
// Keep unchanged arrays, so the document can reuse the rows before the first changed entry.
|
|
632
|
+
if (segment.estimate || !sameValues(segment.lines, lines)) segment.lines = lines;
|
|
633
|
+
if (!sameValues(segment.components, result)) segment.components = result;
|
|
634
|
+
Object.assign(segment, { heights, width, epoch: this.epoch, estimate: false, visibleIn, toolRunIn,
|
|
635
|
+
visibleOut: visibleBlock, toolRunOut: toolRun, print });
|
|
636
|
+
return segment;
|
|
637
|
+
};
|
|
638
|
+
for (let index = 0; index < entries.length; index++) {
|
|
639
|
+
const entry = entries[index];
|
|
640
|
+
if (entry.owner !== entry.source && trees.has(entry.owner)) continue;
|
|
641
|
+
const component = entry.source;
|
|
642
|
+
const compactTool = compactTools && entry.kind === "tool" && !this.live(entry.owner);
|
|
643
|
+
const tree = trees.get(component);
|
|
644
|
+
let segment = this.segments.get(component);
|
|
645
|
+
const previous = segment?.build === build - 1 ? { start: segment.start, height: segment.lines.length } : undefined;
|
|
646
|
+
let render = !virtual;
|
|
647
|
+
if (virtual) {
|
|
648
|
+
const height = segment?.lines.length ?? 1;
|
|
649
|
+
// Rows near the viewport always render; offscreen entries render only when changed, within a budget.
|
|
650
|
+
const hot = view.following ? index >= tail :
|
|
651
|
+
row + height > top + shift - margin && row < top + shift + view.height + margin;
|
|
652
|
+
if (hot) render = true;
|
|
653
|
+
else {
|
|
654
|
+
const measured = segment !== undefined && !segment.estimate && segment.width === width && segment.epoch === this.epoch &&
|
|
655
|
+
segment.visibleIn === visibleBlock && segment.toolRunIn === toolRun;
|
|
656
|
+
// Offscreen changes are invisible until the entry nears the viewport, where it renders anyway.
|
|
657
|
+
// Check a rotating slice per frame so heights converge without reading every entry each frame.
|
|
658
|
+
const swept = this.dirty.delete(component) ||
|
|
659
|
+
(index >= sweep && index < sweep + SWEEP_ENTRIES) || index + entries.length < sweep + SWEEP_ENTRIES;
|
|
660
|
+
if (measured && swept) this.fingerprint(entry, compactTool, tree, this.print);
|
|
661
|
+
if (!measured || (swept && !sameValues(segment!.print, this.print))) {
|
|
662
|
+
if (performance.now() < started + LAZY_BUDGET_MS) render = true;
|
|
663
|
+
// Native changes request their own frames; only unmeasured layout needs follow-up frames.
|
|
664
|
+
// This also keeps an unstable fingerprint from scheduling frames forever.
|
|
665
|
+
else if (!measured) pending = true;
|
|
666
|
+
else this.dirty.add(component);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
if (render) {
|
|
671
|
+
this.dirty.delete(component);
|
|
672
|
+
segment = renderEntry(entry, compactTool, tree);
|
|
673
|
+
}
|
|
674
|
+
else {
|
|
675
|
+
if (!segment) {
|
|
676
|
+
segment = { components: [], heights: [], lines: blankLines(1), width, epoch: -1, estimate: true, visibleIn: visibleBlock,
|
|
677
|
+
toolRunIn: toolRun, visibleOut: visibleBlock, toolRunOut: toolRun, print: [], start: row, build };
|
|
678
|
+
this.segments.set(component, segment);
|
|
679
|
+
} else if (segment.width !== width && !segment.estimate) {
|
|
680
|
+
// Rows at another width would overflow the panel; keep only the height until measured.
|
|
681
|
+
Object.assign(segment, { lines: blankLines(segment.lines.length), components: [], heights: [], estimate: true });
|
|
682
|
+
}
|
|
683
|
+
// An entry without rows passes the separator state through.
|
|
684
|
+
if (segment.lines.length) {
|
|
685
|
+
visibleBlock = segment.visibleOut;
|
|
686
|
+
toolRun = segment.toolRunOut;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
// Entries above the viewport that changed height move the visible rows; the panel scrolls by the difference.
|
|
690
|
+
if (virtual && !view.following && previous && previous.start + previous.height <= top) shift += segment.lines.length - previous.height;
|
|
691
|
+
segment.start = row;
|
|
692
|
+
segment.build = build;
|
|
693
|
+
order.push(segment);
|
|
694
|
+
row += segment.lines.length;
|
|
695
|
+
}
|
|
696
|
+
const built = this.document(order);
|
|
697
|
+
if (entries !== this.segmentsFor) {
|
|
698
|
+
this.segmentsFor = entries;
|
|
699
|
+
for (const [source, segment] of this.segments) if (segment.build !== build) this.segments.delete(source);
|
|
700
|
+
for (const source of this.dirty) if (!this.segments.has(source)) this.dirty.delete(source);
|
|
434
701
|
}
|
|
702
|
+
if (virtual && !view.following) this.scrollShift += shift;
|
|
703
|
+
let lines = built.document;
|
|
704
|
+
let children = built.children;
|
|
435
705
|
// Native follow-end guidance overlays the bottom row. Keep the selected last line above it.
|
|
436
|
-
if (this.searchAnchor)
|
|
437
|
-
|
|
706
|
+
if (this.searchAnchor) {
|
|
707
|
+
lines = [...lines, ""];
|
|
708
|
+
children = [...children, new Spacer(1)];
|
|
709
|
+
}
|
|
710
|
+
this.children = children;
|
|
438
711
|
this.frameWidth = width;
|
|
439
|
-
this.frameLines =
|
|
440
|
-
|
|
712
|
+
this.frameLines = lines;
|
|
713
|
+
if (pending) this.scheduleFollowUp();
|
|
714
|
+
return lines;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* Join segment rows, reusing the previous document up to the first changed segment. The segment offsets are
|
|
719
|
+
* rewritten in place; the returned rows and children are new arrays, so earlier render results stay intact.
|
|
720
|
+
*/
|
|
721
|
+
private document(order: Segment[]): Built {
|
|
722
|
+
const built = this.built ??= { order: [], lines: [], components: [], rows: [], childRows: [], document: [], children: [] };
|
|
723
|
+
let same = 0;
|
|
724
|
+
const limit = Math.min(order.length, built.order.length);
|
|
725
|
+
while (same < limit && order[same] === built.order[same] && order[same].lines === built.lines[same] &&
|
|
726
|
+
order[same].components === built.components[same]) same++;
|
|
727
|
+
if (same === order.length && same === built.order.length) return built;
|
|
728
|
+
built.document = built.document.slice(0, same < built.order.length ? built.rows[same] : built.document.length);
|
|
729
|
+
built.children = built.children.slice(0, same < built.order.length ? built.childRows[same] : built.children.length);
|
|
730
|
+
for (const list of [built.order, built.lines, built.components, built.rows, built.childRows]) list.length = same;
|
|
731
|
+
for (let index = same; index < order.length; index++) {
|
|
732
|
+
const segment = order[index];
|
|
733
|
+
built.order.push(segment);
|
|
734
|
+
built.lines.push(segment.lines);
|
|
735
|
+
built.components.push(segment.components);
|
|
736
|
+
built.rows.push(built.document.length);
|
|
737
|
+
built.childRows.push(built.children.length);
|
|
738
|
+
for (const line of segment.lines) built.document.push(line);
|
|
739
|
+
for (const component of segment.components) built.children.push(component);
|
|
740
|
+
}
|
|
741
|
+
return built;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/** Native state an offscreen entry's rows depend on, read without rendering it. */
|
|
745
|
+
private fingerprint(entry: ContentEntry, compactTool: boolean, tree: NestedTree | undefined, out: unknown[]): void {
|
|
746
|
+
out.length = 0;
|
|
747
|
+
const routed = entry.component;
|
|
748
|
+
pushShellInputs(routed, out);
|
|
749
|
+
const children = (routed as Partial<Container>).children;
|
|
750
|
+
const group = compactTool ? this.groups.get(entry.owner) : undefined;
|
|
751
|
+
out.push(children, (children?.[0] as Partial<Container> | undefined)?.children, entry.spacing.length,
|
|
752
|
+
this.stamped.get(routed)?.revision, compactTool, this.toolOpen(entry.owner),
|
|
753
|
+
group?.calls[0].source, group?.calls.length, group && group.calls.length > 1 ? this.groupOpen(group) : undefined,
|
|
754
|
+
tree ? this.index.nested.revision : undefined);
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/** Measure the remaining offscreen entries in later frames. */
|
|
758
|
+
private scheduleFollowUp(): void {
|
|
759
|
+
if (this.followUp || !this.requestRender || this.disposed) return;
|
|
760
|
+
this.followUp = true;
|
|
761
|
+
setImmediate(() => {
|
|
762
|
+
this.followUp = false;
|
|
763
|
+
if (!this.disposed) this.requestRender?.();
|
|
764
|
+
});
|
|
441
765
|
}
|
|
442
766
|
|
|
443
767
|
override invalidate(): void {
|
|
444
768
|
this.highlightRevision++;
|
|
769
|
+
this.invalidations++;
|
|
445
770
|
this.frameWidth = undefined;
|
|
446
771
|
this.checked = false;
|
|
447
772
|
// Theme changes must rebuild native parents, including collapsed thinking text.
|
package/src/render-cache.ts
CHANGED
|
@@ -61,6 +61,16 @@ function subscribe(component: Component, changed: () => void): (() => void) | un
|
|
|
61
61
|
const inputs = ["text", "paddingX", "paddingY", "customBgFn", "bgFn", "theme", "cachedText", "cachedWidth", "cachedLines"];
|
|
62
62
|
const shellInputs = ["lastMessage", "args", "result", "isPartial", "expanded", "outputPad",
|
|
63
63
|
"hideComponent", "showImages", "imageWidthCells"];
|
|
64
|
+
/** Push the native shell inputs of one block; offscreen blocks compare these instead of a deep check. */
|
|
65
|
+
export function pushShellInputs(component: Component, into: unknown[]): void {
|
|
66
|
+
for (const key of shellInputs) into.push(Reflect.get(component, key));
|
|
67
|
+
}
|
|
68
|
+
/** Whether two input lists hold the same values in the same order, compared by identity. */
|
|
69
|
+
export function sameValues<T>(a: readonly T[] | undefined, b: readonly T[]): boolean {
|
|
70
|
+
if (!a || a.length !== b.length) return false;
|
|
71
|
+
for (let index = 0; index < a.length; index++) if (a[index] !== b[index]) return false;
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
64
74
|
interface NodeLinks {
|
|
65
75
|
children?: Component[];
|
|
66
76
|
child?: Component;
|
|
@@ -151,18 +161,22 @@ function matches(root: Component, state: State): boolean {
|
|
|
151
161
|
* unobservable components always render. Dispose restores subscribed methods.
|
|
152
162
|
*/
|
|
153
163
|
export class NativeRenderCache {
|
|
154
|
-
private
|
|
164
|
+
private invalidations = 0;
|
|
155
165
|
private outputVersion = 0;
|
|
156
166
|
private imagePlacement = false;
|
|
157
|
-
private readonly changed = (): void => { this.
|
|
167
|
+
private readonly changed = (): void => { this.invalidations++; this.cached = undefined; this.notify?.(); };
|
|
158
168
|
private readonly watched = new Map<Component, () => void>();
|
|
159
169
|
private cached?: { width: number; theme: object; state: State; lines: string[] };
|
|
160
170
|
|
|
161
|
-
|
|
171
|
+
/** notify runs when an observed native node invalidates, before the next render. */
|
|
172
|
+
constructor(private readonly component: Component, private readonly notify?: () => void) {}
|
|
162
173
|
|
|
163
174
|
/** Changes after each native render, even when it reuses a mutable row array. */
|
|
164
175
|
get version(): number { return this.outputVersion; }
|
|
165
176
|
|
|
177
|
+
/** Changes whenever an observed native node invalidates; cheap to compare for offscreen blocks. */
|
|
178
|
+
get revision(): number { return this.invalidations; }
|
|
179
|
+
|
|
166
180
|
/** Whether the latest native output contains placement-dependent image rows. */
|
|
167
181
|
get hasImagePlacement(): boolean { return this.imagePlacement; }
|
|
168
182
|
|
|
@@ -198,7 +212,7 @@ export class NativeRenderCache {
|
|
|
198
212
|
this.cached = undefined;
|
|
199
213
|
const initial = components(this.component);
|
|
200
214
|
const observed = this.watch(initial);
|
|
201
|
-
const before = this.
|
|
215
|
+
const before = this.invalidations;
|
|
202
216
|
const lines = nativeLines(this.component, width);
|
|
203
217
|
this.outputVersion++;
|
|
204
218
|
this.imagePlacement = lines.some(imagePlacement);
|
|
@@ -210,7 +224,7 @@ export class NativeRenderCache {
|
|
|
210
224
|
const observable = stableGraph ? observed && after.every(component =>
|
|
211
225
|
component.invalidate === subscriptions.get(component)?.wrapped) : this.watch(after);
|
|
212
226
|
// A newly created child could invalidate during its first render, before observation.
|
|
213
|
-
if (observable && stableGraph && this.
|
|
227
|
+
if (observable && stableGraph && this.invalidations === before && !this.imagePlacement) {
|
|
214
228
|
this.cached = { width, theme, state: snapshot(this.component, after, links), lines };
|
|
215
229
|
}
|
|
216
230
|
return lines;
|