@ny4n/codemode 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +324 -2
- package/dist/cloudflare.d.ts +53 -0
- package/dist/cloudflare.d.ts.map +1 -0
- package/dist/cloudflare.js +64 -0
- package/dist/cloudflare.js.map +1 -0
- package/dist/declarations.d.ts +58 -0
- package/dist/declarations.d.ts.map +1 -0
- package/dist/declarations.js +334 -0
- package/dist/declarations.js.map +1 -0
- package/dist/identifier.d.ts +6 -0
- package/dist/identifier.d.ts.map +1 -0
- package/dist/identifier.js +13 -0
- package/dist/identifier.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/portable.d.ts +15 -0
- package/dist/portable.d.ts.map +1 -0
- package/dist/portable.js +12 -0
- package/dist/portable.js.map +1 -0
- package/dist/runtime/execution.d.ts +54 -0
- package/dist/runtime/execution.d.ts.map +1 -0
- package/dist/runtime/execution.js +371 -0
- package/dist/runtime/execution.js.map +1 -0
- package/dist/runtime/host.d.ts +13 -0
- package/dist/runtime/host.d.ts.map +1 -0
- package/dist/runtime/host.js +72 -0
- package/dist/runtime/host.js.map +1 -0
- package/dist/runtime/inline.d.ts +63 -0
- package/dist/runtime/inline.d.ts.map +1 -0
- package/dist/runtime/inline.js +79 -0
- package/dist/runtime/inline.js.map +1 -0
- package/dist/runtime/prelude-source.d.ts +43 -0
- package/dist/runtime/prelude-source.d.ts.map +1 -0
- package/dist/runtime/prelude-source.js +510 -0
- package/dist/runtime/prelude-source.js.map +1 -0
- package/dist/runtime/protocol.d.ts +75 -0
- package/dist/runtime/protocol.d.ts.map +1 -0
- package/dist/runtime/protocol.js +10 -0
- package/dist/runtime/protocol.js.map +1 -0
- package/dist/runtime/remote.d.ts +102 -0
- package/dist/runtime/remote.d.ts.map +1 -0
- package/dist/runtime/remote.js +170 -0
- package/dist/runtime/remote.js.map +1 -0
- package/dist/runtime/vm.d.ts +22 -0
- package/dist/runtime/vm.d.ts.map +1 -0
- package/dist/runtime/vm.js +171 -0
- package/dist/runtime/vm.js.map +1 -0
- package/dist/runtime/worker.d.ts +2 -0
- package/dist/runtime/worker.d.ts.map +1 -0
- package/dist/runtime/worker.js +38 -0
- package/dist/runtime/worker.js.map +1 -0
- package/dist/source.d.ts +35 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +92 -0
- package/dist/source.js.map +1 -0
- package/dist/types.d.ts +148 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/wasm.d.ts +12 -0
- package/dist/wasm.d.ts.map +1 -0
- package/dist/wasm.js +24 -0
- package/dist/wasm.js.map +1 -0
- package/package.json +75 -5
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drives one QuickJS VM through one script, independent of the transport that connects it to the
|
|
3
|
+
* host. The worker thread, the in-thread launcher, and the remote server all run the VM through
|
|
4
|
+
* this module, so the bridge between prelude and host exists once.
|
|
5
|
+
*
|
|
6
|
+
* Contract with the caller:
|
|
7
|
+
* - `post` receives every message for the host, synchronously from inside VM callbacks. The
|
|
8
|
+
* caller must not re-enter the VM from `post` (no `deliver`, no `dispose`); a worker posts to
|
|
9
|
+
* another thread, and the in-thread launcher defers delivery to a microtask.
|
|
10
|
+
* - `deliver` hands the VM a tool result and runs the script until it waits again. It must only be
|
|
11
|
+
* called while the VM is idle, which holds for any caller that never re-enters from `post`.
|
|
12
|
+
* - `shouldInterrupt` is polled by QuickJS while the script runs, and here between batches of
|
|
13
|
+
* promise jobs. Returning true throws an uncatchable error in the script; the VM then reports
|
|
14
|
+
* `crash` or a failed `done`. The job batches matter for a script such as `while (true) await
|
|
15
|
+
* null`: every iteration is a separate job that executes only a few opcodes, so QuickJS's own
|
|
16
|
+
* polling, which counts opcodes, would let it run for a long time between polls.
|
|
17
|
+
* - The VM never decides on its own when it is finished: after `done` or `crash` it stays alive
|
|
18
|
+
* until the caller disposes it (or terminates the worker that hosts it).
|
|
19
|
+
*
|
|
20
|
+
* Failures after the VM exists are reported as a `crash` message; `startVm` only rejects when the
|
|
21
|
+
* VM cannot be created at all.
|
|
22
|
+
*/
|
|
23
|
+
import { JSException, MAX_STACK_SIZE, QuickJS } from "quickjs-wasi";
|
|
24
|
+
import { PRELUDE_SOURCE } from "./prelude-source.js";
|
|
25
|
+
/**
|
|
26
|
+
* QuickJS writes engine diagnostics to fd 1 and 2, which the default shim
|
|
27
|
+
* forwards to the host's stdout and stderr. That output belongs to the host
|
|
28
|
+
* application (for example a TUI), so it is discarded. Reporting every byte as
|
|
29
|
+
* written keeps libc from retrying.
|
|
30
|
+
*/
|
|
31
|
+
function discardOutput(memory) {
|
|
32
|
+
return {
|
|
33
|
+
fd_write(_fd, iovsPtr, iovsLen, nwrittenPtr) {
|
|
34
|
+
const view = new DataView(memory.buffer);
|
|
35
|
+
let written = 0;
|
|
36
|
+
for (let i = 0; i < iovsLen; i++) {
|
|
37
|
+
written += view.getUint32(iovsPtr + i * 8 + 4, true);
|
|
38
|
+
}
|
|
39
|
+
view.setUint32(nwrittenPtr, written, true);
|
|
40
|
+
return 0;
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function describeException(error) {
|
|
45
|
+
const head = error.message ? `${error.name}: ${error.message}` : error.name;
|
|
46
|
+
const stack = error.stack?.trimEnd();
|
|
47
|
+
return JSON.stringify({ name: error.name, message: error.message, stack: stack ? `${head}\n${stack}` : head });
|
|
48
|
+
}
|
|
49
|
+
/** Promise jobs run between two `shouldInterrupt` polls while draining the job queue. */
|
|
50
|
+
const JOBS_PER_POLL = 50;
|
|
51
|
+
function crashMessage(error) {
|
|
52
|
+
return { type: "crash", message: error instanceof Error ? `${error.name}: ${error.message}` : String(error) };
|
|
53
|
+
}
|
|
54
|
+
export async function startVm(options) {
|
|
55
|
+
const { data, post } = options;
|
|
56
|
+
const vm = await QuickJS.create({
|
|
57
|
+
wasm: options.wasm,
|
|
58
|
+
memoryLimit: data.memoryLimitBytes,
|
|
59
|
+
// Without a guard, deep recursion overflows the wasm stack and traps instead of throwing a
|
|
60
|
+
// catchable RangeError.
|
|
61
|
+
maxStackSize: options.maxStackSize ?? MAX_STACK_SIZE,
|
|
62
|
+
interruptHandler: options.shouldInterrupt,
|
|
63
|
+
wasi: discardOutput,
|
|
64
|
+
});
|
|
65
|
+
let disposed = false;
|
|
66
|
+
const session = {
|
|
67
|
+
deliver(message) {
|
|
68
|
+
if (disposed)
|
|
69
|
+
return;
|
|
70
|
+
try {
|
|
71
|
+
vm.withScope(() => {
|
|
72
|
+
vm.callFunction(settle, api, vm.newNumber(message.id), message.ok ? vm.true : vm.false, message.payload === undefined ? vm.undefined : vm.newString(message.payload));
|
|
73
|
+
});
|
|
74
|
+
drain();
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
post(crashMessage(error));
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
dispose() {
|
|
81
|
+
if (disposed)
|
|
82
|
+
return;
|
|
83
|
+
disposed = true;
|
|
84
|
+
vm.dispose();
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
let api;
|
|
88
|
+
let settle;
|
|
89
|
+
let stalled;
|
|
90
|
+
// `executePendingJobs()` runs the queue to empty without a chance to interrupt, so jobs run one at a
|
|
91
|
+
// time through the (internal, but public) exports; quickjs-wasi is pinned to an exact version.
|
|
92
|
+
const exports = vm._getExports();
|
|
93
|
+
/** Run queued jobs, then fail a script that waits on nothing that can ever resume it. */
|
|
94
|
+
const drain = () => {
|
|
95
|
+
let jobs = 0;
|
|
96
|
+
while (exports.qjs_is_job_pending()) {
|
|
97
|
+
// A job only fails for uncatchable errors, such as an interrupt.
|
|
98
|
+
if (exports.qjs_execute_pending_job() < 0)
|
|
99
|
+
throw new Error("Promise job failed");
|
|
100
|
+
if (++jobs % JOBS_PER_POLL === 0 && options.shouldInterrupt())
|
|
101
|
+
throw new Error("Execution interrupted");
|
|
102
|
+
}
|
|
103
|
+
vm.callFunction(stalled, api).dispose();
|
|
104
|
+
};
|
|
105
|
+
try {
|
|
106
|
+
// Called from the prelude with primitives only.
|
|
107
|
+
const bridge = vm.newFunction("bridge", (kind, a, b, c) => {
|
|
108
|
+
switch (kind.toString()) {
|
|
109
|
+
case "call":
|
|
110
|
+
case "global":
|
|
111
|
+
post({
|
|
112
|
+
type: "call",
|
|
113
|
+
id: a.toNumber(),
|
|
114
|
+
target: kind.toString() === "call" ? "tool" : "global",
|
|
115
|
+
name: b.toString(),
|
|
116
|
+
args: c === undefined || c.isUndefined ? undefined : c.toString(),
|
|
117
|
+
});
|
|
118
|
+
break;
|
|
119
|
+
case "output":
|
|
120
|
+
post({
|
|
121
|
+
type: "output",
|
|
122
|
+
item: a.toString() === "image"
|
|
123
|
+
? { type: "image", data: b.toString(), mimeType: c.toString() }
|
|
124
|
+
: a.toString() === "console"
|
|
125
|
+
? { type: "text", text: b.toString(), console: true }
|
|
126
|
+
: { type: "text", text: b.toString() },
|
|
127
|
+
});
|
|
128
|
+
break;
|
|
129
|
+
case "done":
|
|
130
|
+
if (a.toBoolean()) {
|
|
131
|
+
post({
|
|
132
|
+
type: "done",
|
|
133
|
+
ok: true,
|
|
134
|
+
value: b === undefined || b.isUndefined ? undefined : b.toString(),
|
|
135
|
+
writes: c.toString(),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
post({ type: "done", ok: false, error: b.toString() });
|
|
140
|
+
}
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
return vm.undefined;
|
|
144
|
+
});
|
|
145
|
+
// These handles live as long as the VM, which the caller disposes as a whole.
|
|
146
|
+
api = vm.withScope((scope) => scope.escape(vm.callFunction(vm.evalCode(PRELUDE_SOURCE, "codemode-prelude.js"), vm.undefined, bridge, vm.newString(JSON.stringify(data.tools)), vm.newString(JSON.stringify(data.globals)), vm.newString(JSON.stringify(data.store)))));
|
|
147
|
+
settle = api.getProp("settle");
|
|
148
|
+
const run = api.getProp("run");
|
|
149
|
+
stalled = api.getProp("stalled");
|
|
150
|
+
// The prefix shares the first line with the script so reported line numbers
|
|
151
|
+
// match the script as written.
|
|
152
|
+
let fn;
|
|
153
|
+
try {
|
|
154
|
+
fn = vm.evalCode(`(async (tools, console) => {${data.code}\n})`, "codemode.js");
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
if (!(error instanceof JSException))
|
|
158
|
+
throw error;
|
|
159
|
+
post({ type: "done", ok: false, error: describeException(error) });
|
|
160
|
+
return session;
|
|
161
|
+
}
|
|
162
|
+
vm.callFunction(run, api, fn).dispose();
|
|
163
|
+
fn.dispose();
|
|
164
|
+
drain();
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
post(crashMessage(error));
|
|
168
|
+
}
|
|
169
|
+
return session;
|
|
170
|
+
}
|
|
171
|
+
//# sourceMappingURL=vm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vm.js","sourceRoot":"","sources":["../../src/runtime/vm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,EAAE,WAAW,EAAsB,cAAc,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAExF,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAuBrD;;;;;GAKG;AACH,SAAS,aAAa,CAAC,MAA4C;IAClE,OAAO;QACN,QAAQ,CAAC,GAAW,EAAE,OAAe,EAAE,OAAe,EAAE,WAAmB;YAC1E,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YACzC,IAAI,OAAO,GAAG,CAAC,CAAC;YAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,EAAE,E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ickJS VM through one script, independent of the transport that connects it to the\n * host. The worker thread, the in-thread launcher, and the remote server all run the VM through\n * this module, so the bridge between prelude and host exists once.\n *\n * Contract with the caller:\n * - `post` receives every message for the host, synchronously from inside VM callbacks. The\n * caller must not re-enter the VM from `post` (no `deliver`, no `dispose`); a worker posts to\n * another thread, and the in-thread launcher defers delivery to a microtask.\n * - `deliver` hands the VM a tool result and runs the script until it waits again. It must only be\n * called while the VM is idle, which holds for any caller that never re-enters from `post`.\n * - `shouldInterrupt` is polled by QuickJS while the script runs, and here between batches of\n * promise jobs. Returning true throws an uncatchable error in the script; the VM then reports\n * `crash` or a failed `done`. The job batches matter for a script such as `while (true) await\n * null`: every iteration is a separate job that executes only a few opcodes, so QuickJS's own\n * polling, which counts opcodes, would let it run for a long time between polls.\n * - The VM never decides on its own when it is finished: after `done` or `crash` it stays alive\n * until the caller disposes it (or terminates the worker that hosts it).\n *\n * Failures after the VM exists are reported as a `crash` message; `startVm` only rejects when the\n * VM cannot be created at all.\n */\nimport { JSException, type JSValueHandle, MAX_STACK_SIZE, QuickJS } from \"quickjs-wasi\";\nimport type { CodemodeWasmModule } from \"../wasm.ts\";\nimport { PRELUDE_SOURCE } from \"./prelude-source.ts\";\nimport type { HostToWorkerMessage, VmStartData, WorkerToHostMessage } from \"./protocol.ts\";\n\nexport interface VmSession {\n\tdeliver(message: HostToWorkerMessage): void;\n\t/** Releases the VM's wasm instance. The session must not be used afterwards. */\n\tdispose(): void;\n}\n\nexport interface StartVmOptions {\n\tdata: VmStartData;\n\twasm: CodemodeWasmModule;\n\t/**\n\t * QuickJS's guard on its own stack, which turns deep recursion into a catchable `RangeError`.\n\t * The wasm frames also use the host thread's native stack, so the guard must trip before that\n\t * stack runs out: a host overflow fails the whole execution instead. Default: `MAX_STACK_SIZE`\n\t * (512 KiB), which fits the 4 MB stack of a Node worker thread but not a 1 MB main thread.\n\t */\n\tmaxStackSize?: number;\n\tpost(message: WorkerToHostMessage): void;\n\tshouldInterrupt(): boolean;\n}\n\n/**\n * QuickJS writes engine diagnostics to fd 1 and 2, which the default shim\n * forwards to the host's stdout and stderr. That output belongs to the host\n * application (for example a TUI), so it is discarded. Reporting every byte as\n * written keeps libc from retrying.\n */\nfunction discardOutput(memory: { readonly buffer: ArrayBufferLike }) {\n\treturn {\n\t\tfd_write(_fd: number, iovsPtr: number, iovsLen: number, nwrittenPtr: number): number {\n\t\t\tconst view = new DataView(memory.buffer);\n\t\t\tlet written = 0;\n\t\t\tfor (let i = 0; i < iovsLen; i++) {\n\t\t\t\twritten += view.getUint32(iovsPtr + i * 8 + 4, true);\n\t\t\t}\n\t\t\tview.setUint32(nwrittenPtr, written, true);\n\t\t\treturn 0;\n\t\t},\n\t};\n}\n\nfunction describeException(error: JSException): string {\n\tconst head = error.message ? `${error.name}: ${error.message}` : error.name;\n\tconst stack = error.stack?.trimEnd();\n\treturn JSON.stringify({ name: error.name, message: error.message, stack: stack ? `${head}\\n${stack}` : head });\n}\n\n/** Promise jobs run between two `shouldInterrupt` polls while draining the job queue. */\nconst JOBS_PER_POLL = 50;\n\nfunction crashMessage(error: unknown): WorkerToHostMessage {\n\treturn { type: \"crash\", message: error instanceof Error ? `${error.name}: ${error.message}` : String(error) };\n}\n\nexport async function startVm(options: StartVmOptions): Promise<VmSession> {\n\tconst { data, post } = options;\n\tconst vm = await QuickJS.create({\n\t\twasm: options.wasm,\n\t\tmemoryLimit: data.memoryLimitBytes,\n\t\t// Without a guard, deep recursion overflows the wasm stack and traps instead of throwing a\n\t\t// catchable RangeError.\n\t\tmaxStackSize: options.maxStackSize ?? MAX_STACK_SIZE,\n\t\tinterruptHandler: options.shouldInterrupt,\n\t\twasi: discardOutput,\n\t});\n\tlet disposed = false;\n\tconst session: VmSession = {\n\t\tdeliver(message) {\n\t\t\tif (disposed) return;\n\t\t\ttry {\n\t\t\t\tvm.withScope(() => {\n\t\t\t\t\tvm.callFunction(\n\t\t\t\t\t\tsettle,\n\t\t\t\t\t\tapi,\n\t\t\t\t\t\tvm.newNumber(message.id),\n\t\t\t\t\t\tmessage.ok ? vm.true : vm.false,\n\t\t\t\t\t\tmessage.payload === undefined ? vm.undefined : vm.newString(message.payload),\n\t\t\t\t\t);\n\t\t\t\t});\n\t\t\t\tdrain();\n\t\t\t} catch (error) {\n\t\t\t\tpost(crashMessage(error));\n\t\t\t}\n\t\t},\n\t\tdispose() {\n\t\t\tif (disposed) return;\n\t\t\tdisposed = true;\n\t\t\tvm.dispose();\n\t\t},\n\t};\n\n\tlet api: JSValueHandle;\n\tlet settle: JSValueHandle;\n\tlet stalled: JSValueHandle;\n\t// `executePendingJobs()` runs the queue to empty without a chance to interrupt, so jobs run one at a\n\t// time through the (internal, but public) exports; quickjs-wasi is pinned to an exact version.\n\tconst exports = vm._getExports();\n\t/** Run queued jobs, then fail a script that waits on nothing that can ever resume it. */\n\tconst drain = () => {\n\t\tlet jobs = 0;\n\t\twhile (exports.qjs_is_job_pending()) {\n\t\t\t// A job only fails for uncatchable errors, such as an interrupt.\n\t\t\tif (exports.qjs_execute_pending_job() < 0) throw new Error(\"Promise job failed\");\n\t\t\tif (++jobs % JOBS_PER_POLL === 0 && options.shouldInterrupt()) throw new Error(\"Execution interrupted\");\n\t\t}\n\t\tvm.callFunction(stalled, api).dispose();\n\t};\n\n\ttry {\n\t\t// Called from the prelude with primitives only.\n\t\tconst bridge = vm.newFunction(\"bridge\", (kind, a, b, c) => {\n\t\t\tswitch (kind.toString()) {\n\t\t\t\tcase \"call\":\n\t\t\t\tcase \"global\":\n\t\t\t\t\tpost({\n\t\t\t\t\t\ttype: \"call\",\n\t\t\t\t\t\tid: a.toNumber(),\n\t\t\t\t\t\ttarget: kind.toString() === \"call\" ? \"tool\" : \"global\",\n\t\t\t\t\t\tname: b.toString(),\n\t\t\t\t\t\targs: c === undefined || c.isUndefined ? undefined : c.toString(),\n\t\t\t\t\t});\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"output\":\n\t\t\t\t\tpost({\n\t\t\t\t\t\ttype: \"output\",\n\t\t\t\t\t\titem:\n\t\t\t\t\t\t\ta.toString() === \"image\"\n\t\t\t\t\t\t\t\t? { type: \"image\", data: b.toString(), mimeType: c.toString() }\n\t\t\t\t\t\t\t\t: a.toString() === \"console\"\n\t\t\t\t\t\t\t\t\t? { type: \"text\", text: b.toString(), console: true }\n\t\t\t\t\t\t\t\t\t: { type: \"text\", text: b.toString() },\n\t\t\t\t\t});\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"done\":\n\t\t\t\t\tif (a.toBoolean()) {\n\t\t\t\t\t\tpost({\n\t\t\t\t\t\t\ttype: \"done\",\n\t\t\t\t\t\t\tok: true,\n\t\t\t\t\t\t\tvalue: b === undefined || b.isUndefined ? undefined : b.toString(),\n\t\t\t\t\t\t\twrites: c.toString(),\n\t\t\t\t\t\t});\n\t\t\t\t\t} else {\n\t\t\t\t\t\tpost({ type: \"done\", ok: false, error: b.toString() });\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\treturn vm.undefined;\n\t\t});\n\n\t\t// These handles live as long as the VM, which the caller disposes as a whole.\n\t\tapi = vm.withScope((scope) =>\n\t\t\tscope.escape(\n\t\t\t\tvm.callFunction(\n\t\t\t\t\tvm.evalCode(PRELUDE_SOURCE, \"codemode-prelude.js\"),\n\t\t\t\t\tvm.undefined,\n\t\t\t\t\tbridge,\n\t\t\t\t\tvm.newString(JSON.stringify(data.tools)),\n\t\t\t\t\tvm.newString(JSON.stringify(data.globals)),\n\t\t\t\t\tvm.newString(JSON.stringify(data.store)),\n\t\t\t\t),\n\t\t\t),\n\t\t);\n\t\tsettle = api.getProp(\"settle\");\n\t\tconst run = api.getProp(\"run\");\n\t\tstalled = api.getProp(\"stalled\");\n\n\t\t// The prefix shares the first line with the script so reported line numbers\n\t\t// match the script as written.\n\t\tlet fn: JSValueHandle;\n\t\ttry {\n\t\t\tfn = vm.evalCode(`(async (tools, console) => {${data.code}\\n})`, \"codemode.js\");\n\t\t} catch (error) {\n\t\t\tif (!(error instanceof JSException)) throw error;\n\t\t\tpost({ type: \"done\", ok: false, error: describeException(error) });\n\t\t\treturn session;\n\t\t}\n\t\tvm.callFunction(run, api, fn).dispose();\n\t\tfn.dispose();\n\t\tdrain();\n\t} catch (error) {\n\t\tpost(crashMessage(error));\n\t}\n\treturn session;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/runtime/worker.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker thread entry. One worker runs one script inside a fresh QuickJS VM
|
|
3
|
+
* (a separate wasm instance), relays tool calls and output to the host, and
|
|
4
|
+
* reports the result. The host terminates the worker when the script settles,
|
|
5
|
+
* times out, or is aborted; the worker exists so that a spinning script never
|
|
6
|
+
* blocks the host thread.
|
|
7
|
+
*
|
|
8
|
+
* Importing this module starts the worker. Hosts that bundle their code (for
|
|
9
|
+
* example a Bun compiled executable) add a file that imports
|
|
10
|
+
* `@ny4n/codemode/worker` as a separate entrypoint and pass its URL
|
|
11
|
+
* or embedded-module string specifier as `workerUrl`.
|
|
12
|
+
*/
|
|
13
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
14
|
+
import { isHostToWorkerMessage } from "./protocol.js";
|
|
15
|
+
import { startVm } from "./vm.js";
|
|
16
|
+
function post(message) {
|
|
17
|
+
parentPort?.postMessage(message);
|
|
18
|
+
}
|
|
19
|
+
async function main(data) {
|
|
20
|
+
const interrupt = new Int32Array(data.interrupt);
|
|
21
|
+
// The VM lives until the host terminates the worker, so the session is never disposed.
|
|
22
|
+
const session = await startVm({
|
|
23
|
+
data,
|
|
24
|
+
wasm: data.wasm,
|
|
25
|
+
post,
|
|
26
|
+
shouldInterrupt: () => Atomics.load(interrupt, 0) !== 0,
|
|
27
|
+
});
|
|
28
|
+
parentPort?.on("message", (message) => {
|
|
29
|
+
if (isHostToWorkerMessage(message))
|
|
30
|
+
session.deliver(message);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
if (parentPort) {
|
|
34
|
+
main(workerData).catch((error) => {
|
|
35
|
+
post({ type: "crash", message: error instanceof Error ? `${error.name}: ${error.message}` : String(error) });
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.js","sourceRoot":"","sources":["../../src/runtime/worker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,qBAAqB,EAA6C,MAAM,eAAe,CAAC;AACjG,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,SAAS,IAAI,CAAC,OAA4B;IACzC,UAAU,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;AAClC,CAAC;AAED,KAAK,UAAU,IAAI,CAAC,IAAgB;IACnC,MAAM,SAAS,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACjD,uFAAuF;IACvF,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC;QAC7B,IAAI;QACJ,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,IAAI;QACJ,eAAe,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC,KAAK,CAAC;KACvD,CAAC,CAAC;IACH,UAAU,EAAE,EAAE,CAAC,SAAS,EAAE,CAAC,OAAgB,EAAE,EAAE;QAC9C,IAAI,qBAAqB,CAAC,OAAO,CAAC;YAAE,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9D,CAAC,CAAC,CAAC;AACJ,CAAC;AAED,IAAI,UAAU,EAAE,CAAC;IAChB,IAAI,CAAC,UAAwB,CAAC,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;QACvD,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IAC9G,CAAC,CAAC,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Worker thread entry. One worker runs one script inside a fresh QuickJS VM\n * (a separate wasm instance), relays tool calls and output to the host, and\n * reports the result. The host terminates the worker when the script settles,\n * times out, or is aborted; the worker exists so that a spinning script never\n * blocks the host thread.\n *\n * Importing this module starts the worker. Hosts that bundle their code (for\n * example a Bun compiled executable) add a file that imports\n * `@ny4n/codemode/worker` as a separate entrypoint and pass its URL\n * or embedded-module string specifier as `workerUrl`.\n */\nimport { parentPort, workerData } from \"node:worker_threads\";\nimport { isHostToWorkerMessage, type WorkerData, type WorkerToHostMessage } from \"./protocol.ts\";\nimport { startVm } from \"./vm.ts\";\n\nfunction post(message: WorkerToHostMessage): void {\n\tparentPort?.postMessage(message);\n}\n\nasync function main(data: WorkerData): Promise<void> {\n\tconst interrupt = new Int32Array(data.interrupt);\n\t// The VM lives until the host terminates the worker, so the session is never disposed.\n\tconst session = await startVm({\n\t\tdata,\n\t\twasm: data.wasm,\n\t\tpost,\n\t\tshouldInterrupt: () => Atomics.load(interrupt, 0) !== 0,\n\t});\n\tparentPort?.on(\"message\", (message: unknown) => {\n\t\tif (isHostToWorkerMessage(message)) session.deliver(message);\n\t});\n}\n\nif (parentPort) {\n\tmain(workerData as WorkerData).catch((error: unknown) => {\n\t\tpost({ type: \"crash\", message: error instanceof Error ? `${error.name}: ${error.message}` : String(error) });\n\t});\n}\n"]}
|
package/dist/source.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codemode source format: JavaScript, optionally preceded by one options line.
|
|
3
|
+
*
|
|
4
|
+
* ```js
|
|
5
|
+
* // @options: {"max_output_tokens": 2000, "timeout_ms": 30000}
|
|
6
|
+
* const text = await tools.read({ path: "package.json" });
|
|
7
|
+
* text(JSON.parse(text).name);
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
export declare const CODEMODE_OPTIONS_PREFIX = "// @options:";
|
|
11
|
+
/**
|
|
12
|
+
* Lark grammar for providers with grammar-constrained tool input. It only fixes the shape of the
|
|
13
|
+
* options line; the options JSON and the code are checked by {@link parseCodemodeSource}.
|
|
14
|
+
*/
|
|
15
|
+
export declare const CODEMODE_SOURCE_GRAMMAR: string;
|
|
16
|
+
export interface CodemodeSourceOptions {
|
|
17
|
+
/** Token budget for the script's output. */
|
|
18
|
+
maxOutputTokens?: number;
|
|
19
|
+
/** Hard deadline for the whole script in milliseconds, including tool calls. */
|
|
20
|
+
timeoutMs?: number;
|
|
21
|
+
}
|
|
22
|
+
export interface ParsedCodemodeSource {
|
|
23
|
+
/** The script with the options line replaced by an empty line, so line numbers are unchanged. */
|
|
24
|
+
code: string;
|
|
25
|
+
options: CodemodeSourceOptions;
|
|
26
|
+
}
|
|
27
|
+
export declare class CodemodeSourceError extends Error {
|
|
28
|
+
constructor(message: string);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Split an optional first-line `// @options: {...}` from the script. Throws
|
|
32
|
+
* {@link CodemodeSourceError} for empty input and invalid options.
|
|
33
|
+
*/
|
|
34
|
+
export declare function parseCodemodeSource(input: string): ParsedCodemodeSource;
|
|
35
|
+
//# sourceMappingURL=source.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"source.d.ts","sourceRoot":"","sources":["../src/source.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,eAAO,MAAM,uBAAuB,iBAAiB,CAAC;AAOtD;;;GAGG;AACH,eAAO,MAAM,uBAAuB,QAQnC,CAAC;AAEF,MAAM,WAAW,qBAAqB;IACrC,4CAA4C;IAC5C,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gFAAgF;IAChF,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,oBAAoB;IACpC,iGAAiG;IACjG,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,qBAAqB,CAAC;CAC/B;AAED,qBAAa,mBAAoB,SAAQ,KAAK;IAC7C,YAAY,OAAO,EAAE,MAAM,EAG1B;CACD;AA8CD;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,oBAAoB,CAevE"}
|
package/dist/source.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codemode source format: JavaScript, optionally preceded by one options line.
|
|
3
|
+
*
|
|
4
|
+
* ```js
|
|
5
|
+
* // @options: {"max_output_tokens": 2000, "timeout_ms": 30000}
|
|
6
|
+
* const text = await tools.read({ path: "package.json" });
|
|
7
|
+
* text(JSON.parse(text).name);
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
export const CODEMODE_OPTIONS_PREFIX = "// @options:";
|
|
11
|
+
const SUPPORTED_FIELDS = ["max_output_tokens", "timeout_ms"];
|
|
12
|
+
const SUPPORTED_FIELDS_TEXT = "`max_output_tokens` and `timeout_ms`";
|
|
13
|
+
/** Largest delay `setTimeout` supports, which bounds `timeout_ms`. */
|
|
14
|
+
const MAX_TIMEOUT_MS = 2_147_483_647;
|
|
15
|
+
/**
|
|
16
|
+
* Lark grammar for providers with grammar-constrained tool input. It only fixes the shape of the
|
|
17
|
+
* options line; the options JSON and the code are checked by {@link parseCodemodeSource}.
|
|
18
|
+
*/
|
|
19
|
+
export const CODEMODE_SOURCE_GRAMMAR = String.raw `
|
|
20
|
+
start: options_source | plain_source
|
|
21
|
+
options_source: OPTIONS_LINE NEWLINE SOURCE
|
|
22
|
+
plain_source: SOURCE
|
|
23
|
+
|
|
24
|
+
OPTIONS_LINE: /[ \t]*\/\/ @options:[^\r\n]*/
|
|
25
|
+
NEWLINE: /\r?\n/
|
|
26
|
+
SOURCE: /[\s\S]+/
|
|
27
|
+
`;
|
|
28
|
+
export class CodemodeSourceError extends Error {
|
|
29
|
+
constructor(message) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.name = "CodemodeSourceError";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function isSafeInteger(value) {
|
|
35
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
36
|
+
}
|
|
37
|
+
function parseOptions(directive) {
|
|
38
|
+
if (directive === "") {
|
|
39
|
+
throw new CodemodeSourceError(`@options must be a JSON object with supported fields ${SUPPORTED_FIELDS_TEXT}`);
|
|
40
|
+
}
|
|
41
|
+
let value;
|
|
42
|
+
try {
|
|
43
|
+
value = JSON.parse(directive);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw new CodemodeSourceError(`@options must be valid JSON with supported fields ${SUPPORTED_FIELDS_TEXT}: ${error instanceof Error ? error.message : String(error)}`);
|
|
47
|
+
}
|
|
48
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
49
|
+
throw new CodemodeSourceError(`@options must be a JSON object with supported fields ${SUPPORTED_FIELDS_TEXT}`);
|
|
50
|
+
}
|
|
51
|
+
const fields = value;
|
|
52
|
+
for (const key of Object.keys(fields)) {
|
|
53
|
+
if (!SUPPORTED_FIELDS.includes(key)) {
|
|
54
|
+
throw new CodemodeSourceError(`@options only supports ${SUPPORTED_FIELDS_TEXT}; got \`${key}\``);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const options = {};
|
|
58
|
+
const { max_output_tokens, timeout_ms } = fields;
|
|
59
|
+
if (max_output_tokens !== undefined) {
|
|
60
|
+
if (!isSafeInteger(max_output_tokens)) {
|
|
61
|
+
throw new CodemodeSourceError("@options field `max_output_tokens` must be a non-negative safe integer");
|
|
62
|
+
}
|
|
63
|
+
options.maxOutputTokens = max_output_tokens;
|
|
64
|
+
}
|
|
65
|
+
if (timeout_ms !== undefined) {
|
|
66
|
+
if (!isSafeInteger(timeout_ms) || timeout_ms === 0 || timeout_ms > MAX_TIMEOUT_MS) {
|
|
67
|
+
throw new CodemodeSourceError(`@options field \`timeout_ms\` must be a positive integer up to ${MAX_TIMEOUT_MS}`);
|
|
68
|
+
}
|
|
69
|
+
options.timeoutMs = timeout_ms;
|
|
70
|
+
}
|
|
71
|
+
return options;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Split an optional first-line `// @options: {...}` from the script. Throws
|
|
75
|
+
* {@link CodemodeSourceError} for empty input and invalid options.
|
|
76
|
+
*/
|
|
77
|
+
export function parseCodemodeSource(input) {
|
|
78
|
+
if (input.trim() === "") {
|
|
79
|
+
throw new CodemodeSourceError('Expected JavaScript source text (non-empty). Provide JS only, optionally with a first line `// @options: {"max_output_tokens": 1000}`.');
|
|
80
|
+
}
|
|
81
|
+
const newline = input.indexOf("\n");
|
|
82
|
+
const firstLine = (newline === -1 ? input : input.slice(0, newline)).replace(/\r$/, "");
|
|
83
|
+
const trimmed = firstLine.trimStart();
|
|
84
|
+
if (!trimmed.startsWith(CODEMODE_OPTIONS_PREFIX))
|
|
85
|
+
return { code: input, options: {} };
|
|
86
|
+
const code = newline === -1 ? "" : input.slice(newline);
|
|
87
|
+
if (code.trim() === "") {
|
|
88
|
+
throw new CodemodeSourceError("The @options line must be followed by JavaScript source on subsequent lines");
|
|
89
|
+
}
|
|
90
|
+
return { code, options: parseOptions(trimmed.slice(CODEMODE_OPTIONS_PREFIX.length).trim()) };
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=source.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"source.js","sourceRoot":"","sources":["../src/source.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,CAAC,MAAM,uBAAuB,GAAG,cAAc,CAAC;AAEtD,MAAM,gBAAgB,GAAG,CAAC,mBAAmB,EAAE,YAAY,CA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EAAE,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IACxF,MAAM,OAAO,GAAG,SAAS,CAAC,SAAS,EAAE,CAAC;IACtC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,uBAAuB,CAAC;QAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;IACtF,MAAM,IAAI,GAAG,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACxD,IAAI,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QACxB,MAAM,IAAI,mBAAmB,CAAC,6EAA6E,CAAC,CAAC;IAC9G,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,YAAY,CAAC,OAAO,CAAC,KAAK,CAAC,uBAAuB,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC;AAC9F,CAAC","sourcesContent":["/**\n * Codemode source format: JavaScript, optionally preceded by one options line.\n *\n * ```js\n * // @options: {\"max_output_tokens\": 2000, \"timeout_ms\": 30000}\n * const text = await tools.read({ path: \"package.json\" });\n * text(JSON.parse(text).name);\n * ```\n */\n\nexport const CODEMODE_OPTIONS_PREFIX = \"// @options:\";\n\nconst SUPPORTED_FIELDS = [\"max_output_tokens\", \"timeout_ms\"] as const;\nconst SUPPORTED_FIELDS_TEXT = \"`max_output_tokens` and `timeout_ms`\";\n/** Largest delay `setTimeout` supports, which bounds `timeout_ms`. */\nconst MAX_TIMEOUT_MS = 2_147_483_647;\n\n/**\n * Lark grammar for providers with grammar-constrained tool input. It only fixes the shape of the\n * options line; the options JSON and the code are checked by {@link parseCodemodeSource}.\n */\nexport const CODEMODE_SOURCE_GRAMMAR = String.raw`\nstart: options_source | plain_source\noptions_source: OPTIONS_LINE NEWLINE SOURCE\nplain_source: SOURCE\n\nOPTIONS_LINE: /[ \\t]*\\/\\/ @options:[^\\r\\n]*/\nNEWLINE: /\\r?\\n/\nSOURCE: /[\\s\\S]+/\n`;\n\nexport interface CodemodeSourceOptions {\n\t/** Token budget for the script's output. */\n\tmaxOutputTokens?: number;\n\t/** Hard deadline for the whole script in milliseconds, including tool calls. */\n\ttimeoutMs?: number;\n}\n\nexport interface ParsedCodemodeSource {\n\t/** The script with the options line replaced by an empty line, so line numbers are unchanged. */\n\tcode: string;\n\toptions: CodemodeSourceOptions;\n}\n\nexport class CodemodeSourceError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"CodemodeSourceError\";\n\t}\n}\n\nfunction isSafeInteger(value: unknown): value is number {\n\treturn typeof value === \"number\" && Number.isSafeInteger(value) && value >= 0;\n}\n\nfunction parseOptions(directive: string): CodemodeSourceOptions {\n\tif (directive === \"\") {\n\t\tthrow new CodemodeSourceError(`@options must be a JSON object with supported fields ${SUPPORTED_FIELDS_TEXT}`);\n\t}\n\tlet value: unknown;\n\ttry {\n\t\tvalue = JSON.parse(directive);\n\t} catch (error) {\n\t\tthrow new CodemodeSourceError(\n\t\t\t`@options must be valid JSON with supported fields ${SUPPORTED_FIELDS_TEXT}: ${error instanceof Error ? error.message : String(error)}`,\n\t\t);\n\t}\n\tif (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n\t\tthrow new CodemodeSourceError(`@options must be a JSON object with supported fields ${SUPPORTED_FIELDS_TEXT}`);\n\t}\n\tconst fields = value as Record<string, unknown>;\n\tfor (const key of Object.keys(fields)) {\n\t\tif (!(SUPPORTED_FIELDS as readonly string[]).includes(key)) {\n\t\t\tthrow new CodemodeSourceError(`@options only supports ${SUPPORTED_FIELDS_TEXT}; got \\`${key}\\``);\n\t\t}\n\t}\n\tconst options: CodemodeSourceOptions = {};\n\tconst { max_output_tokens, timeout_ms } = fields;\n\tif (max_output_tokens !== undefined) {\n\t\tif (!isSafeInteger(max_output_tokens)) {\n\t\t\tthrow new CodemodeSourceError(\"@options field `max_output_tokens` must be a non-negative safe integer\");\n\t\t}\n\t\toptions.maxOutputTokens = max_output_tokens;\n\t}\n\tif (timeout_ms !== undefined) {\n\t\tif (!isSafeInteger(timeout_ms) || timeout_ms === 0 || timeout_ms > MAX_TIMEOUT_MS) {\n\t\t\tthrow new CodemodeSourceError(\n\t\t\t\t`@options field \\`timeout_ms\\` must be a positive integer up to ${MAX_TIMEOUT_MS}`,\n\t\t\t);\n\t\t}\n\t\toptions.timeoutMs = timeout_ms;\n\t}\n\treturn options;\n}\n\n/**\n * Split an optional first-line `// @options: {...}` from the script. Throws\n * {@link CodemodeSourceError} for empty input and invalid options.\n */\nexport function parseCodemodeSource(input: string): ParsedCodemodeSource {\n\tif (input.trim() === \"\") {\n\t\tthrow new CodemodeSourceError(\n\t\t\t'Expected JavaScript source text (non-empty). Provide JS only, optionally with a first line `// @options: {\"max_output_tokens\": 1000}`.',\n\t\t);\n\t}\n\tconst newline = input.indexOf(\"\\n\");\n\tconst firstLine = (newline === -1 ? input : input.slice(0, newline)).replace(/\\r$/, \"\");\n\tconst trimmed = firstLine.trimStart();\n\tif (!trimmed.startsWith(CODEMODE_OPTIONS_PREFIX)) return { code: input, options: {} };\n\tconst code = newline === -1 ? \"\" : input.slice(newline);\n\tif (code.trim() === \"\") {\n\t\tthrow new CodemodeSourceError(\"The @options line must be followed by JavaScript source on subsequent lines\");\n\t}\n\treturn { code, options: parseOptions(trimmed.slice(CODEMODE_OPTIONS_PREFIX.length).trim()) };\n}\n"]}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { CodemodeWasmModule } from "./wasm.ts";
|
|
2
|
+
export interface CodemodeToolContext {
|
|
3
|
+
/**
|
|
4
|
+
* Aborted when the script finishes (including unawaited calls), the
|
|
5
|
+
* execution times out, the caller aborts, or the sandbox is closed.
|
|
6
|
+
*/
|
|
7
|
+
signal: AbortSignal;
|
|
8
|
+
}
|
|
9
|
+
/** A JSON Schema document. Only used to render declarations; values are not validated against it. */
|
|
10
|
+
export type CodemodeJsonSchema = {
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
} | boolean;
|
|
13
|
+
export interface CodemodeTool {
|
|
14
|
+
/**
|
|
15
|
+
* The script calls tools as `tools.<id>(args)`, where `<id>` is the name with characters that
|
|
16
|
+
* are not valid in identifiers replaced by `_` (see `toCodemodeIdentifier`), and also as
|
|
17
|
+
* `tools["<name>"](args)`. Globals are called as `<name>(args)` and must be identifiers, or
|
|
18
|
+
* `<namespace>.<member>`, which groups them into a frozen namespace object.
|
|
19
|
+
*/
|
|
20
|
+
name: string;
|
|
21
|
+
/** Shown as a doc comment in {@link renderDeclarations}, and listed in `ALL_TOOLS` for tools. */
|
|
22
|
+
description?: string;
|
|
23
|
+
/** Schema of the single argument. Rendered as the parameter type; `unknown` when omitted. */
|
|
24
|
+
inputSchema?: CodemodeJsonSchema;
|
|
25
|
+
/** Schema of the resolved value. Rendered as the promise type; `unknown` when omitted. */
|
|
26
|
+
outputSchema?: CodemodeJsonSchema;
|
|
27
|
+
/** Globals only: `execute` receives all call arguments as an array instead of the first one. */
|
|
28
|
+
spread?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Globals only: TypeScript parameter list and return type for {@link renderDeclarations}, for
|
|
31
|
+
* example `(type: string, id?: string): Promise<Model[]>`. Replaces the rendering from the schemas.
|
|
32
|
+
*/
|
|
33
|
+
signature?: string;
|
|
34
|
+
/**
|
|
35
|
+
* `args` is whatever the script passed, after a JSON round trip. The return
|
|
36
|
+
* value must be JSON-serializable; a thrown error surfaces in the script as
|
|
37
|
+
* an `Error` with the same message.
|
|
38
|
+
*/
|
|
39
|
+
execute(args: unknown, context: CodemodeToolContext): Promise<unknown> | unknown;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* One item of the script's output, in the order the script produced it: `text()` and `console.*`
|
|
43
|
+
* produce text items, with `console: true` for `console.*`, and `image()` image items. `data` is base64.
|
|
44
|
+
*/
|
|
45
|
+
export type CodemodeOutputItem = {
|
|
46
|
+
type: "text";
|
|
47
|
+
text: string;
|
|
48
|
+
console?: true;
|
|
49
|
+
} | {
|
|
50
|
+
type: "image";
|
|
51
|
+
data: string;
|
|
52
|
+
mimeType: string;
|
|
53
|
+
};
|
|
54
|
+
export type CodemodeCallStatus = "ok" | "error" | "cancelled";
|
|
55
|
+
export interface CodemodeCall {
|
|
56
|
+
name: string;
|
|
57
|
+
status: CodemodeCallStatus;
|
|
58
|
+
durationMs: number;
|
|
59
|
+
}
|
|
60
|
+
export type CodemodeErrorKind =
|
|
61
|
+
/** The script threw or failed to parse. `name` and `stack` come from the script's error. */
|
|
62
|
+
"script"
|
|
63
|
+
/**
|
|
64
|
+
* The overall deadline expired, or a VM on the calling thread used up its interrupt budget. The VM
|
|
65
|
+
* was stopped.
|
|
66
|
+
*/
|
|
67
|
+
| "timeout"
|
|
68
|
+
/** The caller's signal fired or the sandbox was closed. The VM was stopped. */
|
|
69
|
+
| "aborted"
|
|
70
|
+
/**
|
|
71
|
+
* The VM or its transport failed outside the script's control (for example a wasm trap, a missing
|
|
72
|
+
* worker file, or a remote sandbox that failed).
|
|
73
|
+
*/
|
|
74
|
+
| "sandbox";
|
|
75
|
+
export interface CodemodeError {
|
|
76
|
+
kind: CodemodeErrorKind;
|
|
77
|
+
name?: string;
|
|
78
|
+
message: string;
|
|
79
|
+
stack?: string;
|
|
80
|
+
}
|
|
81
|
+
/** Keys the script changed with `store()`. Only successful executions report writes. */
|
|
82
|
+
export interface CodemodeStoreWrites {
|
|
83
|
+
set: Record<string, unknown>;
|
|
84
|
+
/** Keys stored as `undefined`. */
|
|
85
|
+
delete: string[];
|
|
86
|
+
}
|
|
87
|
+
/** `output` is kept for failed executions too, up to the failure. `exit()` completes with `value: undefined`. */
|
|
88
|
+
export type CodemodeResult = {
|
|
89
|
+
ok: true;
|
|
90
|
+
value: unknown;
|
|
91
|
+
output: CodemodeOutputItem[];
|
|
92
|
+
calls: CodemodeCall[];
|
|
93
|
+
storeWrites: CodemodeStoreWrites;
|
|
94
|
+
} | {
|
|
95
|
+
ok: false;
|
|
96
|
+
error: CodemodeError;
|
|
97
|
+
output: CodemodeOutputItem[];
|
|
98
|
+
calls: CodemodeCall[];
|
|
99
|
+
};
|
|
100
|
+
/** Options every sandbox accepts, wherever its VM runs. */
|
|
101
|
+
export interface CodemodeSandboxBaseOptions {
|
|
102
|
+
tools?: CodemodeTool[];
|
|
103
|
+
/**
|
|
104
|
+
* Functions exposed as top-level identifiers instead of on `tools`, for host helpers such as
|
|
105
|
+
* attaching an image to the result. They behave like tools (JSON round trip, promise result)
|
|
106
|
+
* but are not recorded in `result.calls`. Names must be identifiers and may not shadow the
|
|
107
|
+
* built-in globals (`tools`, `ALL_TOOLS`, `console`, `text`, `image`, `exit`, `store`, `load`).
|
|
108
|
+
*/
|
|
109
|
+
globals?: CodemodeTool[];
|
|
110
|
+
/**
|
|
111
|
+
* Overall deadline per execution, including time spent in tools. `Infinity` disables the
|
|
112
|
+
* deadline; the execution then only ends when the script settles or is aborted.
|
|
113
|
+
* Default: 300000.
|
|
114
|
+
*/
|
|
115
|
+
timeoutMs?: number;
|
|
116
|
+
/**
|
|
117
|
+
* Maximum memory the QuickJS VM may allocate. Allocations beyond it fail inside the script as
|
|
118
|
+
* `InternalError: out of memory`. Default: no limit beyond wasm32's 4 GiB address space.
|
|
119
|
+
*/
|
|
120
|
+
memoryLimitBytes?: number;
|
|
121
|
+
}
|
|
122
|
+
/** Options of `CodemodeSandbox`, which runs each VM in a Node or Bun worker thread. */
|
|
123
|
+
export interface CodemodeSandboxOptions extends CodemodeSandboxBaseOptions {
|
|
124
|
+
/**
|
|
125
|
+
* Compiled `quickjs-wasi/quickjs.wasm`, usually from {@link loadQuickJSWasm}. Default:
|
|
126
|
+
* `loadQuickJSWasm()`, the file in the installed `quickjs-wasi` package. Pass it when that file
|
|
127
|
+
* is not on disk, for example in a Bun compiled executable.
|
|
128
|
+
*/
|
|
129
|
+
wasm?: CodemodeWasmModule | Promise<CodemodeWasmModule>;
|
|
130
|
+
/**
|
|
131
|
+
* Worker entry that imports `@ny4n/codemode/worker`. Default: this package's own
|
|
132
|
+
* worker file. Pass it when this package is bundled, since the default is resolved relative to
|
|
133
|
+
* the module that creates the sandbox. Bun compiled executables require the relative string
|
|
134
|
+
* specifier of an embedded build entrypoint; other hosts usually use a URL.
|
|
135
|
+
*/
|
|
136
|
+
workerUrl?: string | URL;
|
|
137
|
+
}
|
|
138
|
+
export interface CodemodeExecuteOptions {
|
|
139
|
+
signal?: AbortSignal;
|
|
140
|
+
/** Overrides the sandbox default for this execution. */
|
|
141
|
+
timeoutMs?: number;
|
|
142
|
+
/**
|
|
143
|
+
* Values the script reads with `load(key)`. Must be JSON-serializable. The script's own
|
|
144
|
+
* `store()` calls come back as `result.storeWrites`; persisting them is up to the caller.
|
|
145
|
+
*/
|
|
146
|
+
store?: Readonly<Record<string, unknown>>;
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAEpD,MAAM,WAAW,mBAAmB;IACnC;;;OAGG;IACH,MAAM,EAAE,WAAW,CAAC;CACpB;AAED,qGAAqG;AACrG,MAAM,MAAM,kBAAkB,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,GAAG,OAAO,CAAC;AAEtE,MAAM,WAAW,YAAY;IAC5B;;;;;OAKG;IACH,IAAI,EAAE,MAAM,CAAC;IACb,iGAAiG;IACjG,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,6FAA6F;IAC7F,WAAW,CAAC,EAAE,kBAAkB,CAAC;IACjC,0FAA0F;IAC1F,YAAY,CAAC,EAAE,kBAAkB,CAAC;IA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,CAAC;IACf,MAAM,EAAE,kBAAkB,EAAE,CAAC;IAC7B,KAAK,EAAE,YAAY,EAAE,CAAC;IACtB,WAAW,EAAE,mBAAmB,CAAC;CAChC,GACD;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,aAAa,CAAC;IAAC,MAAM,EAAE,kBAAkB,EAAE,CAAC;IAAC,KAAK,EAAE,YAAY,EAAE,CAAA;CAAE,CAAC;AAE5F,2DAA2D;AAC3D,MAAM,WAAW,0BAA0B;IAC1C,KAAK,CAAC,EAAE,YAAY,EAAE,CAAC;IACvB;;;;;OAKG;IACH,OAAO,CAAC,EAAE,YAAY,EAAE,CAAC;IACzB;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,uFAAuF;AACvF,MAAM,WAAW,sBAAuB,SAAQ,0BAA0B;IACzE;;;;OAIG;IACH,IAAI,CAAC,EAAE,kBAAkB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC;IACxD;;;;;OAKG;IACH,SAAS,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;CACzB;AAED,MAAM,WAAW,sBAAsB;IACtC,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,wDAAwD;IACxD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,KAAK,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CAC1C"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { CodemodeWasmModule } from \"./wasm.ts\";\n\nexport interface CodemodeToolContext {\n\t/**\n\t * Aborted when the script finishes (including unawaited calls), the\n\t * execution times out, the caller aborts, or the sandbox is closed.\n\t */\n\tsignal: AbortSignal;\n}\n\n/** A JSON Schema document. Only used to render declarations; values are not validated against it. */\nexport type CodemodeJsonSchema = { [key: string]: unknown } | boolean;\n\nexport interface CodemodeTool {\n\t/**\n\t * The script calls tools as `tools.<id>(args)`, where `<id>` is the name with characters that\n\t * are not valid in identifiers replaced by `_` (see `toCodemodeIdentifier`), and also as\n\t * `tools[\"<name>\"](args)`. Globals are called as `<name>(args)` and must be identifiers, or\n\t * `<namespace>.<member>`, which groups them into a frozen namespace object.\n\t */\n\tname: string;\n\t/** Shown as a doc comment in {@link renderDeclarations}, and listed in `ALL_TOOLS` for tools. */\n\tdescription?: string;\n\t/** Schema of the single argument. Rendered as the parameter type; `unknown` when omitted. */\n\tinputSchema?: CodemodeJsonSchema;\n\t/** Schema of the resolved value. Rendered as the promise type; `unknown` when omitted. */\n\toutputSchema?: CodemodeJsonSchema;\n\t/** Globals only: `execute` receives all call arguments as an array instead of the first one. */\n\tspread?: boolean;\n\t/**\n\t * Globals only: TypeScript parameter list and return type for {@link renderDeclarations}, for\n\t * example `(type: string, id?: string): Promise<Model[]>`. Replaces the rendering from the schemas.\n\t */\n\tsignature?: string;\n\t/**\n\t * `args` is whatever the script passed, after a JSON round trip. The return\n\t * value must be JSON-serializable; a thrown error surfaces in the script as\n\t * an `Error` with the same message.\n\t */\n\texecute(args: unknown, context: CodemodeToolContext): Promise<unknown> | unknown;\n}\n\n/**\n * One item of the script's output, in the order the script produced it: `text()` and `console.*`\n * produce text items, with `console: true` for `console.*`, and `image()` image items. `data` is base64.\n */\nexport type CodemodeOutputItem =\n\t| { type: \"text\"; text: string; console?: true }\n\t| { type: \"image\"; data: string; mimeType: string };\n\nexport type CodemodeCallStatus = \"ok\" | \"error\" | \"cancelled\";\n\nexport interface CodemodeCall {\n\tname: string;\n\tstatus: CodemodeCallStatus;\n\tdurationMs: number;\n}\n\nexport type CodemodeErrorKind =\n\t/** The script threw or failed to parse. `name` and `stack` come from the script's error. */\n\t| \"script\"\n\t/**\n\t * The overall deadline expired, or a VM on the calling thread used up its interrupt budget. The VM\n\t * was stopped.\n\t */\n\t| \"timeout\"\n\t/** The caller's signal fired or the sandbox was closed. The VM was stopped. */\n\t| \"aborted\"\n\t/**\n\t * The VM or its transport failed outside the script's control (for example a wasm trap, a missing\n\t * worker file, or a remote sandbox that failed).\n\t */\n\t| \"sandbox\";\n\nexport interface CodemodeError {\n\tkind: CodemodeErrorKind;\n\tname?: string;\n\tmessage: string;\n\tstack?: string;\n}\n\n/** Keys the script changed with `store()`. Only successful executions report writes. */\nexport interface CodemodeStoreWrites {\n\tset: Record<string, unknown>;\n\t/** Keys stored as `undefined`. */\n\tdelete: string[];\n}\n\n/** `output` is kept for failed executions too, up to the failure. `exit()` completes with `value: undefined`. */\nexport type CodemodeResult =\n\t| {\n\t\t\tok: true;\n\t\t\tvalue: unknown;\n\t\t\toutput: CodemodeOutputItem[];\n\t\t\tcalls: CodemodeCall[];\n\t\t\tstoreWrites: CodemodeStoreWrites;\n\t }\n\t| { ok: false; error: CodemodeError; output: CodemodeOutputItem[]; calls: CodemodeCall[] };\n\n/** Options every sandbox accepts, wherever its VM runs. */\nexport interface CodemodeSandboxBaseOptions {\n\ttools?: CodemodeTool[];\n\t/**\n\t * Functions exposed as top-level identifiers instead of on `tools`, for host helpers such as\n\t * attaching an image to the result. They behave like tools (JSON round trip, promise result)\n\t * but are not recorded in `result.calls`. Names must be identifiers and may not shadow the\n\t * built-in globals (`tools`, `ALL_TOOLS`, `console`, `text`, `image`, `exit`, `store`, `load`).\n\t */\n\tglobals?: CodemodeTool[];\n\t/**\n\t * Overall deadline per execution, including time spent in tools. `Infinity` disables the\n\t * deadline; the execution then only ends when the script settles or is aborted.\n\t * Default: 300000.\n\t */\n\ttimeoutMs?: number;\n\t/**\n\t * Maximum memory the QuickJS VM may allocate. Allocations beyond it fail inside the script as\n\t * `InternalError: out of memory`. Default: no limit beyond wasm32's 4 GiB address space.\n\t */\n\tmemoryLimitBytes?: number;\n}\n\n/** Options of `CodemodeSandbox`, which runs each VM in a Node or Bun worker thread. */\nexport interface CodemodeSandboxOptions extends CodemodeSandboxBaseOptions {\n\t/**\n\t * Compiled `quickjs-wasi/quickjs.wasm`, usually from {@link loadQuickJSWasm}. Default:\n\t * `loadQuickJSWasm()`, the file in the installed `quickjs-wasi` package. Pass it when that file\n\t * is not on disk, for example in a Bun compiled executable.\n\t */\n\twasm?: CodemodeWasmModule | Promise<CodemodeWasmModule>;\n\t/**\n\t * Worker entry that imports `@ny4n/codemode/worker`. Default: this package's own\n\t * worker file. Pass it when this package is bundled, since the default is resolved relative to\n\t * the module that creates the sandbox. Bun compiled executables require the relative string\n\t * specifier of an embedded build entrypoint; other hosts usually use a URL.\n\t */\n\tworkerUrl?: string | URL;\n}\n\nexport interface CodemodeExecuteOptions {\n\tsignal?: AbortSignal;\n\t/** Overrides the sandbox default for this execution. */\n\ttimeoutMs?: number;\n\t/**\n\t * Values the script reads with `load(key)`. Must be JSON-serializable. The script's own\n\t * `store()` calls come back as `result.storeWrites`; persisting them is up to the caller.\n\t */\n\tstore?: Readonly<Record<string, unknown>>;\n}\n"]}
|
package/dist/wasm.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A compiled `WebAssembly.Module` of `quickjs-wasi/quickjs.wasm`. Typed opaquely because the Node
|
|
3
|
+
* type definitions do not declare the WebAssembly globals (they live in the DOM lib).
|
|
4
|
+
*/
|
|
5
|
+
export type CodemodeWasmModule = object;
|
|
6
|
+
/**
|
|
7
|
+
* Read and compile the QuickJS wasm once per path. `path` defaults to the file in the installed
|
|
8
|
+
* `quickjs-wasi` package; pass it when that file lives elsewhere, for example embedded in a Bun
|
|
9
|
+
* compiled executable. A failed load is retried on the next call.
|
|
10
|
+
*/
|
|
11
|
+
export declare function loadQuickJSWasm(path?: string): Promise<CodemodeWasmModule>;
|
|
12
|
+
//# sourceMappingURL=wasm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wasm.d.ts","sourceRoot":"","sources":["../src/wasm.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAQxC;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAc1E"}
|
package/dist/wasm.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
const modules = new Map();
|
|
4
|
+
/**
|
|
5
|
+
* Read and compile the QuickJS wasm once per path. `path` defaults to the file in the installed
|
|
6
|
+
* `quickjs-wasi` package; pass it when that file lives elsewhere, for example embedded in a Bun
|
|
7
|
+
* compiled executable. A failed load is retried on the next call.
|
|
8
|
+
*/
|
|
9
|
+
export function loadQuickJSWasm(path) {
|
|
10
|
+
const resolved = path ?? createRequire(import.meta.url).resolve("quickjs-wasi/quickjs.wasm");
|
|
11
|
+
let module = modules.get(resolved);
|
|
12
|
+
if (!module) {
|
|
13
|
+
const { WebAssembly } = globalThis;
|
|
14
|
+
module = readFile(resolved)
|
|
15
|
+
.then((bytes) => WebAssembly.compile(bytes))
|
|
16
|
+
.catch((error) => {
|
|
17
|
+
modules.delete(resolved);
|
|
18
|
+
throw error;
|
|
19
|
+
});
|
|
20
|
+
modules.set(resolved, module);
|
|
21
|
+
}
|
|
22
|
+
return module;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=wasm.js.map
|
package/dist/wasm.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wasm.js","sourceRoot":"","sources":["../src/wasm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAY5C,MAAM,OAAO,GAAG,IAAI,GAAG,EAAuC,CAAC;AAE/D;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,IAAa;IAC5C,MAAM,QAAQ,GAAG,IAAI,IAAI,aAAa,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,2BAA2B,CAAC,CAAC;IAC7F,IAAI,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACnC,IAAI,CAAC,MAAM,EAAE,CAAC;QACb,MAAM,EAAE,WAAW,EAAE,GAAG,UAA2D,CAAC;QACpF,MAAM,GAAG,QAAQ,CAAC,QAAQ,CAAC;aACzB,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,WAAW,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;aAC3C,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;YACzB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YACzB,MAAM,KAAK,CAAC;QACb,CAAC,CAAC,CAAC;QACJ,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IAC/B,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC","sourcesContent":["import { readFile } from \"node:fs/promises\";\nimport { createRequire } from \"node:module\";\n\n/**\n * A compiled `WebAssembly.Module` of `quickjs-wasi/quickjs.wasm`. Typed opaquely because the Node\n * type definitions do not declare the WebAssembly globals (they live in the DOM lib).\n */\nexport type CodemodeWasmModule = object;\n\ninterface WebAssemblyGlobal {\n\tcompile(bytes: Uint8Array): Promise<CodemodeWasmModule>;\n}\n\nconst modules = new Map<string, Promise<CodemodeWasmModule>>();\n\n/**\n * Read and compile the QuickJS wasm once per path. `path` defaults to the file in the installed\n * `quickjs-wasi` package; pass it when that file lives elsewhere, for example embedded in a Bun\n * compiled executable. A failed load is retried on the next call.\n */\nexport function loadQuickJSWasm(path?: string): Promise<CodemodeWasmModule> {\n\tconst resolved = path ?? createRequire(import.meta.url).resolve(\"quickjs-wasi/quickjs.wasm\");\n\tlet module = modules.get(resolved);\n\tif (!module) {\n\t\tconst { WebAssembly } = globalThis as unknown as { WebAssembly: WebAssemblyGlobal };\n\t\tmodule = readFile(resolved)\n\t\t\t.then((bytes) => WebAssembly.compile(bytes))\n\t\t\t.catch((error: unknown) => {\n\t\t\t\tmodules.delete(resolved);\n\t\t\t\tthrow error;\n\t\t\t});\n\t\tmodules.set(resolved, module);\n\t}\n\treturn module;\n}\n"]}
|