@mcpview/compose 0.14.1 → 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.
Files changed (63) hide show
  1. package/CHANGELOG.md +101 -1
  2. package/README.md +113 -22
  3. package/esm/src/code-mode/engine.d.ts +59 -0
  4. package/esm/src/code-mode/engine.d.ts.map +1 -0
  5. package/esm/src/code-mode/engine.js +189 -0
  6. package/esm/src/code-mode/guest-worker.d.ts +2 -5
  7. package/esm/src/code-mode/guest-worker.d.ts.map +1 -1
  8. package/esm/src/code-mode/guest-worker.js +4 -75
  9. package/esm/src/code-mode/sandbox-api.d.ts +37 -0
  10. package/esm/src/code-mode/sandbox-api.d.ts.map +1 -0
  11. package/esm/src/code-mode/sandbox-api.js +36 -0
  12. package/esm/src/code-mode/sandbox-core.d.ts +99 -21
  13. package/esm/src/code-mode/sandbox-core.d.ts.map +1 -1
  14. package/esm/src/code-mode/sandbox-core.js +926 -221
  15. package/esm/src/code-mode/sandbox-worker.d.ts +37 -0
  16. package/esm/src/code-mode/sandbox-worker.d.ts.map +1 -0
  17. package/esm/src/code-mode/sandbox-worker.js +140 -0
  18. package/esm/src/code-mode/sandbox.d.ts +142 -29
  19. package/esm/src/code-mode/sandbox.d.ts.map +1 -1
  20. package/esm/src/code-mode/sandbox.js +399 -67
  21. package/esm/src/code-mode/surface.d.ts +148 -0
  22. package/esm/src/code-mode/surface.d.ts.map +1 -0
  23. package/esm/src/code-mode/surface.js +204 -0
  24. package/esm/src/code-mode/tool.js +1 -1
  25. package/esm/src/code-mode/worker-host.d.ts +39 -11
  26. package/esm/src/code-mode/worker-host.d.ts.map +1 -1
  27. package/esm/src/code-mode/worker-host.js +100 -43
  28. package/esm/src/code-mode/worker-protocol.d.ts +45 -8
  29. package/esm/src/code-mode/worker-protocol.d.ts.map +1 -1
  30. package/esm/src/code-mode/worker-protocol.js +4 -2
  31. package/esm/src/version.d.ts +1 -1
  32. package/esm/src/version.js +1 -1
  33. package/package.json +9 -1
  34. package/script/src/code-mode/engine.d.ts +59 -0
  35. package/script/src/code-mode/engine.d.ts.map +1 -0
  36. package/script/src/code-mode/engine.js +198 -0
  37. package/script/src/code-mode/guest-worker.d.ts +2 -5
  38. package/script/src/code-mode/guest-worker.d.ts.map +1 -1
  39. package/script/src/code-mode/guest-worker.js +4 -98
  40. package/script/src/code-mode/sandbox-api.d.ts +37 -0
  41. package/script/src/code-mode/sandbox-api.d.ts.map +1 -0
  42. package/script/src/code-mode/sandbox-api.js +50 -0
  43. package/script/src/code-mode/sandbox-core.d.ts +99 -21
  44. package/script/src/code-mode/sandbox-core.d.ts.map +1 -1
  45. package/script/src/code-mode/sandbox-core.js +931 -221
  46. package/script/src/code-mode/sandbox-worker.d.ts +37 -0
  47. package/script/src/code-mode/sandbox-worker.d.ts.map +1 -0
  48. package/script/src/code-mode/sandbox-worker.js +166 -0
  49. package/script/src/code-mode/sandbox.d.ts +142 -29
  50. package/script/src/code-mode/sandbox.d.ts.map +1 -1
  51. package/script/src/code-mode/sandbox.js +429 -72
  52. package/script/src/code-mode/surface.d.ts +148 -0
  53. package/script/src/code-mode/surface.d.ts.map +1 -0
  54. package/script/src/code-mode/surface.js +211 -0
  55. package/script/src/code-mode/tool.js +1 -1
  56. package/script/src/code-mode/worker-host.d.ts +39 -11
  57. package/script/src/code-mode/worker-host.d.ts.map +1 -1
  58. package/script/src/code-mode/worker-host.js +100 -42
  59. package/script/src/code-mode/worker-protocol.d.ts +45 -8
  60. package/script/src/code-mode/worker-protocol.d.ts.map +1 -1
  61. package/script/src/code-mode/worker-protocol.js +4 -2
  62. package/script/src/version.d.ts +1 -1
  63. 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,211 @@
1
+ "use strict";
2
+ /**
3
+ * What a host exposes to a Code Mode program: the object the program receives,
4
+ * the constants set on it and its closed table of operations. The sandbox
5
+ * builds that object from this declaration and nothing else (ADR 0007).
6
+ *
7
+ * @module code-mode/surface
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.SandboxHostError = exports.SandboxResultReference = void 0;
11
+ exports.resolveSandboxSurface = resolveSandboxSurface;
12
+ exports.surfaceUsesReferences = surfaceUsesReferences;
13
+ /**
14
+ * Stands for an earlier result of the same run, where an operation or the
15
+ * surface asked for references. `call` is the identifier the host received
16
+ * with the call that produced the result, so the host finds what it kept.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * const kept = new Map<number, unknown>();
21
+ * function designated(argument: unknown): unknown {
22
+ * return argument instanceof SandboxResultReference ? kept.get(argument.call) : undefined;
23
+ * }
24
+ * ```
25
+ */
26
+ class SandboxResultReference {
27
+ /** The identifier of the call that produced the result. */
28
+ call;
29
+ constructor(call) {
30
+ this.call = call;
31
+ }
32
+ }
33
+ exports.SandboxResultReference = SandboxResultReference;
34
+ const HOST_ERROR_CODE = /^[A-Z][A-Z0-9_]{0,63}$/;
35
+ /**
36
+ * The codes the sandbox itself throws into a program. A host error never takes
37
+ * one of them, so a `catch` can tell the host's refusal from the sandbox's.
38
+ * Codes a program never catches from the sandbox (a time limit ends the run)
39
+ * stay free for the host, and so do the codes of Compose's own chain.
40
+ */
41
+ const PROGRAM_VISIBLE_SANDBOX_CODES = new Set([
42
+ "ARGUMENT_INVALID",
43
+ "OPERATION_LIMIT",
44
+ "OPERATION_FAILED",
45
+ "RECEIPT_TOO_LARGE",
46
+ // A result the program has no memory left to hold fails the call it awaited.
47
+ "RESOURCE_LIMIT",
48
+ "INTERRUPTED",
49
+ ]);
50
+ /**
51
+ * An error in the host's own vocabulary, for the program to read. A host
52
+ * operation rejects with it to say what happened in terms the program can act
53
+ * on. The program catches an error whose `code`, `message` and `recovery` are
54
+ * the host's, unchanged; its `name` is `CodeModeError` and its `context` is
55
+ * empty. Any other rejection reaches the program as `OPERATION_FAILED`, without
56
+ * its text. If the program does not handle it, the run rejects with this very
57
+ * object.
58
+ *
59
+ * The code is the host's own. The few codes the sandbox itself throws into a
60
+ * program (`ARGUMENT_INVALID`, `OPERATION_LIMIT`, `OPERATION_FAILED`,
61
+ * `RECEIPT_TOO_LARGE`, `RESOURCE_LIMIT`, `INTERRUPTED`) are refused here, so a `catch` can tell
62
+ * the two apart. To report one of those, an operation rejects with a
63
+ * `CodeModeError`.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * throw new SandboxHostError(
68
+ * "ORDER_CHANGED",
69
+ * "Order 1042 changed since it was read",
70
+ * "Read it again, then decide",
71
+ * );
72
+ * ```
73
+ */
74
+ class SandboxHostError extends Error {
75
+ /** Upper-case letters, digits and underscores, starting with a letter: at most 64 characters. */
76
+ code;
77
+ /** What the program or the agent can do next. */
78
+ recovery;
79
+ constructor(code, message, recovery, options) {
80
+ if (typeof code !== "string" || !HOST_ERROR_CODE.test(code)) {
81
+ throw new RangeError("A sandbox host error code is 1 to 64 upper-case letters, digits or underscores, starting with a letter.");
82
+ }
83
+ if (PROGRAM_VISIBLE_SANDBOX_CODES.has(code)) {
84
+ throw new RangeError(`${code} is a code the sandbox itself throws into a program: reject with a CodeModeError to report it.`);
85
+ }
86
+ if (typeof message !== "string" || typeof recovery !== "string") {
87
+ throw new RangeError("A sandbox host error needs a message and a recovery, both strings.");
88
+ }
89
+ super(message, options);
90
+ this.name = "SandboxHostError";
91
+ this.code = code;
92
+ this.recovery = recovery;
93
+ }
94
+ }
95
+ exports.SandboxHostError = SandboxHostError;
96
+ /** Names a strict async function cannot bind as a parameter. */
97
+ const RESERVED_NAMES = new Set([
98
+ ...["arguments", "await", "break", "case", "catch", "class", "const", "continue", "debugger"],
99
+ ...["default", "delete", "do", "else", "enum", "eval", "export", "extends", "false", "finally"],
100
+ ...["for", "function", "if", "implements", "import", "in", "instanceof", "interface", "let"],
101
+ ...["new", "null", "package", "private", "protected", "public", "return", "static", "super"],
102
+ ...["switch", "this", "throw", "true", "try", "typeof", "var", "void", "while", "with", "yield"],
103
+ ]);
104
+ /** `__proto__` would replace the object's prototype, and `then` would make it a thenable. */
105
+ const FORBIDDEN_MEMBERS = new Set(["__proto__", "then"]);
106
+ const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
107
+ const START_VALUES = new Set(["on-await", "on-call"]);
108
+ const RESULT_ARGUMENT_VALUES = new Set(["copy", "reference"]);
109
+ function isRecord(value) {
110
+ return typeof value === "object" && value !== null && !Array.isArray(value);
111
+ }
112
+ function assertMember(namespace, name, kind) {
113
+ if (!IDENTIFIER.test(name) || FORBIDDEN_MEMBERS.has(name)) {
114
+ throw new RangeError(`Sandbox ${kind} ${namespace}.${name} must be a plain property name other than __proto__ and then.`);
115
+ }
116
+ }
117
+ /**
118
+ * Validate a surface and return a frozen copy of its declaration: its name,
119
+ * its operations, its budgets and the list of its constants. Throws a
120
+ * RangeError for a surface a program could not use as declared: this is a host
121
+ * configuration error, raised before any program runs.
122
+ *
123
+ * The values of the constants are not copied here. Each run copies them to
124
+ * JSON when it starts, and refuses one that is not JSON within the value limit.
125
+ *
126
+ * @example
127
+ * ```ts
128
+ * const surface = resolveSandboxSurface({
129
+ * namespace: "shop",
130
+ * operations: { getOrder: { budget: "reads" } },
131
+ * budgets: { reads: 12 },
132
+ * });
133
+ * ```
134
+ */
135
+ function resolveSandboxSurface(surface) {
136
+ if (!isRecord(surface))
137
+ throw new RangeError("A sandbox surface must be an object.");
138
+ const { namespace } = surface;
139
+ if (typeof namespace !== "string" || !IDENTIFIER.test(namespace) || RESERVED_NAMES.has(namespace)) {
140
+ throw new RangeError("Sandbox surface namespace must be an identifier that is not a reserved word.");
141
+ }
142
+ if (!isRecord(surface.budgets)) {
143
+ throw new RangeError("Sandbox surface budgets must be an object.");
144
+ }
145
+ const budgets = {};
146
+ for (const [name, limit] of Object.entries(surface.budgets)) {
147
+ // `budgets.__proto__ = limit` would set nothing and report the budget as unlisted.
148
+ // A budget is a key of this table, never a member of the object a program gets.
149
+ if (!IDENTIFIER.test(name) || name === "__proto__") {
150
+ throw new RangeError(`Sandbox budget ${name} must be a plain name other than __proto__.`);
151
+ }
152
+ if (!Number.isSafeInteger(limit) || limit <= 0) {
153
+ throw new RangeError(`Sandbox budget ${name} must be a positive integer.`);
154
+ }
155
+ budgets[name] = limit;
156
+ }
157
+ if (!isRecord(surface.operations)) {
158
+ throw new RangeError("Sandbox surface operations must be an object.");
159
+ }
160
+ const operations = {};
161
+ for (const [name, operation] of Object.entries(surface.operations)) {
162
+ assertMember(namespace, name, "operation");
163
+ if (!isRecord(operation)) {
164
+ throw new RangeError(`Sandbox operation ${namespace}.${name} must name its budget.`);
165
+ }
166
+ const { budget, start, resultArguments } = operation;
167
+ if (typeof budget !== "string") {
168
+ throw new RangeError(`Sandbox operation ${namespace}.${name} must name its budget.`);
169
+ }
170
+ if (!Object.hasOwn(budgets, budget)) {
171
+ throw new RangeError(`Sandbox operation ${namespace}.${name} names the budget ${budget}, which the surface does not list.`);
172
+ }
173
+ if (start !== undefined && !START_VALUES.has(start)) {
174
+ throw new RangeError(`Sandbox operation ${namespace}.${name}: start must be "on-await" or "on-call".`);
175
+ }
176
+ if (resultArguments !== undefined && !RESULT_ARGUMENT_VALUES.has(resultArguments)) {
177
+ throw new RangeError(`Sandbox operation ${namespace}.${name}: resultArguments must be "copy" or "reference".`);
178
+ }
179
+ operations[name] = Object.freeze({
180
+ budget,
181
+ ...(start !== undefined && { start: start }),
182
+ ...(resultArguments !== undefined &&
183
+ { resultArguments: resultArguments }),
184
+ });
185
+ }
186
+ const { returnedResult } = surface;
187
+ if (returnedResult !== undefined && !RESULT_ARGUMENT_VALUES.has(returnedResult)) {
188
+ throw new RangeError('Sandbox surface returnedResult must be "copy" or "reference".');
189
+ }
190
+ const resolved = { namespace, operations: Object.freeze(operations), budgets: Object.freeze(budgets) };
191
+ if (returnedResult !== undefined)
192
+ resolved.returnedResult = returnedResult;
193
+ if (surface.constants !== undefined) {
194
+ if (!isRecord(surface.constants)) {
195
+ throw new RangeError("Sandbox surface constants must be an object.");
196
+ }
197
+ for (const name of Object.keys(surface.constants)) {
198
+ assertMember(namespace, name, "constant");
199
+ if (Object.hasOwn(operations, name)) {
200
+ throw new RangeError(`Sandbox constant ${namespace}.${name} has the name of an operation.`);
201
+ }
202
+ }
203
+ resolved.constants = Object.freeze({ ...surface.constants });
204
+ }
205
+ return Object.freeze(resolved);
206
+ }
207
+ /** True when a run of this surface has to recognise earlier results. */
208
+ function surfaceUsesReferences(surface) {
209
+ return surface.returnedResult === "reference" ||
210
+ Object.values(surface.operations).some((operation) => operation.resultArguments === "reference");
211
+ }
@@ -22,7 +22,7 @@ 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.",
25
+ "Await every api call (or pass them to Promise.all): a call the program has not waited for when it ends is not sent.",
26
26
  "A failed api call throws an Error with code, recovery and context; tool errors read [execute] CODE: detail. Recovery: hint.",
27
27
  ...(eventPorts.length
28
28
  ? [
@@ -1,11 +1,11 @@
1
1
  import { CodeModeError } from "./errors.js";
2
- import { type SandboxLimits } from "./sandbox-core.js";
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?: {
@@ -53,30 +53,58 @@ export declare function nodeWorkerEnv(env: Readonly<Record<string, string | unde
53
53
  * posted, so an oversized result never crosses the worker boundary (no parse and
54
54
  * no structured clone). The guest decodes the text.
55
55
  */
56
- export declare function encodeReply(id: number, operation: string, value: unknown, valueLength: number): HostMessage;
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;
57
59
  /** Inputs of {@link runInWorker}. */
58
60
  export interface WorkerRunInput {
59
61
  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>;
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>;
64
83
  /** Aborting stops the guest at its next interrupt check. */
65
84
  readonly signal: AbortSignal;
66
85
  }
67
86
  /** The error for a worker that could not be created at all. */
68
87
  export declare function workerSpawnFailure(error: unknown): CodeModeError;
69
88
  /**
70
- * Run one program in a fresh worker and settle once with its value or a
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
71
97
  * {@link CodeModeError}. The worker is always terminated afterwards.
72
98
  *
73
99
  * @example
74
100
  * ```ts
75
- * const value = await runInWorker({
76
- * code: "return 1 + 1;", params: {}, limits: CODE_MODE_LIMITS,
101
+ * const json = await runInWorker({
102
+ * code: "return 1 + 1;",
103
+ * surface: { namespace: "api", operations: {}, budgets: {} },
104
+ * limits: SANDBOX_RESOURCE_LIMITS,
77
105
  * call: () => Promise.reject(new Error("unused")), signal: new AbortController().signal,
78
106
  * });
79
107
  * ```
80
108
  */
81
- export declare function runInWorker(input: WorkerRunInput): Promise<unknown>;
109
+ export declare function runInWorker(input: WorkerRunInput): Promise<string | undefined>;
82
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,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,C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