openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
54 lines (53 loc) • 1.78 kB
JavaScript
import { i as createAbortError } from "./src-BPmQBLJp.mjs";
import "./abort-signal-D4Q6SEw7.mjs";
import { setTimeout } from "node:timers/promises";
//#region src/infra/embedded-state-lock.ts
const EMBEDDED_STATE_SIGNALS = ["SIGINT", "SIGTERM"];
/** Bridges process signals into embedded-run cancellation so lock cleanup can unwind. */
function createEmbeddedStateSignalBridge() {
const e = new AbortController();
let t;
const n = /* @__PURE__ */ new Map();
const dispose = () => {
for (const [e, t] of n) process.off(e, t);
n.clear();
};
for (const r of EMBEDDED_STATE_SIGNALS) {
const handler = () => {
t = r;
e.abort();
dispose();
};
n.set(r, handler);
process.on(r, handler);
}
return {
signal: e.signal,
getReceivedSignal: () => t,
dispose
};
}
/** Probe the Gateway owner first, then acquire the shared embedded-writer role. */
async function acquireEmbeddedStateLock(e) {
const { acquireGatewayLock: t, GatewayLockError: n, readActiveGatewayLockIdentity: r } = await import("./gateway-lock-C8DzJkpV.mjs");
const i = e.options?.env ?? process.env;
if (e.options?.allowInTests !== true && (i.VITEST !== void 0 || i.NODE_ENV === "test")) return null;
const a = await r(e.options);
if (a) throw new n(e.formatActiveGatewayRefusal(a));
try {
return await t({
...e.options,
role: "agent-embedded",
sleep: e.options?.sleep ?? ((t) => setTimeout(t, void 0, { signal: e.signal }).catch(() => {
throw createAbortError("embedded state lock acquisition aborted");
}))
});
} catch (t) {
if (!(t instanceof n)) throw t;
const i = await r(e.options);
if (i) throw new n(e.formatActiveGatewayRefusal(i), t);
throw t;
}
}
//#endregion
export { createEmbeddedStateSignalBridge as n, acquireEmbeddedStateLock as t };