@pi-kaush/pi-content-layout 0.1.1 → 0.1.2
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/package.json +1 -1
- package/src/index.ts +130 -5
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -6,7 +6,12 @@ import {
|
|
|
6
6
|
type Theme,
|
|
7
7
|
UserMessageComponent,
|
|
8
8
|
} from "@earendil-works/pi-coding-agent";
|
|
9
|
-
import
|
|
9
|
+
import {
|
|
10
|
+
type EditorComponent,
|
|
11
|
+
type EditorTheme,
|
|
12
|
+
Loader,
|
|
13
|
+
type TUI,
|
|
14
|
+
} from "@earendil-works/pi-tui";
|
|
10
15
|
import {
|
|
11
16
|
chatContainerHooks,
|
|
12
17
|
runChatContainerHooks,
|
|
@@ -25,6 +30,9 @@ const USER_RENDER_PATCH = Symbol.for("kg.pi.contentLayout.userRender.v1");
|
|
|
25
30
|
const SYSTEM_CONTAINER_RENDER_PATCH = Symbol.for(
|
|
26
31
|
"kg.pi.contentLayout.systemContainerRender.v1",
|
|
27
32
|
);
|
|
33
|
+
const STATUS_LOADER_RENDER_PATCH = Symbol.for(
|
|
34
|
+
"kg.pi.contentLayout.statusLoaderRender.v1",
|
|
35
|
+
);
|
|
28
36
|
|
|
29
37
|
type EditorFactory = (
|
|
30
38
|
tui: TUI,
|
|
@@ -49,10 +57,54 @@ type NativeTextRow = RenderRow & {
|
|
|
49
57
|
customBgFn?: unknown;
|
|
50
58
|
};
|
|
51
59
|
|
|
60
|
+
type LoaderRow = RenderRow & {
|
|
61
|
+
kind?: unknown;
|
|
62
|
+
paddingX?: unknown;
|
|
63
|
+
};
|
|
64
|
+
|
|
52
65
|
type RenderPatchState = {
|
|
53
66
|
originalRender: (this: RenderRow, width: number) => string[];
|
|
54
67
|
patchedRender: (this: RenderRow, width: number) => string[];
|
|
55
68
|
owners: Map<object, ThemeGetter>;
|
|
69
|
+
cache: WeakMap<object, RenderCacheEntry>;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
type RenderCacheEntry = {
|
|
73
|
+
width: number;
|
|
74
|
+
src: string[];
|
|
75
|
+
lines: string[];
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// ==================================================================
|
|
79
|
+
// Render memoization — PLEASE DO NOT REMOVE OR "SIMPLIFY" THIS CACHE.
|
|
80
|
+
// ==================================================================
|
|
81
|
+
//
|
|
82
|
+
// Pi's TUI re-renders the ENTIRE transcript on every keystroke (the frame
|
|
83
|
+
// walk is O(session) in pi-tui 0.84.x; upstream viewport-windowing work is
|
|
84
|
+
// not merged yet). Components normally stay fast because leaf components
|
|
85
|
+
// reuse cached line strings until their content changes. This package,
|
|
86
|
+
// however, decorates message lines AFTER rendering — building brand-new
|
|
87
|
+
// strings (insets, rails, backgrounds) for every line of every message on
|
|
88
|
+
// every frame. Measured on a ~1,000-message session that cost ~90 ms per
|
|
89
|
+
// keystroke and made the input box visibly lag as sessions grew.
|
|
90
|
+
//
|
|
91
|
+
// The cache below makes decoration cost O(changed messages) instead of
|
|
92
|
+
// O(all messages): decorators compare the freshly rendered source lines
|
|
93
|
+
// against the lines they decorated last frame BY IDENTITY (=== per element)
|
|
94
|
+
// and reuse the decorated output when nothing changed. Identity comparison
|
|
95
|
+
// is what makes the cache self-invalidating across every change path —
|
|
96
|
+
// streaming chunks, content edits, theme switches — because all of those
|
|
97
|
+
// regenerate the source strings upstream of us. This is why there is no
|
|
98
|
+
// explicit invalidate wiring here: partial invalidation would be worse than
|
|
99
|
+
// none, and identity checks can never miss a real change.
|
|
100
|
+
//
|
|
101
|
+
// If typing ever gets slow again in long sessions, suspect this cache first.
|
|
102
|
+
// ==================================================================
|
|
103
|
+
type RenderMemo = {
|
|
104
|
+
/** Decorated lines from the last frame, when `src` is unchanged. */
|
|
105
|
+
hit(src: string[]): string[] | undefined;
|
|
106
|
+
/** Record decorated output keyed by the source lines that produced it. */
|
|
107
|
+
store(src: string[], out: string[]): void;
|
|
56
108
|
};
|
|
57
109
|
|
|
58
110
|
type RenderDecorator = (
|
|
@@ -60,6 +112,7 @@ type RenderDecorator = (
|
|
|
60
112
|
row: RenderRow,
|
|
61
113
|
width: number,
|
|
62
114
|
theme: Theme,
|
|
115
|
+
memo: RenderMemo,
|
|
63
116
|
) => string[];
|
|
64
117
|
|
|
65
118
|
type PatchableRenderPrototype = RenderRow &
|
|
@@ -87,14 +140,32 @@ function installRenderPatch(
|
|
|
87
140
|
const state = {} as RenderPatchState;
|
|
88
141
|
state.originalRender = prototype.render;
|
|
89
142
|
state.owners = new Map([[owner, getTheme]]);
|
|
143
|
+
state.cache = new WeakMap();
|
|
90
144
|
state.patchedRender = function renderWithContentLayout(
|
|
91
145
|
this: RenderRow,
|
|
92
146
|
width: number,
|
|
93
147
|
): string[] {
|
|
94
148
|
const theme = currentTheme(state);
|
|
95
149
|
if (!theme) return state.originalRender.call(this, width);
|
|
150
|
+
// See the memoization block above RenderMemo: decorators get a memo
|
|
151
|
+
// bound to this row + width so unchanged rows skip string building.
|
|
152
|
+
const entry = state.cache.get(this);
|
|
153
|
+
const memo: RenderMemo = {
|
|
154
|
+
hit: (src: string[]): string[] | undefined => {
|
|
155
|
+
if (!entry || entry.width !== width) return undefined;
|
|
156
|
+
const prev = entry.src;
|
|
157
|
+
if (prev.length !== src.length) return undefined;
|
|
158
|
+
for (let i = 0; i < src.length; i++) {
|
|
159
|
+
if (prev[i] !== src[i]) return undefined;
|
|
160
|
+
}
|
|
161
|
+
return entry.lines;
|
|
162
|
+
},
|
|
163
|
+
store: (src: string[], out: string[]): void => {
|
|
164
|
+
state.cache.set(this, { width, src, lines: out });
|
|
165
|
+
},
|
|
166
|
+
};
|
|
96
167
|
try {
|
|
97
|
-
return decorate(state.originalRender, this, width, theme);
|
|
168
|
+
return decorate(state.originalRender, this, width, theme, memo);
|
|
98
169
|
} catch {
|
|
99
170
|
return state.originalRender.call(this, width);
|
|
100
171
|
}
|
|
@@ -131,6 +202,8 @@ function assistantDecorator(
|
|
|
131
202
|
original: RenderPatchState["originalRender"],
|
|
132
203
|
row: RenderRow,
|
|
133
204
|
width: number,
|
|
205
|
+
_theme: Theme,
|
|
206
|
+
memo: RenderMemo,
|
|
134
207
|
): string[] {
|
|
135
208
|
const nativeInset = Math.max(
|
|
136
209
|
0,
|
|
@@ -139,7 +212,11 @@ function assistantDecorator(
|
|
|
139
212
|
const inset = Math.max(0, contentInset(width) - nativeInset);
|
|
140
213
|
if (inset === 0) return original.call(row, width);
|
|
141
214
|
const lines = original.call(row, Math.max(1, width - inset * 2));
|
|
142
|
-
|
|
215
|
+
const cached = memo.hit(lines);
|
|
216
|
+
if (cached) return cached;
|
|
217
|
+
const decorated = insetLines(lines, width, inset);
|
|
218
|
+
memo.store(lines, decorated);
|
|
219
|
+
return decorated;
|
|
143
220
|
}
|
|
144
221
|
|
|
145
222
|
function isChatMessageChild(child: unknown): boolean {
|
|
@@ -212,10 +289,31 @@ function systemTextHook(
|
|
|
212
289
|
};
|
|
213
290
|
}
|
|
214
291
|
|
|
292
|
+
// Status indicators (Working/Retry/Compaction/BranchSummary) sit in a
|
|
293
|
+
// sibling container that the chat-child hooks never see, so they need their
|
|
294
|
+
// own inset to line up with the chat content. StatusIndicator marks Loader
|
|
295
|
+
// subclasses with a `kind`; bare Loaders inside tool boxes keep native
|
|
296
|
+
// layout.
|
|
297
|
+
function statusLoaderDecorator(
|
|
298
|
+
original: RenderPatchState["originalRender"],
|
|
299
|
+
row: RenderRow,
|
|
300
|
+
width: number,
|
|
301
|
+
): string[] {
|
|
302
|
+
if (typeof (row as LoaderRow).kind !== "string")
|
|
303
|
+
return original.call(row, width);
|
|
304
|
+
const nativeInset = Math.max(0, Number((row as LoaderRow).paddingX) || 0);
|
|
305
|
+
const inset = Math.max(0, contentInset(width) - nativeInset);
|
|
306
|
+
if (inset === 0) return original.call(row, width);
|
|
307
|
+
const lines = original.call(row, Math.max(1, width - inset * 2));
|
|
308
|
+
return insetLines(lines, width, inset);
|
|
309
|
+
}
|
|
310
|
+
|
|
215
311
|
function systemContainerDecorator(
|
|
216
312
|
original: RenderPatchState["originalRender"],
|
|
217
313
|
row: RenderRow,
|
|
218
314
|
width: number,
|
|
315
|
+
_theme: Theme,
|
|
316
|
+
memo: RenderMemo,
|
|
219
317
|
): string[] {
|
|
220
318
|
const restore = runChatContainerHooks(
|
|
221
319
|
row,
|
|
@@ -223,7 +321,14 @@ function systemContainerDecorator(
|
|
|
223
321
|
width,
|
|
224
322
|
);
|
|
225
323
|
try {
|
|
226
|
-
|
|
324
|
+
const lines = original.call(row, width);
|
|
325
|
+
// Decoration happens inside the children during the original call, so
|
|
326
|
+
// the decorated output IS the source output; cache it the same way so
|
|
327
|
+
// downstream containers reuse the array instead of re-concatenating.
|
|
328
|
+
const cached = memo.hit(lines);
|
|
329
|
+
if (cached) return cached;
|
|
330
|
+
memo.store(lines, lines);
|
|
331
|
+
return lines;
|
|
227
332
|
} finally {
|
|
228
333
|
restore();
|
|
229
334
|
}
|
|
@@ -234,12 +339,17 @@ function userDecorator(
|
|
|
234
339
|
row: RenderRow,
|
|
235
340
|
width: number,
|
|
236
341
|
theme: Theme,
|
|
342
|
+
memo: RenderMemo,
|
|
237
343
|
): string[] {
|
|
238
344
|
const inset = contentInset(width);
|
|
239
345
|
const bodyWidth = width - inset * 2 - 1;
|
|
240
346
|
if (inset === 0 || bodyWidth < 1) return original.call(row, width);
|
|
241
347
|
const lines = original.call(row, bodyWidth);
|
|
242
|
-
|
|
348
|
+
const cached = memo.hit(lines);
|
|
349
|
+
if (cached) return cached;
|
|
350
|
+
const decorated = renderSubmittedUserLines(lines, width, theme, inset);
|
|
351
|
+
memo.store(lines, decorated);
|
|
352
|
+
return decorated;
|
|
243
353
|
}
|
|
244
354
|
|
|
245
355
|
function decorateEditor(
|
|
@@ -275,6 +385,7 @@ export default function contentLayout(pi: ExtensionAPI): void {
|
|
|
275
385
|
let assistantPatch: RenderPatchState | undefined;
|
|
276
386
|
let userPatch: RenderPatchState | undefined;
|
|
277
387
|
let systemContainerPatch: RenderPatchState | undefined;
|
|
388
|
+
let statusLoaderPatch: RenderPatchState | undefined;
|
|
278
389
|
let editorRegistration:
|
|
279
390
|
| {
|
|
280
391
|
factory: EditorFactory;
|
|
@@ -317,6 +428,13 @@ export default function contentLayout(pi: ExtensionAPI): void {
|
|
|
317
428
|
getTheme,
|
|
318
429
|
systemContainerDecorator,
|
|
319
430
|
);
|
|
431
|
+
statusLoaderPatch = installRenderPatch(
|
|
432
|
+
Loader.prototype as unknown as PatchableRenderPrototype,
|
|
433
|
+
STATUS_LOADER_RENDER_PATCH,
|
|
434
|
+
owner,
|
|
435
|
+
getTheme,
|
|
436
|
+
statusLoaderDecorator,
|
|
437
|
+
);
|
|
320
438
|
|
|
321
439
|
const previous = ctx.ui.getEditorComponent() as EditorFactory | undefined;
|
|
322
440
|
const factory: EditorFactory = (tui, editorTheme, keybindings) => {
|
|
@@ -359,9 +477,16 @@ export default function contentLayout(pi: ExtensionAPI): void {
|
|
|
359
477
|
owner,
|
|
360
478
|
systemContainerPatch,
|
|
361
479
|
);
|
|
480
|
+
uninstallRenderPatch(
|
|
481
|
+
Loader.prototype as unknown as PatchableRenderPrototype,
|
|
482
|
+
STATUS_LOADER_RENDER_PATCH,
|
|
483
|
+
owner,
|
|
484
|
+
statusLoaderPatch,
|
|
485
|
+
);
|
|
362
486
|
assistantPatch = undefined;
|
|
363
487
|
userPatch = undefined;
|
|
364
488
|
systemContainerPatch = undefined;
|
|
489
|
+
statusLoaderPatch = undefined;
|
|
365
490
|
activeTheme = undefined;
|
|
366
491
|
});
|
|
367
492
|
}
|