UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

111 lines (110 loc) • 3.17 kB
import { n as GATEWAY_SERVICE_STOP_TIMEOUT_MS } from "./gateway-shutdown-budget-cDAYhBVI.mjs"; import { f as CLI_RESPAWN_PARENT_ENV } from "./cli-invocation-ZSPsKRWa.mjs"; import { n as tryProcessCwd } from "./safe-cwd-CZqaxroH.mjs"; import { t as settlesWithin } from "./settle-within-Bj8A0Tzk.mjs"; import { c as registerSignalExitGate } from "./signal-exit-barrier-BOGtFHQL.mjs"; import { o as signalChildProcessTree } from "./child-process-tree-BHY5y2g3.mjs"; import { n as resolveCurrentOpenClawCliInvocation } from "./openclaw-cli-invocation-CiQvieV9.mjs"; import { r as isGatewayProtocolResponseError } from "./protocol-request-CsClAJsG.mjs"; import { t as spawnAndWait } from "./spawn-utils-DLbtt13t.mjs"; //#region src/commands/onboard-gateway-process.ts async function requestOwnedGatewayStop(e) { try { const { readActiveGatewayLockIdentity: t } = await import("./gateway-lock-C8DzJkpV.mjs"); const n = await t(); if (!n || n.pid !== e || !n.ownerId) return false; const { callGatewayCli: r } = await import("./call-CT5jlfNe.mjs"); let i = false; try { await r({ method: "gateway.stop.request", params: { target: { pid: n.pid, ownerId: n.ownerId, port: n.port } }, localPortOverride: n.port, sharedStateMode: "read-only", allowLocalBackendAuthNone: true, requiredMethods: ["gateway.stop.request"], timeoutMs: 1e4, assertDispatchCurrent: () => { i = true; } }); } catch (e) { if (isGatewayProtocolResponseError(e)) i = false; } return i; } catch { return false; } } /** Setup keeps its one-shot database role; the child owns Gateway admission and shutdown. */ async function startOnboardingGatewayProcess(e = {}) { const t = resolveCurrentOpenClawCliInvocation([ "gateway", "run", ...e.port === void 0 ? [] : ["--port", String(e.port)] ]); const n = process.platform !== "win32"; const r = await spawnAndWait({ argv: [t.command, ...t.args], options: { cwd: tryProcessCwd() ?? t.cwd, env: { ...process.env, ...t.env, [CLI_RESPAWN_PARENT_ENV]: String(process.pid) }, detached: n, stdio: [ "ignore", "inherit", "inherit", "ipc" ], windowsHide: true } }); let i = false; let a; r.on("error", (e) => { a = e; }); const o = new Promise((e) => { r.once("close", (t, n) => { i = true; e({ code: t, signal: n }); }); }); const s = o.then(({ code: e, signal: t }) => { if (a) throw a; if (e !== 0 || t) throw new Error(`Gateway process exited ${t ? `with signal ${t}` : `with code ${e ?? "unknown"}`}`); }); s.catch(() => {}); let c; const close = () => { c ??= (async () => { const e = !i && await requestOwnedGatewayStop(r.pid); if (!i && !e) r.kill("SIGTERM"); if (!i && !await settlesWithin(o, 33e4)) await new Promise((e) => { signalChildProcessTree(r, "SIGKILL", e); }); await s; })().finally(() => l()); return c; }; const l = registerSignalExitGate(o.then(() => {}), () => { close().catch((e) => process.stderr.write(`${String(e)}\n`)); }); return { wait: s, close }; } //#endregion export { startOnboardingGatewayProcess as t };