Files
openclaw/src/agents/code-mode-script-syntax.ts
T
Peter Steinberger 0fa0315a75 fix(cron): reject malformed Code Mode scripts when scheduling (#118100)
* fix(cron): reject malformed Code Mode scripts

Reject invalid JavaScript syntax during cron add and update so CLI and agent callers receive an actionable location before persistence. Preserve the async Code Mode grammar, including top-level await and return. Fixes #118088.

* fix(cron): keep enabled-only patches working for stored malformed scripts
2026-08-02 11:48:37 -07:00

35 lines
1.0 KiB
TypeScript

import { parse, type Position, type Program } from "acorn";
type AcornSyntaxError = SyntaxError & { loc: Position };
type CodeModeScriptParseResult =
| { ok: true; program: Program }
| { ok: false; message: string; line: number; column: number };
/** Mirrors the worker's async-arrow body grammar so valid top-level await/return stay legal. */
export function buildCodeModeScriptParseSource(code: string): {
source: string;
codeOffset: number;
} {
const prefix = "(async () => {\n";
return { source: `${prefix}${code}\n})`, codeOffset: prefix.length };
}
export function parseCodeModeScriptSyntax(code: string): CodeModeScriptParseResult {
const { source } = buildCodeModeScriptParseSource(code);
try {
return {
ok: true,
program: parse(source, { ecmaVersion: "latest" }),
};
} catch (error) {
const syntaxError = error as AcornSyntaxError;
return {
ok: false,
message: syntaxError.message.replace(/ \(\d+:\d+\)$/u, ""),
line: syntaxError.loc.line - 1,
column: syntaxError.loc.column,
};
}
}