fix: harden production chat runtime
This commit is contained in:
@@ -3,6 +3,7 @@ import { basename, resolve } from "node:path";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { readFile, readdir } from "node:fs/promises";
|
||||
import { DatabaseSync } from "node:sqlite";
|
||||
import { Worker, isMainThread, parentPort, workerData } from "node:worker_threads";
|
||||
|
||||
function toIsoFromUnixSeconds(value) {
|
||||
if (!Number.isFinite(value) || value <= 0) return null;
|
||||
@@ -259,3 +260,43 @@ export async function discoverCodexProjectCandidates(options = {}) {
|
||||
projectCandidates: candidates,
|
||||
};
|
||||
}
|
||||
|
||||
export async function discoverCodexProjectCandidatesInWorker(options = {}) {
|
||||
return await new Promise((resolvePromise, rejectPromise) => {
|
||||
const worker = new Worker(new URL(import.meta.url), {
|
||||
workerData: {
|
||||
kind: "boss_codex_discovery",
|
||||
options,
|
||||
},
|
||||
});
|
||||
|
||||
worker.once("message", (payload) => {
|
||||
if (payload?.ok) {
|
||||
resolvePromise(payload.result);
|
||||
return;
|
||||
}
|
||||
rejectPromise(new Error(payload?.error ?? "DISCOVERY_WORKER_FAILED"));
|
||||
});
|
||||
|
||||
worker.once("error", rejectPromise);
|
||||
|
||||
worker.once("exit", (code) => {
|
||||
if (code === 0) {
|
||||
return;
|
||||
}
|
||||
rejectPromise(new Error(`DISCOVERY_WORKER_EXIT_${code}`));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (!isMainThread && workerData?.kind === "boss_codex_discovery") {
|
||||
try {
|
||||
const result = await discoverCodexProjectCandidates(workerData.options ?? {});
|
||||
parentPort?.postMessage({ ok: true, result });
|
||||
} catch (error) {
|
||||
parentPort?.postMessage({
|
||||
ok: false,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user