@pi-kaush/pi-content-layout 0.1.12 → 0.1.13

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 CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ - Restyle the `intercom_message` box even when `pi-intercom` wins Pi's first-registration-wins renderer lookup: a chat-container hook replaces the box Pi built with this package's frame, so the surface no longer depends on `packages` order. The renderer registration stays as the cheaper path when this package loads first.
6
+
5
7
  - Restore Pi's native active editor border and terminal background, so its embedded status interrupts the top rule as in native Pi. Submitted user messages keep their darker railed shell. Older Pi versions keep the aligned transcript-row status fallback.
6
8
 
7
9
  - Roll back a chat-container hook that mutates the child list and then throws.
package/README.md CHANGED
@@ -64,19 +64,20 @@ When [`pi-intercom`](https://www.npmjs.com/package/pi-intercom) is installed,
64
64
  this package restyles its inbound `intercom_message` box onto the shared
65
65
  transcript columns: the frame's left border sits on the tool-marker column
66
66
  (the outer inset), and the `From:` title and body text share the text column
67
- used by tool rows and Thought labels. The frame borrows the bash-mode green
68
- of `!` shell-block rules, all text inside stays muted, and the sender name
69
- uses the `mdHeading` orange (the Thought-label hue in cobalt2 and Pi's stock
70
- themes). The redundant
67
+ used by tool rows and Thought labels. The frame and the sender name ride
68
+ `customMessageLabel` (Pi's label tone for extension messages), and every
69
+ line inside the frame stays muted. The redundant
71
70
  `Ctrl+O expands` title hint is dropped (the meta line inside the box keeps
72
71
  `Ctrl+O to expand`). Content is unchanged, and expanded view keeps
73
72
  pi-intercom's full body, attachment, and reply sections.
74
73
 
75
- Pi resolves custom message renderers in extension load order (first
76
- registration wins), so list this package before `pi-intercom` in `packages`
77
- for the restyle to take effect. Without pi-intercom installed the
78
- registration is inert; malformed message payloads fall back to Pi's default
79
- custom-message box.
74
+ Pi resolves a customType's renderer in extension load order (first
75
+ registration wins), so `pi-intercom` would otherwise shadow this package's
76
+ box. A chat-container hook swaps the restyled box in for whichever
77
+ component Pi built, so the surface holds in either order; listing this
78
+ package before `pi-intercom` in `packages` keeps the cheaper registration
79
+ path. Without pi-intercom installed both paths are inert, and malformed
80
+ message payloads fall back to Pi's default custom-message box.
80
81
 
81
82
  ## Manual configuration
82
83
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-kaush/pi-content-layout",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "description": "Inset Pi's conversation surface and style active and submitted prompts as matching blocks.",
5
5
  "license": "MIT",
6
6
  "author": "Kaushik Gopal",
package/src/index.ts CHANGED
@@ -3,6 +3,7 @@ import {
3
3
  CustomEditor,
4
4
  type ExtensionAPI,
5
5
  type KeybindingsManager,
6
+ type MessageRenderer,
6
7
  type Theme,
7
8
  UserMessageComponent,
8
9
  } from "@earendil-works/pi-coding-agent";
@@ -15,10 +16,14 @@ import {
15
16
  visibleWidth,
16
17
  } from "@earendil-works/pi-tui";
17
18
  import {
19
+ type ChatContainerHook,
18
20
  chatContainerHooks,
19
21
  runChatContainerHooks,
20
22
  } from "./container-hooks.ts";
21
- import { renderIntercomMessage } from "./intercom-message.ts";
23
+ import {
24
+ interceptIntercomMessages,
25
+ renderIntercomMessage,
26
+ } from "./intercom-message.ts";
22
27
  import {
23
28
  contentInset,
24
29
  insetLines,
@@ -451,15 +456,15 @@ function decorateEditor(editor: EditorComponent): EditorComponent {
451
456
  },
452
457
  });
453
458
  }
459
+ // Pi resolves custom message renderers in extension load order (first
460
+ // registration wins), so this registration only wins when this package loads
461
+ // before pi-intercom; the intercomMessageHook installed below covers the
462
+ // order where pi-intercom wins. Without pi-intercom installed both are inert.
463
+ const renderIntercom: MessageRenderer = (message, options, theme) =>
464
+ renderIntercomMessage(message.details, options.expanded, theme);
454
465
 
455
466
  export default function contentLayout(pi: ExtensionAPI): void {
456
- // Restyle pi-intercom's inbound message box to sit on this package's
457
- // transcript columns. Pi resolves message renderers in extension load
458
- // order (first registration wins), so this must load before pi-intercom
459
- // to take effect; without pi-intercom installed the registration is inert.
460
- pi.registerMessageRenderer("intercom_message", (message, options, theme) =>
461
- renderIntercomMessage(message.details, options.expanded, theme),
462
- );
467
+ pi.registerMessageRenderer("intercom_message", renderIntercom);
463
468
 
464
469
  const owner = {};
465
470
  let activeTheme: Theme | undefined;
@@ -467,6 +472,7 @@ export default function contentLayout(pi: ExtensionAPI): void {
467
472
  let userPatch: RenderPatchState | undefined;
468
473
  let systemContainerPatch: RenderPatchState | undefined;
469
474
  let statusLoaderPatch: RenderPatchState | undefined;
475
+ let intercomMessageHook: ChatContainerHook | undefined;
470
476
  let steeringMessagePatch: RenderPatchState | undefined;
471
477
  let editorRegistration:
472
478
  | {
@@ -502,6 +508,9 @@ export default function contentLayout(pi: ExtensionAPI): void {
502
508
  const hostContainerPrototype = Object.getPrototypeOf(
503
509
  AssistantMessageComponent.prototype,
504
510
  ) as PatchableRenderPrototype;
511
+ intercomMessageHook = (_container, children) =>
512
+ interceptIntercomMessages(children, getTheme(), renderIntercom);
513
+ chatContainerHooks().add(intercomMessageHook);
505
514
  chatContainerHooks().add(systemTextHook);
506
515
  systemContainerPatch = installRenderPatch(
507
516
  hostContainerPrototype,
@@ -543,6 +552,10 @@ export default function contentLayout(pi: ExtensionAPI): void {
543
552
  });
544
553
 
545
554
  pi.on("session_shutdown", (_event, ctx) => {
555
+ if (intercomMessageHook) {
556
+ chatContainerHooks().delete(intercomMessageHook);
557
+ intercomMessageHook = undefined;
558
+ }
546
559
  chatContainerHooks().delete(systemTextHook);
547
560
  if (
548
561
  editorRegistration &&
@@ -171,3 +171,102 @@ export function renderIntercomMessage(
171
171
  if (typeof text !== "string") return undefined;
172
172
  return new IntercomMessageComponent(intercom, expanded, theme);
173
173
  }
174
+
175
+ // Structural mirror of Pi's CustomMessageComponent: the fields this package
176
+ // reads to restyle a box Pi already built with another renderer.
177
+ type IntercomMessageHost = {
178
+ message?: { customType?: unknown; details?: unknown };
179
+ customRenderer?: unknown;
180
+ _expanded?: unknown;
181
+ render(width: number): string[];
182
+ };
183
+
184
+ function isIntercomMessageHost(
185
+ child: unknown,
186
+ ownRenderer: unknown,
187
+ ): child is IntercomMessageHost {
188
+ if (!child || typeof child !== "object") return false;
189
+ const host = child as IntercomMessageHost;
190
+ if (typeof host.render !== "function") return false;
191
+ // Pi picks one renderer per customType by load order; when this package's
192
+ // registration won, Pi already renders the styled box and there is nothing
193
+ // left to intercept.
194
+ if (host.customRenderer === ownRenderer) return false;
195
+ return host.message?.customType === "intercom_message";
196
+ }
197
+
198
+ // Pi re-renders the transcript on every keystroke, so a box rebuilt per frame
199
+ // would re-wrap the body text each time. Cache per host component instead,
200
+ // keyed on the inputs that change what the box looks like.
201
+ const intercomBoxes = new WeakMap<
202
+ object,
203
+ {
204
+ theme: Theme;
205
+ details: unknown;
206
+ expanded: boolean;
207
+ component: Component;
208
+ }
209
+ >();
210
+
211
+ function intercomBoxFor(
212
+ host: IntercomMessageHost,
213
+ details: unknown,
214
+ expanded: boolean,
215
+ theme: Theme,
216
+ ): Component | undefined {
217
+ const cached = intercomBoxes.get(host);
218
+ if (
219
+ cached &&
220
+ cached.theme === theme &&
221
+ cached.details === details &&
222
+ cached.expanded === expanded
223
+ ) {
224
+ return cached.component;
225
+ }
226
+ const component = renderIntercomMessage(details, expanded, theme);
227
+ if (!component) {
228
+ intercomBoxes.delete(host);
229
+ return undefined;
230
+ }
231
+ intercomBoxes.set(host, { theme, details, expanded, component });
232
+ return component;
233
+ }
234
+
235
+ /**
236
+ * Swap this package's box into an intercom message Pi rendered with another
237
+ * extension's renderer. Pi resolves a customType's renderer by extension load
238
+ * order (first registration wins), so the registration above only wins when
239
+ * this package loads before pi-intercom; this hook makes the restyle hold in
240
+ * either order.
241
+ *
242
+ * Returns the undo for the current render pass, or undefined when no child
243
+ * needed intercepting. Malformed payloads fall back to the render Pi built.
244
+ */
245
+ export function interceptIntercomMessages(
246
+ children: unknown[],
247
+ theme: Theme | undefined,
248
+ ownRenderer: unknown,
249
+ ): (() => void) | undefined {
250
+ if (!theme || !Array.isArray(children)) return undefined;
251
+ const restores: Array<() => void> = [];
252
+ for (const child of children) {
253
+ if (!isIntercomMessageHost(child, ownRenderer)) continue;
254
+ const originalRender = child.render;
255
+ child.render = (width: number): string[] => {
256
+ const box = intercomBoxFor(
257
+ child,
258
+ child.message?.details,
259
+ child._expanded === true,
260
+ theme,
261
+ );
262
+ return box ? box.render(width) : originalRender.call(child, width);
263
+ };
264
+ restores.push(() => {
265
+ delete (child as { render?: unknown }).render;
266
+ });
267
+ }
268
+ if (restores.length === 0) return undefined;
269
+ return () => {
270
+ for (const restore of restores.reverse()) restore();
271
+ };
272
+ }