@xzzpig/pi-tidy 0.0.0-stage → 0.1.0
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 +1104 -0
- package/FORK-CHANGELOG.md +31 -0
- package/LICENSE +21 -0
- package/README.md +416 -2
- package/fork/defaults.ts +77 -0
- package/fork/index.ts +2 -0
- package/fork/install.ts +58 -0
- package/fork/thinking-row.ts +221 -0
- package/package.json +68 -3
- package/src/clean-copy.ts +313 -0
- package/src/clipboard.ts +29 -0
- package/src/config.ts +292 -0
- package/src/draw/frame-draw.ts +307 -0
- package/src/draw/icons.ts +2014 -0
- package/src/draw/paint.ts +57 -0
- package/src/frame.ts +20 -0
- package/src/hover.ts +185 -0
- package/src/index.ts +322 -0
- package/src/live.ts +91 -0
- package/src/messages/bash-mode-frame.ts +112 -0
- package/src/messages/error-lines.ts +331 -0
- package/src/messages/extension-notice.ts +204 -0
- package/src/messages/hex-swatches.ts +245 -0
- package/src/messages/message-frame.ts +515 -0
- package/src/messages/user-message.ts +207 -0
- package/src/theme-access.ts +26 -0
- package/src/tools/background-jobs.ts +617 -0
- package/src/tools/bash-highlight.ts +330 -0
- package/src/tools/codemode-body.ts +602 -0
- package/src/tools/result-preview.ts +56 -0
- package/src/tools/split-diff.ts +606 -0
- package/src/tools/tool-frame.ts +1019 -0
- package/src/tools/tool-group-view.ts +1160 -0
- package/src/tools/tool-groups.ts +706 -0
- package/src/tools/tool-lines.ts +621 -0
- package/src/tools/write-preview.ts +81 -0
- package/src/turns/code-blocks.ts +273 -0
- package/src/turns/turn-lifecycle.ts +43 -0
- package/src/turns/turn-state.ts +257 -0
- package/src/turns/turn-view.ts +504 -0
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rounded transparent frames for the message boxes pi draws outside tool
|
|
3
|
+
* calls: compaction / branch / skill summaries, extension custom messages and
|
|
4
|
+
* user messages in the "frame" style.
|
|
5
|
+
*
|
|
6
|
+
* Options live in global slots (see live.ts): every call updates them, and only
|
|
7
|
+
* the first one installs the prototype patches, so a `/reload` with different
|
|
8
|
+
* settings — including `frameMessages: false` — takes effect without a restart.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
BranchSummaryMessageComponent,
|
|
13
|
+
CompactionSummaryMessageComponent,
|
|
14
|
+
CustomMessageComponent,
|
|
15
|
+
getMarkdownTheme,
|
|
16
|
+
SkillInvocationMessageComponent,
|
|
17
|
+
UserMessageComponent,
|
|
18
|
+
} from "@earendil-works/pi-coding-agent";
|
|
19
|
+
import { Markdown, visibleWidth } from "@earendil-works/pi-tui";
|
|
20
|
+
import {
|
|
21
|
+
clipToWidth,
|
|
22
|
+
drawFrame,
|
|
23
|
+
formatMessageTime,
|
|
24
|
+
type HeaderStyle,
|
|
25
|
+
italicDim,
|
|
26
|
+
labeledTopBorder,
|
|
27
|
+
messageHeader,
|
|
28
|
+
sameLines,
|
|
29
|
+
stripBackgroundFills,
|
|
30
|
+
trimBlankEdges,
|
|
31
|
+
trimTrailingSpaces,
|
|
32
|
+
visibleText,
|
|
33
|
+
} from "../draw/frame-draw.ts";
|
|
34
|
+
import { MESSAGE_ICONS, TOOL_ICONS } from "../draw/icons.ts";
|
|
35
|
+
import { liveSlot, optionsRevision, tidyEnabled } from "../live.ts";
|
|
36
|
+
import { noticeBodyText, noticeLabel, noticeText, type NoticeLabel } from "./extension-notice.ts";
|
|
37
|
+
import { getTheme, type ThemeLike } from "../theme-access.ts";
|
|
38
|
+
import { installUserMessageTimes, USER_MESSAGE_TIME } from "./user-message.ts";
|
|
39
|
+
|
|
40
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
41
|
+
// Message boxes (compaction / branch summary)
|
|
42
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
43
|
+
|
|
44
|
+
interface MessageBoxInternals {
|
|
45
|
+
children: Array<{ render(width: number): string[] }>;
|
|
46
|
+
expanded: boolean;
|
|
47
|
+
render(width: number): string[];
|
|
48
|
+
__msgFrameCache?: { width: number; sample: string; revision: number; lines: string[]; out: string[] };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface MessageBoxOptions {
|
|
52
|
+
enabled: boolean;
|
|
53
|
+
borderColors: { compaction: string; branch: string; skill: string };
|
|
54
|
+
icons: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const messageOptions = liveSlot<MessageBoxOptions | undefined>("message-box-options", undefined);
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Compaction, branch-summary, and skill-invocation boxes are plain `Box`es
|
|
61
|
+
* with a filled `customMessageBg` background — ToolExecutionComponent never
|
|
62
|
+
* sees them, so the tool frame patch leaves them in pi's default style. They
|
|
63
|
+
* get the same rounded, transparent frame here, each drawn in its own color
|
|
64
|
+
* from `borderColors` to keep them distinct from the tool state colors and
|
|
65
|
+
* from each other. Children are rendered directly, skipping the Box's
|
|
66
|
+
* background fill and padding, so the frame is the only chrome; on any
|
|
67
|
+
* mismatch the original Box render runs.
|
|
68
|
+
*
|
|
69
|
+
* `enabled: false` turns installed patches into pass-throughs and installs
|
|
70
|
+
* nothing new.
|
|
71
|
+
*/
|
|
72
|
+
export function patchMessageBoxes(
|
|
73
|
+
borderColors: { compaction: string; branch: string; skill: string },
|
|
74
|
+
icons = false,
|
|
75
|
+
enabled = true,
|
|
76
|
+
): void {
|
|
77
|
+
messageOptions.set({ enabled, borderColors, icons });
|
|
78
|
+
if (!enabled) return;
|
|
79
|
+
for (const [cls, kind] of [
|
|
80
|
+
[CompactionSummaryMessageComponent, "compaction"],
|
|
81
|
+
[BranchSummaryMessageComponent, "branch"],
|
|
82
|
+
[SkillInvocationMessageComponent, "skill"],
|
|
83
|
+
] as const) {
|
|
84
|
+
const proto = cls.prototype as unknown as MessageBoxInternals & { __toolboxFramed?: boolean };
|
|
85
|
+
if (proto.__toolboxFramed) continue;
|
|
86
|
+
proto.__toolboxFramed = true;
|
|
87
|
+
|
|
88
|
+
const originalRender = proto.render;
|
|
89
|
+
const icon = MESSAGE_ICONS[kind];
|
|
90
|
+
|
|
91
|
+
proto.render = function (this: MessageBoxInternals, width: number): string[] {
|
|
92
|
+
const options = messageOptions.get();
|
|
93
|
+
const theme = getTheme();
|
|
94
|
+
if (!theme || !options?.enabled || !tidyEnabled.get()) return originalRender.call(this, width);
|
|
95
|
+
try {
|
|
96
|
+
const { icons } = options;
|
|
97
|
+
const borderColor = options.borderColors[kind];
|
|
98
|
+
const w = Math.max(4, width);
|
|
99
|
+
// Frame + one cell of horizontal padding on each side (+2 for the icon).
|
|
100
|
+
const contentWidth = Math.max(1, icons ? w - 6 : w - 4);
|
|
101
|
+
const content: string[] = [];
|
|
102
|
+
for (const child of this.children) {
|
|
103
|
+
for (const line of child.render(contentWidth)) {
|
|
104
|
+
content.push(stripBackgroundFills(line));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (content.length === 0) return [];
|
|
108
|
+
// The border color escape acts as a theme sample — a theme switch
|
|
109
|
+
// with identical content must not hit the cache.
|
|
110
|
+
const sample = theme.fg(borderColor, "·");
|
|
111
|
+
const cache = this.__msgFrameCache;
|
|
112
|
+
const revision = optionsRevision();
|
|
113
|
+
if (
|
|
114
|
+
cache &&
|
|
115
|
+
cache.width === w &&
|
|
116
|
+
cache.sample === sample &&
|
|
117
|
+
cache.revision === revision &&
|
|
118
|
+
sameLines(cache.lines, content)
|
|
119
|
+
)
|
|
120
|
+
return cache.out;
|
|
121
|
+
// pi draws the kind label on a row of its own above the body
|
|
122
|
+
// (`[branch]`, `[compaction]`, `[skill]`). That row is a title, not
|
|
123
|
+
// content, so it goes into the top border and the `Spacer(1)` under it
|
|
124
|
+
// goes with it — the border is the separation now. A collapsed skill
|
|
125
|
+
// box keeps its label inline: it shares the row with the skill's name.
|
|
126
|
+
const lifted = content[0] !== undefined && visibleText(content[0]).trim() === `[${kind}]`
|
|
127
|
+
? trimTrailingSpaces(content[0]).trim()
|
|
128
|
+
: "";
|
|
129
|
+
const body = lifted ? trimBlankEdges(content.slice(1)) : content;
|
|
130
|
+
const label = lifted && icons ? `${theme.fg(borderColor, icon)} ${lifted}` : lifted;
|
|
131
|
+
const padded = body.map((line, i) => (icons && i === 0 && !label ? ` ${icon} ${line}` : ` ${line}`));
|
|
132
|
+
// No blank row above the frame: pi puts a `Spacer(1)` in front of a
|
|
133
|
+
// compaction, branch and skill box already (interactive-mode), and the
|
|
134
|
+
// border is a separation in itself. A second row read as a stray gap
|
|
135
|
+
// (issue #29).
|
|
136
|
+
const out = drawFrame(padded, w, theme, borderColor);
|
|
137
|
+
if (label) out[0] = labeledTopBorder(w, theme, borderColor, clipToWidth(label, Math.max(1, w - 6)));
|
|
138
|
+
this.__msgFrameCache = { width: w, sample, revision, lines: content, out };
|
|
139
|
+
return out;
|
|
140
|
+
} catch {
|
|
141
|
+
return originalRender.call(this, width);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
148
|
+
// Container boxes (user messages / extension custom messages)
|
|
149
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
150
|
+
|
|
151
|
+
// OSC 133 zone markers UserMessageComponent puts on its first/last line —
|
|
152
|
+
// terminals use them for prompt navigation, so the framed output keeps them
|
|
153
|
+
// in the same positions.
|
|
154
|
+
const OSC133_ZONE_START = "\x1b]133;A\x07";
|
|
155
|
+
const OSC133_ZONE_END = "\x1b]133;B\x07";
|
|
156
|
+
const OSC133_ZONE_FINAL = "\x1b]133;C\x07";
|
|
157
|
+
|
|
158
|
+
interface ContainerBoxInternals {
|
|
159
|
+
children: Array<{ render(width: number): string[] }>;
|
|
160
|
+
/** CustomMessageComponent's default box; absent on UserMessageComponent. */
|
|
161
|
+
box?: { render(width: number): string[] };
|
|
162
|
+
/** Set when an extension renderer supplies its own styled component. */
|
|
163
|
+
customComponent?: CustomRender;
|
|
164
|
+
render(width: number): string[];
|
|
165
|
+
/** Where the extension component's rows ended up inside the frame we drew. */
|
|
166
|
+
__customFrame?: { target: CustomRender; top: number; trimTop: number; inset: number; width: number; height: number; rows: number };
|
|
167
|
+
/** A notice Tidy drew itself: the rows a click on it may land on. */
|
|
168
|
+
__noticeFrame?: { rows: number };
|
|
169
|
+
__msgFrameCache?: { width: number; sample: string; revision: number; labelMinute: number; labelTime: number | undefined; lines: string[]; out: string[] };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
interface CustomRender {
|
|
173
|
+
render(width: number): string[];
|
|
174
|
+
handleMouse?(event: unknown): unknown;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* User and extension custom messages are `Container`s whose visible box is an
|
|
179
|
+
* inner `Box` with a filled background. The frame replaces that chrome: the
|
|
180
|
+
* inner box renders one frame-width narrower, its background fills and
|
|
181
|
+
* vertical padding rows are stripped, and the rounded border takes over —
|
|
182
|
+
* the box's own horizontal padding is kept as the insets.
|
|
183
|
+
*/
|
|
184
|
+
export interface ContainerBoxOptions {
|
|
185
|
+
icons?: boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Frame user messages too (`userMessageStyle: "frame"`). The default
|
|
188
|
+
* "rail" style is drawn by user-message.ts. While false, this patch leaves
|
|
189
|
+
* UserMessageComponent alone (and is not installed if it never was).
|
|
190
|
+
*/
|
|
191
|
+
includeUser?: boolean;
|
|
192
|
+
/** Align the framed user message and its label; other message kinds stay left. */
|
|
193
|
+
userAlign?: "left" | "right";
|
|
194
|
+
/** The widest a framed user message's text gets: `"auto"`, a fraction of the width, or columns. */
|
|
195
|
+
userMaxWidth?: number | "auto";
|
|
196
|
+
/** The framed user message's time next to its icon. */
|
|
197
|
+
userTime?: boolean;
|
|
198
|
+
/** `tidy.messageTimeFormat`. */
|
|
199
|
+
timeFormat?: string;
|
|
200
|
+
/** `tidy.messageHeaderStyle`. */
|
|
201
|
+
headerStyle?: HeaderStyle;
|
|
202
|
+
/** False turns installed patches into pass-throughs and installs nothing new. */
|
|
203
|
+
enabled?: boolean;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
interface ResolvedContainerOptions {
|
|
207
|
+
enabled: boolean;
|
|
208
|
+
borderColors: { user: string; custom: string };
|
|
209
|
+
icons: boolean;
|
|
210
|
+
includeUser: boolean;
|
|
211
|
+
userAlign: "left" | "right";
|
|
212
|
+
userMaxWidth: number | "auto";
|
|
213
|
+
userTime: boolean;
|
|
214
|
+
timeFormat: string;
|
|
215
|
+
headerStyle: HeaderStyle;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const containerOptions = liveSlot<ResolvedContainerOptions | undefined>("container-box-options", undefined);
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* A user message's frame hugs its text: a one-word prompt gets a small box
|
|
222
|
+
* instead of a full-width one, and a long one still stops at the full width, or
|
|
223
|
+
* at `userMessageMaxWidth` when that is narrower. The text keeps the one-cell
|
|
224
|
+
* inset it has on the left on its right as well.
|
|
225
|
+
* The box stays wider than its label: `╭─ ` and ` ─╮` alone leave the top border
|
|
226
|
+
* a stub on each side, which reads as a cramped label and not as a border (#49).
|
|
227
|
+
*/
|
|
228
|
+
function hugContent(content: string[], width: number, labelWidth: number): { lines: string[]; width: number } {
|
|
229
|
+
const lines = content.map(trimTrailingSpaces);
|
|
230
|
+
const widest = lines.reduce((max, line) => Math.max(max, visibleWidth(line)), 0);
|
|
231
|
+
const floor = labelWidth > 0 ? labelWidth + 10 : 0;
|
|
232
|
+
return { lines, width: Math.min(width, Math.max(widest + 3, floor)) };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* How wide a framed user message's text renders, in cells: everything inside the
|
|
237
|
+
* border by default, otherwise the `userMessageMaxWidth` cap. A cap below 1 is a
|
|
238
|
+
* fraction of the width pi gave the message. The frame then hugs the wrapped
|
|
239
|
+
* text, and `hugContent` still keeps room for the label.
|
|
240
|
+
*/
|
|
241
|
+
function userTextWidth(maxWidth: number | "auto", width: number): number {
|
|
242
|
+
const inside = width - 2;
|
|
243
|
+
if (maxWidth === "auto") return inside;
|
|
244
|
+
const columns = maxWidth <= 1 ? maxWidth * width : maxWidth;
|
|
245
|
+
return Math.max(1, Math.min(inside, Math.floor(columns)));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Rows of a notice's own text shown before it is opened. */
|
|
249
|
+
const NOTICE_LINES = 1;
|
|
250
|
+
/** Open state per message object: pi keeps the message across rebuilds. */
|
|
251
|
+
const noticeOpen = new WeakMap<object, boolean>();
|
|
252
|
+
/** Markdown costs more than the frame around it, so its rows are kept per message. */
|
|
253
|
+
const noticeBodies = new WeakMap<object, { key: string; lines: string[] }>();
|
|
254
|
+
|
|
255
|
+
/** The notice's own text as rows: one row while closed, everything once open. */
|
|
256
|
+
function noticeBody(message: object & { content?: unknown }, width: number, open: boolean, theme: ThemeLike, label: NoticeLabel): string[] {
|
|
257
|
+
const text = noticeBodyText(message, label).trim();
|
|
258
|
+
if (!text) return [];
|
|
259
|
+
const key = `${width}:${open}:${label.name ?? ""}:${label.status ?? ""}`;
|
|
260
|
+
const cached = noticeBodies.get(message);
|
|
261
|
+
if (cached?.key === key) return cached.lines;
|
|
262
|
+
const rows = new Markdown(text, 0, 0, getMarkdownTheme(), { color: (line: string) => theme.fg("customMessageText", line) }).render(width);
|
|
263
|
+
const lines = open ? rows : rows.slice(0, NOTICE_LINES);
|
|
264
|
+
noticeBodies.set(message, { key, lines });
|
|
265
|
+
return lines;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* A notice Tidy draws itself: `╭─ <icon> worker needs attention ─╮`, the text
|
|
270
|
+
* the model read as the body, and a click that opens the rest of it. The
|
|
271
|
+
* extension's own component is not drawn — its first row is the label that just
|
|
272
|
+
* went into the border, and its `(ctrl+o to expand)` was chrome Tidy does not
|
|
273
|
+
* put on a row that is already a header.
|
|
274
|
+
*/
|
|
275
|
+
function frameNotice(
|
|
276
|
+
component: ContainerBoxInternals & { message?: { customType?: unknown; content?: unknown } },
|
|
277
|
+
notice: NoticeLabel,
|
|
278
|
+
width: number,
|
|
279
|
+
live: ResolvedContainerOptions,
|
|
280
|
+
theme: ThemeLike,
|
|
281
|
+
): string[] {
|
|
282
|
+
const { icons } = live;
|
|
283
|
+
const tone = notice.tone ?? live.borderColors.custom;
|
|
284
|
+
const w = Math.max(4, width);
|
|
285
|
+
const contentWidth = Math.max(1, icons ? w - 4 : w - 2);
|
|
286
|
+
const message = component.message ?? {};
|
|
287
|
+
const open = noticeOpen.get(message) ?? (component as unknown as { _expanded?: boolean })._expanded === true;
|
|
288
|
+
const body = noticeBody(message, contentWidth, open, theme, notice);
|
|
289
|
+
const label = [
|
|
290
|
+
icons ? theme.fg(tone, TOOL_ICONS.subagent!) : "",
|
|
291
|
+
notice.name ?? "",
|
|
292
|
+
notice.status ? theme.fg(tone, notice.status) : "",
|
|
293
|
+
]
|
|
294
|
+
.filter(Boolean)
|
|
295
|
+
.join(" ");
|
|
296
|
+
const clipped = clipToWidth(label, Math.max(1, w - 6));
|
|
297
|
+
const out = drawFrame(body.map((line) => ` ${line}`), w, theme, tone);
|
|
298
|
+
// The glyph fills the cell the width table gives it, so the label is one cell
|
|
299
|
+
// wider than measured whenever it leads with one.
|
|
300
|
+
out[0] = labeledTopBorder(w, theme, tone, clipped, "left", 1, visibleWidth(clipped) + (icons ? 1 : 0));
|
|
301
|
+
component.__noticeFrame = { rows: out.length };
|
|
302
|
+
return out;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function customKindIcon(component: ContainerBoxInternals, icons: boolean): string {
|
|
306
|
+
if (!icons) return "";
|
|
307
|
+
const type = (component as unknown as { message?: { customType?: unknown } }).message?.customType;
|
|
308
|
+
// pi-subagents names its fourteen notice types `subagent-…`/`subagent_…`; the
|
|
309
|
+
// rest are extensions with no glyph of their own, and get the custom one.
|
|
310
|
+
return typeof type === "string" && type.startsWith("subagent") ? TOOL_ICONS.subagent! : MESSAGE_ICONS.custom;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Frame a message whose body an extension renderer drew.
|
|
315
|
+
*
|
|
316
|
+
* The rows are the extension's, so they keep their own ordering and are only
|
|
317
|
+
* trimmed of the blank edges its box wanted around itself — the frame is the
|
|
318
|
+
* separation now. The component renders one frame narrower than the row it
|
|
319
|
+
* lands on, and everything inside it moved down one row and right two cells,
|
|
320
|
+
* which is what `customMouse` maps back.
|
|
321
|
+
*/
|
|
322
|
+
function frameCustom(
|
|
323
|
+
component: ContainerBoxInternals,
|
|
324
|
+
custom: CustomRender,
|
|
325
|
+
width: number,
|
|
326
|
+
live: ResolvedContainerOptions,
|
|
327
|
+
theme: ThemeLike,
|
|
328
|
+
): string[] {
|
|
329
|
+
const { icons } = live;
|
|
330
|
+
const borderColor = live.borderColors.custom;
|
|
331
|
+
const w = Math.max(4, width);
|
|
332
|
+
// Border, then the one-cell inset every body row gets (and two more when the
|
|
333
|
+
// icon is drawn into the border, which no row of the body has to make room for).
|
|
334
|
+
const contentWidth = Math.max(1, icons ? w - 4 : w - 2);
|
|
335
|
+
const raw = custom.render(contentWidth);
|
|
336
|
+
let start = 0;
|
|
337
|
+
let end = raw.length;
|
|
338
|
+
while (start < end && visibleText(raw[start]!).trim() === "") start++;
|
|
339
|
+
while (end > start && visibleText(raw[end - 1]!).trim() === "") end--;
|
|
340
|
+
const content = raw.slice(start, end);
|
|
341
|
+
component.__customFrame = content.length
|
|
342
|
+
? { target: custom, top: 1, trimTop: start, inset: 2, width: contentWidth, height: raw.length, rows: content.length }
|
|
343
|
+
: undefined;
|
|
344
|
+
if (content.length === 0) return [];
|
|
345
|
+
const out = drawFrame(content.map((line) => ` ${line}`), w, theme, borderColor);
|
|
346
|
+
const glyph = customKindIcon(component, icons);
|
|
347
|
+
if (glyph) out[0] = labeledTopBorder(w, theme, borderColor, theme.fg(borderColor, glyph));
|
|
348
|
+
return out;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export function patchContainerBoxes(
|
|
352
|
+
borderColors: { user: string; custom: string },
|
|
353
|
+
options: ContainerBoxOptions = {},
|
|
354
|
+
): void {
|
|
355
|
+
const { icons = false, includeUser = false, enabled = true, userAlign = "left", userMaxWidth = "auto", userTime = true, timeFormat = "auto", headerStyle = "inline" } = options;
|
|
356
|
+
containerOptions.set({ enabled, borderColors, icons, includeUser, userAlign, userMaxWidth, userTime, timeFormat, headerStyle });
|
|
357
|
+
if (!enabled) return;
|
|
358
|
+
if (includeUser) installUserMessageTimes();
|
|
359
|
+
|
|
360
|
+
const patch = (cls: { prototype: unknown }, kind: "user" | "custom") => {
|
|
361
|
+
const proto = cls.prototype as ContainerBoxInternals & { __toolboxFramed?: boolean };
|
|
362
|
+
if (proto.__toolboxFramed) return;
|
|
363
|
+
proto.__toolboxFramed = true;
|
|
364
|
+
|
|
365
|
+
const originalRender = proto.render;
|
|
366
|
+
const icon = MESSAGE_ICONS[kind];
|
|
367
|
+
const isUserMessage = kind === "user";
|
|
368
|
+
|
|
369
|
+
proto.render = function (this: ContainerBoxInternals, width: number): string[] {
|
|
370
|
+
const live = containerOptions.get();
|
|
371
|
+
const theme = getTheme();
|
|
372
|
+
if (!theme || !live?.enabled || !tidyEnabled.get() || (isUserMessage && !live.includeUser)) {
|
|
373
|
+
return originalRender.call(this, width);
|
|
374
|
+
}
|
|
375
|
+
try {
|
|
376
|
+
// pi-subagents' notices are drawn from the message itself: the label in
|
|
377
|
+
// the border, the text the model read as the body. Its own component is
|
|
378
|
+
// not drawn — the first row of it is that label, and its
|
|
379
|
+
// `(ctrl+o to expand)` was chrome on a row that is already a header.
|
|
380
|
+
const message = (this as unknown as { message?: Parameters<typeof noticeLabel>[0] & { content?: unknown } }).message;
|
|
381
|
+
const notice = noticeLabel(message);
|
|
382
|
+
// A notice with nothing to say keeps its renderer: an empty frame would
|
|
383
|
+
// hide whatever its rows were.
|
|
384
|
+
if (notice && (notice.name || notice.status || noticeText(message).trim())) {
|
|
385
|
+
this.__customFrame = undefined;
|
|
386
|
+
return frameNotice(this, notice, width, live, theme);
|
|
387
|
+
}
|
|
388
|
+
// Everything else: the extension owns the rows, Tidy owns the frame around
|
|
389
|
+
// them. Its fills go and its blank edges are trimmed, and its kind goes in
|
|
390
|
+
// the top border instead of the `[type]` row pi's own box would draw.
|
|
391
|
+
const custom = this.customComponent;
|
|
392
|
+
if (custom && typeof custom.render === "function") return frameCustom(this, custom, width, live, theme);
|
|
393
|
+
const box = this.box ?? this.children[0];
|
|
394
|
+
if (!box) return originalRender.call(this, width);
|
|
395
|
+
|
|
396
|
+
const { icons } = live;
|
|
397
|
+
const align = isUserMessage ? live.userAlign : "left";
|
|
398
|
+
const borderColor = live.borderColors[kind];
|
|
399
|
+
const userTime = isUserMessage && live.userTime ? (this as any)[USER_MESSAGE_TIME] : undefined;
|
|
400
|
+
// The clock is painted into the border every frame, but its text only
|
|
401
|
+
// changes when the day it belongs to rolls over. Key the cache on the
|
|
402
|
+
// minute the label was built for and format the two dates on a miss,
|
|
403
|
+
// not on every rendered row.
|
|
404
|
+
const labelMinute = isUserMessage && icons && typeof userTime === "number" ? Math.floor(Date.now() / 60_000) : 0;
|
|
405
|
+
const w = Math.max(4, width);
|
|
406
|
+
// A custom message's icon takes two cells of its first row; rendering
|
|
407
|
+
// the box narrower keeps every line lossless. A user message carries
|
|
408
|
+
// its icon in the label instead, and its text stops at its own cap.
|
|
409
|
+
const iconInRow = icons && !isUserMessage;
|
|
410
|
+
const boxWidth = isUserMessage ? userTextWidth(live.userMaxWidth, w) : w - 2;
|
|
411
|
+
const raw = box.render(iconInRow ? w - 4 : boxWidth).map(stripBackgroundFills);
|
|
412
|
+
let content = trimBlankEdges(raw);
|
|
413
|
+
if (content.length === 0) return [];
|
|
414
|
+
// A custom message's `[type]` row is a title too, and goes into the top
|
|
415
|
+
// border with the spacer under it. Only the custom kind: a user
|
|
416
|
+
// message's first row is its own text, which may well be a bare
|
|
417
|
+
// `[word]`, and a label there would be the user's, not ours.
|
|
418
|
+
const customType = (this as any).message?.customType;
|
|
419
|
+
const labelText = !isUserMessage && typeof customType === "string" ? `[${customType}]` : "";
|
|
420
|
+
const lifted = labelText && visibleText(content[0]).trim() === labelText ? trimTrailingSpaces(content[0]).trim() : "";
|
|
421
|
+
if (lifted) content = trimBlankEdges(content.slice(1));
|
|
422
|
+
if (iconInRow && !lifted) content[0] = ` ${icon} ${content[0].replace(/^ +/, "")}`;
|
|
423
|
+
const label = lifted && icons ? `${theme.fg(borderColor, icon)} ${lifted}` : lifted;
|
|
424
|
+
|
|
425
|
+
const sample = theme.fg(borderColor, "·");
|
|
426
|
+
const cache = this.__msgFrameCache;
|
|
427
|
+
const revision = optionsRevision();
|
|
428
|
+
if (
|
|
429
|
+
cache &&
|
|
430
|
+
cache.width === w &&
|
|
431
|
+
cache.sample === sample &&
|
|
432
|
+
cache.revision === revision &&
|
|
433
|
+
cache.labelMinute === labelMinute &&
|
|
434
|
+
cache.labelTime === userTime &&
|
|
435
|
+
sameLines(cache.lines, content)
|
|
436
|
+
)
|
|
437
|
+
return cache.out;
|
|
438
|
+
|
|
439
|
+
const header = isUserMessage && icons
|
|
440
|
+
? messageHeader(
|
|
441
|
+
theme,
|
|
442
|
+
borderColor,
|
|
443
|
+
theme.fg(borderColor, icon),
|
|
444
|
+
typeof userTime === "number" ? italicDim(theme, formatMessageTime(userTime, live.timeFormat)) : "",
|
|
445
|
+
live.headerStyle,
|
|
446
|
+
w,
|
|
447
|
+
)
|
|
448
|
+
: undefined;
|
|
449
|
+
|
|
450
|
+
// A leading blank row separates the box from the transcript above it.
|
|
451
|
+
// Custom messages need it: pi adds no spacer of its own there. A user
|
|
452
|
+
// message gets a `Spacer(1)` from pi already, and its top border is a
|
|
453
|
+
// separation in itself, so a second row reads as a stray gap.
|
|
454
|
+
let out: string[];
|
|
455
|
+
if (isUserMessage) {
|
|
456
|
+
// `╭─ ─╮`: the icon is the frame's, so the text starts on its own
|
|
457
|
+
// row and copies without it. No icons, no label: a plain top border.
|
|
458
|
+
const framed = hugContent(content, w, visibleWidth(header?.label ?? ""));
|
|
459
|
+
const frame = drawFrame(framed.lines, framed.width, theme, borderColor);
|
|
460
|
+
if (header) frame[0] = labeledTopBorder(framed.width, theme, borderColor, header.label, align, header.lead);
|
|
461
|
+
const indent = align === "right" ? " ".repeat(w - framed.width) : "";
|
|
462
|
+
out = indent ? frame.map((line) => indent + line) : frame;
|
|
463
|
+
} else {
|
|
464
|
+
const framed = drawFrame(content, w, theme, borderColor);
|
|
465
|
+
if (label) framed[0] = labeledTopBorder(w, theme, borderColor, clipToWidth(label, Math.max(1, w - 6)));
|
|
466
|
+
out = ["", ...framed];
|
|
467
|
+
}
|
|
468
|
+
if (isUserMessage) {
|
|
469
|
+
out[0] = OSC133_ZONE_START + out[0];
|
|
470
|
+
out[out.length - 1] = OSC133_ZONE_END + OSC133_ZONE_FINAL + out[out.length - 1];
|
|
471
|
+
}
|
|
472
|
+
this.__msgFrameCache = { width: w, sample, revision, labelMinute, labelTime: userTime, lines: content, out };
|
|
473
|
+
return out;
|
|
474
|
+
} catch {
|
|
475
|
+
this.__customFrame = undefined;
|
|
476
|
+
return originalRender.call(this, width);
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
// The rows inside the frame are the extension's, and the border moved them
|
|
481
|
+
// down one row and right two cells. pi's own handler would measure the
|
|
482
|
+
// child, not the frame, so without this a click lands a row above what it
|
|
483
|
+
// points at, and its column is off by the border.
|
|
484
|
+
const inheritedMouse = (proto as unknown as { handleMouse?: (event: any) => unknown }).handleMouse;
|
|
485
|
+
(proto as unknown as Record<string, unknown>).handleMouse = function (this: ContainerBoxInternals, event: any) {
|
|
486
|
+
const live = containerOptions.get();
|
|
487
|
+
if (!live?.enabled || !tidyEnabled.get()) return inheritedMouse?.call(this, event);
|
|
488
|
+
// A notice Tidy drew has no component under the pointer: the whole frame
|
|
489
|
+
// is the switch, and the rows are its own.
|
|
490
|
+
const notice = this.__noticeFrame;
|
|
491
|
+
if (notice) {
|
|
492
|
+
if (event?.y < 0 || event.y >= notice.rows) return undefined;
|
|
493
|
+
if (event.type !== "click" || (event.button !== "left" && event.button !== 0)) return undefined;
|
|
494
|
+
const message = (this as unknown as { message?: object }).message;
|
|
495
|
+
if (message) noticeOpen.set(message, !(noticeOpen.get(message) ?? (this as unknown as { _expanded?: boolean })._expanded === true));
|
|
496
|
+
return { handled: true, render: true };
|
|
497
|
+
}
|
|
498
|
+
const geometry = this.__customFrame;
|
|
499
|
+
const inside = geometry && event?.y >= geometry.top && event.y < geometry.top + geometry.rows;
|
|
500
|
+
if (!geometry || !inside || typeof geometry.target.handleMouse !== "function") {
|
|
501
|
+
return inheritedMouse?.call(this, event);
|
|
502
|
+
}
|
|
503
|
+
return geometry.target.handleMouse({
|
|
504
|
+
...event,
|
|
505
|
+
x: event.x - geometry.inset,
|
|
506
|
+
y: event.y - geometry.top + geometry.trimTop,
|
|
507
|
+
width: geometry.width,
|
|
508
|
+
height: geometry.height,
|
|
509
|
+
});
|
|
510
|
+
};
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
if (includeUser) patch(UserMessageComponent, "user");
|
|
514
|
+
patch(CustomMessageComponent, "custom");
|
|
515
|
+
}
|