@opencode/codemode 0.0.0-beta-19507 → 0.0.0-dev-19274

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 (65) hide show
  1. package/README.md +0 -6
  2. package/dist/codemode.d.ts +12 -9
  3. package/dist/codemode.js +9 -5
  4. package/dist/index.d.ts +0 -1
  5. package/dist/index.js +0 -1
  6. package/dist/interpreter/errors.d.ts +6 -4
  7. package/dist/interpreter/errors.js +10 -25
  8. package/dist/interpreter/execute.d.ts +3 -4
  9. package/dist/interpreter/execute.js +18 -11
  10. package/dist/interpreter/iterator.d.ts +13 -0
  11. package/dist/interpreter/iterator.js +4 -0
  12. package/dist/interpreter/methods.d.ts +16 -3
  13. package/dist/interpreter/methods.js +290 -101
  14. package/dist/interpreter/model.d.ts +79 -10
  15. package/dist/interpreter/model.js +102 -2
  16. package/dist/interpreter/promises.d.ts +13 -15
  17. package/dist/interpreter/promises.js +52 -70
  18. package/dist/interpreter/references.d.ts +0 -1
  19. package/dist/interpreter/references.js +77 -57
  20. package/dist/interpreter/runtime.d.ts +95 -16
  21. package/dist/interpreter/runtime.js +960 -549
  22. package/dist/openapi/spec.js +6 -3
  23. package/dist/stdlib/collections.d.ts +1 -6
  24. package/dist/stdlib/collections.js +1 -117
  25. package/dist/stdlib/console.d.ts +2 -3
  26. package/dist/stdlib/console.js +28 -39
  27. package/dist/stdlib/date.d.ts +4 -5
  28. package/dist/stdlib/date.js +12 -34
  29. package/dist/stdlib/json.d.ts +6 -3
  30. package/dist/stdlib/json.js +63 -40
  31. package/dist/stdlib/math.d.ts +7 -3
  32. package/dist/stdlib/math.js +153 -85
  33. package/dist/stdlib/number.d.ts +4 -2
  34. package/dist/stdlib/number.js +37 -30
  35. package/dist/stdlib/object.d.ts +6 -6
  36. package/dist/stdlib/object.js +87 -84
  37. package/dist/stdlib/promise.d.ts +2 -0
  38. package/dist/stdlib/promise.js +1 -0
  39. package/dist/stdlib/regexp.d.ts +7 -6
  40. package/dist/stdlib/regexp.js +34 -48
  41. package/dist/stdlib/string.d.ts +3 -1
  42. package/dist/stdlib/string.js +17 -20
  43. package/dist/stdlib/url.d.ts +6 -10
  44. package/dist/stdlib/url.js +25 -102
  45. package/dist/stdlib/value.d.ts +7 -8
  46. package/dist/stdlib/value.js +56 -56
  47. package/dist/tool-runtime.d.ts +15 -16
  48. package/dist/tool-runtime.js +150 -13
  49. package/dist/values.d.ts +16 -22
  50. package/dist/values.js +17 -23
  51. package/package.json +1 -1
  52. package/dist/data.d.ts +0 -25
  53. package/dist/data.js +0 -153
  54. package/dist/interpreter/globals.d.ts +0 -13
  55. package/dist/interpreter/globals.js +0 -63
  56. package/dist/interpreter/host.d.ts +0 -41
  57. package/dist/interpreter/host.js +0 -44
  58. package/dist/interpreter/objects.d.ts +0 -37
  59. package/dist/interpreter/objects.js +0 -151
  60. package/dist/interpreter/runner.d.ts +0 -24
  61. package/dist/interpreter/runner.js +0 -45
  62. package/dist/stdlib/array.d.ts +0 -3
  63. package/dist/stdlib/array.js +0 -68
  64. package/dist/stdlib/web.d.ts +0 -4
  65. package/dist/stdlib/web.js +0 -20
@@ -1,27 +1,37 @@
1
+ import { AsyncIteratorSymbol, CodeModeFunction, CodeModeGenerator, CoercionFunction, ErrorConstructorReference, GlobalMethodReference, GlobalNamespace, GeneratorMethodReference, InterpreterRuntimeError, IntrinsicReference, IteratorSymbol, JsonMethodReference, PromiseCapabilityFunction, PromiseInstanceMethodReference, PromiseMethodReference, PromiseNamespace, SearchFunction, SymbolNamespace, UriFunction, } from "./model.js";
1
2
  import { ToolReference } from "../tool-runtime.js";
2
- import { Values } from "../values.js";
3
- import { HostFunction, HostNamespace } from "./host.js";
4
- import { CodeModeGenerator, GeneratorMethodReference, InterpreterRuntimeError, IntrinsicReference, PromiseInstanceMethodReference, } from "./model.js";
5
- import { getOwn, ownKeys, ProgramArray, ProgramFunction, ProgramObject } from "./objects.js";
6
- export const isRuntimeReference = (value) => value instanceof HostFunction ||
7
- value instanceof HostNamespace ||
8
- value instanceof ProgramFunction ||
3
+ import { isCodeModeValue, CodeModePromise } from "../values.js";
4
+ export const isRuntimeReference = (value) => value instanceof CodeModeFunction ||
9
5
  value instanceof CodeModeGenerator ||
10
6
  value instanceof GeneratorMethodReference ||
11
7
  value instanceof ToolReference ||
12
8
  value instanceof IntrinsicReference ||
9
+ value instanceof GlobalNamespace ||
10
+ value instanceof GlobalMethodReference ||
11
+ value instanceof JsonMethodReference ||
12
+ value instanceof PromiseNamespace ||
13
+ value instanceof PromiseMethodReference ||
13
14
  value instanceof PromiseInstanceMethodReference ||
14
- value instanceof Values.Promise ||
15
- Values.isValue(value);
15
+ value instanceof CodeModePromise ||
16
+ value instanceof CoercionFunction ||
17
+ value instanceof UriFunction ||
18
+ value instanceof SearchFunction ||
19
+ value instanceof PromiseCapabilityFunction ||
20
+ value instanceof ErrorConstructorReference ||
21
+ value instanceof SymbolNamespace ||
22
+ isCodeModeValue(value);
16
23
  function* childValues(value) {
17
- if (!(value instanceof ProgramObject))
18
- return;
19
- for (const key of ownKeys(value))
20
- yield getOwn(value, key);
24
+ for (const key of Reflect.ownKeys(value)) {
25
+ if (!Object.prototype.propertyIsEnumerable.call(value, key))
26
+ continue;
27
+ if (typeof key === "symbol" && key !== AsyncIteratorSymbol && key !== IteratorSymbol)
28
+ continue;
29
+ yield Reflect.get(value, key);
30
+ }
21
31
  }
22
- // Depth-first search over a value tree. `match` stops the walk; `skip` prunes a subtree without matching it.
23
- const find = (value, match, skip, seen) => {
32
+ export const containsRuntimeReference = (value) => {
24
33
  const pending = [[value].values()];
34
+ const seen = new Set();
25
35
  while (pending.length > 0) {
26
36
  const next = pending.at(-1).next();
27
37
  if (next.done) {
@@ -29,65 +39,75 @@ const find = (value, match, skip, seen) => {
29
39
  continue;
30
40
  }
31
41
  const current = next.value;
32
- if (match(current))
42
+ if (isRuntimeReference(current))
33
43
  return true;
34
- if (current === null || typeof current !== "object" || skip(current) || seen.has(current))
44
+ if (current === null || typeof current !== "object" || seen.has(current))
35
45
  continue;
36
46
  seen.add(current);
37
47
  pending.push(childValues(current));
38
48
  }
39
49
  return false;
40
50
  };
41
- const never = () => false;
42
- export const containsRuntimeReference = (value) => find(value, isRuntimeReference, never, new Set());
43
51
  // CodeMode values are data here, not opaque interpreter references.
44
- export const containsOpaqueReference = (value) => find(value, (current) => !Values.isValue(current) && isRuntimeReference(current), Values.isValue, new Set());
52
+ export const containsOpaqueReference = (value) => {
53
+ const pending = [[value].values()];
54
+ const seen = new Set();
55
+ while (pending.length > 0) {
56
+ const next = pending.at(-1).next();
57
+ if (next.done) {
58
+ pending.pop();
59
+ continue;
60
+ }
61
+ const current = next.value;
62
+ if (isCodeModeValue(current))
63
+ continue;
64
+ if (isRuntimeReference(current))
65
+ return true;
66
+ if (current === null || typeof current !== "object" || seen.has(current))
67
+ continue;
68
+ seen.add(current);
69
+ pending.push(childValues(current));
70
+ }
71
+ return false;
72
+ };
45
73
  // Reject cycles before mutation so later boundary walks remain safe.
46
74
  export const rejectCircularInsertion = (container, value, label, node, seen = new Set()) => {
47
- if (find(value, (current) => current === container, isRuntimeReference, seen)) {
48
- throw new InterpreterRuntimeError(`${label} contains a circular value.`, node, "InvalidDataValue");
75
+ const pending = [[value].values()];
76
+ while (pending.length > 0) {
77
+ const next = pending.at(-1).next();
78
+ if (next.done) {
79
+ pending.pop();
80
+ continue;
81
+ }
82
+ const current = next.value;
83
+ if (current === container)
84
+ throw new InterpreterRuntimeError(`${label} contains a circular value.`, node, "InvalidDataValue");
85
+ if (current === null || typeof current !== "object" || isRuntimeReference(current) || seen.has(current))
86
+ continue;
87
+ seen.add(current);
88
+ pending.push(childValues(current));
49
89
  }
50
90
  };
51
- export const describeValue = (value) => {
52
- if (value === null)
53
- return "null";
54
- if (value instanceof ProgramArray)
55
- return "an array";
56
- if (value instanceof Values.Promise)
57
- return "an un-awaited Promise";
58
- if (value instanceof ToolReference)
59
- return "a tool reference";
60
- if (value instanceof Values.Date)
61
- return "a Date";
62
- if (value instanceof Values.RegExp)
63
- return "a RegExp";
64
- if (value instanceof Values.Map)
65
- return "a Map";
66
- if (value instanceof Values.Set)
67
- return "a Set";
68
- if (value instanceof Values.URL)
69
- return "a URL";
70
- if (value instanceof Values.URLSearchParams)
71
- return "a URLSearchParams";
72
- if (value instanceof CodeModeGenerator)
73
- return "a generator";
74
- if (isRuntimeReference(value))
75
- return "a function";
76
- if (typeof value === "object")
77
- return "a data object";
78
- return `a ${typeof value}`;
79
- };
80
91
  export const typeofValue = (value) => {
81
- if (value instanceof HostFunction ||
82
- value instanceof ProgramFunction ||
92
+ if (value instanceof CodeModeFunction ||
83
93
  value instanceof GeneratorMethodReference ||
94
+ value instanceof CoercionFunction ||
84
95
  value instanceof IntrinsicReference ||
85
- value instanceof PromiseInstanceMethodReference) {
96
+ value instanceof GlobalMethodReference ||
97
+ value instanceof JsonMethodReference ||
98
+ value instanceof PromiseMethodReference ||
99
+ value instanceof PromiseInstanceMethodReference ||
100
+ value instanceof PromiseNamespace ||
101
+ value instanceof PromiseCapabilityFunction ||
102
+ value instanceof ErrorConstructorReference ||
103
+ value instanceof SymbolNamespace)
104
+ return "function";
105
+ if (value instanceof UriFunction || value instanceof SearchFunction)
86
106
  return "function";
87
- }
88
- if (value instanceof HostNamespace)
89
- return "object";
90
107
  if (value instanceof ToolReference)
91
108
  return value.path.length > 0 ? "function" : "object";
109
+ if (value instanceof GlobalNamespace) {
110
+ return value.name === "Math" || value.name === "JSON" || value.name === "console" ? "object" : "function";
111
+ }
92
112
  return typeof value;
93
113
  };
@@ -1,19 +1,98 @@
1
- import type { Program } from "acorn";
2
1
  import { Effect } from "effect";
3
- import { type Host } from "./globals.js";
4
- import { type Runner } from "./runner.js";
2
+ import { type ProgramNode } from "./model.js";
5
3
  import { PromiseRuntime } from "./promises.js";
6
- /** One program execution: the tool bridge, promise scheduler, captured logs, and the global scope built once. */
7
- export declare class Runtime<R> {
8
- readonly executeTool: (path: ReadonlyArray<string>, args: Array<unknown>) => Effect.Effect<unknown, unknown, R>;
9
- readonly search: (args: Array<unknown>) => Effect.Effect<unknown, unknown, R>;
10
- readonly toolKeys: (path: ReadonlyArray<string>) => ReadonlyArray<string>;
11
- readonly promises: PromiseRuntime<R>;
12
- readonly logs: Array<string>;
13
- readonly runner: Runner<R>;
14
- /** Built-in globals by name, unaffected by program shadowing. */
15
- readonly builtins: ReadonlyMap<string, unknown>;
16
- private readonly root;
17
- constructor(executeTool: (path: ReadonlyArray<string>, args: Array<unknown>) => Effect.Effect<unknown, unknown, R>, search: (args: Array<unknown>) => Effect.Effect<unknown, unknown, R>, toolKeys: (path: ReadonlyArray<string>) => ReadonlyArray<string>, promises: PromiseRuntime<R>, logs?: Array<string>, extraGlobals?: (host: Host<R>) => ReadonlyArray<readonly [string, unknown]>);
18
- run(program: Program): Effect.Effect<unknown, unknown, R>;
4
+ export declare class Interpreter<R> {
5
+ private scopes;
6
+ private readonly executeTool;
7
+ private readonly invokeSearch;
8
+ private readonly toolKeys;
9
+ private readonly logs;
10
+ private readonly promises;
11
+ private generatorState?;
12
+ private generatorAsync;
13
+ private readonly runner;
14
+ constructor(executeTool: (path: ReadonlyArray<string>, args: Array<unknown>) => Effect.Effect<unknown, unknown, R>, invokeSearch: (args: Array<unknown>) => Effect.Effect<unknown, unknown, R>, toolKeys: (path: ReadonlyArray<string>) => ReadonlyArray<string>, promises: PromiseRuntime<R>, logs?: Array<string>);
15
+ run(program: ProgramNode): Effect.Effect<unknown, unknown, R>;
16
+ private createToolCallPromise;
17
+ private createPromise;
18
+ private settlePromise;
19
+ private evaluateStatement;
20
+ private evaluateBlock;
21
+ private createFunction;
22
+ private hoistFunctions;
23
+ private predeclareLexical;
24
+ private predeclarePattern;
25
+ private evaluateIfStatement;
26
+ private evaluateSwitchStatement;
27
+ private evaluateWhileStatement;
28
+ private evaluateDoWhileStatement;
29
+ private evaluateForStatement;
30
+ private evaluateForOfStatement;
31
+ private awaitValue;
32
+ private awaitAsyncFromSyncValue;
33
+ private syncIterator;
34
+ private customIterator;
35
+ private nextIteratorResult;
36
+ private closeIterator;
37
+ private requireIteratorObject;
38
+ private requireIterator;
39
+ private requireIteratorMethod;
40
+ private enumerableKeys;
41
+ private evaluateForInStatement;
42
+ private evaluateBreakStatement;
43
+ private evaluateContinueStatement;
44
+ private evaluateLabeledStatement;
45
+ private evaluateThrowStatement;
46
+ private evaluateTryStatement;
47
+ private evaluateVariableDeclaration;
48
+ private declarePattern;
49
+ private assignPattern;
50
+ private destructureArrayPattern;
51
+ private destructuringPropertyKey;
52
+ private destructuringPropertyValue;
53
+ private evaluateExpression;
54
+ private evaluateNewExpression;
55
+ private constructArray;
56
+ private constructObject;
57
+ private constructDate;
58
+ private toDatePrimitive;
59
+ private constructRegExp;
60
+ private constructMap;
61
+ private constructSet;
62
+ private constructURL;
63
+ private constructURLSearchParams;
64
+ private readURLSearchParamsPair;
65
+ private evaluateBinaryExpression;
66
+ private applyBinaryOperator;
67
+ private evaluateLogicalExpression;
68
+ private evaluateUnaryExpression;
69
+ private evaluateAssignmentExpression;
70
+ private evaluateLogicalAssignment;
71
+ private evaluateUpdateExpression;
72
+ private evaluateCallExpression;
73
+ private invokeCallable;
74
+ private invokeObjectMethodOnTools;
75
+ private invokeConsole;
76
+ private evaluateCallArguments;
77
+ private invokeFunction;
78
+ private createGenerator;
79
+ private completeGeneratorRequests;
80
+ private takeGeneratorRequest;
81
+ private dequeueGeneratorRequest;
82
+ private evaluateYieldExpression;
83
+ private suspendGenerator;
84
+ private delegateYield;
85
+ private evaluateObjectExpression;
86
+ private evaluateArrayExpression;
87
+ private evaluateTemplateLiteral;
88
+ private evaluateConditionalExpression;
89
+ private applyCompoundAssignment;
90
+ private getMemberReference;
91
+ private readMember;
92
+ private writeMember;
93
+ private evaluateDeleteExpression;
94
+ private modifyMember;
95
+ private readReferenceValue;
96
+ private assignToReference;
97
+ private toPropertyKey;
19
98
  }