@pi-kaush/pi-content-layout 0.1.5 → 0.1.7

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,23 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ - Reference semantic tokens for the remaining extension-colored elements:
6
+ the intercom sender name rides `customMessageLabel`, the submitted-prompt
7
+ rail rides `borderAccent`, and prompt surfaces paint with `userMessageBg`.
8
+ - Paint the prompt surfaces (active editor, submitted prompt) with the
9
+ theme's `customMessageBg` token instead of a hardcoded hex, retiring the
10
+ last fixed color in this package. Falls back to the legacy #071312 only
11
+ for theme lookalikes that cannot resolve the token.
12
+ - Paint the `intercom_message` frame in the bash-mode green used for `!`
13
+ shell-block rules, keep all text inside it muted, and render the sender
14
+ name in the `mdHeading` orange. All colors come from theme tokens.
15
+
16
+ - Restyle pi-intercom's inbound `intercom_message` box onto the shared
17
+ transcript columns (frame border on the marker column, title and body text
18
+ on the text column) and drop the redundant `Ctrl+O expands` title hint.
19
+ Requires this package to load before `pi-intercom`; falls back to Pi's
20
+ default custom-message box for malformed payloads.
21
+
5
22
  - Render active input with the theme's `userMessageText` foreground, restoring
6
23
  that base color after embedded ANSI resets while preserving inline identifier
7
24
  highlighting.
package/README.md CHANGED
@@ -33,6 +33,26 @@ text at this package's message inset, so `pi-tool-call-markers` mirrors
33
33
  `contentInset` for its bash blocks (see its `src/bash-block.ts`). Change
34
34
  indentation in both packages together.
35
35
 
36
+ ## pi-intercom message surface
37
+
38
+ When [`pi-intercom`](https://www.npmjs.com/package/pi-intercom) is installed,
39
+ this package restyles its inbound `intercom_message` box onto the shared
40
+ transcript columns: the frame's left border sits on the tool-marker column
41
+ (the outer inset), and the `From:` title and body text share the text column
42
+ used by tool rows and Thought labels. The frame borrows the bash-mode green
43
+ of `!` shell-block rules, all text inside stays muted, and the sender name
44
+ uses the `mdHeading` orange (the Thought-label hue in cobalt2 and Pi's stock
45
+ themes). The redundant
46
+ `Ctrl+O expands` title hint is dropped (the meta line inside the box keeps
47
+ `Ctrl+O to expand`). Content is unchanged, and expanded view keeps
48
+ pi-intercom's full body, attachment, and reply sections.
49
+
50
+ Pi resolves custom message renderers in extension load order (first
51
+ registration wins), so list this package before `pi-intercom` in `packages`
52
+ for the restyle to take effect. Without pi-intercom installed the
53
+ registration is inert; malformed message payloads fall back to Pi's default
54
+ custom-message box.
55
+
36
56
  ## Install
37
57
 
38
58
  ```json
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-kaush/pi-content-layout",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
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
@@ -17,6 +17,7 @@ import {
17
17
  chatContainerHooks,
18
18
  runChatContainerHooks,
19
19
  } from "./container-hooks.ts";
20
+ import { renderIntercomMessage } from "./intercom-message.ts";
20
21
  import {
21
22
  contentInset,
22
23
  insetLines,
@@ -381,6 +382,14 @@ function decorateEditor(
381
382
  }
382
383
 
383
384
  export default function contentLayout(pi: ExtensionAPI): void {
385
+ // Restyle pi-intercom's inbound message box to sit on this package's
386
+ // transcript columns. Pi resolves message renderers in extension load
387
+ // order (first registration wins), so this must load before pi-intercom
388
+ // to take effect; without pi-intercom installed the registration is inert.
389
+ pi.registerMessageRenderer("intercom_message", (message, options, theme) =>
390
+ renderIntercomMessage(message.details, options.expanded, theme),
391
+ );
392
+
384
393
  const owner = {};
385
394
  let activeTheme: Theme | undefined;
386
395
  let assistantPatch: RenderPatchState | undefined;
@@ -0,0 +1,173 @@
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
2
+ import type { Component } from "@earendil-works/pi-tui";
3
+ import {
4
+ truncateToWidth,
5
+ visibleWidth,
6
+ wrapTextWithAnsi,
7
+ } from "@earendil-works/pi-tui";
8
+ import { contentInset } from "./render.ts";
9
+
10
+ // Structural mirror of pi-intercom's `intercom_message` details payload, so
11
+ // this package can restyle the message without depending on pi-intercom.
12
+ // Malformed payloads make the renderer return undefined, which drops back to
13
+ // pi-intercom's own box (or Pi's default custom-message box).
14
+ export type IntercomMessageDetails = {
15
+ from?: { id?: string; name?: string; cwd?: string };
16
+ message?: {
17
+ replyTo?: string;
18
+ expectsReply?: boolean;
19
+ content?: { text?: string; attachments?: { name?: string }[] };
20
+ };
21
+ replyCommand?: string;
22
+ bodyText?: string;
23
+ };
24
+
25
+ // Geometry contract with the rest of the transcript: the frame's left border
26
+ // sits on the tool-marker column (contentInset), and title/body text start
27
+ // two columns inside the frame, sharing the text column used by tool rows
28
+ // and Thought labels. A frame needs room for "│ x │", so below MIN_BOX_WIDTH
29
+ // the message degrades to plain truncated text instead of a broken frame.
30
+ const MIN_BOX_WIDTH = 8;
31
+
32
+ class IntercomMessageComponent implements Component {
33
+ private readonly bodyText: string;
34
+ private collapsedPreview?: string;
35
+ // Cache is keyed by frame width; a fresh component is built by Pi on every
36
+ // expand toggle or invalidate(), so theme styling can live inside it.
37
+ private wrappedBody?: { width: number; lines: string[] };
38
+
39
+ constructor(
40
+ private readonly details: IntercomMessageDetails,
41
+ private readonly expanded: boolean,
42
+ private readonly theme: Theme,
43
+ ) {
44
+ this.bodyText = details.bodyText ?? details.message?.content?.text ?? "";
45
+ }
46
+
47
+ invalidate(): void {}
48
+
49
+ render(width: number): string[] {
50
+ const inset = contentInset(width);
51
+ const boxWidth = Math.max(1, width - inset * 2);
52
+ const margin = " ".repeat(inset);
53
+ const from = this.details.from ?? {};
54
+ const senderName = from.name || from.id?.slice(0, 8) || "unknown";
55
+
56
+ if (boxWidth < MIN_BOX_WIDTH) {
57
+ const text = truncateToWidth(`From: ${senderName}`, boxWidth, "");
58
+ return [margin + this.theme.fg("muted", text) + margin];
59
+ }
60
+
61
+ // The frame borrows customMessageLabel — Pi's token for extension-message
62
+ // labels (orange in cobalt2) — so the border matches the sender name.
63
+ // Everything textual inside the frame is muted, matching tool-row summaries.
64
+ const border = (text: string) => this.theme.fg("customMessageLabel", text);
65
+ const bodyColor = (text: string) => this.theme.fg("muted", text);
66
+ // Columns inside the frame: │ + space + content(innerWidth) + space + │.
67
+ const innerWidth = boxWidth - 4;
68
+
69
+ const frameLine = (content: string): string => {
70
+ const text = truncateToWidth(content, innerWidth, "");
71
+ const fill = " ".repeat(Math.max(0, innerWidth - visibleWidth(text)));
72
+ return `${margin}${border("│")} ${text}${fill} ${border("│")}${margin}`;
73
+ };
74
+ const bottomBorder = `${margin}${border(`╰${"─".repeat(boxWidth - 2)}╯`)}${margin}`;
75
+
76
+ // The sender name alone rides customMessageLabel — Pi's token for the
77
+ // label of an extension message (orange in cobalt2).
78
+ const titlePrefix = "From: ";
79
+ const title = `${titlePrefix}${senderName}${from.cwd ? ` (${from.cwd})` : ""}`;
80
+ const titleText = truncateToWidth(title, Math.max(1, innerWidth - 1), "");
81
+ const nameEnd = Math.min(
82
+ titleText.length,
83
+ titlePrefix.length + senderName.length,
84
+ );
85
+ const styledTitle =
86
+ bodyColor(titleText.slice(0, titlePrefix.length)) +
87
+ this.theme.fg(
88
+ "customMessageLabel",
89
+ titleText.slice(titlePrefix.length, nameEnd),
90
+ ) +
91
+ bodyColor(titleText.slice(nameEnd));
92
+ const dashes = "─".repeat(
93
+ Math.max(1, innerWidth - visibleWidth(titleText)),
94
+ );
95
+ const topBorder = `${margin}${border("╭")} ${styledTitle} ${border(dashes + "╮")}${margin}`;
96
+
97
+ const lines: string[] = [topBorder];
98
+
99
+ if (!this.expanded) {
100
+ this.collapsedPreview ??= this.bodyText.replace(/\s+/g, " ").trim();
101
+ lines.push(frameLine(bodyColor(this.collapsedPreview)));
102
+
103
+ const meta: string[] = [];
104
+ if (this.details.replyCommand) {
105
+ meta.push(`To reply: ${this.details.replyCommand}`);
106
+ }
107
+ const attachments = this.details.message?.content?.attachments;
108
+ if (attachments?.length) {
109
+ meta.push(
110
+ `${attachments.length} attachment${attachments.length === 1 ? "" : "s"}`,
111
+ );
112
+ }
113
+ const replyTo = this.details.message?.replyTo;
114
+ if (replyTo && !this.details.message?.expectsReply) {
115
+ meta.push(`Reply to ${replyTo.slice(0, 8)}`);
116
+ }
117
+ meta.push("Ctrl+O to expand");
118
+ lines.push(frameLine(bodyColor(meta.join(" · "))));
119
+ lines.push(bottomBorder);
120
+ return lines;
121
+ }
122
+
123
+ if (this.wrappedBody?.width !== innerWidth) {
124
+ this.wrappedBody = {
125
+ width: innerWidth,
126
+ lines: wrapTextWithAnsi(this.bodyText, innerWidth),
127
+ };
128
+ }
129
+ for (const line of this.wrappedBody.lines) {
130
+ lines.push(frameLine(bodyColor(line)));
131
+ }
132
+
133
+ if (this.details.replyCommand) {
134
+ lines.push(frameLine(""));
135
+ const replyLines = wrapTextWithAnsi(
136
+ bodyColor(`To reply: ${this.details.replyCommand}`),
137
+ innerWidth,
138
+ );
139
+ for (const line of replyLines) {
140
+ lines.push(frameLine(line));
141
+ }
142
+ }
143
+
144
+ const attachments = this.details.message?.content?.attachments;
145
+ if (attachments?.length) {
146
+ lines.push(frameLine(""));
147
+ for (const attachment of attachments) {
148
+ lines.push(frameLine(bodyColor(`Attachment: ${attachment.name}`)));
149
+ }
150
+ }
151
+
152
+ const replyTo = this.details.message?.replyTo;
153
+ if (replyTo && !this.details.message?.expectsReply) {
154
+ lines.push(frameLine(""));
155
+ lines.push(frameLine(bodyColor(`Reply to ${replyTo.slice(0, 8)}`)));
156
+ }
157
+
158
+ lines.push(bottomBorder);
159
+ return lines;
160
+ }
161
+ }
162
+
163
+ export function renderIntercomMessage(
164
+ details: unknown,
165
+ expanded: boolean,
166
+ theme: Theme,
167
+ ): Component | undefined {
168
+ if (!details || typeof details !== "object") return undefined;
169
+ const intercom = details as IntercomMessageDetails;
170
+ const text = intercom.bodyText ?? intercom.message?.content?.text;
171
+ if (typeof text !== "string") return undefined;
172
+ return new IntercomMessageComponent(intercom, expanded, theme);
173
+ }
package/src/render.ts CHANGED
@@ -6,11 +6,20 @@ export const OUTER_INSET = 2;
6
6
  export const PROMPT_RAIL = "▎";
7
7
  export const ACTIVE_SIDE_PADDING = 1;
8
8
 
9
- // The prompt surface (active editor block and submitted message body) uses a
10
- // fixed near-black green tint instead of the theme's selectedBg token so both
11
- // user-input surfaces stay identical; the theme token remains for selections
12
- // and dialogs.
13
- export const PROMPT_SURFACE_BG = "\x1b[48;2;7;19;18m"; // #071312
9
+ // The prompt surface (active editor block and submitted message body) paints
10
+ // with the theme's userMessageBg token, so both user-input surfaces stay
11
+ // identical while following the active theme. userMessageBg is a required pi
12
+ // theme token; the legacy hex remains only as a fallback for theme lookalikes
13
+ // that cannot resolve it (test fakes, older hosts).
14
+ const PROMPT_SURFACE_BG_FALLBACK = "\x1b[48;2;7;19;18m"; // #071312
15
+
16
+ export function promptSurfaceBg(theme: Theme): string {
17
+ try {
18
+ return theme.getBgAnsi("userMessageBg");
19
+ } catch {
20
+ return PROMPT_SURFACE_BG_FALLBACK;
21
+ }
22
+ }
14
23
 
15
24
  const BORDER_SENTINEL = "\x1b]133;P;pi-content-layout\x07";
16
25
  const SGR_PATTERN = /\x1b\[([0-9;]*)m/g;
@@ -158,7 +167,7 @@ function activeBlockLine(
158
167
  return paintBackground(
159
168
  `${padding}${fitLine(body, innerWidth)}${padding}`,
160
169
  width,
161
- PROMPT_SURFACE_BG,
170
+ promptSurfaceBg(theme),
162
171
  );
163
172
  }
164
173
 
@@ -235,7 +244,7 @@ export function renderSubmittedUserLines(
235
244
  width: number,
236
245
  theme: Theme,
237
246
  inset = contentInset(width),
238
- railColor: ThemeColor = "accent",
247
+ railColor: ThemeColor = "borderAccent",
239
248
  ): string[] {
240
249
  const blockWidth = width - inset * 2;
241
250
  if (inset === 0 || blockWidth <= visibleWidth(PROMPT_RAIL)) {
@@ -244,16 +253,17 @@ export function renderSubmittedUserLines(
244
253
 
245
254
  const bodyWidth = blockWidth - visibleWidth(PROMPT_RAIL);
246
255
  const margin = " ".repeat(inset);
256
+ const surfaceBg = promptSurfaceBg(theme);
247
257
  return lines.map((line) => {
248
258
  const { controls, content } = splitLeadingSemanticControls(line);
249
- const recolored = replaceBackground(content, PROMPT_SURFACE_BG);
259
+ const recolored = replaceBackground(content, surfaceBg);
250
260
  const rail = theme.fg(railColor, PROMPT_RAIL);
251
261
  // One extra leading space inside the body, so submitted text sits two
252
262
  // columns right of the rail instead of one.
253
263
  const body = paintBackground(
254
264
  ` ${fitLine(recolored, Math.max(1, bodyWidth - 1))}`,
255
265
  bodyWidth,
256
- PROMPT_SURFACE_BG,
266
+ surfaceBg,
257
267
  );
258
268
  return `${controls}${margin}${rail}${body}${margin}`;
259
269
  });