@opencode/codemode 2.2.0 → 2.2.2

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.
Files changed (56) hide show
  1. package/README.md +33 -4
  2. package/dist/codemode.d.ts +9 -7
  3. package/dist/codemode.js +1 -1
  4. package/dist/data.d.ts +5 -4
  5. package/dist/data.js +7 -4
  6. package/dist/extension.d.ts +17 -4
  7. package/dist/extension.js +8 -0
  8. package/dist/interpreter/callback.d.ts +10 -10
  9. package/dist/interpreter/callback.js +43 -31
  10. package/dist/interpreter/errors.d.ts +1 -1
  11. package/dist/interpreter/errors.js +12 -15
  12. package/dist/interpreter/execute.js +59 -29
  13. package/dist/interpreter/extensions.d.ts +2 -2
  14. package/dist/interpreter/extensions.js +46 -18
  15. package/dist/interpreter/generators.js +162 -19
  16. package/dist/interpreter/globals.js +12 -9
  17. package/dist/interpreter/interpreter.d.ts +20 -13
  18. package/dist/interpreter/interpreter.js +1343 -1149
  19. package/dist/interpreter/intrinsics.js +1 -2
  20. package/dist/interpreter/model.d.ts +4 -7
  21. package/dist/interpreter/model.js +1 -11
  22. package/dist/interpreter/native.d.ts +7 -8
  23. package/dist/interpreter/native.js +4 -10
  24. package/dist/interpreter/objects.d.ts +47 -21
  25. package/dist/interpreter/objects.js +33 -12
  26. package/dist/interpreter/promises.d.ts +15 -53
  27. package/dist/interpreter/promises.js +226 -401
  28. package/dist/interpreter/runner.d.ts +188 -0
  29. package/dist/interpreter/runner.js +492 -0
  30. package/dist/stdlib/array.d.ts +3 -3
  31. package/dist/stdlib/array.js +106 -102
  32. package/dist/stdlib/bytes.d.ts +3 -3
  33. package/dist/stdlib/bytes.js +46 -41
  34. package/dist/stdlib/collections.d.ts +5 -5
  35. package/dist/stdlib/collections.js +160 -165
  36. package/dist/stdlib/date.d.ts +1 -1
  37. package/dist/stdlib/date.js +28 -20
  38. package/dist/stdlib/headers.d.ts +1 -1
  39. package/dist/stdlib/headers.js +14 -19
  40. package/dist/stdlib/iterator.d.ts +1 -1
  41. package/dist/stdlib/iterator.js +140 -122
  42. package/dist/stdlib/json.js +9 -12
  43. package/dist/stdlib/math.js +10 -10
  44. package/dist/stdlib/number.d.ts +2 -2
  45. package/dist/stdlib/object.d.ts +1 -1
  46. package/dist/stdlib/object.js +21 -22
  47. package/dist/stdlib/regexp.d.ts +1 -1
  48. package/dist/stdlib/regexp.js +2 -3
  49. package/dist/stdlib/string.d.ts +1 -1
  50. package/dist/stdlib/string.js +18 -20
  51. package/dist/stdlib/url.d.ts +5 -5
  52. package/dist/stdlib/url.js +32 -33
  53. package/dist/stdlib/value.d.ts +2 -2
  54. package/dist/stdlib/web.d.ts +2 -2
  55. package/dist/tool-schema.js +7 -3
  56. package/package.json +2 -2
@@ -1,4 +1,5 @@
1
1
  import { Effect } from "effect";
2
+ import { isAsync } from "../extension.js";
2
3
  import { hooked } from "../tool-runtime.js";
3
4
  import { createErrorValue, isErrorType } from "./intrinsics.js";
4
5
  import { MAX_VALUE_DEPTH } from "./limits.js";
@@ -9,7 +10,7 @@ import { describeValue, isOpaque } from "./references.js";
9
10
  /**
10
11
  * The global bindings of one run's extensions. Everything crossing the boundary is converted: plain data and
11
12
  * built-in wrappers are copied, a host function becomes a program function whose calls cross the same way, and a
12
- * host Promise becomes a program promise.
13
+ * host Promise or Effect becomes a program promise.
13
14
  */
14
15
  export const extensionGlobals = (ctx, extensions) => {
15
16
  const builtins = ctx.builtins;
@@ -122,28 +123,55 @@ export const extensionGlobals = (ctx, extensions) => {
122
123
  }
123
124
  throw typeError(`${label} produced ${describeHost(value)}, which the program cannot hold.`);
124
125
  };
125
- // A host function as a program function. Arguments cross in; a global's call runs inside the host's extension
126
- // hooks, which see the host's own error on failure; then whatever came back, or was thrown, crosses out so the
127
- // program catches a copy. Functions inside results are part of a value's API and skip the hooks.
128
- const wrap = (value, label, describe) => fn(builtins, value.name, value.length, (_, values) => {
126
+ // A host function as a program function. Arguments cross in; for a global, the `extension.before` hook runs before
127
+ // the host function is called, so a call it denies never reaches the host. The host's result, or what it threw,
128
+ // crosses out so the program catches a copy, and `extension.after` sees how the call ended. Functions inside
129
+ // results are part of a value's API and skip the hooks.
130
+ const wrap = (value, label, describe) => fn(builtins, value.name, value.length, function* (_, values, site) {
129
131
  const args = values.map((item, index) => toHost(item, `Argument ${index + 1} to ${label}`));
132
+ const cross = (reason) => new Throw(fromHost(reason, label));
133
+ const call = describe?.(args);
130
134
  const hooks = ctx.tools.hooks;
131
- const settle = (run) => (describe === undefined
132
- ? run
133
- : hooked(describe(args), hooks["extension.before"], hooks["extension.after"], run)).pipe(Effect.mapError((reason) => new Throw(fromHost(reason, label))), Effect.map((settled) => fromHost(settled, label)));
134
- let result;
135
- try {
136
- result = value.apply(undefined, args);
137
- }
138
- catch (reason) {
139
- return settle(Effect.fail(reason));
140
- }
141
- if (!(result instanceof Promise))
142
- return settle(Effect.succeed(result));
143
- return ctx.pending.createHost(settle(Effect.tryPromise({ try: () => result, catch: (reason) => reason })));
135
+ const before = call && hooks["extension.before"]?.(call).pipe(Effect.mapError(cross));
136
+ const after = call && hooks["extension.after"];
137
+ const settle = (effect) => (after ? hooked(call, undefined, after, effect) : effect).pipe(Effect.mapBoth({ onFailure: cross, onSuccess: (result) => fromHost(result, label) }));
138
+ // A denied call fails the way the global would: an async global rejects its promise, a sync one throws.
139
+ if (isAsync(value))
140
+ return pending(Effect.andThen(before ?? Effect.void, settle(Effect.suspend(() => callHost(value, args).effect))), site);
141
+ if (before)
142
+ yield ctx.runner.blocking(before);
143
+ const host = callHost(value, args);
144
+ if (host.async)
145
+ return pending(settle(host.effect), site);
146
+ return (yield ctx.runner.blocking(settle(host.effect)));
144
147
  });
148
+ // An asynchronous host call: the program holds a promise that settles when the host's work does.
149
+ const pending = (work, site) => {
150
+ const promise = ctx.runner.newPromise();
151
+ const resolving = ctx.runner.resolving(promise, site);
152
+ ctx.runner.host(work, (settled) => (settled.ok ? resolving.resolve(settled.value) : resolving.reject(settled.error)), site);
153
+ return promise;
154
+ };
145
155
  return extensions.flatMap((extension) => Object.entries(extension.globals).map(([name, value]) => [name, wrap(value, name, (args) => ({ extension: extension.name, name, args }))]));
146
156
  };
157
+ /**
158
+ * Calls a host function and describes what the program waits on. A returned Effect or Promise settles later; a
159
+ * returned value or a throw settles now.
160
+ */
161
+ const callHost = (value, args) => {
162
+ try {
163
+ const result = value.apply(undefined, args);
164
+ // Extension.Services adds an Effect global's requirements to R.
165
+ if (Effect.isEffect(result))
166
+ return { async: true, effect: result };
167
+ if (result instanceof Promise)
168
+ return { async: true, effect: Effect.tryPromise({ try: () => result, catch: (reason) => reason }) };
169
+ return { async: false, effect: Effect.succeed(result) };
170
+ }
171
+ catch (reason) {
172
+ return { async: false, effect: Effect.fail(reason) };
173
+ }
174
+ };
147
175
  /**
148
176
  * An error crosses as its name, message, `cause`, and own enumerable fields, such as Node's `code`, `errno`,
149
177
  * `syscall`, and `path`. `stack` stays on its own side, and no field may shadow an Error method. A field that cannot
@@ -1,25 +1,168 @@
1
- import { Effect } from "effect";
2
1
  import { fn, methods, receiver } from "./native.js";
3
- import { AsyncIteratorSymbol, IteratorSymbol } from "./model.js";
4
- import { define, hidden, GeneratorObj } from "./objects.js";
2
+ import { AsyncIteratorSymbol, IteratorSymbol, Throw, typeError } from "./model.js";
3
+ import { define, hidden, GeneratorObj, iteratorResult } from "./objects.js";
4
+ import { abrupt, Enter, normal, } from "./runner.js";
5
5
  /** `next`/`return`/`throw` on the generator prototypes; async generators answer with promises. */
6
6
  export const generatorGlobals = (ctx) => {
7
7
  const builtins = ctx.builtins;
8
- const install = (asynchronous) => {
9
- const proto = asynchronous ? builtins.AsyncGenerator : builtins.Generator;
10
- const label = asynchronous ? "AsyncGenerator" : "Generator";
11
- const request = (kind) => [
12
- kind,
13
- 1,
14
- (thisValue, args) => {
15
- const generator = receiver(GeneratorObj, thisValue, `${label}.prototype.${kind}`);
16
- const requested = generator.request(kind, args[0]);
17
- return generator.asynchronous ? ctx.pending.create(requested) : requested;
18
- },
19
- ];
20
- methods(builtins, proto, [request("next"), request("return"), request("throw")]);
21
- define(asynchronous ? builtins.AsyncIterator : builtins.Iterator, asynchronous ? AsyncIteratorSymbol : IteratorSymbol, fn(builtins, asynchronous ? "[Symbol.asyncIterator]" : "[Symbol.iterator]", 0, (thisValue) => thisValue), hidden);
8
+ const sync = (kind) => [
9
+ kind,
10
+ 1,
11
+ (thisValue, args) => {
12
+ const method = `Generator.prototype.${kind}`;
13
+ const generator = receiver(GeneratorObj, thisValue, method);
14
+ if (generator.asynchronous)
15
+ throw typeError(`${method} called on incompatible receiver an async generator.`);
16
+ return step(ctx, generator, kind, args[0]);
17
+ },
18
+ ];
19
+ const async = (kind) => [
20
+ kind,
21
+ 1,
22
+ (thisValue, args) => request(ctx, thisValue, kind, args[0]),
23
+ ];
24
+ methods(builtins, builtins.Generator, [sync("next"), sync("return"), sync("throw")]);
25
+ methods(builtins, builtins.AsyncGenerator, [async("next"), async("return"), async("throw")]);
26
+ define(builtins.Iterator, IteratorSymbol, fn(builtins, "[Symbol.iterator]", 0, (thisValue) => thisValue), hidden);
27
+ define(builtins.AsyncIterator, AsyncIteratorSymbol, fn(builtins, "[Symbol.asyncIterator]", 0, (thisValue) => thisValue), hidden);
28
+ };
29
+ const result = (ctx, value, done) => iteratorResult(ctx.builtins.Object, value, done);
30
+ // Puts the suspended body back on the stack and continues it with `request`; the boundary answers the caller when
31
+ // the body next yields or ends.
32
+ const resume = (ctx, generator, request) => {
33
+ const started = generator.state !== "suspendedStart";
34
+ generator.state = "executing";
35
+ if (!started) {
36
+ return new Enter([{ body: generator.body, boundary: new GeneratorBoundary(ctx, generator) }], normal(undefined));
37
+ }
38
+ const entries = generator.entries;
39
+ generator.entries = undefined;
40
+ return new Enter(entries, normal(request));
41
+ };
42
+ // A synchronous generator step: the body runs on the caller's stack until it yields, returns, or throws.
43
+ function* step(ctx, generator, kind, value) {
44
+ if (generator.state === "executing")
45
+ throw typeError("Generator is already running.");
46
+ if (generator.state === "suspendedStart" && kind !== "next")
47
+ generator.state = "completed";
48
+ if (generator.state === "completed") {
49
+ if (kind === "throw")
50
+ throw new Throw(value);
51
+ return result(ctx, kind === "return" ? value : undefined, true);
52
+ }
53
+ return (yield resume(ctx, generator, { kind, value }));
54
+ }
55
+ // AsyncGenerator.prototype.next/return/throw: queue the request with its promise, and run the body now if it is
56
+ // waiting for one. A request that arrives while the body runs is answered when the body reaches it.
57
+ function* request(ctx, thisValue, kind, value) {
58
+ const runner = ctx.runner;
59
+ const promise = runner.newPromise();
60
+ const resolving = runner.resolving(promise);
61
+ if (!(thisValue instanceof GeneratorObj) || !thisValue.asynchronous) {
62
+ resolving.reject(typeError(`AsyncGenerator.prototype.${kind} called on incompatible receiver.`));
63
+ return promise;
64
+ }
65
+ const generator = thisValue;
66
+ const state = generator.state;
67
+ if (kind === "next" && state === "completed") {
68
+ resolving.resolve(result(ctx, undefined, true));
69
+ return promise;
70
+ }
71
+ if (kind === "throw") {
72
+ if (state === "suspendedStart")
73
+ generator.state = "completed";
74
+ if (generator.state === "completed") {
75
+ resolving.reject(new Throw(value));
76
+ return promise;
77
+ }
78
+ }
79
+ const queued = { kind, value };
80
+ generator.queue.push({ request: queued, resolving });
81
+ if (kind === "return" && (state === "suspendedStart" || state === "completed")) {
82
+ generator.state = "awaiting-return";
83
+ awaitReturn(ctx, generator);
84
+ return promise;
85
+ }
86
+ if (state === "suspendedStart" || state === "suspendedYield")
87
+ yield resume(ctx, generator, queued);
88
+ return promise;
89
+ }
90
+ // AsyncGeneratorCompleteStep: answer the oldest request.
91
+ const completeStep = (ctx, generator, completion, done) => {
92
+ const next = generator.queue.shift();
93
+ if (completion.ok)
94
+ next.resolving.resolve(result(ctx, completion.value, done));
95
+ else
96
+ next.resolving.reject(completion.error);
97
+ };
98
+ // AsyncGeneratorDrainQueue: once the body has ended, answer the remaining requests in order.
99
+ const drainQueue = (ctx, generator) => {
100
+ generator.state = "draining";
101
+ while (true) {
102
+ const next = generator.queue[0];
103
+ if (next === undefined) {
104
+ generator.state = "completed";
105
+ return;
106
+ }
107
+ if (next.request.kind === "return") {
108
+ generator.state = "awaiting-return";
109
+ return awaitReturn(ctx, generator);
110
+ }
111
+ completeStep(ctx, generator, next.request.kind === "throw" ? abrupt(new Throw(next.request.value)) : normal(undefined), true);
112
+ }
113
+ };
114
+ // AsyncGeneratorAwaitReturn: a `return(value)` on an ended or unstarted generator settles once `value` does.
115
+ const awaitReturn = (ctx, generator) => {
116
+ const runner = ctx.runner;
117
+ const finish = (completion) => {
118
+ completeStep(ctx, generator, completion, true);
119
+ drainQueue(ctx, generator);
22
120
  };
23
- install(false);
24
- install(true);
121
+ try {
122
+ runner.then(runner.promiseResolve(generator.queue[0].request.value), finish);
123
+ }
124
+ catch (error) {
125
+ finish(abrupt(error));
126
+ }
25
127
  };
128
+ /** Where a generator body splits off the stack when it yields, and how its end answers the request that ran it. */
129
+ class GeneratorBoundary {
130
+ ctx;
131
+ target;
132
+ generator = true;
133
+ async;
134
+ constructor(ctx, target) {
135
+ this.ctx = ctx;
136
+ this.target = target;
137
+ this.async = target.asynchronous;
138
+ }
139
+ finish(completion) {
140
+ const generator = this.target;
141
+ if (!generator.asynchronous) {
142
+ generator.state = "completed";
143
+ return completion.ok ? normal(result(this.ctx, completion.value, true)) : completion;
144
+ }
145
+ completeStep(this.ctx, generator, completion, true);
146
+ drainQueue(this.ctx, generator);
147
+ return normal(undefined);
148
+ }
149
+ suspended() {
150
+ return normal(undefined);
151
+ }
152
+ yielded(value, split) {
153
+ const generator = this.target;
154
+ if (!generator.asynchronous) {
155
+ generator.entries = split();
156
+ generator.state = "suspendedYield";
157
+ return normal(result(this.ctx, value, false));
158
+ }
159
+ // AsyncGeneratorYield: answer the oldest request, and keep running if another is already waiting.
160
+ completeStep(this.ctx, generator, normal(value), false);
161
+ const next = generator.queue[0];
162
+ if (next !== undefined)
163
+ return normal(next.request);
164
+ generator.entries = split();
165
+ generator.state = "suspendedYield";
166
+ return normal(undefined);
167
+ }
168
+ }
@@ -1,4 +1,3 @@
1
- import { Effect } from "effect";
2
1
  import { Arr, Callable, coerceToInteger, coerceToString, get, Obj } from "./objects.js";
3
2
  import { arrayGlobal } from "../stdlib/array.js";
4
3
  import { textDecoderGlobal, textEncoderGlobal, uint8ArrayGlobal } from "../stdlib/bytes.js";
@@ -26,13 +25,17 @@ import { generatorGlobals } from "./generators.js";
26
25
  import { promiseGlobal } from "./promises.js";
27
26
  // Function.prototype.constructor exists so `fn.constructor === Function` holds; dynamic code is unsupported.
28
27
  const functionGlobal = (ctx) => {
29
- const reject = () => Effect.sync(() => {
28
+ const reject = () => {
30
29
  throw typeError("The Function constructor is not supported; write the function inline.");
31
- });
30
+ };
32
31
  const target = (thisValue, method) => receiver(Callable, thisValue, `Function.prototype.${method}`);
33
32
  methods(ctx.builtins, ctx.builtins.Function, [
34
- ["call", 1, (thisValue, args) => ctx.call(target(thisValue, "call"), args[0], args.slice(1))],
35
- ["apply", 2, (thisValue, args) => ctx.call(target(thisValue, "apply"), args[0], listFromArrayLike(args[1]))],
33
+ ["call", 1, (thisValue, args, site) => ctx.call(target(thisValue, "call"), args[0], args.slice(1), site)],
34
+ [
35
+ "apply",
36
+ 2,
37
+ (thisValue, args, site) => ctx.call(target(thisValue, "apply"), args[0], listFromArrayLike(args[1]), site),
38
+ ],
36
39
  [
37
40
  "bind",
38
41
  1,
@@ -42,7 +45,7 @@ const functionGlobal = (ctx) => {
42
45
  return native(ctx.builtins, {
43
46
  name: `bound ${coerceToString(get(fn, "name"))}`,
44
47
  length: Math.max(0, fn.length - bound.length),
45
- call: (_, rest) => ctx.call(fn, args[0], [...bound, ...rest]),
48
+ call: (_, rest, site) => ctx.call(fn, args[0], [...bound, ...rest], site),
46
49
  });
47
50
  },
48
51
  ],
@@ -69,9 +72,9 @@ const listFromArrayLike = (value) => {
69
72
  const symbolGlobal = (ctx) => {
70
73
  const symbol = native(ctx.builtins, {
71
74
  name: "Symbol",
72
- call: () => Effect.sync(() => {
75
+ call: () => {
73
76
  throw typeError("Symbol is not callable; only Symbol.asyncIterator and Symbol.iterator are available.");
74
- }),
77
+ },
75
78
  callback: false,
76
79
  });
77
80
  constants(symbol, { asyncIterator: AsyncIteratorSymbol, iterator: IteratorSymbol });
@@ -80,7 +83,7 @@ const symbolGlobal = (ctx) => {
80
83
  // A table rather than a list so the names are known before any runtime exists.
81
84
  const table = {
82
85
  tools: () => new ToolReference([]),
83
- search: (ctx) => native(ctx.builtins, { name: "search", call: (_, args) => ctx.tool(ctx.tools.search, args), callback: false }),
86
+ search: (ctx) => native(ctx.builtins, { name: "search", call: (_, args) => ctx.search(args), callback: false }),
84
87
  undefined: () => undefined,
85
88
  NaN: () => NaN,
86
89
  Infinity: () => Infinity,
@@ -1,14 +1,15 @@
1
1
  import type { Program, TaggedTemplateExpression } from "acorn";
2
- import { Effect } from "effect";
2
+ import type { Context } from "effect";
3
3
  import { type Json } from "../data.js";
4
4
  import { type ToolRuntime } from "../tool-runtime.js";
5
+ import { type AstNode } from "./model.js";
5
6
  import { type Builtins } from "./intrinsics.js";
6
- import { type Cursor, Arr, PromiseObj, type Value } from "./objects.js";
7
- import { Pending } from "./promises.js";
8
- /** One program execution: the tool bridge, promise scheduler, captured logs, and the global scope built once. */
7
+ import { type Cursor, Arr, type Value } from "./objects.js";
8
+ import { type Eval, Runner } from "./runner.js";
9
+ /** One program execution: the tool bridge, job queue, captured logs, and the global scope built once. */
9
10
  export declare class Interpreter<R> {
10
11
  readonly tools: ToolRuntime<R>;
11
- readonly pending: Pending<R>;
12
+ readonly runner: Runner<R>;
12
13
  readonly builtins: Builtins;
13
14
  readonly logs: Array<string>;
14
15
  /** Template objects by site: a tag sees the same `strings` array every time its literal is evaluated, as in JS. */
@@ -16,16 +17,22 @@ export declare class Interpreter<R> {
16
17
  private readonly root;
17
18
  constructor(options: {
18
19
  readonly tools: ToolRuntime<R>;
19
- readonly pending: Pending<R>;
20
+ /** The caller's services, which host work runs with. */
21
+ readonly services: Context.Context<R>;
20
22
  readonly builtins: Builtins;
21
23
  readonly logs?: Array<string>;
22
24
  readonly globals?: (ctx: Interpreter<R>) => ReadonlyArray<readonly [string, Value]>;
23
25
  });
24
- run(program: Program): Effect.Effect<Value, unknown, R>;
25
- call(callable: Value, thisValue: Value, args: Array<Value>): Effect.Effect<Value, unknown, R>;
26
- await(promise: PromiseObj): Effect.Effect<Value, unknown, never>;
27
- iterate(value: Value): Effect.Effect<Cursor<R> | undefined, unknown, R>;
28
- iterateDirect(value: Value): Cursor<R>;
29
- /** Runs one host tool: arguments cross as JSON and the result comes back as program values. */
30
- tool(run: (args: Array<Json | undefined>) => Effect.Effect<Json | undefined, unknown, R>, args: Array<Value>): Effect.Effect<Value, unknown, R>;
26
+ /** The program's body, as the first job: its completion value, copied out for the host. */
27
+ main(program: Program): Eval<Json | undefined>;
28
+ /** Calls into the program; a built-in passes its own call site so failures inside are located there. */
29
+ call(callable: Value, thisValue: Value, args: Array<Value>, site?: AstNode): Eval<Value>;
30
+ /** Await: suspends the innermost async body until `value` settles. */
31
+ awaitValue(value: Value, node?: AstNode): Eval<Value>;
32
+ iterate(value: Value): Eval<Cursor | undefined>;
33
+ iterateDirect(value: Value): Cursor;
34
+ /** Runs one host call to completion before any other job: arguments cross as JSON, the result comes back as values. */
35
+ tool(run: ToolRuntime<R>["search"], args: Array<Value>): Generator<import("./runner.js").Signal, Value, import("effect/Schema").Json | undefined>;
36
+ /** The built-in search: its result is usable directly, and can also be awaited or chained like a Promise. */
37
+ search(args: Array<Value>): Generator<import("./runner.js").Signal, Value, import("effect/Schema").Json | undefined>;
31
38
  }