Skip to content

Commit 2e44610

Browse files
committed
refactor(agents): dedupe code mode json serialization
1 parent 0f998e6 commit 2e44610

2 files changed

Lines changed: 7 additions & 30 deletions

File tree

src/agents/code-mode.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,10 +629,15 @@ async function runCodeModeWorker(
629629
timeoutMs: number,
630630
workerUrl?: URL,
631631
): Promise<CodeModeWorkerResult> {
632+
const resolvedWorkerUrl = workerUrl ?? codeModeWorkerUrl();
633+
const sourceWorkerExecArgv = resolvedWorkerUrl.pathname.endsWith(".ts")
634+
? ["--import", "tsx"]
635+
: undefined;
632636
let worker: Worker;
633637
try {
634-
worker = new Worker(workerUrl ?? codeModeWorkerUrl(), {
638+
worker = new Worker(resolvedWorkerUrl, {
635639
workerData,
640+
execArgv: sourceWorkerExecArgv,
636641
});
637642
} catch (error) {
638643
return failedCodeModeWorkerResult(error, "runtime_unavailable");

src/agents/code-mode.worker.ts

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { readFile } from "node:fs/promises";
66
import { createRequire } from "node:module";
77
import { parentPort, workerData } from "node:worker_threads";
88
import { EvalFlags, Intrinsics, JSException, QuickJS, type JSValueHandle } from "quickjs-wasi";
9+
import { toCodeModeJsonSafe as toJsonSafe } from "./code-mode-json.js";
910

1011
const require = createRequire(import.meta.url);
1112
const QUICKJS_WASM_PATH = require.resolve("quickjs-wasi/quickjs.wasm");
@@ -151,35 +152,6 @@ function isRecord(value: unknown): value is Record<string, unknown> {
151152
return Boolean(value && typeof value === "object" && !Array.isArray(value));
152153
}
153154

154-
function toJsonSafe(value: unknown): unknown {
155-
if (value === undefined) {
156-
return null;
157-
}
158-
try {
159-
const serialized = JSON.stringify(value);
160-
return serialized === undefined ? null : (JSON.parse(serialized) as unknown);
161-
} catch {
162-
if (value instanceof Error) {
163-
return { name: value.name, message: value.message };
164-
}
165-
if (value === null) {
166-
return null;
167-
}
168-
switch (typeof value) {
169-
case "string":
170-
case "number":
171-
case "boolean":
172-
return value;
173-
case "bigint":
174-
case "symbol":
175-
case "function":
176-
return String(value);
177-
default:
178-
return Object.prototype.toString.call(value);
179-
}
180-
}
181-
}
182-
183155
function errorMessage(error: unknown): string {
184156
if (error instanceof JSException) {
185157
return error.stack || error.message || String(error);

0 commit comments

Comments
 (0)