@opencode/codemode 0.0.0-beta-19296 → 0.0.0-beta-19378
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 +4 -6
- package/dist/interpreter/errors.js +22 -6
- package/dist/interpreter/execute.d.ts +3 -3
- package/dist/interpreter/execute.js +11 -18
- package/dist/interpreter/globals.d.ts +13 -0
- package/dist/interpreter/globals.js +59 -0
- package/dist/interpreter/host.d.ts +41 -0
- package/dist/interpreter/host.js +44 -0
- package/dist/interpreter/methods.d.ts +3 -16
- package/dist/interpreter/methods.js +42 -239
- package/dist/interpreter/model.d.ts +12 -73
- package/dist/interpreter/model.js +0 -87
- package/dist/interpreter/promises.d.ts +12 -13
- package/dist/interpreter/promises.js +49 -26
- package/dist/interpreter/references.js +23 -70
- package/dist/interpreter/runner.d.ts +23 -0
- package/dist/interpreter/runner.js +42 -0
- package/dist/interpreter/runtime.d.ts +13 -95
- package/dist/interpreter/runtime.js +290 -723
- package/dist/openapi/spec.js +1 -1
- package/dist/stdlib/array.d.ts +3 -0
- package/dist/stdlib/array.js +73 -0
- package/dist/stdlib/collections.d.ts +6 -1
- package/dist/stdlib/collections.js +120 -1
- package/dist/stdlib/console.d.ts +3 -2
- package/dist/stdlib/console.js +25 -16
- package/dist/stdlib/date.d.ts +5 -4
- package/dist/stdlib/date.js +28 -12
- package/dist/stdlib/json.d.ts +4 -4
- package/dist/stdlib/json.js +23 -28
- package/dist/stdlib/math.d.ts +3 -7
- package/dist/stdlib/math.js +85 -153
- package/dist/stdlib/number.d.ts +2 -4
- package/dist/stdlib/number.js +30 -37
- package/dist/stdlib/object.d.ts +4 -6
- package/dist/stdlib/object.js +106 -75
- package/dist/stdlib/regexp.d.ts +4 -6
- package/dist/stdlib/regexp.js +35 -12
- package/dist/stdlib/string.d.ts +1 -3
- package/dist/stdlib/string.js +15 -17
- package/dist/stdlib/url.d.ts +10 -6
- package/dist/stdlib/url.js +102 -25
- package/dist/stdlib/value.d.ts +6 -5
- package/dist/stdlib/value.js +33 -32
- 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/dist/interpreter/iterator.d.ts +0 -13
- package/dist/interpreter/iterator.js +0 -4
- package/dist/stdlib/promise.d.ts +0 -2
- package/dist/stdlib/promise.js +0 -1
package/dist/stdlib/string.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { sync } from "../interpreter/host.js";
|
|
2
|
+
import { InterpreterRuntimeError } from "../interpreter/model.js";
|
|
3
|
+
import { coercion } from "./value.js";
|
|
1
4
|
export const stringMethods = new Set([
|
|
2
5
|
"toLowerCase",
|
|
3
6
|
"toUpperCase",
|
|
@@ -29,20 +32,15 @@ export const stringMethods = new Set([
|
|
|
29
32
|
"localeCompare",
|
|
30
33
|
"normalize",
|
|
31
34
|
]);
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
default:
|
|
45
|
-
throw new InterpreterRuntimeError(`String.${name} is not available.`, node);
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
import { InterpreterRuntimeError } from "../interpreter/model.js";
|
|
35
|
+
const codeUnits = (name, op) => sync(`String.${name}`, (args, node) => op(...args.map((arg) => {
|
|
36
|
+
if (typeof arg !== "number")
|
|
37
|
+
throw new InterpreterRuntimeError(`String.${name} expects number arguments.`, node);
|
|
38
|
+
return arg;
|
|
39
|
+
})));
|
|
40
|
+
export const stringGlobal = coercion("String", {
|
|
41
|
+
instanceOf: () => false,
|
|
42
|
+
members: {
|
|
43
|
+
fromCharCode: codeUnits("fromCharCode", String.fromCharCode),
|
|
44
|
+
fromCodePoint: codeUnits("fromCodePoint", String.fromCodePoint),
|
|
45
|
+
},
|
|
46
|
+
});
|
package/dist/stdlib/url.d.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
+
import { HostFunction } from "../interpreter/host.js";
|
|
2
|
+
import { type AstNode } from "../interpreter/model.js";
|
|
3
|
+
import { type Runner } from "../interpreter/runner.js";
|
|
4
|
+
import { Values } from "../values.js";
|
|
1
5
|
export declare const urlProperties: Set<string>;
|
|
2
6
|
export declare const urlWritableProperties: Set<string>;
|
|
3
7
|
export declare const urlMethods: Set<string>;
|
|
4
|
-
export declare const urlStatics: Set<string>;
|
|
5
8
|
export declare const urlSearchParamsMethods: Set<string>;
|
|
6
9
|
export declare const uriArgument: (value: unknown, label: string) => string;
|
|
7
|
-
|
|
10
|
+
type UriFunction = "encodeURI" | "encodeURIComponent" | "decodeURI" | "decodeURIComponent";
|
|
11
|
+
export declare const uriGlobal: (name: UriFunction) => HostFunction<never>;
|
|
8
12
|
export declare const urlArgument: (value: unknown, label: string) => string;
|
|
9
|
-
export declare const
|
|
10
|
-
export declare const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
export declare const urlGlobal: HostFunction<never>;
|
|
14
|
+
export declare const urlSearchParamsGlobal: <R>(runner: Runner<R>) => HostFunction<R>;
|
|
15
|
+
export declare const invokeURLMethod: (value: Values.URL, name: string, node: AstNode) => string;
|
|
16
|
+
export {};
|
package/dist/stdlib/url.js
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { toProgram } from "../data.js";
|
|
3
|
+
import { HostFunction, requiresNew, sync, syncCall } from "../interpreter/host.js";
|
|
4
|
+
import { InterpreterRuntimeError } from "../interpreter/model.js";
|
|
5
|
+
import { isRuntimeReference } from "../interpreter/references.js";
|
|
6
|
+
import { preserveConsumerError } from "../interpreter/runner.js";
|
|
7
|
+
import { Values } from "../values.js";
|
|
8
|
+
import { coerceToString } from "./value.js";
|
|
1
9
|
export const urlProperties = new Set([
|
|
2
10
|
"href",
|
|
3
11
|
"origin",
|
|
@@ -24,7 +32,6 @@ export const urlWritableProperties = new Set([
|
|
|
24
32
|
"hash",
|
|
25
33
|
]);
|
|
26
34
|
export const urlMethods = new Set(["toString", "toJSON"]);
|
|
27
|
-
export const urlStatics = new Set(["canParse", "parse"]);
|
|
28
35
|
export const urlSearchParamsMethods = new Set([
|
|
29
36
|
"append",
|
|
30
37
|
"delete",
|
|
@@ -39,46 +46,116 @@ export const urlSearchParamsMethods = new Set([
|
|
|
39
46
|
"entries",
|
|
40
47
|
"toString",
|
|
41
48
|
]);
|
|
42
|
-
export const uriArgument = (value, label) => coerceToString(
|
|
43
|
-
|
|
44
|
-
|
|
49
|
+
export const uriArgument = (value, label) => coerceToString(toProgram(value, label));
|
|
50
|
+
const uriFunctions = {
|
|
51
|
+
encodeURI,
|
|
52
|
+
encodeURIComponent,
|
|
53
|
+
decodeURI,
|
|
54
|
+
decodeURIComponent,
|
|
55
|
+
};
|
|
56
|
+
export const uriGlobal = (name) => sync(name, (args, node) => {
|
|
57
|
+
const value = uriArgument(args[0], `${name} input`);
|
|
45
58
|
try {
|
|
46
|
-
|
|
47
|
-
case "encodeURI":
|
|
48
|
-
return encodeURI(value);
|
|
49
|
-
case "encodeURIComponent":
|
|
50
|
-
return encodeURIComponent(value);
|
|
51
|
-
case "decodeURI":
|
|
52
|
-
return decodeURI(value);
|
|
53
|
-
case "decodeURIComponent":
|
|
54
|
-
return decodeURIComponent(value);
|
|
55
|
-
}
|
|
59
|
+
return uriFunctions[name](value);
|
|
56
60
|
}
|
|
57
61
|
catch (error) {
|
|
58
|
-
throw new InterpreterRuntimeError(`${
|
|
62
|
+
throw new InterpreterRuntimeError(`${name} received malformed URI data: ${error instanceof Error ? error.message : String(error)}`, node).as("URIError");
|
|
59
63
|
}
|
|
60
|
-
};
|
|
61
|
-
export const urlArgument = (value, label) => value instanceof
|
|
62
|
-
|
|
63
|
-
if (
|
|
64
|
-
throw new InterpreterRuntimeError(`URL.${name} is not available.`, node);
|
|
65
|
-
if (args.length === 0)
|
|
64
|
+
});
|
|
65
|
+
export const urlArgument = (value, label) => value instanceof Values.URL ? value.url.href : uriArgument(value, label);
|
|
66
|
+
const urlStatic = (name) => sync(`URL.${name}`, (args, node) => {
|
|
67
|
+
if (args.length === 0) {
|
|
66
68
|
throw new InterpreterRuntimeError(`URL.${name} requires a URL argument.`, node).as("TypeError");
|
|
69
|
+
}
|
|
67
70
|
const input = urlArgument(args[0], `URL.${name} input`);
|
|
68
71
|
const base = args[1] === undefined ? undefined : urlArgument(args[1], `URL.${name} base`);
|
|
69
72
|
try {
|
|
70
73
|
const url = new URL(input, base);
|
|
71
|
-
return name === "canParse" ? true : new
|
|
74
|
+
return name === "canParse" ? true : new Values.URL(url);
|
|
72
75
|
}
|
|
73
76
|
catch {
|
|
74
77
|
return name === "canParse" ? false : null;
|
|
75
78
|
}
|
|
79
|
+
});
|
|
80
|
+
const constructURL = (args, node) => {
|
|
81
|
+
if (args.length === 0) {
|
|
82
|
+
throw new InterpreterRuntimeError("new URL(...) requires a URL string and an optional base URL.", node).as("TypeError");
|
|
83
|
+
}
|
|
84
|
+
const input = urlArgument(args[0], "new URL input");
|
|
85
|
+
const base = args[1] === undefined ? undefined : urlArgument(args[1], "new URL base");
|
|
86
|
+
try {
|
|
87
|
+
return new Values.URL(new URL(input, base));
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
throw new InterpreterRuntimeError(`new URL(...) received an invalid URL${base === undefined ? "" : " or base URL"}.`, node).as("TypeError");
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
export const urlGlobal = new HostFunction({
|
|
94
|
+
name: "URL",
|
|
95
|
+
call: requiresNew("URL"),
|
|
96
|
+
construct: syncCall(constructURL),
|
|
97
|
+
instanceOf: (value) => value instanceof Values.URL,
|
|
98
|
+
members: { canParse: urlStatic("canParse"), parse: urlStatic("parse") },
|
|
99
|
+
});
|
|
100
|
+
const readURLSearchParamsPair = (runner, value, node) => Effect.gen(function* () {
|
|
101
|
+
const cursor = yield* runner.syncIterator(value, node);
|
|
102
|
+
if (cursor === undefined) {
|
|
103
|
+
throw new InterpreterRuntimeError("new URLSearchParams(...) expects iterable [name, value] pairs.", node).as("TypeError");
|
|
104
|
+
}
|
|
105
|
+
const items = [];
|
|
106
|
+
while (true) {
|
|
107
|
+
const step = yield* cursor.next;
|
|
108
|
+
if (step.done)
|
|
109
|
+
return items;
|
|
110
|
+
items.push(yield* preserveConsumerError(cursor, Effect.sync(() => uriArgument(step.value, "URLSearchParams pair value"))));
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
const constructURLSearchParams = (runner, init, node) => {
|
|
114
|
+
if (init === undefined)
|
|
115
|
+
return Effect.succeed(new Values.URLSearchParams(new URLSearchParams()));
|
|
116
|
+
if (init instanceof Values.URLSearchParams) {
|
|
117
|
+
return Effect.succeed(new Values.URLSearchParams(new URLSearchParams(init.params)));
|
|
118
|
+
}
|
|
119
|
+
if (typeof init === "string")
|
|
120
|
+
return Effect.succeed(new Values.URLSearchParams(new URLSearchParams(init)));
|
|
121
|
+
if (init === null || typeof init === "number" || typeof init === "boolean") {
|
|
122
|
+
return Effect.succeed(new Values.URLSearchParams(new URLSearchParams(coerceToString(init))));
|
|
123
|
+
}
|
|
124
|
+
return Effect.gen(function* () {
|
|
125
|
+
const cursor = yield* runner.syncIterator(init, node);
|
|
126
|
+
if (cursor !== undefined) {
|
|
127
|
+
const entries = [];
|
|
128
|
+
while (true) {
|
|
129
|
+
const step = yield* cursor.next;
|
|
130
|
+
if (step.done) {
|
|
131
|
+
if (entries.some((entry) => entry.length !== 2)) {
|
|
132
|
+
throw new InterpreterRuntimeError("new URLSearchParams(...) expects iterable [name, value] pairs.", node).as("TypeError");
|
|
133
|
+
}
|
|
134
|
+
return new Values.URLSearchParams(new URLSearchParams(entries.map((entry) => [entry[0] ?? "", entry[1] ?? ""])));
|
|
135
|
+
}
|
|
136
|
+
entries.push(yield* preserveConsumerError(cursor, readURLSearchParamsPair(runner, step.value, node)));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (isRuntimeReference(init)) {
|
|
140
|
+
throw new InterpreterRuntimeError("new URLSearchParams(...) expects a query string, data object, or synchronous iterable pairs.", node).as("TypeError");
|
|
141
|
+
}
|
|
142
|
+
if (Values.isValue(init))
|
|
143
|
+
return new Values.URLSearchParams(new URLSearchParams());
|
|
144
|
+
const data = toProgram(init, "new URLSearchParams input");
|
|
145
|
+
if (data === null || typeof data !== "object") {
|
|
146
|
+
throw new InterpreterRuntimeError("new URLSearchParams(...) expects a query string, data object, iterable pairs, or URLSearchParams.", node).as("TypeError");
|
|
147
|
+
}
|
|
148
|
+
return new Values.URLSearchParams(new URLSearchParams(Object.fromEntries(Object.entries(data).map(([key, value]) => [key, coerceToString(value)]))));
|
|
149
|
+
});
|
|
76
150
|
};
|
|
151
|
+
export const urlSearchParamsGlobal = (runner) => new HostFunction({
|
|
152
|
+
name: "URLSearchParams",
|
|
153
|
+
call: requiresNew("URLSearchParams"),
|
|
154
|
+
construct: (args, node) => constructURLSearchParams(runner, args[0], node),
|
|
155
|
+
instanceOf: (value) => value instanceof Values.URLSearchParams,
|
|
156
|
+
});
|
|
77
157
|
export const invokeURLMethod = (value, name, node) => {
|
|
78
158
|
if (name === "toString" || name === "toJSON")
|
|
79
159
|
return value.url.href;
|
|
80
160
|
throw new InterpreterRuntimeError(`URL method '${name}' is not available.`, node);
|
|
81
161
|
};
|
|
82
|
-
import { InterpreterRuntimeError, UriFunction } from "../interpreter/model.js";
|
|
83
|
-
import { CodeModeURL } from "../values.js";
|
|
84
|
-
import { boundedData, coerceToString } from "./value.js";
|
package/dist/stdlib/value.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
import { type HostFunction, type SyncOptions } from "../interpreter/host.js";
|
|
2
|
+
import { type SafeObject } from "../data.js";
|
|
1
3
|
export declare const errorConstructors: Set<string>;
|
|
2
|
-
export declare const valueConstructors: Set<string>;
|
|
3
4
|
export declare const compoundOperators: Set<string>;
|
|
4
5
|
export declare const createErrorValue: (name: string, message: string) => SafeObject;
|
|
5
6
|
export declare const createAggregateErrorValue: (errors: Array<unknown>, message: string) => SafeObject;
|
|
6
7
|
export declare const errorBrandName: (value: unknown) => string | undefined;
|
|
7
|
-
export declare const boundedData: (value: unknown, label: string) => unknown;
|
|
8
8
|
export declare const coerceToString: (value: unknown) => string;
|
|
9
9
|
export declare const coerceToNumber: (value: unknown) => number;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
type Coercion = "Number" | "String" | "Boolean" | "parseInt" | "parseFloat" | "isFinite" | "isNaN";
|
|
11
|
+
/** A global coercion function such as `Number` or `parseInt`. */
|
|
12
|
+
export declare const coercion: (name: Coercion, options?: SyncOptions) => HostFunction;
|
|
13
|
+
export {};
|
package/dist/stdlib/value.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { sync } from "../interpreter/host.js";
|
|
2
|
+
import { InterpreterRuntimeError } from "../interpreter/model.js";
|
|
3
|
+
import { toProgram } from "../data.js";
|
|
4
|
+
import { Values } from "../values.js";
|
|
1
5
|
export const errorConstructors = new Set([
|
|
2
6
|
"Error",
|
|
3
7
|
"TypeError",
|
|
@@ -8,7 +12,6 @@ export const errorConstructors = new Set([
|
|
|
8
12
|
"URIError",
|
|
9
13
|
"AggregateError",
|
|
10
14
|
]);
|
|
11
|
-
export const valueConstructors = new Set(["Date", "RegExp", "Map", "Set", "URL", "URLSearchParams"]);
|
|
12
15
|
export const compoundOperators = new Set(["+=", "-=", "*=", "/=", "%=", "**=", "&=", "|=", "^=", "<<=", ">>=", ">>>="]);
|
|
13
16
|
const ErrorBrand = Symbol("codemode.error");
|
|
14
17
|
export const createErrorValue = (name, message) => {
|
|
@@ -20,23 +23,22 @@ export const createAggregateErrorValue = (errors, message) => Object.assign(crea
|
|
|
20
23
|
export const errorBrandName = (value) => value !== null && typeof value === "object"
|
|
21
24
|
? value[ErrorBrand]
|
|
22
25
|
: undefined;
|
|
23
|
-
export const boundedData = (value, label) => copyIn(value, label, true);
|
|
24
26
|
export const coerceToString = (value) => {
|
|
25
27
|
if (value === null)
|
|
26
28
|
return "null";
|
|
27
29
|
if (value === undefined)
|
|
28
30
|
return "undefined";
|
|
29
|
-
if (value instanceof
|
|
31
|
+
if (value instanceof Values.Date)
|
|
30
32
|
return Number.isFinite(value.time) ? new Date(value.time).toISOString() : "Invalid Date";
|
|
31
|
-
if (value instanceof
|
|
33
|
+
if (value instanceof Values.RegExp)
|
|
32
34
|
return `/${value.regex.source}/${value.regex.flags}`;
|
|
33
|
-
if (value instanceof
|
|
35
|
+
if (value instanceof Values.Map)
|
|
34
36
|
return "[object Map]";
|
|
35
|
-
if (value instanceof
|
|
37
|
+
if (value instanceof Values.Set)
|
|
36
38
|
return "[object Set]";
|
|
37
|
-
if (value instanceof
|
|
39
|
+
if (value instanceof Values.URL)
|
|
38
40
|
return value.url.href;
|
|
39
|
-
if (value instanceof
|
|
41
|
+
if (value instanceof Values.URLSearchParams)
|
|
40
42
|
return value.params.toString();
|
|
41
43
|
if (errorBrandName(value) !== undefined) {
|
|
42
44
|
// Match Error.prototype.toString: "name: message", or just one when the other is empty.
|
|
@@ -57,9 +59,9 @@ export const coerceToString = (value) => {
|
|
|
57
59
|
return String(value);
|
|
58
60
|
};
|
|
59
61
|
export const coerceToNumber = (value) => {
|
|
60
|
-
if (value instanceof
|
|
62
|
+
if (value instanceof Values.Date)
|
|
61
63
|
return value.time;
|
|
62
|
-
if (
|
|
64
|
+
if (Values.isValue(value))
|
|
63
65
|
return Number.NaN;
|
|
64
66
|
// Arrays coerce through our own string coercion: host Number(array) joins with host
|
|
65
67
|
// ToPrimitive, which throws on the null-prototype objects the interpreter produces.
|
|
@@ -67,54 +69,53 @@ export const coerceToNumber = (value) => {
|
|
|
67
69
|
return Number(coerceToString(value));
|
|
68
70
|
return value !== null && typeof value === "object" ? Number.NaN : Number(value);
|
|
69
71
|
};
|
|
70
|
-
|
|
72
|
+
const coerce = (name, args, node) => {
|
|
71
73
|
// Native: Number() is 0 and String() is "", unlike their undefined-argument forms; the
|
|
72
74
|
// other coercers match native through the undefined-argument path below.
|
|
73
75
|
if (args.length === 0) {
|
|
74
|
-
if (
|
|
76
|
+
if (name === "Number")
|
|
75
77
|
return 0;
|
|
76
|
-
if (
|
|
78
|
+
if (name === "String")
|
|
77
79
|
return "";
|
|
78
80
|
}
|
|
79
81
|
const raw = args[0];
|
|
80
|
-
// Error values are plain SafeObjects; the
|
|
81
|
-
if (
|
|
82
|
+
// Error values are plain SafeObjects; the toProgram path below would strip their brand.
|
|
83
|
+
if (name === "String" && errorBrandName(raw) !== undefined)
|
|
82
84
|
return coerceToString(raw);
|
|
83
|
-
if (
|
|
84
|
-
if (
|
|
85
|
+
if (Values.isValue(raw)) {
|
|
86
|
+
if (name === "Boolean")
|
|
85
87
|
return true;
|
|
86
|
-
if (
|
|
88
|
+
if (name === "Number")
|
|
87
89
|
return coerceToNumber(raw);
|
|
88
|
-
if (
|
|
90
|
+
if (name === "String")
|
|
89
91
|
return coerceToString(raw);
|
|
90
|
-
if (
|
|
92
|
+
if (name === "isFinite")
|
|
91
93
|
return Number.isFinite(coerceToNumber(raw));
|
|
92
|
-
if (
|
|
94
|
+
if (name === "isNaN")
|
|
93
95
|
return Number.isNaN(coerceToNumber(raw));
|
|
94
|
-
if (
|
|
96
|
+
if (name === "parseInt")
|
|
95
97
|
return parseInt(coerceToString(raw));
|
|
96
98
|
return parseFloat(coerceToString(raw));
|
|
97
99
|
}
|
|
98
|
-
const value =
|
|
99
|
-
if (
|
|
100
|
+
const value = toProgram(raw, `${name} input`);
|
|
101
|
+
if (name === "Number")
|
|
100
102
|
return coerceToNumber(value);
|
|
101
|
-
if (
|
|
103
|
+
if (name === "Boolean")
|
|
102
104
|
return Boolean(value);
|
|
103
|
-
if (
|
|
105
|
+
if (name === "isFinite")
|
|
104
106
|
return Number.isFinite(coerceToNumber(value));
|
|
105
|
-
if (
|
|
107
|
+
if (name === "isNaN")
|
|
106
108
|
return Number.isNaN(coerceToNumber(value));
|
|
107
|
-
if (
|
|
109
|
+
if (name === "parseInt") {
|
|
108
110
|
const radix = args[1];
|
|
109
111
|
if (radix !== undefined && typeof radix !== "number") {
|
|
110
112
|
throw new InterpreterRuntimeError("parseInt expects a numeric radix.", node);
|
|
111
113
|
}
|
|
112
114
|
return parseInt(coerceToString(value), radix);
|
|
113
115
|
}
|
|
114
|
-
if (
|
|
116
|
+
if (name === "parseFloat")
|
|
115
117
|
return parseFloat(coerceToString(value));
|
|
116
118
|
return coerceToString(value);
|
|
117
119
|
};
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
import { isCodeModeValue, CodeModeDate, CodeModeMap, CodeModeRegExp, CodeModeSet, CodeModeURL, CodeModeURLSearchParams, } from "../values.js";
|
|
120
|
+
/** A global coercion function such as `Number` or `parseInt`. */
|
|
121
|
+
export const coercion = (name, options = {}) => sync(name, (args, node) => toProgram(coerce(name, args, node), `${name} result`), options);
|
package/dist/tool-runtime.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
+
import { type Namespace } from "./namespace.js";
|
|
3
|
+
import { type Tool } from "./tool.js";
|
|
2
4
|
import type { Tools } from "./tools.js";
|
|
5
|
+
export declare const compareText: (left: string, right: string) => 0 | 1 | -1;
|
|
3
6
|
export type Services<T> = ServicesOf<T, []>;
|
|
4
7
|
type ServicesOf<T, Depth extends ReadonlyArray<unknown>> = Depth["length"] extends 8 ? never : T extends {
|
|
5
8
|
readonly _tag: "CodeModeTool";
|
|
@@ -22,7 +25,9 @@ export type ToolCallEnded = {
|
|
|
22
25
|
readonly message?: string;
|
|
23
26
|
};
|
|
24
27
|
export type ToolCallHooks<R = never> = {
|
|
28
|
+
/** Observes decoded tool input immediately before tool execution. */
|
|
25
29
|
readonly onToolCallStart?: ((call: ToolCallStarted) => Effect.Effect<void, never, R>) | undefined;
|
|
30
|
+
/** Observes each admitted tool call as it succeeds, fails, or is interrupted. */
|
|
26
31
|
readonly onToolCallEnd?: ((call: ToolCallEnded) => Effect.Effect<void, never, R>) | undefined;
|
|
27
32
|
};
|
|
28
33
|
export type ToolDescription = {
|
|
@@ -30,22 +35,19 @@ export type ToolDescription = {
|
|
|
30
35
|
readonly description: string;
|
|
31
36
|
readonly signature: string;
|
|
32
37
|
};
|
|
33
|
-
export type SafeObject = Record<string, unknown>;
|
|
34
38
|
export declare const toolExpression: (path: string) => string;
|
|
35
39
|
export declare class ToolReference {
|
|
36
40
|
readonly path: ReadonlyArray<string>;
|
|
37
41
|
constructor(path: ReadonlyArray<string>);
|
|
38
42
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export
|
|
46
|
-
|
|
47
|
-
export declare const copyOut: (value: unknown, mode: CopyOutMode) => unknown;
|
|
48
|
-
export type DiscoveryPlan = {
|
|
43
|
+
type ToolNode<R> = {
|
|
44
|
+
tool?: Tool<R>;
|
|
45
|
+
namespace?: Namespace<R>;
|
|
46
|
+
readonly children: Map<string, ToolNode<R>>;
|
|
47
|
+
};
|
|
48
|
+
/** Tools indexed once per runtime: the lookup trie plus the model-facing catalog and search index. */
|
|
49
|
+
export type Prepared<R = never> = {
|
|
50
|
+
readonly root: ToolNode<R>;
|
|
49
51
|
readonly catalog: ReadonlyArray<ToolDescription>;
|
|
50
52
|
readonly searchIndex: ReadonlyArray<SearchEntry>;
|
|
51
53
|
};
|
|
@@ -55,14 +57,13 @@ export type SearchEntry = {
|
|
|
55
57
|
};
|
|
56
58
|
/** Exact callable signature of the built-in `search` function, for host-owned instructions. */
|
|
57
59
|
export declare const searchSignature: string;
|
|
58
|
-
export declare const
|
|
59
|
-
export declare const prepare: <R>(tools: Tools<R>) => DiscoveryPlan;
|
|
60
|
+
export declare const prepare: <R>(tools: Tools<R>) => Prepared<R>;
|
|
60
61
|
export type ToolRuntime<R = never> = {
|
|
61
|
-
readonly root: ToolReference;
|
|
62
62
|
readonly calls: Array<ToolCall>;
|
|
63
63
|
readonly execute: (path: ReadonlyArray<string>, args: Array<unknown>) => Effect.Effect<unknown, unknown, R>;
|
|
64
64
|
readonly search: (args: Array<unknown>) => Effect.Effect<unknown, unknown, R>;
|
|
65
65
|
readonly keys: (path: ReadonlyArray<string>) => ReadonlyArray<string>;
|
|
66
66
|
};
|
|
67
|
-
|
|
67
|
+
/** Per-execution call state over tools prepared once for the runtime. */
|
|
68
|
+
export declare const make: <R>(prepared: Prepared<R>, maxToolCalls: number | undefined, hooks?: ToolCallHooks<R>) => ToolRuntime<R>;
|
|
68
69
|
export * as ToolRuntime from "./tool-runtime.js";
|