@opencode/codemode 0.0.0-reserved → 2.0.1

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 (86) hide show
  1. package/README.md +184 -2
  2. package/dist/codemode.d.ts +145 -0
  3. package/dist/codemode.js +66 -0
  4. package/dist/data.d.ts +25 -0
  5. package/dist/data.js +158 -0
  6. package/dist/index.d.ts +7 -0
  7. package/dist/index.js +7 -0
  8. package/dist/interpreter/errors.d.ts +10 -0
  9. package/dist/interpreter/errors.js +112 -0
  10. package/dist/interpreter/execute.d.ts +5 -0
  11. package/dist/interpreter/execute.js +171 -0
  12. package/dist/interpreter/globals.d.ts +13 -0
  13. package/dist/interpreter/globals.js +64 -0
  14. package/dist/interpreter/host.d.ts +41 -0
  15. package/dist/interpreter/host.js +44 -0
  16. package/dist/interpreter/intrinsics.d.ts +10 -0
  17. package/dist/interpreter/intrinsics.js +41 -0
  18. package/dist/interpreter/methods.d.ts +4 -0
  19. package/dist/interpreter/methods.js +837 -0
  20. package/dist/interpreter/model.d.ts +89 -0
  21. package/dist/interpreter/model.js +90 -0
  22. package/dist/interpreter/objects.d.ts +37 -0
  23. package/dist/interpreter/objects.js +154 -0
  24. package/dist/interpreter/promises.d.ts +31 -0
  25. package/dist/interpreter/promises.js +270 -0
  26. package/dist/interpreter/references.d.ts +7 -0
  27. package/dist/interpreter/references.js +93 -0
  28. package/dist/interpreter/runner.d.ts +26 -0
  29. package/dist/interpreter/runner.js +45 -0
  30. package/dist/interpreter/runtime.d.ts +19 -0
  31. package/dist/interpreter/runtime.js +1942 -0
  32. package/dist/interpreter/scope.d.ts +15 -0
  33. package/dist/interpreter/scope.js +79 -0
  34. package/dist/interpreter/transpile.node.d.ts +5 -0
  35. package/dist/interpreter/transpile.node.js +19 -0
  36. package/dist/interpreter/transpile.workerd.d.ts +5 -0
  37. package/dist/interpreter/transpile.workerd.js +6 -0
  38. package/dist/namespace.d.ts +15 -0
  39. package/dist/namespace.js +7 -0
  40. package/dist/openapi/index.d.ts +7 -0
  41. package/dist/openapi/index.js +101 -0
  42. package/dist/openapi/runtime.d.ts +4 -0
  43. package/dist/openapi/runtime.js +283 -0
  44. package/dist/openapi/spec.d.ts +20 -0
  45. package/dist/openapi/spec.js +583 -0
  46. package/dist/openapi/types.d.ts +122 -0
  47. package/dist/openapi/types.js +2 -0
  48. package/dist/stdlib/array.d.ts +3 -0
  49. package/dist/stdlib/array.js +68 -0
  50. package/dist/stdlib/collections.d.ts +9 -0
  51. package/dist/stdlib/collections.js +173 -0
  52. package/dist/stdlib/console.d.ts +3 -0
  53. package/dist/stdlib/console.js +137 -0
  54. package/dist/stdlib/date.d.ts +8 -0
  55. package/dist/stdlib/date.js +208 -0
  56. package/dist/stdlib/json.d.ts +3 -0
  57. package/dist/stdlib/json.js +101 -0
  58. package/dist/stdlib/math.d.ts +8 -0
  59. package/dist/stdlib/math.js +89 -0
  60. package/dist/stdlib/number.d.ts +4 -0
  61. package/dist/stdlib/number.js +69 -0
  62. package/dist/stdlib/object.d.ts +7 -0
  63. package/dist/stdlib/object.js +106 -0
  64. package/dist/stdlib/regexp.d.ts +10 -0
  65. package/dist/stdlib/regexp.js +120 -0
  66. package/dist/stdlib/string.d.ts +2 -0
  67. package/dist/stdlib/string.js +51 -0
  68. package/dist/stdlib/url.d.ts +16 -0
  69. package/dist/stdlib/url.js +161 -0
  70. package/dist/stdlib/value.d.ts +8 -0
  71. package/dist/stdlib/value.js +98 -0
  72. package/dist/stdlib/web.d.ts +4 -0
  73. package/dist/stdlib/web.js +21 -0
  74. package/dist/tool-error.d.ts +11 -0
  75. package/dist/tool-error.js +9 -0
  76. package/dist/tool-runtime.d.ts +69 -0
  77. package/dist/tool-runtime.js +254 -0
  78. package/dist/tool-schema.d.ts +16 -0
  79. package/dist/tool-schema.js +263 -0
  80. package/dist/tool.d.ts +66 -0
  81. package/dist/tool.js +16 -0
  82. package/dist/tools.d.ts +5 -0
  83. package/dist/tools.js +1 -0
  84. package/dist/values.d.ts +37 -0
  85. package/dist/values.js +56 -0
  86. package/package.json +37 -6
@@ -0,0 +1,7 @@
1
+ import { type AstNode } from "./model.js";
2
+ export declare const isRuntimeReference: (value: unknown) => boolean;
3
+ export declare const containsRuntimeReference: (value: unknown) => boolean;
4
+ export declare const containsOpaqueReference: (value: unknown) => boolean;
5
+ export declare const rejectCircularInsertion: (container: object, value: unknown, label: string, node: AstNode, seen?: Set<object>) => void;
6
+ export declare const describeValue: (value: unknown) => string;
7
+ export declare const typeofValue: (value: unknown) => string;
@@ -0,0 +1,93 @@
1
+ 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 ||
9
+ value instanceof CodeModeGenerator ||
10
+ value instanceof GeneratorMethodReference ||
11
+ value instanceof ToolReference ||
12
+ value instanceof IntrinsicReference ||
13
+ value instanceof PromiseInstanceMethodReference ||
14
+ value instanceof Values.Promise ||
15
+ Values.isValue(value);
16
+ function* childValues(value) {
17
+ if (!(value instanceof ProgramObject))
18
+ return;
19
+ for (const key of ownKeys(value))
20
+ yield getOwn(value, key);
21
+ }
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) => {
24
+ const pending = [[value].values()];
25
+ while (pending.length > 0) {
26
+ const next = pending.at(-1).next();
27
+ if (next.done) {
28
+ pending.pop();
29
+ continue;
30
+ }
31
+ const current = next.value;
32
+ if (match(current))
33
+ return true;
34
+ if (current === null || typeof current !== "object" || skip(current) || seen.has(current))
35
+ continue;
36
+ seen.add(current);
37
+ pending.push(childValues(current));
38
+ }
39
+ return false;
40
+ };
41
+ const never = () => false;
42
+ export const containsRuntimeReference = (value) => find(value, isRuntimeReference, never, new Set());
43
+ // 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());
45
+ // Reject cycles before mutation so later boundary walks remain safe.
46
+ 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");
49
+ }
50
+ };
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
+ export const typeofValue = (value) => {
81
+ if (value instanceof HostFunction ||
82
+ value instanceof ProgramFunction ||
83
+ value instanceof GeneratorMethodReference ||
84
+ value instanceof IntrinsicReference ||
85
+ value instanceof PromiseInstanceMethodReference) {
86
+ return "function";
87
+ }
88
+ if (value instanceof HostNamespace)
89
+ return "object";
90
+ if (value instanceof ToolReference)
91
+ return value.path.length > 0 ? "function" : "object";
92
+ return typeof value;
93
+ };
@@ -0,0 +1,26 @@
1
+ import { Effect } from "effect";
2
+ import { Values } from "../values.js";
3
+ import { HostFunction } from "./host.js";
4
+ import { type AstNode, IntrinsicReference } from "./model.js";
5
+ import type { Intrinsics } from "./intrinsics.js";
6
+ import { ProgramFunction } from "./objects.js";
7
+ export type IteratorCursor<R> = {
8
+ readonly next: Effect.Effect<{
9
+ readonly done: boolean;
10
+ readonly value: unknown;
11
+ }, unknown, R>;
12
+ readonly close: Effect.Effect<void, unknown, R>;
13
+ };
14
+ /** Everything a host function needs from the realm: calling back into the program and its intrinsic objects. */
15
+ export type Runner<R> = {
16
+ readonly invokeFunction: (fn: ProgramFunction, args: Array<unknown>) => Effect.Effect<unknown, unknown, R>;
17
+ readonly invokeCallable: (callable: unknown, args: Array<unknown>, node: AstNode) => Effect.Effect<unknown, unknown, R>;
18
+ readonly settlePromise: (promise: Values.Promise) => Effect.Effect<unknown, unknown, never>;
19
+ readonly syncIterator: (value: unknown, node: AstNode) => Effect.Effect<IteratorCursor<R> | undefined, unknown, R>;
20
+ readonly intrinsics: Intrinsics;
21
+ };
22
+ export declare const preserveConsumerError: <A, R>(cursor: IteratorCursor<R>, effect: Effect.Effect<A, unknown, R>) => Effect.Effect<A, unknown, R>;
23
+ export declare const toPrimitive: <R>(runner: Runner<R>, value: unknown, hint: "number" | "string", node: AstNode) => Effect.Effect<unknown, unknown, R>;
24
+ export type SupportedCallback = ProgramFunction | HostFunction<unknown> | IntrinsicReference;
25
+ export declare const isSupportedCallback: (value: unknown) => value is SupportedCallback;
26
+ export declare const applyCollectionCallback: <R>(runner: Runner<R>, callback: unknown, name: string, node: AstNode) => ((args: Array<unknown>) => Effect.Effect<unknown, unknown, R>);
@@ -0,0 +1,45 @@
1
+ import { Effect, Exit } from "effect";
2
+ import { Values } from "../values.js";
3
+ import { coerceToString } from "../stdlib/value.js";
4
+ import { HostFunction } from "./host.js";
5
+ import { InterpreterRuntimeError, IntrinsicReference } from "./model.js";
6
+ import { get, has, ProgramFunction, ProgramObject } from "./objects.js";
7
+ import { typeofValue } from "./references.js";
8
+ export const preserveConsumerError = (cursor, effect) => Effect.flatMap(Effect.exit(effect), (exit) => Exit.isSuccess(exit)
9
+ ? Effect.succeed(exit.value)
10
+ : Effect.andThen(Effect.exit(cursor.close), Effect.failCause(exit.cause)));
11
+ export const toPrimitive = (runner, value, hint, node) => {
12
+ if (value === null || typeof value !== "object")
13
+ return Effect.succeed(value);
14
+ if (Values.isValue(value)) {
15
+ return Effect.succeed(value instanceof Values.Date && hint === "number" ? value.time : coerceToString(value));
16
+ }
17
+ if (!(value instanceof ProgramObject))
18
+ return Effect.succeed(value);
19
+ const order = hint === "number" ? ["valueOf", "toString"] : ["toString", "valueOf"];
20
+ return Effect.gen(function* () {
21
+ for (const method of order) {
22
+ if (method === "toString" && !has(value, "toString"))
23
+ return coerceToString(value);
24
+ const callable = get(value, method);
25
+ if (typeofValue(callable) !== "function")
26
+ continue;
27
+ const result = yield* runner.invokeCallable(callable, [], node);
28
+ if (result === null || (typeof result !== "object" && typeof result !== "function"))
29
+ return result;
30
+ }
31
+ throw new InterpreterRuntimeError("Cannot convert object to primitive value.", node);
32
+ });
33
+ };
34
+ export const isSupportedCallback = (value) => value instanceof ProgramFunction ||
35
+ (value instanceof HostFunction && value.callback) ||
36
+ value instanceof IntrinsicReference;
37
+ export const applyCollectionCallback = (runner, callback, name, node) => {
38
+ if (!isSupportedCallback(callback)) {
39
+ if (typeofValue(callback) === "function") {
40
+ throw new InterpreterRuntimeError(`${name} cannot use this callable as a callback; wrap it in an arrow function, e.g. (value) => tools.ns.tool(value).`, node);
41
+ }
42
+ throw new InterpreterRuntimeError(`${name} expects a function callback.`, node);
43
+ }
44
+ return (callbackArgs) => runner.invokeCallable(callback, callbackArgs, node);
45
+ };
@@ -0,0 +1,19 @@
1
+ import type { Program } from "acorn";
2
+ import { Effect } from "effect";
3
+ import { type Host } from "./globals.js";
4
+ import { type Runner } from "./runner.js";
5
+ 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>;
19
+ }