@opencode/codemode 0.0.0-beta-19425 → 0.0.0-beta-19507

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.
@@ -1,6 +1,7 @@
1
1
  import { sync } from "../interpreter/host.js";
2
2
  import { InterpreterRuntimeError } from "../interpreter/model.js";
3
3
  import { toProgram } from "../data.js";
4
+ import { get, ProgramArray, ProgramError, set } from "../interpreter/objects.js";
4
5
  import { Values } from "../values.js";
5
6
  export const errorConstructors = new Set([
6
7
  "Error",
@@ -13,16 +14,18 @@ export const errorConstructors = new Set([
13
14
  "AggregateError",
14
15
  ]);
15
16
  export const compoundOperators = new Set(["+=", "-=", "*=", "/=", "%=", "**=", "&=", "|=", "^=", "<<=", ">>=", ">>>="]);
16
- const ErrorBrand = Symbol("codemode.error");
17
17
  export const createErrorValue = (name, message) => {
18
- const value = Object.assign(Object.create(null), { name, message });
19
- Object.defineProperty(value, ErrorBrand, { value: name });
18
+ const value = new ProgramError(name);
19
+ set(value, "name", name);
20
+ set(value, "message", message);
20
21
  return value;
21
22
  };
22
- export const createAggregateErrorValue = (errors, message) => Object.assign(createErrorValue("AggregateError", message), { errors });
23
- export const errorBrandName = (value) => value !== null && typeof value === "object"
24
- ? value[ErrorBrand]
25
- : undefined;
23
+ export const createAggregateErrorValue = (errors, message) => {
24
+ const value = createErrorValue("AggregateError", message);
25
+ set(value, "errors", new ProgramArray(errors));
26
+ return value;
27
+ };
28
+ export const errorBrandName = (value) => value instanceof ProgramError ? value.errorName : undefined;
26
29
  export const coerceToString = (value) => {
27
30
  if (value === null)
28
31
  return "null";
@@ -40,22 +43,23 @@ export const coerceToString = (value) => {
40
43
  return value.url.href;
41
44
  if (value instanceof Values.URLSearchParams)
42
45
  return value.params.toString();
43
- if (errorBrandName(value) !== undefined) {
46
+ if (value instanceof ProgramError) {
44
47
  // Match Error.prototype.toString: "name: message", or just one when the other is empty.
45
- const error = value;
46
- const name = typeof error.name === "string" ? error.name : "Error";
47
- const message = typeof error.message === "string" ? error.message : "";
48
- if (message === "")
49
- return name;
50
- if (name === "")
51
- return message;
52
- return `${name}: ${message}`;
48
+ const name = get(value, "name");
49
+ const message = get(value, "message");
50
+ const shownName = typeof name === "string" ? name : "Error";
51
+ const shownMessage = typeof message === "string" ? message : "";
52
+ if (shownMessage === "")
53
+ return shownName;
54
+ if (shownName === "")
55
+ return shownMessage;
56
+ return `${shownName}: ${shownMessage}`;
53
57
  }
54
- if (typeof value === "object") {
55
- return Array.isArray(value)
56
- ? value.map((item) => (item === null || item === undefined ? "" : coerceToString(item))).join(",")
57
- : "[object Object]";
58
+ if (value instanceof ProgramArray) {
59
+ return value.items.map((item) => (item === null || item === undefined ? "" : coerceToString(item))).join(",");
58
60
  }
61
+ if (typeof value === "object")
62
+ return "[object Object]";
59
63
  return String(value);
60
64
  };
61
65
  export const coerceToNumber = (value) => {
@@ -63,9 +67,7 @@ export const coerceToNumber = (value) => {
63
67
  return value.time;
64
68
  if (Values.isValue(value))
65
69
  return Number.NaN;
66
- // Arrays coerce through our own string coercion: host Number(array) joins with host
67
- // ToPrimitive, which throws on the null-prototype objects the interpreter produces.
68
- if (Array.isArray(value))
70
+ if (value instanceof ProgramArray)
69
71
  return Number(coerceToString(value));
70
72
  return value !== null && typeof value === "object" ? Number.NaN : Number(value);
71
73
  };
@@ -79,9 +81,6 @@ const coerce = (name, args, node) => {
79
81
  return "";
80
82
  }
81
83
  const raw = args[0];
82
- // Error values are plain SafeObjects; the toProgram path below would strip their brand.
83
- if (name === "String" && errorBrandName(raw) !== undefined)
84
- return coerceToString(raw);
85
84
  if (Values.isValue(raw)) {
86
85
  if (name === "Boolean")
87
86
  return true;
@@ -0,0 +1,4 @@
1
+ import { HostNamespace } from "../interpreter/host.js";
2
+ export declare const atobGlobal: import("../interpreter/host.js").HostFunction<never>;
3
+ export declare const btoaGlobal: import("../interpreter/host.js").HostFunction<never>;
4
+ export declare const cryptoGlobal: HostNamespace;
@@ -0,0 +1,20 @@
1
+ import { HostNamespace, sync } from "../interpreter/host.js";
2
+ import { InterpreterRuntimeError } from "../interpreter/model.js";
3
+ import { coerceToString } from "./value.js";
4
+ // WebIDL DOMString conversion: a missing argument is a TypeError, anything else stringifies.
5
+ const base64 = (name) => sync(name, (args, node) => {
6
+ if (args.length === 0)
7
+ throw new InterpreterRuntimeError(`${name} requires 1 argument (a string)`, node).as("TypeError");
8
+ const input = coerceToString(args[0]);
9
+ try {
10
+ return name === "atob" ? atob(input) : btoa(input);
11
+ }
12
+ catch {
13
+ throw new InterpreterRuntimeError("The string contains invalid characters.", node).as("InvalidCharacterError");
14
+ }
15
+ });
16
+ export const atobGlobal = base64("atob");
17
+ export const btoaGlobal = base64("btoa");
18
+ export const cryptoGlobal = new HostNamespace("crypto", {
19
+ randomUUID: sync("crypto.randomUUID", () => crypto.randomUUID()),
20
+ });
@@ -2,7 +2,7 @@ import { Effect } from "effect";
2
2
  import { type Namespace } from "./namespace.js";
3
3
  import { type Tool } from "./tool.js";
4
4
  import type { Tools } from "./tools.js";
5
- export declare const compareText: (left: string, right: string) => 0 | 1 | -1;
5
+ export declare const compareText: (left: string, right: string) => 1 | -1 | 0;
6
6
  export type Services<T> = ServicesOf<T, []>;
7
7
  type ServicesOf<T, Depth extends ReadonlyArray<unknown>> = Depth["length"] extends 8 ? never : T extends {
8
8
  readonly _tag: "CodeModeTool";
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": "0.0.0-beta-19425",
4
+ "version": "0.0.0-beta-19507",
5
5
  "description": "Effect-native confined code execution over schema-described tools",
6
6
  "type": "module",
7
7
  "license": "MIT",