@opencode/codemode 2.2.5 → 2.2.7
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/README.md +164 -7
- package/dist/codemode.d.ts +60 -4
- package/dist/codemode.js +63 -8
- package/dist/extension.d.ts +27 -9
- package/dist/extension.js +7 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/interpreter/callback.d.ts +12 -1
- package/dist/interpreter/callback.js +13 -1
- package/dist/interpreter/execute.d.ts +1 -1
- package/dist/interpreter/execute.js +16 -4
- package/dist/interpreter/extensions.d.ts +3 -3
- package/dist/interpreter/extensions.js +17 -11
- package/dist/interpreter/globals.js +10 -0
- package/dist/interpreter/interpreter.d.ts +11 -1
- package/dist/interpreter/interpreter.js +88 -4
- package/dist/interpreter/modules.d.ts +4 -0
- package/dist/interpreter/modules.js +33 -0
- package/dist/interpreter/promises.js +2 -2
- package/dist/interpreter/runner.d.ts +1 -11
- package/dist/interpreter/runner.js +1 -13
- package/dist/stdlib/iterator.js +1 -2
- package/dist/suspend-error.d.ts +24 -0
- package/dist/suspend-error.js +25 -0
- package/dist/tool-runtime.d.ts +31 -7
- package/dist/tool-runtime.js +116 -26
- package/dist/tool.d.ts +45 -2
- package/dist/tool.js +30 -7
- package/dist/vm/async.d.ts +78 -0
- package/dist/vm/async.js +335 -0
- package/dist/vm/code.d.ts +122 -0
- package/dist/vm/code.js +407 -0
- package/dist/vm/compiler/compile.d.ts +13 -0
- package/dist/vm/compiler/compile.js +1477 -0
- package/dist/vm/compiler/scope.d.ts +74 -0
- package/dist/vm/compiler/scope.js +515 -0
- package/dist/vm/data.d.ts +7 -0
- package/dist/vm/data.js +13 -0
- package/dist/vm/generators.d.ts +42 -0
- package/dist/vm/generators.js +413 -0
- package/dist/vm/host.d.ts +106 -0
- package/dist/vm/host.js +231 -0
- package/dist/vm/index.d.ts +6 -0
- package/dist/vm/index.js +6 -0
- package/dist/vm/internal-prelude.d.ts +45 -0
- package/dist/vm/internal-prelude.js +534 -0
- package/dist/vm/opcodes.d.ts +222 -0
- package/dist/vm/opcodes.js +235 -0
- package/dist/vm/prelude/bytecode.d.ts +18 -0
- package/dist/vm/prelude/bytecode.js +31 -0
- package/dist/vm/prelude/bytecode.json +1 -0
- package/dist/vm/prelude/helpers.d.ts +8 -0
- package/dist/vm/prelude/helpers.js +258 -0
- package/dist/vm/prelude/prelude.d.ts +21 -0
- package/dist/vm/prelude/prelude.js +53 -0
- package/dist/vm/prelude/source.d.ts +1 -0
- package/dist/vm/prelude/source.js +652 -0
- package/dist/vm/runtime/callback.d.ts +25 -0
- package/dist/vm/runtime/callback.js +51 -0
- package/dist/vm/runtime/driver.d.ts +68 -0
- package/dist/vm/runtime/driver.js +254 -0
- package/dist/vm/runtime/errors.d.ts +23 -0
- package/dist/vm/runtime/errors.js +169 -0
- package/dist/vm/runtime/execute.d.ts +80 -0
- package/dist/vm/runtime/execute.js +442 -0
- package/dist/vm/runtime/extensions.d.ts +16 -0
- package/dist/vm/runtime/extensions.js +337 -0
- package/dist/vm/runtime/generators.d.ts +6 -0
- package/dist/vm/runtime/generators.js +44 -0
- package/dist/vm/runtime/globals.d.ts +6 -0
- package/dist/vm/runtime/globals.js +137 -0
- package/dist/vm/runtime/interpreter.d.ts +53 -0
- package/dist/vm/runtime/interpreter.js +96 -0
- package/dist/vm/runtime/intrinsic-names.d.ts +56 -0
- package/dist/vm/runtime/intrinsic-names.js +166 -0
- package/dist/vm/runtime/intrinsics.d.ts +15 -0
- package/dist/vm/runtime/intrinsics.js +111 -0
- package/dist/vm/runtime/limits.d.ts +10 -0
- package/dist/vm/runtime/limits.js +19 -0
- package/dist/vm/runtime/model.d.ts +58 -0
- package/dist/vm/runtime/model.js +57 -0
- package/dist/vm/runtime/modules.d.ts +4 -0
- package/dist/vm/runtime/modules.js +33 -0
- package/dist/vm/runtime/native.d.ts +19 -0
- package/dist/vm/runtime/native.js +41 -0
- package/dist/vm/runtime/objects.d.ts +403 -0
- package/dist/vm/runtime/objects.js +784 -0
- package/dist/vm/runtime/operations.d.ts +49 -0
- package/dist/vm/runtime/operations.js +288 -0
- package/dist/vm/runtime/promises.d.ts +55 -0
- package/dist/vm/runtime/promises.js +232 -0
- package/dist/vm/runtime/references.d.ts +8 -0
- package/dist/vm/runtime/references.js +51 -0
- package/dist/vm/runtime/signals.d.ts +69 -0
- package/dist/vm/runtime/signals.js +68 -0
- package/dist/vm/runtime/source.d.ts +29 -0
- package/dist/vm/runtime/source.js +63 -0
- package/dist/vm/runtime/table.d.ts +43 -0
- package/dist/vm/runtime/table.js +132 -0
- package/dist/vm/scheduler.d.ts +190 -0
- package/dist/vm/scheduler.js +74 -0
- package/dist/vm/snapshot/codec.d.ts +117 -0
- package/dist/vm/snapshot/codec.js +848 -0
- package/dist/vm/snapshot/snapshot.d.ts +53 -0
- package/dist/vm/snapshot/snapshot.js +74 -0
- package/dist/vm/snapshot.d.ts +32 -0
- package/dist/vm/snapshot.js +309 -0
- package/dist/vm/stdlib/array.d.ts +16 -0
- package/dist/vm/stdlib/array.js +311 -0
- package/dist/vm/stdlib/bytes.d.ts +11 -0
- package/dist/vm/stdlib/bytes.js +222 -0
- package/dist/vm/stdlib/collections.d.ts +7 -0
- package/dist/vm/stdlib/collections.js +188 -0
- package/dist/vm/stdlib/console.d.ts +6 -0
- package/dist/vm/stdlib/console.js +93 -0
- package/dist/vm/stdlib/date.d.ts +2 -0
- package/dist/vm/stdlib/date.js +123 -0
- package/dist/vm/stdlib/headers.d.ts +2 -0
- package/dist/vm/stdlib/headers.js +92 -0
- package/dist/vm/stdlib/iterator.d.ts +2 -0
- package/dist/vm/stdlib/iterator.js +30 -0
- package/dist/vm/stdlib/json.d.ts +3 -0
- package/dist/vm/stdlib/json.js +42 -0
- package/dist/vm/stdlib/math.d.ts +3 -0
- package/dist/vm/stdlib/math.js +73 -0
- package/dist/vm/stdlib/number.d.ts +3 -0
- package/dist/vm/stdlib/number.js +72 -0
- package/dist/vm/stdlib/object.d.ts +6 -0
- package/dist/vm/stdlib/object.js +201 -0
- package/dist/vm/stdlib/regexp.d.ts +8 -0
- package/dist/vm/stdlib/regexp.js +119 -0
- package/dist/vm/stdlib/string.d.ts +2 -0
- package/dist/vm/stdlib/string.js +272 -0
- package/dist/vm/stdlib/url.d.ts +25 -0
- package/dist/vm/stdlib/url.js +255 -0
- package/dist/vm/stdlib/value.d.ts +6 -0
- package/dist/vm/stdlib/value.js +32 -0
- package/dist/vm/stdlib/web.d.ts +5 -0
- package/dist/vm/stdlib/web.js +97 -0
- package/dist/vm/vm.d.ts +226 -0
- package/dist/vm/vm.js +2931 -0
- package/package.json +8 -5
package/README.md
CHANGED
|
@@ -14,7 +14,8 @@ The idea of code mode was originally introduced by Cloudflare. See
|
|
|
14
14
|
## How it differs from JavaScript
|
|
15
15
|
|
|
16
16
|
- **Only supported APIs are available.** Programs can use the provided tools, supported JavaScript built-ins, and the
|
|
17
|
-
globals the host adds through extensions. Timers, `process`, filesystem access,
|
|
17
|
+
globals the host adds through extensions. Timers, `process`, filesystem access, packages, and `export` are
|
|
18
|
+
unavailable.
|
|
18
19
|
- **Unfinished work is interrupted.** Tool calls and async functions start when called. When the program finishes,
|
|
19
20
|
anything still running is interrupted. Unhandled rejections from un-awaited promises are returned as warnings.
|
|
20
21
|
- **REPL-style results.** Without an explicit `return`, the final top-level expression becomes the result. `undefined`
|
|
@@ -89,12 +90,123 @@ runtime.execute(source) // Effect<CodeMode.Result, never, ToolServices>
|
|
|
89
90
|
```
|
|
90
91
|
|
|
91
92
|
The Effect environment is inferred from the supplied tools. `hooks` surround every call the program makes into the
|
|
92
|
-
host: `tool.before`/`tool.after` receive `{ name, input }` with decoded input
|
|
93
|
-
receive `{ extension, name, args }`. An `after` hook also
|
|
94
|
-
`failure` with its error, or `interrupted`). A `before` hook
|
|
95
|
-
denies it: the host function is never called, and the program
|
|
96
|
-
fails the way the call itself would: a tool call or asynchronous
|
|
97
|
-
throws.
|
|
93
|
+
host: `tool.before`/`tool.after` receive `{ name, input, key }` with decoded input and the call's `<runId>:<callId>`
|
|
94
|
+
idempotency key, and `extension.before`/`extension.after` receive `{ extension, name, args }`. An `after` hook also
|
|
95
|
+
receives how the call ended (`success` with its value, `failure` with its error, or `interrupted`). A `before` hook
|
|
96
|
+
runs before the call reaches the host, and a failing one denies it: the host function is never called, and the program
|
|
97
|
+
catches the failure as a thrown error. A denied call fails the way the call itself would: a tool call or asynchronous
|
|
98
|
+
global rejects its promise, and a synchronous global throws.
|
|
99
|
+
|
|
100
|
+
### Bytecode VM and suspending/resuming a run (experimental, `engine: "vm"`)
|
|
101
|
+
|
|
102
|
+
Pass `engine: "vm"` to `CodeMode.make` or `CodeMode.execute` to run programs on the bytecode VM instead of the default
|
|
103
|
+
`"legacy"` AST interpreter. `runtime.execute(code, bindings?)` keeps the same `Effect<CodeMode.Result>` signature on
|
|
104
|
+
both engines.
|
|
105
|
+
|
|
106
|
+
On `engine: "vm"`, `CodeMode.make` returns a `CodeMode.VmRuntime`, which can also run a program step by step:
|
|
107
|
+
`runtime.program(code, bindings?, { runId }?)` makes a `CodeMode.Program`, and nothing runs until `program.run()`. Every
|
|
108
|
+
host call gets a stable key `<runId>:<callId>` (using a generated UUID when `runId` is omitted), saved in the snapshot
|
|
109
|
+
and passed to tools as `execute(input, { key })` and to hooks as `call.key`. `run()` runs the program in the current
|
|
110
|
+
fiber until it ends or is suspended, and gives a `CodeMode.Outcome`: a `Result`, or:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
type Suspended = {
|
|
114
|
+
ok: "suspended"
|
|
115
|
+
state: CodeMode.Snapshot // JSON; CodeMode.toBytes / CodeMode.fromBytes convert it
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const runtime = CodeMode.make({ tools, engine: "vm" })
|
|
119
|
+
|
|
120
|
+
// inline
|
|
121
|
+
const outcome = yield* runtime.program(code).run()
|
|
122
|
+
|
|
123
|
+
// in the background, suspended from another fiber
|
|
124
|
+
const program = runtime.program(code, undefined, { runId: "run-1" })
|
|
125
|
+
const fiber = yield* Effect.forkChild(program.run())
|
|
126
|
+
const suspended = yield* program.suspend() // the same Suspended that run() returns
|
|
127
|
+
const outcome = yield* Fiber.join(fiber)
|
|
128
|
+
|
|
129
|
+
// later, in this process or another one built from the same version
|
|
130
|
+
yield* runtime.resume(CodeMode.toBytes(suspended.state)).run()
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
The default runtime has no `program` or `resume`, and ignores `Tool.idempotent`, `Tool.durable`, and extension
|
|
134
|
+
`snapshot` hooks. A program runs once: running it again is a defect, so make another with `runtime.program` or
|
|
135
|
+
`runtime.resume`. `run()` owns the run's host calls: interrupting it interrupts the program and every call in progress
|
|
136
|
+
(and cancels any started durable tool work).
|
|
137
|
+
|
|
138
|
+
#### Idempotent and durable tools, and safe suspension
|
|
139
|
+
|
|
140
|
+
A tool's `execute` is either a plain function or wrapped with `Tool.idempotent` or `Tool.durable`:
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
// plain (default): a suspend is refused while it runs
|
|
144
|
+
Tool.make({
|
|
145
|
+
description: "...",
|
|
146
|
+
input: ...,
|
|
147
|
+
output: ...,
|
|
148
|
+
execute: (input, { key }) => compute(input),
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
// idempotent: safe to run again; interrupted on suspend, rerun on resume with the same key
|
|
152
|
+
Tool.make({
|
|
153
|
+
description: "...",
|
|
154
|
+
input: ...,
|
|
155
|
+
output: ...,
|
|
156
|
+
execute: Tool.idempotent((input, { key }) => Db.get(input.name)),
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
// durable: keeps running across a suspend, reconnected through its JSON data on resume
|
|
160
|
+
Tool.make({
|
|
161
|
+
description: "...",
|
|
162
|
+
input: ...,
|
|
163
|
+
output: ...,
|
|
164
|
+
execute: Tool.durable({
|
|
165
|
+
start: (input, { key }) => Jobs.submit(input, key).pipe(Effect.map((id) => ({ id }))),
|
|
166
|
+
wait: ({ id }, { key }) => Jobs.wait(id),
|
|
167
|
+
cancel: ({ id }) => Jobs.kill(id),
|
|
168
|
+
}),
|
|
169
|
+
})
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
- **Plain (`execute: (input, { key }) => ...`):** not durable. Suspend is refused while the call is running.
|
|
173
|
+
- **`Tool.idempotent(execute)`:** idempotent/read-only work safe to run again. If a suspend lands while the call is
|
|
174
|
+
running, the VM detaches and interrupts it without firing `tool.after`, saves the call in the snapshot as queued, and
|
|
175
|
+
re-runs it on resume with the same `key` without double-counting it against `maxToolCalls`.
|
|
176
|
+
- **`Tool.durable({ start, wait, cancel })`:** work that outlives a suspended run (when run without a suspend or on the
|
|
177
|
+
legacy engine, it runs `start` then `wait`):
|
|
178
|
+
- `start(input, { key }) => Effect<Data /* Json */>` begins the work and returns JSON `data` (inferred into `wait`
|
|
179
|
+
and `cancel`);
|
|
180
|
+
- `wait(data, { key }) => Effect<Output>` connects to the running work and waits for its result;
|
|
181
|
+
- `cancel(data) => Effect<void>` stops the work when the run is interrupted for real or ends, never when it is
|
|
182
|
+
suspended.
|
|
183
|
+
|
|
184
|
+
While `start` is running, the call blocks suspend. Once `start` returns its JSON data, the call is in the `started`
|
|
185
|
+
state with its data saved in the snapshot, and suspend is allowed. `wait` is not tied to the program's JavaScript
|
|
186
|
+
`await`: the VM calls `wait(data)` right after `start` returns, and calls `wait(data)` again for every `started` call
|
|
187
|
+
right when `resume` starts, whatever the program does with the call's promise (whether it `await`s it immediately,
|
|
188
|
+
passes it to `Promise.all`, chains `.then`, or keeps the unawaited promise in a variable and awaits it later).
|
|
189
|
+
|
|
190
|
+
`program.suspend(options?)` saves the run's state and stops it at once, and `run()` returns the same `Suspended`.
|
|
191
|
+
Suspend is allowed only when every in-flight call is queued, `Tool.idempotent`, or `Tool.durable` in `started`. When
|
|
192
|
+
any other host call is in progress — a plain tool, a durable tool still in `start`, `search`, or an async or
|
|
193
|
+
synchronous extension call — `suspend` fails with a `CodeMode.SuspendError` naming each blocking call in `running` by
|
|
194
|
+
its `key` and `path` (`tools.x.y`, `search`, `fetch`), and the run goes on untouched. Tool calls queued but not started
|
|
195
|
+
don't block: they start on resume. Outcomes already waiting in the inbox are part of the state. A suspend after the run
|
|
196
|
+
ended gives its result; one issued before `run()` has started waits for it to start (and for ever if it never runs).
|
|
197
|
+
|
|
198
|
+
The suspend is immediate: the program shares the thread with the caller, so it is at a step boundary. Its checks, saving
|
|
199
|
+
the state, and checking it against `options.maxBytes` all happen before anything about the run changes, so a state that
|
|
200
|
+
can't be saved or is too large fails the suspend the same way.
|
|
201
|
+
|
|
202
|
+
A resumed run keeps the limits it was saved with: `timeoutMs` counts active time across every resume, and `maxToolCalls`
|
|
203
|
+
counts every admitted call of the run. A state resumes only on the same build of this package and with the same
|
|
204
|
+
extensions (and the same extension `snapshot` hooks). The state is checked when the resumed program runs: its `run()`
|
|
205
|
+
fails with a `CodeMode.SnapshotError` when it can't be resumed here or is malformed. Built-ins are saved by name and
|
|
206
|
+
only their changes are copied.
|
|
207
|
+
|
|
208
|
+
Set `CODEMODE_ROUNDTRIP=1` (`bun run test:roundtrip`) to run the tests on the VM with every run saved to bytes and
|
|
209
|
+
restored into a brand-new runtime at every host call and every few steps.
|
|
98
210
|
|
|
99
211
|
### `Extension.make`
|
|
100
212
|
|
|
@@ -136,6 +248,51 @@ Every value crossing in either direction is converted, never shared: arguments c
|
|
|
136
248
|
copies, and a function inside a result is callable the same way. A global that shadows a built-in or another
|
|
137
249
|
extension throws at `CodeMode.make`.
|
|
138
250
|
|
|
251
|
+
Functions inside a result have no data form, so a suspended VM run loses them by default: after a resume, calling one
|
|
252
|
+
throws a `TypeError`. An extension can preserve them across suspend and resume with `snapshot` hooks. `encode` receives
|
|
253
|
+
a value one of the extension's functions returned, as the host function returned it, and returns plain data (JSON
|
|
254
|
+
values and `Uint8Array`s); on resume, `decode` receives that data once per value and returns a host value of the same
|
|
255
|
+
shape, whose functions the restored program copies call from then on:
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
const saved = Symbol("saved")
|
|
259
|
+
const makeResponse = (data: { url: string; body: Uint8Array }) => ({
|
|
260
|
+
[saved]: data,
|
|
261
|
+
url: data.url,
|
|
262
|
+
text: async () => new TextDecoder().decode(data.body),
|
|
263
|
+
bytes: async () => data.body,
|
|
264
|
+
})
|
|
265
|
+
const web = Extension.make({
|
|
266
|
+
name: "web",
|
|
267
|
+
globals: {
|
|
268
|
+
fetch: Extension.async(async (url: string) =>
|
|
269
|
+
makeResponse({ url, body: await (await globalThis.fetch(url)).bytes() }),
|
|
270
|
+
),
|
|
271
|
+
},
|
|
272
|
+
snapshot: {
|
|
273
|
+
encode: (value) => (value as { [saved]: { url: string; body: Uint8Array } })[saved],
|
|
274
|
+
decode: (data) => makeResponse(data as { url: string; body: Uint8Array }),
|
|
275
|
+
},
|
|
276
|
+
})
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
A suspend fails (and the run continues) when `encode` throws or returns non-plain data. A snapshot saved with an
|
|
280
|
+
extension's hooks resumes only in a runtime whose extension of the same name also provides `snapshot` hooks.
|
|
281
|
+
|
|
282
|
+
A global can also be an object of functions, such as `fs`. Every function reachable from it is an entry point like a
|
|
283
|
+
global, so calls fire the `extension` hooks with their path as the name (`fs.readFile`, `fs.promises.readFile`).
|
|
284
|
+
|
|
285
|
+
Programs load globals the way Node code loads modules. `import` and `require` take a global by its Node module name,
|
|
286
|
+
with or without `node:`; `fs/promises`, `url`, `util`, and `buffer` resolve to the globals that provide them. There are
|
|
287
|
+
no packages, so anything else throws.
|
|
288
|
+
|
|
289
|
+
```js
|
|
290
|
+
import fs, { readFile } from "node:fs" // the `fs` global, if the host provides one
|
|
291
|
+
import { URL } from "url"
|
|
292
|
+
const crypto = require("crypto")
|
|
293
|
+
const { default: same } = await import("fs")
|
|
294
|
+
```
|
|
295
|
+
|
|
139
296
|
### OpenAPI tools
|
|
140
297
|
|
|
141
298
|
`OpenAPI.fromSpec` converts an OpenAPI 3.x document into one tool per supported operation. Dotted `operationId` values
|
package/dist/codemode.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
2
|
import type { Extension } from "./extension.js";
|
|
3
3
|
import { scanTools } from "./scan.js";
|
|
4
|
+
import { fromBytes, SnapshotError, SuspendError, toBytes } from "./suspend-error.js";
|
|
4
5
|
import { type Services, type ToolDescription, ToolRuntime } from "./tool-runtime.js";
|
|
5
6
|
import type { Tools } from "./tools.js";
|
|
6
|
-
|
|
7
|
+
import type { VmEngine } from "./vm/index.js";
|
|
8
|
+
export { fromBytes, scanTools, SnapshotError, SuspendError, toBytes };
|
|
9
|
+
export type SnapshotHeader = VmEngine.SnapshotHeader;
|
|
10
|
+
export type Snapshot = VmEngine.Snapshot;
|
|
7
11
|
/** A tool call admitted during an execution. */
|
|
8
12
|
export type { CallResult, ExtensionInvocation, Hooks, ToolCall, ToolDescription, ToolInvocation, } from "./tool-runtime.js";
|
|
9
13
|
/** Signature-construction helpers for host-owned catalog instructions. */
|
|
@@ -28,16 +32,33 @@ export type ResolvedExecutionLimits = {
|
|
|
28
32
|
readonly maxToolCalls: number | undefined;
|
|
29
33
|
readonly maxOutputBytes: number | undefined;
|
|
30
34
|
};
|
|
35
|
+
/** Execution engine: the default AST generator interpreter (`"legacy"`) or the experimental bytecode VM (`"vm"`). */
|
|
36
|
+
export type Engine = "legacy" | "vm";
|
|
37
|
+
/** Options for `VmRuntime.program`. */
|
|
38
|
+
export type ProgramOptions = {
|
|
39
|
+
/** Names this run's host calls (`<runId>:<callId>`); a random UUID by default. */
|
|
40
|
+
readonly runId?: string;
|
|
41
|
+
};
|
|
42
|
+
/** How `program.suspend` saves a run's state (`engine: "vm"` only). */
|
|
43
|
+
export type SuspendOptions = {
|
|
44
|
+
/** The most bytes a saved state may take; a larger one fails the suspend, and the run goes on. */
|
|
45
|
+
readonly maxBytes?: number;
|
|
46
|
+
};
|
|
31
47
|
/** Configuration shared by `CodeMode.make` and `CodeMode.execute`. */
|
|
32
48
|
export type Options<Provided extends Record<string, unknown> = {}, Extensions extends ReadonlyArray<Extension> = []> = {
|
|
33
49
|
/** Explicit tools exposed to the program as `tools`. */
|
|
34
50
|
tools?: Provided & Tools<Services<Provided>>;
|
|
35
51
|
/** Hooks around every tool and extension call the program makes; see `Hooks`. */
|
|
36
52
|
hooks?: ToolRuntime.Hooks<Environment<Provided, Extensions>>;
|
|
37
|
-
/** Host functions exposed as globals; see `Extension.make`. */
|
|
53
|
+
/** Host functions and objects exposed as globals, also importable by Node module name; see `Extension.make`. */
|
|
38
54
|
extensions?: Extensions;
|
|
39
|
-
/** Resource limits enforced on each execution. */
|
|
55
|
+
/** Resource limits enforced on each execution. Time counts active time only, across every resume of a run. */
|
|
40
56
|
limits?: ExecutionLimits;
|
|
57
|
+
/**
|
|
58
|
+
* Interpreter that runs programs. `"vm"` is the experimental bytecode VM, whose runtime (`VmRuntime`) can also
|
|
59
|
+
* suspend and resume runs; the default is `"legacy"`.
|
|
60
|
+
*/
|
|
61
|
+
engine?: Engine;
|
|
41
62
|
};
|
|
42
63
|
/** The Effect environment an execution needs: what the tools and the extensions' Effect globals require. */
|
|
43
64
|
export type Environment<Provided extends Record<string, unknown>, Extensions extends ReadonlyArray<Extension>> = Services<Provided> | Extension.Services<Extensions[number]>;
|
|
@@ -145,12 +166,47 @@ export declare const Result: Schema.Union<readonly [Schema.Struct<{
|
|
|
145
166
|
}>]>;
|
|
146
167
|
/** Result of executing a CodeMode program. Program failures are data, not Effect failures. */
|
|
147
168
|
export type Result = typeof Result.Type;
|
|
169
|
+
/** A run stopped by `program.suspend`: its whole state, to resume with `VmRuntime.resume`. */
|
|
170
|
+
export type Suspended = {
|
|
171
|
+
readonly ok: "suspended";
|
|
172
|
+
readonly state: Snapshot;
|
|
173
|
+
};
|
|
174
|
+
/** How a run of a program ended: its result, or its saved state once suspended. */
|
|
175
|
+
export type Outcome = Result | Suspended;
|
|
148
176
|
/** Reusable confined runtime over explicit tools. */
|
|
149
177
|
export type Runtime<R = never> = {
|
|
150
178
|
readonly catalog: ReadonlyArray<ToolDescription>;
|
|
151
179
|
readonly execute: (code: string, bindings?: Readonly<Record<string, DataValue>>) => Effect.Effect<Result, never, R>;
|
|
152
180
|
};
|
|
181
|
+
/** A runtime on the bytecode VM (`engine: "vm"`, experimental): it can also suspend runs and resume them. */
|
|
182
|
+
export type VmRuntime<R = never> = Runtime<R> & {
|
|
183
|
+
/** A program ready to run once; nothing runs until `program.run()`. */
|
|
184
|
+
readonly program: (code: string, bindings?: Readonly<Record<string, DataValue>>, options?: ProgramOptions) => Program<R>;
|
|
185
|
+
/**
|
|
186
|
+
* A suspended run (its `state`, or that state as bytes) as a program to continue in this runtime, which must be the
|
|
187
|
+
* same build with the same extensions. The state is checked when it runs: `run()` fails with a `SnapshotError` when
|
|
188
|
+
* it can't be resumed here.
|
|
189
|
+
*/
|
|
190
|
+
readonly resume: (state: Snapshot | Uint8Array) => Program<R, SnapshotError>;
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* One run of a program on the VM. `run()` runs it in the current fiber until it ends or is suspended, and owns its host
|
|
194
|
+
* calls: interrupting `run()` interrupts them. A program runs once: running it again is a defect.
|
|
195
|
+
*/
|
|
196
|
+
export type Program<R = never, E = never> = {
|
|
197
|
+
readonly run: () => Effect.Effect<Outcome, E, R>;
|
|
198
|
+
/**
|
|
199
|
+
* Saves the run's state and stops it, at once, from another fiber: `run()` then returns the same `Suspended`, and so
|
|
200
|
+
* does this. It fails with a `SuspendError`, leaving the run running, while a host call (tool, `search`, `fetch`, or
|
|
201
|
+
* extension call) is in progress, or when the state can't be saved or is over `options.maxBytes`. A run that already
|
|
202
|
+
* ended gives its result. Before `run()` starts, this waits for it to start.
|
|
203
|
+
*/
|
|
204
|
+
readonly suspend: (options?: SuspendOptions) => Effect.Effect<Outcome, SuspendError>;
|
|
205
|
+
};
|
|
153
206
|
/** Executes one Effect-native CodeMode program without constructing a reusable runtime. */
|
|
154
207
|
export declare const execute: <const Provided extends Record<string, unknown>, const Extensions extends ReadonlyArray<Extension> = []>(options: ExecuteOptions<Provided, Extensions>) => Effect.Effect<Result, never, Environment<Provided, Extensions>>;
|
|
155
208
|
/** Creates an Effect-native runtime over explicit, schema-described tools and extensions. */
|
|
156
|
-
export declare
|
|
209
|
+
export declare function make<const Provided extends Record<string, unknown> = {}, const Extensions extends ReadonlyArray<Extension> = []>(options: Options<Provided, Extensions> & {
|
|
210
|
+
engine: "vm";
|
|
211
|
+
}): VmRuntime<Environment<Provided, Extensions>>;
|
|
212
|
+
export declare function make<const Provided extends Record<string, unknown> = {}, const Extensions extends ReadonlyArray<Extension> = []>(options?: Options<Provided, Extensions>): Runtime<Environment<Provided, Extensions>>;
|
package/dist/codemode.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { Effect, Schema } from "effect";
|
|
1
|
+
import { Deferred, Effect, Exit, Schema } from "effect";
|
|
2
2
|
import { fromJson } from "./data.js";
|
|
3
3
|
import { executeProgram } from "./interpreter/execute.js";
|
|
4
4
|
import { extensionGlobals } from "./interpreter/extensions.js";
|
|
5
5
|
import { globalNames } from "./interpreter/globals.js";
|
|
6
6
|
import { scanTools } from "./scan.js";
|
|
7
|
+
import { fromBytes, SnapshotError, SuspendError, toBytes } from "./suspend-error.js";
|
|
7
8
|
import { ToolRuntime } from "./tool-runtime.js";
|
|
8
|
-
export { scanTools };
|
|
9
|
+
export { fromBytes, scanTools, SnapshotError, SuspendError, toBytes };
|
|
9
10
|
/** Signature-construction helpers for host-owned catalog instructions. */
|
|
10
11
|
export { searchSignature, toolExpression } from "./tool-runtime.js";
|
|
11
12
|
/** Schema for a host tool input containing CodeMode source. */
|
|
@@ -60,11 +61,11 @@ const resolveExecutionLimits = (limits) => ({
|
|
|
60
61
|
});
|
|
61
62
|
/** Executes one Effect-native CodeMode program without constructing a reusable runtime. */
|
|
62
63
|
export const execute = (options) => make(options).execute(options.code, options.bindings);
|
|
63
|
-
|
|
64
|
-
export const make = (options = {}) => {
|
|
64
|
+
export function make(options = {}) {
|
|
65
65
|
const prepared = ToolRuntime.prepare((options.tools ?? {}));
|
|
66
66
|
const limits = resolveExecutionLimits(options.limits);
|
|
67
67
|
const extensions = options.extensions ?? [];
|
|
68
|
+
// Both engines bind the same global names; test/engines.test.ts keeps them in step.
|
|
68
69
|
const bound = new Set(globalNames);
|
|
69
70
|
for (const extension of extensions) {
|
|
70
71
|
for (const name of Object.keys(extension.globals)) {
|
|
@@ -73,13 +74,67 @@ export const make = (options = {}) => {
|
|
|
73
74
|
bound.add(name);
|
|
74
75
|
}
|
|
75
76
|
}
|
|
77
|
+
if (options.engine !== "vm") {
|
|
78
|
+
return {
|
|
79
|
+
get catalog() {
|
|
80
|
+
return prepared.catalog;
|
|
81
|
+
},
|
|
82
|
+
execute: (code, bindings) => executeProgram(code, prepared, limits, options.hooks ?? {}, (ctx) => [
|
|
83
|
+
...Object.entries(bindings ?? {}).map(([name, value]) => [name, fromJson(ctx, value)]),
|
|
84
|
+
...extensionGlobals(ctx, extensions),
|
|
85
|
+
]),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
let setup;
|
|
89
|
+
const getSetup = (vm) => (setup ??= {
|
|
90
|
+
prepared,
|
|
91
|
+
limits,
|
|
92
|
+
hooks: options.hooks ?? {},
|
|
93
|
+
globals: extensions.length === 0 ? undefined : (ctx) => vm.extensionGlobals(ctx, extensions),
|
|
94
|
+
lossy: extensions.some((extension) => extension.snapshot === undefined),
|
|
95
|
+
});
|
|
96
|
+
const data = (vm, bindings) => bindings === undefined
|
|
97
|
+
? undefined
|
|
98
|
+
: (ctx) => Object.entries(bindings).map(([name, value]) => [name, vm.fromJson(ctx, value)]);
|
|
99
|
+
const program = (code, bindings, programOptions) => vmProgram((vm) => Effect.succeed(vm.startProgram(getSetup(vm), code, data(vm, bindings), programOptions?.runId)));
|
|
76
100
|
return {
|
|
77
101
|
get catalog() {
|
|
78
102
|
return prepared.catalog;
|
|
79
103
|
},
|
|
80
|
-
execute: (code, bindings) =>
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
104
|
+
execute: (code, bindings) => Effect.flatMap(loadVm(), (vm) => vm.finishExecution(program(code, bindings).run())),
|
|
105
|
+
program,
|
|
106
|
+
resume: (state) => vmProgram((vm) => vm.resumeProgram(getSetup(vm), state)),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
// A program's one run: `start` makes it once the VM is loaded, and a suspend waits for it.
|
|
110
|
+
const vmProgram = (start) => {
|
|
111
|
+
let claimed = false;
|
|
112
|
+
const started = Deferred.makeUnsafe();
|
|
113
|
+
return {
|
|
114
|
+
run: () => Effect.suspend(() => {
|
|
115
|
+
if (claimed) {
|
|
116
|
+
return Effect.die(new Error("A CodeMode program runs once; make another with runtime.program or runtime.resume."));
|
|
117
|
+
}
|
|
118
|
+
claimed = true;
|
|
119
|
+
return loadVm().pipe(Effect.flatMap(start), Effect.onExit((exit) => Exit.isSuccess(exit)
|
|
120
|
+
? Deferred.succeed(started, exit.value)
|
|
121
|
+
: Deferred.fail(started, new SuspendError({ message: "The program didn't start.", running: [] }))), Effect.flatMap((running) => running.run()));
|
|
122
|
+
}),
|
|
123
|
+
suspend: (options) => {
|
|
124
|
+
const maxBytes = options?.maxBytes;
|
|
125
|
+
if (maxBytes !== undefined && (!Number.isSafeInteger(maxBytes) || maxBytes < 0)) {
|
|
126
|
+
throw new RangeError("maxBytes must be a safe integer greater than or equal to 0.");
|
|
127
|
+
}
|
|
128
|
+
return Effect.flatMap(Deferred.await(started), (running) => running.suspend(maxBytes));
|
|
129
|
+
},
|
|
84
130
|
};
|
|
85
131
|
};
|
|
132
|
+
// The VM is loaded on first use so the default engine pulls in none of its code.
|
|
133
|
+
let vmLoaded;
|
|
134
|
+
let vmEngine;
|
|
135
|
+
const loadVm = () => vmLoaded !== undefined
|
|
136
|
+
? Effect.succeed(vmLoaded)
|
|
137
|
+
: Effect.promise(() => (vmEngine ??= import("./vm/index.js").then((mod) => {
|
|
138
|
+
vmLoaded = mod;
|
|
139
|
+
return mod;
|
|
140
|
+
})));
|
package/dist/extension.d.ts
CHANGED
|
@@ -1,24 +1,42 @@
|
|
|
1
1
|
export * as Extension from "./extension.js";
|
|
2
2
|
import type { Effect } from "effect";
|
|
3
3
|
/**
|
|
4
|
-
* Host
|
|
5
|
-
* shared: arguments come in as copies, results go out as copies, and a
|
|
6
|
-
* same way. Extension calls are not tool calls. A global may return a value,
|
|
7
|
-
* Effect's requirements join the runtime's environment.
|
|
4
|
+
* Host values a program reaches directly as globals: functions, or objects of functions such as `fs`. Values crossing
|
|
5
|
+
* in either direction are converted, never shared: arguments come in as copies, results go out as copies, and a
|
|
6
|
+
* function inside a result is callable the same way. Extension calls are not tool calls. A global may return a value,
|
|
7
|
+
* a Promise, or an Effect, and an Effect's requirements join the runtime's environment.
|
|
8
8
|
*
|
|
9
9
|
* A global is synchronous unless it is an `async` function or is marked with `async`. The `extension.before` hook
|
|
10
10
|
* runs before the call, so a denied call never reaches the host; it fails the way the global would, by rejecting the
|
|
11
|
-
* promise of an asynchronous global and by throwing from a synchronous one.
|
|
11
|
+
* promise of an asynchronous global and by throwing from a synchronous one. Functions inside a global object are
|
|
12
|
+
* called the same way, named by their path (`fs.readFile`).
|
|
13
|
+
*
|
|
14
|
+
* Programs can also `import` or `require` a global by its Node module name; see `interpreter/modules.ts`.
|
|
12
15
|
*/
|
|
13
|
-
export type Extension<Globals extends Readonly<Record<string,
|
|
16
|
+
export type Extension<Globals extends Readonly<Record<string, unknown>> = Readonly<Record<string, unknown>>> = {
|
|
14
17
|
readonly name: string;
|
|
15
18
|
readonly globals: Globals;
|
|
19
|
+
/** How a suspended run saves the values this extension's functions returned (`engine: "vm"` only); see `Snapshot`. */
|
|
20
|
+
readonly snapshot?: Snapshot | undefined;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Lets a suspended run keep the functions inside values this extension's functions returned (a response's `text`,
|
|
24
|
+
* say). `encode` gets such a value, as the host function returned it, and gives plain data: JSON values and
|
|
25
|
+
* `Uint8Array`s. On resume, `decode` gets that data back, once per value, and gives a value of the same shape, whose
|
|
26
|
+
* functions the program's copies call from then on. Without it, those functions are lost on resume: calling one
|
|
27
|
+
* throws. A suspend fails, and the run goes on, when `encode` throws or gives anything else.
|
|
28
|
+
*/
|
|
29
|
+
export type Snapshot = {
|
|
30
|
+
readonly encode: (value: unknown) => unknown;
|
|
31
|
+
readonly decode: (data: unknown) => unknown;
|
|
16
32
|
};
|
|
17
|
-
/** The Effect environment required by an extension's Effect-returning
|
|
33
|
+
/** The Effect environment required by an extension's Effect-returning functions, at the top level or one level in. */
|
|
18
34
|
export type Services<E> = E extends {
|
|
19
35
|
readonly globals: infer Globals;
|
|
20
|
-
} ? Globals[keyof Globals]
|
|
21
|
-
|
|
36
|
+
} ? GlobalServices<Globals[keyof Globals]> : never;
|
|
37
|
+
type GlobalServices<Global> = Global extends (...args: never) => unknown ? Requirements<Global> : Global extends object ? Requirements<Global[keyof Global]> : never;
|
|
38
|
+
type Requirements<F> = F extends (...args: never) => infer Result ? Effect.Services<Result> : never;
|
|
39
|
+
export declare const make: <const Globals extends Readonly<Record<string, unknown>>>(options: Extension<Globals>) => Extension<Globals>;
|
|
22
40
|
/** Marks a global that returns a Promise or an Effect, so a denied call rejects its promise instead of throwing. */
|
|
23
41
|
export declare const async: <F extends Function>(global: F) => F;
|
|
24
42
|
export declare const isAsync: (global: Function) => boolean;
|
package/dist/extension.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
export * as Extension from "./extension.js";
|
|
2
2
|
export const make = (options) => {
|
|
3
3
|
for (const [name, value] of Object.entries(options.globals)) {
|
|
4
|
-
if (typeof value !== "function") {
|
|
5
|
-
throw new TypeError(`Extension "${options.name}" global "${name}" must be a function.`);
|
|
4
|
+
if (typeof value !== "function" && (typeof value !== "object" || value === null)) {
|
|
5
|
+
throw new TypeError(`Extension "${options.name}" global "${name}" must be a function or an object.`);
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
|
-
return {
|
|
8
|
+
return {
|
|
9
|
+
name: options.name,
|
|
10
|
+
globals: { ...options.globals },
|
|
11
|
+
...(options.snapshot === undefined ? {} : { snapshot: options.snapshot }),
|
|
12
|
+
};
|
|
9
13
|
};
|
|
10
14
|
const asyncGlobals = new WeakSet();
|
|
11
15
|
const AsyncFunction = (async () => { }).constructor;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,5 +3,5 @@ export * as Extension from "./extension.js";
|
|
|
3
3
|
export * as Namespace from "./namespace.js";
|
|
4
4
|
export * as Tool from "./tool.js";
|
|
5
5
|
export * as OpenAPI from "./openapi/index.js";
|
|
6
|
-
export { searchSignature, toolExpression } from "./codemode.js";
|
|
6
|
+
export { fromBytes, searchSignature, SnapshotError, SuspendError, toBytes, toolExpression, type Outcome, type Program, type ProgramOptions, type Snapshot, type SnapshotHeader, type Suspended, type SuspendOptions, type VmRuntime, } from "./codemode.js";
|
|
7
7
|
export { ToolError, toolError } from "./tool-error.js";
|
package/dist/index.js
CHANGED
|
@@ -3,5 +3,5 @@ export * as Extension from "./extension.js";
|
|
|
3
3
|
export * as Namespace from "./namespace.js";
|
|
4
4
|
export * as Tool from "./tool.js";
|
|
5
5
|
export * as OpenAPI from "./openapi/index.js";
|
|
6
|
-
export { searchSignature, toolExpression } from "./codemode.js";
|
|
6
|
+
export { fromBytes, searchSignature, SnapshotError, SuspendError, toBytes, toolExpression, } from "./codemode.js";
|
|
7
7
|
export { ToolError, toolError } from "./tool-error.js";
|
|
@@ -1,6 +1,17 @@
|
|
|
1
|
+
import type { Cause } from "effect";
|
|
1
2
|
import type { Interpreter } from "./interpreter.js";
|
|
2
3
|
import { Callable, type Cursor, type NativeResult, type Value } from "./objects.js";
|
|
3
|
-
import {
|
|
4
|
+
import type { Eval } from "./runner.js";
|
|
5
|
+
/**
|
|
6
|
+
* Host work that was interrupted on its own. It settles its promise like a rejection, but program code cannot catch
|
|
7
|
+
* it: handlers, `catch`, and `finally` are skipped, an unobserved one is not reported, and one that reaches the
|
|
8
|
+
* program's body interrupts the execution.
|
|
9
|
+
*/
|
|
10
|
+
export declare class Interruption {
|
|
11
|
+
readonly cause: Cause.Cause<unknown>;
|
|
12
|
+
constructor(cause: Cause.Cause<unknown>);
|
|
13
|
+
}
|
|
14
|
+
export declare const isEval: <A>(value: A | Eval<A>) => value is Eval<A>;
|
|
4
15
|
/** IteratorClose: a consumer failure closes the iterator and wins over any close failure, except that a generator's
|
|
5
16
|
* return() is a return completion, so a failing close wins over it, as after `break`. */
|
|
6
17
|
export declare function preserveConsumerError<A>(close: Cursor["close"], body: Eval<A>): Eval<A>;
|
|
@@ -2,7 +2,19 @@ import { primitivePrototype } from "./intrinsics.js";
|
|
|
2
2
|
import { GeneratorReturn, typeError } from "./model.js";
|
|
3
3
|
import { Callable, get, Native, DateObj, Obj, coerceToNumber, coerceToString, } from "./objects.js";
|
|
4
4
|
import { isOpaque, typeofValue } from "./references.js";
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Host work that was interrupted on its own. It settles its promise like a rejection, but program code cannot catch
|
|
7
|
+
* it: handlers, `catch`, and `finally` are skipped, an unobserved one is not reported, and one that reaches the
|
|
8
|
+
* program's body interrupts the execution.
|
|
9
|
+
*/
|
|
10
|
+
export class Interruption {
|
|
11
|
+
cause;
|
|
12
|
+
constructor(cause) {
|
|
13
|
+
this.cause = cause;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
// Program values never have a `next` method of their own: an object's properties live in its property map.
|
|
17
|
+
export const isEval = (value) => typeof value === "object" && value !== null && typeof value.next === "function";
|
|
6
18
|
/** IteratorClose: a consumer failure closes the iterator and wins over any close failure, except that a generator's
|
|
7
19
|
* return() is a return completion, so a failing close wins over it, as after `break`. */
|
|
8
20
|
export function* preserveConsumerError(close, body) {
|
|
@@ -2,8 +2,8 @@ import { type Program } from "acorn";
|
|
|
2
2
|
import { Effect } from "effect";
|
|
3
3
|
import type { Diagnostic, ResolvedExecutionLimits, Result } from "../codemode.js";
|
|
4
4
|
import { ToolRuntime } from "../tool-runtime.js";
|
|
5
|
-
import type { Value } from "./objects.js";
|
|
6
5
|
import { Interpreter } from "./interpreter.js";
|
|
6
|
+
import type { Value } from "./objects.js";
|
|
7
7
|
export declare const executeProgram: <R>(code: string, prepared: ToolRuntime.Prepared<R>, limits: ResolvedExecutionLimits, hooks: ToolRuntime.Hooks<R>, globals?: (ctx: Interpreter<R>) => ReadonlyArray<readonly [string, Value]>) => Effect.Effect<Result, never, R>;
|
|
8
8
|
export declare const parseProgram: (code: string) => {
|
|
9
9
|
ok: true;
|
|
@@ -4,7 +4,7 @@ import { ToolRuntime } from "../tool-runtime.js";
|
|
|
4
4
|
import { normalizeError } from "./errors.js";
|
|
5
5
|
import { createBuiltins } from "./intrinsics.js";
|
|
6
6
|
import { Interpreter } from "./interpreter.js";
|
|
7
|
-
import { formatLocation } from "./model.js";
|
|
7
|
+
import { formatLocation, sourceLocation } from "./model.js";
|
|
8
8
|
export const executeProgram = (code, prepared, limits, hooks, globals) => {
|
|
9
9
|
if (code.trim().length === 0) {
|
|
10
10
|
return Effect.succeed({
|
|
@@ -93,8 +93,22 @@ export const parseProgram = (code) => {
|
|
|
93
93
|
sourceType: "script",
|
|
94
94
|
allowReturnOutsideFunction: true,
|
|
95
95
|
allowAwaitOutsideFunction: true,
|
|
96
|
+
// Imports bind globals by module name; scripts keep sloppy-mode semantics, which module source would make strict.
|
|
97
|
+
allowImportExportEverywhere: true,
|
|
96
98
|
locations: true,
|
|
97
99
|
});
|
|
100
|
+
const exported = program.body.find((statement) => statement.type.startsWith("Export"));
|
|
101
|
+
if (exported) {
|
|
102
|
+
const location = sourceLocation(exported);
|
|
103
|
+
return {
|
|
104
|
+
ok: false,
|
|
105
|
+
error: {
|
|
106
|
+
kind: "ParseError",
|
|
107
|
+
message: `SyntaxError: Exports aren't available. Return the result instead.${formatLocation(location)}`,
|
|
108
|
+
location,
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
98
112
|
return { ok: true, program };
|
|
99
113
|
}
|
|
100
114
|
catch (error) {
|
|
@@ -114,9 +128,7 @@ const decodeAcornError = Schema.decodeUnknownOption(AcornError);
|
|
|
114
128
|
const parseDiagnostic = (error) => {
|
|
115
129
|
const location = { line: error.loc.line, column: error.loc.column + 1 };
|
|
116
130
|
const reason = error.message.replace(/ \(\d+:\d+\)$/, "");
|
|
117
|
-
const text = reason
|
|
118
|
-
? "Imports and exports aren't available. Call tools through `tools` and use the built-in globals."
|
|
119
|
-
: `${reason.endsWith(".") ? reason : `${reason}.`} Code must be plain JavaScript written as a program body.`;
|
|
131
|
+
const text = `${reason.endsWith(".") ? reason : `${reason}.`} Code must be plain JavaScript written as a program body.`;
|
|
120
132
|
return { kind: "ParseError", message: `SyntaxError: ${text}${formatLocation(location)}`, location };
|
|
121
133
|
};
|
|
122
134
|
const utf8ByteLength = (value) => new TextEncoder().encode(value).byteLength;
|
|
@@ -2,8 +2,8 @@ import { type Extension } from "../extension.js";
|
|
|
2
2
|
import type { Interpreter } from "./interpreter.js";
|
|
3
3
|
import { type Value } from "./objects.js";
|
|
4
4
|
/**
|
|
5
|
-
* The global bindings of one run's extensions. Everything crossing the boundary is converted: plain data and
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* The global bindings of one run's extensions. Everything crossing the boundary is converted: plain data and built-in
|
|
6
|
+
* wrappers are copied, a host function becomes a program function whose calls cross the same way, and a host Promise
|
|
7
|
+
* or Effect becomes a program promise.
|
|
8
8
|
*/
|
|
9
9
|
export declare const extensionGlobals: <R>(ctx: Interpreter<R>, extensions: ReadonlyArray<Extension>) => ReadonlyArray<readonly [string, Value]>;
|