openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
41 lines (40 loc) • 1.69 kB
JavaScript
import { p as toErrorObject } from "./error-coercion-hDrHOSGy.mjs";
import { t as AsyncContext } from "./async-context-BYDAvG9A.mjs";
import { n as runtimeProcessEntrypoints } from "./runtime-process-entrypoints-qiyJvDDT.mjs";
import { t as createSpawnBrokerHost } from "./host-fTIlr4-H.mjs";
import os from "node:os";
//#region src/process/spawn-broker/context.ts
const context = new AsyncContext(Symbol("spawnBroker"));
let gatewayStartupDisabled = false;
/** A failed startup optimization stays disabled across Gateway restarts in this process. */
async function startGatewaySpawnBroker(e) {
if (gatewayStartupDisabled) return;
const t = process.constrainedMemory();
if ((t > 0 ? Math.min(os.totalmem(), t) : os.totalmem()) <= 1024 ** 3) return;
let n;
let r = runtimeProcessEntrypoints.spawnBroker.distWorkerPath;
try {
n = createSpawnBrokerHost({ onReady: e.onReady });
r = n.entryPath;
await n.ready();
return n;
} catch (t) {
gatewayStartupDisabled = true;
let i = toErrorObject(t, "Spawn broker startup failed").message;
try {
await n?.close();
} catch (e) {
i += `; cleanup failed: ${toErrorObject(e, "Spawn broker cleanup failed").message}`;
}
await e.onStartupFailure(`spawn broker startup failed: ${i}; runtime entry=${r}; in-process spawning is in effect for the lifetime of this process`.replaceAll("\r", " ").replaceAll("\n", " "));
return;
}
}
function runWithSpawnBroker(e, t) {
return context.getStore() === e ? t() : context.run(e, t);
}
function getSpawnBroker() {
return gatewayStartupDisabled ? void 0 : context.getStore();
}
//#endregion
export { runWithSpawnBroker as n, startGatewaySpawnBroker as r, getSpawnBroker as t };