@mcpview/compose 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +110 -1
- package/README.md +116 -24
- package/esm/src/code-mode/engine.d.ts +59 -0
- package/esm/src/code-mode/engine.d.ts.map +1 -0
- package/esm/src/code-mode/engine.js +189 -0
- package/esm/src/code-mode/guest-worker.d.ts +2 -5
- package/esm/src/code-mode/guest-worker.d.ts.map +1 -1
- package/esm/src/code-mode/guest-worker.js +4 -75
- package/esm/src/code-mode/sandbox-api.d.ts +37 -0
- package/esm/src/code-mode/sandbox-api.d.ts.map +1 -0
- package/esm/src/code-mode/sandbox-api.js +36 -0
- package/esm/src/code-mode/sandbox-core.d.ts +99 -21
- package/esm/src/code-mode/sandbox-core.d.ts.map +1 -1
- package/esm/src/code-mode/sandbox-core.js +926 -221
- package/esm/src/code-mode/sandbox-worker.d.ts +37 -0
- package/esm/src/code-mode/sandbox-worker.d.ts.map +1 -0
- package/esm/src/code-mode/sandbox-worker.js +140 -0
- package/esm/src/code-mode/sandbox.d.ts +142 -29
- package/esm/src/code-mode/sandbox.d.ts.map +1 -1
- package/esm/src/code-mode/sandbox.js +402 -68
- package/esm/src/code-mode/surface.d.ts +148 -0
- package/esm/src/code-mode/surface.d.ts.map +1 -0
- package/esm/src/code-mode/surface.js +204 -0
- package/esm/src/code-mode/tool.js +1 -1
- package/esm/src/code-mode/worker-host.d.ts +51 -11
- package/esm/src/code-mode/worker-host.d.ts.map +1 -1
- package/esm/src/code-mode/worker-host.js +114 -48
- package/esm/src/code-mode/worker-protocol.d.ts +45 -8
- package/esm/src/code-mode/worker-protocol.d.ts.map +1 -1
- package/esm/src/code-mode/worker-protocol.js +4 -2
- package/esm/src/version.d.ts +1 -1
- package/esm/src/version.js +1 -1
- package/package.json +9 -1
- package/script/src/code-mode/engine.d.ts +59 -0
- package/script/src/code-mode/engine.d.ts.map +1 -0
- package/script/src/code-mode/engine.js +198 -0
- package/script/src/code-mode/guest-worker.d.ts +2 -5
- package/script/src/code-mode/guest-worker.d.ts.map +1 -1
- package/script/src/code-mode/guest-worker.js +4 -98
- package/script/src/code-mode/sandbox-api.d.ts +37 -0
- package/script/src/code-mode/sandbox-api.d.ts.map +1 -0
- package/script/src/code-mode/sandbox-api.js +50 -0
- package/script/src/code-mode/sandbox-core.d.ts +99 -21
- package/script/src/code-mode/sandbox-core.d.ts.map +1 -1
- package/script/src/code-mode/sandbox-core.js +931 -221
- package/script/src/code-mode/sandbox-worker.d.ts +37 -0
- package/script/src/code-mode/sandbox-worker.d.ts.map +1 -0
- package/script/src/code-mode/sandbox-worker.js +166 -0
- package/script/src/code-mode/sandbox.d.ts +142 -29
- package/script/src/code-mode/sandbox.d.ts.map +1 -1
- package/script/src/code-mode/sandbox.js +432 -73
- package/script/src/code-mode/surface.d.ts +148 -0
- package/script/src/code-mode/surface.d.ts.map +1 -0
- package/script/src/code-mode/surface.js +211 -0
- package/script/src/code-mode/tool.js +1 -1
- package/script/src/code-mode/worker-host.d.ts +51 -11
- package/script/src/code-mode/worker-host.d.ts.map +1 -1
- package/script/src/code-mode/worker-host.js +115 -47
- package/script/src/code-mode/worker-protocol.d.ts +45 -8
- package/script/src/code-mode/worker-protocol.d.ts.map +1 -1
- package/script/src/code-mode/worker-protocol.js +4 -2
- package/script/src/version.d.ts +1 -1
- package/script/src/version.js +1 -1
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The worker side of the Code Mode sandbox. A worker that calls
|
|
3
|
+
* {@link serveSandboxWorker} owns one QuickJS VM, runs one program, and asks
|
|
4
|
+
* the host for each operation; the host terminates it afterwards. It runs
|
|
5
|
+
* under Deno (Web Worker) and Node (`node:worker_threads`). Keep this module
|
|
6
|
+
* free of top-level await, which the CommonJS npm build rejects.
|
|
7
|
+
*
|
|
8
|
+
* Compose's own worker entry is the two lines below. A host bundled into one
|
|
9
|
+
* file writes the same entry, bundles it next to its main file and names it in
|
|
10
|
+
* the `worker` option of the sandbox:
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { serveSandboxWorker } from "@mcpview/compose/code-mode/sandbox-worker";
|
|
15
|
+
*
|
|
16
|
+
* serveSandboxWorker();
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @module code-mode/sandbox-worker
|
|
20
|
+
*/
|
|
21
|
+
import "../../_dnt.polyfills.js";
|
|
22
|
+
/**
|
|
23
|
+
* Serve the sandbox host from the worker this is called in, and tell whether
|
|
24
|
+
* it does: `true` in a worker, `false` anywhere else, where it does nothing.
|
|
25
|
+
*
|
|
26
|
+
* It takes the worker's message port for itself. Call it at the top of a
|
|
27
|
+
* worker module that does nothing else: the one a host names in the `worker`
|
|
28
|
+
* option of the sandbox. Calling it again changes nothing and gives the same
|
|
29
|
+
* answer.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* serveSandboxWorker();
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function serveSandboxWorker(): Promise<boolean>;
|
|
37
|
+
//# sourceMappingURL=sandbox-worker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sandbox-worker.d.ts","sourceRoot":"","sources":["../../../src/src/code-mode/sandbox-worker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,OAAO,yBAAyB,CAAC;AAwHjC;;;;;;;;;;;;;GAaG;AACH,wBAAgB,kBAAkB,IAAI,OAAO,CAAC,OAAO,CAAC,CAMrD"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The worker side of the Code Mode sandbox. A worker that calls
|
|
3
|
+
* {@link serveSandboxWorker} owns one QuickJS VM, runs one program, and asks
|
|
4
|
+
* the host for each operation; the host terminates it afterwards. It runs
|
|
5
|
+
* under Deno (Web Worker) and Node (`node:worker_threads`). Keep this module
|
|
6
|
+
* free of top-level await, which the CommonJS npm build rejects.
|
|
7
|
+
*
|
|
8
|
+
* Compose's own worker entry is the two lines below. A host bundled into one
|
|
9
|
+
* file writes the same entry, bundles it next to its main file and names it in
|
|
10
|
+
* the `worker` option of the sandbox:
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { serveSandboxWorker } from "@mcpview/compose/code-mode/sandbox-worker";
|
|
15
|
+
*
|
|
16
|
+
* serveSandboxWorker();
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @module code-mode/sandbox-worker
|
|
20
|
+
*/
|
|
21
|
+
import "../../_dnt.polyfills.js";
|
|
22
|
+
import * as dntShim from "../../_dnt.shims.js";
|
|
23
|
+
import { newEngine } from "./engine.js";
|
|
24
|
+
import { CodeModeError, codeModeErrorFromFields } from "./errors.js";
|
|
25
|
+
import { HostErrorCarrier, operationFailed, runVm, sandboxFailure } from "./sandbox-core.js";
|
|
26
|
+
/** The host port, or undefined when this module is loaded outside a worker. */
|
|
27
|
+
function connect() {
|
|
28
|
+
const deno = dntShim.dntGlobalThis.Deno;
|
|
29
|
+
if (typeof deno?.version?.deno === "string") {
|
|
30
|
+
const workerScope = dntShim.dntGlobalThis
|
|
31
|
+
.WorkerGlobalScope;
|
|
32
|
+
if (typeof workerScope !== "function" || !(dntShim.dntGlobalThis instanceof workerScope)) {
|
|
33
|
+
return Promise.resolve(undefined);
|
|
34
|
+
}
|
|
35
|
+
const scope = dntShim.dntGlobalThis;
|
|
36
|
+
return Promise.resolve({
|
|
37
|
+
post: (message) => scope.postMessage(message),
|
|
38
|
+
listen: (handler) => scope.addEventListener("message", (event) => handler(event.data)),
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return import("node:worker_threads").then(({ parentPort }) => {
|
|
42
|
+
if (!parentPort)
|
|
43
|
+
return undefined;
|
|
44
|
+
return {
|
|
45
|
+
post: (message) => parentPort.postMessage(message),
|
|
46
|
+
listen: (handler) => parentPort.on("message", handler),
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/** Run the one program the host sends on this port. */
|
|
51
|
+
function serve(port) {
|
|
52
|
+
const waiting = new Map();
|
|
53
|
+
let started = false;
|
|
54
|
+
let namespace = "";
|
|
55
|
+
/** Calls the host completed and cannot undo: their result must reach the program. */
|
|
56
|
+
const irreversible = new Set();
|
|
57
|
+
/** The run waiting for the host's decision about the calls it did not send. */
|
|
58
|
+
let decision;
|
|
59
|
+
port.listen((message) => {
|
|
60
|
+
if (message.type === "unsent-reply") {
|
|
61
|
+
const waiting = decision;
|
|
62
|
+
decision = undefined;
|
|
63
|
+
if (message.ok)
|
|
64
|
+
waiting?.resolve();
|
|
65
|
+
else
|
|
66
|
+
waiting?.reject(codeModeErrorFromFields(message.error) ?? sandboxFailure());
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (message.type === "reply") {
|
|
70
|
+
const waiter = waiting.get(message.id);
|
|
71
|
+
if (!waiter)
|
|
72
|
+
return;
|
|
73
|
+
waiting.delete(message.id);
|
|
74
|
+
if ("irreversible" in message && message.irreversible)
|
|
75
|
+
irreversible.add(message.id);
|
|
76
|
+
if (message.ok)
|
|
77
|
+
waiter.resolve(message.json);
|
|
78
|
+
else if ("host" in message) {
|
|
79
|
+
// A host's own error: the carrier of the call this reply answers.
|
|
80
|
+
waiter.reject(new HostErrorCarrier(namespace, waiter.operation, message.id, message.host));
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
waiter.reject(codeModeErrorFromFields(message.error) ?? operationFailed(namespace, waiter.operation));
|
|
84
|
+
}
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (message.type !== "run" || started)
|
|
88
|
+
return;
|
|
89
|
+
started = true;
|
|
90
|
+
namespace = message.surface.namespace;
|
|
91
|
+
const cancel = new Int32Array(message.cancel);
|
|
92
|
+
newEngine(message.limits.memoryBytes).then((quickjs) => runVm({
|
|
93
|
+
quickjs,
|
|
94
|
+
code: message.code,
|
|
95
|
+
surface: message.surface,
|
|
96
|
+
limits: message.limits,
|
|
97
|
+
referenceKey: message.referenceKey,
|
|
98
|
+
isCancelled: () => Atomics.load(cancel, 0) === 1,
|
|
99
|
+
mustDeliver: (id) => irreversible.has(id),
|
|
100
|
+
...(message.asksAboutUnsent && {
|
|
101
|
+
unsent: (calls) => new Promise((resolve, reject) => {
|
|
102
|
+
decision = { resolve, reject };
|
|
103
|
+
port.post({ type: "unsent", calls });
|
|
104
|
+
}),
|
|
105
|
+
}),
|
|
106
|
+
// The call's own identifier pairs the reply with it.
|
|
107
|
+
call: (operation, args, id) => new Promise((resolve, reject) => {
|
|
108
|
+
waiting.set(id, { operation, resolve, reject });
|
|
109
|
+
port.post({ type: "call", id, operation, args });
|
|
110
|
+
}),
|
|
111
|
+
})).then((json) => port.post({ type: "done", ...(json !== undefined && { json }) }), (error) => port.post({
|
|
112
|
+
type: "fail",
|
|
113
|
+
error: (error instanceof CodeModeError ? error : sandboxFailure()).toJSON(),
|
|
114
|
+
}));
|
|
115
|
+
});
|
|
116
|
+
port.post({ type: "ready" });
|
|
117
|
+
}
|
|
118
|
+
let serving;
|
|
119
|
+
/**
|
|
120
|
+
* Serve the sandbox host from the worker this is called in, and tell whether
|
|
121
|
+
* it does: `true` in a worker, `false` anywhere else, where it does nothing.
|
|
122
|
+
*
|
|
123
|
+
* It takes the worker's message port for itself. Call it at the top of a
|
|
124
|
+
* worker module that does nothing else: the one a host names in the `worker`
|
|
125
|
+
* option of the sandbox. Calling it again changes nothing and gives the same
|
|
126
|
+
* answer.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* serveSandboxWorker();
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
export function serveSandboxWorker() {
|
|
134
|
+
return serving ??= connect().then((port) => {
|
|
135
|
+
if (!port)
|
|
136
|
+
return false;
|
|
137
|
+
serve(port);
|
|
138
|
+
return true;
|
|
139
|
+
});
|
|
140
|
+
}
|
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* CPU-bound program cannot block the host's event loop.
|
|
5
|
-
*
|
|
6
|
-
* @module code-mode/sandbox
|
|
7
|
-
*/
|
|
8
|
-
import { type SandboxLimits } from "./sandbox-core.js";
|
|
9
|
-
export { CODE_MODE_LIMITS, encodeValue, maxCodeLength, type SandboxLimits, } from "./sandbox-core.js";
|
|
1
|
+
import { type SandboxResourceLimits } from "./sandbox-core.js";
|
|
2
|
+
import { type SandboxSurface } from "./surface.js";
|
|
3
|
+
export { encodeValue, maxCodeLength, SANDBOX_RESOURCE_LIMITS, type SandboxResourceLimits, } from "./sandbox-core.js";
|
|
10
4
|
/**
|
|
11
5
|
* Where the guest runs. `worker` (default) keeps the host responsive during a
|
|
12
6
|
* CPU-bound program. `inline` runs it on the calling thread, for runtimes or
|
|
@@ -14,32 +8,82 @@ export { CODE_MODE_LIMITS, encodeValue, maxCodeLength, type SandboxLimits, } fro
|
|
|
14
8
|
* `cpuMs`.
|
|
15
9
|
*/
|
|
16
10
|
export type SandboxIsolation = "worker" | "inline";
|
|
17
|
-
/** Options of {@link
|
|
11
|
+
/** Options of {@link runSandboxProgram}. */
|
|
18
12
|
export interface SandboxOptions {
|
|
19
13
|
readonly isolation?: SandboxIsolation;
|
|
20
|
-
/**
|
|
21
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Override some of {@link SANDBOX_RESOURCE_LIMITS}. Each value must be a
|
|
16
|
+
* positive integer; `memoryBytes` must be at least 64 KiB and `stackBytes` at
|
|
17
|
+
* least 16 KiB. How many calls a program may make is not set here: those are
|
|
18
|
+
* the budgets of the surface.
|
|
19
|
+
*/
|
|
20
|
+
readonly limits?: Partial<SandboxResourceLimits>;
|
|
21
|
+
/**
|
|
22
|
+
* Marks a call whose effect cannot be undone, such as a write the user
|
|
23
|
+
* already approved. Decided on the host just before the call is sent, from
|
|
24
|
+
* the arguments the host receives. Two things follow.
|
|
25
|
+
*
|
|
26
|
+
* The wall limit does not cut such a call: the wall clock stops while it
|
|
27
|
+
* runs. A caller abort still ends the run.
|
|
28
|
+
*
|
|
29
|
+
* Once the host has completed it, a result that cannot be handed to the
|
|
30
|
+
* program (too large, not JSON, no memory left for it) ends the run with
|
|
31
|
+
* that failure. The program is not given an error it could catch and answer
|
|
32
|
+
* by calling again. A call the host itself rejects stays an error the
|
|
33
|
+
* program can catch: nothing was done.
|
|
34
|
+
*/
|
|
35
|
+
readonly irreversible?: (operation: string, args: unknown[]) => boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Decides about the calls a program made and did not wait for, when it
|
|
38
|
+
* finished normally. They were not sent, and never will be: once a program
|
|
39
|
+
* has finished, nothing more of it reaches the host. Return to let the
|
|
40
|
+
* program's value stand; throw to fail the run with what you throw. A
|
|
41
|
+
* promise is waited for, and its rejection counts as a throw. Without this
|
|
42
|
+
* option the run fails with `UNAWAITED_OPERATION`.
|
|
43
|
+
*
|
|
44
|
+
* The calls come in the order the program made them, each with the
|
|
45
|
+
* arguments the host would have received.
|
|
46
|
+
*/
|
|
47
|
+
readonly onUnsentCalls?: (calls: readonly SandboxUnsentCall[]) => void | Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* The worker module `worker` isolation starts, instead of Compose's own. For
|
|
50
|
+
* a host that bundles its worker: its entry calls `serveSandboxWorker` from
|
|
51
|
+
* `@mcpview/compose/code-mode/sandbox-worker`. A module that is missing fails
|
|
52
|
+
* the run; nothing falls back to `inline`. Naming one together with
|
|
53
|
+
* `isolation: "inline"` is refused with a RangeError.
|
|
54
|
+
*/
|
|
55
|
+
readonly worker?: URL;
|
|
56
|
+
}
|
|
57
|
+
/** A call a program made that never reached the host, as {@link SandboxOptions.onUnsentCalls} sees it. */
|
|
58
|
+
export interface SandboxUnsentCall {
|
|
59
|
+
readonly operation: string;
|
|
60
|
+
readonly args: unknown[];
|
|
22
61
|
}
|
|
23
62
|
/**
|
|
24
|
-
* Resolve and validate limits. Throws a RangeError for a
|
|
25
|
-
*
|
|
63
|
+
* Resolve and validate resource limits. Throws a RangeError for a name that is
|
|
64
|
+
* not a resource limit, or for a value out of range.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* const limits = resolveSandboxResourceLimits({ wallMs: 10_000 });
|
|
69
|
+
* ```
|
|
26
70
|
*/
|
|
27
|
-
export declare function
|
|
71
|
+
export declare function resolveSandboxResourceLimits(overrides?: Partial<SandboxResourceLimits>): SandboxResourceLimits;
|
|
28
72
|
/**
|
|
29
73
|
* Throw `SANDBOX_UNAVAILABLE` when the isolation cannot run in this runtime.
|
|
30
74
|
* Call it at configuration time to fail before the first program.
|
|
31
75
|
*
|
|
32
76
|
* This is a partial check. Under Deno it only verifies the permission for the
|
|
33
|
-
* worker module itself; the worker also needs read access to the
|
|
34
|
-
*
|
|
35
|
-
* the
|
|
77
|
+
* worker module itself; the worker also needs read access to the modules it
|
|
78
|
+
* imports, and a missing grant surfaces as `SANDBOX_FAILED` on the first run.
|
|
79
|
+
* `worker` is the module a host named in its options, when it did.
|
|
36
80
|
*
|
|
37
81
|
* @example
|
|
38
82
|
* ```ts
|
|
39
83
|
* assertSandboxIsolation("worker");
|
|
40
84
|
* ```
|
|
41
85
|
*/
|
|
42
|
-
export declare function assertSandboxIsolation(isolation: SandboxIsolation): void;
|
|
86
|
+
export declare function assertSandboxIsolation(isolation: SandboxIsolation, worker?: URL): void;
|
|
43
87
|
/** Wait for `work`, but give up as soon as `signal` aborts. A late rejection of `work` is observed. */
|
|
44
88
|
export declare function untilAborted<T>(work: Promise<T>, signal: AbortSignal): Promise<T>;
|
|
45
89
|
/** Wait for host I/O without allowing a provider that ignores cancellation to hold the VM open. */
|
|
@@ -47,14 +91,85 @@ export declare function waitForOperation(invoke: () => Promise<unknown>, signal:
|
|
|
47
91
|
/** True once the caller aborted or the wall deadline passed, even if its timer could not run. */
|
|
48
92
|
stopped?: () => boolean): Promise<unknown>;
|
|
49
93
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
94
|
+
* The host side of an operation. `signal` aborts when the caller aborts or the
|
|
95
|
+
* wall deadline passes; `stopped` is true from then on, even if the timer
|
|
96
|
+
* could not run: check it right before any effect. `call` identifies this call
|
|
97
|
+
* within the run: a {@link SandboxResultReference} to its result carries the
|
|
98
|
+
* same number.
|
|
99
|
+
*/
|
|
100
|
+
export type SandboxInvoke = (operation: string, args: unknown[], signal: AbortSignal, stopped: () => boolean, call: number) => Promise<unknown>;
|
|
101
|
+
/**
|
|
102
|
+
* Run a function body in an isolated QuickJS context, against the surface the
|
|
103
|
+
* host declares. Only JSON and the declared operations cross into the host.
|
|
104
|
+
* Host operations run one at a time, in the order the program started them: an
|
|
105
|
+
* operation starts when the program awaits its call, or as soon as it is
|
|
106
|
+
* called if it is declared `start: "on-call"`. Their time does not count as
|
|
107
|
+
* guest CPU.
|
|
108
|
+
*
|
|
109
|
+
* Once the program has finished, none of its code runs and no call it left
|
|
110
|
+
* waiting is sent. If it finished normally with such calls, the host decides
|
|
111
|
+
* through `onUnsentCalls`; without it the run fails with `UNAWAITED_OPERATION`.
|
|
112
|
+
* Only a call of an `"on-call"` operation, which the program did not have to
|
|
113
|
+
* wait for, is still delivered.
|
|
114
|
+
*
|
|
115
|
+
* The promise rejects with:
|
|
116
|
+
* - a RangeError, before anything runs, for what the host itself got wrong: an
|
|
117
|
+
* option, a limit, the surface, or a constant that is not JSON;
|
|
118
|
+
* - the caller signal's reason, when the caller aborts;
|
|
119
|
+
* - the host's own {@link SandboxHostError}, when an operation rejected with
|
|
120
|
+
* one and the program did not handle it;
|
|
121
|
+
* - a {@link CodeModeError} in every other case.
|
|
122
|
+
*
|
|
123
|
+
* An operation that rejects with a `CodeModeError` or a `SandboxHostError`
|
|
124
|
+
* shows it to the program. Any other rejection reaches the program as
|
|
125
|
+
* `OPERATION_FAILED`, without its message.
|
|
126
|
+
*
|
|
127
|
+
* The promise resolves with the JSON value the program returned, and with
|
|
128
|
+
* `undefined` when it returned nothing: a forgotten `return` is not a `null`.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```ts
|
|
132
|
+
* const total = await runSandboxProgram(
|
|
133
|
+
* "const order = await shop.getOrder(shop.params.id); return order.total;",
|
|
134
|
+
* {
|
|
135
|
+
* namespace: "shop",
|
|
136
|
+
* constants: { params: { id: 1042 } },
|
|
137
|
+
* operations: { getOrder: { budget: "reads" } },
|
|
138
|
+
* budgets: { reads: 12 },
|
|
139
|
+
* },
|
|
140
|
+
* (operation, args) => myHost(operation, args),
|
|
141
|
+
* );
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export declare function runSandboxProgram(code: string, surface: SandboxSurface, invoke: SandboxInvoke, callerSignal?: AbortSignal, options?: SandboxOptions): Promise<unknown>;
|
|
145
|
+
/** Limits of a program of Compose's chain: the resource limits and its two call budgets. */
|
|
146
|
+
export interface SandboxLimits extends SandboxResourceLimits {
|
|
147
|
+
/** `listSnippets` and `runSnippet` calls: upstream, possibly side-effecting work. */
|
|
148
|
+
readonly providerCalls: number;
|
|
149
|
+
/** `panel` and `sync` calls: board choices kept in host memory. */
|
|
150
|
+
readonly boardOperations: number;
|
|
151
|
+
}
|
|
152
|
+
/** Default limits of a program of Compose's chain. */
|
|
153
|
+
export declare const CODE_MODE_LIMITS: SandboxLimits;
|
|
154
|
+
/**
|
|
155
|
+
* Resolve and validate the limits of Compose's chain. Throws a RangeError for
|
|
156
|
+
* a value that is not a positive integer, or out of range.
|
|
53
157
|
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* const limits = resolveSandboxLimits({ providerCalls: 4 });
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
export declare function resolveSandboxLimits(overrides?: Partial<SandboxLimits>): SandboxLimits;
|
|
164
|
+
/** Options of {@link runProgram}. */
|
|
165
|
+
export interface SnippetProgramOptions {
|
|
166
|
+
readonly isolation?: SandboxIsolation;
|
|
167
|
+
/** Override some of {@link CODE_MODE_LIMITS}. */
|
|
168
|
+
readonly limits?: Partial<SandboxLimits>;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Run a program against Compose's own surface: `api.params` and the four
|
|
172
|
+
* snippet operations of ADR 0006. See {@link runSandboxProgram} for the rules.
|
|
58
173
|
*
|
|
59
174
|
* @example
|
|
60
175
|
* ```ts
|
|
@@ -65,7 +180,5 @@ stopped?: () => boolean): Promise<unknown>;
|
|
|
65
180
|
* );
|
|
66
181
|
* ```
|
|
67
182
|
*/
|
|
68
|
-
export declare function runProgram(code: string, params: Record<string, unknown>, invoke:
|
|
69
|
-
/** True once the caller aborted or the wall deadline passed: check it right before any effect. */
|
|
70
|
-
stopped: () => boolean) => Promise<unknown>, callerSignal?: AbortSignal, options?: SandboxOptions): Promise<unknown>;
|
|
183
|
+
export declare function runProgram(code: string, params: Record<string, unknown>, invoke: SandboxInvoke, callerSignal?: AbortSignal, options?: SnippetProgramOptions): Promise<unknown>;
|
|
71
184
|
//# sourceMappingURL=sandbox.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sandbox.d.ts","sourceRoot":"","sources":["../../../src/src/code-mode/sandbox.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"sandbox.d.ts","sourceRoot":"","sources":["../../../src/src/code-mode/sandbox.ts"],"names":[],"mappings":"AAYA,OAAO,EAUL,KAAK,qBAAqB,EAG3B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAIL,KAAK,cAAc,EAEpB,MAAM,cAAc,CAAC;AAGtB,OAAO,EACL,WAAW,EACX,aAAa,EACb,uBAAuB,EACvB,KAAK,qBAAqB,GAC3B,MAAM,mBAAmB,CAAC;AAE3B;;;;;GAKG;AACH,MAAM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