@opencode/codemode 0.0.0-dev-160 → 0.0.0-dev-170

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.
@@ -9,10 +9,11 @@ import { toPrimitiveString } from "./callback.js";
9
9
  import { formatValue } from "../stdlib/console.js";
10
10
  export const normalizeError = (error) => {
11
11
  if (error instanceof PendingThrow) {
12
+ const location = error.node?.loc ? sourceLocation(error.node) : undefined;
12
13
  return {
13
14
  kind: error.kind,
14
- message: `${error.type}: ${error.message}${formatLocation(error.node)}`,
15
- ...(error.node?.loc ? { location: sourceLocation(error.node) } : {}),
15
+ message: `${error.type}: ${error.message}${formatLocation(location)}`,
16
+ ...(location ? { location } : {}),
16
17
  ...(error.suggestions ? { suggestions: error.suggestions } : {}),
17
18
  };
18
19
  }
@@ -1,9 +1,10 @@
1
1
  import { parse } from "acorn";
2
- import { Cause, Effect } from "effect";
2
+ import { Cause, Effect, Option, Schema } from "effect";
3
3
  import { ToolRuntime } from "../tool-runtime.js";
4
4
  import { normalizeError } from "./errors.js";
5
5
  import { createBuiltins } from "./intrinsics.js";
6
6
  import { Interpreter } from "./interpreter.js";
7
+ import { formatLocation } from "./model.js";
7
8
  export const executeProgram = (code, prepared, limits, hooks, globals) => {
8
9
  if (code.trim().length === 0) {
9
10
  return Effect.succeed({
@@ -22,11 +23,13 @@ export const executeProgram = (code, prepared, limits, hooks, globals) => {
22
23
  let returned;
23
24
  let runner;
24
25
  const base = Effect.gen(function* () {
25
- const program = parseProgram(code);
26
+ const parsed = parseProgram(code);
27
+ if (!parsed.ok)
28
+ return { ok: false, error: parsed.error, toolCalls: [] };
26
29
  const services = yield* Effect.context();
27
30
  const ctx = new Interpreter({ tools, services, builtins, logs, globals });
28
31
  runner = ctx.runner;
29
- const outcome = yield* runner.run(ctx.main(program));
32
+ const outcome = yield* runner.run(ctx.main(parsed.program));
30
33
  if (!outcome.ok) {
31
34
  return { ok: false, error: normalizeError(outcome.error), ...logged(), toolCalls: tools.calls };
32
35
  }
@@ -84,13 +87,37 @@ export const executeProgram = (code, prepared, limits, hooks, globals) => {
84
87
  });
85
88
  };
86
89
  const parseProgram = (code) => {
87
- return parse(code, {
88
- ecmaVersion: "latest",
89
- sourceType: "script",
90
- allowReturnOutsideFunction: true,
91
- allowAwaitOutsideFunction: true,
92
- locations: true,
93
- });
90
+ try {
91
+ const program = parse(code, {
92
+ ecmaVersion: "latest",
93
+ sourceType: "script",
94
+ allowReturnOutsideFunction: true,
95
+ allowAwaitOutsideFunction: true,
96
+ locations: true,
97
+ });
98
+ return { ok: true, program };
99
+ }
100
+ catch (error) {
101
+ // Only acorn's own syntax errors become a located ParseError; anything else keeps the generic failure path.
102
+ const acorn = error instanceof SyntaxError ? decodeAcornError(error) : Option.none();
103
+ if (Option.isNone(acorn))
104
+ throw error;
105
+ return { ok: false, error: parseDiagnostic(acorn.value) };
106
+ }
107
+ };
108
+ // Acorn appends ` (line:column)` to its messages; its lines are 1-based and its columns 0-based.
109
+ const AcornError = Schema.Struct({
110
+ message: Schema.String,
111
+ loc: Schema.Struct({ line: Schema.Number, column: Schema.Number }),
112
+ });
113
+ const decodeAcornError = Schema.decodeUnknownOption(AcornError);
114
+ const parseDiagnostic = (error) => {
115
+ const location = { line: error.loc.line, column: error.loc.column + 1 };
116
+ const reason = error.message.replace(/ \(\d+:\d+\)$/, "");
117
+ const text = reason === "'import' and 'export' may appear only with 'sourceType: module'"
118
+ ? "Imports and exports aren't available. Call tools through `tools` and use the built-in globals."
119
+ : `${reason.endsWith(".") ? reason : `${reason}.`} Code must be plain JavaScript written as a program body.`;
120
+ return { kind: "ParseError", message: `SyntaxError: ${text}${formatLocation(location)}`, location };
94
121
  };
95
122
  const utf8ByteLength = (value) => new TextEncoder().encode(value).byteLength;
96
123
  // Drop a replacement character produced by truncating inside a UTF-8 sequence.
@@ -62,4 +62,7 @@ export declare const sourceLocation: (node: AstNode) => {
62
62
  readonly line: number;
63
63
  readonly column: number;
64
64
  };
65
- export declare const formatLocation: (node?: AstNode) => string;
65
+ export declare const formatLocation: (location?: {
66
+ readonly line: number;
67
+ readonly column: number;
68
+ }) => string;
@@ -51,9 +51,4 @@ export const sourceLocation = (node) => ({
51
51
  line: node.loc?.start.line ?? 1,
52
52
  column: (node.loc?.start.column ?? 0) + 1,
53
53
  });
54
- export const formatLocation = (node) => {
55
- if (!node?.loc)
56
- return "";
57
- const location = sourceLocation(node);
58
- return ` (line ${location.line}, col ${location.column})`;
59
- };
54
+ export const formatLocation = (location) => location ? ` (line ${location.line}, col ${location.column})` : "";
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-dev-160",
4
+ "version": "0.0.0-dev-170",
5
5
  "description": "Effect-native confined code execution over schema-described tools",
6
6
  "type": "module",
7
7
  "license": "MIT",