@mcpview/compose 0.13.1 → 0.14.1
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 +118 -0
- package/README.md +53 -0
- package/esm/src/code-mode/chain.d.ts +24 -1
- package/esm/src/code-mode/chain.d.ts.map +1 -1
- package/esm/src/code-mode/chain.js +219 -86
- package/esm/src/code-mode/errors.d.ts +80 -0
- package/esm/src/code-mode/errors.d.ts.map +1 -0
- package/esm/src/code-mode/errors.js +140 -0
- package/esm/src/code-mode/guest-worker.d.ts +11 -0
- package/esm/src/code-mode/guest-worker.d.ts.map +1 -0
- package/esm/src/code-mode/guest-worker.js +80 -0
- package/esm/src/code-mode/mod.d.ts +3 -1
- package/esm/src/code-mode/mod.d.ts.map +1 -1
- package/esm/src/code-mode/mod.js +2 -0
- package/esm/src/code-mode/sandbox-core.d.ts +61 -0
- package/esm/src/code-mode/sandbox-core.d.ts.map +1 -0
- package/esm/src/code-mode/sandbox-core.js +424 -0
- package/esm/src/code-mode/sandbox.d.ts +68 -7
- package/esm/src/code-mode/sandbox.d.ts.map +1 -1
- package/esm/src/code-mode/sandbox.js +182 -120
- package/esm/src/code-mode/tool.d.ts.map +1 -1
- package/esm/src/code-mode/tool.js +2 -0
- package/esm/src/code-mode/worker-host.d.ts +82 -0
- package/esm/src/code-mode/worker-host.d.ts.map +1 -0
- package/esm/src/code-mode/worker-host.js +364 -0
- package/esm/src/code-mode/worker-protocol.d.ts +44 -0
- package/esm/src/code-mode/worker-protocol.d.ts.map +1 -0
- package/esm/src/code-mode/worker-protocol.js +7 -0
- package/esm/src/host/renderer/css/panel-controls.d.ts.map +1 -1
- package/esm/src/host/renderer/css/panel-controls.js +5 -1
- package/esm/src/host/renderer/js/card-height.d.ts +11 -1
- package/esm/src/host/renderer/js/card-height.d.ts.map +1 -1
- package/esm/src/host/renderer/js/card-height.js +57 -10
- package/esm/src/host/renderer/js/event-bus.d.ts +11 -0
- package/esm/src/host/renderer/js/event-bus.d.ts.map +1 -1
- package/esm/src/host/renderer/js/event-bus.js +49 -8
- package/esm/src/host/renderer/js/intrinsic-layout.d.ts +26 -0
- package/esm/src/host/renderer/js/intrinsic-layout.d.ts.map +1 -0
- package/esm/src/host/renderer/js/intrinsic-layout.js +44 -0
- package/esm/src/host/renderer/panel-resizing.d.ts.map +1 -1
- package/esm/src/host/renderer/panel-resizing.js +8 -0
- package/esm/src/version.d.ts +1 -1
- package/esm/src/version.js +1 -1
- package/package.json +1 -1
- package/script/src/code-mode/chain.d.ts +24 -1
- package/script/src/code-mode/chain.d.ts.map +1 -1
- package/script/src/code-mode/chain.js +218 -85
- package/script/src/code-mode/errors.d.ts +80 -0
- package/script/src/code-mode/errors.d.ts.map +1 -0
- package/script/src/code-mode/errors.js +148 -0
- package/script/src/code-mode/guest-worker.d.ts +11 -0
- package/script/src/code-mode/guest-worker.d.ts.map +1 -0
- package/script/src/code-mode/guest-worker.js +105 -0
- package/script/src/code-mode/mod.d.ts +3 -1
- package/script/src/code-mode/mod.d.ts.map +1 -1
- package/script/src/code-mode/mod.js +9 -1
- package/script/src/code-mode/sandbox-core.d.ts +61 -0
- package/script/src/code-mode/sandbox-core.d.ts.map +1 -0
- package/script/src/code-mode/sandbox-core.js +430 -0
- package/script/src/code-mode/sandbox.d.ts +68 -7
- package/script/src/code-mode/sandbox.d.ts.map +1 -1
- package/script/src/code-mode/sandbox.js +190 -122
- package/script/src/code-mode/tool.d.ts.map +1 -1
- package/script/src/code-mode/tool.js +2 -0
- package/script/src/code-mode/worker-host.d.ts +82 -0
- package/script/src/code-mode/worker-host.d.ts.map +1 -0
- package/script/src/code-mode/worker-host.js +399 -0
- package/script/src/code-mode/worker-protocol.d.ts +44 -0
- package/script/src/code-mode/worker-protocol.d.ts.map +1 -0
- package/script/src/code-mode/worker-protocol.js +8 -0
- package/script/src/host/renderer/css/panel-controls.d.ts.map +1 -1
- package/script/src/host/renderer/css/panel-controls.js +5 -1
- package/script/src/host/renderer/js/card-height.d.ts +11 -1
- package/script/src/host/renderer/js/card-height.d.ts.map +1 -1
- package/script/src/host/renderer/js/card-height.js +57 -10
- package/script/src/host/renderer/js/event-bus.d.ts +11 -0
- package/script/src/host/renderer/js/event-bus.d.ts.map +1 -1
- package/script/src/host/renderer/js/event-bus.js +50 -8
- package/script/src/host/renderer/js/intrinsic-layout.d.ts +26 -0
- package/script/src/host/renderer/js/intrinsic-layout.d.ts.map +1 -0
- package/script/src/host/renderer/js/intrinsic-layout.js +48 -0
- package/script/src/host/renderer/panel-resizing.d.ts.map +1 -1
- package/script/src/host/renderer/panel-resizing.js +8 -0
- package/script/src/version.d.ts +1 -1
- package/script/src/version.js +1 -1
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host side of worker isolation: one short-lived worker per program, so a
|
|
3
|
+
* CPU-bound guest cannot block the server's event loop.
|
|
4
|
+
*
|
|
5
|
+
* Stopping relies on a SharedArrayBuffer flag read by the QuickJS interrupt
|
|
6
|
+
* handler. `terminate()` is only a backstop: under Deno it does not stop a
|
|
7
|
+
* thread that is still spinning.
|
|
8
|
+
*
|
|
9
|
+
* @module code-mode/worker-host
|
|
10
|
+
*/
|
|
11
|
+
import * as dntShim from "../../_dnt.shims.js";
|
|
12
|
+
import { CodeModeError, codeModeErrorFromFields } from "./errors.js";
|
|
13
|
+
import { receiptTooLarge } from "./sandbox-core.js";
|
|
14
|
+
/** Time a stopped guest has to report before the host gives up on it. */
|
|
15
|
+
const STOP_GRACE_MS = 250;
|
|
16
|
+
function workerRuntime(scope = dntShim.dntGlobalThis) {
|
|
17
|
+
const { Deno: deno, process, Worker } = scope;
|
|
18
|
+
if (typeof deno?.version?.deno === "string") {
|
|
19
|
+
return typeof Worker === "function" ? "deno" : undefined;
|
|
20
|
+
}
|
|
21
|
+
if (typeof process?.versions?.node === "string")
|
|
22
|
+
return "node";
|
|
23
|
+
return typeof Worker === "function" ? "browser" : undefined;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Why worker isolation cannot run here, or undefined when it can. `scope` is the
|
|
27
|
+
* global object to inspect.
|
|
28
|
+
*/
|
|
29
|
+
export function workerIsolationUnavailable(scope = dntShim.dntGlobalThis) {
|
|
30
|
+
if (typeof SharedArrayBuffer !== "function")
|
|
31
|
+
return "SharedArrayBuffer is not available";
|
|
32
|
+
const runtime = workerRuntime(scope);
|
|
33
|
+
if (!runtime)
|
|
34
|
+
return "Workers are not available in this runtime";
|
|
35
|
+
// The guest entry connects through Deno's Web Worker port or node:worker_threads.
|
|
36
|
+
if (runtime === "browser")
|
|
37
|
+
return "Browser workers are not supported by the Code Mode guest";
|
|
38
|
+
if (runtime === "node") {
|
|
39
|
+
return nodeWorkerPermission(scope.process);
|
|
40
|
+
}
|
|
41
|
+
return denoWorkerPermission();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Why Node's enforced permission model forbids workers, or undefined. Audit mode
|
|
45
|
+
* (`--permission-audit`) reports a missing grant but still creates the worker, so it
|
|
46
|
+
* does not count.
|
|
47
|
+
*/
|
|
48
|
+
export function nodeWorkerPermission(process) {
|
|
49
|
+
const has = process?.permission?.has;
|
|
50
|
+
if (typeof has !== "function")
|
|
51
|
+
return undefined;
|
|
52
|
+
const audit = [...(process?.execArgv ?? []), process?.env?.NODE_OPTIONS ?? ""].some((option) => option.includes("--permission-audit"));
|
|
53
|
+
if (audit)
|
|
54
|
+
return undefined;
|
|
55
|
+
try {
|
|
56
|
+
return has.call(process?.permission, "worker") === false
|
|
57
|
+
? "Node's permission model does not allow worker threads (--allow-worker)"
|
|
58
|
+
: undefined;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The permission Deno needs to load the guest module, or undefined for schemes it does
|
|
66
|
+
* not gate. An import query must name the port: a bare `jsr.io` asks about every port
|
|
67
|
+
* and reads "prompt", although Deno grants `jsr.io:443` by default.
|
|
68
|
+
*/
|
|
69
|
+
export function guestModulePermission(url) {
|
|
70
|
+
if (url.protocol === "file:")
|
|
71
|
+
return { name: "read", path: url };
|
|
72
|
+
if (url.protocol !== "https:" && url.protocol !== "http:")
|
|
73
|
+
return undefined;
|
|
74
|
+
const port = url.port || (url.protocol === "https:" ? "443" : "80");
|
|
75
|
+
return { name: "import", host: `${url.hostname}:${port}` };
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* A Deno worker inherits the host's permissions and must be able to load its module.
|
|
79
|
+
* Only the module itself is checked: its siblings and the QuickJS WASM file are not.
|
|
80
|
+
*/
|
|
81
|
+
function denoWorkerPermission() {
|
|
82
|
+
const deno = dntShim.dntGlobalThis.Deno;
|
|
83
|
+
const query = deno?.permissions?.querySync;
|
|
84
|
+
if (!query)
|
|
85
|
+
return undefined;
|
|
86
|
+
const url = guestUrl();
|
|
87
|
+
const descriptor = guestModulePermission(url);
|
|
88
|
+
if (!descriptor)
|
|
89
|
+
return undefined;
|
|
90
|
+
try {
|
|
91
|
+
const { state } = query.call(deno.permissions, descriptor);
|
|
92
|
+
return state === "granted"
|
|
93
|
+
? undefined
|
|
94
|
+
: `Deno has no ${descriptor.name} permission for the worker module ${url.href}`;
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
// Older runtimes may not know the descriptor; the worker reports its own failure.
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Node options a file worker may repeat. Node rejects process-wide options such
|
|
103
|
+
* as `--max-old-space-size` in a worker's `execArgv` (`ERR_WORKER_INVALID_EXEC_ARGV`),
|
|
104
|
+
* and entry-point options such as `--input-type` or `-e` break it. Inheriting
|
|
105
|
+
* the parent's list is no better: it carries the same flags. So only these
|
|
106
|
+
* resolution and diagnostics options are forwarded.
|
|
107
|
+
*/
|
|
108
|
+
const WORKER_FLAGS_WITH_VALUE = new Set(["--conditions", "-C"]);
|
|
109
|
+
const WORKER_SWITCHES = new Set(["--enable-source-maps"]);
|
|
110
|
+
/** The part of the host's Node options that a Code Mode worker can start with. */
|
|
111
|
+
export function nodeWorkerExecArgv(execArgv) {
|
|
112
|
+
const kept = [];
|
|
113
|
+
for (let index = 0; index < execArgv.length; index++) {
|
|
114
|
+
const arg = execArgv[index];
|
|
115
|
+
const name = arg.split("=", 1)[0];
|
|
116
|
+
if (WORKER_SWITCHES.has(arg))
|
|
117
|
+
kept.push(arg);
|
|
118
|
+
else if (WORKER_FLAGS_WITH_VALUE.has(name)) {
|
|
119
|
+
kept.push(arg);
|
|
120
|
+
if (!arg.includes("=") && index + 1 < execArgv.length)
|
|
121
|
+
kept.push(execArgv[++index]);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return kept;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* The host's `NODE_OPTIONS` reduced to what a Code Mode worker can start with. A
|
|
128
|
+
* worker inherits the environment by default, so `--input-type` or
|
|
129
|
+
* `--max-old-space-size` there would break it just like in `execArgv`.
|
|
130
|
+
*/
|
|
131
|
+
export function nodeWorkerNodeOptions(options) {
|
|
132
|
+
return nodeWorkerExecArgv(parseNodeOptions(options ?? "")).map(quoteNodeOption).join(" ");
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Split `NODE_OPTIONS` the way Node does: whitespace separates options, double
|
|
136
|
+
* quotes group (and are dropped), and inside quotes a backslash escapes the next
|
|
137
|
+
* character.
|
|
138
|
+
*/
|
|
139
|
+
export function parseNodeOptions(options) {
|
|
140
|
+
const tokens = [];
|
|
141
|
+
let current = "";
|
|
142
|
+
let started = false;
|
|
143
|
+
let quoted = false;
|
|
144
|
+
for (let index = 0; index < options.length; index++) {
|
|
145
|
+
const char = options[index];
|
|
146
|
+
if (quoted && char === "\\" && index + 1 < options.length) {
|
|
147
|
+
current += options[++index];
|
|
148
|
+
}
|
|
149
|
+
else if (char === '"') {
|
|
150
|
+
quoted = !quoted;
|
|
151
|
+
started = true;
|
|
152
|
+
}
|
|
153
|
+
else if (!quoted && /\s/.test(char)) {
|
|
154
|
+
if (started)
|
|
155
|
+
tokens.push(current);
|
|
156
|
+
current = "";
|
|
157
|
+
started = false;
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
current += char;
|
|
161
|
+
started = true;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (started)
|
|
165
|
+
tokens.push(current);
|
|
166
|
+
return tokens.filter(Boolean);
|
|
167
|
+
}
|
|
168
|
+
/** One option in `NODE_OPTIONS` syntax: quoted when it holds whitespace, a quote or a backslash. */
|
|
169
|
+
function quoteNodeOption(option) {
|
|
170
|
+
return /[\s"\\]/.test(option) ? `"${option.replace(/["\\]/g, "\\$&")}"` : option;
|
|
171
|
+
}
|
|
172
|
+
/** The host's environment with `NODE_OPTIONS` reduced for a worker. */
|
|
173
|
+
export function nodeWorkerEnv(env) {
|
|
174
|
+
const { NODE_OPTIONS: options, ...rest } = env;
|
|
175
|
+
const kept = nodeWorkerNodeOptions(options);
|
|
176
|
+
return kept ? { ...rest, NODE_OPTIONS: kept } : rest;
|
|
177
|
+
}
|
|
178
|
+
function guestUrl() {
|
|
179
|
+
// dnt keeps this URL as written, so derive the extension from this module:
|
|
180
|
+
// `.ts` from source or JSR, `.js` from the npm build.
|
|
181
|
+
const extension = import.meta.url.endsWith(".ts") ? ".ts" : ".js";
|
|
182
|
+
return new URL(`./guest-worker${extension}`, import.meta.url);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Load a Node builtin. The specifier is built at run time so that a bundler
|
|
186
|
+
* targeting the browser or another runtime does not resolve it: `inline`
|
|
187
|
+
* isolation must stay usable there.
|
|
188
|
+
*/
|
|
189
|
+
function importNodeBuiltin(name) {
|
|
190
|
+
return import(`node:${name}`);
|
|
191
|
+
}
|
|
192
|
+
async function spawn(onMessage, onFailure) {
|
|
193
|
+
if (workerRuntime() === "node") {
|
|
194
|
+
const { Worker } = await importNodeBuiltin("worker_threads");
|
|
195
|
+
const process = dntShim.dntGlobalThis.process;
|
|
196
|
+
const worker = new Worker(guestUrl(), {
|
|
197
|
+
execArgv: nodeWorkerExecArgv(process?.execArgv ?? []),
|
|
198
|
+
env: nodeWorkerEnv(process?.env ?? {}),
|
|
199
|
+
});
|
|
200
|
+
worker.on("message", onMessage);
|
|
201
|
+
worker.on("error", onFailure);
|
|
202
|
+
worker.on("exit", onFailure);
|
|
203
|
+
return {
|
|
204
|
+
post: (message) => worker.postMessage(message),
|
|
205
|
+
terminate: () => {
|
|
206
|
+
worker.off("exit", onFailure);
|
|
207
|
+
void worker.terminate();
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
const worker = new Worker(guestUrl(), { type: "module" });
|
|
212
|
+
worker.onmessage = (event) => onMessage(event.data);
|
|
213
|
+
worker.onerror = (event) => {
|
|
214
|
+
event.preventDefault();
|
|
215
|
+
onFailure();
|
|
216
|
+
};
|
|
217
|
+
worker.onmessageerror = () => onFailure();
|
|
218
|
+
return { post: (message) => worker.postMessage(message), terminate: () => worker.terminate() };
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* The reply for one host operation. The JSON text is bounded here, before it is
|
|
222
|
+
* posted, so an oversized result never crosses the worker boundary (no parse and
|
|
223
|
+
* no structured clone). The guest decodes the text.
|
|
224
|
+
*/
|
|
225
|
+
export function encodeReply(id, operation, value, valueLength) {
|
|
226
|
+
let json;
|
|
227
|
+
try {
|
|
228
|
+
// Like the inline path, drop what JSON drops (functions, undefined).
|
|
229
|
+
json = JSON.stringify(value ?? null);
|
|
230
|
+
}
|
|
231
|
+
catch {
|
|
232
|
+
json = undefined;
|
|
233
|
+
}
|
|
234
|
+
if (json === undefined) {
|
|
235
|
+
return {
|
|
236
|
+
type: "reply",
|
|
237
|
+
id,
|
|
238
|
+
ok: false,
|
|
239
|
+
error: new CodeModeError("OPERATION_FAILED", `api.${operation} returned a value that is not JSON`, "Return plain JSON from the host operation").toJSON(),
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
if (json.length > valueLength) {
|
|
243
|
+
return { type: "reply", id, ok: false, error: receiptTooLarge(valueLength).toJSON() };
|
|
244
|
+
}
|
|
245
|
+
return { type: "reply", id, ok: true, json };
|
|
246
|
+
}
|
|
247
|
+
/** The error for a worker that could not be created at all. */
|
|
248
|
+
export function workerSpawnFailure(error) {
|
|
249
|
+
if (error?.code === "ERR_ACCESS_DENIED") {
|
|
250
|
+
// Node refused the Worker constructor under its permission model.
|
|
251
|
+
return new CodeModeError("SANDBOX_UNAVAILABLE", "Worker isolation is unavailable: Node's permission model does not allow worker threads", 'Start Node with --allow-worker, or configure isolation: "inline"', { context: { isolation: "worker" } });
|
|
252
|
+
}
|
|
253
|
+
return sandboxFailed();
|
|
254
|
+
}
|
|
255
|
+
function sandboxFailed() {
|
|
256
|
+
return new CodeModeError("SANDBOX_FAILED", "The Code Mode worker stopped unexpectedly", 'Run the program again. If the worker module cannot be loaded (a bundle without it, or a missing read or import permission), grant access to it or configure isolation: "inline"');
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Run one program in a fresh worker and settle once with its value or a
|
|
260
|
+
* {@link CodeModeError}. The worker is always terminated afterwards.
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```ts
|
|
264
|
+
* const value = await runInWorker({
|
|
265
|
+
* code: "return 1 + 1;", params: {}, limits: CODE_MODE_LIMITS,
|
|
266
|
+
* call: () => Promise.reject(new Error("unused")), signal: new AbortController().signal,
|
|
267
|
+
* });
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
export function runInWorker(input) {
|
|
271
|
+
const cancel = new Int32Array(new SharedArrayBuffer(4));
|
|
272
|
+
return new Promise((resolve, reject) => {
|
|
273
|
+
let port;
|
|
274
|
+
let settled = false;
|
|
275
|
+
let grace;
|
|
276
|
+
const finish = (outcome) => {
|
|
277
|
+
if (settled)
|
|
278
|
+
return;
|
|
279
|
+
settled = true;
|
|
280
|
+
clearTimeout(grace);
|
|
281
|
+
input.signal.removeEventListener("abort", stop);
|
|
282
|
+
outcome();
|
|
283
|
+
port?.terminate();
|
|
284
|
+
};
|
|
285
|
+
function stop() {
|
|
286
|
+
Atomics.store(cancel, 0, 1);
|
|
287
|
+
// A guest blocked in a host call is released by that call's own abort;
|
|
288
|
+
// one stuck in a native operation gets a grace period, then is dropped.
|
|
289
|
+
grace = setTimeout(() => finish(() => reject(new CodeModeError("INTERRUPTED", "The program did not stop in time and was abandoned", "Run it again if the result is still needed"))), STOP_GRACE_MS);
|
|
290
|
+
}
|
|
291
|
+
if (input.signal.aborted) {
|
|
292
|
+
reject(input.signal.reason);
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
input.signal.addEventListener("abort", stop, { once: true });
|
|
296
|
+
const early = [];
|
|
297
|
+
const send = (message) => {
|
|
298
|
+
try {
|
|
299
|
+
port?.post(message);
|
|
300
|
+
}
|
|
301
|
+
catch {
|
|
302
|
+
finish(() => reject(sandboxFailed()));
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
const onMessage = (message) => {
|
|
306
|
+
if (settled)
|
|
307
|
+
return;
|
|
308
|
+
if (!port) {
|
|
309
|
+
early.push(message);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
switch (message.type) {
|
|
313
|
+
case "ready":
|
|
314
|
+
send({
|
|
315
|
+
type: "run",
|
|
316
|
+
code: input.code,
|
|
317
|
+
params: input.params,
|
|
318
|
+
limits: input.limits,
|
|
319
|
+
cancel: cancel.buffer,
|
|
320
|
+
});
|
|
321
|
+
return;
|
|
322
|
+
case "call": {
|
|
323
|
+
const { id } = message;
|
|
324
|
+
// Building a reply runs host-controlled code (a provider error's own
|
|
325
|
+
// methods): any failure settles the run instead of escaping as a rejection.
|
|
326
|
+
const reply = (build) => {
|
|
327
|
+
if (settled)
|
|
328
|
+
return;
|
|
329
|
+
try {
|
|
330
|
+
send(build());
|
|
331
|
+
}
|
|
332
|
+
catch {
|
|
333
|
+
finish(() => reject(sandboxFailed()));
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
input.call(message.operation, message.args).then((value) => reply(() => encodeReply(id, message.operation, value, input.limits.valueLength)), (error) => reply(() => ({
|
|
337
|
+
type: "reply",
|
|
338
|
+
id,
|
|
339
|
+
ok: false,
|
|
340
|
+
// The base serializer reads plain fields: a subclass cannot override it.
|
|
341
|
+
error: CodeModeError.prototype.toJSON.call(error instanceof CodeModeError ? error : new CodeModeError("OPERATION_FAILED", `api.${message.operation} failed`, "Check the arguments, then retry once")),
|
|
342
|
+
})));
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
case "done":
|
|
346
|
+
// A guest can finish during the stop grace period: an abort still wins.
|
|
347
|
+
finish(() => input.signal.aborted ? reject(input.signal.reason) : resolve(message.value));
|
|
348
|
+
return;
|
|
349
|
+
case "fail":
|
|
350
|
+
finish(() => reject(codeModeErrorFromFields(message.error) ?? sandboxFailed()));
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
spawn(onMessage, () => finish(() => reject(sandboxFailed()))).then((spawned) => {
|
|
355
|
+
if (settled) {
|
|
356
|
+
spawned.terminate();
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
port = spawned;
|
|
360
|
+
for (const message of early.splice(0))
|
|
361
|
+
onMessage(message);
|
|
362
|
+
}, (error) => finish(() => reject(workerSpawnFailure(error))));
|
|
363
|
+
});
|
|
364
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Messages between the host and the Code Mode guest worker. Only JSON values
|
|
3
|
+
* and one SharedArrayBuffer (the cancel flag) cross the boundary.
|
|
4
|
+
*
|
|
5
|
+
* @module code-mode/worker-protocol
|
|
6
|
+
*/
|
|
7
|
+
import type { CodeModeErrorFields } from "./errors.js";
|
|
8
|
+
import type { SandboxLimits } from "./sandbox-core.js";
|
|
9
|
+
/** Host to worker. */
|
|
10
|
+
export type HostMessage = {
|
|
11
|
+
readonly type: "run";
|
|
12
|
+
readonly code: string;
|
|
13
|
+
readonly params: Record<string, unknown>;
|
|
14
|
+
readonly limits: SandboxLimits;
|
|
15
|
+
/** Int32Array flag: 1 asks the guest to stop at its next interrupt check. */
|
|
16
|
+
readonly cancel: SharedArrayBuffer;
|
|
17
|
+
} | {
|
|
18
|
+
readonly type: "reply";
|
|
19
|
+
readonly id: number;
|
|
20
|
+
readonly ok: true;
|
|
21
|
+
/** The JSON text of the result, already within `limits.valueLength`. */
|
|
22
|
+
readonly json: string;
|
|
23
|
+
} | {
|
|
24
|
+
readonly type: "reply";
|
|
25
|
+
readonly id: number;
|
|
26
|
+
readonly ok: false;
|
|
27
|
+
readonly error: CodeModeErrorFields;
|
|
28
|
+
};
|
|
29
|
+
/** Worker to host. */
|
|
30
|
+
export type GuestMessage = {
|
|
31
|
+
readonly type: "ready";
|
|
32
|
+
} | {
|
|
33
|
+
readonly type: "call";
|
|
34
|
+
readonly id: number;
|
|
35
|
+
readonly operation: string;
|
|
36
|
+
readonly args: unknown[];
|
|
37
|
+
} | {
|
|
38
|
+
readonly type: "done";
|
|
39
|
+
readonly value: unknown;
|
|
40
|
+
} | {
|
|
41
|
+
readonly type: "fail";
|
|
42
|
+
readonly error: CodeModeErrorFields;
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=worker-protocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-protocol.d.ts","sourceRoot":"","sources":["../../../src/src/code-mode/worker-protocol.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAEvD,sBAAsB;AACtB,MAAM,MAAM,WAAW,GACnB;IACA,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACzC,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;IAC/B,6EAA6E;IAC7E,QAAQ,CAAC,MAAM,EAAE,iBAAiB,CAAC;CACpC,GACC;IACA,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAClB,wEAAwE;IACxE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB,GACC;IACA,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IACnB,QAAQ,CAAC,KAAK,EAAE,mBAAmB,CAAC;CACrC,CAAC;AAEJ,sBAAsB;AACtB,MAAM,MAAM,YAAY,GACpB;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAA;CAAE,GAC1B;IACA,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,IAAI,EAAE,OAAO,EAAE,CAAC;CAC1B,GACC;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GAClD;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,mBAAmB,CAAA;CAAE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"panel-controls.d.ts","sourceRoot":"","sources":["../../../../../src/src/host/renderer/css/panel-controls.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"panel-controls.d.ts","sourceRoot":"","sources":["../../../../../src/src/host/renderer/css/panel-controls.ts"],"names":[],"mappings":"AAEA,qEAAqE;AACrE,wBAAgB,mBAAmB,IAAI,MAAM,CA0F5C"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { VIEWER_FALLBACK_HEIGHT } from "../js/intrinsic-layout.js";
|
|
1
2
|
/** Shared styles for the controls around mounted Compose viewers. */
|
|
2
3
|
export function getPanelControlsCss() {
|
|
3
4
|
return `
|
|
@@ -17,7 +18,10 @@ export function getPanelControlsCss() {
|
|
|
17
18
|
grid-auto-rows: auto;
|
|
18
19
|
}
|
|
19
20
|
#container[data-compose-intrinsic] > .compose-viewer {
|
|
20
|
-
height: var(
|
|
21
|
+
height: var(
|
|
22
|
+
--compose-native-height,
|
|
23
|
+
var(--compose-template-height, ${VIEWER_FALLBACK_HEIGHT}px)
|
|
24
|
+
);
|
|
21
25
|
}
|
|
22
26
|
.compose-viewer > iframe {
|
|
23
27
|
display: block; width: 100%; height: 100%; border: 0;
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Connect the generated dashboard to its enclosing MCP Apps host as an App.
|
|
3
|
+
*
|
|
4
|
+
* Viewer reports size the board at once, even while the handshake is pending,
|
|
5
|
+
* and a plain iframe that never replies keeps working. A host that explicitly
|
|
6
|
+
* refuses `ui/initialize` (an error reply or another protocol version) is
|
|
7
|
+
* logged as `{ code: "CARD_HANDSHAKE_FAILED", expected, received }`; the board
|
|
8
|
+
* then fills its frame again (`data-compose-card-refused`) and ignores later
|
|
9
|
+
* viewer reports for sizing. ChatGPT's `notifyIntrinsicHeight` keeps the card
|
|
10
|
+
* sized regardless.
|
|
11
|
+
*/
|
|
2
12
|
export declare function generateCardHeightScript(): string;
|
|
3
13
|
//# sourceMappingURL=card-height.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"card-height.d.ts","sourceRoot":"","sources":["../../../../../src/src/host/renderer/js/card-height.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"card-height.d.ts","sourceRoot":"","sources":["../../../../../src/src/host/renderer/js/card-height.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;GAUG;AACH,wBAAgB,wBAAwB,IAAI,MAAM,CA2FjD"}
|
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
import { COMPOSE_VERSION, MCP_APPS_PROTOCOL_VERSION } from "../../../version.js";
|
|
2
|
-
|
|
2
|
+
import { generateIntrinsicLayoutFunction } from "./intrinsic-layout.js";
|
|
3
|
+
/**
|
|
4
|
+
* Connect the generated dashboard to its enclosing MCP Apps host as an App.
|
|
5
|
+
*
|
|
6
|
+
* Viewer reports size the board at once, even while the handshake is pending,
|
|
7
|
+
* and a plain iframe that never replies keeps working. A host that explicitly
|
|
8
|
+
* refuses `ui/initialize` (an error reply or another protocol version) is
|
|
9
|
+
* logged as `{ code: "CARD_HANDSHAKE_FAILED", expected, received }`; the board
|
|
10
|
+
* then fills its frame again (`data-compose-card-refused`) and ignores later
|
|
11
|
+
* viewer reports for sizing. ChatGPT's `notifyIntrinsicHeight` keeps the card
|
|
12
|
+
* sized regardless.
|
|
13
|
+
*/
|
|
3
14
|
export function generateCardHeightScript() {
|
|
4
15
|
return `
|
|
5
16
|
(() => {
|
|
@@ -7,7 +18,7 @@ export function generateCardHeightScript() {
|
|
|
7
18
|
if (!host || host === window) return;
|
|
8
19
|
const board = document.querySelector('#container[data-compose-board]');
|
|
9
20
|
if (!board) return;
|
|
10
|
-
|
|
21
|
+
${generateIntrinsicLayoutFunction()}
|
|
11
22
|
|
|
12
23
|
const initializeId = 'compose-card-initialize';
|
|
13
24
|
const protocolVersion = '${MCP_APPS_PROTOCOL_VERSION}';
|
|
@@ -16,26 +27,62 @@ export function generateCardHeightScript() {
|
|
|
16
27
|
const send = (message) => host.postMessage(
|
|
17
28
|
{ jsonrpc: '2.0', ...message }, hostOrigin && hostOrigin !== 'null' ? hostOrigin : '*'
|
|
18
29
|
);
|
|
30
|
+
function heightChannel() {
|
|
31
|
+
if (hostOrigin !== undefined) {
|
|
32
|
+
return (height) => send({ method: 'ui/notifications/size-changed', params: { height } });
|
|
33
|
+
}
|
|
34
|
+
if (typeof window.openai?.notifyIntrinsicHeight === 'function') {
|
|
35
|
+
return (height) => window.openai.notifyIntrinsicHeight(height);
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
19
39
|
function reportHeight() {
|
|
40
|
+
const notify = heightChannel();
|
|
41
|
+
if (!notify) return;
|
|
42
|
+
if (board.hasAttribute('data-compose-card-refused')) {
|
|
43
|
+
// ChatGPT's bridge arrived after the refusal: it sizes the card.
|
|
44
|
+
board.removeAttribute('data-compose-card-refused');
|
|
45
|
+
observer.observe(board, { box: 'border-box' });
|
|
46
|
+
}
|
|
47
|
+
enterIntrinsicLayout(board);
|
|
20
48
|
// The viewport still has the previous height while a row disappears.
|
|
21
49
|
// The grid's border box includes the skeleton, row gaps and padding.
|
|
22
50
|
const height = Math.ceil(board.getBoundingClientRect().height);
|
|
23
51
|
if (!Number.isFinite(height) || height < 0 || height === lastHeight) return;
|
|
24
|
-
|
|
25
|
-
send({ method: 'ui/notifications/size-changed', params: { height } });
|
|
26
|
-
} else if (typeof window.openai?.notifyIntrinsicHeight === 'function') {
|
|
27
|
-
window.openai.notifyIntrinsicHeight(height);
|
|
28
|
-
} else {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
52
|
+
notify(height);
|
|
31
53
|
lastHeight = height;
|
|
32
54
|
}
|
|
33
55
|
const observer = new ResizeObserver(reportHeight);
|
|
56
|
+
// An explicit refusal hands the board back to its frame, unless
|
|
57
|
+
// ChatGPT's height bridge still sizes the card.
|
|
58
|
+
function refuseCard() {
|
|
59
|
+
if (heightChannel()) return;
|
|
60
|
+
observer.disconnect();
|
|
61
|
+
window.removeEventListener('message', onHostMessage);
|
|
62
|
+
board.setAttribute('data-compose-card-refused', '');
|
|
63
|
+
board.removeAttribute('data-compose-intrinsic');
|
|
64
|
+
for (const viewer of Array.from(board.children ?? [])) {
|
|
65
|
+
viewer.style?.removeProperty?.('--compose-native-height');
|
|
66
|
+
viewer.style?.removeProperty?.('--compose-template-height');
|
|
67
|
+
}
|
|
68
|
+
board.dispatchEvent(new Event('compose-layout-intrinsic'));
|
|
69
|
+
}
|
|
34
70
|
function onHostMessage(event) {
|
|
35
71
|
const msg = event.data;
|
|
36
72
|
if (event.source !== host || msg?.jsonrpc !== '2.0') return;
|
|
37
73
|
if (hostOrigin === undefined) {
|
|
38
|
-
if (msg.id !== initializeId
|
|
74
|
+
if (msg.id !== initializeId) return;
|
|
75
|
+
const received = msg.result?.protocolVersion;
|
|
76
|
+
if (received !== protocolVersion) {
|
|
77
|
+
console.warn('[mcp-compose] Card height handshake failed', {
|
|
78
|
+
code: 'CARD_HANDSHAKE_FAILED',
|
|
79
|
+
expected: protocolVersion,
|
|
80
|
+
received: typeof received === 'string' ? received : null,
|
|
81
|
+
...(msg.error ? { error: msg.error } : {})
|
|
82
|
+
});
|
|
83
|
+
refuseCard();
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
39
86
|
hostOrigin = event.origin;
|
|
40
87
|
send({ method: 'ui/notifications/initialized' });
|
|
41
88
|
lastHeight = undefined;
|
|
@@ -12,6 +12,17 @@
|
|
|
12
12
|
*/
|
|
13
13
|
import type { CompositeUiDescriptor } from "../../../core/types/descriptor.js";
|
|
14
14
|
import type { ResolvedRendererSlotOptions } from "../options.js";
|
|
15
|
+
/**
|
|
16
|
+
* Tallest height, in CSS pixels, a viewer can claim with
|
|
17
|
+
* `ui/notifications/size-changed`. Taller content scrolls inside the viewer's
|
|
18
|
+
* iframe. Advertised to each viewer as `containerDimensions.maxHeight`.
|
|
19
|
+
*
|
|
20
|
+
* The cap also bounds a viewer whose content follows its own iframe
|
|
21
|
+
* (`min-height: 100vh` plus padding): it stops growing here. Compose does not
|
|
22
|
+
* guess such loops from equal increments, which a list gaining rows produces
|
|
23
|
+
* too.
|
|
24
|
+
*/
|
|
25
|
+
export declare const MAX_VIEWER_HEIGHT = 2400;
|
|
15
26
|
/**
|
|
16
27
|
* Generate the event bus JavaScript for a composite UI.
|
|
17
28
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event-bus.d.ts","sourceRoot":"","sources":["../../../../../src/src/host/renderer/js/event-bus.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAG/E,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"event-bus.d.ts","sourceRoot":"","sources":["../../../../../src/src/host/renderer/js/event-bus.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAG/E,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,eAAe,CAAC;AAOjE;;;;;;;;;GASG;AACH,eAAO,MAAM,iBAAiB,OAAQ,CAAC;AAEvC;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,sBAAsB,CACpC,UAAU,EAAE,qBAAqB,EACjC,KAAK,GAAE,WAAW,CAAC,MAAM,EAAE,2BAA2B,CAAa,GAClE,MAAM,CAolBR"}
|