UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

141 lines (140 loc) • 4.82 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { s as resolveOpenClawStateSqlitePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs"; import { i as getActiveOpenClawStateDatabaseReadSnapshot, n as executeExistingOpenClawStateRead } from "./openclaw-state-db-readonly-B6YHb1oF.mjs"; import { r as captureOpenClawStateWorkerContext, t as captureOpenClawStateReadContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs"; import { n as createAsyncLock } from "./json-files-Cb1h8pYs.mjs"; import { t as captureDevicePairingPublication } from "./device-pairing-publication-PPGAGd4E.mjs"; import { AsyncLocalStorage } from "node:async_hooks"; //#region src/infra/device-pairing-lock.ts const owner = resolveGlobalSingleton(Symbol.for("openclaw.devicePairingLock"), () => ({ lock: createAsyncLock(), current: new AsyncLocalStorage() })); /** Domain facades and their broker adapter share one admission interval. */ async function withDevicePairingLock(e) { if (owner.current.getStore()?.active) return e(); return owner.lock(async () => { const t = { active: true }; try { return await owner.current.run(t, e); } finally { t.active = false; } }); } //#endregion //#region src/infra/device-pairing-store-readonly.ts async function readPairing(e, t, n = false, r = false) { const i = t ? { env: { ...process.env, OPENCLAW_STATE_DIR: t } } : {}; const a = captureOpenClawStateWorkerContext(i); const o = { path: a.admission.databasePath, env: a.environment }; const s = n ? void 0 : getActiveOpenClawStateDatabaseReadSnapshot(o); const read = async () => { const t = captureDevicePairingPublication(a.admission); const i = r && !s ? t.readNodes() : void 0; if (i) { a.admission.assertCurrent(); return { reply: void 0, nodes: i }; } let c; try { const r = e.type === "devicePairing.list" && !s ? { ...e, publishedRevision: t.completeRevision() } : e; c = await executeExistingOpenClawStateRead(o, r, { current: n }); } catch (e) { if (!s) t.fail(); throw e; } a.admission.assertCurrent(); if (s) return { reply: c, nodes: void 0 }; if (!t.isCurrent()) return; if (c && "bindings" in c) t.publish(c.revision, c.bindings, c.type === "devicePairing.list"); else if (!c) t.publish("missing", [], true); return { reply: c, nodes: r ? t.readNodes() : void 0 }; }; const c = await read(); if (c) return c; return withDevicePairingLock(async () => { const e = await read(); if (!e) throw new Error("Device pairing read publication was replaced"); return e; }); } /** Current paired rows and prepared node bindings share the publication's revision and lifetime. */ async function readDevicePairingNodeSnapshot(e) { const { nodes: t } = await readPairing({ type: "devicePairing.list", nowMs: Date.now() }, e, true, true); if (!t) throw new Error("Unexpected pairing node snapshot reply"); return t; } /** Readers never create, migrate, or synchronously open the shared database. */ async function listDevicePairingStoreRecordsReadOnly(e, t = false) { const { reply: n } = await readPairing({ type: "devicePairing.list", nowMs: Date.now() }, e, t); if (!n) return { pending: [], paired: [] }; return n.list; } function loadPairedDevicePairingStoreRecordReadOnly(e, t) { return readCurrentPairedDevice(e, (e) => e, t); } /** Run authorization without yielding after reading the committed publication. */ async function readCurrentPairedDevice(e, t, n) { const r = captureOpenClawStateReadContext(resolveOpenClawStateSqlitePath(n ? { ...process.env, OPENCLAW_STATE_DIR: n } : process.env)); let i = captureDevicePairingPublication(r.admission).readDevice(e.trim()); if (i === void 0) { await readPairing({ type: "devicePairing.lookup", deviceId: e.trim() }, n, true); i = captureDevicePairingPublication(r.admission).readDevice(e.trim()); if (i === void 0) throw new Error("Device pairing read publication was replaced"); } r.admission.assertCurrent(); return t(i); } async function loadPendingDevicePairingStoreRecordReadOnly(e, t) { const { reply: n } = await readPairing({ type: "devicePairing.pending", requestId: e, nowMs: Date.now() }, t, true); if (!n) return null; return n.pending; } async function loadBoundDeviceBootstrapContextReadOnly(e, t) { const { reply: n } = await readPairing({ type: "devicePairing.bootstrapContext", input: e }, t, true); if (!n) return null; return n.context; } //#endregion export { readCurrentPairedDevice as a, loadPendingDevicePairingStoreRecordReadOnly as i, loadBoundDeviceBootstrapContextReadOnly as n, readDevicePairingNodeSnapshot as o, loadPairedDevicePairingStoreRecordReadOnly as r, withDevicePairingLock as s, listDevicePairingStoreRecordsReadOnly as t };