@pi-kaush/pi-content-layout 0.1.19 → 0.1.21
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/container-hooks.ts +19 -17
- package/src/index.ts +79 -41
- package/src/intercom-message.ts +133 -25
- package/src/render.ts +25 -6
package/package.json
CHANGED
package/src/container-hooks.ts
CHANGED
|
@@ -33,29 +33,31 @@ export function runChatContainerHooks(
|
|
|
33
33
|
children: unknown,
|
|
34
34
|
width: number,
|
|
35
35
|
): () => void {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
36
|
+
if (!Array.isArray(children)) return () => {};
|
|
37
|
+
|
|
38
|
+
const restores: Array<{ restore: () => void; before: unknown[] }> = [];
|
|
39
|
+
for (const hook of chatContainerHooks()) {
|
|
40
|
+
// A hook rewrites `children` and hands back its own undo. Keep the
|
|
41
|
+
// pre-hook list so a failed apply or undo cannot leave transcript rows
|
|
42
|
+
// missing from later renders.
|
|
43
|
+
const before = [...children];
|
|
44
|
+
try {
|
|
45
|
+
const restore = hook(container, children, width);
|
|
46
|
+
if (typeof restore === "function") restores.push({ restore, before });
|
|
47
|
+
} catch {
|
|
48
|
+
// Hooks are cosmetic; one failing hook must not break rendering.
|
|
49
|
+
children.length = 0;
|
|
50
|
+
children.push(...before);
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
return () => {
|
|
54
|
-
for (const restore of restores.reverse()) {
|
|
54
|
+
for (const { restore, before } of restores.reverse()) {
|
|
55
55
|
try {
|
|
56
56
|
restore();
|
|
57
57
|
} catch {
|
|
58
|
-
//
|
|
58
|
+
// Preserve earlier hooks' changes until their own undos run.
|
|
59
|
+
children.length = 0;
|
|
60
|
+
for (const child of before) children.push(child);
|
|
59
61
|
}
|
|
60
62
|
}
|
|
61
63
|
};
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
AssistantMessageComponent,
|
|
3
3
|
CustomEditor,
|
|
4
4
|
type ExtensionAPI,
|
|
5
|
+
type ExtensionUIContext,
|
|
5
6
|
type KeybindingsManager,
|
|
6
7
|
type MessageRenderer,
|
|
7
8
|
type Theme,
|
|
@@ -14,6 +15,7 @@ import {
|
|
|
14
15
|
stripTerminalSequences,
|
|
15
16
|
Text,
|
|
16
17
|
TruncatedText,
|
|
18
|
+
truncateToWidth,
|
|
17
19
|
type TUI,
|
|
18
20
|
visibleWidth,
|
|
19
21
|
} from "@earendil-works/pi-tui";
|
|
@@ -29,7 +31,11 @@ import {
|
|
|
29
31
|
import {
|
|
30
32
|
contentInset,
|
|
31
33
|
insetLines,
|
|
34
|
+
PROMPT_RAIL,
|
|
32
35
|
renderSubmittedUserLines,
|
|
36
|
+
type RenderColors,
|
|
37
|
+
type RenderMemo,
|
|
38
|
+
submittedPromptContentWidth,
|
|
33
39
|
} from "./render.ts";
|
|
34
40
|
|
|
35
41
|
const ASSISTANT_RENDER_PATCH = Symbol.for(
|
|
@@ -52,6 +58,12 @@ type EditorFactory = (
|
|
|
52
58
|
keybindings: KeybindingsManager,
|
|
53
59
|
) => EditorComponent;
|
|
54
60
|
|
|
61
|
+
type EditorRegistration = {
|
|
62
|
+
factory: EditorFactory;
|
|
63
|
+
previous: EditorFactory | undefined;
|
|
64
|
+
boundUi?: ExtensionUIContext;
|
|
65
|
+
};
|
|
66
|
+
|
|
55
67
|
type ThemeGetter = () => Theme | undefined;
|
|
56
68
|
|
|
57
69
|
type RenderRow = {
|
|
@@ -84,6 +96,7 @@ type RenderPatchState = {
|
|
|
84
96
|
type RenderCacheEntry = {
|
|
85
97
|
width: number;
|
|
86
98
|
src: string[];
|
|
99
|
+
colors: RenderColors | undefined;
|
|
87
100
|
lines: string[];
|
|
88
101
|
};
|
|
89
102
|
|
|
@@ -101,23 +114,13 @@ type RenderCacheEntry = {
|
|
|
101
114
|
// keystroke and made the input box visibly lag as sessions grew.
|
|
102
115
|
//
|
|
103
116
|
// The cache below makes decoration cost O(changed messages) instead of
|
|
104
|
-
// O(all messages): decorators
|
|
105
|
-
//
|
|
106
|
-
//
|
|
107
|
-
//
|
|
108
|
-
// streaming chunks, content edits, theme switches — because all of those
|
|
109
|
-
// regenerate the source strings upstream of us. This is why there is no
|
|
110
|
-
// explicit invalidate wiring here: partial invalidation would be worse than
|
|
111
|
-
// none, and identity checks can never miss a real change.
|
|
117
|
+
// O(all messages): decorators reuse output at the same width when the
|
|
118
|
+
// freshly rendered source strings are unchanged. Submitted prompts also
|
|
119
|
+
// compare their resolved shell colors, which may change independently of
|
|
120
|
+
// the source strings. No explicit invalidate wiring is needed.
|
|
112
121
|
//
|
|
113
122
|
// If typing ever gets slow again in long sessions, suspect this cache first.
|
|
114
123
|
// ==================================================================
|
|
115
|
-
type RenderMemo = {
|
|
116
|
-
/** Decorated lines from the last frame, when `src` is unchanged. */
|
|
117
|
-
hit(src: string[]): string[] | undefined;
|
|
118
|
-
/** Record decorated output keyed by the source lines that produced it. */
|
|
119
|
-
store(src: string[], out: string[]): void;
|
|
120
|
-
};
|
|
121
124
|
|
|
122
125
|
type RenderDecorator = (
|
|
123
126
|
original: RenderPatchState["originalRender"],
|
|
@@ -163,17 +166,23 @@ function installRenderPatch(
|
|
|
163
166
|
// bound to this row + width so unchanged rows skip string building.
|
|
164
167
|
const entry = state.cache.get(this);
|
|
165
168
|
const memo: RenderMemo = {
|
|
166
|
-
hit: (src
|
|
169
|
+
hit: (src, colors) => {
|
|
167
170
|
if (!entry || entry.width !== width) return undefined;
|
|
168
171
|
const prev = entry.src;
|
|
169
172
|
if (prev.length !== src.length) return undefined;
|
|
170
173
|
for (let i = 0; i < src.length; i++) {
|
|
171
174
|
if (prev[i] !== src[i]) return undefined;
|
|
172
175
|
}
|
|
176
|
+
if (
|
|
177
|
+
entry.colors?.rail !== colors?.rail ||
|
|
178
|
+
entry.colors?.surfaceBg !== colors?.surfaceBg
|
|
179
|
+
) {
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
173
182
|
return entry.lines;
|
|
174
183
|
},
|
|
175
|
-
store: (src
|
|
176
|
-
state.cache.set(this, { width, src, lines: out });
|
|
184
|
+
store: (src, out, colors) => {
|
|
185
|
+
state.cache.set(this, { width, src, lines: out, colors });
|
|
177
186
|
},
|
|
178
187
|
};
|
|
179
188
|
try {
|
|
@@ -307,6 +316,8 @@ function renderNotice(notice: NoticePresentation, width: number): string[] {
|
|
|
307
316
|
return lines;
|
|
308
317
|
}
|
|
309
318
|
|
|
319
|
+
// Keep one hook in its original position until the last TUI owner leaves.
|
|
320
|
+
const systemTextHookOwners = new Set<object>();
|
|
310
321
|
let systemTextHookActive = 0;
|
|
311
322
|
|
|
312
323
|
// Embedded status indicators call Loader.render() from inside the editor border.
|
|
@@ -335,8 +346,8 @@ function systemTextHook(
|
|
|
335
346
|
|
|
336
347
|
const targetInset = contentInset(width);
|
|
337
348
|
const restores: Array<() => void> = [];
|
|
338
|
-
|
|
339
|
-
if (!isSystemTextChild(child))
|
|
349
|
+
const substitute = (child: unknown) => {
|
|
350
|
+
if (!isSystemTextChild(child)) return;
|
|
340
351
|
const extraInset = Math.max(0, targetInset - (child.paddingX ?? 0));
|
|
341
352
|
const ownRender = Object.getOwnPropertyDescriptor(child, "render");
|
|
342
353
|
const originalRender = child.render;
|
|
@@ -369,12 +380,23 @@ function systemTextHook(
|
|
|
369
380
|
if (ownRender) Object.defineProperty(child, "render", ownRender);
|
|
370
381
|
else delete (child as Partial<NativeTextRow>).render;
|
|
371
382
|
});
|
|
383
|
+
};
|
|
384
|
+
const restoreRenderers = () => {
|
|
385
|
+
for (const restore of restores.reverse()) restore();
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
try {
|
|
389
|
+
for (const child of children) substitute(child);
|
|
390
|
+
} catch (error) {
|
|
391
|
+
// The shared runner can restore the child list, but not instance renderers.
|
|
392
|
+
restoreRenderers();
|
|
393
|
+
throw error;
|
|
372
394
|
}
|
|
373
395
|
if (restores.length === 0) return undefined;
|
|
374
396
|
systemTextHookActive = 1;
|
|
375
397
|
return () => {
|
|
376
398
|
systemTextHookActive = 0;
|
|
377
|
-
|
|
399
|
+
restoreRenderers();
|
|
378
400
|
};
|
|
379
401
|
}
|
|
380
402
|
|
|
@@ -478,14 +500,23 @@ function userDecorator(
|
|
|
478
500
|
memo: RenderMemo,
|
|
479
501
|
): string[] {
|
|
480
502
|
const inset = contentInset(width);
|
|
481
|
-
const bodyWidth = width - inset * 2 -
|
|
503
|
+
const bodyWidth = width - inset * 2 - visibleWidth(PROMPT_RAIL);
|
|
482
504
|
if (inset === 0 || bodyWidth < 1) return original.call(row, width);
|
|
483
|
-
|
|
484
|
-
const
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
505
|
+
// Reserve the shell's one left and two right padding columns before wrapping.
|
|
506
|
+
const wrapWidth = submittedPromptContentWidth(bodyWidth);
|
|
507
|
+
const lines = original.call(row, wrapWidth);
|
|
508
|
+
// Pi can emit native padding beyond its wrap width. In a one-cell body,
|
|
509
|
+
// even the shell's own left padding leaves no room for the native text.
|
|
510
|
+
const textWidth = Math.min(wrapWidth, bodyWidth - 1);
|
|
511
|
+
if (
|
|
512
|
+
!lines.some((line) =>
|
|
513
|
+
stripTerminalSequences(truncateToWidth(line, textWidth, "")).trim(),
|
|
514
|
+
) &&
|
|
515
|
+
lines.some((line) => stripTerminalSequences(line).trim())
|
|
516
|
+
) {
|
|
517
|
+
return original.call(row, width);
|
|
518
|
+
}
|
|
519
|
+
return renderSubmittedUserLines(lines, width, theme, inset, memo);
|
|
489
520
|
}
|
|
490
521
|
|
|
491
522
|
function supportsEmbeddedWorkingStatus(editor: EditorComponent): boolean {
|
|
@@ -541,18 +572,16 @@ export default function contentLayout(pi: ExtensionAPI): void {
|
|
|
541
572
|
let statusLoaderPatch: RenderPatchState | undefined;
|
|
542
573
|
let intercomMessageHook: ChatContainerHook | undefined;
|
|
543
574
|
let steeringMessagePatch: RenderPatchState | undefined;
|
|
544
|
-
let editorRegistration:
|
|
545
|
-
| {
|
|
546
|
-
factory: EditorFactory;
|
|
547
|
-
previous: EditorFactory | undefined;
|
|
548
|
-
}
|
|
549
|
-
| undefined;
|
|
575
|
+
let editorRegistration: EditorRegistration | undefined;
|
|
550
576
|
|
|
551
577
|
const getTheme = () => activeTheme;
|
|
552
578
|
|
|
553
579
|
pi.on("session_start", (_event, ctx) => {
|
|
554
580
|
if (ctx.mode !== "tui") return;
|
|
555
|
-
|
|
581
|
+
const ui = ctx.ui;
|
|
582
|
+
activeTheme = ui.theme;
|
|
583
|
+
// Overlapping Pi reloads can start the same TUI binding twice.
|
|
584
|
+
const alreadyBound = editorRegistration?.boundUi === ui;
|
|
556
585
|
|
|
557
586
|
const assistantPrototype =
|
|
558
587
|
AssistantMessageComponent.prototype as unknown as PatchableRenderPrototype;
|
|
@@ -575,9 +604,12 @@ export default function contentLayout(pi: ExtensionAPI): void {
|
|
|
575
604
|
const hostContainerPrototype = Object.getPrototypeOf(
|
|
576
605
|
AssistantMessageComponent.prototype,
|
|
577
606
|
) as PatchableRenderPrototype;
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
607
|
+
if (!alreadyBound) {
|
|
608
|
+
intercomMessageHook = (_container, children) =>
|
|
609
|
+
interceptIntercomMessages(children, getTheme(), renderIntercom);
|
|
610
|
+
chatContainerHooks().add(intercomMessageHook);
|
|
611
|
+
}
|
|
612
|
+
systemTextHookOwners.add(owner);
|
|
581
613
|
chatContainerHooks().add(systemTextHook);
|
|
582
614
|
systemContainerPatch = installRenderPatch(
|
|
583
615
|
hostContainerPrototype,
|
|
@@ -602,15 +634,19 @@ export default function contentLayout(pi: ExtensionAPI): void {
|
|
|
602
634
|
steeringMessageDecorator,
|
|
603
635
|
);
|
|
604
636
|
|
|
605
|
-
|
|
637
|
+
if (alreadyBound) return;
|
|
638
|
+
const previous = ui.getEditorComponent() as EditorFactory | undefined;
|
|
606
639
|
const factory: EditorFactory = (tui, editorTheme, keybindings) => {
|
|
607
640
|
const editor = previous
|
|
608
641
|
? previous(tui, editorTheme, keybindings)
|
|
609
642
|
: new CustomEditor(tui, editorTheme, keybindings);
|
|
610
643
|
return decorateEditor(editor);
|
|
611
644
|
};
|
|
612
|
-
|
|
613
|
-
|
|
645
|
+
// Pi can publish the factory before calling it; keep the undo even if it throws.
|
|
646
|
+
const registration: EditorRegistration = { factory, previous };
|
|
647
|
+
editorRegistration = registration;
|
|
648
|
+
ui.setEditorComponent(factory);
|
|
649
|
+
registration.boundUi = ui;
|
|
614
650
|
});
|
|
615
651
|
|
|
616
652
|
pi.on("session_shutdown", (_event, ctx) => {
|
|
@@ -618,7 +654,9 @@ export default function contentLayout(pi: ExtensionAPI): void {
|
|
|
618
654
|
chatContainerHooks().delete(intercomMessageHook);
|
|
619
655
|
intercomMessageHook = undefined;
|
|
620
656
|
}
|
|
621
|
-
|
|
657
|
+
if (systemTextHookOwners.delete(owner) && systemTextHookOwners.size === 0) {
|
|
658
|
+
chatContainerHooks().delete(systemTextHook);
|
|
659
|
+
}
|
|
622
660
|
if (
|
|
623
661
|
editorRegistration &&
|
|
624
662
|
ctx.ui.getEditorComponent() === editorRegistration.factory
|
package/src/intercom-message.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { stripVTControlCharacters } from "node:util";
|
|
1
2
|
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
3
|
import type { Component } from "@earendil-works/pi-tui";
|
|
3
4
|
import {
|
|
@@ -41,17 +42,38 @@ class IntercomMessageComponent implements Component {
|
|
|
41
42
|
private readonly expanded: boolean,
|
|
42
43
|
private readonly theme: Theme,
|
|
43
44
|
) {
|
|
44
|
-
this.bodyText =
|
|
45
|
+
this.bodyText = sanitizeIntercomText(
|
|
46
|
+
details.bodyText ?? details.message?.content?.text ?? "",
|
|
47
|
+
true,
|
|
48
|
+
);
|
|
45
49
|
}
|
|
46
50
|
|
|
47
51
|
invalidate(): void {}
|
|
48
52
|
|
|
49
53
|
render(width: number): string[] {
|
|
54
|
+
// Pi only chooses its default box at rebuild time. Until then the
|
|
55
|
+
// registered component can still display the body it already captured.
|
|
56
|
+
return (
|
|
57
|
+
this.renderFrame(width) ??
|
|
58
|
+
wrapTextWithAnsi(this.bodyText, Math.max(1, width)).map((line) =>
|
|
59
|
+
truncateToWidth(line, width, ""),
|
|
60
|
+
)
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
renderFrame(width: number): string[] | undefined {
|
|
65
|
+
// Metadata stays live, so the construction check cannot protect these reads.
|
|
66
|
+
if (!hasValidIntercomFields(this.details, "live")) return undefined;
|
|
67
|
+
|
|
50
68
|
const inset = contentInset(width);
|
|
51
69
|
const boxWidth = Math.max(1, width - inset * 2);
|
|
52
70
|
const margin = " ".repeat(inset);
|
|
53
71
|
const from = this.details.from ?? {};
|
|
54
|
-
const senderName =
|
|
72
|
+
const senderName =
|
|
73
|
+
sanitizeIntercomText(from.name ?? "") ||
|
|
74
|
+
sanitizeIntercomText(from.id ?? "").slice(0, 8) ||
|
|
75
|
+
"unknown";
|
|
76
|
+
const cwd = sanitizeIntercomText(from.cwd ?? "");
|
|
55
77
|
|
|
56
78
|
if (boxWidth < MIN_BOX_WIDTH) {
|
|
57
79
|
const text = truncateToWidth(`From: ${senderName}`, boxWidth, "");
|
|
@@ -76,7 +98,7 @@ class IntercomMessageComponent implements Component {
|
|
|
76
98
|
// The sender name alone rides customMessageLabel — Pi's token for the
|
|
77
99
|
// label of an extension message (orange in cobalt2).
|
|
78
100
|
const titlePrefix = "From: ";
|
|
79
|
-
const title = `${titlePrefix}${senderName}${
|
|
101
|
+
const title = `${titlePrefix}${senderName}${cwd ? ` (${cwd})` : ""}`;
|
|
80
102
|
const titleText = truncateToWidth(title, Math.max(1, innerWidth - 1), "");
|
|
81
103
|
const nameEnd = Math.min(
|
|
82
104
|
titleText.length,
|
|
@@ -108,8 +130,9 @@ class IntercomMessageComponent implements Component {
|
|
|
108
130
|
const lines = [frameLine(bodyColor(this.collapsedPreview))];
|
|
109
131
|
|
|
110
132
|
const meta: string[] = [];
|
|
111
|
-
|
|
112
|
-
|
|
133
|
+
const replyCommand = sanitizeIntercomText(this.details.replyCommand ?? "");
|
|
134
|
+
if (replyCommand) {
|
|
135
|
+
meta.push(`To reply: ${replyCommand}`);
|
|
113
136
|
}
|
|
114
137
|
const attachments = this.details.message?.content?.attachments;
|
|
115
138
|
if (attachments?.length) {
|
|
@@ -117,7 +140,7 @@ class IntercomMessageComponent implements Component {
|
|
|
117
140
|
`${attachments.length} attachment${attachments.length === 1 ? "" : "s"}`,
|
|
118
141
|
);
|
|
119
142
|
}
|
|
120
|
-
const replyTo = this.details.message?.replyTo;
|
|
143
|
+
const replyTo = sanitizeIntercomText(this.details.message?.replyTo ?? "");
|
|
121
144
|
if (replyTo && !this.details.message?.expectsReply) {
|
|
122
145
|
meta.push(`Reply to ${replyTo.slice(0, 8)}`);
|
|
123
146
|
}
|
|
@@ -141,10 +164,11 @@ class IntercomMessageComponent implements Component {
|
|
|
141
164
|
frameLine(bodyColor(line)),
|
|
142
165
|
);
|
|
143
166
|
|
|
144
|
-
|
|
167
|
+
const replyCommand = sanitizeIntercomText(this.details.replyCommand ?? "");
|
|
168
|
+
if (replyCommand) {
|
|
145
169
|
lines.push(frameLine(""));
|
|
146
170
|
const replyLines = wrapTextWithAnsi(
|
|
147
|
-
bodyColor(`To reply: ${
|
|
171
|
+
bodyColor(`To reply: ${replyCommand}`),
|
|
148
172
|
innerWidth,
|
|
149
173
|
);
|
|
150
174
|
for (const line of replyLines) {
|
|
@@ -156,11 +180,12 @@ class IntercomMessageComponent implements Component {
|
|
|
156
180
|
if (attachments?.length) {
|
|
157
181
|
lines.push(frameLine(""));
|
|
158
182
|
for (const attachment of attachments) {
|
|
159
|
-
|
|
183
|
+
const name = sanitizeIntercomText(`${attachment.name}`);
|
|
184
|
+
lines.push(frameLine(bodyColor(`Attachment: ${name}`)));
|
|
160
185
|
}
|
|
161
186
|
}
|
|
162
187
|
|
|
163
|
-
const replyTo = this.details.message?.replyTo;
|
|
188
|
+
const replyTo = sanitizeIntercomText(this.details.message?.replyTo ?? "");
|
|
164
189
|
if (replyTo && !this.details.message?.expectsReply) {
|
|
165
190
|
lines.push(frameLine(""));
|
|
166
191
|
lines.push(frameLine(bodyColor(`Reply to ${replyTo.slice(0, 8)}`)));
|
|
@@ -169,16 +194,88 @@ class IntercomMessageComponent implements Component {
|
|
|
169
194
|
}
|
|
170
195
|
}
|
|
171
196
|
|
|
197
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
198
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function isOptionalString(value: unknown): value is string | undefined {
|
|
202
|
+
return value === undefined || typeof value === "string";
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function hasValidIntercomFields(
|
|
206
|
+
details: unknown,
|
|
207
|
+
phase: "initial" | "live",
|
|
208
|
+
): boolean {
|
|
209
|
+
if (!isRecord(details)) return false;
|
|
210
|
+
if (phase === "initial" && !isOptionalString(details.bodyText)) return false;
|
|
211
|
+
if (!isOptionalString(details.replyCommand)) return false;
|
|
212
|
+
|
|
213
|
+
const from = details.from;
|
|
214
|
+
if (
|
|
215
|
+
from !== undefined &&
|
|
216
|
+
(!isRecord(from) || ![from.id, from.name, from.cwd].every(isOptionalString))
|
|
217
|
+
)
|
|
218
|
+
return false;
|
|
219
|
+
|
|
220
|
+
const message = details.message;
|
|
221
|
+
if (message !== undefined) {
|
|
222
|
+
if (!isRecord(message)) return false;
|
|
223
|
+
if (
|
|
224
|
+
!isOptionalString(message.replyTo) ||
|
|
225
|
+
(message.expectsReply !== undefined &&
|
|
226
|
+
typeof message.expectsReply !== "boolean")
|
|
227
|
+
)
|
|
228
|
+
return false;
|
|
229
|
+
|
|
230
|
+
const content = message.content;
|
|
231
|
+
if (content !== undefined) {
|
|
232
|
+
if (!isRecord(content)) return false;
|
|
233
|
+
if (phase === "initial" && !isOptionalString(content.text)) return false;
|
|
234
|
+
const attachments = content.attachments;
|
|
235
|
+
if (attachments !== undefined) {
|
|
236
|
+
if (!Array.isArray(attachments)) return false;
|
|
237
|
+
for (const attachment of attachments) {
|
|
238
|
+
if (!isRecord(attachment) || !isOptionalString(attachment.name))
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function isIntercomMessageDetails(
|
|
248
|
+
details: unknown,
|
|
249
|
+
): details is IntercomMessageDetails {
|
|
250
|
+
return hasValidIntercomFields(details, "initial");
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Details carry text, not terminal output. Strip external ANSI before adding
|
|
254
|
+
// our own theme styling; only message bodies may retain literal line feeds.
|
|
255
|
+
function sanitizeIntercomText(text: string, multiline = false): string {
|
|
256
|
+
return stripVTControlCharacters(text).replace(
|
|
257
|
+
/[\x00-\x1f\x7f-\x9f]/g,
|
|
258
|
+
(control) => (multiline && control === "\n" ? "\n" : " "),
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
172
262
|
export function renderIntercomMessage(
|
|
173
263
|
details: unknown,
|
|
174
264
|
expanded: boolean,
|
|
175
265
|
theme: Theme,
|
|
176
266
|
): Component | undefined {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
267
|
+
return intercomBox(details, expanded, theme);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function intercomBox(
|
|
271
|
+
details: unknown,
|
|
272
|
+
expanded: boolean,
|
|
273
|
+
theme: Theme,
|
|
274
|
+
): IntercomMessageComponent | undefined {
|
|
275
|
+
if (!isIntercomMessageDetails(details)) return undefined;
|
|
276
|
+
const text = details.bodyText ?? details.message?.content?.text;
|
|
180
277
|
if (typeof text !== "string") return undefined;
|
|
181
|
-
return new IntercomMessageComponent(
|
|
278
|
+
return new IntercomMessageComponent(details, expanded, theme);
|
|
182
279
|
}
|
|
183
280
|
|
|
184
281
|
// Structural mirror of Pi's CustomMessageComponent: the fields this package
|
|
@@ -213,7 +310,7 @@ const intercomBoxes = new WeakMap<
|
|
|
213
310
|
theme: Theme;
|
|
214
311
|
details: unknown;
|
|
215
312
|
expanded: boolean;
|
|
216
|
-
component:
|
|
313
|
+
component: IntercomMessageComponent;
|
|
217
314
|
}
|
|
218
315
|
>();
|
|
219
316
|
|
|
@@ -222,7 +319,7 @@ function intercomBoxFor(
|
|
|
222
319
|
details: unknown,
|
|
223
320
|
expanded: boolean,
|
|
224
321
|
theme: Theme,
|
|
225
|
-
):
|
|
322
|
+
): IntercomMessageComponent | undefined {
|
|
226
323
|
const cached = intercomBoxes.get(host);
|
|
227
324
|
if (
|
|
228
325
|
cached &&
|
|
@@ -232,7 +329,7 @@ function intercomBoxFor(
|
|
|
232
329
|
) {
|
|
233
330
|
return cached.component;
|
|
234
331
|
}
|
|
235
|
-
const component =
|
|
332
|
+
const component = intercomBox(details, expanded, theme);
|
|
236
333
|
if (!component) {
|
|
237
334
|
intercomBoxes.delete(host);
|
|
238
335
|
return undefined;
|
|
@@ -258,8 +355,9 @@ export function interceptIntercomMessages(
|
|
|
258
355
|
): (() => void) | undefined {
|
|
259
356
|
if (!theme || !Array.isArray(children)) return undefined;
|
|
260
357
|
const restores: Array<() => void> = [];
|
|
261
|
-
|
|
262
|
-
if (!isIntercomMessageHost(child, ownRenderer))
|
|
358
|
+
const substitute = (child: unknown) => {
|
|
359
|
+
if (!isIntercomMessageHost(child, ownRenderer)) return;
|
|
360
|
+
const ownRender = Object.getOwnPropertyDescriptor(child, "render");
|
|
263
361
|
const originalRender = child.render;
|
|
264
362
|
child.render = (width: number): string[] => {
|
|
265
363
|
const box = intercomBoxFor(
|
|
@@ -268,18 +366,28 @@ export function interceptIntercomMessages(
|
|
|
268
366
|
child._expanded === true,
|
|
269
367
|
theme,
|
|
270
368
|
);
|
|
271
|
-
|
|
369
|
+
const lines = box?.renderFrame(width);
|
|
370
|
+
if (!lines) return originalRender.call(child, width);
|
|
272
371
|
// Pi's CustomMessageComponent renders a Spacer(1) ahead of the box.
|
|
273
372
|
// The swap bypasses the host's own render, so reproduce that blank
|
|
274
373
|
// line to keep the frame separated from the entry above it.
|
|
275
|
-
return ["", ...
|
|
374
|
+
return ["", ...lines];
|
|
276
375
|
};
|
|
277
376
|
restores.push(() => {
|
|
278
|
-
|
|
377
|
+
if (ownRender) Object.defineProperty(child, "render", ownRender);
|
|
378
|
+
else delete (child as Partial<IntercomMessageHost>).render;
|
|
279
379
|
});
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
return () => {
|
|
380
|
+
};
|
|
381
|
+
const restoreRenderers = () => {
|
|
283
382
|
for (const restore of restores.reverse()) restore();
|
|
284
383
|
};
|
|
384
|
+
|
|
385
|
+
try {
|
|
386
|
+
for (const child of children) substitute(child);
|
|
387
|
+
} catch (error) {
|
|
388
|
+
// The shared runner can restore the child list, but not instance renderers.
|
|
389
|
+
restoreRenderers();
|
|
390
|
+
throw error;
|
|
391
|
+
}
|
|
392
|
+
return restores.length === 0 ? undefined : restoreRenderers;
|
|
285
393
|
}
|
package/src/render.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Theme
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
3
3
|
|
|
4
4
|
export const OUTER_INSET = 2;
|
|
@@ -26,6 +26,10 @@ export function contentInset(width: number): number {
|
|
|
26
26
|
return width > OUTER_INSET * 2 ? OUTER_INSET : 0;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
export function submittedPromptContentWidth(bodyWidth: number): number {
|
|
30
|
+
return Math.max(1, bodyWidth - (SUBMITTED_SIDE_PADDING * 2 + 1));
|
|
31
|
+
}
|
|
32
|
+
|
|
29
33
|
function semanticControlEnd(text: string, offset: number): number | undefined {
|
|
30
34
|
if (text[offset] !== "\x1b") return undefined;
|
|
31
35
|
const kind = text[offset + 1];
|
|
@@ -102,12 +106,22 @@ function replaceBackground(text: string, backgroundAnsi: string): string {
|
|
|
102
106
|
return text.replace(BACKGROUND_PATTERN, backgroundAnsi);
|
|
103
107
|
}
|
|
104
108
|
|
|
109
|
+
export type RenderColors = {
|
|
110
|
+
readonly rail: string;
|
|
111
|
+
readonly surfaceBg: string;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export type RenderMemo = {
|
|
115
|
+
hit(src: string[], colors?: RenderColors): string[] | undefined;
|
|
116
|
+
store(src: string[], out: string[], colors?: RenderColors): void;
|
|
117
|
+
};
|
|
118
|
+
|
|
105
119
|
export function renderSubmittedUserLines(
|
|
106
120
|
lines: string[],
|
|
107
121
|
width: number,
|
|
108
122
|
theme: Theme,
|
|
109
123
|
inset = contentInset(width),
|
|
110
|
-
|
|
124
|
+
memo?: RenderMemo,
|
|
111
125
|
): string[] {
|
|
112
126
|
const blockWidth = width - inset * 2;
|
|
113
127
|
if (inset === 0 || blockWidth <= visibleWidth(PROMPT_RAIL)) {
|
|
@@ -117,21 +131,26 @@ export function renderSubmittedUserLines(
|
|
|
117
131
|
const bodyWidth = blockWidth - visibleWidth(PROMPT_RAIL);
|
|
118
132
|
const margin = " ".repeat(inset);
|
|
119
133
|
const surfaceBg = promptSurfaceBg(theme);
|
|
120
|
-
|
|
134
|
+
const rail = lines.length === 0 ? "" : theme.fg("borderAccent", PROMPT_RAIL);
|
|
135
|
+
const colors = lines.length === 0 ? undefined : { rail, surfaceBg };
|
|
136
|
+
const cached = memo?.hit(lines, colors);
|
|
137
|
+
if (cached) return cached;
|
|
138
|
+
const decorated = lines.map((line) => {
|
|
121
139
|
const { controls, content } = splitLeadingSemanticControls(line);
|
|
122
140
|
const recolored = replaceBackground(content, surfaceBg);
|
|
123
|
-
const rail = theme.fg(railColor, PROMPT_RAIL);
|
|
124
141
|
// The rail glyph (▎) fills only the left quarter of its cell, so one
|
|
125
142
|
// column of left padding already reads as a two-column visual gap.
|
|
126
143
|
// Give the right edge two columns so both sides of the text read equal.
|
|
127
144
|
const leftPadding = " ".repeat(SUBMITTED_SIDE_PADDING);
|
|
128
145
|
const rightPadding = " ".repeat(SUBMITTED_SIDE_PADDING + 1);
|
|
129
|
-
const
|
|
146
|
+
const submittedContentWidth = submittedPromptContentWidth(bodyWidth);
|
|
130
147
|
const body = paintBackground(
|
|
131
|
-
`${leftPadding}${fitLine(recolored,
|
|
148
|
+
`${leftPadding}${fitLine(recolored, submittedContentWidth)}${rightPadding}`,
|
|
132
149
|
bodyWidth,
|
|
133
150
|
surfaceBg,
|
|
134
151
|
);
|
|
135
152
|
return `${controls}${margin}${rail}${body}${margin}`;
|
|
136
153
|
});
|
|
154
|
+
memo?.store(lines, decorated, colors);
|
|
155
|
+
return decorated;
|
|
137
156
|
}
|