@opencode/codemode 0.0.0-dev-19355 → 0.0.0-dev-19358
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/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/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.js +9 -8
- package/dist/interpreter/model.d.ts +7 -27
- package/dist/interpreter/model.js +0 -31
- package/dist/interpreter/runtime.d.ts +2 -2
- package/dist/interpreter/runtime.js +200 -235
- package/dist/openapi/spec.js +1 -1
- package/dist/stdlib/console.js +4 -4
- package/dist/stdlib/json.js +13 -15
- package/dist/stdlib/number.js +3 -2
- package/dist/stdlib/object.js +5 -4
- package/dist/stdlib/regexp.js +1 -1
- package/dist/stdlib/url.js +3 -2
- package/dist/stdlib/value.d.ts +1 -2
- package/dist/stdlib/value.js +3 -4
- package/dist/tool-runtime.d.ts +15 -16
- package/dist/tool-runtime.js +12 -141
- package/package.json +1 -1
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
|
+
};
|
|
@@ -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,7 +1,8 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { CodeModeFunction, CodeModeGenerator, CoercionFunction, ErrorConstructorReference, GlobalMethodReference, GlobalNamespace, IntrinsicReference, InterpreterRuntimeError, JsonMethodReference, PromiseCapabilityFunction, PromiseNamespace, UriFunction, } from "./model.js";
|
|
3
3
|
import { containsOpaqueReference, isRuntimeReference, rejectCircularInsertion, typeofValue } from "./references.js";
|
|
4
|
-
import {
|
|
4
|
+
import { isBlockedMember, toProgram } from "../data.js";
|
|
5
|
+
import { compareText } from "../tool-runtime.js";
|
|
5
6
|
import { Values } from "../values.js";
|
|
6
7
|
import { dateSetterArgumentCount, invokeDateMethod, invokeDateStatic } from "../stdlib/date.js";
|
|
7
8
|
import { invokeMathMethod } from "../stdlib/math.js";
|
|
@@ -10,7 +11,7 @@ import { invokeObjectMethod } from "../stdlib/object.js";
|
|
|
10
11
|
import { invokeRegExpMethod, invokeRegExpStatic, matchToValue, toHostRegex } from "../stdlib/regexp.js";
|
|
11
12
|
import { invokeStringStatic } from "../stdlib/string.js";
|
|
12
13
|
import { invokeURLMethod, invokeURLStatic, uriArgument } from "../stdlib/url.js";
|
|
13
|
-
import {
|
|
14
|
+
import { coerceToNumber, coerceToString, errorBrandName } from "../stdlib/value.js";
|
|
14
15
|
import { preserveConsumerError } from "./iterator.js";
|
|
15
16
|
export const isSupportedCallback = (value) => value instanceof CodeModeFunction ||
|
|
16
17
|
value instanceof CoercionFunction ||
|
|
@@ -227,7 +228,7 @@ const invokeStringMethod = (value, name, args, node) => {
|
|
|
227
228
|
return null;
|
|
228
229
|
// Preserve the own `index` and `groups` properties on non-global matches.
|
|
229
230
|
if (pattern.global)
|
|
230
|
-
return
|
|
231
|
+
return toProgram(matched, "String.match result");
|
|
231
232
|
return matchToValue(matched);
|
|
232
233
|
}
|
|
233
234
|
case "matchAll": {
|
|
@@ -279,7 +280,7 @@ const invokeStringMethod = (value, name, args, node) => {
|
|
|
279
280
|
default:
|
|
280
281
|
throw new InterpreterRuntimeError(`String method '${name}' is not available.`, node);
|
|
281
282
|
}
|
|
282
|
-
return
|
|
283
|
+
return toProgram(result, `String.${name} result`);
|
|
283
284
|
};
|
|
284
285
|
export const arrayStatics = new Set(["isArray", "of", "from"]);
|
|
285
286
|
const invokeArrayStatic = (name, args, node) => {
|
|
@@ -436,16 +437,16 @@ const invokeStringReplacer = (runner, value, name, args, node) => {
|
|
|
436
437
|
let end = 0;
|
|
437
438
|
for (const match of matches) {
|
|
438
439
|
const replacement = yield* apply(match.args);
|
|
439
|
-
// Error values are branded plain objects;
|
|
440
|
+
// Error values are branded plain objects; toProgram would strip the brand before coercion.
|
|
440
441
|
output.push(value.slice(end, match.offset), replacement instanceof Values.Promise
|
|
441
442
|
? "[object Promise]"
|
|
442
443
|
: errorBrandName(replacement)
|
|
443
444
|
? coerceToString(replacement)
|
|
444
|
-
: coerceToString(
|
|
445
|
+
: coerceToString(toProgram(replacement, `String.${name} replacer result`)));
|
|
445
446
|
end = match.offset + match.match.length;
|
|
446
447
|
}
|
|
447
448
|
output.push(value.slice(end));
|
|
448
|
-
return
|
|
449
|
+
return toProgram(output.join(""), `String.${name} result`);
|
|
449
450
|
});
|
|
450
451
|
};
|
|
451
452
|
export const applyCollectionCallback = (runner, callback, name, node) => {
|
|
@@ -737,7 +738,7 @@ const invokeArrayMethod = (runner, target, name, args, node) => {
|
|
|
737
738
|
if (args.length > 1 || (args.length === 1 && typeof args[0] !== "string")) {
|
|
738
739
|
throw new InterpreterRuntimeError("Array.join expects zero arguments or one string separator.", node);
|
|
739
740
|
}
|
|
740
|
-
const input =
|
|
741
|
+
const input = toProgram(target, "Array.join input");
|
|
741
742
|
return Effect.succeed(input.map((item) => coerceToString(item ?? "")).join(args.length === 0 ? "," : args[0]));
|
|
742
743
|
}
|
|
743
744
|
case "includes":
|
|
@@ -1,24 +1,10 @@
|
|
|
1
|
+
import type { BlockStatement, Expression, Node, Pattern } from "acorn";
|
|
1
2
|
import type { Effect } from "effect";
|
|
2
3
|
import type { DiagnosticKind } from "../codemode.js";
|
|
3
|
-
import type { SafeObject } from "../
|
|
4
|
+
import type { SafeObject } from "../data.js";
|
|
4
5
|
import type { Values } from "../values.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
column: number;
|
|
8
|
-
};
|
|
9
|
-
export type SourceLocation = {
|
|
10
|
-
start: SourcePosition;
|
|
11
|
-
end: SourcePosition;
|
|
12
|
-
};
|
|
13
|
-
export type AstNode = {
|
|
14
|
-
type: string;
|
|
15
|
-
loc?: SourceLocation;
|
|
16
|
-
[key: string]: unknown;
|
|
17
|
-
};
|
|
18
|
-
export type ProgramNode = AstNode & {
|
|
19
|
-
type: "Program";
|
|
20
|
-
body: Array<AstNode>;
|
|
21
|
-
};
|
|
6
|
+
/** Any parsed node; the interpreter narrows on `type` and reads `loc` for diagnostics. */
|
|
7
|
+
export type AstNode = Node;
|
|
22
8
|
export type Binding = {
|
|
23
9
|
mutable: boolean;
|
|
24
10
|
value: unknown;
|
|
@@ -41,12 +27,12 @@ export type MemberReference = {
|
|
|
41
27
|
key: PropertyKey;
|
|
42
28
|
};
|
|
43
29
|
export declare class CodeModeFunction {
|
|
44
|
-
readonly parameters: ReadonlyArray<
|
|
45
|
-
readonly body:
|
|
30
|
+
readonly parameters: ReadonlyArray<Pattern>;
|
|
31
|
+
readonly body: BlockStatement | Expression;
|
|
46
32
|
readonly capturedScopes: ReadonlyArray<Map<string, Binding>>;
|
|
47
33
|
readonly async: boolean;
|
|
48
34
|
readonly generator: boolean;
|
|
49
|
-
constructor(parameters: ReadonlyArray<
|
|
35
|
+
constructor(parameters: ReadonlyArray<Pattern>, body: BlockStatement | Expression, capturedScopes: ReadonlyArray<Map<string, Binding>>, async: boolean, generator: boolean);
|
|
50
36
|
}
|
|
51
37
|
export type GeneratorRequestKind = "next" | "return" | "throw";
|
|
52
38
|
export declare class CodeModeGenerator {
|
|
@@ -138,12 +124,6 @@ export declare class InterpreterRuntimeError extends Error {
|
|
|
138
124
|
}
|
|
139
125
|
export declare const unsupportedSyntax: (kind: string, node: AstNode) => InterpreterRuntimeError;
|
|
140
126
|
export declare const isRecord: (value: unknown) => value is Record<string, unknown>;
|
|
141
|
-
export declare const asNode: (value: unknown, context: string) => AstNode;
|
|
142
|
-
export declare const getArray: (node: AstNode, key: string) => Array<unknown>;
|
|
143
|
-
export declare const getString: (node: AstNode, key: string) => string;
|
|
144
|
-
export declare const getBoolean: (node: AstNode, key: string) => boolean;
|
|
145
|
-
export declare const getOptionalNode: (node: AstNode, key: string) => AstNode | undefined;
|
|
146
|
-
export declare const getNode: (node: AstNode, key: string) => AstNode;
|
|
147
127
|
export declare const sourceLocation: (node: AstNode) => {
|
|
148
128
|
readonly line: number;
|
|
149
129
|
readonly column: number;
|
|
@@ -143,37 +143,6 @@ export class InterpreterRuntimeError extends Error {
|
|
|
143
143
|
}
|
|
144
144
|
export const unsupportedSyntax = (kind, node) => new InterpreterRuntimeError(`Syntax '${kind}' is not supported. ${supportedSyntaxMessage}`, node, "UnsupportedSyntax", [supportedSyntaxMessage]);
|
|
145
145
|
export const isRecord = (value) => typeof value === "object" && value !== null;
|
|
146
|
-
export const asNode = (value, context) => {
|
|
147
|
-
if (!isRecord(value) || typeof value.type !== "string") {
|
|
148
|
-
throw new InterpreterRuntimeError(`Invalid AST node while reading ${context}.`);
|
|
149
|
-
}
|
|
150
|
-
return value;
|
|
151
|
-
};
|
|
152
|
-
export const getArray = (node, key) => {
|
|
153
|
-
const value = node[key];
|
|
154
|
-
if (!Array.isArray(value))
|
|
155
|
-
throw new InterpreterRuntimeError(`Expected '${key}' to be an array.`, node);
|
|
156
|
-
return value;
|
|
157
|
-
};
|
|
158
|
-
export const getString = (node, key) => {
|
|
159
|
-
const value = node[key];
|
|
160
|
-
if (typeof value !== "string")
|
|
161
|
-
throw new InterpreterRuntimeError(`Expected '${key}' to be a string.`, node);
|
|
162
|
-
return value;
|
|
163
|
-
};
|
|
164
|
-
export const getBoolean = (node, key) => {
|
|
165
|
-
const value = node[key];
|
|
166
|
-
if (typeof value !== "boolean")
|
|
167
|
-
throw new InterpreterRuntimeError(`Expected '${key}' to be a boolean.`, node);
|
|
168
|
-
return value;
|
|
169
|
-
};
|
|
170
|
-
export const getOptionalNode = (node, key) => {
|
|
171
|
-
const value = node[key];
|
|
172
|
-
if (value === undefined || value === null)
|
|
173
|
-
return undefined;
|
|
174
|
-
return asNode(value, key);
|
|
175
|
-
};
|
|
176
|
-
export const getNode = (node, key) => asNode(node[key], key);
|
|
177
146
|
export const sourceLocation = (node) => ({
|
|
178
147
|
line: Math.max(1, (node.loc?.start.line ?? 2) - 1),
|
|
179
148
|
column: Math.max(1, (node.loc?.start.column ?? 4) - 3),
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import type { Program } from "acorn";
|
|
1
2
|
import { Effect } from "effect";
|
|
2
|
-
import { type ProgramNode } from "./model.js";
|
|
3
3
|
import { PromiseRuntime } from "./promises.js";
|
|
4
4
|
export declare class Interpreter<R> {
|
|
5
5
|
private scopes;
|
|
@@ -12,7 +12,7 @@ export declare class Interpreter<R> {
|
|
|
12
12
|
private generatorAsync;
|
|
13
13
|
private readonly runner;
|
|
14
14
|
constructor(executeTool: (path: ReadonlyArray<string>, args: Array<unknown>) => Effect.Effect<unknown, unknown, R>, invokeSearch: (args: Array<unknown>) => Effect.Effect<unknown, unknown, R>, toolKeys: (path: ReadonlyArray<string>) => ReadonlyArray<string>, promises: PromiseRuntime<R>, logs?: Array<string>);
|
|
15
|
-
run(program:
|
|
15
|
+
run(program: Program): Effect.Effect<unknown, unknown, R>;
|
|
16
16
|
private createToolCallPromise;
|
|
17
17
|
private createPromise;
|
|
18
18
|
private settlePromise;
|