UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

45 lines • 1.95 kB
import { fileURLToPath } from "node:url"; import { Worker } from "node:worker_threads"; import { isBunBinary } from "../../config.js"; function looplessWorkerEntry() { return isBunBinary ? fileURLToPath(new URL("./src/modes/rpc/loopless-worker.ts", import.meta.url)).replaceAll("\\", "/") : new URL(import.meta.url.endsWith(".ts") ? "./loopless-worker.ts" : "./loopless-worker.js", import.meta.url); } export function startLooplessWorker() { const worker = new Worker(looplessWorkerEntry()); const exited = new Promise((resolve) => worker.once("exit", () => resolve())); // An isolate that fails outside its message handler (a module that does not load, an uncaught throw) emits `error`; // with no listener that would throw in the host and take every other session down with it. let failure; worker.on("error", (error) => { failure = error instanceof Error ? error.message : String(error); }); const post = (message) => worker.postMessage(message); return { exited, start: (configuration, cwd) => new Promise((resolve, reject) => { const onMessage = (message) => { worker.off("message", onMessage); if (message.type === "ready") resolve(message.tools); else reject(new Error(message.error)); }; worker.on("message", onMessage); void exited.then(() => reject(new Error(failure ?? "loopless_worker_exited"))); post({ type: "start", configuration, cwd }); }), close: async (graceMs) => { post({ type: "close" }); const grace = setTimeout(() => void worker.terminate(), graceMs); try { await exited; } finally { clearTimeout(grace); } }, }; } //# sourceMappingURL=loopless-worker-client.js.map