@opencode/codemode 0.0.0-beta-19296 → 0.0.0-beta-19365
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 +6 -0
- package/dist/codemode.d.ts +8 -11
- package/dist/codemode.js +4 -8
- package/dist/data.d.ts +28 -0
- package/dist/data.js +130 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interpreter/errors.d.ts +1 -1
- package/dist/interpreter/errors.js +2 -2
- package/dist/interpreter/execute.d.ts +3 -3
- package/dist/interpreter/execute.js +9 -15
- package/dist/interpreter/methods.d.ts +9 -2
- package/dist/interpreter/methods.js +54 -76
- package/dist/interpreter/model.d.ts +12 -32
- package/dist/interpreter/model.js +0 -31
- package/dist/interpreter/promises.d.ts +8 -8
- package/dist/interpreter/promises.js +4 -4
- package/dist/interpreter/references.js +12 -42
- package/dist/interpreter/runtime.d.ts +2 -3
- package/dist/interpreter/runtime.js +255 -311
- package/dist/openapi/spec.js +1 -1
- package/dist/stdlib/console.js +14 -14
- package/dist/stdlib/date.d.ts +2 -2
- package/dist/stdlib/date.js +1 -1
- package/dist/stdlib/json.js +17 -26
- package/dist/stdlib/number.d.ts +1 -1
- package/dist/stdlib/number.js +4 -3
- package/dist/stdlib/object.js +11 -10
- package/dist/stdlib/regexp.d.ts +2 -2
- package/dist/stdlib/regexp.js +3 -3
- package/dist/stdlib/string.d.ts +1 -1
- package/dist/stdlib/string.js +1 -1
- package/dist/stdlib/url.d.ts +3 -3
- package/dist/stdlib/url.js +7 -6
- package/dist/stdlib/value.d.ts +2 -3
- package/dist/stdlib/value.js +14 -15
- package/dist/tool-runtime.d.ts +16 -15
- package/dist/tool-runtime.js +13 -150
- package/dist/values.d.ts +22 -16
- package/dist/values.js +23 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -91,6 +91,12 @@ runtime.execute(source) // Effect<CodeMode.Result, never, ToolServices>
|
|
|
91
91
|
The Effect environment is inferred from the supplied tools. `onToolCallStart` observes admitted calls with decoded
|
|
92
92
|
input; `onToolCallEnd` observes settled outcomes and duration. Both hooks return Effects and must not fail.
|
|
93
93
|
|
|
94
|
+
### `Values`
|
|
95
|
+
|
|
96
|
+
`Values` exports the runtime's non-JSON value classes: `Values.URL`, `Values.URLSearchParams`, `Values.Date`,
|
|
97
|
+
`Values.RegExp`, `Values.Map`, `Values.Set`, and `Values.Promise`. The interpreter recognizes these by class; a
|
|
98
|
+
program's `new URL(...)` is a `Values.URL` wrapping the host `URL`. `Values.isValue` narrows to the data-like kinds.
|
|
99
|
+
|
|
94
100
|
### OpenAPI tools
|
|
95
101
|
|
|
96
102
|
`OpenAPI.fromSpec` converts an OpenAPI 3.x document into one tool per supported operation. Dotted `operationId` values
|
package/dist/codemode.d.ts
CHANGED
|
@@ -25,23 +25,20 @@ export type ResolvedExecutionLimits = {
|
|
|
25
25
|
readonly maxToolCalls: number | undefined;
|
|
26
26
|
readonly maxOutputBytes: number | undefined;
|
|
27
27
|
};
|
|
28
|
-
/**
|
|
29
|
-
export type
|
|
30
|
-
/** Source for one program in the supported JavaScript subset. */
|
|
31
|
-
code: string;
|
|
28
|
+
/** Configuration shared by `CodeMode.make` and `CodeMode.execute`. */
|
|
29
|
+
export type Options<Provided extends Record<string, unknown> = {}> = ToolRuntime.ToolCallHooks<Services<Provided>> & {
|
|
32
30
|
/** Explicit tools exposed to the program as `tools`. */
|
|
33
31
|
tools?: Provided & Tools<Services<Provided>>;
|
|
34
|
-
/**
|
|
32
|
+
/** Resource limits enforced on each execution. */
|
|
35
33
|
limits?: ExecutionLimits;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
};
|
|
35
|
+
/** Options for one CodeMode execution. */
|
|
36
|
+
export type ExecuteOptions<Provided extends Record<string, unknown> = {}> = Options<Provided> & {
|
|
37
|
+
/** Source for one program in the supported JavaScript subset. */
|
|
38
|
+
code: string;
|
|
40
39
|
};
|
|
41
40
|
/** A JSON value that can cross the confined interpreter boundary. */
|
|
42
41
|
export type DataValue = Schema.Json;
|
|
43
|
-
/** Configuration shared by `CodeMode.make` and `CodeMode.execute`. */
|
|
44
|
-
export type Options<Provided extends Record<string, unknown> = {}> = Omit<ExecuteOptions<Provided>, "code">;
|
|
45
42
|
/** Schema for a host tool input containing CodeMode source. */
|
|
46
43
|
export declare const Input: Schema.Struct<{
|
|
47
44
|
readonly code: Schema.String;
|
package/dist/codemode.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
|
-
import {
|
|
2
|
+
import { executeProgram } from "./interpreter/execute.js";
|
|
3
3
|
import { ToolRuntime } from "./tool-runtime.js";
|
|
4
4
|
/** Signature-construction helpers for host-owned catalog instructions. */
|
|
5
5
|
export { searchSignature, toolExpression } from "./tool-runtime.js";
|
|
@@ -54,17 +54,13 @@ const resolveExecutionLimits = (limits) => ({
|
|
|
54
54
|
maxOutputBytes: validateLimit("maxOutputBytes", limits?.maxOutputBytes, 0),
|
|
55
55
|
});
|
|
56
56
|
/** Executes one Effect-native CodeMode program without constructing a reusable runtime. */
|
|
57
|
-
export const execute = (options) =>
|
|
58
|
-
const tools = (options.tools ?? {});
|
|
59
|
-
return executeWithLimits(options, resolveExecutionLimits(options.limits), ToolRuntime.searchIndex(tools));
|
|
60
|
-
};
|
|
57
|
+
export const execute = (options) => make(options).execute(options.code);
|
|
61
58
|
/** Creates an Effect-native runtime over explicit, schema-described tools. */
|
|
62
59
|
export const make = (options = {}) => {
|
|
63
|
-
const
|
|
60
|
+
const prepared = ToolRuntime.prepare((options.tools ?? {}));
|
|
64
61
|
const limits = resolveExecutionLimits(options.limits);
|
|
65
|
-
const prepared = ToolRuntime.prepare(tools);
|
|
66
62
|
return {
|
|
67
63
|
catalog: () => prepared.catalog,
|
|
68
|
-
execute: (code) =>
|
|
64
|
+
execute: (code) => executeProgram(code, prepared, limits, options),
|
|
69
65
|
};
|
|
70
66
|
};
|
package/dist/data.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export * as Data from "./data.js";
|
|
2
|
+
import type { DiagnosticKind } from "./codemode.js";
|
|
3
|
+
/** A null-prototype object owned by the program. */
|
|
4
|
+
export type SafeObject = Record<string, unknown>;
|
|
5
|
+
export declare class ToolRuntimeError extends Error {
|
|
6
|
+
readonly kind: Extract<DiagnosticKind, "UnknownTool" | "InvalidToolInput" | "InvalidToolOutput" | "InvalidDataValue" | "ToolCallLimitExceeded">;
|
|
7
|
+
readonly suggestions: ReadonlyArray<string>;
|
|
8
|
+
constructor(kind: Extract<DiagnosticKind, "UnknownTool" | "InvalidToolInput" | "InvalidToolOutput" | "InvalidDataValue" | "ToolCallLimitExceeded">, message: string, suggestions?: ReadonlyArray<string>);
|
|
9
|
+
}
|
|
10
|
+
export declare const isBlockedMember: (name: string) => boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Brings a host-produced runtime value into the program: runtime values pass through, their host
|
|
13
|
+
* counterparts (Date, RegExp, Map, Set, URL, URLSearchParams) are wrapped, and objects become
|
|
14
|
+
* null-prototype copies. Arrays keep extra enumerable properties such as `index` and `groups`.
|
|
15
|
+
*/
|
|
16
|
+
export declare const toProgram: (value: unknown, label: string) => unknown;
|
|
17
|
+
/**
|
|
18
|
+
* Brings host data into the program: Date and URL become strings, other host collections become
|
|
19
|
+
* empty objects, and objects become null-prototype copies. Used for tool results and parsed JSON.
|
|
20
|
+
*/
|
|
21
|
+
export declare const fromData: (value: unknown, label: string) => unknown;
|
|
22
|
+
/**
|
|
23
|
+
* Takes a program value out as plain JSON: runtime values serialize like `JSON.stringify` would,
|
|
24
|
+
* non-finite numbers become null, and array holes become null. `undefined` object properties are
|
|
25
|
+
* dropped ("json") or become null ("result", for program results where the consumer must never see
|
|
26
|
+
* undefined); a bare `undefined` follows the same rule.
|
|
27
|
+
*/
|
|
28
|
+
export declare const toData: (value: unknown, label: string, undefinedAs?: "json" | "result") => unknown;
|
package/dist/data.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
export * as Data from "./data.js";
|
|
2
|
+
import { Values } from "./values.js";
|
|
3
|
+
const MAX_VALUE_DEPTH = 32;
|
|
4
|
+
export class ToolRuntimeError extends Error {
|
|
5
|
+
kind;
|
|
6
|
+
suggestions;
|
|
7
|
+
constructor(kind, message, suggestions = []) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.kind = kind;
|
|
10
|
+
this.suggestions = suggestions;
|
|
11
|
+
this.name = "ToolRuntimeError";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
const blockedMemberNames = new Set(["__proto__", "constructor", "prototype"]);
|
|
15
|
+
export const isBlockedMember = (name) => blockedMemberNames.has(name);
|
|
16
|
+
/**
|
|
17
|
+
* Brings a host-produced runtime value into the program: runtime values pass through, their host
|
|
18
|
+
* counterparts (Date, RegExp, Map, Set, URL, URLSearchParams) are wrapped, and objects become
|
|
19
|
+
* null-prototype copies. Arrays keep extra enumerable properties such as `index` and `groups`.
|
|
20
|
+
*/
|
|
21
|
+
export const toProgram = (value, label) => copy(value, label, "program", 0, new Set());
|
|
22
|
+
/**
|
|
23
|
+
* Brings host data into the program: Date and URL become strings, other host collections become
|
|
24
|
+
* empty objects, and objects become null-prototype copies. Used for tool results and parsed JSON.
|
|
25
|
+
*/
|
|
26
|
+
export const fromData = (value, label) => copy(value, label, "data", 0, new Set());
|
|
27
|
+
/**
|
|
28
|
+
* Takes a program value out as plain JSON: runtime values serialize like `JSON.stringify` would,
|
|
29
|
+
* non-finite numbers become null, and array holes become null. `undefined` object properties are
|
|
30
|
+
* dropped ("json") or become null ("result", for program results where the consumer must never see
|
|
31
|
+
* undefined); a bare `undefined` follows the same rule.
|
|
32
|
+
*/
|
|
33
|
+
export const toData = (value, label, undefinedAs = "json") => copy(value, label, undefinedAs, 0, new Set());
|
|
34
|
+
const copy = (value, label, mode, depth, seen) => {
|
|
35
|
+
if (depth > MAX_VALUE_DEPTH) {
|
|
36
|
+
throw new ToolRuntimeError("InvalidDataValue", `${label} exceeds the maximum value depth of ${MAX_VALUE_DEPTH}.`);
|
|
37
|
+
}
|
|
38
|
+
if (value === undefined)
|
|
39
|
+
return mode === "result" ? null : undefined;
|
|
40
|
+
if (typeof value === "number")
|
|
41
|
+
return (mode === "json" || mode === "result") && !Number.isFinite(value) ? null : value;
|
|
42
|
+
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
43
|
+
return value;
|
|
44
|
+
if (typeof value !== "object") {
|
|
45
|
+
throw new ToolRuntimeError("InvalidDataValue", `${label} must contain data only.`);
|
|
46
|
+
}
|
|
47
|
+
if (value instanceof Values.Promise) {
|
|
48
|
+
throw new ToolRuntimeError("InvalidDataValue", `${label} contains an un-awaited Promise; await tool calls (e.g. \`const result = await tools.ns.tool(...)\`) before using their results.`);
|
|
49
|
+
}
|
|
50
|
+
if (mode === "program") {
|
|
51
|
+
if (Values.isValue(value))
|
|
52
|
+
return value;
|
|
53
|
+
if (value instanceof Date)
|
|
54
|
+
return new Values.Date(value.getTime());
|
|
55
|
+
if (value instanceof RegExp)
|
|
56
|
+
return new Values.RegExp(value.source, value.flags);
|
|
57
|
+
if (value instanceof Map) {
|
|
58
|
+
const wrapped = new Values.Map();
|
|
59
|
+
for (const [key, item] of value.entries()) {
|
|
60
|
+
wrapped.map.set(copy(key, label, mode, depth + 1, seen), copy(item, label, mode, depth + 1, seen));
|
|
61
|
+
}
|
|
62
|
+
return wrapped;
|
|
63
|
+
}
|
|
64
|
+
if (value instanceof Set) {
|
|
65
|
+
const wrapped = new Values.Set();
|
|
66
|
+
for (const item of value.values())
|
|
67
|
+
wrapped.set.add(copy(item, label, mode, depth + 1, seen));
|
|
68
|
+
return wrapped;
|
|
69
|
+
}
|
|
70
|
+
if (value instanceof URL)
|
|
71
|
+
return new Values.URL(new URL(value.href));
|
|
72
|
+
if (value instanceof URLSearchParams)
|
|
73
|
+
return new Values.URLSearchParams(new URLSearchParams(value));
|
|
74
|
+
}
|
|
75
|
+
const plain = mode === "program" || mode === "data";
|
|
76
|
+
if (value instanceof Values.Date)
|
|
77
|
+
return Number.isFinite(value.time) ? new Date(value.time).toISOString() : null;
|
|
78
|
+
if (value instanceof Date)
|
|
79
|
+
return Number.isFinite(value.getTime()) ? value.toISOString() : null;
|
|
80
|
+
if (value instanceof Values.URL)
|
|
81
|
+
return value.url.href;
|
|
82
|
+
if (value instanceof URL)
|
|
83
|
+
return value.href;
|
|
84
|
+
// Remaining runtime values and their host counterparts serialize as empty objects, like JSON.stringify.
|
|
85
|
+
if (Values.isValue(value) ||
|
|
86
|
+
value instanceof RegExp ||
|
|
87
|
+
value instanceof Map ||
|
|
88
|
+
value instanceof Set ||
|
|
89
|
+
value instanceof URLSearchParams) {
|
|
90
|
+
return plain ? Object.create(null) : {};
|
|
91
|
+
}
|
|
92
|
+
if (seen.has(value)) {
|
|
93
|
+
throw new ToolRuntimeError("InvalidDataValue", `${label} contains a circular value.`);
|
|
94
|
+
}
|
|
95
|
+
seen.add(value);
|
|
96
|
+
if (Array.isArray(value)) {
|
|
97
|
+
// Host output densifies holes to null like JSON; program copies keep them.
|
|
98
|
+
const copied = plain
|
|
99
|
+
? value.map((item) => copy(item, label, mode, depth + 1, seen))
|
|
100
|
+
: Array.from(value, (item) => copy(item, label, mode, depth + 1, seen) ?? null);
|
|
101
|
+
if (mode === "program") {
|
|
102
|
+
for (const [key, item] of Object.entries(value)) {
|
|
103
|
+
if (Object.hasOwn(copied, key))
|
|
104
|
+
continue;
|
|
105
|
+
if (isBlockedMember(key)) {
|
|
106
|
+
throw new ToolRuntimeError("InvalidDataValue", `${label} contains blocked property '${key}'.`);
|
|
107
|
+
}
|
|
108
|
+
Reflect.set(copied, key, copy(item, label, mode, depth + 1, seen));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
seen.delete(value);
|
|
112
|
+
return copied;
|
|
113
|
+
}
|
|
114
|
+
const prototype = Object.getPrototypeOf(value);
|
|
115
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
116
|
+
throw new ToolRuntimeError("InvalidDataValue", `${label} must contain plain objects only.`);
|
|
117
|
+
}
|
|
118
|
+
const copied = plain ? Object.create(null) : {};
|
|
119
|
+
for (const [key, item] of Object.entries(value)) {
|
|
120
|
+
if (isBlockedMember(key)) {
|
|
121
|
+
throw new ToolRuntimeError("InvalidDataValue", `${label} contains blocked property '${key}'.`);
|
|
122
|
+
}
|
|
123
|
+
const next = copy(item, label, mode, depth + 1, seen);
|
|
124
|
+
if (next === undefined && mode === "json")
|
|
125
|
+
continue;
|
|
126
|
+
copied[key] = next;
|
|
127
|
+
}
|
|
128
|
+
seen.delete(value);
|
|
129
|
+
return copied;
|
|
130
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -2,5 +2,6 @@ export * as CodeMode from "./codemode.js";
|
|
|
2
2
|
export * as Namespace from "./namespace.js";
|
|
3
3
|
export * as Tool from "./tool.js";
|
|
4
4
|
export * as OpenAPI from "./openapi/index.js";
|
|
5
|
+
export { Values } from "./values.js";
|
|
5
6
|
export { searchSignature, toolExpression } from "./codemode.js";
|
|
6
7
|
export { ToolError, toolError } from "./tool-error.js";
|
package/dist/index.js
CHANGED
|
@@ -2,5 +2,6 @@ export * as CodeMode from "./codemode.js";
|
|
|
2
2
|
export * as Namespace from "./namespace.js";
|
|
3
3
|
export * as Tool from "./tool.js";
|
|
4
4
|
export * as OpenAPI from "./openapi/index.js";
|
|
5
|
+
export { Values } from "./values.js";
|
|
5
6
|
export { searchSignature, toolExpression } from "./codemode.js";
|
|
6
7
|
export { ToolError, toolError } from "./tool-error.js";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import type { Diagnostic } from "../codemode.js";
|
|
3
|
-
import { type SafeObject } from "../
|
|
3
|
+
import { type SafeObject } from "../data.js";
|
|
4
4
|
import { type AstNode } from "./model.js";
|
|
5
5
|
import { type SyncIteratorRunner } from "./iterator.js";
|
|
6
6
|
export declare const normalizeError: (error: unknown) => Diagnostic;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { ToolError } from "../tool-error.js";
|
|
3
|
-
import {
|
|
3
|
+
import { toData, ToolRuntimeError } from "../data.js";
|
|
4
4
|
import { formatLocation, InterpreterRuntimeError, ProgramThrow, sourceLocation } from "./model.js";
|
|
5
5
|
import { containsRuntimeReference } from "./references.js";
|
|
6
6
|
import {} from "./iterator.js";
|
|
@@ -41,7 +41,7 @@ export const normalizeError = (error) => {
|
|
|
41
41
|
}
|
|
42
42
|
else {
|
|
43
43
|
try {
|
|
44
|
-
message = JSON.stringify(
|
|
44
|
+
message = JSON.stringify(toData(value, "Thrown value")) ?? String(value);
|
|
45
45
|
}
|
|
46
46
|
catch {
|
|
47
47
|
message = String(value);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
-
import type {
|
|
3
|
-
import { ToolRuntime
|
|
4
|
-
export declare const
|
|
2
|
+
import type { ResolvedExecutionLimits, Result } from "../codemode.js";
|
|
3
|
+
import { ToolRuntime } from "../tool-runtime.js";
|
|
4
|
+
export declare const executeProgram: <R>(code: string, prepared: ToolRuntime.Prepared<R>, limits: ResolvedExecutionLimits, hooks: ToolRuntime.ToolCallHooks<R>) => Effect.Effect<Result, never, R>;
|
|
@@ -3,13 +3,14 @@ import { Cause, Effect, Scope } from "effect";
|
|
|
3
3
|
// #transpile: conditional import — full typescript on node/bun, an identity
|
|
4
4
|
// pass-through on workerd (the compiler is ~11 MiB and can't init there).
|
|
5
5
|
import { transpile } from "#transpile";
|
|
6
|
-
import {
|
|
6
|
+
import { toData } from "../data.js";
|
|
7
|
+
import { ToolRuntime } from "../tool-runtime.js";
|
|
7
8
|
import { normalizeError } from "./errors.js";
|
|
8
|
-
import { InterpreterRuntimeError
|
|
9
|
+
import { InterpreterRuntimeError } from "./model.js";
|
|
9
10
|
import { PromiseRuntime } from "./promises.js";
|
|
10
11
|
import { Interpreter } from "./runtime.js";
|
|
11
|
-
export const
|
|
12
|
-
if (
|
|
12
|
+
export const executeProgram = (code, prepared, limits, hooks) => {
|
|
13
|
+
if (code.trim().length === 0) {
|
|
13
14
|
return Effect.succeed({
|
|
14
15
|
ok: false,
|
|
15
16
|
error: { kind: "ParseError", message: "Code cannot be empty." },
|
|
@@ -18,20 +19,17 @@ export const executeWithLimits = (options, limits, searchIndex) => {
|
|
|
18
19
|
}
|
|
19
20
|
// Allocate execution state inside suspension so reused Effects never share it.
|
|
20
21
|
return Effect.suspend(() => {
|
|
21
|
-
const tools = ToolRuntime.make(
|
|
22
|
-
onToolCallStart: options.onToolCallStart,
|
|
23
|
-
onToolCallEnd: options.onToolCallEnd,
|
|
24
|
-
});
|
|
22
|
+
const tools = ToolRuntime.make(prepared, limits.maxToolCalls, hooks);
|
|
25
23
|
const logs = [];
|
|
26
24
|
const logged = () => (logs.length > 0 ? { logs: [...logs] } : {});
|
|
27
25
|
// Set only after copy-out so timeouts cannot report invalid values as completed.
|
|
28
26
|
let returned;
|
|
29
27
|
const base = Effect.acquireUseRelease(Scope.make("parallel"), (scope) => Effect.gen(function* () {
|
|
30
|
-
const program = parseProgram(
|
|
28
|
+
const program = parseProgram(code);
|
|
31
29
|
const promises = new PromiseRuntime(scope);
|
|
32
30
|
const interpreter = new Interpreter(tools.execute, tools.search, tools.keys, promises, logs);
|
|
33
31
|
const value = yield* interpreter.run(program);
|
|
34
|
-
const result =
|
|
32
|
+
const result = toData(value, "Execution result", "result");
|
|
35
33
|
returned = { value: result, promises };
|
|
36
34
|
const warnings = yield* promises.interrupt();
|
|
37
35
|
return {
|
|
@@ -90,17 +88,13 @@ const parseProgram = (code) => {
|
|
|
90
88
|
const bodyStart = transpiled.outputText.indexOf("{") + 1;
|
|
91
89
|
const bodyEnd = transpiled.outputText.lastIndexOf("}");
|
|
92
90
|
const executableCode = transpiled.outputText.slice(bodyStart, bodyEnd);
|
|
93
|
-
|
|
91
|
+
return parse(executableCode, {
|
|
94
92
|
ecmaVersion: "latest",
|
|
95
93
|
sourceType: "script",
|
|
96
94
|
allowReturnOutsideFunction: true,
|
|
97
95
|
allowAwaitOutsideFunction: true,
|
|
98
96
|
locations: true,
|
|
99
97
|
});
|
|
100
|
-
if (!isRecord(parsed) || parsed.type !== "Program" || !Array.isArray(parsed.body)) {
|
|
101
|
-
throw new InterpreterRuntimeError("Failed to parse script as a Program node.");
|
|
102
|
-
}
|
|
103
|
-
return parsed;
|
|
104
98
|
};
|
|
105
99
|
const utf8ByteLength = (value) => new TextEncoder().encode(value).byteLength;
|
|
106
100
|
// Drop a replacement character produced by truncating inside a UTF-8 sequence.
|
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { type AstNode, CodeModeFunction, CoercionFunction, ErrorConstructorReference, GlobalMethodReference, GlobalNamespace, IntrinsicReference, JsonMethodReference, PromiseCapabilityFunction, PromiseNamespace, UriFunction } from "./model.js";
|
|
3
|
-
import {
|
|
3
|
+
import { Values } from "../values.js";
|
|
4
4
|
import { type SyncIteratorRunner } from "./iterator.js";
|
|
5
5
|
export type CallbackRunner<R> = {
|
|
6
6
|
readonly invokeFunction: (fn: CodeModeFunction, args: Array<unknown>) => Effect.Effect<unknown, unknown, R>;
|
|
7
7
|
readonly invokeCallable: (callable: unknown, args: Array<unknown>, node: AstNode) => Effect.Effect<unknown, unknown, R>;
|
|
8
|
-
readonly settlePromise: (promise:
|
|
8
|
+
readonly settlePromise: (promise: Values.Promise) => Effect.Effect<unknown, unknown, never>;
|
|
9
9
|
};
|
|
10
10
|
export type SupportedCallback = CodeModeFunction | CoercionFunction | UriFunction | PromiseCapabilityFunction | GlobalMethodReference | JsonMethodReference | IntrinsicReference | ErrorConstructorReference | GlobalNamespace | PromiseNamespace;
|
|
11
11
|
export declare const isSupportedCallback: (value: unknown) => value is SupportedCallback;
|
|
12
12
|
export declare const invokeIntrinsic: <R>(runner: CallbackRunner<R>, ref: IntrinsicReference, args: Array<unknown>, node: AstNode) => Effect.Effect<unknown, unknown, R>;
|
|
13
|
+
/**
|
|
14
|
+
* ToPrimitive: tries an object's own `valueOf`/`toString` in hint order and returns the first
|
|
15
|
+
* primitive result. Runtime values behave like their JS counterparts (Date yields its time under a
|
|
16
|
+
* number hint; the rest yield their string form). An inherited `toString` yields the default
|
|
17
|
+
* string form, so plain objects become "[object Object]" and arrays join.
|
|
18
|
+
*/
|
|
19
|
+
export declare const toPrimitive: <R>(runner: CallbackRunner<R>, value: unknown, hint: "number" | "string", node: AstNode) => Effect.Effect<unknown, unknown, R>;
|
|
13
20
|
export declare const invokeGlobalMethod: (ref: GlobalMethodReference, args: Array<unknown>, node: AstNode) => unknown;
|
|
14
21
|
export declare const arrayStatics: Set<string>;
|
|
15
22
|
export declare const invokeArrayFrom: <R>(runner: CallbackRunner<R> & SyncIteratorRunner<R>, args: Array<unknown>, node: AstNode) => Effect.Effect<unknown, unknown, R>;
|