@opencode/codemode 0.0.0-dev-19602 → 0.0.0-dev-19607
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 +3 -0
- package/dist/codemode.js +12 -1
- package/dist/data.d.ts +1 -0
- package/dist/data.js +9 -2
- package/dist/extension.d.ts +12 -0
- package/dist/extension.js +9 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interpreter/extensions.d.ts +8 -0
- package/dist/interpreter/extensions.js +270 -0
- package/dist/interpreter/globals.d.ts +2 -0
- package/dist/interpreter/globals.js +41 -38
- package/dist/interpreter/objects.d.ts +6 -1
- package/dist/interpreter/objects.js +8 -0
- package/dist/interpreter/references.js +4 -1
- package/dist/stdlib/object.js +1 -1
- package/package.json +1 -1
package/dist/codemode.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
|
+
import type { Extension } from "./extension.js";
|
|
2
3
|
import { type Services, type ToolDescription, ToolRuntime } from "./tool-runtime.js";
|
|
3
4
|
import type { Tools } from "./tools.js";
|
|
4
5
|
/** A tool call admitted during an execution. */
|
|
@@ -29,6 +30,8 @@ export type ResolvedExecutionLimits = {
|
|
|
29
30
|
export type Options<Provided extends Record<string, unknown> = {}> = ToolRuntime.ToolCallHooks<Services<Provided>> & {
|
|
30
31
|
/** Explicit tools exposed to the program as `tools`. */
|
|
31
32
|
tools?: Provided & Tools<Services<Provided>>;
|
|
33
|
+
/** Host classes and functions exposed as globals; see `Extension.make`. */
|
|
34
|
+
extensions?: ReadonlyArray<Extension>;
|
|
32
35
|
/** Resource limits enforced on each execution. */
|
|
33
36
|
limits?: ExecutionLimits;
|
|
34
37
|
};
|
package/dist/codemode.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
2
|
import { executeProgram } from "./interpreter/execute.js";
|
|
3
|
+
import { extensionGlobals } from "./interpreter/extensions.js";
|
|
4
|
+
import { globalNames } from "./interpreter/globals.js";
|
|
3
5
|
import { ToolRuntime } from "./tool-runtime.js";
|
|
4
6
|
/** Signature-construction helpers for host-owned catalog instructions. */
|
|
5
7
|
export { searchSignature, toolExpression } from "./tool-runtime.js";
|
|
@@ -59,8 +61,17 @@ export const execute = (options) => make(options).execute(options.code);
|
|
|
59
61
|
export const make = (options = {}) => {
|
|
60
62
|
const prepared = ToolRuntime.prepare((options.tools ?? {}));
|
|
61
63
|
const limits = resolveExecutionLimits(options.limits);
|
|
64
|
+
const extensions = options.extensions ?? [];
|
|
65
|
+
const bound = new Set(globalNames);
|
|
66
|
+
for (const extension of extensions) {
|
|
67
|
+
for (const name of Object.keys(extension.globals)) {
|
|
68
|
+
if (bound.has(name))
|
|
69
|
+
throw new TypeError(`Extension "${extension.name}" global "${name}" is already defined.`);
|
|
70
|
+
bound.add(name);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
62
73
|
return {
|
|
63
74
|
catalog: prepared.catalog,
|
|
64
|
-
execute: (code) => executeProgram(code, prepared, limits, options),
|
|
75
|
+
execute: (code) => executeProgram(code, prepared, limits, options, (host) => extensionGlobals(host, extensions)),
|
|
65
76
|
};
|
|
66
77
|
};
|
package/dist/data.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * as Data from "./data.js";
|
|
2
2
|
import type { DiagnosticKind } from "./codemode.js";
|
|
3
3
|
import type { Prototypes } from "./interpreter/intrinsics.js";
|
|
4
|
+
export declare const MAX_VALUE_DEPTH = 32;
|
|
4
5
|
export declare class ToolRuntimeError extends Error {
|
|
5
6
|
readonly kind: Extract<DiagnosticKind, "UnknownTool" | "InvalidToolInput" | "InvalidToolOutput" | "InvalidDataValue" | "ToolCallLimitExceeded">;
|
|
6
7
|
readonly suggestions: ReadonlyArray<string>;
|
package/dist/data.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * as Data from "./data.js";
|
|
2
|
-
import { Callable, define, entries, get, isWrapper, parseArrayIndex, ProgramArray, ProgramDate, ProgramError, ProgramGenerator, ProgramMap, ProgramObject, ProgramPromise, ProgramRegExp, ProgramSet, ProgramURL, ProgramURLSearchParams, } from "./interpreter/objects.js";
|
|
3
|
-
const MAX_VALUE_DEPTH = 32;
|
|
2
|
+
import { Callable, define, entries, get, isWrapper, parseArrayIndex, ProgramArray, ProgramDate, ProgramError, ProgramGenerator, ProgramHandle, ProgramMap, ProgramObject, ProgramPromise, ProgramRegExp, ProgramSet, ProgramURL, ProgramURLSearchParams, } from "./interpreter/objects.js";
|
|
3
|
+
export const MAX_VALUE_DEPTH = 32;
|
|
4
4
|
export class ToolRuntimeError extends Error {
|
|
5
5
|
kind;
|
|
6
6
|
suggestions;
|
|
@@ -49,6 +49,13 @@ const copy = (value, label, mode, depth, seen, protos, stripProto = true) => {
|
|
|
49
49
|
if ((value instanceof Callable || value instanceof ProgramGenerator) && mode !== "program") {
|
|
50
50
|
throw new ToolRuntimeError("InvalidDataValue", `${label} must contain data only.`);
|
|
51
51
|
}
|
|
52
|
+
if (value instanceof ProgramHandle && mode !== "program") {
|
|
53
|
+
throw new ToolRuntimeError("InvalidDataValue", `${label} contains a ${value.instance.constructor.name}, which only extension functions accept.`);
|
|
54
|
+
}
|
|
55
|
+
// Host-produced input never holds program objects; one arriving here would come back as a host object.
|
|
56
|
+
if (value instanceof ProgramObject && mode === "data") {
|
|
57
|
+
throw new ToolRuntimeError("InvalidDataValue", `${label} must be host data, not a program value.`);
|
|
58
|
+
}
|
|
52
59
|
if (protos !== undefined && mode === "program") {
|
|
53
60
|
if (value instanceof ProgramObject)
|
|
54
61
|
return value;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * as Extension from "./extension.js";
|
|
2
|
+
/**
|
|
3
|
+
* Host classes and functions a program uses directly, like JavaScript. Values crossing in either direction are
|
|
4
|
+
* converted, never shared: plain data is copied, instances of the classes stay on the host behind program-side
|
|
5
|
+
* handles. Extension calls are not tool calls.
|
|
6
|
+
*/
|
|
7
|
+
export type Extension = {
|
|
8
|
+
readonly name: string;
|
|
9
|
+
/** Each value is a class or a function; everything on a class is exposed, including statics and accessors. */
|
|
10
|
+
readonly globals: Readonly<Record<string, Function>>;
|
|
11
|
+
};
|
|
12
|
+
export declare const make: (options: Extension) => Extension;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * as Extension from "./extension.js";
|
|
2
|
+
export const make = (options) => {
|
|
3
|
+
for (const [name, value] of Object.entries(options.globals)) {
|
|
4
|
+
if (typeof value !== "function") {
|
|
5
|
+
throw new TypeError(`Extension "${options.name}" global "${name}" must be a class or a function.`);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
return { name: options.name, globals: { ...options.globals } };
|
|
9
|
+
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Extension } from "../extension.js";
|
|
2
|
+
import type { Host } from "./globals.js";
|
|
3
|
+
/**
|
|
4
|
+
* The global bindings of one run's extensions. Everything crossing the boundary is converted: plain data is
|
|
5
|
+
* copied, built-in wrappers are copied, instances of exposed classes travel as handles, and a host Promise becomes
|
|
6
|
+
* a program promise. Prototypes, constructors, and handle identity are all per run.
|
|
7
|
+
*/
|
|
8
|
+
export declare const extensionGlobals: <R>(host: Host<R>, extensions: ReadonlyArray<Extension>) => ReadonlyArray<readonly [string, unknown]>;
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { MAX_VALUE_DEPTH } from "../data.js";
|
|
3
|
+
import { coerceToString } from "../stdlib/value.js";
|
|
4
|
+
import { createErrorValue, isErrorType } from "./intrinsics.js";
|
|
5
|
+
import { ProgramThrow, typeError } from "./model.js";
|
|
6
|
+
import { constructor, fn } from "./native.js";
|
|
7
|
+
import { Callable, define, defineAccessor, entries, get, hidden, ProgramArray, ProgramDate, ProgramError, ProgramGenerator, ProgramHandle, ProgramMap, ProgramObject, ProgramPromise, ProgramRegExp, ProgramSet, ProgramURL, ProgramURLSearchParams, } from "./objects.js";
|
|
8
|
+
import { describeValue } from "./references.js";
|
|
9
|
+
const isClass = (value) => typeof value === "function" && typeof value.prototype === "object" && value.prototype !== null;
|
|
10
|
+
// Own keys the native function already carries.
|
|
11
|
+
const ownFunctionKeys = new Set(["length", "name", "prototype"]);
|
|
12
|
+
const ownPrototypeKeys = new Set(["constructor"]);
|
|
13
|
+
/**
|
|
14
|
+
* The global bindings of one run's extensions. Everything crossing the boundary is converted: plain data is
|
|
15
|
+
* copied, built-in wrappers are copied, instances of exposed classes travel as handles, and a host Promise becomes
|
|
16
|
+
* a program promise. Prototypes, constructors, and handle identity are all per run.
|
|
17
|
+
*/
|
|
18
|
+
export const extensionGlobals = (host, extensions) => {
|
|
19
|
+
const protos = host.runner.prototypes;
|
|
20
|
+
const classes = new Set(extensions.flatMap((extension) => Object.values(extension.globals)).filter(isClass));
|
|
21
|
+
// Host prototype object → this run's program prototype, so an instance wraps as its most-derived exposed class.
|
|
22
|
+
const prototypes = new Map();
|
|
23
|
+
const exposed = new Map();
|
|
24
|
+
const classOf = new Map();
|
|
25
|
+
const handles = new WeakMap();
|
|
26
|
+
const toHost = (value, label, depth = 0, seen = new Set()) => {
|
|
27
|
+
if (depth > MAX_VALUE_DEPTH)
|
|
28
|
+
throw typeError(`${label} exceeds the maximum value depth of ${MAX_VALUE_DEPTH}.`);
|
|
29
|
+
if (value === null || typeof value !== "object") {
|
|
30
|
+
if (isPrimitive(value))
|
|
31
|
+
return value;
|
|
32
|
+
throw typeError(`${label} contains ${describeValue(value)}, which cannot be passed to an extension.`);
|
|
33
|
+
}
|
|
34
|
+
if (value instanceof ProgramHandle)
|
|
35
|
+
return value.instance;
|
|
36
|
+
if (value instanceof ProgramDate)
|
|
37
|
+
return new Date(value.time);
|
|
38
|
+
if (value instanceof ProgramRegExp)
|
|
39
|
+
return new RegExp(value.regex.source, value.regex.flags);
|
|
40
|
+
if (value instanceof ProgramURL)
|
|
41
|
+
return new URL(value.url.href);
|
|
42
|
+
if (value instanceof ProgramURLSearchParams)
|
|
43
|
+
return new URLSearchParams(value.params);
|
|
44
|
+
const next = (item) => toHost(item, label, depth + 1, seen);
|
|
45
|
+
if (value instanceof ProgramMap)
|
|
46
|
+
return new Map([...value.map].map(([key, item]) => [next(key), next(item)]));
|
|
47
|
+
if (value instanceof ProgramSet)
|
|
48
|
+
return new Set([...value.set].map(next));
|
|
49
|
+
if (!(value instanceof ProgramObject) ||
|
|
50
|
+
value instanceof Callable ||
|
|
51
|
+
value instanceof ProgramGenerator ||
|
|
52
|
+
value instanceof ProgramPromise) {
|
|
53
|
+
throw typeError(`${label} contains ${describeValue(value)}, which cannot be passed to an extension.`);
|
|
54
|
+
}
|
|
55
|
+
if (value instanceof ProgramError) {
|
|
56
|
+
const name = coerceToString(get(value, "name"));
|
|
57
|
+
const message = get(value, "message");
|
|
58
|
+
const text = message === undefined ? "" : coerceToString(message);
|
|
59
|
+
return name === "AggregateError" ? new AggregateError([], text) : new (hostErrors[name] ?? Error)(text);
|
|
60
|
+
}
|
|
61
|
+
if (seen.has(value))
|
|
62
|
+
throw typeError(`${label} contains a circular value.`);
|
|
63
|
+
seen.add(value);
|
|
64
|
+
const copied = value instanceof ProgramArray
|
|
65
|
+
? value.items.map(next)
|
|
66
|
+
: Object.fromEntries(entries(value)
|
|
67
|
+
.filter(([key]) => key !== "__proto__")
|
|
68
|
+
.map(([key, item]) => [key, next(item)]));
|
|
69
|
+
seen.delete(value);
|
|
70
|
+
return copied;
|
|
71
|
+
};
|
|
72
|
+
const fromHost = (value, label, depth = 0, seen = new Set()) => {
|
|
73
|
+
if (depth > MAX_VALUE_DEPTH)
|
|
74
|
+
throw typeError(`${label} exceeds the maximum value depth of ${MAX_VALUE_DEPTH}.`);
|
|
75
|
+
if (isPrimitive(value))
|
|
76
|
+
return value;
|
|
77
|
+
if (value !== null && typeof value === "object") {
|
|
78
|
+
const existing = handles.get(value);
|
|
79
|
+
if (existing !== undefined)
|
|
80
|
+
return existing;
|
|
81
|
+
const proto = handlePrototype(value);
|
|
82
|
+
if (proto !== undefined) {
|
|
83
|
+
const handle = new ProgramHandle(proto, value);
|
|
84
|
+
handles.set(value, handle);
|
|
85
|
+
return handle;
|
|
86
|
+
}
|
|
87
|
+
if (value instanceof Date)
|
|
88
|
+
return new ProgramDate(protos.Date, value.getTime());
|
|
89
|
+
if (value instanceof RegExp)
|
|
90
|
+
return new ProgramRegExp(protos.RegExp, value.source, value.flags);
|
|
91
|
+
if (value instanceof Error) {
|
|
92
|
+
return createErrorValue(protos[isErrorType(value.name) ? value.name : "Error"], value.message);
|
|
93
|
+
}
|
|
94
|
+
if (value instanceof URL)
|
|
95
|
+
return new ProgramURL(protos.URL, protos.URLSearchParams, new URL(value.href));
|
|
96
|
+
if (value instanceof URLSearchParams) {
|
|
97
|
+
return new ProgramURLSearchParams(protos.URLSearchParams, new URLSearchParams(value));
|
|
98
|
+
}
|
|
99
|
+
const next = (item) => fromHost(item, label, depth + 1, seen);
|
|
100
|
+
if (value instanceof Map) {
|
|
101
|
+
const wrapped = new ProgramMap(protos.Map);
|
|
102
|
+
for (const [key, item] of value)
|
|
103
|
+
wrapped.map.set(next(key), next(item));
|
|
104
|
+
return wrapped;
|
|
105
|
+
}
|
|
106
|
+
if (value instanceof Set) {
|
|
107
|
+
const wrapped = new ProgramSet(protos.Set);
|
|
108
|
+
for (const item of value)
|
|
109
|
+
wrapped.set.add(next(item));
|
|
110
|
+
return wrapped;
|
|
111
|
+
}
|
|
112
|
+
if (seen.has(value))
|
|
113
|
+
throw typeError(`${label} produced a circular value.`);
|
|
114
|
+
seen.add(value);
|
|
115
|
+
if (Array.isArray(value)) {
|
|
116
|
+
const copied = new ProgramArray(protos.Array, value.map(next));
|
|
117
|
+
seen.delete(value);
|
|
118
|
+
return copied;
|
|
119
|
+
}
|
|
120
|
+
const prototype = Object.getPrototypeOf(value);
|
|
121
|
+
if (prototype === Object.prototype || prototype === null) {
|
|
122
|
+
const copied = new ProgramObject(protos.Object);
|
|
123
|
+
for (const [key, item] of Object.entries(value))
|
|
124
|
+
define(copied, key, next(item));
|
|
125
|
+
seen.delete(value);
|
|
126
|
+
return copied;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
throw typeError(`${label} produced ${describeHost(value)}, which the program cannot hold.`);
|
|
130
|
+
};
|
|
131
|
+
const handlePrototype = (instance) => {
|
|
132
|
+
for (let level = Object.getPrototypeOf(instance); level !== null; level = Object.getPrototypeOf(level)) {
|
|
133
|
+
const proto = prototypes.get(level);
|
|
134
|
+
if (proto !== undefined)
|
|
135
|
+
return proto;
|
|
136
|
+
}
|
|
137
|
+
return undefined;
|
|
138
|
+
};
|
|
139
|
+
// Runs host code with already-converted inputs. Whatever it returns, resolves, throws, or rejects with crosses
|
|
140
|
+
// the same way, so the program catches what the author threw.
|
|
141
|
+
const invoke = (run, label) => {
|
|
142
|
+
const thrown = (reason) => new ProgramThrow(fromHost(reason, label));
|
|
143
|
+
let result;
|
|
144
|
+
try {
|
|
145
|
+
result = run();
|
|
146
|
+
}
|
|
147
|
+
catch (reason) {
|
|
148
|
+
return Effect.fail(thrown(reason));
|
|
149
|
+
}
|
|
150
|
+
if (!(result instanceof Promise))
|
|
151
|
+
return Effect.succeed(fromHost(result, label));
|
|
152
|
+
return host.promises.create(Effect.map(Effect.tryPromise({ try: () => result, catch: thrown }), (settled) => fromHost(settled, label)));
|
|
153
|
+
};
|
|
154
|
+
const args = (values, label) => values.map((value, index) => toHost(value, `Argument ${index + 1} to ${label}`));
|
|
155
|
+
// Own members of each level from `from` up to (excluding) `root`, child first, as JS resolves them.
|
|
156
|
+
const members = (target, from, root, skip, label, receiver) => {
|
|
157
|
+
for (let level = from; level !== null && level !== root; level = Object.getPrototypeOf(level)) {
|
|
158
|
+
for (const [key, descriptor] of Object.entries(Object.getOwnPropertyDescriptors(level))) {
|
|
159
|
+
if (skip.has(key) || target.props.has(key))
|
|
160
|
+
continue;
|
|
161
|
+
const name = `${label}.${key}`;
|
|
162
|
+
if (typeof descriptor.value === "function") {
|
|
163
|
+
const method = descriptor.value;
|
|
164
|
+
const impl = (thisValue, values) => {
|
|
165
|
+
const self = receiver(thisValue, name);
|
|
166
|
+
const converted = args(values, name);
|
|
167
|
+
return invoke(() => method.apply(self, converted), name);
|
|
168
|
+
};
|
|
169
|
+
define(target, key, fn(protos, key, method.length, impl), hidden);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
// Data properties stay host-side: a program write to one would change the host class itself.
|
|
173
|
+
if ("value" in descriptor)
|
|
174
|
+
continue;
|
|
175
|
+
const get = descriptor.get;
|
|
176
|
+
const set = descriptor.set;
|
|
177
|
+
defineAccessor(target, key, get === undefined
|
|
178
|
+
? undefined
|
|
179
|
+
: (thisValue) => {
|
|
180
|
+
const value = get.call(receiver(thisValue, name));
|
|
181
|
+
if (value instanceof Promise)
|
|
182
|
+
throw typeError(`${name} returned a Promise; a getter must be synchronous.`);
|
|
183
|
+
return fromHost(value, name);
|
|
184
|
+
}, set === undefined
|
|
185
|
+
? undefined
|
|
186
|
+
: (thisValue, value) => {
|
|
187
|
+
set.call(receiver(thisValue, name), toHost(value, `${name} value`));
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
const expose = (cls) => {
|
|
193
|
+
const existing = exposed.get(cls);
|
|
194
|
+
if (existing !== undefined)
|
|
195
|
+
return existing;
|
|
196
|
+
const ancestor = exposedAncestor(cls);
|
|
197
|
+
const base = ancestor === undefined ? undefined : expose(ancestor);
|
|
198
|
+
const proto = new ProgramObject(base === undefined ? protos.Object : base.proto);
|
|
199
|
+
prototypes.set(cls.prototype, proto);
|
|
200
|
+
const name = cls.name;
|
|
201
|
+
const ctor = constructor(protos, proto, {
|
|
202
|
+
name,
|
|
203
|
+
length: cls.length,
|
|
204
|
+
call: (_, values) => {
|
|
205
|
+
const converted = args(values, name);
|
|
206
|
+
return invoke(() => cls.apply(undefined, converted), name);
|
|
207
|
+
},
|
|
208
|
+
construct: (values) => {
|
|
209
|
+
const label = `new ${name}`;
|
|
210
|
+
const construct = cls;
|
|
211
|
+
const converted = args(values, label);
|
|
212
|
+
return invoke(() => new construct(...converted), label);
|
|
213
|
+
},
|
|
214
|
+
});
|
|
215
|
+
if (base !== undefined)
|
|
216
|
+
ctor.proto = base.ctor;
|
|
217
|
+
const entry = { ctor, proto };
|
|
218
|
+
exposed.set(cls, entry);
|
|
219
|
+
classOf.set(ctor, cls);
|
|
220
|
+
// A static called through an exposed subclass sees that subclass as `this`, like JS.
|
|
221
|
+
members(ctor, cls, ancestor ?? Function.prototype, ownFunctionKeys, name, (thisValue) => {
|
|
222
|
+
const called = classOf.get(thisValue);
|
|
223
|
+
return called !== undefined && (called === cls || called.prototype instanceof cls) ? called : cls;
|
|
224
|
+
});
|
|
225
|
+
members(proto, cls.prototype, ancestor?.prototype ?? Object.prototype, ownPrototypeKeys, `${name}.prototype`, (thisValue, member) => {
|
|
226
|
+
if (thisValue instanceof ProgramHandle && thisValue.instance instanceof cls)
|
|
227
|
+
return thisValue.instance;
|
|
228
|
+
throw typeError(`Illegal invocation: ${member} called on ${describeValue(thisValue)}.`);
|
|
229
|
+
});
|
|
230
|
+
return entry;
|
|
231
|
+
};
|
|
232
|
+
const exposedAncestor = (cls) => {
|
|
233
|
+
for (let level = Object.getPrototypeOf(cls); isClass(level); level = Object.getPrototypeOf(level)) {
|
|
234
|
+
if (classes.has(level))
|
|
235
|
+
return level;
|
|
236
|
+
}
|
|
237
|
+
return undefined;
|
|
238
|
+
};
|
|
239
|
+
return extensions.flatMap((extension) => Object.entries(extension.globals).map(([name, value]) => {
|
|
240
|
+
if (isClass(value))
|
|
241
|
+
return [name, expose(value).ctor];
|
|
242
|
+
const impl = (_, values) => {
|
|
243
|
+
const converted = args(values, name);
|
|
244
|
+
return invoke(() => value.apply(undefined, converted), name);
|
|
245
|
+
};
|
|
246
|
+
return [name, fn(protos, name, value.length, impl)];
|
|
247
|
+
}));
|
|
248
|
+
};
|
|
249
|
+
const hostErrors = {
|
|
250
|
+
TypeError,
|
|
251
|
+
RangeError,
|
|
252
|
+
SyntaxError,
|
|
253
|
+
ReferenceError,
|
|
254
|
+
EvalError,
|
|
255
|
+
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 === "function")
|
|
265
|
+
return "a function";
|
|
266
|
+
if (typeof value !== "object" || value === null)
|
|
267
|
+
return `a ${typeof value}`;
|
|
268
|
+
const name = value.constructor?.name;
|
|
269
|
+
return name === undefined || name === "" ? "an object" : `a ${name}`;
|
|
270
|
+
};
|
|
@@ -9,5 +9,7 @@ export type Host<R> = {
|
|
|
9
9
|
readonly toolKeys: (path: ReadonlyArray<string>) => ReadonlyArray<string>;
|
|
10
10
|
readonly logs: Array<string>;
|
|
11
11
|
};
|
|
12
|
+
/** Names bound in every program before extensions apply. */
|
|
13
|
+
export declare const globalNames: ReadonlySet<string>;
|
|
12
14
|
/** The immutable global bindings of every program, in declaration order. */
|
|
13
15
|
export declare const globals: <R>(host: Host<R>) => ReadonlyArray<readonly [string, unknown]>;
|
|
@@ -42,44 +42,47 @@ const symbolGlobal = (runner) => {
|
|
|
42
42
|
constants(symbol, { asyncIterator: AsyncIteratorSymbol, iterator: IteratorSymbol });
|
|
43
43
|
return symbol;
|
|
44
44
|
};
|
|
45
|
+
// A table rather than a list so the names are known before any runtime exists.
|
|
46
|
+
const table = {
|
|
47
|
+
tools: () => new ToolReference([]),
|
|
48
|
+
search: (host) => native(host.runner.prototypes, { name: "search", call: (_, args) => host.search(args), callback: false }),
|
|
49
|
+
undefined: () => undefined,
|
|
50
|
+
NaN: () => NaN,
|
|
51
|
+
Infinity: () => Infinity,
|
|
52
|
+
Object: (host) => objectGlobal(host.runner, host.toolKeys),
|
|
53
|
+
Function: (host) => functionGlobal(host.runner),
|
|
54
|
+
Array: (host) => arrayGlobal(host.runner),
|
|
55
|
+
Math: (host) => mathGlobal(host.runner),
|
|
56
|
+
JSON: (host) => jsonGlobal(host.runner),
|
|
57
|
+
console: (host) => consoleGlobal(host.runner, host.logs),
|
|
58
|
+
Promise: (host) => promiseGlobal(host.runner, host.promises),
|
|
59
|
+
Symbol: (host) => symbolGlobal(host.runner),
|
|
60
|
+
Number: (host) => numberGlobal(host.runner),
|
|
61
|
+
String: (host) => stringGlobal(host.runner),
|
|
62
|
+
Boolean: (host) => booleanGlobal(host.runner),
|
|
63
|
+
parseInt: (host) => coercion(host.runner, "parseInt", 2),
|
|
64
|
+
parseFloat: (host) => coercion(host.runner, "parseFloat"),
|
|
65
|
+
isFinite: (host) => coercion(host.runner, "isFinite"),
|
|
66
|
+
isNaN: (host) => coercion(host.runner, "isNaN"),
|
|
67
|
+
Date: (host) => dateGlobal(host.runner),
|
|
68
|
+
RegExp: (host) => regexpGlobal(host.runner),
|
|
69
|
+
Map: (host) => mapGlobal(host.runner),
|
|
70
|
+
Set: (host) => setGlobal(host.runner),
|
|
71
|
+
URL: (host) => urlGlobal(host.runner),
|
|
72
|
+
URLSearchParams: (host) => urlSearchParamsGlobal(host.runner),
|
|
73
|
+
encodeURI: (host) => uriGlobal(host.runner, "encodeURI"),
|
|
74
|
+
encodeURIComponent: (host) => uriGlobal(host.runner, "encodeURIComponent"),
|
|
75
|
+
decodeURI: (host) => uriGlobal(host.runner, "decodeURI"),
|
|
76
|
+
decodeURIComponent: (host) => uriGlobal(host.runner, "decodeURIComponent"),
|
|
77
|
+
atob: (host) => base64Global(host.runner, "atob"),
|
|
78
|
+
btoa: (host) => base64Global(host.runner, "btoa"),
|
|
79
|
+
crypto: (host) => cryptoGlobal(host.runner),
|
|
80
|
+
...Object.fromEntries(errorTypes.map((type) => [type, (host) => errorGlobal(type, host.runner)])),
|
|
81
|
+
};
|
|
82
|
+
/** Names bound in every program before extensions apply. */
|
|
83
|
+
export const globalNames = new Set(Object.keys(table));
|
|
45
84
|
/** The immutable global bindings of every program, in declaration order. */
|
|
46
85
|
export const globals = (host) => {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return [
|
|
50
|
-
["tools", new ToolReference([])],
|
|
51
|
-
["search", native(runner.prototypes, { name: "search", call: (_, args) => host.search(args), callback: false })],
|
|
52
|
-
["undefined", undefined],
|
|
53
|
-
["NaN", NaN],
|
|
54
|
-
["Infinity", Infinity],
|
|
55
|
-
["Object", objectGlobal(runner, host.toolKeys)],
|
|
56
|
-
["Function", functionGlobal(runner)],
|
|
57
|
-
["Array", arrayGlobal(runner)],
|
|
58
|
-
["Math", mathGlobal(runner)],
|
|
59
|
-
["JSON", jsonGlobal(runner)],
|
|
60
|
-
["console", consoleGlobal(runner, host.logs)],
|
|
61
|
-
["Promise", promiseGlobal(runner, host.promises)],
|
|
62
|
-
["Symbol", symbolGlobal(runner)],
|
|
63
|
-
["Number", numberGlobal(runner)],
|
|
64
|
-
["String", stringGlobal(runner)],
|
|
65
|
-
["Boolean", booleanGlobal(runner)],
|
|
66
|
-
["parseInt", coercion(runner, "parseInt", 2)],
|
|
67
|
-
["parseFloat", coercion(runner, "parseFloat")],
|
|
68
|
-
["isFinite", coercion(runner, "isFinite")],
|
|
69
|
-
["isNaN", coercion(runner, "isNaN")],
|
|
70
|
-
["Date", dateGlobal(runner)],
|
|
71
|
-
["RegExp", regexpGlobal(runner)],
|
|
72
|
-
["Map", mapGlobal(runner)],
|
|
73
|
-
["Set", setGlobal(runner)],
|
|
74
|
-
["URL", urlGlobal(runner)],
|
|
75
|
-
["URLSearchParams", urlSearchParamsGlobal(runner)],
|
|
76
|
-
["encodeURI", uriGlobal(runner, "encodeURI")],
|
|
77
|
-
["encodeURIComponent", uriGlobal(runner, "encodeURIComponent")],
|
|
78
|
-
["decodeURI", uriGlobal(runner, "decodeURI")],
|
|
79
|
-
["decodeURIComponent", uriGlobal(runner, "decodeURIComponent")],
|
|
80
|
-
["atob", base64Global(runner, "atob")],
|
|
81
|
-
["btoa", base64Global(runner, "btoa")],
|
|
82
|
-
["crypto", cryptoGlobal(runner)],
|
|
83
|
-
...errorTypes.map((type) => [type, errorGlobal(type, runner)]),
|
|
84
|
-
];
|
|
86
|
+
generatorGlobals(host.runner, host.promises);
|
|
87
|
+
return Object.entries(table).map(([name, factory]) => [name, factory(host)]);
|
|
85
88
|
};
|
|
@@ -104,6 +104,11 @@ export declare class ProgramURL extends ProgramObject {
|
|
|
104
104
|
readonly searchParams: ProgramURLSearchParams;
|
|
105
105
|
constructor(proto: ProgramObject, searchParamsProto: ProgramObject, url: URL);
|
|
106
106
|
}
|
|
107
|
+
/** An instance of an extension class: the host object lives in a field no property path reaches. */
|
|
108
|
+
export declare class ProgramHandle extends ProgramObject {
|
|
109
|
+
readonly instance: object;
|
|
110
|
+
constructor(proto: ProgramObject, instance: object);
|
|
111
|
+
}
|
|
107
112
|
/** Built-in objects that wrap a host value; data-like, but never plain data. */
|
|
108
113
|
export declare const isWrapper: (value: unknown) => value is ProgramDate | ProgramRegExp | ProgramMap | ProgramSet | ProgramURL | ProgramURLSearchParams;
|
|
109
114
|
export declare const parseArrayIndex: (key: string | number) => number | undefined;
|
|
@@ -119,7 +124,7 @@ export declare const hasPrototype: (value: unknown, proto: ProgramObject) => boo
|
|
|
119
124
|
export declare const set: (target: ProgramObject, key: PropertyKey, value: unknown) => boolean;
|
|
120
125
|
/** [[DefineOwnProperty]] for a data property, ignoring the chain. */
|
|
121
126
|
export declare const define: (target: ProgramObject, key: PropertyKey, value: unknown, attrs?: Attributes) => void;
|
|
122
|
-
export declare const defineAccessor: (target: ProgramObject, key: PropertyKey, get: Getter, set?: Setter) => void;
|
|
127
|
+
export declare const defineAccessor: (target: ProgramObject, key: PropertyKey, get: Getter | undefined, set?: Setter) => void;
|
|
123
128
|
export declare const remove: (target: ProgramObject, key: PropertyKey) => boolean;
|
|
124
129
|
export declare const ownKeys: (target: ProgramObject) => Array<string | symbol>;
|
|
125
130
|
/** Own enumerable keys, including the iterator symbols; what spread and `Object.assign` copy. */
|
|
@@ -114,6 +114,14 @@ export class ProgramURL extends ProgramObject {
|
|
|
114
114
|
this.searchParams = new ProgramURLSearchParams(searchParamsProto, url.searchParams);
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
|
+
/** An instance of an extension class: the host object lives in a field no property path reaches. */
|
|
118
|
+
export class ProgramHandle extends ProgramObject {
|
|
119
|
+
instance;
|
|
120
|
+
constructor(proto, instance) {
|
|
121
|
+
super(proto);
|
|
122
|
+
this.instance = instance;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
117
125
|
/** Built-in objects that wrap a host value; data-like, but never plain data. */
|
|
118
126
|
export const isWrapper = (value) => value instanceof ProgramDate ||
|
|
119
127
|
value instanceof ProgramRegExp ||
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { ToolReference } from "../tool-runtime.js";
|
|
2
2
|
import { invalidData } from "./model.js";
|
|
3
|
-
import { Callable, getOwn, isWrapper, ownKeys, ProgramArray, ProgramDate, ProgramGenerator, ProgramMap, ProgramObject, ProgramPromise, ProgramRegExp, ProgramSet, ProgramURL, ProgramURLSearchParams, } from "./objects.js";
|
|
3
|
+
import { Callable, getOwn, isWrapper, ownKeys, ProgramArray, ProgramDate, ProgramGenerator, ProgramHandle, ProgramMap, ProgramObject, ProgramPromise, ProgramRegExp, ProgramSet, ProgramURL, ProgramURLSearchParams, } from "./objects.js";
|
|
4
4
|
/** Values that cannot cross the data boundary. */
|
|
5
5
|
export const isRuntimeReference = (value) => value instanceof Callable ||
|
|
6
6
|
value instanceof ProgramGenerator ||
|
|
7
|
+
value instanceof ProgramHandle ||
|
|
7
8
|
value instanceof ToolReference ||
|
|
8
9
|
value instanceof ProgramPromise ||
|
|
9
10
|
isWrapper(value);
|
|
@@ -65,6 +66,8 @@ export const describeValue = (value) => {
|
|
|
65
66
|
return "a URLSearchParams";
|
|
66
67
|
if (value instanceof ProgramGenerator)
|
|
67
68
|
return "a generator";
|
|
69
|
+
if (value instanceof ProgramHandle)
|
|
70
|
+
return `a ${value.instance.constructor.name}`;
|
|
68
71
|
if (isRuntimeReference(value))
|
|
69
72
|
return "a function";
|
|
70
73
|
if (typeof value === "object")
|
package/dist/stdlib/object.js
CHANGED
|
@@ -99,7 +99,7 @@ export const objectGlobal = (runner, toolKeys) => {
|
|
|
99
99
|
const first = args[0];
|
|
100
100
|
if (first === null || first === undefined)
|
|
101
101
|
return new ProgramObject(protos.Object);
|
|
102
|
-
if (
|
|
102
|
+
if (first instanceof ProgramObject)
|
|
103
103
|
return first;
|
|
104
104
|
throw typeError(`Object(${typeof first}) wrapper objects are not supported; use the primitive value directly.`);
|
|
105
105
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "@opencode/codemode",
|
|
4
|
-
"version": "0.0.0-dev-
|
|
4
|
+
"version": "0.0.0-dev-19607",
|
|
5
5
|
"description": "Effect-native confined code execution over schema-described tools",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|