@bismawy/pi-arnative 0.16.1 → 0.18.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 +25 -16
- package/extensions/index.ts +41 -0
- package/lib/active-clock.ts +91 -0
- package/lib/ansi.ts +40 -78
- package/lib/box.ts +49 -68
- package/lib/chatbox.ts +371 -0
- package/lib/color.ts +3 -31
- package/lib/compat.ts +0 -60
- package/lib/edit-diff.ts +199 -0
- package/{extensions → lib/features}/arnative.ts +96 -423
- package/{extensions/autocompact.ts → lib/features/auto-compact.ts} +30 -327
- package/{extensions → lib/features}/budget.ts +17 -242
- package/lib/features/footer.ts +2232 -0
- package/{extensions/section-headers.ts → lib/features/headers.ts} +38 -49
- package/{extensions → lib/features}/model-heal.ts +5 -28
- package/lib/features/progress-group.ts +1557 -0
- package/{extensions → lib/features}/render-cache.ts +113 -161
- package/{extensions → lib/features}/timestamps.ts +18 -94
- package/{extensions → lib/features}/todos.ts +44 -158
- package/lib/features/tools.ts +1202 -0
- package/lib/features/ui-render-tweaks.ts +606 -0
- package/lib/features/usage.ts +786 -0
- package/lib/format.ts +22 -4
- package/lib/git-status.ts +164 -0
- package/lib/list-picker.ts +1 -127
- package/lib/model-heal.ts +70 -162
- package/lib/patch.ts +0 -63
- package/lib/preset-store.ts +17 -133
- package/lib/provider-store.ts +573 -0
- package/lib/provider.ts +52 -851
- package/lib/runtime.ts +209 -0
- package/lib/shortcuts.ts +8 -58
- package/lib/spinner.ts +88 -162
- package/lib/status-widget.ts +4 -30
- package/lib/todos.ts +20 -143
- package/lib/usage-pricing.ts +143 -0
- package/lib/usage-store.ts +44 -378
- package/package.json +5 -6
- package/themes/arnative-blossom.json +0 -0
- package/themes/arnative-crimson.json +0 -0
- package/themes/arnative-cyberpunk.json +0 -0
- package/themes/arnative-dracula.json +0 -0
- package/themes/arnative-earthy.json +0 -0
- package/themes/arnative-garden.json +0 -0
- package/themes/arnative-gruvbox.json +0 -0
- package/themes/arnative-lemonade.json +0 -0
- package/themes/arnative-magical.json +0 -0
- package/themes/arnative-matrix.json +0 -0
- package/themes/arnative-neutral.json +0 -0
- package/themes/arnative-nord.json +0 -0
- package/themes/arnative-zinc.json +0 -0
- package/themes/arnative.json +0 -0
- package/CHANGELOG.md +0 -683
- package/extensions/animations.ts +0 -242
- package/extensions/footer.ts +0 -2617
- package/extensions/tools.ts +0 -2231
- package/extensions/ui-render-tweaks.ts +0 -1005
- package/extensions/usage-export.ts +0 -283
- package/extensions/usage.ts +0 -1406
- package/lib/check.ts +0 -21
- package/themes/gen-themes.mjs +0 -715
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* - When every child is known and settled, the container checks child state, then
|
|
19
19
|
* reuses its cached `lines` and `mouseLayout` without rebuilding the combined array.
|
|
20
20
|
* - Attaches at the `InteractiveMode.chatContainer` instance layer (not `Container.prototype`),
|
|
21
|
-
* so it never collides with `
|
|
21
|
+
* so it never collides with `headers.ts` or `ui-render-tweaks.ts` prototype
|
|
22
22
|
* patches and sits outside `tools.ts` / `timestamps.ts` render wrappers.
|
|
23
23
|
*/
|
|
24
24
|
import {
|
|
@@ -32,30 +32,32 @@ import {
|
|
|
32
32
|
type ExtensionAPI,
|
|
33
33
|
} from "@earendil-works/pi-coding-agent";
|
|
34
34
|
import { Text } from "@earendil-works/pi-tui";
|
|
35
|
-
import {
|
|
36
|
-
import {
|
|
37
|
-
import {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
import { patchOnce } from "../patch.ts";
|
|
36
|
+
import { RENDER_CACHE_KEY, choiceSetting } from "../preset-store.ts";
|
|
37
|
+
import {
|
|
38
|
+
TOOL_BOX_SCROLL_HOLD_KEY as SCROLL_HOLD_KEY,
|
|
39
|
+
TOOL_BOX_SCROLL_OFFSET_KEY as SCROLL_OFFSET_KEY,
|
|
40
|
+
getRuntime,
|
|
41
|
+
} from "../runtime.ts";
|
|
42
|
+
import {
|
|
43
|
+
activeToolLayout,
|
|
44
|
+
isProgressBox,
|
|
45
|
+
onToolLayoutChange,
|
|
46
|
+
projectTranscriptChildren,
|
|
47
|
+
} from "./progress-group.ts";
|
|
41
48
|
|
|
42
49
|
/** Stored exactly as the picker shows them; `"On"` is the default. */
|
|
43
50
|
export const RENDER_CACHE_CHOICES = ["On", "Off"] as const;
|
|
44
51
|
export type RenderCacheChoice = (typeof RENDER_CACHE_CHOICES)[number];
|
|
45
|
-
|
|
52
|
+
const DEFAULT_RENDER_CACHE: RenderCacheChoice = "On";
|
|
46
53
|
|
|
47
54
|
const RENDER_CACHE_CONFIG_KEY = Symbol.for("pi-arnative.renderCache");
|
|
48
|
-
const RENDER_CACHE_STATE_KEY = Symbol.for("pi-arnative.renderCacheState");
|
|
49
55
|
const INSTANCE_WRAP_KEY = Symbol.for("pi-arnative.renderCacheWrapped");
|
|
50
56
|
const INSTANCE_ORIG_KEY = Symbol.for("pi-arnative.renderCacheOriginals");
|
|
51
57
|
|
|
52
|
-
// Shared symbols from sibling extensions so active animations / scroll-holds bypass cache.
|
|
53
|
-
const THINKING_STATE_KEY = Symbol.for("pi-arnative.thinkingLabelState");
|
|
54
|
-
const SCROLL_OFFSET_KEY = Symbol.for("pi-arnative.toolBoxScrollOffset");
|
|
55
|
-
const SCROLL_HOLD_KEY = Symbol.for("pi-arnative.toolBoxScrollHold");
|
|
56
|
-
|
|
57
58
|
interface ChildMeta {
|
|
58
59
|
version: number;
|
|
60
|
+
interactionVersion: number;
|
|
59
61
|
scrollOffset: number;
|
|
60
62
|
expanded: boolean;
|
|
61
63
|
}
|
|
@@ -65,6 +67,7 @@ interface ChildCacheEntry {
|
|
|
65
67
|
rows: number;
|
|
66
68
|
epoch: number;
|
|
67
69
|
version: number;
|
|
70
|
+
interactionVersion: number;
|
|
68
71
|
scrollOffset: number;
|
|
69
72
|
expanded: boolean;
|
|
70
73
|
lines: string[];
|
|
@@ -97,7 +100,9 @@ const CACHEABLE_CHILD_PROTOTYPES = new Set<object>([
|
|
|
97
100
|
]);
|
|
98
101
|
|
|
99
102
|
function isCacheableChild(child: unknown): boolean {
|
|
100
|
-
|
|
103
|
+
if (!child || typeof child !== "object") return false;
|
|
104
|
+
if (isProgressBox(child)) return true;
|
|
105
|
+
return CACHEABLE_CHILD_PROTOTYPES.has(Object.getPrototypeOf(child));
|
|
101
106
|
}
|
|
102
107
|
|
|
103
108
|
interface RenderCacheState {
|
|
@@ -110,9 +115,9 @@ interface RenderCacheState {
|
|
|
110
115
|
}
|
|
111
116
|
|
|
112
117
|
function getCacheState(): RenderCacheState {
|
|
113
|
-
const
|
|
114
|
-
if (!
|
|
115
|
-
|
|
118
|
+
const rt = getRuntime();
|
|
119
|
+
if (!rt.renderCacheState) {
|
|
120
|
+
rt.renderCacheState = {
|
|
116
121
|
epoch: 0,
|
|
117
122
|
versions: new WeakMap(),
|
|
118
123
|
childCache: new WeakMap(),
|
|
@@ -120,7 +125,7 @@ function getCacheState(): RenderCacheState {
|
|
|
120
125
|
stats: { containerHits: 0, childHits: 0, childRenders: 0 },
|
|
121
126
|
} satisfies RenderCacheState;
|
|
122
127
|
}
|
|
123
|
-
return
|
|
128
|
+
return rt.renderCacheState as RenderCacheState;
|
|
124
129
|
}
|
|
125
130
|
|
|
126
131
|
const renderCacheSetting = choiceSetting(RENDER_CACHE_KEY, RENDER_CACHE_CHOICES, DEFAULT_RENDER_CACHE);
|
|
@@ -186,14 +191,13 @@ function terminalRows(): number {
|
|
|
186
191
|
/** True when a transcript component is currently animating, streaming, or hold-scrolling. */
|
|
187
192
|
export function isDynamicChild(child: unknown): boolean {
|
|
188
193
|
if (!child || typeof child !== "object") return false;
|
|
194
|
+
if (isProgressBox(child)) return child.isDynamic();
|
|
189
195
|
const c = child as Record<string | symbol, unknown>;
|
|
190
196
|
if (c.isStreaming === true) return true;
|
|
191
197
|
if (c.isPartial === true || c.argsPending === true) return true;
|
|
192
198
|
if (c.status === "running") return true;
|
|
193
199
|
if (c[SCROLL_HOLD_KEY] !== undefined) return true;
|
|
194
|
-
const thinking = (
|
|
195
|
-
| { active?: Set<object> }
|
|
196
|
-
| undefined;
|
|
200
|
+
const thinking = getRuntime().thinkingLabelState;
|
|
197
201
|
if (thinking?.active?.has(child)) return true;
|
|
198
202
|
return false;
|
|
199
203
|
}
|
|
@@ -203,7 +207,7 @@ type MarkedWrap = AnyFn & { [INSTANCE_WRAP_KEY]?: true };
|
|
|
203
207
|
|
|
204
208
|
/**
|
|
205
209
|
* Wrap an instance method without touching the prototype chain, so prototype-level
|
|
206
|
-
* patches from sibling extensions (`tools.ts`, `timestamps.ts`, `
|
|
210
|
+
* patches from sibling extensions (`tools.ts`, `timestamps.ts`, `headers.ts`,
|
|
207
211
|
* `ui-render-tweaks.ts`) continue to run and reinstall cleanly across `/reload`.
|
|
208
212
|
*/
|
|
209
213
|
function wrapInstanceMethod(
|
|
@@ -295,17 +299,78 @@ export interface ChatContainerLike {
|
|
|
295
299
|
handleMouse?: (event: unknown) => unknown;
|
|
296
300
|
}
|
|
297
301
|
|
|
302
|
+
/** 32-bit FNV-1a mix (odd multiplier = bijective mod 2^32, so early values never shift out). */
|
|
303
|
+
function mixHash(hash: number, value: number): number {
|
|
304
|
+
return Math.imul(hash ^ (value | 0), 0x01000193) | 0;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function childMetaOf(child: unknown, state: RenderCacheState): ChildMeta {
|
|
308
|
+
if (!child || typeof child !== "object") {
|
|
309
|
+
return { version: 0, interactionVersion: 0, scrollOffset: 0, expanded: false };
|
|
310
|
+
}
|
|
311
|
+
if (isProgressBox(child)) {
|
|
312
|
+
const items = child.getItems();
|
|
313
|
+
let hash = mixHash(0x811c9dc5 | 0, items.length);
|
|
314
|
+
let expanded = false;
|
|
315
|
+
for (const item of items) {
|
|
316
|
+
if (item && typeof item === "object") {
|
|
317
|
+
attachChildHooks(item);
|
|
318
|
+
const itemVer = state.versions.get(item) ?? 0;
|
|
319
|
+
const itemExp = Boolean((item as { expanded?: boolean }).expanded);
|
|
320
|
+
if (itemExp) expanded = true;
|
|
321
|
+
hash = mixHash(hash, itemVer);
|
|
322
|
+
hash = mixHash(hash, itemExp ? 1 : 0);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return {
|
|
326
|
+
version: hash,
|
|
327
|
+
interactionVersion: child.getInteractionVersion(),
|
|
328
|
+
scrollOffset: child.getScrollSignature(),
|
|
329
|
+
expanded: expanded || items.length > 6,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
const cObj = child as Record<string | symbol, unknown>;
|
|
333
|
+
return {
|
|
334
|
+
version: state.versions.get(cObj) ?? 0,
|
|
335
|
+
interactionVersion: 0,
|
|
336
|
+
scrollOffset: Number(cObj[SCROLL_OFFSET_KEY] ?? 0),
|
|
337
|
+
expanded: Boolean(cObj.expanded),
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function renderProjectedDirect(container: ChatContainerLike, children: readonly unknown[], width: number): string[] {
|
|
342
|
+
const lines: string[] = [];
|
|
343
|
+
const mouseChildren: Array<{ component: unknown; height: number }> = [];
|
|
344
|
+
const snapshot: unknown[] = new Array(children.length);
|
|
345
|
+
const parts: Array<readonly string[]> = new Array(children.length);
|
|
346
|
+
for (let i = 0; i < children.length; i++) {
|
|
347
|
+
const child = children[i] as { render?: (w: number) => readonly string[] } | null | undefined;
|
|
348
|
+
snapshot[i] = child;
|
|
349
|
+
const childLines = child && typeof child.render === "function" ? child.render(width) : [];
|
|
350
|
+
parts[i] = childLines;
|
|
351
|
+
mouseChildren.push({ component: child, height: childLines.length });
|
|
352
|
+
for (let j = 0; j < childLines.length; j++) lines.push(childLines[j]!);
|
|
353
|
+
}
|
|
354
|
+
container.mouseLayout = { width, children: mouseChildren };
|
|
355
|
+
container.childLines = { width, children: snapshot, parts, lines };
|
|
356
|
+
return lines;
|
|
357
|
+
}
|
|
358
|
+
|
|
298
359
|
function renderCachedChatContainer(
|
|
299
360
|
container: ChatContainerLike,
|
|
300
361
|
width: number,
|
|
301
362
|
callNextRender: (self: any, args: unknown[]) => unknown,
|
|
302
363
|
): string[] {
|
|
364
|
+
const rawChildren = Array.isArray(container.children) ? container.children : [];
|
|
365
|
+
const grouped = activeToolLayout() === "Grouped";
|
|
366
|
+
const children = grouped ? projectTranscriptChildren(rawChildren) : rawChildren;
|
|
367
|
+
|
|
303
368
|
if (activeRenderCache() !== "On") {
|
|
304
|
-
return callNextRender(container, [width]) as string[];
|
|
369
|
+
if (!grouped) return callNextRender(container, [width]) as string[];
|
|
370
|
+
return renderProjectedDirect(container, children, width);
|
|
305
371
|
}
|
|
306
372
|
|
|
307
373
|
const state = getCacheState();
|
|
308
|
-
const children = Array.isArray(container.children) ? container.children : [];
|
|
309
374
|
const rows = terminalRows();
|
|
310
375
|
const containerVer = state.versions.get(container) ?? 0;
|
|
311
376
|
|
|
@@ -326,12 +391,13 @@ function renderCachedChatContainer(
|
|
|
326
391
|
allClean = false;
|
|
327
392
|
break;
|
|
328
393
|
}
|
|
329
|
-
const
|
|
394
|
+
const cur = childMetaOf(child, state);
|
|
330
395
|
const meta = cached.childMeta[i]!;
|
|
331
396
|
if (
|
|
332
|
-
meta.version !==
|
|
333
|
-
meta.
|
|
334
|
-
meta.
|
|
397
|
+
meta.version !== cur.version ||
|
|
398
|
+
meta.interactionVersion !== cur.interactionVersion ||
|
|
399
|
+
meta.scrollOffset !== cur.scrollOffset ||
|
|
400
|
+
meta.expanded !== cur.expanded
|
|
335
401
|
) {
|
|
336
402
|
allClean = false;
|
|
337
403
|
break;
|
|
@@ -358,17 +424,16 @@ function renderCachedChatContainer(
|
|
|
358
424
|
snapshot[i] = child;
|
|
359
425
|
if (!child || typeof child.render !== "function") {
|
|
360
426
|
parts[i] = [];
|
|
361
|
-
childMeta[i] = { version: 0, scrollOffset: 0, expanded: false };
|
|
427
|
+
childMeta[i] = { version: 0, interactionVersion: 0, scrollOffset: 0, expanded: false };
|
|
362
428
|
continue;
|
|
363
429
|
}
|
|
364
|
-
attachChildHooks(child);
|
|
430
|
+
if (!isProgressBox(child)) attachChildHooks(child);
|
|
365
431
|
|
|
366
432
|
const isObj = typeof child === "object";
|
|
367
|
-
const
|
|
368
|
-
const
|
|
369
|
-
const expanded = Boolean(isObj && child.expanded);
|
|
433
|
+
const curMeta = childMetaOf(child, state);
|
|
434
|
+
const { version: ver, interactionVersion, scrollOffset, expanded } = curMeta;
|
|
370
435
|
if (expanded) hasExpanded = true;
|
|
371
|
-
childMeta[i] =
|
|
436
|
+
childMeta[i] = curMeta;
|
|
372
437
|
|
|
373
438
|
let childLines: readonly string[];
|
|
374
439
|
if (!isCacheableChild(child) || isDynamicChild(child)) {
|
|
@@ -383,6 +448,7 @@ function renderCachedChatContainer(
|
|
|
383
448
|
(entry) =>
|
|
384
449
|
entry.epoch === state.epoch &&
|
|
385
450
|
entry.version === ver &&
|
|
451
|
+
entry.interactionVersion === interactionVersion &&
|
|
386
452
|
entry.scrollOffset === scrollOffset &&
|
|
387
453
|
entry.expanded === expanded &&
|
|
388
454
|
(!expanded || entry.rows === rows),
|
|
@@ -403,6 +469,7 @@ function renderCachedChatContainer(
|
|
|
403
469
|
rows,
|
|
404
470
|
epoch: state.epoch,
|
|
405
471
|
version: ver,
|
|
472
|
+
interactionVersion,
|
|
406
473
|
scrollOffset,
|
|
407
474
|
expanded,
|
|
408
475
|
lines: [...childLines],
|
|
@@ -410,6 +477,7 @@ function renderCachedChatContainer(
|
|
|
410
477
|
(entry) =>
|
|
411
478
|
entry.epoch === state.epoch &&
|
|
412
479
|
entry.version === ver &&
|
|
480
|
+
entry.interactionVersion === interactionVersion &&
|
|
413
481
|
entry.scrollOffset === scrollOffset &&
|
|
414
482
|
entry.expanded === expanded,
|
|
415
483
|
);
|
|
@@ -480,6 +548,10 @@ export function attachChatContainer(container: unknown): void {
|
|
|
480
548
|
}
|
|
481
549
|
if (typeof obj.handleMouse === "function") {
|
|
482
550
|
wrapInstanceMethod(obj, "handleMouse", (callNext) => function (this: any, ...args: unknown[]) {
|
|
551
|
+
const ev = args[0] as { width?: number } | undefined;
|
|
552
|
+
if (ev && typeof ev.width === "number" && this.childLines?.width !== ev.width && typeof this.render === "function") {
|
|
553
|
+
this.render(ev.width);
|
|
554
|
+
}
|
|
483
555
|
const res = callNext(this, args);
|
|
484
556
|
if (res !== undefined) bumpVersion(this);
|
|
485
557
|
return res;
|
|
@@ -487,6 +559,8 @@ export function attachChatContainer(container: unknown): void {
|
|
|
487
559
|
}
|
|
488
560
|
}
|
|
489
561
|
|
|
562
|
+
onToolLayoutChange(clearRenderCache);
|
|
563
|
+
|
|
490
564
|
// Install InteractiveMode hooks so `this.chatContainer` is attached on startup,
|
|
491
565
|
// `/reload`, session switch, and any incoming chat/tool event.
|
|
492
566
|
if (InteractiveMode?.prototype) {
|
|
@@ -524,129 +598,7 @@ export default function registerRenderCache(pi: ExtensionAPI): void {
|
|
|
524
598
|
});
|
|
525
599
|
}
|
|
526
600
|
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
delete (globalThis as Record<symbol, unknown>)[RENDER_CACHE_CONFIG_KEY];
|
|
532
|
-
resetPresetCache();
|
|
533
|
-
|
|
534
|
-
// Choice persistence & default.
|
|
535
|
-
assert(activeRenderCache() === "On", "the default is On");
|
|
536
|
-
setRenderCache("Off");
|
|
537
|
-
assert(activeRenderCache() === "Off", "Off persists immediately");
|
|
538
|
-
assert(loadChoice(RENDER_CACHE_KEY, RENDER_CACHE_CHOICES) === "Off", "Off survives reload from disk");
|
|
539
|
-
setRenderCache("On");
|
|
540
|
-
assert(activeRenderCache() === "On", "switching back to On restores caching");
|
|
541
|
-
|
|
542
|
-
// Use a known Text prototype to exercise cache policy without loading a theme.
|
|
543
|
-
function makeChild(text: string) {
|
|
544
|
-
const child = Object.assign(new Text(text, 0, 0), { renders: 0, isStreaming: false, isPartial: false, expanded: false });
|
|
545
|
-
const setText = child.setText.bind(child);
|
|
546
|
-
child.setText = (next: string): void => { text = next; setText(next); };
|
|
547
|
-
const result = Object.assign(child, {
|
|
548
|
-
handleInput(key: string): void { child.setText(`${text}:${key}`); },
|
|
549
|
-
});
|
|
550
|
-
result.render = (width: number): string[] => { result.renders += 1; return [`${text}@${width}`]; };
|
|
551
|
-
return result as typeof result & { [key: symbol]: unknown };
|
|
552
|
-
}
|
|
553
|
-
type FakeChild = ReturnType<typeof makeChild>;
|
|
554
|
-
class FakeContainer implements ChatContainerLike {
|
|
555
|
-
children: FakeChild[] = [];
|
|
556
|
-
mouseLayout?: { width: number; children: Array<{ component: unknown; height: number }> };
|
|
557
|
-
addChild(c: FakeChild): void {
|
|
558
|
-
this.children.push(c);
|
|
559
|
-
}
|
|
560
|
-
invalidate(): void {
|
|
561
|
-
for (const c of this.children) c.invalidate();
|
|
562
|
-
}
|
|
563
|
-
render(width: number): string[] {
|
|
564
|
-
const lines: string[] = [];
|
|
565
|
-
const mouseChildren: Array<{ component: unknown; height: number }> = [];
|
|
566
|
-
for (const c of this.children) {
|
|
567
|
-
const out = c.render(width);
|
|
568
|
-
mouseChildren.push({ component: c, height: out.length });
|
|
569
|
-
lines.push(...out);
|
|
570
|
-
}
|
|
571
|
-
this.mouseLayout = { width, children: mouseChildren };
|
|
572
|
-
return lines;
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
const container = new FakeContainer();
|
|
577
|
-
const c1 = makeChild("msg1");
|
|
578
|
-
const c2 = makeChild("msg2");
|
|
579
|
-
container.addChild(c1);
|
|
580
|
-
container.addChild(c2);
|
|
581
|
-
attachChatContainer(container);
|
|
582
|
-
|
|
583
|
-
const out1 = container.render(80);
|
|
584
|
-
assert(out1.join(",") === "msg1@80,msg2@80", "first render outputs both children");
|
|
585
|
-
assert(c1.renders === 1 && c2.renders === 1, "first render calls each child once");
|
|
586
|
-
|
|
587
|
-
// Second render at same width hits whole container cache: zero child.render calls.
|
|
588
|
-
const out2 = container.render(80);
|
|
589
|
-
assert(out2 === out1, "whole-container hit returns cached array reference");
|
|
590
|
-
assert(c1.renders === 1 && c2.renders === 1, "container hit skips child renders");
|
|
591
|
-
assert(container.mouseLayout?.children.length === 2, "mouseLayout preserved on container hit");
|
|
592
|
-
assert(
|
|
593
|
-
container.childLines?.width === 80 &&
|
|
594
|
-
container.childLines.children.length === 2 &&
|
|
595
|
-
container.childLines.parts.length === 2 &&
|
|
596
|
-
container.childLines.lines === out2,
|
|
597
|
-
"Pi 1.6.0+ childLines preserved on container hit",
|
|
598
|
-
);
|
|
599
|
-
|
|
600
|
-
// Streaming child bypasses cache while settled sibling stays cached.
|
|
601
|
-
c2.isStreaming = true;
|
|
602
|
-
c2.setText("streaming");
|
|
603
|
-
const out3 = container.render(80);
|
|
604
|
-
assert(out3.join(",") === "msg1@80,streaming@80", "streaming child updates live");
|
|
605
|
-
assert(c1.renders === 1 && c2.renders === 2, "settled sibling stayed cached while streaming child re-rendered");
|
|
606
|
-
|
|
607
|
-
c2.isStreaming = false;
|
|
608
|
-
const out4 = container.render(80);
|
|
609
|
-
assert(out4.join(",") === "msg1@80,streaming@80", "post-stream render caches final state");
|
|
610
|
-
assert(c1.renders === 1 && c2.renders === 3, "settled after stream renders once then caches");
|
|
611
|
-
container.render(80);
|
|
612
|
-
assert(c1.renders === 1 && c2.renders === 3, "steady state hits container cache again");
|
|
613
|
-
|
|
614
|
-
// Width change renders unseen width; toggling back to previous width hits 2-width LRU in O(1).
|
|
615
|
-
const out5 = container.render(100);
|
|
616
|
-
assert(out5.join(",") === "msg1@100,streaming@100", "width change re-renders at new width");
|
|
617
|
-
assert(c1.renders === 2 && c2.renders === 4, "both re-rendered for new width");
|
|
618
|
-
const out5Back = container.render(80);
|
|
619
|
-
assert(out5Back === out4, "toggling back to prior width hits 2-width container LRU");
|
|
620
|
-
assert(c1.renders === 2 && c2.renders === 4, "toggling back skips child renders");
|
|
621
|
-
container.render(100);
|
|
622
|
-
|
|
623
|
-
// Mutation on one child invalidates only that child.
|
|
624
|
-
c1.setText("edited");
|
|
625
|
-
const out6 = container.render(100);
|
|
626
|
-
assert(out6.join(",") === "edited@100,streaming@100", "mutated child re-renders");
|
|
627
|
-
assert(c1.renders === 3 && c2.renders === 4, "unchanged sibling stayed cached");
|
|
628
|
-
|
|
629
|
-
// handleInput mutation invalidates that child without needing explicit invalidate().
|
|
630
|
-
c1.handleInput("x");
|
|
631
|
-
const out6Input = container.render(100);
|
|
632
|
-
assert(out6Input.join(",") === "edited:x@100,streaming@100", "handleInput invalidates mutated child");
|
|
633
|
-
assert(c1.renders === 4 && c2.renders === 4, "handleInput did not invalidate sibling");
|
|
634
|
-
|
|
635
|
-
// Tool box scroll-offset change invalidates without explicit invalidate().
|
|
636
|
-
c2[SCROLL_OFFSET_KEY] = 5;
|
|
637
|
-
container.render(100);
|
|
638
|
-
assert(c1.renders === 4 && c2.renders === 5, "scrollOffset change invalidates that tool box");
|
|
639
|
-
|
|
640
|
-
// Off mode disables caching completely.
|
|
641
|
-
setRenderCache("Off");
|
|
642
|
-
container.render(100);
|
|
643
|
-
container.render(100);
|
|
644
|
-
assert(c1.renders === 6 && c2.renders === 7, "Off mode renders directly every frame");
|
|
645
|
-
setRenderCache("On");
|
|
646
|
-
|
|
647
|
-
rmSync(fixture, { recursive: true, force: true });
|
|
648
|
-
delete process.env.PI_CODING_AGENT_DIR;
|
|
649
|
-
delete (globalThis as Record<symbol, unknown>)[RENDER_CACHE_CONFIG_KEY];
|
|
650
|
-
resetPresetCache();
|
|
651
|
-
console.log("render-cache.ts self-check OK");
|
|
652
|
-
}
|
|
601
|
+
export const __test = {
|
|
602
|
+
RENDER_CACHE_CONFIG_KEY,
|
|
603
|
+
SCROLL_OFFSET_KEY,
|
|
604
|
+
};
|
|
@@ -12,22 +12,13 @@
|
|
|
12
12
|
import type { AssistantMessage } from "@earendil-works/pi-ai";
|
|
13
13
|
import { AssistantMessageComponent, UserMessageComponent, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
14
14
|
import { sliceByColumn, visibleWidth } from "@earendil-works/pi-tui";
|
|
15
|
-
import { ansiBgOpen, stripAnsi, themeOf } from "../
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
15
|
+
import { ansiBgOpen, getActiveTheme, setActiveTheme, stripAnsi, themeOf } from "../ansi.ts";
|
|
16
|
+
import { markPatched, originalOf } from "../patch.ts";
|
|
17
|
+
import { getRuntime } from "../runtime.ts";
|
|
18
18
|
|
|
19
|
-
// Shared across reloads
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
// the closure on stale data — and a captured ctx even on an invalidated one (pi throws
|
|
23
|
-
// "stale after session replacement or reload" inside the render pass -> uncaughtException).
|
|
24
|
-
type ThemeProxy = { fg(color: string, text: string): string; bg?(color: string, text: string): string } | null;
|
|
25
|
-
const TS_STATE_KEY = Symbol.for("pi-arnative.chatTimestampState");
|
|
26
|
-
const tsState = ((globalThis as Record<symbol, unknown>)[TS_STATE_KEY] ??= {
|
|
27
|
-
map: new Map<string, number>(),
|
|
28
|
-
theme: null as ThemeProxy,
|
|
29
|
-
}) as { map: Map<string, number>; theme: ThemeProxy };
|
|
30
|
-
const USER_TIMESTAMPS_MAP = tsState.map;
|
|
19
|
+
// Shared across reloads via RuntimeState so prototype patches and newly registered
|
|
20
|
+
// handlers read the same map and shared active theme.
|
|
21
|
+
const USER_TIMESTAMPS_MAP = (getRuntime().userTimestamps ??= new Map<string, number>());
|
|
31
22
|
|
|
32
23
|
function userTextOf(message: { content?: unknown }): string {
|
|
33
24
|
const c = message.content;
|
|
@@ -61,7 +52,7 @@ function formatClock(timestamp?: number): string {
|
|
|
61
52
|
return `${h}:${m}`;
|
|
62
53
|
}
|
|
63
54
|
|
|
64
|
-
|
|
55
|
+
function formatTimeBadge(timeStr: string, th: { fg(color: string, text: string): string } | null): string {
|
|
65
56
|
if (th) {
|
|
66
57
|
try {
|
|
67
58
|
return th.fg("accent", timeStr);
|
|
@@ -195,10 +186,11 @@ if (UserMessageComponent?.prototype?.render || AssistantMessageComponent?.protot
|
|
|
195
186
|
const source = origRender.call(this, width);
|
|
196
187
|
if (source.length < 2) return source;
|
|
197
188
|
|
|
198
|
-
const
|
|
189
|
+
const activeTheme = getActiveTheme<ThemeProxy>();
|
|
190
|
+
const bgOpen = ansiBgOpen(activeTheme, "userMessageBg");
|
|
199
191
|
const ts = resolveUserTimestamp(this as { text?: string; timestamp?: string | number });
|
|
200
192
|
const timeStr = ts ? formatClock(ts) : "";
|
|
201
|
-
const badge = timeStr ? formatTimeBadge(timeStr,
|
|
193
|
+
const badge = timeStr ? formatTimeBadge(timeStr, activeTheme) : "";
|
|
202
194
|
|
|
203
195
|
const prev = userRenderCache.get(this);
|
|
204
196
|
if (prev && prev.source === source && prev.width === width && prev.bgOpen === bgOpen && prev.badge === badge) {
|
|
@@ -254,7 +246,7 @@ if (UserMessageComponent?.prototype?.render || AssistantMessageComponent?.protot
|
|
|
254
246
|
if (!hasText || source.length === 0) return source;
|
|
255
247
|
|
|
256
248
|
const timeStr = formatClock(lastMsg?.timestamp);
|
|
257
|
-
const badge = formatTimeBadge(timeStr,
|
|
249
|
+
const badge = formatTimeBadge(timeStr, getActiveTheme<ThemeProxy>());
|
|
258
250
|
const prev = assistantRenderCache.get(this);
|
|
259
251
|
if (prev && prev.source === source && prev.width === width && prev.badge === badge) {
|
|
260
252
|
return prev.lines;
|
|
@@ -319,82 +311,14 @@ export default function (pi: ExtensionAPI) {
|
|
|
319
311
|
// manager already holds the restored history when session_start fires (all reasons:
|
|
320
312
|
// startup/resume/fork/new/reload).
|
|
321
313
|
pi.on("session_start", async (_event, ctx) => {
|
|
322
|
-
|
|
314
|
+
const th = themeOf<ThemeProxy>(ctx);
|
|
315
|
+
if (th) setActiveTheme(th);
|
|
323
316
|
prefillUserTimestamps(ctx.sessionManager.getBranch());
|
|
324
317
|
});
|
|
325
318
|
}
|
|
326
319
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
// Clock flush right with a 1-space margin
|
|
333
|
-
const res1 = placeTimeAtRight("Short text", 30, badge, badgeW, "");
|
|
334
|
-
assert(visibleWidth(res1) === 30, "line length matches the terminal width");
|
|
335
|
-
assert(res1.endsWith(badge + " "), "badge sits 1 space from the right edge");
|
|
336
|
-
|
|
337
|
-
// With the user bubble background
|
|
338
|
-
const bg = "\x1b[48;2;52;53;61m";
|
|
339
|
-
const res2 = placeTimeAtRight(`${bg}Short text`, 30, badge, badgeW, bg);
|
|
340
|
-
assert(res2.includes(badge + " \x1b[49m"), "hours, margin space, then bg reset");
|
|
341
|
-
assert(res2.endsWith("\x1b[49m"), "background closed at end of line");
|
|
342
|
-
assert(visibleWidth(res2) === 30, "line length still matches the width");
|
|
343
|
-
|
|
344
|
-
// bubble bg repair: padding after [0m] must be bg'd, line closed with [49m], and idempotent
|
|
345
|
-
const dirty = `${bg}Hello\x1b[0m${" ".repeat(5)}\x1b[49m`;
|
|
346
|
-
const clean = repairBubbleBg(dirty, bg);
|
|
347
|
-
assert(clean.includes(`\x1b[0m${bg}`), "bg re-applied after the reset");
|
|
348
|
-
assert(clean.endsWith("\x1b[49m"), "bubble bg closed at end of line");
|
|
349
|
-
assert(repairBubbleBg(clean, bg) === clean, "repairBubbleBg is idempotent across repeated frames");
|
|
350
|
-
assert(repairBubbleBg("plain", "") === "plain", "no bgOpen: unchanged");
|
|
351
|
-
|
|
352
|
-
// memoized repair: a different bg code resets the cache, so nothing repairs
|
|
353
|
-
// against a stale bubble background after a theme switch
|
|
354
|
-
const dirty2 = `${bg}Second\x1b[0m pad \x1b[49m`;
|
|
355
|
-
const otherBg = "\x1b[48;2;1;2;3m";
|
|
356
|
-
assert(repairBubbleBg(dirty2, otherBg).includes(`\x1b[0m${otherBg}`), "a new bg code repairs against that bg");
|
|
357
|
-
assert(repairBubbleBg(dirty2, bg).includes(`\x1b[0m${bg}`), "and back to the previous bg after another reset");
|
|
358
|
-
assert(repairBubbleBg(dirty2, bg) === repairBubbleBg(dirty2, bg), "repeat repair stays stable");
|
|
359
|
-
assert(ansiBgOpen(null, "userMessageBg") === "", "no theme: empty bgOpen");
|
|
360
|
-
assert(
|
|
361
|
-
ansiBgOpen({ bg: (_c, t) => `\x1b[48;2;52;53;61m${t}\x1b[49m` }, "userMessageBg") === "\x1b[48;2;52;53;61m",
|
|
362
|
-
"bgOpen taken from the theme probe",
|
|
363
|
-
);
|
|
364
|
-
|
|
365
|
-
// Long text: sliceByColumn keeps the width intact (bg branch: badge area carries the bubble bg)
|
|
366
|
-
const bgOpenTest = "\x1b[48;2;52;53;61m";
|
|
367
|
-
const res3 = placeTimeAtRight("a".repeat(40), 30, badge, badgeW, bgOpenTest);
|
|
368
|
-
assert(visibleWidth(res3) === 30, "long text sliced exactly at targetCol");
|
|
369
|
-
assert(res3.includes(bgOpenTest), "bg branch: bubble bg applied in the badge area");
|
|
370
|
-
|
|
371
|
-
// History prefill: old user messages get their real clock on restore
|
|
372
|
-
assert(userTextOf({ content: "plain" }) === "plain", "userTextOf string content");
|
|
373
|
-
assert(userTextOf({ content: [{ type: "text", text: "a" }, { type: "text", text: "b" }] }) === "ab", "userTextOf text parts joined");
|
|
374
|
-
prefillUserTimestamps([
|
|
375
|
-
{ type: "message", timestamp: "2026-01-02T03:04:05.000Z", message: { role: "user", content: "old question", timestamp: 1000 } },
|
|
376
|
-
{ type: "message", timestamp: "not-a-date", message: { role: "user", content: [{ type: "text", text: "old reply" }] } },
|
|
377
|
-
]);
|
|
378
|
-
assert(USER_TIMESTAMPS_MAP.get("old question") === 1000, "prefill stores the message timestamp");
|
|
379
|
-
assert((USER_TIMESTAMPS_MAP.get("old reply") ?? 0) > 0, "prefill falls back to the entry timestamp");
|
|
380
|
-
|
|
381
|
-
// Reload invariant: /reload re-imports the module while the prototype patch below keeps
|
|
382
|
-
// running, so the fresh handlers MUST write into the same map/theme the patched render reads.
|
|
383
|
-
const reloaded = (await import(`${import.meta.url}?reload=1`)) as typeof import("./timestamps.ts");
|
|
384
|
-
const handlers: Record<string, (event: unknown, ctx: unknown) => unknown> = {};
|
|
385
|
-
const theme = { fg: (_c: string, t: string) => t };
|
|
386
|
-
reloaded.default({ on: (ev: string, h: never) => void (handlers[ev] = h) } as never);
|
|
387
|
-
await handlers.session_start(
|
|
388
|
-
{},
|
|
389
|
-
{
|
|
390
|
-
sessionManager: {
|
|
391
|
-
getBranch: () => [{ timestamp: "", message: { role: "user", content: "after reload", timestamp: 4242 } }],
|
|
392
|
-
},
|
|
393
|
-
ui: { theme },
|
|
394
|
-
},
|
|
395
|
-
);
|
|
396
|
-
assert(USER_TIMESTAMPS_MAP.get("after reload") === 4242, "reloaded module prefills the shared map read by the patched render");
|
|
397
|
-
assert(!USER_TIMESTAMPS_MAP.has("old question"), "session_start clears the map first (bounded growth, current session only)");
|
|
398
|
-
assert(tsState.theme === theme, "reloaded module shares the theme read by the patched render");
|
|
399
|
-
console.log("timestamps.ts self-check OK");
|
|
400
|
-
}
|
|
320
|
+
export const __test = {
|
|
321
|
+
USER_TIMESTAMPS_MAP,
|
|
322
|
+
userTextOf,
|
|
323
|
+
prefillUserTimestamps,
|
|
324
|
+
};
|