@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,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a host exposes to a Code Mode program: the object the program receives,
|
|
3
|
+
* the constants set on it and its closed table of operations. The sandbox
|
|
4
|
+
* builds that object from this declaration and nothing else (ADR 0007).
|
|
5
|
+
*
|
|
6
|
+
* @module code-mode/surface
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* One operation a program can call on the surface object.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* // Awaited by the program, arguments copied: the defaults.
|
|
14
|
+
* const getOrder: SandboxOperation = { budget: "reads" };
|
|
15
|
+
* // Runs as soon as it is called, and designates earlier results themselves.
|
|
16
|
+
* const show: SandboxOperation = {
|
|
17
|
+
* budget: "displays",
|
|
18
|
+
* start: "on-call",
|
|
19
|
+
* resultArguments: "reference",
|
|
20
|
+
* };
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export interface SandboxOperation {
|
|
24
|
+
/** The budget this operation counts against: a key of {@link SandboxSurface.budgets}. */
|
|
25
|
+
readonly budget: string;
|
|
26
|
+
/**
|
|
27
|
+
* When a call reaches the host. `"on-await"` (default): when the program
|
|
28
|
+
* awaits it or attaches a handler, and a call it never awaits never runs.
|
|
29
|
+
* `"on-call"`: as soon as the program calls it; if it fails, the program
|
|
30
|
+
* ends before any later call is sent.
|
|
31
|
+
*/
|
|
32
|
+
readonly start?: "on-await" | "on-call";
|
|
33
|
+
/**
|
|
34
|
+
* How an earlier result passed as an argument crosses. `"copy"` (default):
|
|
35
|
+
* as a copy of its data, like any JSON value. `"reference"`: as a reference
|
|
36
|
+
* to the call that produced it, so the host finds the native result it kept.
|
|
37
|
+
*/
|
|
38
|
+
readonly resultArguments?: "copy" | "reference";
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The surface of a sandbox: only JSON crosses it, and an operation the table
|
|
42
|
+
* does not declare does not exist in the program.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* const surface: SandboxSurface = {
|
|
47
|
+
* namespace: "shop",
|
|
48
|
+
* constants: { params: { month: "2026-11" } },
|
|
49
|
+
* operations: {
|
|
50
|
+
* listOrders: { budget: "reads" },
|
|
51
|
+
* getOrder: { budget: "reads" },
|
|
52
|
+
* },
|
|
53
|
+
* budgets: { reads: 16 },
|
|
54
|
+
* };
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export interface SandboxSurface {
|
|
58
|
+
/** Name of the object the program receives, such as `shop`. */
|
|
59
|
+
readonly namespace: string;
|
|
60
|
+
/**
|
|
61
|
+
* JSON values set on that object, such as `params` or a catalog. Each run
|
|
62
|
+
* gives the program its own copy: the object is frozen, so a constant cannot
|
|
63
|
+
* be replaced, but the program may change what is inside its copy.
|
|
64
|
+
*/
|
|
65
|
+
readonly constants?: Readonly<Record<string, unknown>>;
|
|
66
|
+
/** The closed table of host operations, keyed by the name the program calls. */
|
|
67
|
+
readonly operations: Readonly<Record<string, SandboxOperation>>;
|
|
68
|
+
/** Calls a program may make per budget; every budget an operation names must be listed. */
|
|
69
|
+
readonly budgets: Readonly<Record<string, number>>;
|
|
70
|
+
/**
|
|
71
|
+
* What the run yields when the program returns an earlier result itself.
|
|
72
|
+
* `"copy"` (default): a copy of its data, like any returned value.
|
|
73
|
+
* `"reference"`: a {@link SandboxResultReference} to the call that produced
|
|
74
|
+
* it. A value that merely contains a result is always data.
|
|
75
|
+
*/
|
|
76
|
+
readonly returnedResult?: "copy" | "reference";
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Stands for an earlier result of the same run, where an operation or the
|
|
80
|
+
* surface asked for references. `call` is the identifier the host received
|
|
81
|
+
* with the call that produced the result, so the host finds what it kept.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* const kept = new Map<number, unknown>();
|
|
86
|
+
* function designated(argument: unknown): unknown {
|
|
87
|
+
* return argument instanceof SandboxResultReference ? kept.get(argument.call) : undefined;
|
|
88
|
+
* }
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export declare class SandboxResultReference {
|
|
92
|
+
/** The identifier of the call that produced the result. */
|
|
93
|
+
readonly call: number;
|
|
94
|
+
constructor(call: number);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* An error in the host's own vocabulary, for the program to read. A host
|
|
98
|
+
* operation rejects with it to say what happened in terms the program can act
|
|
99
|
+
* on. The program catches an error whose `code`, `message` and `recovery` are
|
|
100
|
+
* the host's, unchanged; its `name` is `CodeModeError` and its `context` is
|
|
101
|
+
* empty. Any other rejection reaches the program as `OPERATION_FAILED`, without
|
|
102
|
+
* its text. If the program does not handle it, the run rejects with this very
|
|
103
|
+
* object.
|
|
104
|
+
*
|
|
105
|
+
* The code is the host's own. The few codes the sandbox itself throws into a
|
|
106
|
+
* program (`ARGUMENT_INVALID`, `OPERATION_LIMIT`, `OPERATION_FAILED`,
|
|
107
|
+
* `RECEIPT_TOO_LARGE`, `RESOURCE_LIMIT`, `INTERRUPTED`) are refused here, so a `catch` can tell
|
|
108
|
+
* the two apart. To report one of those, an operation rejects with a
|
|
109
|
+
* `CodeModeError`.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* throw new SandboxHostError(
|
|
114
|
+
* "ORDER_CHANGED",
|
|
115
|
+
* "Order 1042 changed since it was read",
|
|
116
|
+
* "Read it again, then decide",
|
|
117
|
+
* );
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export declare class SandboxHostError extends Error {
|
|
121
|
+
/** Upper-case letters, digits and underscores, starting with a letter: at most 64 characters. */
|
|
122
|
+
readonly code: string;
|
|
123
|
+
/** What the program or the agent can do next. */
|
|
124
|
+
readonly recovery: string;
|
|
125
|
+
constructor(code: string, message: string, recovery: string, options?: ErrorOptions);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Validate a surface and return a frozen copy of its declaration: its name,
|
|
129
|
+
* its operations, its budgets and the list of its constants. Throws a
|
|
130
|
+
* RangeError for a surface a program could not use as declared: this is a host
|
|
131
|
+
* configuration error, raised before any program runs.
|
|
132
|
+
*
|
|
133
|
+
* The values of the constants are not copied here. Each run copies them to
|
|
134
|
+
* JSON when it starts, and refuses one that is not JSON within the value limit.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* const surface = resolveSandboxSurface({
|
|
139
|
+
* namespace: "shop",
|
|
140
|
+
* operations: { getOrder: { budget: "reads" } },
|
|
141
|
+
* budgets: { reads: 12 },
|
|
142
|
+
* });
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
export declare function resolveSandboxSurface(surface: SandboxSurface): SandboxSurface;
|
|
146
|
+
/** True when a run of this surface has to recognise earlier results. */
|
|
147
|
+
export declare function surfaceUsesReferences(surface: SandboxSurface): boolean;
|
|
148
|
+
//# sourceMappingURL=surface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"surface.d.ts","sourceRoot":"","sources":["../../../src/src/code-mode/surface.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,gBAAgB;IAC/B,yFAAyF;IACzF,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,UAAU,GAAG,SAAS,CAAC;IACxC;;;;OAIG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,GAAG,WAAW,CAAC;CACjD;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,WAAW,cAAc;IAC7B,+DAA+D;IAC/D,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACvD,gFAAgF;IAChF,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC,CAAC;IAChE,2FAA2F;IAC3F,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACnD;;;;;OAKG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,GAAG,WAAW,CAAC;CAChD;AAED;;;;;;;;;;;;GAYG;AACH,qBAAa,sBAAsB;IACjC,2DAA2D;IAC3D,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;gBAEV,IAAI,EAAE,MAAM;CAGzB;AAoBD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,iGAAiG;IACjG,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,iDAAiD;IACjD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;gBAEd,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAmBpF;AA+BD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,cAAc,GAAG,cAAc,CAuF7E;AAED,wEAAwE;AACxE,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAKtE"}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a host exposes to a Code Mode program: the object the program receives,
|
|
3
|
+
* the constants set on it and its closed table of operations. The sandbox
|
|
4
|
+
* builds that object from this declaration and nothing else (ADR 0007).
|
|
5
|
+
*
|
|
6
|
+
* @module code-mode/surface
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Stands for an earlier result of the same run, where an operation or the
|
|
10
|
+
* surface asked for references. `call` is the identifier the host received
|
|
11
|
+
* with the call that produced the result, so the host finds what it kept.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const kept = new Map<number, unknown>();
|
|
16
|
+
* function designated(argument: unknown): unknown {
|
|
17
|
+
* return argument instanceof SandboxResultReference ? kept.get(argument.call) : undefined;
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export class SandboxResultReference {
|
|
22
|
+
/** The identifier of the call that produced the result. */
|
|
23
|
+
call;
|
|
24
|
+
constructor(call) {
|
|
25
|
+
this.call = call;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const HOST_ERROR_CODE = /^[A-Z][A-Z0-9_]{0,63}$/;
|
|
29
|
+
/**
|
|
30
|
+
* The codes the sandbox itself throws into a program. A host error never takes
|
|
31
|
+
* one of them, so a `catch` can tell the host's refusal from the sandbox's.
|
|
32
|
+
* Codes a program never catches from the sandbox (a time limit ends the run)
|
|
33
|
+
* stay free for the host, and so do the codes of Compose's own chain.
|
|
34
|
+
*/
|
|
35
|
+
const PROGRAM_VISIBLE_SANDBOX_CODES = new Set([
|
|
36
|
+
"ARGUMENT_INVALID",
|
|
37
|
+
"OPERATION_LIMIT",
|
|
38
|
+
"OPERATION_FAILED",
|
|
39
|
+
"RECEIPT_TOO_LARGE",
|
|
40
|
+
// A result the program has no memory left to hold fails the call it awaited.
|
|
41
|
+
"RESOURCE_LIMIT",
|
|
42
|
+
"INTERRUPTED",
|
|
43
|
+
]);
|
|
44
|
+
/**
|
|
45
|
+
* An error in the host's own vocabulary, for the program to read. A host
|
|
46
|
+
* operation rejects with it to say what happened in terms the program can act
|
|
47
|
+
* on. The program catches an error whose `code`, `message` and `recovery` are
|
|
48
|
+
* the host's, unchanged; its `name` is `CodeModeError` and its `context` is
|
|
49
|
+
* empty. Any other rejection reaches the program as `OPERATION_FAILED`, without
|
|
50
|
+
* its text. If the program does not handle it, the run rejects with this very
|
|
51
|
+
* object.
|
|
52
|
+
*
|
|
53
|
+
* The code is the host's own. The few codes the sandbox itself throws into a
|
|
54
|
+
* program (`ARGUMENT_INVALID`, `OPERATION_LIMIT`, `OPERATION_FAILED`,
|
|
55
|
+
* `RECEIPT_TOO_LARGE`, `RESOURCE_LIMIT`, `INTERRUPTED`) are refused here, so a `catch` can tell
|
|
56
|
+
* the two apart. To report one of those, an operation rejects with a
|
|
57
|
+
* `CodeModeError`.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* throw new SandboxHostError(
|
|
62
|
+
* "ORDER_CHANGED",
|
|
63
|
+
* "Order 1042 changed since it was read",
|
|
64
|
+
* "Read it again, then decide",
|
|
65
|
+
* );
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export class SandboxHostError extends Error {
|
|
69
|
+
/** Upper-case letters, digits and underscores, starting with a letter: at most 64 characters. */
|
|
70
|
+
code;
|
|
71
|
+
/** What the program or the agent can do next. */
|
|
72
|
+
recovery;
|
|
73
|
+
constructor(code, message, recovery, options) {
|
|
74
|
+
if (typeof code !== "string" || !HOST_ERROR_CODE.test(code)) {
|
|
75
|
+
throw new RangeError("A sandbox host error code is 1 to 64 upper-case letters, digits or underscores, starting with a letter.");
|
|
76
|
+
}
|
|
77
|
+
if (PROGRAM_VISIBLE_SANDBOX_CODES.has(code)) {
|
|
78
|
+
throw new RangeError(`${code} is a code the sandbox itself throws into a program: reject with a CodeModeError to report it.`);
|
|
79
|
+
}
|
|
80
|
+
if (typeof message !== "string" || typeof recovery !== "string") {
|
|
81
|
+
throw new RangeError("A sandbox host error needs a message and a recovery, both strings.");
|
|
82
|
+
}
|
|
83
|
+
super(message, options);
|
|
84
|
+
this.name = "SandboxHostError";
|
|
85
|
+
this.code = code;
|
|
86
|
+
this.recovery = recovery;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** Names a strict async function cannot bind as a parameter. */
|
|
90
|
+
const RESERVED_NAMES = new Set([
|
|
91
|
+
...["arguments", "await", "break", "case", "catch", "class", "const", "continue", "debugger"],
|
|
92
|
+
...["default", "delete", "do", "else", "enum", "eval", "export", "extends", "false", "finally"],
|
|
93
|
+
...["for", "function", "if", "implements", "import", "in", "instanceof", "interface", "let"],
|
|
94
|
+
...["new", "null", "package", "private", "protected", "public", "return", "static", "super"],
|
|
95
|
+
...["switch", "this", "throw", "true", "try", "typeof", "var", "void", "while", "with", "yield"],
|
|
96
|
+
]);
|
|
97
|
+
/** `__proto__` would replace the object's prototype, and `then` would make it a thenable. */
|
|
98
|
+
const FORBIDDEN_MEMBERS = new Set(["__proto__", "then"]);
|
|
99
|
+
const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
100
|
+
const START_VALUES = new Set(["on-await", "on-call"]);
|
|
101
|
+
const RESULT_ARGUMENT_VALUES = new Set(["copy", "reference"]);
|
|
102
|
+
function isRecord(value) {
|
|
103
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
104
|
+
}
|
|
105
|
+
function assertMember(namespace, name, kind) {
|
|
106
|
+
if (!IDENTIFIER.test(name) || FORBIDDEN_MEMBERS.has(name)) {
|
|
107
|
+
throw new RangeError(`Sandbox ${kind} ${namespace}.${name} must be a plain property name other than __proto__ and then.`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Validate a surface and return a frozen copy of its declaration: its name,
|
|
112
|
+
* its operations, its budgets and the list of its constants. Throws a
|
|
113
|
+
* RangeError for a surface a program could not use as declared: this is a host
|
|
114
|
+
* configuration error, raised before any program runs.
|
|
115
|
+
*
|
|
116
|
+
* The values of the constants are not copied here. Each run copies them to
|
|
117
|
+
* JSON when it starts, and refuses one that is not JSON within the value limit.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* const surface = resolveSandboxSurface({
|
|
122
|
+
* namespace: "shop",
|
|
123
|
+
* operations: { getOrder: { budget: "reads" } },
|
|
124
|
+
* budgets: { reads: 12 },
|
|
125
|
+
* });
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
export function resolveSandboxSurface(surface) {
|
|
129
|
+
if (!isRecord(surface))
|
|
130
|
+
throw new RangeError("A sandbox surface must be an object.");
|
|
131
|
+
const { namespace } = surface;
|
|
132
|
+
if (typeof namespace !== "string" || !IDENTIFIER.test(namespace) || RESERVED_NAMES.has(namespace)) {
|
|
133
|
+
throw new RangeError("Sandbox surface namespace must be an identifier that is not a reserved word.");
|
|
134
|
+
}
|
|
135
|
+
if (!isRecord(surface.budgets)) {
|
|
136
|
+
throw new RangeError("Sandbox surface budgets must be an object.");
|
|
137
|
+
}
|
|
138
|
+
const budgets = {};
|
|
139
|
+
for (const [name, limit] of Object.entries(surface.budgets)) {
|
|
140
|
+
// `budgets.__proto__ = limit` would set nothing and report the budget as unlisted.
|
|
141
|
+
// A budget is a key of this table, never a member of the object a program gets.
|
|
142
|
+
if (!IDENTIFIER.test(name) || name === "__proto__") {
|
|
143
|
+
throw new RangeError(`Sandbox budget ${name} must be a plain name other than __proto__.`);
|
|
144
|
+
}
|
|
145
|
+
if (!Number.isSafeInteger(limit) || limit <= 0) {
|
|
146
|
+
throw new RangeError(`Sandbox budget ${name} must be a positive integer.`);
|
|
147
|
+
}
|
|
148
|
+
budgets[name] = limit;
|
|
149
|
+
}
|
|
150
|
+
if (!isRecord(surface.operations)) {
|
|
151
|
+
throw new RangeError("Sandbox surface operations must be an object.");
|
|
152
|
+
}
|
|
153
|
+
const operations = {};
|
|
154
|
+
for (const [name, operation] of Object.entries(surface.operations)) {
|
|
155
|
+
assertMember(namespace, name, "operation");
|
|
156
|
+
if (!isRecord(operation)) {
|
|
157
|
+
throw new RangeError(`Sandbox operation ${namespace}.${name} must name its budget.`);
|
|
158
|
+
}
|
|
159
|
+
const { budget, start, resultArguments } = operation;
|
|
160
|
+
if (typeof budget !== "string") {
|
|
161
|
+
throw new RangeError(`Sandbox operation ${namespace}.${name} must name its budget.`);
|
|
162
|
+
}
|
|
163
|
+
if (!Object.hasOwn(budgets, budget)) {
|
|
164
|
+
throw new RangeError(`Sandbox operation ${namespace}.${name} names the budget ${budget}, which the surface does not list.`);
|
|
165
|
+
}
|
|
166
|
+
if (start !== undefined && !START_VALUES.has(start)) {
|
|
167
|
+
throw new RangeError(`Sandbox operation ${namespace}.${name}: start must be "on-await" or "on-call".`);
|
|
168
|
+
}
|
|
169
|
+
if (resultArguments !== undefined && !RESULT_ARGUMENT_VALUES.has(resultArguments)) {
|
|
170
|
+
throw new RangeError(`Sandbox operation ${namespace}.${name}: resultArguments must be "copy" or "reference".`);
|
|
171
|
+
}
|
|
172
|
+
operations[name] = Object.freeze({
|
|
173
|
+
budget,
|
|
174
|
+
...(start !== undefined && { start: start }),
|
|
175
|
+
...(resultArguments !== undefined &&
|
|
176
|
+
{ resultArguments: resultArguments }),
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
const { returnedResult } = surface;
|
|
180
|
+
if (returnedResult !== undefined && !RESULT_ARGUMENT_VALUES.has(returnedResult)) {
|
|
181
|
+
throw new RangeError('Sandbox surface returnedResult must be "copy" or "reference".');
|
|
182
|
+
}
|
|
183
|
+
const resolved = { namespace, operations: Object.freeze(operations), budgets: Object.freeze(budgets) };
|
|
184
|
+
if (returnedResult !== undefined)
|
|
185
|
+
resolved.returnedResult = returnedResult;
|
|
186
|
+
if (surface.constants !== undefined) {
|
|
187
|
+
if (!isRecord(surface.constants)) {
|
|
188
|
+
throw new RangeError("Sandbox surface constants must be an object.");
|
|
189
|
+
}
|
|
190
|
+
for (const name of Object.keys(surface.constants)) {
|
|
191
|
+
assertMember(namespace, name, "constant");
|
|
192
|
+
if (Object.hasOwn(operations, name)) {
|
|
193
|
+
throw new RangeError(`Sandbox constant ${namespace}.${name} has the name of an operation.`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
resolved.constants = Object.freeze({ ...surface.constants });
|
|
197
|
+
}
|
|
198
|
+
return Object.freeze(resolved);
|
|
199
|
+
}
|
|
200
|
+
/** True when a run of this surface has to recognise earlier results. */
|
|
201
|
+
export function surfaceUsesReferences(surface) {
|
|
202
|
+
return surface.returnedResult === "reference" ||
|
|
203
|
+
Object.values(surface.operations).some((operation) => operation.resultArguments === "reference");
|
|
204
|
+
}
|
|
@@ -19,7 +19,7 @@ export function createSnippetExecuteTool(options) {
|
|
|
19
19
|
"Configured provider aliases: " + JSON.stringify(available) + ".",
|
|
20
20
|
"await api.runSnippet(provider, name, input) returns a receipt {id, value}.",
|
|
21
21
|
"Pass values between calls. Use await api.panel(receipt.id, optionalTitle) for each result to display, in display order. Return a JSON summary.",
|
|
22
|
-
"Await every api call (or pass them to Promise.all): a call
|
|
22
|
+
"Await every api call (or pass them to Promise.all): a call the program has not waited for when it ends is not sent.",
|
|
23
23
|
"A failed api call throws an Error with code, recovery and context; tool errors read [execute] CODE: detail. Recovery: hint.",
|
|
24
24
|
...(eventPorts.length
|
|
25
25
|
? [
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { CodeModeError } from "./errors.js";
|
|
2
|
-
import { type
|
|
2
|
+
import { type EncodedSurface, type SandboxResourceLimits, type UnsentCall } from "./sandbox-core.js";
|
|
3
3
|
import type { HostMessage } from "./worker-protocol.js";
|
|
4
4
|
/**
|
|
5
5
|
* Why worker isolation cannot run here, or undefined when it can. `scope` is the
|
|
6
6
|
* global object to inspect.
|
|
7
7
|
*/
|
|
8
|
-
export declare function workerIsolationUnavailable(scope?: object): string | undefined;
|
|
8
|
+
export declare function workerIsolationUnavailable(scope?: object, worker?: URL): string | undefined;
|
|
9
9
|
/** The part of `process` that tells whether Node's permission model allows workers. */
|
|
10
10
|
export interface NodeProcessLike {
|
|
11
11
|
readonly permission?: {
|
|
@@ -20,6 +20,18 @@ export interface NodeProcessLike {
|
|
|
20
20
|
* does not count.
|
|
21
21
|
*/
|
|
22
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;
|
|
23
35
|
/** The part of the host's Node options that a Code Mode worker can start with. */
|
|
24
36
|
export declare function nodeWorkerExecArgv(execArgv: readonly string[]): string[];
|
|
25
37
|
/**
|
|
@@ -41,30 +53,58 @@ export declare function nodeWorkerEnv(env: Readonly<Record<string, string | unde
|
|
|
41
53
|
* posted, so an oversized result never crosses the worker boundary (no parse and
|
|
42
54
|
* no structured clone). The guest decodes the text.
|
|
43
55
|
*/
|
|
44
|
-
export declare function encodeReply(id: number, operation: string, value: unknown, valueLength: number
|
|
56
|
+
export declare function encodeReply(id: number, operation: string, value: unknown, valueLength: number, namespace: string,
|
|
57
|
+
/** True when the host completed the call and its effect cannot be undone. */
|
|
58
|
+
irreversible?: boolean): HostMessage;
|
|
45
59
|
/** Inputs of {@link runInWorker}. */
|
|
46
60
|
export interface WorkerRunInput {
|
|
47
61
|
readonly code: string;
|
|
48
|
-
|
|
49
|
-
readonly
|
|
50
|
-
|
|
51
|
-
|
|
62
|
+
/** What the program receives. Resolved by the caller, each constant as the JSON text of its value. */
|
|
63
|
+
readonly surface: EncodedSurface;
|
|
64
|
+
readonly limits: SandboxResourceLimits;
|
|
65
|
+
/**
|
|
66
|
+
* Host operation for the guest. `args` is the JSON text of the list of
|
|
67
|
+
* arguments and `id` identifies the call. Rejections should be {@link CodeModeError}s.
|
|
68
|
+
*/
|
|
69
|
+
call(operation: string, args: string, id: number): Promise<unknown>;
|
|
70
|
+
/** The key of the record that stands for an earlier result, when the surface uses references. */
|
|
71
|
+
readonly referenceKey?: string;
|
|
72
|
+
/** The worker module to start instead of Compose's own, for a host that bundles its worker. */
|
|
73
|
+
readonly worker?: URL;
|
|
74
|
+
/** Called once the worker reported: from then on there is a guest to run the program. */
|
|
75
|
+
started?(): void;
|
|
76
|
+
/** True when the host completed call `id` and its effect cannot be undone. */
|
|
77
|
+
mustDeliver?(id: number): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Decide about the calls a program that finished normally did not wait for.
|
|
80
|
+
* Reject with a {@link CodeModeError} to fail the run.
|
|
81
|
+
*/
|
|
82
|
+
unsent?(calls: UnsentCall[]): Promise<void>;
|
|
52
83
|
/** Aborting stops the guest at its next interrupt check. */
|
|
53
84
|
readonly signal: AbortSignal;
|
|
54
85
|
}
|
|
55
86
|
/** The error for a worker that could not be created at all. */
|
|
56
87
|
export declare function workerSpawnFailure(error: unknown): CodeModeError;
|
|
57
88
|
/**
|
|
58
|
-
*
|
|
89
|
+
* The error for a worker that loaded but never reported: it stayed silent for
|
|
90
|
+
* the wall time of the program (`wallMs`), or its module ran to its end and
|
|
91
|
+
* the thread exited. No program ran, so this is no time limit of the program.
|
|
92
|
+
*/
|
|
93
|
+
export declare function workerNotStarted(wallMs?: number): CodeModeError;
|
|
94
|
+
/**
|
|
95
|
+
* Run one program in a fresh worker and settle once: with the JSON text of
|
|
96
|
+
* what the program returned (undefined when it returned nothing), or with a
|
|
59
97
|
* {@link CodeModeError}. The worker is always terminated afterwards.
|
|
60
98
|
*
|
|
61
99
|
* @example
|
|
62
100
|
* ```ts
|
|
63
|
-
* const
|
|
64
|
-
* code: "return 1 + 1;",
|
|
101
|
+
* const json = await runInWorker({
|
|
102
|
+
* code: "return 1 + 1;",
|
|
103
|
+
* surface: { namespace: "api", operations: {}, budgets: {} },
|
|
104
|
+
* limits: SANDBOX_RESOURCE_LIMITS,
|
|
65
105
|
* call: () => Promise.reject(new Error("unused")), signal: new AbortController().signal,
|
|
66
106
|
* });
|
|
67
107
|
* ```
|
|
68
108
|
*/
|
|
69
|
-
export declare function runInWorker(input: WorkerRunInput): Promise<
|
|
109
|
+
export declare function runInWorker(input: WorkerRunInput): Promise<string | undefined>;
|
|
70
110
|
//# sourceMappingURL=worker-host.d.ts.map
|
|
@@ -1 +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,
|
|
1
|
+
{"version":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