UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

152 lines (151 loc) • 6.46 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { n as notifyListeners, r as registerListener } from "./listeners-Cb2-CnP8.mjs"; import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs"; import { i as createSqliteWorkerOperationAdmission } from "./sqlite-worker-operation-admission-9E_X-oRy.mjs"; import { r as captureOpenClawStateWorkerContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs"; import { i as runOpenClawStateWorkerOperation } from "./openclaw-state-worker-store-2LoAv_8B.mjs"; import { o as sessionChanges } from "./session-row-changes-BA4BqVwF.mjs"; import { n as reserveWorkerEnvironmentNativePublication } from "./store-native-publication-CYGHGL2V.mjs"; import { t as clearDeviceAuthToken } from "./device-auth-store-B3F61F1r.mjs"; import { s as withDevicePairingLock, t as listDevicePairingStoreRecordsReadOnly } from "./device-pairing-store-readonly-CIEEZowO.mjs"; import { t as captureDevicePairingPublication } from "./device-pairing-publication-PPGAGd4E.mjs"; //#region src/infra/device-pairing-card-renderer.ts let pairedCardRendererCache; function invalidatePairedCardRendererCache() { pairedCardRendererCache = void 0; } /** The pairing publication owner invalidates this projection after committed mutations. */ function readPairedCardRendererCache(e, t) { if (pairedCardRendererCache?.stateDir !== e) pairedCardRendererCache = { stateDir: e, value: t() }; return pairedCardRendererCache.value; } //#endregion //#region src/infra/device-pairing-worker.ts const pairingLog = createSubsystemLogger("gateway/device-pairing"); const DevicePairingAuthorityRefusedError = resolveGlobalSingleton(Symbol.for("openclaw.devicePairingAuthorityRefusedError"), () => class extends Error { constructor(e = "Device pairing authority changed") { super(e); } }); const setupChanges = resolveGlobalSingleton(Symbol.for("openclaw.devicePairSetupChanges"), () => /* @__PURE__ */ new Set()); const onDevicePairSetupChange = (e) => registerListener(setupChanges, e); function admissionFacts(e) { if (!Array.isArray(e) || !e.every((e) => isRecord(e) && typeof e.kind === "string")) throw new Error("Invalid pairing admission facts"); for (const t of e) if (t.kind === "bootstrap.cloudWorkerSetup" && (typeof t.environmentId !== "string" || typeof t.setupId !== "string" || typeof t.credentialDigest !== "string" || typeof t.provisionOperationId !== "string" || typeof t.ownerEpoch !== "number") || (t.kind === "bootstrap.consume" || t.kind === "bootstrap.token") && typeof t.expiresAtMs !== "number") throw new Error("Invalid pairing admission facts"); return e; } function commitReceipt(e) { if (!isRecord(e) || e.kind !== "devicePairing") throw new Error("Invalid pairing commit receipt"); return e; } function executeDevicePairingMutation(e, t = {}) { const n = t.context ?? captureOpenClawStateWorkerContext(t.baseDir ? { env: { ...process.env, OPENCLAW_STATE_DIR: t.baseDir } } : {}); const r = structuredClone(e); const i = { ...process.env, ...n.environment }; const a = withDevicePairingLock(async () => { n.admission.assertCurrent(); t.assertCurrent?.(); const e = r.type === "devicePairing.registerJoinCode" || r.type === "devicePairing.redeemJoinCode" || r.type === "bootstrap.prune" ? void 0 : captureDevicePairingPublication(n.admission); const a = e?.beginMutation(); let o; let s = false; let c; let l; const install = () => { if (!a) return; const e = o?.committed; if (e && !s) { const r = commitReceipt(e.facts); if (r.retiredNodeToken) c = clearDeviceAuthToken({ ...r.retiredNodeToken, role: "node", env: i }).catch((e) => { pairingLog.warn(`Local retired node token cleanup failed: ${String(e)}`); return false; }); const o = r.workerEnvironment; let u = false; if (o && l) { n.admission.assertCurrent(); u = l(o.environmentId, { nodeDeviceId: o.nodeDeviceId, updatedAtMs: o.updatedAtMs }); } s = true; a.publish(r); if (r.nodeApproval) t.onNodeApproved?.(r.nodeApproval); invalidatePairedCardRendererCache(); if (u) sessionChanges.emit({ all: true, scope: "worker-environments" }); if (r.tokensReplaced) t.onTokensReplaced?.(r.tokensReplaced.deviceId, r.tokensReplaced.roles); } }; const u = e?.servicePending(install); try { const e = await runOpenClawStateWorkerOperation(n, async (e) => { try { return await e.execute(r); } finally { install(); } }, { assertCurrent: t.assertCurrent, createAdmission: () => { let e = false; o = createSqliteWorkerOperationAdmission((i, a) => { if (e || i.stage !== "transaction" && i.stage !== "commit") throw new Error("Pairing admission requested out of order"); n.admission.assertCurrent(); t.assertCurrent?.(); for (const e of admissionFacts(i.facts)) t.admit?.(e); if (i.stage === "commit" && r.type === "bootstrap.consume") l = reserveWorkerEnvironmentNativePublication(n.admission.identity); if (!a()) throw new DevicePairingAuthorityRefusedError(); e = i.stage === "commit"; }); return { admission: o, nativeLocations: [n.admission.databasePath] }; } }); await c; return e; } finally { try { install(); } finally { a?.finish(!o || o.settlement?.kind === "completed"); u?.(); if (r.type === "bootstrap.consume" || r.type === "bootstrap.confirm" || r.type === "bootstrap.ensure" || r.type === "bootstrap.readCompletion") notifyListeners(setupChanges, n.admission.databasePath); } } }); const o = t.onAuthorityRefused; return o ? a.catch((e) => { if (e instanceof DevicePairingAuthorityRefusedError) return o(); throw e; }) : a; } /** Start the privileged effect in the same interval that publishes its pairing facts. */ async function withCurrentDevicePairingSnapshot(e, t, n) { return (await withDevicePairingLock(async () => { const { paired: r } = await listDevicePairingStoreRecordsReadOnly(e, true); if (n) await n(); return { value: t(r)?.start() }; })).value; } //#endregion export { invalidatePairedCardRendererCache as a, withCurrentDevicePairingSnapshot as i, executeDevicePairingMutation as n, readPairedCardRendererCache as o, onDevicePairSetupChange as r, DevicePairingAuthorityRefusedError as t };