@opencode/codemode 2.2.7 → 2.3.0
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 +11 -13
- package/dist/{vm/async.d.ts → async.d.ts} +1 -1
- package/dist/codemode.d.ts +8 -22
- package/dist/codemode.js +17 -38
- package/dist/data.d.ts +4 -19
- package/dist/data.js +1 -82
- package/dist/extension.d.ts +8 -2
- package/dist/extension.js +9 -0
- package/dist/{vm/host.d.ts → host.d.ts} +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/{vm/opcodes.d.ts → opcodes.d.ts} +1 -1
- package/dist/{vm/prelude → prelude}/helpers.js +1 -1
- package/dist/{vm/runtime → runtime}/driver.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/driver.js +1 -1
- package/dist/{vm/runtime → runtime}/errors.js +2 -2
- package/dist/{vm/runtime → runtime}/execute.d.ts +17 -8
- package/dist/{vm/runtime → runtime}/execute.js +11 -9
- package/dist/{vm/runtime → runtime}/extensions.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/extensions.js +39 -4
- package/dist/{vm/runtime → runtime}/globals.js +1 -1
- package/dist/{vm/runtime → runtime}/interpreter.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/model.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/objects.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/objects.js +1 -1
- package/dist/{vm/runtime → runtime}/operations.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/operations.js +1 -1
- package/dist/scan.js +2 -2
- package/dist/{vm/scheduler.d.ts → scheduler.d.ts} +1 -1
- package/dist/{vm/snapshot → snapshot}/codec.d.ts +6 -5
- package/dist/{vm/snapshot → snapshot}/codec.js +12 -5
- package/dist/{vm/snapshot → snapshot}/snapshot.d.ts +8 -5
- package/dist/{vm/snapshot → snapshot}/snapshot.js +14 -3
- package/dist/{vm/snapshot.js → snapshot.js} +1 -1
- package/dist/stdlib/array.d.ts +14 -9
- package/dist/stdlib/array.js +89 -276
- package/dist/stdlib/bytes.d.ts +11 -7
- package/dist/stdlib/bytes.js +112 -79
- package/dist/stdlib/collections.d.ts +7 -7
- package/dist/stdlib/collections.js +30 -339
- package/dist/stdlib/console.d.ts +2 -2
- package/dist/stdlib/console.js +3 -3
- package/dist/stdlib/date.d.ts +2 -2
- package/dist/stdlib/date.js +21 -30
- package/dist/stdlib/headers.d.ts +2 -2
- package/dist/stdlib/headers.js +15 -33
- package/dist/stdlib/iterator.d.ts +2 -2
- package/dist/stdlib/iterator.js +10 -302
- package/dist/stdlib/json.d.ts +2 -2
- package/dist/stdlib/json.js +15 -31
- package/dist/stdlib/math.d.ts +2 -7
- package/dist/stdlib/math.js +6 -30
- package/dist/stdlib/number.d.ts +3 -3
- package/dist/stdlib/number.js +8 -8
- package/dist/stdlib/object.d.ts +4 -4
- package/dist/stdlib/object.js +12 -30
- package/dist/stdlib/regexp.d.ts +6 -5
- package/dist/stdlib/regexp.js +18 -13
- package/dist/stdlib/string.d.ts +2 -2
- package/dist/stdlib/string.js +53 -53
- package/dist/stdlib/url.d.ts +21 -9
- package/dist/stdlib/url.js +55 -55
- package/dist/stdlib/value.d.ts +3 -4
- package/dist/stdlib/value.js +5 -6
- package/dist/stdlib/web.d.ts +4 -4
- package/dist/stdlib/web.js +7 -7
- package/dist/tool-runtime.d.ts +14 -13
- package/dist/tool-runtime.js +245 -53
- package/dist/tool-schema.d.ts +5 -0
- package/dist/tool-schema.js +36 -16
- package/dist/tool.d.ts +24 -9
- package/dist/tool.js +15 -22
- package/dist/{vm/vm.d.ts → vm.d.ts} +3 -3
- package/dist/{vm/vm.js → vm.js} +1 -1
- package/package.json +3 -5
- package/dist/interpreter/callback.d.ts +0 -35
- package/dist/interpreter/callback.js +0 -98
- package/dist/interpreter/errors.js +0 -173
- package/dist/interpreter/execute.d.ts +0 -14
- package/dist/interpreter/execute.js +0 -207
- package/dist/interpreter/extensions.d.ts +0 -9
- package/dist/interpreter/extensions.js +0 -248
- package/dist/interpreter/generators.d.ts +0 -3
- package/dist/interpreter/generators.js +0 -168
- package/dist/interpreter/globals.js +0 -144
- package/dist/interpreter/interpreter.d.ts +0 -48
- package/dist/interpreter/interpreter.js +0 -2253
- package/dist/interpreter/intrinsics.d.ts +0 -15
- package/dist/interpreter/intrinsics.js +0 -105
- package/dist/interpreter/model.d.ts +0 -68
- package/dist/interpreter/model.js +0 -54
- package/dist/interpreter/native.d.ts +0 -17
- package/dist/interpreter/native.js +0 -35
- package/dist/interpreter/objects.d.ts +0 -301
- package/dist/interpreter/objects.js +0 -592
- package/dist/interpreter/promises.d.ts +0 -18
- package/dist/interpreter/promises.js +0 -292
- package/dist/interpreter/runner.d.ts +0 -178
- package/dist/interpreter/runner.js +0 -480
- package/dist/interpreter/scope.d.ts +0 -16
- package/dist/interpreter/scope.js +0 -79
- package/dist/suspend-error.d.ts +0 -24
- package/dist/suspend-error.js +0 -25
- package/dist/vm/data.d.ts +0 -7
- package/dist/vm/data.js +0 -13
- package/dist/vm/index.d.ts +0 -6
- package/dist/vm/index.js +0 -6
- package/dist/vm/runtime/errors.d.ts +0 -23
- package/dist/vm/runtime/globals.d.ts +0 -6
- package/dist/vm/runtime/limits.d.ts +0 -10
- package/dist/vm/runtime/limits.js +0 -19
- package/dist/vm/runtime/modules.d.ts +0 -4
- package/dist/vm/runtime/modules.js +0 -33
- package/dist/vm/runtime/references.d.ts +0 -8
- package/dist/vm/runtime/references.js +0 -51
- package/dist/vm/stdlib/array.d.ts +0 -16
- package/dist/vm/stdlib/array.js +0 -311
- package/dist/vm/stdlib/bytes.d.ts +0 -11
- package/dist/vm/stdlib/bytes.js +0 -222
- package/dist/vm/stdlib/collections.d.ts +0 -7
- package/dist/vm/stdlib/collections.js +0 -188
- package/dist/vm/stdlib/console.d.ts +0 -6
- package/dist/vm/stdlib/console.js +0 -93
- package/dist/vm/stdlib/date.d.ts +0 -2
- package/dist/vm/stdlib/date.js +0 -123
- package/dist/vm/stdlib/headers.d.ts +0 -2
- package/dist/vm/stdlib/headers.js +0 -92
- package/dist/vm/stdlib/iterator.d.ts +0 -2
- package/dist/vm/stdlib/iterator.js +0 -30
- package/dist/vm/stdlib/json.d.ts +0 -3
- package/dist/vm/stdlib/json.js +0 -42
- package/dist/vm/stdlib/math.d.ts +0 -3
- package/dist/vm/stdlib/math.js +0 -73
- package/dist/vm/stdlib/number.d.ts +0 -3
- package/dist/vm/stdlib/number.js +0 -72
- package/dist/vm/stdlib/object.d.ts +0 -6
- package/dist/vm/stdlib/object.js +0 -201
- package/dist/vm/stdlib/regexp.d.ts +0 -8
- package/dist/vm/stdlib/regexp.js +0 -119
- package/dist/vm/stdlib/string.d.ts +0 -2
- package/dist/vm/stdlib/string.js +0 -272
- package/dist/vm/stdlib/url.d.ts +0 -25
- package/dist/vm/stdlib/url.js +0 -255
- package/dist/vm/stdlib/value.d.ts +0 -6
- package/dist/vm/stdlib/value.js +0 -32
- package/dist/vm/stdlib/web.d.ts +0 -5
- package/dist/vm/stdlib/web.js +0 -97
- /package/dist/{vm/async.js → async.js} +0 -0
- /package/dist/{vm/code.d.ts → code.d.ts} +0 -0
- /package/dist/{vm/code.js → code.js} +0 -0
- /package/dist/{vm/compiler → compiler}/compile.d.ts +0 -0
- /package/dist/{vm/compiler → compiler}/compile.js +0 -0
- /package/dist/{vm/compiler → compiler}/scope.d.ts +0 -0
- /package/dist/{vm/compiler → compiler}/scope.js +0 -0
- /package/dist/{vm/generators.d.ts → generators.d.ts} +0 -0
- /package/dist/{vm/generators.js → generators.js} +0 -0
- /package/dist/{vm/host.js → host.js} +0 -0
- /package/dist/{vm/internal-prelude.d.ts → internal-prelude.d.ts} +0 -0
- /package/dist/{vm/internal-prelude.js → internal-prelude.js} +0 -0
- /package/dist/{vm/opcodes.js → opcodes.js} +0 -0
- /package/dist/{vm/prelude → prelude}/bytecode.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/bytecode.js +0 -0
- /package/dist/{vm/prelude → prelude}/bytecode.json +0 -0
- /package/dist/{vm/prelude → prelude}/helpers.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/prelude.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/prelude.js +0 -0
- /package/dist/{vm/prelude → prelude}/source.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/source.js +0 -0
- /package/dist/{vm/runtime → runtime}/callback.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/callback.js +0 -0
- /package/dist/{interpreter → runtime}/errors.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/generators.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/generators.js +0 -0
- /package/dist/{interpreter → runtime}/globals.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/interpreter.js +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsic-names.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsic-names.js +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsics.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsics.js +0 -0
- /package/dist/{interpreter → runtime}/limits.d.ts +0 -0
- /package/dist/{interpreter → runtime}/limits.js +0 -0
- /package/dist/{vm/runtime → runtime}/model.js +0 -0
- /package/dist/{interpreter → runtime}/modules.d.ts +0 -0
- /package/dist/{interpreter → runtime}/modules.js +0 -0
- /package/dist/{vm/runtime → runtime}/native.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/native.js +0 -0
- /package/dist/{vm/runtime → runtime}/promises.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/promises.js +0 -0
- /package/dist/{interpreter → runtime}/references.d.ts +0 -0
- /package/dist/{interpreter → runtime}/references.js +0 -0
- /package/dist/{vm/runtime → runtime}/signals.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/signals.js +0 -0
- /package/dist/{vm/runtime → runtime}/source.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/source.js +0 -0
- /package/dist/{vm/runtime → runtime}/table.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/table.js +0 -0
- /package/dist/{vm/scheduler.js → scheduler.js} +0 -0
- /package/dist/{vm/snapshot.d.ts → snapshot.d.ts} +0 -0
package/dist/tool.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
2
|
export const isTool = (value) => value !== undefined && Object.hasOwn(value, "_tag") && value._tag === "CodeModeTool";
|
|
3
3
|
/** Marks a tool's `execute` function as safe to interrupt on suspend and re-run on resume with the same key. */
|
|
4
|
-
export const idempotent = (execute) =>
|
|
4
|
+
export const idempotent = (execute) => ({
|
|
5
5
|
_tag: "CodeModeIdempotent",
|
|
6
6
|
execute,
|
|
7
7
|
});
|
|
@@ -9,15 +9,12 @@ export const idempotent = (execute) => Object.assign((input, context) => execute
|
|
|
9
9
|
* Wraps a tool's execution as `start`, `wait`, and `cancel` so work started before a suspend keeps running and
|
|
10
10
|
* reconnects through its JSON data on resume.
|
|
11
11
|
*/
|
|
12
|
-
export const durable = (options) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
cancel: options.cancel,
|
|
19
|
-
});
|
|
20
|
-
};
|
|
12
|
+
export const durable = (options) => ({
|
|
13
|
+
_tag: "CodeModeDurable",
|
|
14
|
+
start: options.start,
|
|
15
|
+
wait: options.wait,
|
|
16
|
+
cancel: options.cancel,
|
|
17
|
+
});
|
|
21
18
|
/**
|
|
22
19
|
* Declares one schema-described tool available to a CodeMode program through `tools.*`.
|
|
23
20
|
*
|
|
@@ -25,15 +22,11 @@ export const durable = (options) => {
|
|
|
25
22
|
* Without `output`, results are exposed as `void`. Hosts remain responsible for authorization
|
|
26
23
|
* and durable side effects.
|
|
27
24
|
*/
|
|
28
|
-
export const make = (options) => {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
...(mode === undefined ? {} : { mode }),
|
|
37
|
-
execute: (input, context = { key: crypto.randomUUID() }) => spec(input, context),
|
|
38
|
-
};
|
|
39
|
-
};
|
|
25
|
+
export const make = (options) => ({
|
|
26
|
+
_tag: "CodeModeTool",
|
|
27
|
+
description: options.description,
|
|
28
|
+
input: options.input,
|
|
29
|
+
...(options.freeform && { freeform: true }),
|
|
30
|
+
output: options.output,
|
|
31
|
+
execute: options.execute,
|
|
32
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Diagnostic } from "
|
|
1
|
+
import type { Diagnostic } from "./codemode.js";
|
|
2
2
|
import type { Interpreter } from "./runtime/interpreter.js";
|
|
3
3
|
import { type AstNode } from "./runtime/model.js";
|
|
4
4
|
import { Arr, Fn, Obj, type Value } from "./runtime/objects.js";
|
|
@@ -124,11 +124,11 @@ export declare class Vm {
|
|
|
124
124
|
} | undefined;
|
|
125
125
|
/** Saves the run's state as it is now. */
|
|
126
126
|
capture(options: CaptureOptions): {
|
|
127
|
-
readonly snapshot: import("./
|
|
127
|
+
readonly snapshot: import("./codemode.js").Snapshot;
|
|
128
128
|
readonly lost: ReadonlyArray<string>;
|
|
129
129
|
};
|
|
130
130
|
/** Restores a saved state into this fresh VM, after checking it belongs to this build and runtime. */
|
|
131
|
-
restore(saved: unknown, options: RestoreOptions): import("./
|
|
131
|
+
restore(saved: unknown, options: RestoreOptions): import("./codemode.js").Snapshot;
|
|
132
132
|
deliverAnswer(frame: Frame): void;
|
|
133
133
|
acquire(code: Code): Frame;
|
|
134
134
|
release(frame: Frame): void;
|
package/dist/{vm/vm.js → vm.js}
RENAMED
|
@@ -12,7 +12,7 @@ import { Source } from "./runtime/source.js";
|
|
|
12
12
|
import { compiledPrelude } from "./prelude/prelude.js";
|
|
13
13
|
import { constructRegExp } from "./stdlib/regexp.js";
|
|
14
14
|
import { enumerableSource } from "./stdlib/object.js";
|
|
15
|
-
import { ToolReference } from "
|
|
15
|
+
import { ToolReference } from "./tool-runtime.js";
|
|
16
16
|
import { registerAsyncIntrinsics, VmScheduler } from "./async.js";
|
|
17
17
|
import { captureVm, restoreVm } from "./snapshot.js";
|
|
18
18
|
import { compileSource } from "./compiler/compile.js";
|
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": "2.
|
|
4
|
+
"version": "2.3.0",
|
|
5
5
|
"description": "Effect-native confined code execution over schema-described tools",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
@@ -27,10 +27,8 @@
|
|
|
27
27
|
"pack": "bun run script/pack.ts",
|
|
28
28
|
"typecheck": "bun check --no-script",
|
|
29
29
|
"typecheck:tsc": "tsc --noEmit",
|
|
30
|
-
"test": "bun test
|
|
31
|
-
"test:
|
|
32
|
-
"test:vm": "CODEMODE_ENGINE=vm bun test",
|
|
33
|
-
"test:roundtrip": "CODEMODE_ENGINE=vm CODEMODE_ROUNDTRIP=1 bun test"
|
|
30
|
+
"test": "bun test",
|
|
31
|
+
"test:roundtrip": "CODEMODE_ROUNDTRIP=1 bun test"
|
|
34
32
|
},
|
|
35
33
|
"dependencies": {
|
|
36
34
|
"acorn": "8.15.0",
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { Cause } from "effect";
|
|
2
|
-
import type { Interpreter } from "./interpreter.js";
|
|
3
|
-
import { Callable, type Cursor, type NativeResult, type Value } from "./objects.js";
|
|
4
|
-
import type { Eval } from "./runner.js";
|
|
5
|
-
/**
|
|
6
|
-
* Host work that was interrupted on its own. It settles its promise like a rejection, but program code cannot catch
|
|
7
|
-
* it: handlers, `catch`, and `finally` are skipped, an unobserved one is not reported, and one that reaches the
|
|
8
|
-
* program's body interrupts the execution.
|
|
9
|
-
*/
|
|
10
|
-
export declare class Interruption {
|
|
11
|
-
readonly cause: Cause.Cause<unknown>;
|
|
12
|
-
constructor(cause: Cause.Cause<unknown>);
|
|
13
|
-
}
|
|
14
|
-
export declare const isEval: <A>(value: A | Eval<A>) => value is Eval<A>;
|
|
15
|
-
/** IteratorClose: a consumer failure closes the iterator and wins over any close failure, except that a generator's
|
|
16
|
-
* return() is a return completion, so a failing close wins over it, as after `break`. */
|
|
17
|
-
export declare function preserveConsumerError<A>(close: Cursor["close"], body: Eval<A>): Eval<A>;
|
|
18
|
-
export type Hint = "number" | "string" | "default";
|
|
19
|
-
/**
|
|
20
|
-
* ToPrimitive: calls `valueOf`/`toString` in hint order and returns the first primitive result. Dates treat the
|
|
21
|
-
* default hint as "string", like their `Symbol.toPrimitive`. Opaque values (functions, promises, generators, tool
|
|
22
|
-
* references) pass through unchanged so callers reject or describe them in their built-in form.
|
|
23
|
-
*/
|
|
24
|
-
export declare function toPrimitive<R>(ctx: Interpreter<R>, value: Value, hint: Hint): Eval<Value>;
|
|
25
|
-
/** Invoke(value, name): calls the method the value would find through its prototype. */
|
|
26
|
-
export declare const invoke: <R>(ctx: Interpreter<R>, value: Value, name: string, label: string) => Eval<Value>;
|
|
27
|
-
export declare function toPrimitiveString<R>(ctx: Interpreter<R>, value: Value): Eval<string>;
|
|
28
|
-
export declare function toPrimitiveNumber<R>(ctx: Interpreter<R>, value: Value): Eval<number>;
|
|
29
|
-
/**
|
|
30
|
-
* Runs a native body on its arguments after ToPrimitive, in order, with one hint for all positions or one per
|
|
31
|
-
* position. Primitive arguments skip the conversion entirely.
|
|
32
|
-
*/
|
|
33
|
-
export declare const withPrimitives: <R>(ctx: Interpreter<R>, hints: Hint | ReadonlyArray<Hint>, values: Array<Value>, body: (primitives: Array<Value>) => NativeResult) => NativeResult;
|
|
34
|
-
export declare const isSupportedCallback: (value: Value) => value is Callable;
|
|
35
|
-
export declare const applyCollectionCallback: <R>(ctx: Interpreter<R>, callback: Value, name: string) => ((args: Array<Value>, thisValue?: Value) => Eval<Value>);
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { primitivePrototype } from "./intrinsics.js";
|
|
2
|
-
import { GeneratorReturn, typeError } from "./model.js";
|
|
3
|
-
import { Callable, get, Native, DateObj, Obj, coerceToNumber, coerceToString, } from "./objects.js";
|
|
4
|
-
import { isOpaque, typeofValue } from "./references.js";
|
|
5
|
-
/**
|
|
6
|
-
* Host work that was interrupted on its own. It settles its promise like a rejection, but program code cannot catch
|
|
7
|
-
* it: handlers, `catch`, and `finally` are skipped, an unobserved one is not reported, and one that reaches the
|
|
8
|
-
* program's body interrupts the execution.
|
|
9
|
-
*/
|
|
10
|
-
export class Interruption {
|
|
11
|
-
cause;
|
|
12
|
-
constructor(cause) {
|
|
13
|
-
this.cause = cause;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
// Program values never have a `next` method of their own: an object's properties live in its property map.
|
|
17
|
-
export const isEval = (value) => typeof value === "object" && value !== null && typeof value.next === "function";
|
|
18
|
-
/** IteratorClose: a consumer failure closes the iterator and wins over any close failure, except that a generator's
|
|
19
|
-
* return() is a return completion, so a failing close wins over it, as after `break`. */
|
|
20
|
-
export function* preserveConsumerError(close, body) {
|
|
21
|
-
try {
|
|
22
|
-
return yield* body;
|
|
23
|
-
}
|
|
24
|
-
catch (error) {
|
|
25
|
-
if (error instanceof Interruption)
|
|
26
|
-
throw error;
|
|
27
|
-
try {
|
|
28
|
-
yield* close();
|
|
29
|
-
}
|
|
30
|
-
catch (closed) {
|
|
31
|
-
if (error instanceof GeneratorReturn)
|
|
32
|
-
throw closed;
|
|
33
|
-
}
|
|
34
|
-
throw error;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* ToPrimitive: calls `valueOf`/`toString` in hint order and returns the first primitive result. Dates treat the
|
|
39
|
-
* default hint as "string", like their `Symbol.toPrimitive`. Opaque values (functions, promises, generators, tool
|
|
40
|
-
* references) pass through unchanged so callers reject or describe them in their built-in form.
|
|
41
|
-
*/
|
|
42
|
-
export function* toPrimitive(ctx, value, hint) {
|
|
43
|
-
if (!(value instanceof Obj) || isOpaque(value))
|
|
44
|
-
return value;
|
|
45
|
-
const asString = hint === "string" || (hint === "default" && value instanceof DateObj);
|
|
46
|
-
for (const method of asString ? ["toString", "valueOf"] : ["valueOf", "toString"]) {
|
|
47
|
-
const callable = get(value, method);
|
|
48
|
-
if (!(callable instanceof Callable))
|
|
49
|
-
continue;
|
|
50
|
-
const result = yield* ctx.call(callable, value, []);
|
|
51
|
-
if (result === null || (typeof result !== "object" && typeof result !== "function"))
|
|
52
|
-
return result;
|
|
53
|
-
}
|
|
54
|
-
throw typeError("Cannot convert object to primitive value.");
|
|
55
|
-
}
|
|
56
|
-
/** Invoke(value, name): calls the method the value would find through its prototype. */
|
|
57
|
-
export const invoke = (ctx, value, name, label) => {
|
|
58
|
-
const target = value instanceof Obj ? value : primitivePrototype(ctx.builtins, value);
|
|
59
|
-
if (target === undefined)
|
|
60
|
-
throw typeError(`${label} called on null or undefined.`);
|
|
61
|
-
return ctx.call(get(target, name), value, []);
|
|
62
|
-
};
|
|
63
|
-
export function* toPrimitiveString(ctx, value) {
|
|
64
|
-
return coerceToString(yield* toPrimitive(ctx, value, "string"));
|
|
65
|
-
}
|
|
66
|
-
export function* toPrimitiveNumber(ctx, value) {
|
|
67
|
-
return coerceToNumber(yield* toPrimitive(ctx, value, "number"));
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Runs a native body on its arguments after ToPrimitive, in order, with one hint for all positions or one per
|
|
71
|
-
* position. Primitive arguments skip the conversion entirely.
|
|
72
|
-
*/
|
|
73
|
-
export const withPrimitives = (ctx, hints, values, body) => {
|
|
74
|
-
if (!values.some((value) => value instanceof Obj))
|
|
75
|
-
return body(values);
|
|
76
|
-
return (function* () {
|
|
77
|
-
const primitives = [];
|
|
78
|
-
for (const [index, value] of values.entries()) {
|
|
79
|
-
primitives.push(yield* toPrimitive(ctx, value, typeof hints === "string" ? hints : hints[index]));
|
|
80
|
-
}
|
|
81
|
-
const result = body(primitives);
|
|
82
|
-
return isEval(result) ? yield* result : result;
|
|
83
|
-
})();
|
|
84
|
-
};
|
|
85
|
-
// The single acceptance list for callbacks: collections, sort, string replacers,
|
|
86
|
-
// Array.from mappers, and promise reactions all admit exactly these callables.
|
|
87
|
-
// Admission means dispatchable, not necessarily invocable: new-requiring
|
|
88
|
-
// constructors pass the gate and throw a TypeError on call, like JS.
|
|
89
|
-
export const isSupportedCallback = (value) => value instanceof Callable && !(value instanceof Native && !value.callback);
|
|
90
|
-
export const applyCollectionCallback = (ctx, callback, name) => {
|
|
91
|
-
if (!isSupportedCallback(callback)) {
|
|
92
|
-
if (typeofValue(callback) === "function") {
|
|
93
|
-
throw typeError(`${name} cannot use this callable as a callback; wrap it in an arrow function, e.g. (value) => tools.ns.tool(value).`);
|
|
94
|
-
}
|
|
95
|
-
throw typeError(`${name} expects a function callback.`);
|
|
96
|
-
}
|
|
97
|
-
return (callbackArgs, thisValue) => ctx.call(callback, thisValue, callbackArgs);
|
|
98
|
-
};
|
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import { ToolError } from "../tool-error.js";
|
|
2
|
-
import { ToolRuntimeError } from "../tool-runtime.js";
|
|
3
|
-
import { formatLocation, PendingThrow, Throw, sourceLocation, typeError } from "./model.js";
|
|
4
|
-
import { containsRuntimeReference } from "./references.js";
|
|
5
|
-
import { createErrorValue, isErrorType } from "./intrinsics.js";
|
|
6
|
-
import { constructor, methods, prototypeFrom, receiver } from "./native.js";
|
|
7
|
-
import { define, get, has, hidden, Arr, ErrorObj, Obj, coerceToString, } from "./objects.js";
|
|
8
|
-
import { toPrimitiveString } from "./callback.js";
|
|
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
|
-
};
|
|
28
|
-
export const normalizeError = (error) => {
|
|
29
|
-
if (error instanceof PendingThrow) {
|
|
30
|
-
const location = error.node?.loc ? sourceLocation(error.node) : undefined;
|
|
31
|
-
return {
|
|
32
|
-
kind: error.kind,
|
|
33
|
-
message: `${error.type}: ${error.message}${formatLocation(location)}`,
|
|
34
|
-
...(location ? { location } : {}),
|
|
35
|
-
...(error.suggestions ? { suggestions: error.suggestions } : {}),
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
if (error instanceof ToolRuntimeError) {
|
|
39
|
-
return {
|
|
40
|
-
kind: error.kind,
|
|
41
|
-
message: error.message,
|
|
42
|
-
...(error.suggestions.length > 0 ? { suggestions: error.suggestions } : {}),
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
if (error instanceof ToolError) {
|
|
46
|
-
return { kind: "ToolFailure", message: error.message };
|
|
47
|
-
}
|
|
48
|
-
if (error instanceof Throw) {
|
|
49
|
-
const value = error.value;
|
|
50
|
-
if (value instanceof ErrorObj) {
|
|
51
|
-
return value.host ? normalizeError(value.host) : { kind: "ExecutionFailure", message: errorToString(value) };
|
|
52
|
-
}
|
|
53
|
-
let message;
|
|
54
|
-
if (containsRuntimeReference(value)) {
|
|
55
|
-
// Never expose runtime reference internals through thrown values.
|
|
56
|
-
message = "a non-data value";
|
|
57
|
-
}
|
|
58
|
-
else if (typeof value === "string") {
|
|
59
|
-
message = value;
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
message = formatValue(value);
|
|
63
|
-
}
|
|
64
|
-
return { kind: "ExecutionFailure", message: `Uncaught: ${message}` };
|
|
65
|
-
}
|
|
66
|
-
if (error instanceof RangeError && /call stack|recursion/i.test(errorMessage(error))) {
|
|
67
|
-
return {
|
|
68
|
-
kind: "ExecutionFailure",
|
|
69
|
-
message: "Execution exceeded the maximum nesting depth.",
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
if (error instanceof Error) {
|
|
73
|
-
return {
|
|
74
|
-
kind: error.name === "SyntaxError" ? "ParseError" : "ExecutionFailure",
|
|
75
|
-
message: errorMessage(error),
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
return {
|
|
79
|
-
kind: "ExecutionFailure",
|
|
80
|
-
message: String(error),
|
|
81
|
-
};
|
|
82
|
-
};
|
|
83
|
-
/**
|
|
84
|
-
* Gives a failure the source location of the expression that raised it, keeping the first one attached. Host errors
|
|
85
|
-
* that escape a built-in become the equivalent program error here.
|
|
86
|
-
*/
|
|
87
|
-
export const locate = (error, node) => {
|
|
88
|
-
if (error instanceof PendingThrow) {
|
|
89
|
-
if (error.node === undefined && node)
|
|
90
|
-
error.node = node;
|
|
91
|
-
return error;
|
|
92
|
-
}
|
|
93
|
-
if (error instanceof Error && !(error instanceof ToolError) && !(error instanceof ToolRuntimeError)) {
|
|
94
|
-
return new PendingThrow(errorType(error), errorMessage(error), node);
|
|
95
|
-
}
|
|
96
|
-
return error;
|
|
97
|
-
};
|
|
98
|
-
/** The program value a handler receives for a failure; one failure always yields the same value. */
|
|
99
|
-
export const materialize = (ctx, thrown) => {
|
|
100
|
-
if (thrown instanceof Throw)
|
|
101
|
-
return thrown.value;
|
|
102
|
-
const builtins = ctx.builtins;
|
|
103
|
-
if (thrown instanceof PendingThrow) {
|
|
104
|
-
if (thrown.value === undefined) {
|
|
105
|
-
thrown.value = createErrorValue(builtins[thrown.type], thrown.message);
|
|
106
|
-
thrown.value.host = thrown;
|
|
107
|
-
}
|
|
108
|
-
return thrown.value;
|
|
109
|
-
}
|
|
110
|
-
return createErrorValue(builtins[thrown instanceof Error ? errorType(thrown) : "Error"], normalizeError(thrown).message);
|
|
111
|
-
};
|
|
112
|
-
/** Error.prototype.toString: `name: message`, omitting whichever side is empty. */
|
|
113
|
-
const errorToString = (self) => {
|
|
114
|
-
const name = get(self, "name");
|
|
115
|
-
const message = get(self, "message");
|
|
116
|
-
const shownName = name === undefined ? "Error" : coerceToString(name);
|
|
117
|
-
const shownMessage = message === undefined ? "" : coerceToString(message);
|
|
118
|
-
if (shownMessage === "")
|
|
119
|
-
return shownName;
|
|
120
|
-
if (shownName === "")
|
|
121
|
-
return shownMessage;
|
|
122
|
-
return `${shownName}: ${shownMessage}`;
|
|
123
|
-
};
|
|
124
|
-
export const createAggregateErrorValue = (ctx, errors, message, proto = ctx.builtins.AggregateError) => {
|
|
125
|
-
const value = createErrorValue(proto, message);
|
|
126
|
-
define(value, "errors", new Arr(ctx.builtins.Array, errors), { ...hidden });
|
|
127
|
-
return value;
|
|
128
|
-
};
|
|
129
|
-
function* constructAggregateErrorValue(ctx, args, proto) {
|
|
130
|
-
const message = args[1] === undefined ? "" : yield* toPrimitiveString(ctx, args[1]);
|
|
131
|
-
const cursor = yield* ctx.iterate(args[0]);
|
|
132
|
-
if (cursor === undefined)
|
|
133
|
-
throw typeError("new AggregateError(...) expects a synchronous iterable of errors.");
|
|
134
|
-
const errors = [];
|
|
135
|
-
while (true) {
|
|
136
|
-
const step = yield* cursor.next();
|
|
137
|
-
if (step.done)
|
|
138
|
-
return createAggregateErrorValue(ctx, errors, message, proto);
|
|
139
|
-
errors.push(step.value);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
/** An error constructor such as `Error` or `TypeError`; callable with or without `new`, like JS. */
|
|
143
|
-
export const errorGlobal = (type, ctx) => {
|
|
144
|
-
const builtins = ctx.builtins;
|
|
145
|
-
const prototype = builtins[type];
|
|
146
|
-
const construct = function* (args, newTarget) {
|
|
147
|
-
const proto = prototypeFrom(newTarget, prototype);
|
|
148
|
-
const created = type === "AggregateError"
|
|
149
|
-
? yield* constructAggregateErrorValue(ctx, args, proto)
|
|
150
|
-
: createErrorValue(proto, args[0] === undefined ? undefined : yield* toPrimitiveString(ctx, args[0]));
|
|
151
|
-
// ES2022 `new Error(message, { cause })`: installed only when the options object has the property at all.
|
|
152
|
-
const options = args[type === "AggregateError" ? 2 : 1];
|
|
153
|
-
if (options instanceof Obj && has(options, "cause"))
|
|
154
|
-
define(created, "cause", get(options, "cause"), hidden);
|
|
155
|
-
return created;
|
|
156
|
-
};
|
|
157
|
-
const ctor = constructor(builtins, prototype, {
|
|
158
|
-
name: type,
|
|
159
|
-
length: type === "AggregateError" ? 2 : 1,
|
|
160
|
-
call: (_, args) => construct(args, ctor),
|
|
161
|
-
construct,
|
|
162
|
-
});
|
|
163
|
-
if (type === "Error") {
|
|
164
|
-
methods(builtins, prototype, [
|
|
165
|
-
["toString", 0, (thisValue) => errorToString(receiver(Obj, thisValue, "Error.prototype.toString"))],
|
|
166
|
-
]);
|
|
167
|
-
methods(builtins, ctor, [["isError", 1, (_, args) => args[0] instanceof ErrorObj]]);
|
|
168
|
-
return ctor;
|
|
169
|
-
}
|
|
170
|
-
// Derived constructors extend Error itself, so its statics are inherited. The globals table creates Error first.
|
|
171
|
-
ctor.proto = get(builtins.Error, "constructor");
|
|
172
|
-
return ctor;
|
|
173
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { type Program } from "acorn";
|
|
2
|
-
import { Effect } from "effect";
|
|
3
|
-
import type { Diagnostic, ResolvedExecutionLimits, Result } from "../codemode.js";
|
|
4
|
-
import { ToolRuntime } from "../tool-runtime.js";
|
|
5
|
-
import { Interpreter } from "./interpreter.js";
|
|
6
|
-
import type { Value } from "./objects.js";
|
|
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
|
-
};
|
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
import { parse } from "acorn";
|
|
2
|
-
import { Cause, Effect, Option, Schema } from "effect";
|
|
3
|
-
import { ToolRuntime } from "../tool-runtime.js";
|
|
4
|
-
import { normalizeError } from "./errors.js";
|
|
5
|
-
import { createBuiltins } from "./intrinsics.js";
|
|
6
|
-
import { Interpreter } from "./interpreter.js";
|
|
7
|
-
import { formatLocation, sourceLocation } from "./model.js";
|
|
8
|
-
export const executeProgram = (code, prepared, limits, hooks, globals) => {
|
|
9
|
-
if (code.trim().length === 0) {
|
|
10
|
-
return Effect.succeed({
|
|
11
|
-
ok: false,
|
|
12
|
-
error: { kind: "ParseError", message: "Code cannot be empty." },
|
|
13
|
-
toolCalls: [],
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
// Allocate execution state inside suspension so reused Effects never share it.
|
|
17
|
-
return Effect.suspend(() => {
|
|
18
|
-
const builtins = createBuiltins();
|
|
19
|
-
const tools = ToolRuntime.make(prepared, limits.maxToolCalls, hooks);
|
|
20
|
-
const logs = [];
|
|
21
|
-
const logged = () => (logs.length > 0 ? { logs: [...logs] } : {});
|
|
22
|
-
// Set only after copy-out so timeouts cannot report invalid values as completed.
|
|
23
|
-
let returned;
|
|
24
|
-
let runner;
|
|
25
|
-
const base = Effect.gen(function* () {
|
|
26
|
-
const parsed = parseProgram(code);
|
|
27
|
-
if (!parsed.ok)
|
|
28
|
-
return { ok: false, error: parsed.error, toolCalls: [] };
|
|
29
|
-
const services = yield* Effect.context();
|
|
30
|
-
const ctx = new Interpreter({ tools, services, builtins, logs, globals });
|
|
31
|
-
runner = ctx.runner;
|
|
32
|
-
const outcome = yield* runner.run(ctx.main(parsed.program));
|
|
33
|
-
if (!outcome.ok) {
|
|
34
|
-
return { ok: false, error: normalizeError(outcome.error), ...logged(), toolCalls: tools.calls };
|
|
35
|
-
}
|
|
36
|
-
const result = outcome.value ?? null;
|
|
37
|
-
returned = { value: result, runner };
|
|
38
|
-
// Host work the program left running is interrupted, not awaited.
|
|
39
|
-
yield* runner.interrupt();
|
|
40
|
-
const warnings = runner.diagnostics();
|
|
41
|
-
return {
|
|
42
|
-
ok: true,
|
|
43
|
-
value: result,
|
|
44
|
-
...(warnings.length > 0 ? { warnings } : {}),
|
|
45
|
-
...logged(),
|
|
46
|
-
toolCalls: tools.calls,
|
|
47
|
-
};
|
|
48
|
-
}).pipe(Effect.ensuring(Effect.suspend(() => runner?.interrupt() ?? Effect.void)));
|
|
49
|
-
const timeoutMs = limits.timeoutMs;
|
|
50
|
-
const operation = timeoutMs === undefined
|
|
51
|
-
? base
|
|
52
|
-
: base.pipe(Effect.timeoutOrElse({
|
|
53
|
-
duration: timeoutMs,
|
|
54
|
-
orElse: () => Effect.sync(() => {
|
|
55
|
-
if (returned === undefined) {
|
|
56
|
-
return {
|
|
57
|
-
ok: false,
|
|
58
|
-
error: { kind: "TimeoutExceeded", message: `Execution timed out after ${timeoutMs}ms.` },
|
|
59
|
-
...logged(),
|
|
60
|
-
toolCalls: tools.calls,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
// Keep the timeout warning first so truncation preserves it.
|
|
64
|
-
return {
|
|
65
|
-
ok: true,
|
|
66
|
-
value: returned.value,
|
|
67
|
-
warnings: [
|
|
68
|
-
{
|
|
69
|
-
kind: "TimeoutExceeded",
|
|
70
|
-
message: `The program returned, but background work was still running at the ${timeoutMs}ms timeout and was interrupted. Await all started promises.`,
|
|
71
|
-
},
|
|
72
|
-
...returned.runner.diagnostics(),
|
|
73
|
-
],
|
|
74
|
-
...logged(),
|
|
75
|
-
toolCalls: tools.calls,
|
|
76
|
-
};
|
|
77
|
-
}),
|
|
78
|
-
}));
|
|
79
|
-
return operation.pipe(Effect.catchCause((cause) => Cause.hasInterruptsOnly(cause)
|
|
80
|
-
? Effect.interrupt
|
|
81
|
-
: Effect.succeed({
|
|
82
|
-
ok: false,
|
|
83
|
-
error: normalizeError(Cause.squash(cause)),
|
|
84
|
-
...logged(),
|
|
85
|
-
toolCalls: tools.calls,
|
|
86
|
-
})), Effect.map((result) => limits.maxOutputBytes === undefined ? result : boundOutput(result, limits.maxOutputBytes)));
|
|
87
|
-
});
|
|
88
|
-
};
|
|
89
|
-
export const parseProgram = (code) => {
|
|
90
|
-
try {
|
|
91
|
-
const program = parse(code, {
|
|
92
|
-
ecmaVersion: "latest",
|
|
93
|
-
sourceType: "script",
|
|
94
|
-
allowReturnOutsideFunction: true,
|
|
95
|
-
allowAwaitOutsideFunction: true,
|
|
96
|
-
// Imports bind globals by module name; scripts keep sloppy-mode semantics, which module source would make strict.
|
|
97
|
-
allowImportExportEverywhere: true,
|
|
98
|
-
locations: true,
|
|
99
|
-
});
|
|
100
|
-
const exported = program.body.find((statement) => statement.type.startsWith("Export"));
|
|
101
|
-
if (exported) {
|
|
102
|
-
const location = sourceLocation(exported);
|
|
103
|
-
return {
|
|
104
|
-
ok: false,
|
|
105
|
-
error: {
|
|
106
|
-
kind: "ParseError",
|
|
107
|
-
message: `SyntaxError: Exports aren't available. Return the result instead.${formatLocation(location)}`,
|
|
108
|
-
location,
|
|
109
|
-
},
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
return { ok: true, program };
|
|
113
|
-
}
|
|
114
|
-
catch (error) {
|
|
115
|
-
// Only acorn's own syntax errors become a located ParseError; anything else keeps the generic failure path.
|
|
116
|
-
const acorn = error instanceof SyntaxError ? decodeAcornError(error) : Option.none();
|
|
117
|
-
if (Option.isNone(acorn))
|
|
118
|
-
throw error;
|
|
119
|
-
return { ok: false, error: parseDiagnostic(acorn.value) };
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
// Acorn appends ` (line:column)` to its messages; its lines are 1-based and its columns 0-based.
|
|
123
|
-
const AcornError = Schema.Struct({
|
|
124
|
-
message: Schema.String,
|
|
125
|
-
loc: Schema.Struct({ line: Schema.Number, column: Schema.Number }),
|
|
126
|
-
});
|
|
127
|
-
const decodeAcornError = Schema.decodeUnknownOption(AcornError);
|
|
128
|
-
const parseDiagnostic = (error) => {
|
|
129
|
-
const location = { line: error.loc.line, column: error.loc.column + 1 };
|
|
130
|
-
const reason = error.message.replace(/ \(\d+:\d+\)$/, "");
|
|
131
|
-
const text = `${reason.endsWith(".") ? reason : `${reason}.`} Code must be plain JavaScript written as a program body.`;
|
|
132
|
-
return { kind: "ParseError", message: `SyntaxError: ${text}${formatLocation(location)}`, location };
|
|
133
|
-
};
|
|
134
|
-
const utf8ByteLength = (value) => new TextEncoder().encode(value).byteLength;
|
|
135
|
-
// Drop a replacement character produced by truncating inside a UTF-8 sequence.
|
|
136
|
-
const utf8Truncate = (value, maxBytes) => {
|
|
137
|
-
const bytes = new TextEncoder().encode(value);
|
|
138
|
-
if (bytes.byteLength <= maxBytes)
|
|
139
|
-
return value;
|
|
140
|
-
const text = new TextDecoder("utf-8").decode(bytes.slice(0, Math.max(0, maxBytes)));
|
|
141
|
-
return text.endsWith("\uFFFD") ? text.slice(0, -1) : text;
|
|
142
|
-
};
|
|
143
|
-
// Warnings have a separate budget so result data cannot starve diagnostics.
|
|
144
|
-
const boundOutput = (result, maxOutputBytes) => {
|
|
145
|
-
let truncated = false;
|
|
146
|
-
let value = null;
|
|
147
|
-
let valueBytes = 0;
|
|
148
|
-
if (result.ok) {
|
|
149
|
-
const serialized = JSON.stringify(result.value) ?? "null";
|
|
150
|
-
const bytes = utf8ByteLength(serialized);
|
|
151
|
-
if (bytes > maxOutputBytes) {
|
|
152
|
-
truncated = true;
|
|
153
|
-
value = `${utf8Truncate(serialized, maxOutputBytes)} [result truncated: ${bytes} bytes exceeds the ${maxOutputBytes}-byte output limit; return a smaller value]`;
|
|
154
|
-
valueBytes = maxOutputBytes;
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
value = result.value;
|
|
158
|
-
valueBytes = bytes;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
const warnings = result.ok ? (result.warnings ?? []) : [];
|
|
162
|
-
const keptWarnings = [];
|
|
163
|
-
let warningBytes = 0;
|
|
164
|
-
for (const warning of warnings) {
|
|
165
|
-
const bytes = utf8ByteLength(JSON.stringify(warning)) + 1;
|
|
166
|
-
if (warningBytes + bytes > maxOutputBytes)
|
|
167
|
-
break;
|
|
168
|
-
warningBytes += bytes;
|
|
169
|
-
keptWarnings.push(warning);
|
|
170
|
-
}
|
|
171
|
-
if (keptWarnings.length < warnings.length) {
|
|
172
|
-
truncated = true;
|
|
173
|
-
keptWarnings.push({
|
|
174
|
-
kind: "Truncated",
|
|
175
|
-
message: `${warnings.length - keptWarnings.length} additional warnings omitted by the output limit.`,
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
const logs = result.logs ?? [];
|
|
179
|
-
const kept = [];
|
|
180
|
-
const logBudget = Math.max(0, maxOutputBytes - valueBytes);
|
|
181
|
-
let logBytes = 0;
|
|
182
|
-
for (const line of logs) {
|
|
183
|
-
const lineBytes = utf8ByteLength(line) + 1;
|
|
184
|
-
if (logBytes + lineBytes > logBudget)
|
|
185
|
-
break;
|
|
186
|
-
logBytes += lineBytes;
|
|
187
|
-
kept.push(line);
|
|
188
|
-
}
|
|
189
|
-
if (kept.length < logs.length) {
|
|
190
|
-
truncated = true;
|
|
191
|
-
kept.push(`[logs truncated: showing ${kept.length} of ${logs.length} lines]`);
|
|
192
|
-
}
|
|
193
|
-
if (!truncated)
|
|
194
|
-
return result;
|
|
195
|
-
const warningsPart = keptWarnings.length > 0 ? { warnings: keptWarnings } : {};
|
|
196
|
-
const logsPart = kept.length > 0 ? { logs: kept } : {};
|
|
197
|
-
return result.ok
|
|
198
|
-
? {
|
|
199
|
-
ok: true,
|
|
200
|
-
value,
|
|
201
|
-
...warningsPart,
|
|
202
|
-
...logsPart,
|
|
203
|
-
truncated: true,
|
|
204
|
-
toolCalls: result.toolCalls,
|
|
205
|
-
}
|
|
206
|
-
: { ok: false, error: result.error, ...logsPart, truncated: true, toolCalls: result.toolCalls };
|
|
207
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { type Extension } from "../extension.js";
|
|
2
|
-
import type { Interpreter } from "./interpreter.js";
|
|
3
|
-
import { type Value } from "./objects.js";
|
|
4
|
-
/**
|
|
5
|
-
* The global bindings of one run's extensions. Everything crossing the boundary is converted: plain data and built-in
|
|
6
|
-
* wrappers are copied, a host function becomes a program function whose calls cross the same way, and a host Promise
|
|
7
|
-
* or Effect becomes a program promise.
|
|
8
|
-
*/
|
|
9
|
-
export declare const extensionGlobals: <R>(ctx: Interpreter<R>, extensions: ReadonlyArray<Extension>) => ReadonlyArray<readonly [string, Value]>;
|