pi-odd-worktree 3.15.0 → 3.16.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/README.md +48 -25
- package/extensions/odd-worktree-tools.ts +64 -211
- package/lib/shell-api/api.ts +523 -0
- package/lib/shell-api/arm.ts +83 -0
- package/lib/shell-api/bus.ts +1389 -0
- package/lib/shell-api/capture.ts +1340 -0
- package/lib/shell-api/card-style-line.ts +112 -0
- package/lib/shell-api/card-style-rows.ts +390 -0
- package/lib/shell-api/card-style-transform.ts +862 -0
- package/lib/shell-api/card-style-wiring.ts +477 -0
- package/lib/shell-api/card-styles.ts +153 -0
- package/lib/shell-api/config.ts +71 -0
- package/lib/shell-api/extension.ts +247 -0
- package/lib/shell-api/hook.ts +322 -0
- package/lib/shell-api/index.ts +88 -0
- package/lib/shell-api/message-design-adoption.ts +549 -0
- package/lib/shell-api/message-design-rows.ts +313 -0
- package/lib/shell-api/message-design-wiring.ts +369 -0
- package/lib/shell-api/message-designs.ts +746 -0
- package/lib/shell-api/notices.ts +137 -0
- package/lib/shell-api/plugin-lines.ts +877 -0
- package/lib/shell-api/presence.ts +258 -0
- package/lib/shell-api/repaint.ts +351 -0
- package/lib/shell-api/row-labels.ts +146 -0
- package/lib/shell-api/row-names.ts +273 -0
- package/lib/shell-api/row-origin.ts +144 -0
- package/lib/shell-api/rows.ts +591 -0
- package/lib/shell-api/section-panel.ts +378 -0
- package/lib/shell-api/section-pointer.ts +164 -0
- package/lib/shell-api/sections-preview.ts +275 -0
- package/lib/shell-api/sections-rows.ts +133 -0
- package/lib/shell-api/sidebar-render.ts +705 -0
- package/lib/shell-api/sidebar.ts +1126 -0
- package/lib/shell-api/store.ts +279 -0
- package/lib/shell-api/tool-design-adoption.ts +263 -0
- package/lib/shell-api/tool-design-rows.ts +307 -0
- package/lib/shell-api/tool-design-wiring.ts +481 -0
- package/lib/shell-api/tool-designs.ts +1411 -0
- package/lib/shell-api/transform-pipeline.ts +351 -0
- package/lib/shell-api/types.ts +803 -0
- package/lib/shell-api/visibility.ts +154 -0
- package/lib/user-prompt-card.js +48 -2
- package/package.json +3 -2
- package/tools/files-bridge.test.js +81 -66
- package/tools/fixtures/load-extension.js +40 -18
- package/tools/issues-command.test.js +4 -7
- package/tools/issues-section.test.js +71 -81
- package/tools/odd-rail-wiring.test.js +16 -10
- package/tools/package-surface.test.js +42 -13
- package/tools/prs-command.test.js +11 -6
- package/tools/shell-api/api.test.ts +484 -0
- package/tools/shell-api/apply-on-move.test.ts +529 -0
- package/tools/shell-api/arm.test.ts +220 -0
- package/tools/shell-api/bridge-ready.test.ts +309 -0
- package/tools/shell-api/bus.test.ts +194 -0
- package/tools/shell-api/button-rows.test.ts +435 -0
- package/tools/shell-api/capture.test.ts +583 -0
- package/tools/shell-api/card-style-rows.test.ts +493 -0
- package/tools/shell-api/card-style-transform.test.ts +379 -0
- package/tools/shell-api/card-style-wiring.test.ts +258 -0
- package/tools/shell-api/card-styles-e2e.test.ts +502 -0
- package/tools/shell-api/card-styles.test.ts +324 -0
- package/tools/shell-api/card-transcript-path.test.ts +178 -0
- package/tools/shell-api/compatibility-regression.test.mjs +64 -0
- package/tools/shell-api/config.test.ts +93 -0
- package/tools/shell-api/docs-coverage.test.ts +82 -0
- package/tools/shell-api/e2e.test.ts +562 -0
- package/tools/shell-api/example-bash-identity.test.ts +372 -0
- package/tools/shell-api/example-bash-matrix.test.ts +856 -0
- package/tools/shell-api/example-designs.test.ts +737 -0
- package/tools/shell-api/example-neon-transform.test.ts +283 -0
- package/tools/shell-api/example-plain-style.test.ts +1225 -0
- package/tools/shell-api/extension.test.ts +157 -0
- package/tools/shell-api/fake-events.ts +58 -0
- package/tools/shell-api/fake-host.ts +211 -0
- package/tools/shell-api/fake-sidebar.ts +516 -0
- package/tools/shell-api/fake-transcript.ts +486 -0
- package/tools/shell-api/frame-telemetry.test.ts +317 -0
- package/tools/shell-api/hook.test.ts +196 -0
- package/tools/shell-api/manifest.test.ts +47 -0
- package/tools/shell-api/message-design-adoption.test.ts +611 -0
- package/tools/shell-api/message-design-follows.test.ts +361 -0
- package/tools/shell-api/message-design-pi-release.test.mjs +157 -0
- package/tools/shell-api/message-design-regressions.test.mjs +162 -0
- package/tools/shell-api/message-design-rows.test.ts +266 -0
- package/tools/shell-api/message-design-wiring.test.ts +362 -0
- package/tools/shell-api/message-designs.test.ts +339 -0
- package/tools/shell-api/new-category.test.ts +1101 -0
- package/tools/shell-api/notices.test.ts +292 -0
- package/tools/shell-api/plugin-integration-lines.test.ts +827 -0
- package/tools/shell-api/presence.test.ts +396 -0
- package/tools/shell-api/rail-part-hits.test.ts +281 -0
- package/tools/shell-api/rail-surfaces.test.ts +402 -0
- package/tools/shell-api/rail-width.test.ts +406 -0
- package/tools/shell-api/regression.test.ts +434 -0
- package/tools/shell-api/repaint-api.test.ts +200 -0
- package/tools/shell-api/reserved-categories.test.ts +250 -0
- package/tools/shell-api/row-labels.test.ts +261 -0
- package/tools/shell-api/row-names.test.ts +407 -0
- package/tools/shell-api/row-origin.test.ts +395 -0
- package/tools/shell-api/row-placement-regression.test.ts +797 -0
- package/tools/shell-api/row-placement.test.ts +410 -0
- package/tools/shell-api/section-interaction.test.ts +364 -0
- package/tools/shell-api/section-panel.test.ts +430 -0
- package/tools/shell-api/section-placement-rail.test.ts +390 -0
- package/tools/shell-api/section-placement.test.ts +386 -0
- package/tools/shell-api/sections-preview.test.ts +312 -0
- package/tools/shell-api/sections-rows.test.ts +293 -0
- package/tools/shell-api/sidebar-backend.test.ts +477 -0
- package/tools/shell-api/sidebar-e2e.test.ts +317 -0
- package/tools/shell-api/sidebar-hover.test.ts +276 -0
- package/tools/shell-api/sidebar-independent.test.ts +291 -0
- package/tools/shell-api/sidebar-render.test.ts +281 -0
- package/tools/shell-api/sidebar-sections.test.ts +247 -0
- package/tools/shell-api/store.test.ts +192 -0
- package/tools/shell-api/surface-repaint-cost.test.ts +520 -0
- package/tools/shell-api/tool-design-card-style.test.ts +308 -0
- package/tools/shell-api/tool-design-chain.test.ts +471 -0
- package/tools/shell-api/tool-design-cost.test.ts +591 -0
- package/tools/shell-api/tool-design-rows.test.ts +627 -0
- package/tools/shell-api/tool-design-scopes-audit.test.ts +212 -0
- package/tools/shell-api/tool-designs.test.ts +2412 -0
- package/tools/shell-api/transform-pipeline.test.ts +376 -0
- package/tools/shell-api/transforms-bus.test.ts +233 -0
- package/tools/shell-api/verifier-width-regressions.test.ts +147 -0
- package/tools/shell-api-rpc.test.js +189 -0
- package/tools/user-prompt-card.test.js +80 -10
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ONE `pi.registerToolRenderer` integration (S4, S5, T8): chosen per tool, composed with the host.
|
|
3
|
+
*
|
|
4
|
+
* ## Why the bridge owns it
|
|
5
|
+
*
|
|
6
|
+
* Pi composes every extension's `ToolRendererResolver` (`dist/core/extensions/runner.js:550-553`), so
|
|
7
|
+
* a resolver that returns `next()` adds nothing and a resolver that REPLACES the renderers takes the
|
|
8
|
+
* host's card away from every other extension. The bridge therefore registers exactly one resolver.
|
|
9
|
+
*
|
|
10
|
+
* ## The rule: wrap every tool that HAS a design, decide on every render pass (S2)
|
|
11
|
+
*
|
|
12
|
+
* Pi resolves renderers ONCE per card, when it constructs it (`getRegisteredToolDefinition(toolName)`
|
|
13
|
+
* is passed into `new ToolExecutionComponent(...)`,
|
|
14
|
+
* `interactive-mode.js:1816-1818,2957-2961,3028-3032,3376-3380`), and re-renders that same component
|
|
15
|
+
* on every later frame. So the decision cannot live in the resolver: a card resolved while nothing
|
|
16
|
+
* was selected would have no wrapper to ask, and could never be repainted. That was the defect the
|
|
17
|
+
* user reported ("cambiar tanto las antiguas como posteriores"): a design selected at runtime has to
|
|
18
|
+
* reach a card that was already drawn.
|
|
19
|
+
*
|
|
20
|
+
* The resolver therefore returns the host's renderers themselves ONLY when the tool has no registered
|
|
21
|
+
* design at all (nothing to choose, so nothing to change). For every tool with at least one design,
|
|
22
|
+
* selected or not, it returns a wrapper that reads the live selection on EVERY render pass:
|
|
23
|
+
*
|
|
24
|
+
* - no design selected, or the selected design is gone: the host's component is rendered and its
|
|
25
|
+
* lines are returned byte for byte, untouched;
|
|
26
|
+
* - a design selected: the host's component is rendered, and those lines are handed to the design's
|
|
27
|
+
* `apply`.
|
|
28
|
+
*
|
|
29
|
+
* The wrapper replaces only `renderCall`/`renderResult` — every other member (`renderShell`, and
|
|
30
|
+
* anything a host adds later) is carried over unchanged — so the card is still the host's:
|
|
31
|
+
*
|
|
32
|
+
* ```
|
|
33
|
+
* render(width) → host component's render(width) → [live selection] → design.apply(rows, ctx)
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* The host renders itself, at the width the host asked for, and only those rows reach the design. That
|
|
37
|
+
* is what keeps the frame the host's: `neon` stays neon and `float` stays float, because the chrome
|
|
38
|
+
* was drawn before the design saw a byte of it (`ToolExecutionComponent` caches no lines — it asks its
|
|
39
|
+
* renderer every pass, `tool-execution.js:64-68,164-175`).
|
|
40
|
+
*
|
|
41
|
+
* ## The theme
|
|
42
|
+
*
|
|
43
|
+
* A design's `ctx.theme` is the theme the host handed that renderer: `renderCall(args, theme, ctx)`
|
|
44
|
+
* has it second, `renderResult(result, options, theme, ctx)` third
|
|
45
|
+
* (`dist/core/extensions/types.d.ts:538-545`). It is passed through untouched; the bridge never reads
|
|
46
|
+
* a colour from it.
|
|
47
|
+
*
|
|
48
|
+
* ## Every absence is a reported outcome
|
|
49
|
+
*
|
|
50
|
+
* No design registered for the tool, no `next()` and a host whose component has no `render` all return
|
|
51
|
+
* what the host gave us. A design that throws is the failure of THAT design and nothing else: the
|
|
52
|
+
* host's rows are drawn unchanged and one failure is recorded (`getToolDesignFailures`). A selection
|
|
53
|
+
* that cannot be read is the same degradation — the host's card is drawn. Nothing in this module can
|
|
54
|
+
* throw into Pi's render loop.
|
|
55
|
+
*
|
|
56
|
+
* ## Idempotency
|
|
57
|
+
*
|
|
58
|
+
* Registering twice would stack two wrappers around one component and apply the design twice, so the
|
|
59
|
+
* install records the exact `pi` objects it registered on in the process-global slot: a reload, a
|
|
60
|
+
* rebind or a second loaded copy of this package reports `already-registered` instead of adding a
|
|
61
|
+
* second resolver.
|
|
62
|
+
*
|
|
63
|
+
* This module is internal: `src/index.ts` does not re-export it.
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
import { isRecord } from "./rows.ts";
|
|
67
|
+
import {
|
|
68
|
+
applyToolDesign,
|
|
69
|
+
designsOfTool,
|
|
70
|
+
recordToolDesignFailure,
|
|
71
|
+
resolveToolDesignSelection,
|
|
72
|
+
type ToolDesignSelection,
|
|
73
|
+
} from "./tool-designs.ts";
|
|
74
|
+
import { resolveCardStyleSelection } from "./card-styles.ts";
|
|
75
|
+
import type { CardStyleSelection, ToolDesignContext, ToolDesignRender } from "./types.ts";
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The renderers Pi hands a resolver, described structurally: the bridge reads `renderCall` and
|
|
79
|
+
* `renderResult` and preserves every other member, so a host that grows a new one keeps it.
|
|
80
|
+
*/
|
|
81
|
+
export interface ToolRenderersLike {
|
|
82
|
+
renderShell?: unknown;
|
|
83
|
+
renderCall?: unknown;
|
|
84
|
+
renderResult?: unknown;
|
|
85
|
+
[member: string]: unknown;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** What `next()` answers: the renderers the remaining resolvers and the tool itself would use. */
|
|
89
|
+
export type ToolRendererNext = () => ToolRenderersLike | undefined;
|
|
90
|
+
|
|
91
|
+
/** Pi's `ToolRendererResolver`, structurally (`dist/core/extensions/types.d.ts:546`). */
|
|
92
|
+
export type ToolDesignResolver = (
|
|
93
|
+
toolName: string,
|
|
94
|
+
next: ToolRendererNext,
|
|
95
|
+
) => ToolRenderersLike | undefined;
|
|
96
|
+
|
|
97
|
+
/** The parts of a Pi component this module keeps working: `render` plus the optional members. */
|
|
98
|
+
interface Renderable {
|
|
99
|
+
render(width: number): readonly string[];
|
|
100
|
+
invalidate?(): void;
|
|
101
|
+
handleMouse?(event: Record<string, unknown>): Record<string, unknown> | undefined;
|
|
102
|
+
wantsKeyRelease?: boolean;
|
|
103
|
+
capturesViewportInput?: boolean;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function errorMessage(error: unknown): string {
|
|
107
|
+
return error instanceof Error ? error.message : String(error);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The resolver the bridge registers. Returns `next()`'s renderers themselves only when the tool has
|
|
112
|
+
* no design at all; otherwise a wrapper that reads the selection on every render pass, so a design
|
|
113
|
+
* selected after a card was drawn repaints that card (S2).
|
|
114
|
+
*/
|
|
115
|
+
export const toolDesignResolver: ToolDesignResolver = (toolName, next) => {
|
|
116
|
+
// The host's own answer first, and outside the guard: a host resolver that throws keeps its own
|
|
117
|
+
// failure path, and `next()` must run exactly once either way.
|
|
118
|
+
const renderers = next();
|
|
119
|
+
try {
|
|
120
|
+
// No design for this tool: there is nothing to choose, so nothing may change, not even identity.
|
|
121
|
+
if (designsOfTool(toolName).length === 0) return renderers;
|
|
122
|
+
if (renderers === undefined) return renderers;
|
|
123
|
+
return wrapRenderers(renderers, toolName);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
// A registry that refuses to answer is reported, and the host's card is what the user sees (S5).
|
|
126
|
+
recordToolDesignFailure(toolName, "(resolver)", errorMessage(error));
|
|
127
|
+
return renderers;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The renderer members this module wraps, with the argument index that carries the host's theme and
|
|
133
|
+
* the part of the card that renderer draws (S12). The two are wrapped SEPARATELY, so the design is
|
|
134
|
+
* told which one it is drawing instead of having to guess it from the rows.
|
|
135
|
+
*/
|
|
136
|
+
const WRAPPED_RENDERERS: ReadonlyArray<{
|
|
137
|
+
member: "renderCall" | "renderResult";
|
|
138
|
+
themeIndex: number;
|
|
139
|
+
/** The argument that carries Pi's render context, always the one after the theme. */
|
|
140
|
+
contextIndex: number;
|
|
141
|
+
part: "call" | "result";
|
|
142
|
+
}> = [
|
|
143
|
+
{ member: "renderCall", themeIndex: 1, contextIndex: 2, part: "call" },
|
|
144
|
+
{ member: "renderResult", themeIndex: 2, contextIndex: 3, part: "result" },
|
|
145
|
+
];
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* A by-value snapshot of the host's render context, or `undefined` when the host handed none. Taken
|
|
149
|
+
* on every render pass (the context is live: `durationMs` arrives with the result), and limited to
|
|
150
|
+
* the fields of `ToolDesignRender`, each kept only when it has the documented type.
|
|
151
|
+
*/
|
|
152
|
+
function snapshotRender(context: unknown): ToolDesignRender | undefined {
|
|
153
|
+
// Inside Pi's render loop: a host object whose accessors throw (a Proxy, a getter) has no context
|
|
154
|
+
// this module can read, so the design simply draws without `render`. Never a throw into the frame.
|
|
155
|
+
try {
|
|
156
|
+
if (!isRecord(context)) return undefined;
|
|
157
|
+
const render: {
|
|
158
|
+
-readonly [K in keyof ToolDesignRender]: ToolDesignRender[K];
|
|
159
|
+
} = {};
|
|
160
|
+
if (typeof context.toolCallId === "string") render.toolCallId = context.toolCallId;
|
|
161
|
+
// By REFERENCE: `args` is the host's own object (never frozen or cloned here); a design reads it.
|
|
162
|
+
if ("args" in context) render.args = context.args;
|
|
163
|
+
if (typeof context.isPartial === "boolean") render.isPartial = context.isPartial;
|
|
164
|
+
if (typeof context.isError === "boolean") render.isError = context.isError;
|
|
165
|
+
if (typeof context.durationMs === "number" && Number.isFinite(context.durationMs)) {
|
|
166
|
+
render.durationMs = context.durationMs;
|
|
167
|
+
}
|
|
168
|
+
if (typeof context.expanded === "boolean") render.expanded = context.expanded;
|
|
169
|
+
return render;
|
|
170
|
+
} catch {
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Own property on a renderers object THIS package wrapped: the resolver path
|
|
177
|
+
* (`./tool-design-wiring.ts`) and the adoption path (`./tool-design-adoption.ts`) share it, so a card
|
|
178
|
+
* is wrapped exactly once no matter which path reached it first.
|
|
179
|
+
*/
|
|
180
|
+
export const TOOL_RENDERERS_WRAPPED: unique symbol = Symbol.for(
|
|
181
|
+
"gentle-shell-bridge.tool-designs.renderers-wrapped.v1",
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* A copy of the host's renderers with the call/result renderer wrapped. Everything else — including
|
|
186
|
+
* `renderShell`, which decides whether the host frames the card itself — is the host's own value.
|
|
187
|
+
*
|
|
188
|
+
* The copy is stamped with `TOOL_RENDERERS_WRAPPED`, and an already-stamped object comes back
|
|
189
|
+
* untouched: the resolver path and the adoption path cooperate instead of stacking two wrappers (and
|
|
190
|
+
* two design applications) around one card.
|
|
191
|
+
*/
|
|
192
|
+
export function wrapRenderers(
|
|
193
|
+
renderers: ToolRenderersLike,
|
|
194
|
+
tool: string,
|
|
195
|
+
origin: ToolRendererWrapperOrigin = "resolver",
|
|
196
|
+
): ToolRenderersLike {
|
|
197
|
+
// Already wrapped by the other path: wrapping again would apply the design twice.
|
|
198
|
+
if (Object.hasOwn(renderers, TOOL_RENDERERS_WRAPPED)) return renderers;
|
|
199
|
+
const wrapCall = typeof renderers.renderCall === "function";
|
|
200
|
+
const wrapResult = typeof renderers.renderResult === "function";
|
|
201
|
+
// Nothing renderable to wrap: hand the host's own object back rather than a copy nobody needs.
|
|
202
|
+
if (!wrapCall && !wrapResult) return renderers;
|
|
203
|
+
|
|
204
|
+
const wrapped: ToolRenderersLike = { ...renderers };
|
|
205
|
+
for (const { member, themeIndex, contextIndex, part } of WRAPPED_RENDERERS) {
|
|
206
|
+
const original = renderers[member];
|
|
207
|
+
if (typeof original !== "function") continue;
|
|
208
|
+
// SAFETY: `typeof` just proved this member callable. A renderer's arguments (args, options, the
|
|
209
|
+
// host's context) and its answer (a Component) are objects the host owns, so they are forwarded
|
|
210
|
+
// as such rather than re-typed here.
|
|
211
|
+
const hostRenderer = original as (
|
|
212
|
+
...args: Record<string, unknown>[]
|
|
213
|
+
) => Record<string, unknown>;
|
|
214
|
+
wrapped[member] = wrapRenderer(hostRenderer, tool, themeIndex, contextIndex, origin, part);
|
|
215
|
+
}
|
|
216
|
+
Object.defineProperty(wrapped, TOOL_RENDERERS_WRAPPED, {
|
|
217
|
+
value: true,
|
|
218
|
+
enumerable: false,
|
|
219
|
+
configurable: true,
|
|
220
|
+
});
|
|
221
|
+
return wrapped;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Which path produced a wrapper: the resolver (`./tool-design-wiring.ts`'s own registration) or the
|
|
226
|
+
* post-construction adoption pass (`./tool-design-adoption.ts`). Only the resolver path is counted
|
|
227
|
+
* as "the resolver wrapped this card", so the diagnostics record can say which path reached what (T16).
|
|
228
|
+
*/
|
|
229
|
+
export type ToolRendererWrapperOrigin = "resolver" | "adoption";
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Wrap one host renderer: call it with the host's own arguments and `this` (a host failure stays the
|
|
233
|
+
* host's), then wrap the component it returned. A non-component answer is returned untouched.
|
|
234
|
+
*/
|
|
235
|
+
function wrapRenderer(
|
|
236
|
+
original: (...args: Record<string, unknown>[]) => Record<string, unknown>,
|
|
237
|
+
tool: string,
|
|
238
|
+
themeIndex: number,
|
|
239
|
+
contextIndex: number,
|
|
240
|
+
origin: ToolRendererWrapperOrigin,
|
|
241
|
+
part: "call" | "result",
|
|
242
|
+
): (...args: Record<string, unknown>[]) => Record<string, unknown> {
|
|
243
|
+
return function (this: unknown, ...args: Record<string, unknown>[]): Record<string, unknown> {
|
|
244
|
+
const component = original.apply(this, args);
|
|
245
|
+
// The theme is the host's own opaque object at the documented index of this renderer's arguments
|
|
246
|
+
// (`renderCall`: second, `renderResult`: third); the bridge only forwards it.
|
|
247
|
+
return wrapComponent(component, tool, args[themeIndex], args[contextIndex], origin, part);
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* The Component Pi mounts for one card: it renders the host's component, then reads the LIVE selection
|
|
253
|
+
* on every pass. No design selected (or a design that disappeared) returns the host's own lines
|
|
254
|
+
* untouched; a selected design decides what is drawn. `invalidate()` and the optional mouse/key-release
|
|
255
|
+
* members are delegated, so the card keeps behaving exactly as the host's did — only the rows can
|
|
256
|
+
* differ.
|
|
257
|
+
*/
|
|
258
|
+
function wrapComponent(
|
|
259
|
+
component: Record<string, unknown>,
|
|
260
|
+
tool: string,
|
|
261
|
+
theme: Record<string, unknown> | undefined,
|
|
262
|
+
hostContext: unknown,
|
|
263
|
+
origin: ToolRendererWrapperOrigin,
|
|
264
|
+
part: "call" | "result",
|
|
265
|
+
): Record<string, unknown> {
|
|
266
|
+
if (!isRecord(component) || typeof component.render !== "function") return component;
|
|
267
|
+
// SAFETY: a record whose own `render` is callable is exactly the Pi component contract
|
|
268
|
+
// (`render(width): string[]`, plus optional members). The bridge replaces nothing on the host's
|
|
269
|
+
// object; it only calls `render`/`invalidate`/`handleMouse` and reads two booleans.
|
|
270
|
+
const child = component as unknown as Renderable;
|
|
271
|
+
const hostRender = child.render;
|
|
272
|
+
// A card is now mounted through the resolver's wrapper: the diagnostics record can answer "is this card
|
|
273
|
+
// even wrapped?" per tool instead of leaving the user to attach a debugger. The adoption path
|
|
274
|
+
// records its own count (`./tool-design-adoption.ts`), so the two numbers stay separate.
|
|
275
|
+
if (origin === "resolver") recordWrappedCard(tool);
|
|
276
|
+
|
|
277
|
+
const wrapper = {
|
|
278
|
+
render(width: number): string[] {
|
|
279
|
+
const lines = hostRender.call(child, width);
|
|
280
|
+
// A host that answers something other than rows keeps its own answer; the design has nothing
|
|
281
|
+
// to rewrite and the bridge invents nothing.
|
|
282
|
+
if (!Array.isArray(lines)) return lines as string[];
|
|
283
|
+
// SAFETY: `Array.isArray` just proved this is the host's own `string[]`; `applyToolDesign` only
|
|
284
|
+
// reads it (through a copy) and answers with strings, or `undefined` for "draw the host's".
|
|
285
|
+
const rows = lines as string[];
|
|
286
|
+
// The host's context is snapshotted BEFORE resolving: the scope decides on THIS call, and the
|
|
287
|
+
// arguments are exactly what the design gets as `ctx.render.args` afterwards (S10/S12).
|
|
288
|
+
const render = snapshotRender(hostContext);
|
|
289
|
+
let selection: ToolDesignSelection;
|
|
290
|
+
try {
|
|
291
|
+
// Read on EVERY pass, never captured at resolution: that is what lets a design selected after
|
|
292
|
+
// this card was drawn repaint it (S2). The call's arguments make a scoped claim a candidate
|
|
293
|
+
// for THIS call; with no render context they are `undefined` and only unscoped claims apply.
|
|
294
|
+
selection = resolveToolDesignSelection(tool, render?.args);
|
|
295
|
+
} catch (error) {
|
|
296
|
+
recordToolDesignFailure(tool, "(selection)", errorMessage(error));
|
|
297
|
+
return rows;
|
|
298
|
+
}
|
|
299
|
+
// No design selected, or the selected design is gone: the host's bytes, untouched.
|
|
300
|
+
if (selection.kind !== "design") return rows;
|
|
301
|
+
// S22: the card's style is resolved ON ACCESS and at most ONCE per pass. A design that never
|
|
302
|
+
// reads `cardStyle` therefore pays nothing — no store read, no registry walk, no row changed —
|
|
303
|
+
// and a design that reads it twice gets one answer, so a switch landing mid-pass cannot draw a
|
|
304
|
+
// card that is half of each. It is read from the live registry, never snapshotted at
|
|
305
|
+
// resolution, so a style chosen after this card was built still reaches it.
|
|
306
|
+
let cardStyle: CardStyleSelection | undefined;
|
|
307
|
+
const ctx: ToolDesignContext = {
|
|
308
|
+
width,
|
|
309
|
+
tool,
|
|
310
|
+
theme,
|
|
311
|
+
part,
|
|
312
|
+
...(render === undefined ? {} : { render }),
|
|
313
|
+
get cardStyle(): CardStyleSelection {
|
|
314
|
+
cardStyle ??= resolveCardStyleSelection();
|
|
315
|
+
return cardStyle;
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
const applied = applyToolDesign(selection.design, rows, ctx);
|
|
319
|
+
return applied === undefined ? rows : applied;
|
|
320
|
+
},
|
|
321
|
+
invalidate(): void {
|
|
322
|
+
if (typeof child.invalidate === "function") child.invalidate.call(child);
|
|
323
|
+
},
|
|
324
|
+
handleMouse(event: Record<string, unknown>): Record<string, unknown> | undefined {
|
|
325
|
+
if (typeof child.handleMouse === "function") return child.handleMouse.call(child, event);
|
|
326
|
+
return undefined;
|
|
327
|
+
},
|
|
328
|
+
get wantsKeyRelease(): boolean {
|
|
329
|
+
return child.wantsKeyRelease === true;
|
|
330
|
+
},
|
|
331
|
+
get capturesViewportInput(): boolean {
|
|
332
|
+
return child.capturesViewportInput === true;
|
|
333
|
+
},
|
|
334
|
+
};
|
|
335
|
+
return wrapper;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// ---------------------------------------------------------------------------------------------
|
|
339
|
+
// The install: once per `pi`, process-globally, reported either way
|
|
340
|
+
// ---------------------------------------------------------------------------------------------
|
|
341
|
+
|
|
342
|
+
/** Why the resolver is (or is not) installed. Every value is reported, none is a throw. */
|
|
343
|
+
export type ToolDesignResolverOutcome =
|
|
344
|
+
| "registered"
|
|
345
|
+
| "already-registered"
|
|
346
|
+
| "no-register-function"
|
|
347
|
+
| "threw";
|
|
348
|
+
|
|
349
|
+
export interface InstallToolDesignResolverResult {
|
|
350
|
+
installed: boolean;
|
|
351
|
+
outcome: ToolDesignResolverOutcome;
|
|
352
|
+
/** Only for `threw`: the message the host's `registerToolRenderer` threw. */
|
|
353
|
+
reason?: string;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
interface ResolverState {
|
|
357
|
+
/** The exact `pi` objects this process registered on; identity is the idempotency key. */
|
|
358
|
+
targets: unknown[];
|
|
359
|
+
lastOutcome?: ToolDesignResolverOutcome;
|
|
360
|
+
lastReason?: string;
|
|
361
|
+
/** Per tool, how many card components were built through the wrapper: "is it even wrapped?". */
|
|
362
|
+
wrappedCards: Array<{ tool: string; cards: number }>;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** Stable process-global slot: a second loaded copy of this package must register nothing. */
|
|
366
|
+
const RESOLVER_KEY = Symbol.for("gentle-shell-bridge.tool-designs.resolver.v1");
|
|
367
|
+
|
|
368
|
+
function resolverHost(): Record<symbol, ResolverState | undefined> {
|
|
369
|
+
// SAFETY: only `resolverState()` writes this key, and it re-checks the list before trusting a
|
|
370
|
+
// value left by another copy.
|
|
371
|
+
return globalThis as unknown as Record<symbol, ResolverState | undefined>;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function resolverState(): ResolverState {
|
|
375
|
+
const host = resolverHost();
|
|
376
|
+
const existing = host[RESOLVER_KEY];
|
|
377
|
+
if (existing !== undefined && Array.isArray(existing.targets)) {
|
|
378
|
+
// A slot created by a copy that predates the per-tool wrapper counter gains the list in place, so
|
|
379
|
+
// two copies always see one record.
|
|
380
|
+
if (!Array.isArray(existing.wrappedCards)) existing.wrappedCards = [];
|
|
381
|
+
return existing;
|
|
382
|
+
}
|
|
383
|
+
const created: ResolverState = { targets: [], wrappedCards: [] };
|
|
384
|
+
host[RESOLVER_KEY] = created;
|
|
385
|
+
return created;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** Record that one card of `tool` was built through the wrapper (monotonic, per process). */
|
|
389
|
+
function recordWrappedCard(tool: string): void {
|
|
390
|
+
const state = resolverState();
|
|
391
|
+
const entry = state.wrappedCards.find((candidate) => candidate.tool === tool);
|
|
392
|
+
if (entry === undefined) state.wrappedCards.push({ tool, cards: 1 });
|
|
393
|
+
else entry.cards += 1;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function recordOutcome(
|
|
397
|
+
state: ResolverState,
|
|
398
|
+
result: InstallToolDesignResolverResult,
|
|
399
|
+
): InstallToolDesignResolverResult {
|
|
400
|
+
state.lastOutcome = result.outcome;
|
|
401
|
+
if (result.reason === undefined) delete state.lastReason;
|
|
402
|
+
else state.lastReason = result.reason;
|
|
403
|
+
return result;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Install the resolver on one `pi`, once. Called from the same `session_start` that arms the row
|
|
408
|
+
* surfaces (`./extension.ts`), never at module load.
|
|
409
|
+
*
|
|
410
|
+
* Idempotent per `pi` object through the process-global slot, so a rebind, a `/reload` or a second
|
|
411
|
+
* loaded copy of this package reports `already-registered` and adds no second resolver. A host without
|
|
412
|
+
* `registerToolRenderer` is a reported outcome instead of a throw inside the session handler.
|
|
413
|
+
*/
|
|
414
|
+
export function installToolDesignResolver(pi: unknown): InstallToolDesignResolverResult {
|
|
415
|
+
const state = resolverState();
|
|
416
|
+
let register: unknown;
|
|
417
|
+
try {
|
|
418
|
+
register = isRecord(pi) ? pi.registerToolRenderer : undefined;
|
|
419
|
+
} catch (error) {
|
|
420
|
+
return recordOutcome(state, {
|
|
421
|
+
installed: false,
|
|
422
|
+
outcome: "threw",
|
|
423
|
+
reason: errorMessage(error),
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
if (typeof register !== "function") {
|
|
427
|
+
return recordOutcome(state, { installed: false, outcome: "no-register-function" });
|
|
428
|
+
}
|
|
429
|
+
if (state.targets.includes(pi)) {
|
|
430
|
+
return recordOutcome(state, { installed: true, outcome: "already-registered" });
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
try {
|
|
434
|
+
(register as (resolver: ToolDesignResolver) => void).call(pi, toolDesignResolver);
|
|
435
|
+
} catch (error) {
|
|
436
|
+
return recordOutcome(state, {
|
|
437
|
+
installed: false,
|
|
438
|
+
outcome: "threw",
|
|
439
|
+
reason: errorMessage(error),
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
state.targets.push(pi);
|
|
443
|
+
return recordOutcome(state, { installed: true, outcome: "registered" });
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/** What the diagnostics record reports about the one integration point. */
|
|
447
|
+
export interface ToolDesignWiringDiagnostics {
|
|
448
|
+
/** How many `pi` objects this process registered the resolver on (`0` or `1` in a real TUI). */
|
|
449
|
+
registered: number;
|
|
450
|
+
/** The most recent install attempt's outcome; absent when it was never attempted. */
|
|
451
|
+
lastOutcome?: ToolDesignResolverOutcome;
|
|
452
|
+
/** Only for `threw`: the reason, verbatim. */
|
|
453
|
+
lastReason?: string;
|
|
454
|
+
/**
|
|
455
|
+
* Per tool, how many card components were built through the wrapper. A tool with designs and no
|
|
456
|
+
* entry has never had a card resolved through the wrapper, which answers "is this card even
|
|
457
|
+
* wrapped?" without a debugger (a card built before its design was registered stays unwrapped until
|
|
458
|
+
* the host repaints it).
|
|
459
|
+
*/
|
|
460
|
+
wrappedCards: Array<{ tool: string; cards: number }>;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/** Diagnostics snapshot for the diagnostics record. */
|
|
464
|
+
export function getToolDesignWiringDiagnostics(): ToolDesignWiringDiagnostics {
|
|
465
|
+
const state = resolverState();
|
|
466
|
+
return {
|
|
467
|
+
registered: state.targets.length,
|
|
468
|
+
...(state.lastOutcome === undefined ? {} : { lastOutcome: state.lastOutcome }),
|
|
469
|
+
...(state.lastReason === undefined ? {} : { lastReason: state.lastReason }),
|
|
470
|
+
wrappedCards: state.wrappedCards.map((entry) => ({ ...entry })),
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/** Test-only seam; deliberately not re-exported from `src/index.ts`. */
|
|
475
|
+
export function resetToolDesignWiringForTests(): void {
|
|
476
|
+
const state = resolverState();
|
|
477
|
+
state.targets.length = 0;
|
|
478
|
+
delete state.lastOutcome;
|
|
479
|
+
delete state.lastReason;
|
|
480
|
+
state.wrappedCards.length = 0;
|
|
481
|
+
}
|