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

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.14",
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,
@@ -45,15 +50,6 @@ type EditorFactory = (
45
50
  keybindings: KeybindingsManager,
46
51
  ) => EditorComponent;
47
52
 
48
- type EmbeddedStatusEditorOptions = NonNullable<
49
- ConstructorParameters<typeof CustomEditor>[3]
50
- > & { embedWorkingStatus: true };
51
-
52
- // Newer Pi consumes this option; older editors keep the transcript status row.
53
- const EMBEDDED_STATUS_EDITOR_OPTIONS: EmbeddedStatusEditorOptions = {
54
- embedWorkingStatus: true,
55
- };
56
-
57
53
  type ThemeGetter = () => Theme | undefined;
58
54
 
59
55
  type RenderRow = {
@@ -451,15 +447,15 @@ function decorateEditor(editor: EditorComponent): EditorComponent {
451
447
  },
452
448
  });
453
449
  }
450
+ // Pi resolves custom message renderers in extension load order (first
451
+ // registration wins), so this registration only wins when this package loads
452
+ // before pi-intercom; the intercomMessageHook installed below covers the
453
+ // order where pi-intercom wins. Without pi-intercom installed both are inert.
454
+ const renderIntercom: MessageRenderer = (message, options, theme) =>
455
+ renderIntercomMessage(message.details, options.expanded, theme);
454
456
 
455
457
  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
- );
458
+ pi.registerMessageRenderer("intercom_message", renderIntercom);
463
459
 
464
460
  const owner = {};
465
461
  let activeTheme: Theme | undefined;
@@ -467,6 +463,7 @@ export default function contentLayout(pi: ExtensionAPI): void {
467
463
  let userPatch: RenderPatchState | undefined;
468
464
  let systemContainerPatch: RenderPatchState | undefined;
469
465
  let statusLoaderPatch: RenderPatchState | undefined;
466
+ let intercomMessageHook: ChatContainerHook | undefined;
470
467
  let steeringMessagePatch: RenderPatchState | undefined;
471
468
  let editorRegistration:
472
469
  | {
@@ -502,6 +499,9 @@ export default function contentLayout(pi: ExtensionAPI): void {
502
499
  const hostContainerPrototype = Object.getPrototypeOf(
503
500
  AssistantMessageComponent.prototype,
504
501
  ) as PatchableRenderPrototype;
502
+ intercomMessageHook = (_container, children) =>
503
+ interceptIntercomMessages(children, getTheme(), renderIntercom);
504
+ chatContainerHooks().add(intercomMessageHook);
505
505
  chatContainerHooks().add(systemTextHook);
506
506
  systemContainerPatch = installRenderPatch(
507
507
  hostContainerPrototype,
@@ -530,12 +530,7 @@ export default function contentLayout(pi: ExtensionAPI): void {
530
530
  const factory: EditorFactory = (tui, editorTheme, keybindings) => {
531
531
  const editor = previous
532
532
  ? previous(tui, editorTheme, keybindings)
533
- : new CustomEditor(
534
- tui,
535
- editorTheme,
536
- keybindings,
537
- EMBEDDED_STATUS_EDITOR_OPTIONS,
538
- );
533
+ : new CustomEditor(tui, editorTheme, keybindings);
539
534
  return decorateEditor(editor);
540
535
  };
541
536
  editorRegistration = { factory, previous };
@@ -543,6 +538,10 @@ export default function contentLayout(pi: ExtensionAPI): void {
543
538
  });
544
539
 
545
540
  pi.on("session_shutdown", (_event, ctx) => {
541
+ if (intercomMessageHook) {
542
+ chatContainerHooks().delete(intercomMessageHook);
543
+ intercomMessageHook = undefined;
544
+ }
546
545
  chatContainerHooks().delete(systemTextHook);
547
546
  if (
548
547
  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
+ }