@opencode/codemode 2.2.2 → 2.2.4
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 +5 -1
- package/dist/codemode.js +8 -2
- package/dist/interpreter/errors.d.ts +7 -0
- package/dist/interpreter/errors.js +22 -5
- package/dist/interpreter/execute.d.ts +9 -1
- package/dist/interpreter/execute.js +1 -1
- package/dist/interpreter/extensions.js +64 -33
- package/dist/scan.d.ts +7 -0
- package/dist/scan.js +245 -0
- package/dist/stdlib/bytes.js +5 -1
- package/package.json +1 -1
package/dist/codemode.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
2
|
import type { Extension } from "./extension.js";
|
|
3
|
+
import { scanTools } from "./scan.js";
|
|
3
4
|
import { type Services, type ToolDescription, ToolRuntime } from "./tool-runtime.js";
|
|
4
5
|
import type { Tools } from "./tools.js";
|
|
6
|
+
export { scanTools };
|
|
5
7
|
/** A tool call admitted during an execution. */
|
|
6
8
|
export type { CallResult, ExtensionInvocation, Hooks, ToolCall, ToolDescription, ToolInvocation, } from "./tool-runtime.js";
|
|
7
9
|
/** Signature-construction helpers for host-owned catalog instructions. */
|
|
@@ -43,6 +45,8 @@ export type Environment<Provided extends Record<string, unknown>, Extensions ext
|
|
|
43
45
|
export type ExecuteOptions<Provided extends Record<string, unknown> = {}, Extensions extends ReadonlyArray<Extension> = []> = Options<Provided, Extensions> & {
|
|
44
46
|
/** Source for one program in the supported JavaScript subset. */
|
|
45
47
|
code: string;
|
|
48
|
+
/** Read-only JSON values bound in the program's outer global scope (shadowable by top-level declarations). */
|
|
49
|
+
bindings?: Readonly<Record<string, DataValue>>;
|
|
46
50
|
};
|
|
47
51
|
/** A JSON value that can cross the confined interpreter boundary. */
|
|
48
52
|
export type DataValue = Schema.Json;
|
|
@@ -144,7 +148,7 @@ export type Result = typeof Result.Type;
|
|
|
144
148
|
/** Reusable confined runtime over explicit tools. */
|
|
145
149
|
export type Runtime<R = never> = {
|
|
146
150
|
readonly catalog: ReadonlyArray<ToolDescription>;
|
|
147
|
-
readonly execute: (code: string) => Effect.Effect<Result, never, R>;
|
|
151
|
+
readonly execute: (code: string, bindings?: Readonly<Record<string, DataValue>>) => Effect.Effect<Result, never, R>;
|
|
148
152
|
};
|
|
149
153
|
/** Executes one Effect-native CodeMode program without constructing a reusable runtime. */
|
|
150
154
|
export declare const execute: <const Provided extends Record<string, unknown>, const Extensions extends ReadonlyArray<Extension> = []>(options: ExecuteOptions<Provided, Extensions>) => Effect.Effect<Result, never, Environment<Provided, Extensions>>;
|
package/dist/codemode.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
|
+
import { fromJson } from "./data.js";
|
|
2
3
|
import { executeProgram } from "./interpreter/execute.js";
|
|
3
4
|
import { extensionGlobals } from "./interpreter/extensions.js";
|
|
4
5
|
import { globalNames } from "./interpreter/globals.js";
|
|
6
|
+
import { scanTools } from "./scan.js";
|
|
5
7
|
import { ToolRuntime } from "./tool-runtime.js";
|
|
8
|
+
export { scanTools };
|
|
6
9
|
/** Signature-construction helpers for host-owned catalog instructions. */
|
|
7
10
|
export { searchSignature, toolExpression } from "./tool-runtime.js";
|
|
8
11
|
/** Schema for a host tool input containing CodeMode source. */
|
|
@@ -56,7 +59,7 @@ const resolveExecutionLimits = (limits) => ({
|
|
|
56
59
|
maxOutputBytes: validateLimit("maxOutputBytes", limits?.maxOutputBytes, 0),
|
|
57
60
|
});
|
|
58
61
|
/** Executes one Effect-native CodeMode program without constructing a reusable runtime. */
|
|
59
|
-
export const execute = (options) => make(options).execute(options.code);
|
|
62
|
+
export const execute = (options) => make(options).execute(options.code, options.bindings);
|
|
60
63
|
/** Creates an Effect-native runtime over explicit, schema-described tools and extensions. */
|
|
61
64
|
export const make = (options = {}) => {
|
|
62
65
|
const prepared = ToolRuntime.prepare((options.tools ?? {}));
|
|
@@ -74,6 +77,9 @@ export const make = (options = {}) => {
|
|
|
74
77
|
get catalog() {
|
|
75
78
|
return prepared.catalog;
|
|
76
79
|
},
|
|
77
|
-
execute: (code) => executeProgram(code, prepared, limits, options.hooks ?? {}, (ctx) =>
|
|
80
|
+
execute: (code, bindings) => executeProgram(code, prepared, limits, options.hooks ?? {}, (ctx) => [
|
|
81
|
+
...Object.entries(bindings ?? {}).map(([name, value]) => [name, fromJson(ctx, value)]),
|
|
82
|
+
...extensionGlobals(ctx, extensions),
|
|
83
|
+
]),
|
|
78
84
|
};
|
|
79
85
|
};
|
|
@@ -3,6 +3,13 @@ import { type AstNode } from "./model.js";
|
|
|
3
3
|
import { type ErrorType } from "./intrinsics.js";
|
|
4
4
|
import { type Native, ErrorObj, Obj, type Value } from "./objects.js";
|
|
5
5
|
import type { Interpreter } from "./interpreter.js";
|
|
6
|
+
/**
|
|
7
|
+
* A host error's message as text. A host can store anything there; an object must not cross as itself, and turning it
|
|
8
|
+
* into text would run its own methods.
|
|
9
|
+
*/
|
|
10
|
+
export declare const errorMessage: (error: Error) => string;
|
|
11
|
+
/** A host error's name as a program error type, read once so a getter cannot pass the check and then change. */
|
|
12
|
+
export declare const errorType: (error: Error) => ErrorType;
|
|
6
13
|
export declare const normalizeError: (error: unknown) => Diagnostic;
|
|
7
14
|
/**
|
|
8
15
|
* Gives a failure the source location of the expression that raised it, keeping the first one attached. Host errors
|
|
@@ -7,6 +7,24 @@ import { constructor, methods, prototypeFrom, receiver } from "./native.js";
|
|
|
7
7
|
import { define, get, has, hidden, Arr, ErrorObj, Obj, coerceToString, } from "./objects.js";
|
|
8
8
|
import { toPrimitiveString } from "./callback.js";
|
|
9
9
|
import { formatValue } from "../stdlib/console.js";
|
|
10
|
+
/**
|
|
11
|
+
* A host error's message as text. A host can store anything there; an object must not cross as itself, and turning it
|
|
12
|
+
* into text would run its own methods.
|
|
13
|
+
*/
|
|
14
|
+
export const errorMessage = (error) => {
|
|
15
|
+
const message = error.message;
|
|
16
|
+
if (typeof message === "string")
|
|
17
|
+
return message;
|
|
18
|
+
if (message === undefined || typeof message === "function" || (typeof message === "object" && message !== null)) {
|
|
19
|
+
return "";
|
|
20
|
+
}
|
|
21
|
+
return String(message);
|
|
22
|
+
};
|
|
23
|
+
/** A host error's name as a program error type, read once so a getter cannot pass the check and then change. */
|
|
24
|
+
export const errorType = (error) => {
|
|
25
|
+
const name = error.name;
|
|
26
|
+
return typeof name === "string" && isErrorType(name) ? name : "Error";
|
|
27
|
+
};
|
|
10
28
|
export const normalizeError = (error) => {
|
|
11
29
|
if (error instanceof PendingThrow) {
|
|
12
30
|
const location = error.node?.loc ? sourceLocation(error.node) : undefined;
|
|
@@ -45,7 +63,7 @@ export const normalizeError = (error) => {
|
|
|
45
63
|
}
|
|
46
64
|
return { kind: "ExecutionFailure", message: `Uncaught: ${message}` };
|
|
47
65
|
}
|
|
48
|
-
if (error instanceof RangeError && /call stack|recursion/i.test(error
|
|
66
|
+
if (error instanceof RangeError && /call stack|recursion/i.test(errorMessage(error))) {
|
|
49
67
|
return {
|
|
50
68
|
kind: "ExecutionFailure",
|
|
51
69
|
message: "Execution exceeded the maximum nesting depth.",
|
|
@@ -54,7 +72,7 @@ export const normalizeError = (error) => {
|
|
|
54
72
|
if (error instanceof Error) {
|
|
55
73
|
return {
|
|
56
74
|
kind: error.name === "SyntaxError" ? "ParseError" : "ExecutionFailure",
|
|
57
|
-
message: error
|
|
75
|
+
message: errorMessage(error),
|
|
58
76
|
};
|
|
59
77
|
}
|
|
60
78
|
return {
|
|
@@ -73,7 +91,7 @@ export const locate = (error, node) => {
|
|
|
73
91
|
return error;
|
|
74
92
|
}
|
|
75
93
|
if (error instanceof Error && !(error instanceof ToolError) && !(error instanceof ToolRuntimeError)) {
|
|
76
|
-
return new PendingThrow(
|
|
94
|
+
return new PendingThrow(errorType(error), errorMessage(error), node);
|
|
77
95
|
}
|
|
78
96
|
return error;
|
|
79
97
|
};
|
|
@@ -89,8 +107,7 @@ export const materialize = (ctx, thrown) => {
|
|
|
89
107
|
}
|
|
90
108
|
return thrown.value;
|
|
91
109
|
}
|
|
92
|
-
|
|
93
|
-
return createErrorValue(builtins[type], normalizeError(thrown).message);
|
|
110
|
+
return createErrorValue(builtins[thrown instanceof Error ? errorType(thrown) : "Error"], normalizeError(thrown).message);
|
|
94
111
|
};
|
|
95
112
|
/** Error.prototype.toString: `name: message`, omitting whichever side is empty. */
|
|
96
113
|
const errorToString = (self) => {
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
import { type Program } from "acorn";
|
|
1
2
|
import { Effect } from "effect";
|
|
2
|
-
import type { ResolvedExecutionLimits, Result } from "../codemode.js";
|
|
3
|
+
import type { Diagnostic, ResolvedExecutionLimits, Result } from "../codemode.js";
|
|
3
4
|
import { ToolRuntime } from "../tool-runtime.js";
|
|
4
5
|
import type { Value } from "./objects.js";
|
|
5
6
|
import { Interpreter } from "./interpreter.js";
|
|
6
7
|
export declare const executeProgram: <R>(code: string, prepared: ToolRuntime.Prepared<R>, limits: ResolvedExecutionLimits, hooks: ToolRuntime.Hooks<R>, globals?: (ctx: Interpreter<R>) => ReadonlyArray<readonly [string, Value]>) => Effect.Effect<Result, never, R>;
|
|
8
|
+
export declare const parseProgram: (code: string) => {
|
|
9
|
+
ok: true;
|
|
10
|
+
program: Program;
|
|
11
|
+
} | {
|
|
12
|
+
ok: false;
|
|
13
|
+
error: Diagnostic;
|
|
14
|
+
};
|
|
@@ -86,7 +86,7 @@ export const executeProgram = (code, prepared, limits, hooks, globals) => {
|
|
|
86
86
|
})), Effect.map((result) => limits.maxOutputBytes === undefined ? result : boundOutput(result, limits.maxOutputBytes)));
|
|
87
87
|
});
|
|
88
88
|
};
|
|
89
|
-
const parseProgram = (code) => {
|
|
89
|
+
export const parseProgram = (code) => {
|
|
90
90
|
try {
|
|
91
91
|
const program = parse(code, {
|
|
92
92
|
ecmaVersion: "latest",
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { isAsync } from "../extension.js";
|
|
3
3
|
import { hooked } from "../tool-runtime.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { errorMessage, errorType } from "./errors.js";
|
|
5
|
+
import { createErrorValue } from "./intrinsics.js";
|
|
6
|
+
import { MAX_ARRAY_LENGTH, MAX_VALUE_DEPTH } from "./limits.js";
|
|
7
|
+
import { PendingThrow, rangeError, Throw, typeError } from "./model.js";
|
|
7
8
|
import { fn } from "./native.js";
|
|
8
9
|
import { define, get, has, hidden, keys, Arr, Bytes, DateObj, ErrorObj, HeadersObj, MapObj, Obj, RegExpObj, SetObj, URLObj, URLSearchParamsObj, coerceToString, } from "./objects.js";
|
|
9
10
|
import { describeValue, isOpaque } from "./references.js";
|
|
@@ -60,24 +61,35 @@ export const extensionGlobals = (ctx, extensions) => {
|
|
|
60
61
|
return wrap(value, label);
|
|
61
62
|
if (value !== null && typeof value === "object") {
|
|
62
63
|
const next = (item, path) => fromHost(item, path, depth + 1, seen);
|
|
64
|
+
// Nothing a host value hands back is kept: results are read as primitives or copied. Where the value's own
|
|
65
|
+
// method could hand back host storage or an object (`getTime`, `slice`, `map`), its internal data is read.
|
|
63
66
|
if (value instanceof Date)
|
|
64
|
-
return new DateObj(builtins.Date,
|
|
67
|
+
return new DateObj(builtins.Date, Date.prototype.getTime.call(value));
|
|
65
68
|
if (value instanceof RegExp)
|
|
66
69
|
return new RegExpObj(builtins.RegExp, value.source, value.flags);
|
|
67
70
|
if (value instanceof Uint8Array)
|
|
68
71
|
return new Bytes(builtins.Uint8Array, new Uint8Array(value));
|
|
69
|
-
if (value instanceof ArrayBuffer)
|
|
70
|
-
|
|
72
|
+
if (value instanceof ArrayBuffer) {
|
|
73
|
+
const view = new Uint8Array(value);
|
|
74
|
+
// Only a real buffer has bytes to view; anything else would be read as array-like and lose them silently.
|
|
75
|
+
if (view.buffer !== value)
|
|
76
|
+
throw typeError(`${label} produced ${describeHost(value)}, which the program cannot hold.`);
|
|
77
|
+
return new Bytes(builtins.Uint8Array, new Uint8Array(view));
|
|
78
|
+
}
|
|
71
79
|
if (value instanceof Error) {
|
|
72
80
|
if (seen.has(value))
|
|
73
81
|
throw typeError(`${label} produced a circular value.`);
|
|
74
82
|
seen.add(value);
|
|
75
|
-
const copied = createErrorValue(builtins[
|
|
83
|
+
const copied = createErrorValue(builtins[errorType(value)], errorMessage(value));
|
|
76
84
|
const fields = value;
|
|
77
85
|
for (const key of new Set(["cause", ...Object.keys(value)])) {
|
|
78
|
-
if (uncrossed.has(key) || !(key in value)
|
|
86
|
+
if (uncrossed.has(key) || !(key in value))
|
|
87
|
+
continue;
|
|
88
|
+
// Read once: a getter could pass the check and then hand back a function.
|
|
89
|
+
const field = fields[key];
|
|
90
|
+
if (typeof field === "function")
|
|
79
91
|
continue;
|
|
80
|
-
const item = crossing(() => next(
|
|
92
|
+
const item = crossing(() => next(field, `${label}.${key}`));
|
|
81
93
|
if (item === left)
|
|
82
94
|
continue;
|
|
83
95
|
define(copied, key, item, key === "cause" ? hidden : undefined);
|
|
@@ -108,9 +120,19 @@ export const extensionGlobals = (ctx, extensions) => {
|
|
|
108
120
|
throw typeError(`${label} produced a circular value.`);
|
|
109
121
|
seen.add(value);
|
|
110
122
|
if (Array.isArray(value)) {
|
|
111
|
-
const
|
|
123
|
+
const length = value.length;
|
|
124
|
+
if (length > MAX_ARRAY_LENGTH) {
|
|
125
|
+
throw rangeError(`${label} produced an array of ${length} items; at most ${MAX_ARRAY_LENGTH} can cross.`);
|
|
126
|
+
}
|
|
127
|
+
// Filled in order so the copy stays a dense array; holes stay holes, as a copy made with `map` would leave them.
|
|
128
|
+
const items = [];
|
|
129
|
+
for (let index = 0; index < length; index += 1) {
|
|
130
|
+
if (index in value)
|
|
131
|
+
items[index] = next(value[index], `${label}[${index}]`);
|
|
132
|
+
}
|
|
133
|
+
items.length = length;
|
|
112
134
|
seen.delete(value);
|
|
113
|
-
return
|
|
135
|
+
return new Arr(builtins.Array, items);
|
|
114
136
|
}
|
|
115
137
|
const prototype = Object.getPrototypeOf(value);
|
|
116
138
|
if (prototype === Object.prototype || prototype === null) {
|
|
@@ -127,24 +149,30 @@ export const extensionGlobals = (ctx, extensions) => {
|
|
|
127
149
|
// the host function is called, so a call it denies never reaches the host. The host's result, or what it threw,
|
|
128
150
|
// crosses out so the program catches a copy, and `extension.after` sees how the call ended. Functions inside
|
|
129
151
|
// results are part of a value's API and skip the hooks.
|
|
130
|
-
const wrap = (value, label, describe) =>
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
152
|
+
const wrap = (value, label, describe) => {
|
|
153
|
+
// The host function's own `name` and `length` can be anything, even getters: each is read once, and only text
|
|
154
|
+
// and a count cross.
|
|
155
|
+
const name = value.name;
|
|
156
|
+
const length = value.length;
|
|
157
|
+
return fn(builtins, typeof name === "string" ? name : "", typeof length === "number" && Number.isSafeInteger(length) && length >= 0 ? length : 0, function* (_, values, site) {
|
|
158
|
+
const args = values.map((item, index) => toHost(item, `Argument ${index + 1} to ${label}`));
|
|
159
|
+
const cross = (reason) => new Throw(fromHost(reason, label));
|
|
160
|
+
const call = describe?.(args);
|
|
161
|
+
const hooks = ctx.tools.hooks;
|
|
162
|
+
const before = call && hooks["extension.before"]?.(call).pipe(Effect.mapError(cross));
|
|
163
|
+
const after = call && hooks["extension.after"];
|
|
164
|
+
const settle = (effect) => (after ? hooked(call, undefined, after, effect) : effect).pipe(Effect.mapBoth({ onFailure: cross, onSuccess: (result) => fromHost(result, label) }));
|
|
165
|
+
// A denied call fails the way the global would: an async global rejects its promise, a sync one throws.
|
|
166
|
+
if (isAsync(value))
|
|
167
|
+
return pending(Effect.andThen(before ?? Effect.void, settle(Effect.suspend(() => callHost(value, args).effect))), site);
|
|
168
|
+
if (before)
|
|
169
|
+
yield ctx.runner.blocking(before);
|
|
170
|
+
const host = callHost(value, args);
|
|
171
|
+
if (host.async)
|
|
172
|
+
return pending(settle(host.effect), site);
|
|
173
|
+
return (yield ctx.runner.blocking(settle(host.effect)));
|
|
174
|
+
});
|
|
175
|
+
};
|
|
148
176
|
// An asynchronous host call: the program holds a promise that settles when the host's work does.
|
|
149
177
|
const pending = (work, site) => {
|
|
150
178
|
const promise = ctx.runner.newPromise();
|
|
@@ -174,10 +202,11 @@ const callHost = (value, args) => {
|
|
|
174
202
|
};
|
|
175
203
|
/**
|
|
176
204
|
* An error crosses as its name, message, `cause`, and own enumerable fields, such as Node's `code`, `errno`,
|
|
177
|
-
* `syscall`, and `path`. `stack` stays on its own side, and no field may shadow an
|
|
178
|
-
* cross (a socket, a handle, a function) is left behind so the error itself always
|
|
205
|
+
* `syscall`, and `path`. `stack` stays on its own side, `message` crosses once as text, and no field may shadow an
|
|
206
|
+
* Error method. A field that cannot cross (a socket, a handle, a function) is left behind so the error itself always
|
|
207
|
+
* arrives.
|
|
179
208
|
*/
|
|
180
|
-
const uncrossed = new Set(["stack", "constructor", "toString", "__proto__"]);
|
|
209
|
+
const uncrossed = new Set(["stack", "message", "constructor", "toString", "__proto__"]);
|
|
181
210
|
const left = Symbol("left behind");
|
|
182
211
|
const crossing = (convert) => {
|
|
183
212
|
try {
|
|
@@ -207,5 +236,7 @@ const describeHost = (value) => {
|
|
|
207
236
|
if (typeof value !== "object" || value === null)
|
|
208
237
|
return `a ${typeof value}`;
|
|
209
238
|
const name = value.constructor?.name;
|
|
210
|
-
|
|
239
|
+
if (name === undefined || name === "")
|
|
240
|
+
return "an object";
|
|
241
|
+
return `${/^[AEIO]/.test(name) ? "an" : "a"} ${name}`;
|
|
211
242
|
};
|
package/dist/scan.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Result } from "effect";
|
|
2
|
+
import type { Diagnostic } from "./codemode.js";
|
|
3
|
+
/**
|
|
4
|
+
* Extracts every `tools.<path>` referenced by a Code Mode program, in first-seen order. Fails on syntax errors,
|
|
5
|
+
* dynamic bracket access (`tools[name]`), or aliasing/passing `tools` rather than calling `tools.<path>(...)` directly.
|
|
6
|
+
*/
|
|
7
|
+
export declare function scanTools(code: string): Result.Result<ReadonlyArray<string>, Diagnostic>;
|
package/dist/scan.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { Result } from "effect";
|
|
2
|
+
import { parseProgram } from "./interpreter/execute.js";
|
|
3
|
+
import { formatLocation } from "./interpreter/model.js";
|
|
4
|
+
/**
|
|
5
|
+
* Extracts every `tools.<path>` referenced by a Code Mode program, in first-seen order. Fails on syntax errors,
|
|
6
|
+
* dynamic bracket access (`tools[name]`), or aliasing/passing `tools` rather than calling `tools.<path>(...)` directly.
|
|
7
|
+
*/
|
|
8
|
+
export function scanTools(code) {
|
|
9
|
+
if (code.trim().length === 0) {
|
|
10
|
+
return Result.fail({ kind: "ParseError", message: "Code cannot be empty." });
|
|
11
|
+
}
|
|
12
|
+
const parsed = parseProgram(code);
|
|
13
|
+
if (!parsed.ok)
|
|
14
|
+
return Result.fail(parsed.error);
|
|
15
|
+
const found = new Set();
|
|
16
|
+
const scopes = [collectScope(parsed.program, true)];
|
|
17
|
+
const ancestors = [];
|
|
18
|
+
const isShadowed = () => scopes.some((scope) => scope.has("tools"));
|
|
19
|
+
const fail = (node, message) => {
|
|
20
|
+
const loc = node.loc?.start ? { line: node.loc.start.line, column: node.loc.start.column + 1 } : undefined;
|
|
21
|
+
return {
|
|
22
|
+
kind: "UnsupportedSyntax",
|
|
23
|
+
message: `${message}${loc ? formatLocation(loc) : ""}`,
|
|
24
|
+
...(loc ? { location: loc } : {}),
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
const visit = (node) => {
|
|
28
|
+
if (!node)
|
|
29
|
+
return undefined;
|
|
30
|
+
if (node.type === "Identifier" && node.name === "tools" && !isShadowed()) {
|
|
31
|
+
const parent = ancestors.at(-1);
|
|
32
|
+
if (!isReferencePosition(node, parent))
|
|
33
|
+
return undefined;
|
|
34
|
+
const chain = readStaticToolCall(node, ancestors);
|
|
35
|
+
if (!chain.ok)
|
|
36
|
+
return fail(chain.node, chain.reason);
|
|
37
|
+
found.add(chain.path);
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
const pushedScope = enterNodeScope(node);
|
|
41
|
+
if (pushedScope)
|
|
42
|
+
scopes.push(pushedScope);
|
|
43
|
+
ancestors.push(node);
|
|
44
|
+
const error = visitChildren(node, visit);
|
|
45
|
+
ancestors.pop();
|
|
46
|
+
if (pushedScope)
|
|
47
|
+
scopes.pop();
|
|
48
|
+
return error;
|
|
49
|
+
};
|
|
50
|
+
const diagnostic = visit(parsed.program);
|
|
51
|
+
if (diagnostic)
|
|
52
|
+
return Result.fail(diagnostic);
|
|
53
|
+
return Result.succeed(Array.from(found));
|
|
54
|
+
}
|
|
55
|
+
function isReferencePosition(node, parent) {
|
|
56
|
+
if (!parent)
|
|
57
|
+
return true;
|
|
58
|
+
if (parent.type === "MemberExpression" && parent.property === node && !parent.computed)
|
|
59
|
+
return false;
|
|
60
|
+
if (parent.type === "Property" && parent.key === node && !parent.computed && !parent.shorthand)
|
|
61
|
+
return false;
|
|
62
|
+
if (parent.type === "MethodDefinition" && parent.key === node && !parent.computed)
|
|
63
|
+
return false;
|
|
64
|
+
if ((parent.type === "LabeledStatement" || parent.type === "BreakStatement" || parent.type === "ContinueStatement") &&
|
|
65
|
+
parent.label === node) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
if ((parent.type === "FunctionDeclaration" || parent.type === "FunctionExpression" || parent.type === "ClassDeclaration" || parent.type === "ClassExpression") &&
|
|
69
|
+
parent.id === node) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
if (parent.type === "VariableDeclarator" && parent.id === node)
|
|
73
|
+
return false;
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
function readStaticToolCall(root, ancestors) {
|
|
77
|
+
const segments = [];
|
|
78
|
+
let current = root;
|
|
79
|
+
let index = ancestors.length - 1;
|
|
80
|
+
while (index >= 0) {
|
|
81
|
+
const parent = ancestors[index];
|
|
82
|
+
if (parent.type === "ChainExpression" && parent.expression === current) {
|
|
83
|
+
current = parent;
|
|
84
|
+
index -= 1;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (parent.type !== "MemberExpression" || parent.object !== current)
|
|
88
|
+
break;
|
|
89
|
+
if (!parent.computed && parent.property.type === "Identifier") {
|
|
90
|
+
segments.push(parent.property.name);
|
|
91
|
+
current = parent;
|
|
92
|
+
index -= 1;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (parent.computed && parent.property.type === "Literal" && typeof parent.property.value === "string") {
|
|
96
|
+
segments.push(parent.property.value);
|
|
97
|
+
current = parent;
|
|
98
|
+
index -= 1;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
ok: false,
|
|
103
|
+
node: parent.property,
|
|
104
|
+
reason: "Schedule checks must reference tools statically (e.g. tools.slack.read_thread), not with dynamic bracket expressions.",
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
const callParent = index >= 0 ? ancestors[index] : undefined;
|
|
108
|
+
if (segments.length === 0 || callParent?.type !== "CallExpression" || callParent.callee !== current) {
|
|
109
|
+
return {
|
|
110
|
+
ok: false,
|
|
111
|
+
node: current,
|
|
112
|
+
reason: "Schedule checks must call tools directly as tools.<name>(...), not alias or pass the tools object.",
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
return { ok: true, path: segments.join(".") };
|
|
116
|
+
}
|
|
117
|
+
function enterNodeScope(node) {
|
|
118
|
+
if (node.type === "FunctionDeclaration" ||
|
|
119
|
+
node.type === "FunctionExpression" ||
|
|
120
|
+
node.type === "ArrowFunctionExpression") {
|
|
121
|
+
const names = new Set();
|
|
122
|
+
if (node.id)
|
|
123
|
+
names.add(node.id.name);
|
|
124
|
+
for (const param of node.params)
|
|
125
|
+
collectPattern(param, names);
|
|
126
|
+
if (node.body.type === "BlockStatement") {
|
|
127
|
+
for (const name of collectScope(node.body, true))
|
|
128
|
+
names.add(name);
|
|
129
|
+
}
|
|
130
|
+
return names;
|
|
131
|
+
}
|
|
132
|
+
if (node.type === "BlockStatement")
|
|
133
|
+
return collectScope(node, false);
|
|
134
|
+
if (node.type === "CatchClause") {
|
|
135
|
+
const names = new Set();
|
|
136
|
+
if (node.param)
|
|
137
|
+
collectPattern(node.param, names);
|
|
138
|
+
return names;
|
|
139
|
+
}
|
|
140
|
+
if (node.type === "ForStatement" && node.init?.type === "VariableDeclaration" && node.init.kind !== "var") {
|
|
141
|
+
const names = new Set();
|
|
142
|
+
for (const decl of node.init.declarations)
|
|
143
|
+
collectPattern(decl.id, names);
|
|
144
|
+
return names;
|
|
145
|
+
}
|
|
146
|
+
if ((node.type === "ForInStatement" || node.type === "ForOfStatement") &&
|
|
147
|
+
node.left.type === "VariableDeclaration" &&
|
|
148
|
+
node.left.kind !== "var") {
|
|
149
|
+
const names = new Set();
|
|
150
|
+
for (const decl of node.left.declarations)
|
|
151
|
+
collectPattern(decl.id, names);
|
|
152
|
+
return names;
|
|
153
|
+
}
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
function collectScope(container, includeVar) {
|
|
157
|
+
const names = new Set();
|
|
158
|
+
for (const statement of container.body) {
|
|
159
|
+
if (statement.type === "FunctionDeclaration" && statement.id)
|
|
160
|
+
names.add(statement.id.name);
|
|
161
|
+
if (statement.type === "ClassDeclaration" && statement.id)
|
|
162
|
+
names.add(statement.id.name);
|
|
163
|
+
if (statement.type === "VariableDeclaration" && (includeVar || statement.kind !== "var")) {
|
|
164
|
+
for (const decl of statement.declarations)
|
|
165
|
+
collectPattern(decl.id, names);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (includeVar)
|
|
169
|
+
collectNestedVars(container.body, names);
|
|
170
|
+
return names;
|
|
171
|
+
}
|
|
172
|
+
function collectNestedVars(statements, names) {
|
|
173
|
+
const walkStmt = (node) => {
|
|
174
|
+
if (!node)
|
|
175
|
+
return;
|
|
176
|
+
if (node.type === "FunctionDeclaration" ||
|
|
177
|
+
node.type === "FunctionExpression" ||
|
|
178
|
+
node.type === "ArrowFunctionExpression") {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (node.type === "VariableDeclaration" && node.kind === "var") {
|
|
182
|
+
for (const decl of node.declarations)
|
|
183
|
+
collectPattern(decl.id, names);
|
|
184
|
+
}
|
|
185
|
+
visitChildren(node, (child) => {
|
|
186
|
+
walkStmt(child);
|
|
187
|
+
return undefined;
|
|
188
|
+
});
|
|
189
|
+
};
|
|
190
|
+
for (const stmt of statements)
|
|
191
|
+
walkStmt(stmt);
|
|
192
|
+
}
|
|
193
|
+
function collectPattern(pattern, names) {
|
|
194
|
+
switch (pattern.type) {
|
|
195
|
+
case "Identifier":
|
|
196
|
+
names.add(pattern.name);
|
|
197
|
+
return;
|
|
198
|
+
case "AssignmentPattern":
|
|
199
|
+
collectPattern(pattern.left, names);
|
|
200
|
+
return;
|
|
201
|
+
case "RestElement":
|
|
202
|
+
collectPattern(pattern.argument, names);
|
|
203
|
+
return;
|
|
204
|
+
case "ArrayPattern":
|
|
205
|
+
for (const element of pattern.elements)
|
|
206
|
+
if (element)
|
|
207
|
+
collectPattern(element, names);
|
|
208
|
+
return;
|
|
209
|
+
case "ObjectPattern":
|
|
210
|
+
for (const prop of pattern.properties) {
|
|
211
|
+
if (prop.type === "RestElement")
|
|
212
|
+
collectPattern(prop.argument, names);
|
|
213
|
+
if (prop.type === "Property")
|
|
214
|
+
collectPattern(prop.value, names);
|
|
215
|
+
}
|
|
216
|
+
return;
|
|
217
|
+
case "MemberExpression":
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
function visitChildren(node, visit) {
|
|
222
|
+
for (const [key, value] of Object.entries(node)) {
|
|
223
|
+
if (key === "type" || key === "start" || key === "end" || key === "loc" || key === "range")
|
|
224
|
+
continue;
|
|
225
|
+
if (Array.isArray(value)) {
|
|
226
|
+
for (const item of value) {
|
|
227
|
+
if (isAstNode(item)) {
|
|
228
|
+
const error = visit(item);
|
|
229
|
+
if (error)
|
|
230
|
+
return error;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
if (isAstNode(value)) {
|
|
236
|
+
const error = visit(value);
|
|
237
|
+
if (error)
|
|
238
|
+
return error;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return undefined;
|
|
242
|
+
}
|
|
243
|
+
function isAstNode(value) {
|
|
244
|
+
return typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
|
|
245
|
+
}
|
package/dist/stdlib/bytes.js
CHANGED
|
@@ -189,7 +189,11 @@ export const textEncoderGlobal = (ctx) => {
|
|
|
189
189
|
const encoder = new TextEncoder();
|
|
190
190
|
defineAccessor(proto, "encoding", () => "utf-8");
|
|
191
191
|
methods(builtins, proto, [
|
|
192
|
-
[
|
|
192
|
+
[
|
|
193
|
+
"encode",
|
|
194
|
+
0,
|
|
195
|
+
(_, args) => new Bytes(builtins.Uint8Array, encoder.encode(coerceToString(args[0] === undefined ? "" : args[0]))),
|
|
196
|
+
],
|
|
193
197
|
]);
|
|
194
198
|
return constructor(builtins, proto, {
|
|
195
199
|
name: "TextEncoder",
|
package/package.json
CHANGED