- Added worker-backed JS runtime via Transport and WorkerCore, with queued runs, status/error output, and clean teardown. - Refactored executeInVmContext to reuse worker sessions, route pending runs by id, and fallback inline on spawn failure. - Added rewrite helpers to convert top-level imports and demote top-level const/let/class declarations for persistence. - Handled aborts by canceling in-flight tool calls, hard-killing worker sessions, and dropping JS timeout enforcement.
25 lines
648 B
TypeScript
25 lines
648 B
TypeScript
import { parentPort } from "node:worker_threads";
|
|
import { WorkerCore } from "./worker-core";
|
|
import type { Transport, WorkerInbound, WorkerOutbound } from "./worker-protocol";
|
|
|
|
if (!parentPort) throw new Error("js worker-entry: missing parentPort");
|
|
|
|
const port = parentPort;
|
|
const transport: Transport = {
|
|
send: (msg: WorkerOutbound) => port.postMessage(msg),
|
|
onMessage: handler => {
|
|
const wrap = (data: unknown): void => handler(data as WorkerInbound);
|
|
port.on("message", wrap);
|
|
return () => port.off("message", wrap);
|
|
},
|
|
close: () => {
|
|
try {
|
|
port.close();
|
|
} catch {
|
|
// Already closed.
|
|
}
|
|
},
|
|
};
|
|
|
|
new WorkerCore(transport);
|