pi-odd-worktree 3.10.0 → 3.11.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/LICENSE +21 -0
- package/README.md +5 -0
- package/extensions/odd-worktree-tools.ts +169 -5
- package/lib/user-prompt-card.js +477 -0
- package/package.json +1 -1
- package/tools/package-surface.test.js +4 -2
- package/tools/user-prompt-card.test.js +963 -0
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// The user's own prompt, drawn as a card on this package's substrate
|
|
3
|
+
// (`lib/shell-card.js`), and only while the bridge's `Card activity` line answers this
|
|
4
|
+
// package's `activity` design.
|
|
5
|
+
//
|
|
6
|
+
// The bridge has no surface for a user prompt: it owns tool designs, message designs and
|
|
7
|
+
// its rail, and none of them is reached by a prompt. So this card is a PROTOTYPE SHIM —
|
|
8
|
+
// `UserMessageComponent.prototype.render` is wrapped once per process — and the class is
|
|
9
|
+
// handed in by the caller (`extensions/odd-worktree-tools.ts` resolves it with the same
|
|
10
|
+
// lazy dynamic `import()` every other host package here uses). This module imports nothing
|
|
11
|
+
// from Pi, so a stub class is enough to exercise all of it.
|
|
12
|
+
//
|
|
13
|
+
// WHAT IT DRAWS, and what it never touches:
|
|
14
|
+
// * THE HEADING is `✿ You`, tone `info`, with the theme's `accent` role for the name AND
|
|
15
|
+
// for the whole closed box (`railRole`) — corners, lead, rule, sides, rail and bottom.
|
|
16
|
+
// That one role around the entire frame is what tells this card apart from the tool
|
|
17
|
+
// cards, which paint their tone; no glyph override, no subtitle and no timestamp are
|
|
18
|
+
// added (the `✿` is the substrate's default mark, and `gentle-timestamps` owns time).
|
|
19
|
+
// * THE BODY is Pi's own rendering of the prompt. The component's Markdown child is
|
|
20
|
+
// rebuilt from its OWN constructor with the user message background dropped
|
|
21
|
+
// (`bgColor: undefined`), no side padding (the frame is the padding) and the host's own
|
|
22
|
+
// `options`, so `preserveOrderedListMarkers`, `preserveBackslashEscapes` and every
|
|
23
|
+
// extension markdown transformer keep working. Its rows arrive ALREADY PAINTED, so they
|
|
24
|
+
// are placed with `cardPaintedRows` — never `cardBodyRows`, which paints a role over
|
|
25
|
+
// them and would destroy the ANSI. The text colour stays exactly what the body's own
|
|
26
|
+
// paint says.
|
|
27
|
+
// * THE STYLE is the shell's, read on every paint by `floatRows` (`lib/card-style.js`):
|
|
28
|
+
// `float` and `neon` both follow live, and this module writes no style code at all.
|
|
29
|
+
// * THE PROMPT ZONE stays where Pi put it. `UserMessageComponent.render` PREFIXES its first
|
|
30
|
+
// row with the OSC 133 zone-start sequence and PREFIXES its last row with the zone-end
|
|
31
|
+
// marker followed by the final one (`last = OSC133_ZONE_END + OSC133_ZONE_FINAL + last`),
|
|
32
|
+
// so the terminal knows where a prompt begins and ends (click-to-jump and shell integration
|
|
33
|
+
// follow those markers). A card that replaces those rows carries the SAME markers in the
|
|
34
|
+
// same order and position on its own first and last row — once each, never twice, and never
|
|
35
|
+
// on the native path, which stays byte for byte.
|
|
36
|
+
// * THE GATE is asked INSIDE the patched `render`, on every paint, never cached: the
|
|
37
|
+
// bridge's published reader (the `Symbol.for("gentle-shell-bridge/api/v1")` slot after
|
|
38
|
+
// its schema and version gate) must answer `{ kind: "design", design: { id:
|
|
39
|
+
// <this package's activity design> } }` for `bash`, the line every other card asks.
|
|
40
|
+
// UNLIKE the file cards, this one fails CLOSED: an absent bridge, an absent reader, a
|
|
41
|
+
// reader that throws, any other answer and a width too small to draw a frame all hand
|
|
42
|
+
// the host's own rows back — the very array `render` returned — byte for byte (S6).
|
|
43
|
+
// * THE COST, stated honestly, per PAINT: ONE GATE READ ALWAYS, PLUS ONE STYLE READ ONLY WHILE
|
|
44
|
+
// THE CARD ACTUALLY DRAWS. The gate asks the bridge's reader on every paint — its reader
|
|
45
|
+
// queries the selection store on every call, and that read is what makes the flip live, so
|
|
46
|
+
// it is never cached. Any answer but this package's design returns BEFORE the style is
|
|
47
|
+
// asked, so a native frame pays the gate read alone; a frame that does draw then pays one
|
|
48
|
+
// more live read, `floatRows` asking the shell's style through `lib/card-style.js` (the live
|
|
49
|
+
// slot Gentle Shell writes, or `<home>/card-style.json` when nothing is published there).
|
|
50
|
+
// Both are deliberate. The memo below covers only the DRAWING (the Markdown rows and the
|
|
51
|
+
// composed frame parts), keyed on width, paint and rendered rows, so a steady frame
|
|
52
|
+
// recomposes nothing.
|
|
53
|
+
//
|
|
54
|
+
// The wrapper never throws: any error on this side answers the original rows. `uninstall()`
|
|
55
|
+
// restores the original `render`, and the extension calls it on `session_shutdown`.
|
|
56
|
+
|
|
57
|
+
const {
|
|
58
|
+
CARD_TONE,
|
|
59
|
+
cardBottom,
|
|
60
|
+
cardInnerWidth,
|
|
61
|
+
cardPaintedRows,
|
|
62
|
+
cardTopRows,
|
|
63
|
+
floatRows,
|
|
64
|
+
} = require('./shell-card.js');
|
|
65
|
+
const { ACTIVITY_DESIGN_ID, bridgeApi } = require('./file-card-resolver.js');
|
|
66
|
+
const { themeFor } = require('./file-card.js');
|
|
67
|
+
|
|
68
|
+
// The one heading this card wears: the mark is the substrate's default `✿`, the name is the
|
|
69
|
+
// person, and the role is the accent for the name and for the whole closed box. No subtitle
|
|
70
|
+
// (there is one fact: the prompt) and no timestamp (`gentle-timestamps` owns the clock).
|
|
71
|
+
const IDENTITY = Object.freeze({
|
|
72
|
+
title: 'You',
|
|
73
|
+
tone: CARD_TONE.INFO,
|
|
74
|
+
titleRole: 'accent',
|
|
75
|
+
railRole: 'accent',
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// Below this the frame's two side rails and the glyph column leave no room for one body row
|
|
79
|
+
// (`lib/message-card.js` states the same floor), so the host's rows come straight back
|
|
80
|
+
// instead of a heading painted over nothing.
|
|
81
|
+
const MIN_PAINT_WIDTH = 6;
|
|
82
|
+
|
|
83
|
+
// The line every card of this package asks the bridge about, and the members the injected
|
|
84
|
+
// measurement seam must carry (the host TUI helpers this package never imports).
|
|
85
|
+
const BRIDGE_TOOL = 'bash';
|
|
86
|
+
const METRICS_FUNCTIONS = ['visibleWidth', 'truncateToWidth', 'wrapTextWithAnsi'];
|
|
87
|
+
|
|
88
|
+
// Pi keeps its live theme instance on a process-wide slot (`gentle-timestamps` reads the same
|
|
89
|
+
// one), so no theme import is needed and a theme switched mid-session reaches the next frame.
|
|
90
|
+
const PI_THEME_SLOT = Symbol.for('@earendil-works/pi-coding-agent:theme');
|
|
91
|
+
|
|
92
|
+
// The process-wide shim state: the prototypes this package has wrapped with their ORIGINAL
|
|
93
|
+
// `render`, the one wrapper, and the logic the most recently evaluated module installed. It
|
|
94
|
+
// lives on `globalThis` behind a `Symbol.for` key so `/reload` reuses the same cell: the
|
|
95
|
+
// wrapper installed by the first evaluation keeps calling the newest logic.
|
|
96
|
+
const STATE_KEY = Symbol.for('gentle-pi.user-prompt-card');
|
|
97
|
+
|
|
98
|
+
// The prompt zone's own markers, exactly as `UserMessageComponent.render` writes them
|
|
99
|
+
// (`dist/modes/interactive/components/user-message.js`): the zone opens on the first row and
|
|
100
|
+
// closes with END followed by FINAL on the last. They are OSC escapes, so they occupy no
|
|
101
|
+
// column and no measurement here ever sees them: the card's rows are composed first, and the
|
|
102
|
+
// markers are put on the finished first and last row.
|
|
103
|
+
const OSC133_ZONE_START = '\x1b]133;A\x07';
|
|
104
|
+
const OSC133_ZONE_END = '\x1b]133;B\x07';
|
|
105
|
+
const OSC133_ZONE_FINAL = '\x1b]133;C\x07';
|
|
106
|
+
const OSC133_ZONE_CLOSE = OSC133_ZONE_END + OSC133_ZONE_FINAL;
|
|
107
|
+
|
|
108
|
+
// A host that hands over no theme: the text is drawn plain rather than not at all.
|
|
109
|
+
const PLAIN_THEME = { fg: (_role, text) => text };
|
|
110
|
+
|
|
111
|
+
const asObject = (value) => (value !== null && typeof value === 'object' ? value : {});
|
|
112
|
+
const hasFunction = (value, name) => value !== null && typeof value === 'object' && typeof value[name] === 'function';
|
|
113
|
+
const usableMetrics = (value) => (METRICS_FUNCTIONS.every((name) => hasFunction(value, name)) ? value : undefined);
|
|
114
|
+
|
|
115
|
+
// ---- the one switch (S1/S2/S6) ----
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Whether the activity design draws the `bash` line right now, asked per paint and never
|
|
119
|
+
* cached. STRICT and FAIL-CLOSED: only `{ kind: "design", design: { id: <ours> } }` paints,
|
|
120
|
+
* and a bridge that cannot answer at all is a no — an unavailable gate never changes a row.
|
|
121
|
+
*/
|
|
122
|
+
function activitySelected() {
|
|
123
|
+
try {
|
|
124
|
+
const api = bridgeApi();
|
|
125
|
+
if (api === undefined || typeof api.resolveToolDesignSelection !== 'function') return false;
|
|
126
|
+
const answer = asObject(api.resolveToolDesignSelection(BRIDGE_TOOL));
|
|
127
|
+
return answer.kind === 'design' && asObject(answer.design).id === ACTIVITY_DESIGN_ID;
|
|
128
|
+
} catch {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ---- the seams, read on every paint ----
|
|
134
|
+
|
|
135
|
+
/** The measurement seam: an object, or a function asked now (the host's TUI may arrive late). */
|
|
136
|
+
function readMetrics(seam) {
|
|
137
|
+
try {
|
|
138
|
+
return usableMetrics(typeof seam === 'function' ? seam() : seam);
|
|
139
|
+
} catch {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The theme to paint with: the injected seam when there is one, else Pi's live instance from
|
|
146
|
+
* its process-wide slot. A seam that throws or answers nothing usable leaves the plain one.
|
|
147
|
+
*/
|
|
148
|
+
function readTheme(seam) {
|
|
149
|
+
try {
|
|
150
|
+
if (typeof seam === 'function') return seam();
|
|
151
|
+
if (seam !== undefined) return seam;
|
|
152
|
+
return globalThis[PI_THEME_SLOT];
|
|
153
|
+
} catch {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// ---- the body: Pi's own Markdown, rebuilt without the user message background (S5) ----
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* The child's own Markdown as a clone with no background and no padding at all, or `undefined`
|
|
162
|
+
* when the shape is not Pi's Markdown — a missing child, a non-numeric `paddingX`, no theme,
|
|
163
|
+
* or a constructor that is not a function. Every mismatch means "answer native", never a
|
|
164
|
+
* guess.
|
|
165
|
+
*
|
|
166
|
+
* NO PADDING: the frame is the padding. Pi builds the user prompt's Markdown with
|
|
167
|
+
* `paddingX: outputPad, paddingY: 1` and its renderer pays for both with blank rows — the
|
|
168
|
+
* terminal charges every row — so the clone is built with `0` for both, exactly as the
|
|
169
|
+
* substrate's other Markdown body is (`lib/file-card-markdown.js` builds its document renderer
|
|
170
|
+
* with `new Renderer(source, 0, 0, …)`). What a prompt costs is the frame plus its own lines.
|
|
171
|
+
* The child's own `paddingY` stays part of what the memo remembers about the CHILD, so a host
|
|
172
|
+
* that changes it still rebuilds the clone.
|
|
173
|
+
*
|
|
174
|
+
* `paint` is the theme THIS paint uses, and it is part of the memo: Pi's `Markdown` caches its
|
|
175
|
+
* painted lines on (text, width) alone and the host invalidates only its OWN components, so a
|
|
176
|
+
* clone kept across a theme switch would keep painting the old colours while a freshly built
|
|
177
|
+
* prompt already wears the new ones. When the theme changed, the clone is REBUILT (a new
|
|
178
|
+
* instance starts with an empty cache) from the child's own live MarkdownTheme.
|
|
179
|
+
*/
|
|
180
|
+
function promptMarkdown(child, paint) {
|
|
181
|
+
if (child === null || typeof child !== 'object') return undefined;
|
|
182
|
+
if (typeof child.paddingX !== 'number' || !Number.isFinite(child.paddingX)) return undefined;
|
|
183
|
+
if (child.theme === undefined || child.theme === null) return undefined;
|
|
184
|
+
const Constructor = child.constructor;
|
|
185
|
+
if (typeof Constructor !== 'function' || Constructor === Object) return undefined;
|
|
186
|
+
const remembered = cloneMemo.get(child);
|
|
187
|
+
if (
|
|
188
|
+
remembered !== undefined
|
|
189
|
+
&& remembered.paint === paint
|
|
190
|
+
&& remembered.Constructor === Constructor
|
|
191
|
+
&& remembered.text === child.text
|
|
192
|
+
&& remembered.paddingX === child.paddingX
|
|
193
|
+
&& remembered.paddingY === child.paddingY
|
|
194
|
+
&& remembered.theme === child.theme
|
|
195
|
+
&& remembered.options === child.options
|
|
196
|
+
&& sameStyle(remembered.defaultTextStyle, child.defaultTextStyle)
|
|
197
|
+
) return remembered.clone;
|
|
198
|
+
let clone;
|
|
199
|
+
try {
|
|
200
|
+
clone = new Constructor(
|
|
201
|
+
child.text,
|
|
202
|
+
// `paddingX` and `paddingY` are the frame's job now: the card adds its own columns,
|
|
203
|
+
// and its own blank rows where the style wants them.
|
|
204
|
+
0,
|
|
205
|
+
0,
|
|
206
|
+
child.theme,
|
|
207
|
+
{ ...asObject(child.defaultTextStyle), bgColor: undefined },
|
|
208
|
+
child.options,
|
|
209
|
+
);
|
|
210
|
+
} catch {
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
cloneMemo.set(child, {
|
|
214
|
+
paint,
|
|
215
|
+
Constructor,
|
|
216
|
+
clone,
|
|
217
|
+
text: child.text,
|
|
218
|
+
paddingX: child.paddingX,
|
|
219
|
+
paddingY: child.paddingY,
|
|
220
|
+
theme: child.theme,
|
|
221
|
+
options: child.options,
|
|
222
|
+
defaultTextStyle: child.defaultTextStyle,
|
|
223
|
+
});
|
|
224
|
+
return clone;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** The two text styles state the same colour and the same attributes, background aside. */
|
|
228
|
+
function sameStyle(a, b) {
|
|
229
|
+
const left = asObject(a);
|
|
230
|
+
const right = asObject(b);
|
|
231
|
+
for (const key of new Set([...Object.keys(left), ...Object.keys(right)])) {
|
|
232
|
+
if (key === 'bgColor') continue;
|
|
233
|
+
if (left[key] !== right[key]) return false;
|
|
234
|
+
}
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** The clone of one component's Markdown child for this paint, or `undefined` when it is not that shape. */
|
|
239
|
+
function componentMarkdown(self, paint) {
|
|
240
|
+
try {
|
|
241
|
+
const children = asObject(self).children;
|
|
242
|
+
return promptMarkdown(Array.isArray(children) ? children[0] : undefined, paint);
|
|
243
|
+
} catch {
|
|
244
|
+
return undefined;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** The rows the Markdown draws for `width`, already painted, or `undefined` when it draws none. */
|
|
249
|
+
function renderedRows(markdown, width) {
|
|
250
|
+
try {
|
|
251
|
+
const drawn = markdown.render(width);
|
|
252
|
+
if (!Array.isArray(drawn)) return undefined;
|
|
253
|
+
const rows = drawn.filter((row) => typeof row === 'string');
|
|
254
|
+
return rows.length > 0 ? rows : undefined;
|
|
255
|
+
} catch {
|
|
256
|
+
return undefined;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const sameRows = (a, b) => a === b || (a.length === b.length && a.every((row, index) => row === b[index]));
|
|
261
|
+
|
|
262
|
+
// ---- the card rows, memoized per component ----
|
|
263
|
+
|
|
264
|
+
/** The per-component clone memo (keyed on the host's Markdown child: no leak). */
|
|
265
|
+
const cloneMemo = new WeakMap();
|
|
266
|
+
/** The per-component card memo: (requested width, drawn width, paint, rendered rows) in, parts out. */
|
|
267
|
+
const cardMemo = new WeakMap();
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* One component's card parts for one draw — `{ head, body, bottom }`, the shape `floatRows`
|
|
271
|
+
* renders — or `undefined` when the body draws no row at all.
|
|
272
|
+
*
|
|
273
|
+
* WHAT THE MEMO IS KEYED ON, and why the requested width is in it: `inner` alone does not
|
|
274
|
+
* decide a row. `float` gives two columns to its margins, so float at 62 and neon at 60 both
|
|
275
|
+
* compose at inner 60 — with DIFFERENT chrome (`▎` rails against the rounded outline). The
|
|
276
|
+
* chrome in force is decided by the requested width and the style `floatRows` resolved for it
|
|
277
|
+
* (`floatOpener(theme, tone, target)`), so `target` and `inner` are both keyed: a style at the
|
|
278
|
+
* same inner width therefore always recomposes. The paint is keyed because the roles are the
|
|
279
|
+
* theme's, and the rendered rows because the body is Pi's own Markdown. A steady frame only
|
|
280
|
+
* compares the fields and hands the same parts back. No file, no child process and no store is
|
|
281
|
+
* read in here. A paint's live reads are the gate's selection read (always, above) and, only
|
|
282
|
+
* while the card actually draws, the shell's style read `floatRows` makes.
|
|
283
|
+
*/
|
|
284
|
+
function cardParts(self, markdown, target, inner, paint, metrics) {
|
|
285
|
+
const width = cardInnerWidth(inner);
|
|
286
|
+
const rows = renderedRows(markdown, width);
|
|
287
|
+
if (rows === undefined) return undefined;
|
|
288
|
+
const remembered = cardMemo.get(self);
|
|
289
|
+
if (
|
|
290
|
+
remembered !== undefined
|
|
291
|
+
&& remembered.markdown === markdown
|
|
292
|
+
&& remembered.target === target
|
|
293
|
+
&& remembered.inner === inner
|
|
294
|
+
&& remembered.paint === paint
|
|
295
|
+
&& sameRows(remembered.rows, rows)
|
|
296
|
+
) return remembered.parts;
|
|
297
|
+
const card = { ...IDENTITY };
|
|
298
|
+
const parts = {
|
|
299
|
+
head: cardTopRows(card, paint, inner, undefined, metrics),
|
|
300
|
+
// The body is never clipped: a prompt is not a preview, so every row is drawn.
|
|
301
|
+
body: cardPaintedRows(rows, IDENTITY.tone, paint, inner, { expanded: true }, metrics),
|
|
302
|
+
// No content rides the closing rule: the time belongs to `gentle-timestamps`.
|
|
303
|
+
bottom: cardBottom(IDENTITY.tone, paint, inner, undefined, metrics),
|
|
304
|
+
};
|
|
305
|
+
cardMemo.set(self, { markdown, target, inner, paint, rows, parts });
|
|
306
|
+
return parts;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* The card's rows with Pi's prompt-zone markers, written exactly as `user-message.js` writes
|
|
311
|
+
* them: `first = START + first`, then `last = END + FINAL + last` — the close pair PREFIXED on
|
|
312
|
+
* the last row, in that order, never appended after it. A row list that already carries them is
|
|
313
|
+
* handed back untouched, so a second pass (a stacked wrapper, a repaint) can never write a
|
|
314
|
+
* second copy, and an empty list stays empty. The markers are zero-width escapes and are added
|
|
315
|
+
* to rows that are already composed, so no width and no measurement changes.
|
|
316
|
+
*/
|
|
317
|
+
function zoneRows(rows) {
|
|
318
|
+
if (!Array.isArray(rows) || rows.length === 0) return rows;
|
|
319
|
+
if (rows[0].startsWith(OSC133_ZONE_START) || rows.at(-1).startsWith(OSC133_ZONE_CLOSE)) return rows;
|
|
320
|
+
const zoned = [...rows];
|
|
321
|
+
// Applied in Pi's own order, so a one-row list ends up `END + FINAL + START + row` exactly
|
|
322
|
+
// as Pi's sequential writes produce it.
|
|
323
|
+
zoned[0] = OSC133_ZONE_START + zoned[0];
|
|
324
|
+
zoned[zoned.length - 1] = OSC133_ZONE_CLOSE + zoned[zoned.length - 1];
|
|
325
|
+
return zoned;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* The rows one paint answers: the host's own rows for every answer but the activity design,
|
|
330
|
+
* the card for it. `rows` is exactly what the original `render` returned, so every refusal is
|
|
331
|
+
* byte for byte (S6).
|
|
332
|
+
*/
|
|
333
|
+
function paintPrompt(self, rows, width, seams) {
|
|
334
|
+
if (!Array.isArray(rows) || rows.length === 0) return rows;
|
|
335
|
+
if (!activitySelected()) return rows;
|
|
336
|
+
const target = Math.max(0, Math.floor(Number.isFinite(width) ? width : 0));
|
|
337
|
+
if (target < MIN_PAINT_WIDTH) return rows;
|
|
338
|
+
const metrics = readMetrics(seams.metrics);
|
|
339
|
+
if (metrics === undefined) return rows;
|
|
340
|
+
// The theme is read BEFORE the body: the clone is keyed on it, so a theme switched while the
|
|
341
|
+
// prompt is on screen repaints the body instead of serving the clone it was built under.
|
|
342
|
+
const paint = themeFor(readTheme(seams.theme), PLAIN_THEME);
|
|
343
|
+
const markdown = componentMarkdown(self, paint);
|
|
344
|
+
if (markdown === undefined) return rows;
|
|
345
|
+
try {
|
|
346
|
+
const drawn = floatRows(IDENTITY.tone, paint, target, (inner) => {
|
|
347
|
+
const parts = cardParts(self, markdown, target, inner, paint, metrics);
|
|
348
|
+
// A body that draws nothing is no card: the host's own rows are the honest answer,
|
|
349
|
+
// and the throw below is how that leaves `floatRows` untouched.
|
|
350
|
+
if (parts === undefined) throw new TypeError('the user prompt drew no row');
|
|
351
|
+
return parts;
|
|
352
|
+
}, IDENTITY.railRole);
|
|
353
|
+
// The one thing added AFTER the frame is composed: the prompt zone Pi marks on the rows
|
|
354
|
+
// this card replaces. Zero width, once each, and part of no memo key — they cannot change
|
|
355
|
+
// between paints, so a steady frame still recomposes nothing.
|
|
356
|
+
return zoneRows(drawn);
|
|
357
|
+
} catch {
|
|
358
|
+
return rows;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// ---- the prototype shim (S5) ----
|
|
363
|
+
|
|
364
|
+
/** The shim state, read without creating it. */
|
|
365
|
+
function readState() {
|
|
366
|
+
try {
|
|
367
|
+
const state = globalThis[STATE_KEY];
|
|
368
|
+
return state !== null && typeof state === 'object' ? state : undefined;
|
|
369
|
+
} catch {
|
|
370
|
+
return undefined;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** The shim state, created when it is absent, always with the list of wrapped prototypes. */
|
|
375
|
+
function stateSlot() {
|
|
376
|
+
const existing = readState();
|
|
377
|
+
if (existing !== undefined) {
|
|
378
|
+
if (!Array.isArray(existing.patched)) existing.patched = [];
|
|
379
|
+
return existing;
|
|
380
|
+
}
|
|
381
|
+
const state = { patched: [] };
|
|
382
|
+
globalThis[STATE_KEY] = state;
|
|
383
|
+
return state;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** The original `render` of the nearest wrapped prototype, so the wrapper always calls the host's own. */
|
|
387
|
+
function originalFor(state, self) {
|
|
388
|
+
if (self === null || self === undefined) return undefined;
|
|
389
|
+
let proto = Object.getPrototypeOf(self);
|
|
390
|
+
while (proto !== null && proto !== undefined) {
|
|
391
|
+
const entry = state.patched.find((kept) => kept.proto === proto);
|
|
392
|
+
if (entry !== undefined) return entry.original;
|
|
393
|
+
proto = Object.getPrototypeOf(proto);
|
|
394
|
+
}
|
|
395
|
+
return undefined;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* The one wrapper this process ever installs. It closes over the state OBJECT, not over one
|
|
400
|
+
* evaluation's module scope, so a `/reload` that leaves the state in place still reaches the
|
|
401
|
+
* logic the newest evaluation installed.
|
|
402
|
+
*/
|
|
403
|
+
function wrapperFor(state) {
|
|
404
|
+
return function render(width) {
|
|
405
|
+
const original = originalFor(state, this);
|
|
406
|
+
// Unreachable for a wrapped prototype; answering no rows beats throwing at the host.
|
|
407
|
+
if (typeof original !== 'function') return [];
|
|
408
|
+
const rows = original.call(this, width);
|
|
409
|
+
const logic = state.logic;
|
|
410
|
+
if (typeof logic !== 'function') return rows;
|
|
411
|
+
try {
|
|
412
|
+
return logic(this, rows, width);
|
|
413
|
+
} catch {
|
|
414
|
+
// A card must never cost the person their prompt (S6).
|
|
415
|
+
return rows;
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* `installUserPromptCard({ component, metrics, theme })` — wrap the class's `render` once per
|
|
422
|
+
* process and answer through the seams given now.
|
|
423
|
+
*
|
|
424
|
+
* Idempotent by construction: a prototype this package already wraps is not wrapped again, so
|
|
425
|
+
* `/reload`, a second session and a second evaluation add no second layer, and only the LIVE
|
|
426
|
+
* logic is replaced. A class that cannot draw (no prototype, no `render`) installs nothing and
|
|
427
|
+
* answers `{ installed: false }`.
|
|
428
|
+
*/
|
|
429
|
+
function installUserPromptCard(options) {
|
|
430
|
+
const opts = asObject(options);
|
|
431
|
+
const component = opts.component;
|
|
432
|
+
const proto = typeof component === 'function' ? component.prototype : undefined;
|
|
433
|
+
if (proto === null || typeof proto !== 'object' || typeof proto.render !== 'function') {
|
|
434
|
+
return { installed: false, uninstall: uninstallUserPromptCard };
|
|
435
|
+
}
|
|
436
|
+
const seams = { metrics: opts.metrics, theme: opts.theme };
|
|
437
|
+
const state = stateSlot();
|
|
438
|
+
if (typeof state.wrapper !== 'function') state.wrapper = wrapperFor(state);
|
|
439
|
+
if (!state.patched.some((kept) => kept.proto === proto)) {
|
|
440
|
+
state.patched.push({ proto, original: proto.render });
|
|
441
|
+
proto.render = state.wrapper;
|
|
442
|
+
}
|
|
443
|
+
// The most recently evaluated logic is the one every wrapper calls, from now on.
|
|
444
|
+
state.logic = (self, rows, width) => paintPrompt(self, rows, width, seams);
|
|
445
|
+
return { installed: true, uninstall: uninstallUserPromptCard };
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Restore every prototype this package wrapped, byte for byte, and forget the live logic. The
|
|
450
|
+
* state object stays on its slot so the same wrapper object can be reused by the next install;
|
|
451
|
+
* a prototype another plugin has re-wrapped in the meantime is left alone.
|
|
452
|
+
*/
|
|
453
|
+
function uninstallUserPromptCard() {
|
|
454
|
+
const state = readState();
|
|
455
|
+
if (state === undefined) return false;
|
|
456
|
+
const patched = Array.isArray(state.patched) ? state.patched : [];
|
|
457
|
+
for (const entry of patched) {
|
|
458
|
+
try {
|
|
459
|
+
if (entry.proto.render === state.wrapper) entry.proto.render = entry.original;
|
|
460
|
+
} catch {
|
|
461
|
+
// A frozen prototype keeps the wrapper; nothing else of the install survives it.
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
state.patched = [];
|
|
465
|
+
state.logic = undefined;
|
|
466
|
+
return true;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
module.exports = {
|
|
470
|
+
IDENTITY,
|
|
471
|
+
MIN_PAINT_WIDTH,
|
|
472
|
+
activitySelected,
|
|
473
|
+
installUserPromptCard,
|
|
474
|
+
paintPrompt,
|
|
475
|
+
promptMarkdown,
|
|
476
|
+
uninstallUserPromptCard,
|
|
477
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-odd-worktree",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.11.0",
|
|
4
4
|
"description": "Pi package for odd-worktree: the Git worktree tools a Pi session uses (create and list the checkouts the agent works in), the /warp worktree panel, the /odd ODD viewer, and the /issues GitHub issue reader. Needs only Pi and git: no companion app, no extra service.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
@@ -72,8 +72,10 @@ describe('manifest', () => {
|
|
|
72
72
|
// classifier and the one design registered on the bridge. The file cards add
|
|
73
73
|
// their own four modules — a classifier, the card, its resolver and the document
|
|
74
74
|
// body Pi's Markdown draws — and own no preference at all (S1): they are gated by
|
|
75
|
-
// the bridge's `Card activity` line, which is the bridge's, not ours.
|
|
76
|
-
|
|
75
|
+
// the bridge's `Card activity` line, which is the bridge's, not ours. The user
|
|
76
|
+
// prompt card is the one module that wraps a host class, and it adds no row, no
|
|
77
|
+
// command and no design of its own.
|
|
78
|
+
for (const kept of ['gh-issues.js', 'issue-preview.js', 'issues-view.js', 'gh-prs.js', 'pr-preview.js', 'prs-view.js', 'prs-rail-store.js', 'issues-section.js', 'warp-selector-view.js', 'odd-view.js', 'worktree-git.js', 'worktree-registry.js', 'worktree-session-guard.js', 'skill-read-guard.js', 'bash-activity.js', 'bash-inline-script.js', 'bash-design.js', 'bash-frame.js', 'diff-design.js', 'skill-odd-card.js', 'file-card.js', 'file-card-resolver.js', 'file-card-markdown.js', 'user-prompt-card.js']) {
|
|
77
79
|
assert.ok(shipped.includes(kept), `lib/${kept} is shipped`);
|
|
78
80
|
}
|
|
79
81
|
for (const gone of ['card-files-mode.js', 'cards-customize.js']) {
|