@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
|
@@ -1,150 +1,218 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Run a Code Mode program: QuickJS isolation, resource limits, cancellation and
|
|
4
|
+
* machine-readable errors. By default the guest runs in a worker, so a
|
|
5
|
+
* CPU-bound program cannot block the host's event loop.
|
|
6
|
+
*
|
|
7
|
+
* @module code-mode/sandbox
|
|
8
|
+
*/
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.maxCodeLength = void 0;
|
|
4
|
-
exports.
|
|
10
|
+
exports.maxCodeLength = exports.encodeValue = exports.CODE_MODE_LIMITS = void 0;
|
|
11
|
+
exports.resolveSandboxLimits = resolveSandboxLimits;
|
|
12
|
+
exports.assertSandboxIsolation = assertSandboxIsolation;
|
|
13
|
+
exports.untilAborted = untilAborted;
|
|
14
|
+
exports.waitForOperation = waitForOperation;
|
|
5
15
|
exports.runProgram = runProgram;
|
|
6
16
|
const quickjs_emscripten_1 = require("quickjs-emscripten");
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
const errors_js_1 = require("./errors.js");
|
|
18
|
+
const sandbox_core_js_1 = require("./sandbox-core.js");
|
|
19
|
+
const worker_host_js_1 = require("./worker-host.js");
|
|
20
|
+
var sandbox_core_js_2 = require("./sandbox-core.js");
|
|
21
|
+
Object.defineProperty(exports, "CODE_MODE_LIMITS", { enumerable: true, get: function () { return sandbox_core_js_2.CODE_MODE_LIMITS; } });
|
|
22
|
+
Object.defineProperty(exports, "encodeValue", { enumerable: true, get: function () { return sandbox_core_js_2.encodeValue; } });
|
|
23
|
+
Object.defineProperty(exports, "maxCodeLength", { enumerable: true, get: function () { return sandbox_core_js_2.maxCodeLength; } });
|
|
24
|
+
/**
|
|
25
|
+
* Resolve and validate limits. Throws a RangeError for a value that is not a
|
|
26
|
+
* positive integer.
|
|
27
|
+
*/
|
|
28
|
+
function resolveSandboxLimits(overrides = {}) {
|
|
29
|
+
const limits = { ...sandbox_core_js_1.CODE_MODE_LIMITS, ...overrides };
|
|
30
|
+
for (const [name, value] of Object.entries(limits)) {
|
|
31
|
+
if (!Number.isSafeInteger(value) || value <= 0) {
|
|
32
|
+
throw new RangeError(`Code Mode limit ${name} must be a positive integer.`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
for (const name of ["memoryBytes", "stackBytes"]) {
|
|
36
|
+
// The QuickJS WASM setters take an i32: a larger value wraps, and 0 disables the limit.
|
|
37
|
+
if (limits[name] > MAX_WASM_BYTES) {
|
|
38
|
+
throw new RangeError(`Code Mode limit ${name} must be at most ${MAX_WASM_BYTES} bytes.`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
for (const name of ["memoryBytes", "stackBytes"]) {
|
|
42
|
+
// Below this the VM cannot even finish its own setup, and the failure carries no diagnosis.
|
|
43
|
+
if (limits[name] < MIN_VM_BYTES[name]) {
|
|
44
|
+
throw new RangeError(`Code Mode limit ${name} must be at least ${MIN_VM_BYTES[name]} bytes.`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
for (const name of ["cpuMs", "wallMs"]) {
|
|
48
|
+
// Timers clamp longer delays to about 1 ms.
|
|
49
|
+
if (limits[name] > MAX_TIMER_DELAY_MS) {
|
|
50
|
+
throw new RangeError(`Code Mode limit ${name} must be at most ${MAX_TIMER_DELAY_MS} ms.`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return Object.freeze(limits);
|
|
54
|
+
}
|
|
55
|
+
const MAX_TIMER_DELAY_MS = 2_147_483_647;
|
|
56
|
+
const MAX_WASM_BYTES = 2_147_483_647;
|
|
57
|
+
/** About ten times what the VM needs to start, so a small limit fails at configuration. */
|
|
58
|
+
const MIN_VM_BYTES = { memoryBytes: 64 * 1024, stackBytes: 16 * 1024 };
|
|
59
|
+
/**
|
|
60
|
+
* Throw `SANDBOX_UNAVAILABLE` when the isolation cannot run in this runtime.
|
|
61
|
+
* Call it at configuration time to fail before the first program.
|
|
62
|
+
*
|
|
63
|
+
* This is a partial check. Under Deno it only verifies the permission for the
|
|
64
|
+
* worker module itself; the worker also needs read access to the `code-mode`
|
|
65
|
+
* directory and to the QuickJS WASM file, which surface as `SANDBOX_FAILED` on
|
|
66
|
+
* the first run.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* assertSandboxIsolation("worker");
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
function assertSandboxIsolation(isolation) {
|
|
74
|
+
if (isolation !== "worker" && isolation !== "inline") {
|
|
75
|
+
throw new RangeError('Code Mode isolation must be "worker" or "inline".');
|
|
76
|
+
}
|
|
77
|
+
if (isolation === "inline")
|
|
78
|
+
return;
|
|
79
|
+
const reason = (0, worker_host_js_1.workerIsolationUnavailable)();
|
|
80
|
+
if (reason) {
|
|
81
|
+
throw new errors_js_1.CodeModeError("SANDBOX_UNAVAILABLE", `Worker isolation is unavailable: ${reason}`, reason.includes("--allow-worker")
|
|
82
|
+
? 'Start Node with --allow-worker, or configure isolation: "inline"'
|
|
83
|
+
: reason.startsWith("Deno has no ")
|
|
84
|
+
? 'Grant that Deno permission, or configure isolation: "inline"'
|
|
85
|
+
: 'Configure isolation: "inline" explicitly for this runtime', { context: { isolation } });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** A JSON copy of the params, identical in both isolations. */
|
|
89
|
+
function jsonParams(params, limit) {
|
|
90
|
+
let value;
|
|
91
|
+
try {
|
|
92
|
+
value = JSON.parse((0, sandbox_core_js_1.encodeValue)(params ?? {}, limit));
|
|
16
93
|
}
|
|
17
|
-
|
|
94
|
+
catch {
|
|
95
|
+
value = undefined;
|
|
96
|
+
}
|
|
97
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
98
|
+
throw new errors_js_1.CodeModeError("PARAMS_INVALID", `params must be a JSON object within ${limit} characters`, "Pass a smaller plain object", { context: { limit } });
|
|
99
|
+
}
|
|
100
|
+
return value;
|
|
101
|
+
}
|
|
102
|
+
/** Wait for `work`, but give up as soon as `signal` aborts. A late rejection of `work` is observed. */
|
|
103
|
+
function untilAborted(work, signal) {
|
|
104
|
+
return new Promise((resolve, reject) => {
|
|
105
|
+
if (signal.aborted) {
|
|
106
|
+
work.catch(() => { });
|
|
107
|
+
reject(signal.reason);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const abort = () => reject(signal.reason);
|
|
111
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
112
|
+
work.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
|
|
113
|
+
});
|
|
18
114
|
}
|
|
19
115
|
/** Wait for host I/O without allowing a provider that ignores cancellation to hold the VM open. */
|
|
20
|
-
function waitForOperation(invoke, signal
|
|
116
|
+
function waitForOperation(invoke, signal,
|
|
117
|
+
/** True once the caller aborted or the wall deadline passed, even if its timer could not run. */
|
|
118
|
+
stopped = () => signal.aborted) {
|
|
21
119
|
signal.throwIfAborted();
|
|
22
120
|
return new Promise((resolve, reject) => {
|
|
23
121
|
const abort = () => reject(signal.reason);
|
|
24
122
|
signal.addEventListener("abort", abort, { once: true });
|
|
25
123
|
Promise.resolve().then(() => {
|
|
26
|
-
|
|
124
|
+
// Host work may have held the event loop since admission: check the clock
|
|
125
|
+
// right before any provider work starts.
|
|
126
|
+
if (stopped())
|
|
127
|
+
throw signal.reason;
|
|
27
128
|
return invoke();
|
|
28
129
|
}).then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
|
|
29
130
|
});
|
|
30
131
|
}
|
|
31
132
|
/**
|
|
32
|
-
* Run a function body in an isolated QuickJS context. Only JSON and explicit
|
|
33
|
-
* operations cross into the host.
|
|
133
|
+
* Run a function body in an isolated QuickJS context. Only JSON and explicit
|
|
134
|
+
* snippet operations cross into the host. Host operations run one at a time,
|
|
135
|
+
* when the guest awaits them; their time does not count as guest CPU.
|
|
136
|
+
*
|
|
137
|
+
* Failures are {@link CodeModeError}s, except that a caller abort rejects with
|
|
138
|
+
* the caller signal's reason. `invoke` should reject with a CodeModeError;
|
|
139
|
+
* any other rejection reaches the guest as `OPERATION_FAILED`, without its
|
|
140
|
+
* message.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* const value = await runProgram(
|
|
145
|
+
* "const r = await api.runSnippet('cad', 'plate-stl', {}); return r.value;",
|
|
146
|
+
* {},
|
|
147
|
+
* (operation, args) => myHost(operation, args),
|
|
148
|
+
* );
|
|
149
|
+
* ```
|
|
34
150
|
*/
|
|
35
|
-
async function runProgram(code, params, invoke, callerSignal) {
|
|
36
|
-
if (typeof code !== "string" || !code.trim() || code.length >
|
|
37
|
-
throw new
|
|
151
|
+
async function runProgram(code, params, invoke, callerSignal, options = {}) {
|
|
152
|
+
if (typeof code !== "string" || !code.trim() || code.length > sandbox_core_js_1.maxCodeLength) {
|
|
153
|
+
throw new errors_js_1.CodeModeError("INVALID_INPUT", `Supply a JavaScript function body of 1 to ${sandbox_core_js_1.maxCodeLength} characters`, "Send a shorter function body", { context: { limit: sandbox_core_js_1.maxCodeLength } });
|
|
38
154
|
}
|
|
39
|
-
|
|
40
|
-
|
|
155
|
+
const isolation = options.isolation ?? "worker";
|
|
156
|
+
assertSandboxIsolation(isolation);
|
|
157
|
+
const limits = resolveSandboxLimits(options.limits);
|
|
158
|
+
const input = jsonParams(params, limits.valueLength);
|
|
41
159
|
callerSignal?.throwIfAborted();
|
|
42
160
|
const controller = new AbortController();
|
|
43
161
|
const signal = controller.signal;
|
|
44
162
|
const abort = () => controller.abort(callerSignal?.reason);
|
|
45
163
|
callerSignal?.addEventListener("abort", abort, { once: true });
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (text.length > maxValueLength)
|
|
65
|
-
throw new Error("JSON value exceeds the size limit.");
|
|
66
|
-
return JSON.parse(text);
|
|
67
|
-
}
|
|
68
|
-
finally {
|
|
69
|
-
encoded.dispose();
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
function decode(value) {
|
|
73
|
-
const encoded = vm.newString(encodeValue(value));
|
|
74
|
-
try {
|
|
75
|
-
return scope.manage(vm.unwrapResult(vm.callFunction(parse, vm.undefined, encoded)));
|
|
76
|
-
}
|
|
77
|
-
finally {
|
|
78
|
-
encoded.dispose();
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
const dispatch = scope.manage(vm.newFunction("dispatch", (name, args) => {
|
|
82
|
-
if (++calls > maxOperations || vm.typeof(name) !== "string") {
|
|
83
|
-
throw new Error("Snippet operation limit exceeded.");
|
|
84
|
-
}
|
|
85
|
-
const operation = vm.getString(name);
|
|
86
|
-
const arguments_ = read(args);
|
|
87
|
-
if (!Array.isArray(arguments_))
|
|
88
|
-
throw new Error("Expected snippet arguments.");
|
|
89
|
-
const deferred = scope.manage(vm.newPromise());
|
|
90
|
-
pending.push(async () => {
|
|
91
|
-
try {
|
|
92
|
-
const result = await waitForOperation(() => invoke(operation, arguments_, signal), signal);
|
|
93
|
-
signal.throwIfAborted();
|
|
94
|
-
deferred.resolve(decode(result));
|
|
95
|
-
}
|
|
96
|
-
catch (error) {
|
|
97
|
-
const message = scope.manage(vm.newString(error instanceof Error ? error.message : "Snippet failed."));
|
|
98
|
-
deferred.reject(message);
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
return deferred.handle.dup();
|
|
102
|
-
}));
|
|
103
|
-
vm.setProp(vm.global, "dispatch", dispatch);
|
|
104
|
-
vm.setProp(vm.global, "params", decode(params));
|
|
105
|
-
const promise = scope.manage(vm.unwrapResult(vm.evalCode(`(() => {
|
|
106
|
-
const call = globalThis.dispatch, params = globalThis.params;
|
|
107
|
-
delete globalThis.dispatch;
|
|
108
|
-
delete globalThis.params;
|
|
109
|
-
const api = Object.freeze({
|
|
110
|
-
params,
|
|
111
|
-
listSnippets: (...args) => call("listSnippets", args),
|
|
112
|
-
runSnippet: (...args) => call("runSnippet", args),
|
|
113
|
-
panel: (...args) => call("panel", args),
|
|
114
|
-
sync: (...args) => call("sync", args),
|
|
115
|
-
});
|
|
116
|
-
return (async function(api) { "use strict";\n${code}\n})(api);
|
|
117
|
-
})()`, "mcpview-code-mode.js", { type: "global" })));
|
|
118
|
-
for (;;) {
|
|
119
|
-
signal.throwIfAborted();
|
|
120
|
-
if (performance.now() >= cpuDeadline) {
|
|
121
|
-
throw new Error("JavaScript exceeded its time limit.");
|
|
122
|
-
}
|
|
123
|
-
const state = vm.getPromiseState(promise);
|
|
124
|
-
if (state.type === "fulfilled") {
|
|
125
|
-
const value = scope.manage(state.value);
|
|
126
|
-
if (pending.length) {
|
|
127
|
-
throw new Error("Await every snippet operation, including api.panel() and api.sync().");
|
|
128
|
-
}
|
|
129
|
-
return vm.typeof(value) === "undefined" ? null : read(value);
|
|
130
|
-
}
|
|
131
|
-
if (state.type === "rejected") {
|
|
132
|
-
const reason = scope.manage(state.error);
|
|
133
|
-
throw new Error(vm.typeof(reason) === "string" ? vm.getString(reason) : "JavaScript failed.");
|
|
134
|
-
}
|
|
135
|
-
if (vm.runtime.hasPendingJob()) {
|
|
136
|
-
vm.unwrapResult(vm.runtime.executePendingJobs(1));
|
|
137
|
-
}
|
|
138
|
-
else if (pending.length) {
|
|
139
|
-
const before = performance.now();
|
|
140
|
-
await pending.shift()();
|
|
141
|
-
cpuDeadline += performance.now() - before;
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
throw new Error("JavaScript is waiting on an unresolved promise.");
|
|
145
|
-
}
|
|
164
|
+
const wallDeadline = performance.now() + limits.wallMs;
|
|
165
|
+
const expire = () => controller.abort(new errors_js_1.CodeModeError("TIME_LIMIT", `The program ran for more than ${limits.wallMs} ms`, "Split the work into several execute calls", { context: { limit: "wall", ms: limits.wallMs } }));
|
|
166
|
+
const timer = setTimeout(expire, limits.wallMs);
|
|
167
|
+
/** True once the caller aborted or the wall deadline passed, even if the timer could not run. */
|
|
168
|
+
const stopped = () => {
|
|
169
|
+
if (!signal.aborted && performance.now() >= wallDeadline)
|
|
170
|
+
expire();
|
|
171
|
+
return signal.aborted;
|
|
172
|
+
};
|
|
173
|
+
const call = async (operation, args) => {
|
|
174
|
+
try {
|
|
175
|
+
return await waitForOperation(() => invoke(operation, args, signal, stopped), signal, stopped);
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
if (error instanceof errors_js_1.CodeModeError)
|
|
179
|
+
throw error;
|
|
180
|
+
if (signal.aborted) {
|
|
181
|
+
throw new errors_js_1.CodeModeError("INTERRUPTED", `api.${operation} was interrupted`, "Run the program again if the result is still needed");
|
|
146
182
|
}
|
|
147
|
-
|
|
183
|
+
throw new errors_js_1.CodeModeError("OPERATION_FAILED", `api.${operation} failed`, "Check the arguments, then retry once", { context: { operation } });
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
try {
|
|
187
|
+
if (isolation === "inline") {
|
|
188
|
+
const quickjs = await untilAborted((0, quickjs_emscripten_1.getQuickJS)(), signal);
|
|
189
|
+
signal.throwIfAborted();
|
|
190
|
+
const value = await (0, sandbox_core_js_1.runVm)({
|
|
191
|
+
quickjs,
|
|
192
|
+
code,
|
|
193
|
+
params: input,
|
|
194
|
+
limits,
|
|
195
|
+
call,
|
|
196
|
+
// An inline guest holds the event loop, so the wall timer cannot fire: poll it.
|
|
197
|
+
isCancelled: stopped,
|
|
198
|
+
});
|
|
199
|
+
if (stopped())
|
|
200
|
+
throw signal.reason;
|
|
201
|
+
return value;
|
|
202
|
+
}
|
|
203
|
+
const value = await (0, worker_host_js_1.runInWorker)({ code, params: input, limits, call, signal });
|
|
204
|
+
if (stopped())
|
|
205
|
+
throw signal.reason;
|
|
206
|
+
return value;
|
|
207
|
+
}
|
|
208
|
+
catch (error) {
|
|
209
|
+
if (stopped())
|
|
210
|
+
throw signal.reason;
|
|
211
|
+
const failure = error instanceof errors_js_1.CodeModeError ? error : new errors_js_1.CodeModeError("SANDBOX_FAILED", "The sandbox failed while running the program", "Run the program again");
|
|
212
|
+
// A worker that died leaves provider calls in flight: cancel them, but keep this outcome.
|
|
213
|
+
if (failure.code === "SANDBOX_FAILED")
|
|
214
|
+
controller.abort(failure);
|
|
215
|
+
throw failure;
|
|
148
216
|
}
|
|
149
217
|
finally {
|
|
150
218
|
clearTimeout(timer);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../../src/src/code-mode/tool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAItD,8EAA8E;AAC9E,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC,MAAM,EAAE,IAAI,CAAC,eAAe,EAAE,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC;IACnF,uFAAuF;IACvF,QAAQ,CAAC,WAAW,CAAC,EAAE,OAAO,CAAC;IAC/B,QAAQ,CAAC,WAAW,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;CAC/C;AAED,0EAA0E;AAC1E,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,
|
|
1
|
+
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../../src/src/code-mode/tool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAItD,8EAA8E;AAC9E,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC,MAAM,EAAE,IAAI,CAAC,eAAe,EAAE,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC;IACnF,uFAAuF;IACvF,QAAQ,CAAC,WAAW,CAAC,EAAE,OAAO,CAAC;IAC/B,QAAQ,CAAC,WAAW,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;CAC/C;AAED,0EAA0E;AAC1E,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAoEpF"}
|
|
@@ -22,6 +22,8 @@ function createSnippetExecuteTool(options) {
|
|
|
22
22
|
"Configured provider aliases: " + JSON.stringify(available) + ".",
|
|
23
23
|
"await api.runSnippet(provider, name, input) returns a receipt {id, value}.",
|
|
24
24
|
"Pass values between calls. Use await api.panel(receipt.id, optionalTitle) for each result to display, in display order. Return a JSON summary.",
|
|
25
|
+
"Await every api call (or pass them to Promise.all): a call that is never awaited does not run.",
|
|
26
|
+
"A failed api call throws an Error with code, recovery and context; tool errors read [execute] CODE: detail. Recovery: hint.",
|
|
25
27
|
...(eventPorts.length
|
|
26
28
|
? [
|
|
27
29
|
"After selecting both panels, use await api.sync({from: source.id, event, to: target.id, action}) to route a UI event between them.",
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { CodeModeError } from "./errors.js";
|
|
2
|
+
import { type SandboxLimits } from "./sandbox-core.js";
|
|
3
|
+
import type { HostMessage } from "./worker-protocol.js";
|
|
4
|
+
/**
|
|
5
|
+
* Why worker isolation cannot run here, or undefined when it can. `scope` is the
|
|
6
|
+
* global object to inspect.
|
|
7
|
+
*/
|
|
8
|
+
export declare function workerIsolationUnavailable(scope?: object): string | undefined;
|
|
9
|
+
/** The part of `process` that tells whether Node's permission model allows workers. */
|
|
10
|
+
export interface NodeProcessLike {
|
|
11
|
+
readonly permission?: {
|
|
12
|
+
has?(scope: string): boolean;
|
|
13
|
+
};
|
|
14
|
+
readonly execArgv?: readonly string[];
|
|
15
|
+
readonly env?: Readonly<Record<string, string | undefined>>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Why Node's enforced permission model forbids workers, or undefined. Audit mode
|
|
19
|
+
* (`--permission-audit`) reports a missing grant but still creates the worker, so it
|
|
20
|
+
* does not count.
|
|
21
|
+
*/
|
|
22
|
+
export declare function nodeWorkerPermission(process: NodeProcessLike | undefined): string | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* The permission Deno needs to load the guest module, or undefined for schemes it does
|
|
25
|
+
* not gate. An import query must name the port: a bare `jsr.io` asks about every port
|
|
26
|
+
* and reads "prompt", although Deno grants `jsr.io:443` by default.
|
|
27
|
+
*/
|
|
28
|
+
export declare function guestModulePermission(url: URL): {
|
|
29
|
+
name: "read";
|
|
30
|
+
path: URL;
|
|
31
|
+
} | {
|
|
32
|
+
name: "import";
|
|
33
|
+
host: string;
|
|
34
|
+
} | undefined;
|
|
35
|
+
/** The part of the host's Node options that a Code Mode worker can start with. */
|
|
36
|
+
export declare function nodeWorkerExecArgv(execArgv: readonly string[]): string[];
|
|
37
|
+
/**
|
|
38
|
+
* The host's `NODE_OPTIONS` reduced to what a Code Mode worker can start with. A
|
|
39
|
+
* worker inherits the environment by default, so `--input-type` or
|
|
40
|
+
* `--max-old-space-size` there would break it just like in `execArgv`.
|
|
41
|
+
*/
|
|
42
|
+
export declare function nodeWorkerNodeOptions(options: string | undefined): string;
|
|
43
|
+
/**
|
|
44
|
+
* Split `NODE_OPTIONS` the way Node does: whitespace separates options, double
|
|
45
|
+
* quotes group (and are dropped), and inside quotes a backslash escapes the next
|
|
46
|
+
* character.
|
|
47
|
+
*/
|
|
48
|
+
export declare function parseNodeOptions(options: string): string[];
|
|
49
|
+
/** The host's environment with `NODE_OPTIONS` reduced for a worker. */
|
|
50
|
+
export declare function nodeWorkerEnv(env: Readonly<Record<string, string | undefined>>): Record<string, string | undefined>;
|
|
51
|
+
/**
|
|
52
|
+
* The reply for one host operation. The JSON text is bounded here, before it is
|
|
53
|
+
* posted, so an oversized result never crosses the worker boundary (no parse and
|
|
54
|
+
* no structured clone). The guest decodes the text.
|
|
55
|
+
*/
|
|
56
|
+
export declare function encodeReply(id: number, operation: string, value: unknown, valueLength: number): HostMessage;
|
|
57
|
+
/** Inputs of {@link runInWorker}. */
|
|
58
|
+
export interface WorkerRunInput {
|
|
59
|
+
readonly code: string;
|
|
60
|
+
readonly params: Record<string, unknown>;
|
|
61
|
+
readonly limits: SandboxLimits;
|
|
62
|
+
/** Host operation for the guest. Rejections should be {@link CodeModeError}s. */
|
|
63
|
+
call(operation: string, args: unknown[]): Promise<unknown>;
|
|
64
|
+
/** Aborting stops the guest at its next interrupt check. */
|
|
65
|
+
readonly signal: AbortSignal;
|
|
66
|
+
}
|
|
67
|
+
/** The error for a worker that could not be created at all. */
|
|
68
|
+
export declare function workerSpawnFailure(error: unknown): CodeModeError;
|
|
69
|
+
/**
|
|
70
|
+
* Run one program in a fresh worker and settle once with its value or a
|
|
71
|
+
* {@link CodeModeError}. The worker is always terminated afterwards.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* const value = await runInWorker({
|
|
76
|
+
* code: "return 1 + 1;", params: {}, limits: CODE_MODE_LIMITS,
|
|
77
|
+
* call: () => Promise.reject(new Error("unused")), signal: new AbortController().signal,
|
|
78
|
+
* });
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare function runInWorker(input: WorkerRunInput): Promise<unknown>;
|
|
82
|
+
//# sourceMappingURL=worker-host.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-host.d.ts","sourceRoot":"","sources":["../../../src/src/code-mode/worker-host.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,aAAa,EAA2B,MAAM,aAAa,CAAC;AACrE,OAAO,EAAmB,KAAK,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACxE,OAAO,KAAK,EAAgB,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAoBtE;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,GAAE,MAA8B,GAAG,MAAM,GAAG,SAAS,CAUpG;AAED,uFAAuF;AACvF,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,UAAU,CAAC,EAAE;QAAE,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC;IACvD,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACtC,QAAQ,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;CAC7D;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,eAAe,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAc7F;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,GAAG,GACP;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,GAAG,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CAK5E;AAwCD,kFAAkF;AAClF,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,EAAE,CAYxE;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAEzE;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,CAuB1D;AAOD,uEAAuE;AACvE,wBAAgB,aAAa,CAC3B,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,GAChD,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAIpC;AAyDD;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,EAAE,EAAE,MAAM,EACV,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,OAAO,EACd,WAAW,EAAE,MAAM,GAClB,WAAW,CAwBb;AAED,qCAAqC;AACrC,MAAM,WAAW,cAAc;IAC7B,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,iFAAiF;IACjF,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC3D,4DAA4D;IAC5D,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;CAC9B;AAED,+DAA+D;AAC/D,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,CAWhE;AAUD;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,CAmHnE"}
|