@opencode/codemode 0.0.0-dev-884 → 0.0.0-dev-900
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 +3 -0
- package/dist/codemode.d.ts +2 -0
- package/dist/codemode.js +10 -4
- 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 +103 -0
- package/dist/vm/host.js +226 -0
- package/dist/vm/index.d.ts +3 -0
- package/dist/vm/index.js +3 -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 +41 -0
- package/dist/vm/runtime/driver.js +142 -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 +22 -0
- package/dist/vm/runtime/execute.js +278 -0
- package/dist/vm/runtime/extensions.d.ts +16 -0
- package/dist/vm/runtime/extensions.js +270 -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 +50 -0
- package/dist/vm/runtime/interpreter.js +94 -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 +178 -0
- package/dist/vm/scheduler.js +63 -0
- package/dist/vm/snapshot/codec.d.ts +88 -0
- package/dist/vm/snapshot/codec.js +752 -0
- package/dist/vm/snapshot/snapshot.d.ts +39 -0
- package/dist/vm/snapshot/snapshot.js +69 -0
- package/dist/vm/snapshot.d.ts +29 -0
- package/dist/vm/snapshot.js +304 -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 +224 -0
- package/dist/vm/vm.js +2921 -0
- package/package.json +5 -2
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { parse } from "acorn";
|
|
2
|
+
import { Cause, Effect, Option, Schema } from "effect";
|
|
3
|
+
import { fromJson } from "../data.js";
|
|
4
|
+
import { fromBytes, SnapshotError, toBytes } from "../snapshot/snapshot.js";
|
|
5
|
+
import { ToolRuntime } from "../../tool-runtime.js";
|
|
6
|
+
import { normalizeError } from "./errors.js";
|
|
7
|
+
import { extensionGlobals } from "./extensions.js";
|
|
8
|
+
import { createBuiltins } from "./intrinsics.js";
|
|
9
|
+
import { Interpreter } from "./interpreter.js";
|
|
10
|
+
import { formatLocation, sourceLocation } from "./model.js";
|
|
11
|
+
import { Driver } from "./driver.js";
|
|
12
|
+
import { Interruption } from "./signals.js";
|
|
13
|
+
import { Source } from "./source.js";
|
|
14
|
+
/** Runs a program on the VM with `CodeMode.make`'s extensions and JSON bindings. */
|
|
15
|
+
export const execute = (code, prepared, limits, hooks, extensions, bindings) => executeProgram(code, prepared, limits, hooks, extensions.length === 0 ? undefined : (ctx) => extensionGlobals(ctx, extensions), bindings === undefined
|
|
16
|
+
? undefined
|
|
17
|
+
: (ctx) => Object.entries(bindings).map(([name, value]) => [name, fromJson(ctx, value)]), extensions.length > 0);
|
|
18
|
+
export const executeProgram = (code, prepared, limits, hooks, globals, bindings, lossy = false) => Effect.suspend(() => {
|
|
19
|
+
if (code.trim().length === 0) {
|
|
20
|
+
return Effect.succeed({
|
|
21
|
+
ok: false,
|
|
22
|
+
error: { kind: "ParseError", message: "Code cannot be empty." },
|
|
23
|
+
toolCalls: [],
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
const parsed = parseProgram(code);
|
|
27
|
+
if (!parsed.ok)
|
|
28
|
+
return Effect.succeed({ ok: false, error: parsed.error, toolCalls: [] });
|
|
29
|
+
const setup = { prepared, limits, hooks, globals, lossy };
|
|
30
|
+
const ctx = instantiate(setup, bindings);
|
|
31
|
+
ctx.vm.start(parsed.source);
|
|
32
|
+
return new Running(setup, ctx).run();
|
|
33
|
+
});
|
|
34
|
+
// Sources restored recently, by text: the AST is never changed, so runs restored from the same program share it.
|
|
35
|
+
const restoredSources = new Map();
|
|
36
|
+
const restoreInto = (saved, vm) => {
|
|
37
|
+
vm.restore(saved, {
|
|
38
|
+
parse: (code) => {
|
|
39
|
+
const known = restoredSources.get(code);
|
|
40
|
+
if (known !== undefined)
|
|
41
|
+
return known;
|
|
42
|
+
const parsed = parseProgram(code);
|
|
43
|
+
if (!parsed.ok)
|
|
44
|
+
throw new SnapshotError(`A saved source no longer parses: ${parsed.error.message}`);
|
|
45
|
+
if (restoredSources.size >= 16)
|
|
46
|
+
restoredSources.delete(restoredSources.keys().next().value);
|
|
47
|
+
restoredSources.set(code, parsed.source);
|
|
48
|
+
return parsed.source;
|
|
49
|
+
},
|
|
50
|
+
interruption: () => new Interruption(Cause.interrupt()),
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
// A new runtime: intrinsics, the host globals, the program's bindings, a fresh tool bridge, and its VM.
|
|
54
|
+
const instantiate = (setup, bindings) => {
|
|
55
|
+
const ctx = new Interpreter({
|
|
56
|
+
tools: ToolRuntime.make(setup.prepared, setup.limits.maxToolCalls, setup.hooks),
|
|
57
|
+
builtins: createBuiltins(),
|
|
58
|
+
logs: [],
|
|
59
|
+
globals: setup.globals,
|
|
60
|
+
bindings,
|
|
61
|
+
});
|
|
62
|
+
if (roundTrip)
|
|
63
|
+
ctx.vm.tripEvery = ROUND_TRIP_TICKS;
|
|
64
|
+
return ctx;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* The round-trip test mode: at every host stop and every slice, which come every few loop back-edges and calls, a run's
|
|
68
|
+
* state is saved to bytes and restored into a brand-new runtime, which continues the run. It proves the VM's state is
|
|
69
|
+
* plain data at every step. Internal: only the test suite turns it on (`bun run test:roundtrip`).
|
|
70
|
+
*/
|
|
71
|
+
let roundTrip = false;
|
|
72
|
+
export const setRoundTrip = (enabled) => {
|
|
73
|
+
roundTrip = enabled;
|
|
74
|
+
};
|
|
75
|
+
const ROUND_TRIP_TICKS = 2;
|
|
76
|
+
const ROUND_TRIP_MAX = 200;
|
|
77
|
+
/** One run: the runtime it is in now (a round trip swaps it). */
|
|
78
|
+
class Running {
|
|
79
|
+
setup;
|
|
80
|
+
ctx;
|
|
81
|
+
trips = 0;
|
|
82
|
+
// Checkpoints to let pass before the next round trip, so a large state is not saved at every one.
|
|
83
|
+
skip = 0;
|
|
84
|
+
constructor(setup, ctx) {
|
|
85
|
+
this.setup = setup;
|
|
86
|
+
this.ctx = ctx;
|
|
87
|
+
}
|
|
88
|
+
run() {
|
|
89
|
+
const self = this;
|
|
90
|
+
return Effect.gen(function* () {
|
|
91
|
+
const driver = new Driver(self.ctx.vm, self.ctx.tools, yield* Effect.context());
|
|
92
|
+
if (roundTrip)
|
|
93
|
+
driver.checkpoint = Effect.sync(() => self.trip(driver));
|
|
94
|
+
return yield* self.execute(driver);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
execute(driver) {
|
|
98
|
+
const self = this;
|
|
99
|
+
const limits = this.setup.limits;
|
|
100
|
+
const logged = () => {
|
|
101
|
+
const logs = self.ctx.logs;
|
|
102
|
+
return logs.length > 0 ? { logs: [...logs] } : {};
|
|
103
|
+
};
|
|
104
|
+
const toolCalls = () => self.ctx.tools.calls;
|
|
105
|
+
const failed = (error) => ({ ok: false, error, ...logged(), toolCalls: toolCalls() });
|
|
106
|
+
// Set only after copy-out so timeouts cannot report invalid values as completed.
|
|
107
|
+
let returned;
|
|
108
|
+
const base = Effect.gen(function* () {
|
|
109
|
+
const outcome = yield* driver.run();
|
|
110
|
+
if (!outcome.ok)
|
|
111
|
+
return failed(normalizeError(outcome.error));
|
|
112
|
+
const result = outcome.value ?? null;
|
|
113
|
+
const vm = self.ctx.vm;
|
|
114
|
+
returned = { value: result, warnings: () => vm.diagnostics() };
|
|
115
|
+
// Host work the program left running is interrupted, not awaited.
|
|
116
|
+
yield* driver.interrupt();
|
|
117
|
+
const warnings = vm.diagnostics();
|
|
118
|
+
return {
|
|
119
|
+
ok: true,
|
|
120
|
+
value: result,
|
|
121
|
+
...(warnings.length > 0 ? { warnings } : {}),
|
|
122
|
+
...logged(),
|
|
123
|
+
toolCalls: toolCalls(),
|
|
124
|
+
};
|
|
125
|
+
}).pipe(Effect.ensuring(Effect.suspend(() => driver.interrupt())));
|
|
126
|
+
const timeoutMs = limits.timeoutMs;
|
|
127
|
+
const operation = timeoutMs === undefined
|
|
128
|
+
? base
|
|
129
|
+
: base.pipe(Effect.timeoutOrElse({
|
|
130
|
+
duration: timeoutMs,
|
|
131
|
+
orElse: () => Effect.sync(() => {
|
|
132
|
+
if (returned === undefined) {
|
|
133
|
+
return failed({ kind: "TimeoutExceeded", message: `Execution timed out after ${timeoutMs}ms.` });
|
|
134
|
+
}
|
|
135
|
+
// Keep the timeout warning first so truncation preserves it.
|
|
136
|
+
return {
|
|
137
|
+
ok: true,
|
|
138
|
+
value: returned.value,
|
|
139
|
+
warnings: [
|
|
140
|
+
{
|
|
141
|
+
kind: "TimeoutExceeded",
|
|
142
|
+
message: `The program returned, but background work was still running at the ${timeoutMs}ms timeout and was interrupted. Await all started promises.`,
|
|
143
|
+
},
|
|
144
|
+
...returned.warnings(),
|
|
145
|
+
],
|
|
146
|
+
...logged(),
|
|
147
|
+
toolCalls: toolCalls(),
|
|
148
|
+
};
|
|
149
|
+
}),
|
|
150
|
+
}));
|
|
151
|
+
return operation.pipe(Effect.catchCause((cause) => Cause.hasInterruptsOnly(cause) ? Effect.interrupt : Effect.succeed(failed(normalizeError(Cause.squash(cause))))), Effect.map((result) => limits.maxOutputBytes === undefined ? result : boundOutput(result, limits.maxOutputBytes)));
|
|
152
|
+
}
|
|
153
|
+
// The round-trip test mode's checkpoint: saves the state to bytes and continues in a brand-new runtime. States that
|
|
154
|
+
// hold live host values (an extension call in flight, a function an extension returned) can't round-trip, so they
|
|
155
|
+
// are skipped; a lost value anywhere else is a codec gap and fails the run.
|
|
156
|
+
trip(driver) {
|
|
157
|
+
if (this.trips >= ROUND_TRIP_MAX) {
|
|
158
|
+
this.ctx.vm.tripEvery = 0;
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (this.skip > 0) {
|
|
162
|
+
this.skip--;
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (driver.holdsHostValues)
|
|
166
|
+
return;
|
|
167
|
+
this.trips++;
|
|
168
|
+
const saved = this.ctx.vm.capture({ strict: !this.setup.lossy });
|
|
169
|
+
const bytes = toBytes(saved.snapshot);
|
|
170
|
+
this.skip = Math.floor(saved.snapshot.records.length / 200 + bytes.byteLength / 100_000);
|
|
171
|
+
if (saved.lost.length > 0)
|
|
172
|
+
return;
|
|
173
|
+
const fresh = instantiate(this.setup);
|
|
174
|
+
restoreInto(fromBytes(bytes), fresh.vm);
|
|
175
|
+
this.ctx = fresh;
|
|
176
|
+
driver.vm = fresh.vm;
|
|
177
|
+
driver.tools = fresh.tools;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
export const parseProgram = (code) => {
|
|
181
|
+
try {
|
|
182
|
+
const program = parse(code, {
|
|
183
|
+
ecmaVersion: "latest",
|
|
184
|
+
sourceType: "script",
|
|
185
|
+
allowReturnOutsideFunction: true,
|
|
186
|
+
allowAwaitOutsideFunction: true,
|
|
187
|
+
// Imports bind globals by module name; scripts keep sloppy-mode semantics, which module source would make strict.
|
|
188
|
+
allowImportExportEverywhere: true,
|
|
189
|
+
locations: true,
|
|
190
|
+
});
|
|
191
|
+
const exported = program.body.find((statement) => statement.type.startsWith("Export"));
|
|
192
|
+
if (exported) {
|
|
193
|
+
const location = sourceLocation(exported);
|
|
194
|
+
return {
|
|
195
|
+
ok: false,
|
|
196
|
+
error: {
|
|
197
|
+
kind: "ParseError",
|
|
198
|
+
message: `SyntaxError: Exports aren't available. Return the result instead.${formatLocation(location)}`,
|
|
199
|
+
location,
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
return { ok: true, program, source: new Source("main", code, program) };
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
// Only acorn's own syntax errors become a located ParseError; anything else keeps the generic failure path.
|
|
207
|
+
const acorn = error instanceof SyntaxError ? decodeAcornError(error) : Option.none();
|
|
208
|
+
if (Option.isNone(acorn))
|
|
209
|
+
throw error;
|
|
210
|
+
return { ok: false, error: parseDiagnostic(acorn.value) };
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
// Acorn appends ` (line:column)` to its messages; its lines are 1-based and its columns 0-based.
|
|
214
|
+
const AcornError = Schema.Struct({
|
|
215
|
+
message: Schema.String,
|
|
216
|
+
loc: Schema.Struct({ line: Schema.Number, column: Schema.Number }),
|
|
217
|
+
});
|
|
218
|
+
const decodeAcornError = Schema.decodeUnknownOption(AcornError);
|
|
219
|
+
const parseDiagnostic = (error) => {
|
|
220
|
+
const location = { line: error.loc.line, column: error.loc.column + 1 };
|
|
221
|
+
const reason = error.message.replace(/ \(\d+:\d+\)$/, "");
|
|
222
|
+
const text = `${reason.endsWith(".") ? reason : `${reason}.`} Code must be plain JavaScript written as a program body.`;
|
|
223
|
+
return { kind: "ParseError", message: `SyntaxError: ${text}${formatLocation(location)}`, location };
|
|
224
|
+
};
|
|
225
|
+
const utf8ByteLength = (value) => new TextEncoder().encode(value).byteLength;
|
|
226
|
+
// Drop a replacement character produced by truncating inside a UTF-8 sequence.
|
|
227
|
+
const utf8Truncate = (value, maxBytes) => {
|
|
228
|
+
const bytes = new TextEncoder().encode(value);
|
|
229
|
+
if (bytes.byteLength <= maxBytes)
|
|
230
|
+
return value;
|
|
231
|
+
const text = new TextDecoder("utf-8").decode(bytes.slice(0, Math.max(0, maxBytes)));
|
|
232
|
+
return text.endsWith("\uFFFD") ? text.slice(0, -1) : text;
|
|
233
|
+
};
|
|
234
|
+
// The longest prefix of `items` whose sizes add up to at most `budget` bytes.
|
|
235
|
+
const fitting = (items, budget, size) => {
|
|
236
|
+
const kept = [];
|
|
237
|
+
let used = 0;
|
|
238
|
+
for (const item of items) {
|
|
239
|
+
used += size(item);
|
|
240
|
+
if (used > budget)
|
|
241
|
+
break;
|
|
242
|
+
kept.push(item);
|
|
243
|
+
}
|
|
244
|
+
return kept;
|
|
245
|
+
};
|
|
246
|
+
// Warnings have a separate budget so result data cannot starve diagnostics.
|
|
247
|
+
const boundOutput = (result, maxOutputBytes) => {
|
|
248
|
+
const serialized = result.ok ? (JSON.stringify(result.value) ?? "null") : "";
|
|
249
|
+
const valueBytes = result.ok ? utf8ByteLength(serialized) : 0;
|
|
250
|
+
const valueTruncated = valueBytes > maxOutputBytes;
|
|
251
|
+
const value = !result.ok
|
|
252
|
+
? null
|
|
253
|
+
: valueTruncated
|
|
254
|
+
? `${utf8Truncate(serialized, maxOutputBytes)} [result truncated: ${valueBytes} bytes exceeds the ${maxOutputBytes}-byte output limit; return a smaller value]`
|
|
255
|
+
: result.value;
|
|
256
|
+
const warnings = result.ok ? (result.warnings ?? []) : [];
|
|
257
|
+
const keptWarnings = fitting(warnings, maxOutputBytes, (warning) => utf8ByteLength(JSON.stringify(warning)) + 1);
|
|
258
|
+
const warningsCut = keptWarnings.length < warnings.length;
|
|
259
|
+
if (warningsCut) {
|
|
260
|
+
keptWarnings.push({
|
|
261
|
+
kind: "Truncated",
|
|
262
|
+
message: `${warnings.length - keptWarnings.length} additional warnings omitted by the output limit.`,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
const logs = result.logs ?? [];
|
|
266
|
+
const logBudget = Math.max(0, maxOutputBytes - Math.min(valueBytes, maxOutputBytes));
|
|
267
|
+
const kept = fitting(logs, logBudget, (line) => utf8ByteLength(line) + 1);
|
|
268
|
+
const logsCut = kept.length < logs.length;
|
|
269
|
+
if (logsCut)
|
|
270
|
+
kept.push(`[logs truncated: showing ${kept.length} of ${logs.length} lines]`);
|
|
271
|
+
if (!valueTruncated && !warningsCut && !logsCut)
|
|
272
|
+
return result;
|
|
273
|
+
const warningsPart = keptWarnings.length > 0 ? { warnings: keptWarnings } : {};
|
|
274
|
+
const logsPart = kept.length > 0 ? { logs: kept } : {};
|
|
275
|
+
return result.ok
|
|
276
|
+
? { ok: true, value, ...warningsPart, ...logsPart, truncated: true, toolCalls: result.toolCalls }
|
|
277
|
+
: { ok: false, error: result.error, ...logsPart, truncated: true, toolCalls: result.toolCalls };
|
|
278
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Extension } from "../../extension.js";
|
|
2
|
+
import type { Interpreter } from "./interpreter.js";
|
|
3
|
+
import { type AstNode } from "./model.js";
|
|
4
|
+
import { PromiseObj, type Value } from "./objects.js";
|
|
5
|
+
import type { ExtensionRequest } from "../scheduler.js";
|
|
6
|
+
/**
|
|
7
|
+
* The global bindings of one run's extensions. Everything crossing the boundary is converted: plain data and built-in
|
|
8
|
+
* wrappers are copied, a host function becomes a program function whose calls cross the same way, and a host Promise
|
|
9
|
+
* or Effect becomes a program promise.
|
|
10
|
+
*/
|
|
11
|
+
export declare const extensionGlobals: <R>(ctx: Interpreter<R>, extensions: ReadonlyArray<Extension>) => ReadonlyArray<readonly [string, Value]>;
|
|
12
|
+
/**
|
|
13
|
+
* Calls a synchronous extension function's host function, once its `extension.before` hook allowed it: a promise for
|
|
14
|
+
* a result that settles later (an Effect or a Promise), or the request that settles what it returned or threw now.
|
|
15
|
+
*/
|
|
16
|
+
export declare const startExtensionCall: <R>(ctx: Interpreter<R>, host: Function, args: ReadonlyArray<unknown>, call: Pick<ExtensionRequest, "label" | "invocation">, site: AstNode | undefined) => PromiseObj | ExtensionRequest;
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { isAsync } from "../../extension.js";
|
|
3
|
+
import { hooked } from "../../tool-runtime.js";
|
|
4
|
+
import { errorMessage, errorType } from "./errors.js";
|
|
5
|
+
import { createErrorValue } from "./intrinsics.js";
|
|
6
|
+
import { MAX_ARRAY_LENGTH, MAX_VALUE_DEPTH } from "./limits.js";
|
|
7
|
+
import { PendingThrow, rangeError, Throw, typeError } from "./model.js";
|
|
8
|
+
import { native } from "./native.js";
|
|
9
|
+
import { define, get, has, hidden, keys, Arr, Bytes, DateObj, ErrorObj, HeadersObj, PromiseObj, MapObj, Obj, RegExpObj, SetObj, URLObj, URLSearchParamsObj, coerceToString, } from "./objects.js";
|
|
10
|
+
import { describeValue, isOpaque } from "./references.js";
|
|
11
|
+
import { Push } from "./signals.js";
|
|
12
|
+
/**
|
|
13
|
+
* The global bindings of one run's extensions. Everything crossing the boundary is converted: plain data and built-in
|
|
14
|
+
* wrappers are copied, a host function becomes a program function whose calls cross the same way, and a host Promise
|
|
15
|
+
* or Effect becomes a program promise.
|
|
16
|
+
*/
|
|
17
|
+
export const extensionGlobals = (ctx, extensions) => {
|
|
18
|
+
const builtins = ctx.builtins;
|
|
19
|
+
const toHost = (value, label, depth = 0, seen = new Set()) => {
|
|
20
|
+
if (depth > MAX_VALUE_DEPTH)
|
|
21
|
+
throw typeError(`${label} exceeds the maximum value depth of ${MAX_VALUE_DEPTH}.`);
|
|
22
|
+
if (isPrimitive(value))
|
|
23
|
+
return value;
|
|
24
|
+
if (!(value instanceof Obj) || isOpaque(value)) {
|
|
25
|
+
throw typeError(`${label} contains ${describeValue(value)}, which cannot be passed to an extension.`);
|
|
26
|
+
}
|
|
27
|
+
if (seen.has(value))
|
|
28
|
+
throw typeError(`${label} contains a circular value.`);
|
|
29
|
+
seen.add(value);
|
|
30
|
+
const next = (item) => toHost(item, label, depth + 1, seen);
|
|
31
|
+
if (value instanceof ErrorObj) {
|
|
32
|
+
const name = coerceToString(get(value, "name"));
|
|
33
|
+
const message = get(value, "message");
|
|
34
|
+
const text = message === undefined ? "" : coerceToString(message);
|
|
35
|
+
const copied = name === "AggregateError" ? new AggregateError([], text) : new (hostErrors.get(name) ?? Error)(text);
|
|
36
|
+
for (const key of new Set(["cause", ...keys(value)])) {
|
|
37
|
+
if (uncrossed.has(key) || !has(value, key))
|
|
38
|
+
continue;
|
|
39
|
+
const item = crossing(() => next(get(value, key)));
|
|
40
|
+
if (item === left)
|
|
41
|
+
continue;
|
|
42
|
+
Object.defineProperty(copied, key, {
|
|
43
|
+
value: item,
|
|
44
|
+
writable: true,
|
|
45
|
+
configurable: true,
|
|
46
|
+
enumerable: key !== "cause",
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
seen.delete(value);
|
|
50
|
+
return copied;
|
|
51
|
+
}
|
|
52
|
+
const copied = value.toHost(next);
|
|
53
|
+
seen.delete(value);
|
|
54
|
+
return copied;
|
|
55
|
+
};
|
|
56
|
+
// `extension` is set under an extension's global object, whose functions are entry points that fire the hooks.
|
|
57
|
+
const fromHost = (value, label, depth = 0, seen = new Set(), extension) => {
|
|
58
|
+
if (depth > MAX_VALUE_DEPTH)
|
|
59
|
+
throw typeError(`${label} exceeds the maximum value depth of ${MAX_VALUE_DEPTH}.`);
|
|
60
|
+
if (isPrimitive(value))
|
|
61
|
+
return value;
|
|
62
|
+
if (typeof value === "function")
|
|
63
|
+
return wrap(value, label, extension);
|
|
64
|
+
if (value !== null && typeof value === "object") {
|
|
65
|
+
const next = (item, path) => fromHost(item, path, depth + 1, seen, extension);
|
|
66
|
+
// Nothing a host value hands back is kept: results are read as primitives or copied. Where the value's own
|
|
67
|
+
// method could hand back host storage or an object (`getTime`, `slice`, `map`), its internal data is read.
|
|
68
|
+
if (value instanceof Date)
|
|
69
|
+
return new DateObj(builtins.Date, Date.prototype.getTime.call(value));
|
|
70
|
+
if (value instanceof RegExp)
|
|
71
|
+
return new RegExpObj(builtins.RegExp, value.source, value.flags);
|
|
72
|
+
if (value instanceof Uint8Array)
|
|
73
|
+
return new Bytes(builtins.Uint8Array, new Uint8Array(value));
|
|
74
|
+
if (value instanceof ArrayBuffer) {
|
|
75
|
+
const view = new Uint8Array(value);
|
|
76
|
+
// Only a real buffer has bytes to view; anything else would be read as array-like and lose them silently.
|
|
77
|
+
if (view.buffer !== value)
|
|
78
|
+
throw typeError(`${label} produced ${describeHost(value)}, which the program cannot hold.`);
|
|
79
|
+
return new Bytes(builtins.Uint8Array, new Uint8Array(view));
|
|
80
|
+
}
|
|
81
|
+
if (value instanceof Error) {
|
|
82
|
+
if (seen.has(value))
|
|
83
|
+
throw typeError(`${label} produced a circular value.`);
|
|
84
|
+
seen.add(value);
|
|
85
|
+
const copied = createErrorValue(builtins[errorType(value)], errorMessage(value));
|
|
86
|
+
const fields = value;
|
|
87
|
+
for (const key of new Set(["cause", ...Object.keys(value)])) {
|
|
88
|
+
if (uncrossed.has(key) || !(key in value))
|
|
89
|
+
continue;
|
|
90
|
+
// Read once: a getter could pass the check and then hand back a function.
|
|
91
|
+
const field = fields[key];
|
|
92
|
+
if (typeof field === "function")
|
|
93
|
+
continue;
|
|
94
|
+
const item = crossing(() => next(field, `${label}.${key}`));
|
|
95
|
+
if (item === left)
|
|
96
|
+
continue;
|
|
97
|
+
define(copied, key, item, key === "cause" ? hidden : undefined);
|
|
98
|
+
}
|
|
99
|
+
seen.delete(value);
|
|
100
|
+
return copied;
|
|
101
|
+
}
|
|
102
|
+
if (value instanceof URL)
|
|
103
|
+
return new URLObj(builtins.URL, builtins.URLSearchParams, new URL(value.href));
|
|
104
|
+
if (value instanceof URLSearchParams) {
|
|
105
|
+
return new URLSearchParamsObj(builtins.URLSearchParams, new URLSearchParams(value));
|
|
106
|
+
}
|
|
107
|
+
if (value instanceof Headers)
|
|
108
|
+
return new HeadersObj(builtins.Headers, new Headers(value));
|
|
109
|
+
if (value instanceof Map) {
|
|
110
|
+
const wrapped = new MapObj(builtins.Map);
|
|
111
|
+
for (const [key, item] of value)
|
|
112
|
+
wrapped.map.set(next(key, label), next(item, label));
|
|
113
|
+
return wrapped;
|
|
114
|
+
}
|
|
115
|
+
if (value instanceof Set) {
|
|
116
|
+
const wrapped = new SetObj(builtins.Set);
|
|
117
|
+
for (const item of value)
|
|
118
|
+
wrapped.set.add(next(item, label));
|
|
119
|
+
return wrapped;
|
|
120
|
+
}
|
|
121
|
+
if (seen.has(value))
|
|
122
|
+
throw typeError(`${label} produced a circular value.`);
|
|
123
|
+
seen.add(value);
|
|
124
|
+
if (Array.isArray(value)) {
|
|
125
|
+
const length = value.length;
|
|
126
|
+
if (length > MAX_ARRAY_LENGTH) {
|
|
127
|
+
throw rangeError(`${label} produced an array of ${length} items; at most ${MAX_ARRAY_LENGTH} can cross.`);
|
|
128
|
+
}
|
|
129
|
+
// Filled in order so the copy stays a dense array; holes stay holes, as a copy made with `map` would leave them.
|
|
130
|
+
const items = [];
|
|
131
|
+
for (let index = 0; index < length; index += 1) {
|
|
132
|
+
if (index in value)
|
|
133
|
+
items[index] = next(value[index], `${label}[${index}]`);
|
|
134
|
+
}
|
|
135
|
+
items.length = length;
|
|
136
|
+
seen.delete(value);
|
|
137
|
+
return new Arr(builtins.Array, items);
|
|
138
|
+
}
|
|
139
|
+
const prototype = Object.getPrototypeOf(value);
|
|
140
|
+
if (prototype === Object.prototype || prototype === null) {
|
|
141
|
+
const copied = new Obj(builtins.Object);
|
|
142
|
+
for (const [key, item] of Object.entries(value))
|
|
143
|
+
define(copied, key, next(item, `${label}.${key}`));
|
|
144
|
+
seen.delete(value);
|
|
145
|
+
return copied;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
throw typeError(`${label} produced ${describeHost(value)}, which the program cannot hold.`);
|
|
149
|
+
};
|
|
150
|
+
// A host function as a program function. Arguments cross in; for a global or a global object's function, the
|
|
151
|
+
// `extension.before` hook runs before the host function is called, so a call it denies never reaches the host. The
|
|
152
|
+
// host's result, or what it threw, crosses out so the program catches a copy, and `extension.after` sees how the
|
|
153
|
+
// call ended. Functions inside results are part of a value's API and skip the hooks.
|
|
154
|
+
const wrap = (value, label, extension) => {
|
|
155
|
+
// The host function's own `name` and `length` can be anything, even getters: each is read once, and only text
|
|
156
|
+
// and a count cross.
|
|
157
|
+
const name = value.name;
|
|
158
|
+
const length = value.length;
|
|
159
|
+
return native(builtins, {
|
|
160
|
+
runtime: "extension",
|
|
161
|
+
name: typeof name === "string" ? name : "",
|
|
162
|
+
length: typeof length === "number" && Number.isSafeInteger(length) && length >= 0 ? length : 0,
|
|
163
|
+
call: (_, values, site) => {
|
|
164
|
+
const args = values.map((item, index) => toHost(item, `Argument ${index + 1} to ${label}`));
|
|
165
|
+
const invocation = extension === undefined ? undefined : { extension, name: label, args };
|
|
166
|
+
// A denied call fails the way the global would: an async global rejects its promise, a sync one throws.
|
|
167
|
+
if (isAsync(value)) {
|
|
168
|
+
return pending(ctx, { stage: "run", label, invocation, host: value, args, threw: false }, site);
|
|
169
|
+
}
|
|
170
|
+
return new Push("extension call", [value, args, label, invocation]);
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
};
|
|
174
|
+
// The Effect each step of an extension call runs as, with the program's services in its requirements.
|
|
175
|
+
ctx.extensionWork = (request) => {
|
|
176
|
+
const label = request.label;
|
|
177
|
+
const call = request.invocation;
|
|
178
|
+
const cross = (reason) => new Throw(fromHost(reason, label));
|
|
179
|
+
const hooks = ctx.tools.hooks;
|
|
180
|
+
const before = call && hooks["extension.before"]?.(call).pipe(Effect.mapError(cross));
|
|
181
|
+
if (request.stage === "before")
|
|
182
|
+
return before ?? Effect.void;
|
|
183
|
+
const after = call && hooks["extension.after"];
|
|
184
|
+
const settle = (effect) => (after ? hooked(call, undefined, after, effect) : effect).pipe(Effect.mapBoth({ onFailure: cross, onSuccess: (result) => fromHost(result, label) }));
|
|
185
|
+
if (request.stage === "settle")
|
|
186
|
+
return settle(outcome(request.host, request.threw));
|
|
187
|
+
return Effect.andThen(before ?? Effect.void, settle(Effect.suspend(() => outcome(...callHost(request.host, request.args)))));
|
|
188
|
+
};
|
|
189
|
+
return extensions.flatMap((extension) => Object.entries(extension.globals).map(([name, value]) => [name, fromHost(value, name, 0, new Set(), extension.name)]));
|
|
190
|
+
};
|
|
191
|
+
// An asynchronous host call: the program holds a promise that settles when the host's work does.
|
|
192
|
+
const pending = (ctx, call, site) => {
|
|
193
|
+
const resolving = ctx.vm.scheduler.resolving(ctx.vm.scheduler.newPromise(), site);
|
|
194
|
+
ctx.vm.hosts.host({ kind: "extension", call }, { label: call.label, result: "value", resolving, site });
|
|
195
|
+
return resolving.promise;
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* Calls a synchronous extension function's host function, once its `extension.before` hook allowed it: a promise for
|
|
199
|
+
* a result that settles later (an Effect or a Promise), or the request that settles what it returned or threw now.
|
|
200
|
+
*/
|
|
201
|
+
export const startExtensionCall = (ctx, host, args, call, site) => {
|
|
202
|
+
const [result, threw] = callHost(host, args);
|
|
203
|
+
const request = { stage: "settle", ...call, host: result, args, threw };
|
|
204
|
+
if (!threw && (Effect.isEffect(result) || result instanceof Promise))
|
|
205
|
+
return pending(ctx, request, site);
|
|
206
|
+
return request;
|
|
207
|
+
};
|
|
208
|
+
/** Calls a host function: what it returned, or what it threw. */
|
|
209
|
+
const callHost = (value, args) => {
|
|
210
|
+
try {
|
|
211
|
+
return [value.apply(undefined, args), false];
|
|
212
|
+
}
|
|
213
|
+
catch (reason) {
|
|
214
|
+
return [reason, true];
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* What a host function's call settles as: a returned Effect or Promise settles later; a returned value or a throw
|
|
219
|
+
* settles now.
|
|
220
|
+
*/
|
|
221
|
+
const outcome = (result, threw) => {
|
|
222
|
+
if (threw)
|
|
223
|
+
return Effect.fail(result);
|
|
224
|
+
// Extension.Services adds an Effect global's requirements to R.
|
|
225
|
+
if (Effect.isEffect(result))
|
|
226
|
+
return result;
|
|
227
|
+
if (result instanceof Promise)
|
|
228
|
+
return Effect.tryPromise({ try: () => result, catch: (reason) => reason });
|
|
229
|
+
return Effect.succeed(result);
|
|
230
|
+
};
|
|
231
|
+
/**
|
|
232
|
+
* An error crosses as its name, message, `cause`, and own enumerable fields, such as Node's `code`, `errno`,
|
|
233
|
+
* `syscall`, and `path`. `stack` stays on its own side, `message` crosses once as text, and no field may shadow an
|
|
234
|
+
* Error method. A field that cannot cross (a socket, a handle, a function) is left behind so the error itself always
|
|
235
|
+
* arrives.
|
|
236
|
+
*/
|
|
237
|
+
const uncrossed = new Set(["stack", "message", "constructor", "toString", "__proto__"]);
|
|
238
|
+
const left = Symbol("left behind");
|
|
239
|
+
const crossing = (convert) => {
|
|
240
|
+
try {
|
|
241
|
+
return convert();
|
|
242
|
+
}
|
|
243
|
+
catch (reason) {
|
|
244
|
+
if (reason instanceof PendingThrow)
|
|
245
|
+
return left;
|
|
246
|
+
throw reason;
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
const hostErrors = new Map([
|
|
250
|
+
["TypeError", TypeError],
|
|
251
|
+
["RangeError", RangeError],
|
|
252
|
+
["SyntaxError", SyntaxError],
|
|
253
|
+
["ReferenceError", ReferenceError],
|
|
254
|
+
["EvalError", EvalError],
|
|
255
|
+
["URIError", URIError],
|
|
256
|
+
]);
|
|
257
|
+
// The primitives the interpreter operates on; symbols and BigInts are not among them.
|
|
258
|
+
const isPrimitive = (value) => value === null ||
|
|
259
|
+
value === undefined ||
|
|
260
|
+
typeof value === "string" ||
|
|
261
|
+
typeof value === "number" ||
|
|
262
|
+
typeof value === "boolean";
|
|
263
|
+
const describeHost = (value) => {
|
|
264
|
+
if (typeof value !== "object" || value === null)
|
|
265
|
+
return `a ${typeof value}`;
|
|
266
|
+
const name = value.constructor?.name;
|
|
267
|
+
if (name === undefined || name === "")
|
|
268
|
+
return "an object";
|
|
269
|
+
return `${/^[AEIO]/.test(name) ? "an" : "a"} ${name}`;
|
|
270
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Interpreter } from "./interpreter.js";
|
|
2
|
+
/**
|
|
3
|
+
* `next`/`return`/`throw` on the generator prototypes (async generators answer with promises), on iterator helpers,
|
|
4
|
+
* and on the async-from-sync iterator. A generator's suspended body runs on the caller's job stack.
|
|
5
|
+
*/
|
|
6
|
+
export declare const generatorGlobals: <R>(ctx: Interpreter<R>) => void;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { fn, methods, receiver } from "./native.js";
|
|
2
|
+
import { AsyncIteratorSymbol, IteratorSymbol, typeError } from "./model.js";
|
|
3
|
+
import { define, hidden, GeneratorObj } from "./objects.js";
|
|
4
|
+
import { Push } from "./signals.js";
|
|
5
|
+
import { AsyncFromSyncIterator, generatorRequest, generatorStep } from "../generators.js";
|
|
6
|
+
/**
|
|
7
|
+
* `next`/`return`/`throw` on the generator prototypes (async generators answer with promises), on iterator helpers,
|
|
8
|
+
* and on the async-from-sync iterator. A generator's suspended body runs on the caller's job stack.
|
|
9
|
+
*/
|
|
10
|
+
export const generatorGlobals = (ctx) => {
|
|
11
|
+
const builtins = ctx.builtins;
|
|
12
|
+
const sync = (kind, owner = "Generator.prototype") => [
|
|
13
|
+
kind,
|
|
14
|
+
1,
|
|
15
|
+
(thisValue, args) => {
|
|
16
|
+
const method = `${owner}.${kind}`;
|
|
17
|
+
const generator = receiver(GeneratorObj, thisValue, method);
|
|
18
|
+
if (generator.asynchronous)
|
|
19
|
+
throw typeError(`${method} called on incompatible receiver an async generator.`);
|
|
20
|
+
return generatorStep(ctx.vm, generator, kind, args[0]);
|
|
21
|
+
},
|
|
22
|
+
];
|
|
23
|
+
const async = (kind) => [
|
|
24
|
+
kind,
|
|
25
|
+
1,
|
|
26
|
+
(thisValue, args) => generatorRequest(ctx.vm, thisValue, kind, args[0]),
|
|
27
|
+
];
|
|
28
|
+
const adapter = (kind) => [
|
|
29
|
+
kind,
|
|
30
|
+
1,
|
|
31
|
+
(thisValue, args) => new Push("AsyncFromSyncIterator", [
|
|
32
|
+
receiver(AsyncFromSyncIterator, thisValue, `AsyncFromSyncIterator.${kind}`),
|
|
33
|
+
kind,
|
|
34
|
+
args,
|
|
35
|
+
]),
|
|
36
|
+
];
|
|
37
|
+
methods(builtins, builtins.Generator, [sync("next"), sync("return"), sync("throw")]);
|
|
38
|
+
// Iterator helpers are the prelude's generators (see prelude.ts).
|
|
39
|
+
methods(builtins, builtins.IteratorHelper, [sync("next", "%IteratorHelper%"), sync("return", "%IteratorHelper%")]);
|
|
40
|
+
methods(builtins, builtins.AsyncGenerator, [async("next"), async("return"), async("throw")]);
|
|
41
|
+
methods(builtins, builtins.AsyncFromSyncIterator, [adapter("next"), adapter("return"), adapter("throw")]);
|
|
42
|
+
define(builtins.Iterator, IteratorSymbol, fn(builtins, "[Symbol.iterator]", 0, (thisValue) => thisValue), hidden);
|
|
43
|
+
define(builtins.AsyncIterator, AsyncIteratorSymbol, fn(builtins, "[Symbol.asyncIterator]", 0, (thisValue) => thisValue), hidden);
|
|
44
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type Value } from "./objects.js";
|
|
2
|
+
import type { Interpreter } from "./interpreter.js";
|
|
3
|
+
/** Names bound in every program before extensions apply. */
|
|
4
|
+
export declare const globalNames: ReadonlySet<string>;
|
|
5
|
+
/** The immutable global bindings of every program, in declaration order. */
|
|
6
|
+
export declare const globals: <R>(ctx: Interpreter<R>) => ReadonlyArray<readonly [string, Value]>;
|