UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

224 lines (223 loc) • 8.04 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs"; import "./paths-DAcAg5aV.mjs"; import { r as captureOpenClawStateWorkerContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs"; import { c as isProgressCardRendererClient } from "./message-channel-DTYVLJT1.mjs"; import { a as invalidatePairedCardRendererCache, n as executeDevicePairingMutation, o as readPairedCardRendererCache, t as DevicePairingAuthorityRefusedError } from "./device-pairing-worker-SqWhlBR1.mjs"; import { i as loadPendingDevicePairingStoreRecordReadOnly, r as loadPairedDevicePairingStoreRecordReadOnly, s as withDevicePairingLock, t as listDevicePairingStoreRecordsReadOnly } from "./device-pairing-store-readonly-CIEEZowO.mjs"; import { u as loadDevicePairingStateForMutation, w as persistDevicePairingStoreState } from "./device-pairing-identity-BqPJxbIo.mjs"; import path from "node:path"; //#region src/infra/device-pairing-access.ts /** Last-seen time/reason may lag by a minute; changed metadata is always recorded. */ function preparePairedDeviceAccessPatch(e, t) { if (![ "displayName", "operatorLabel", "platform", "clientId", "clientMode", "remoteIp" ].some((n) => n in t && e[n] !== t[n]) && (t.lastSeenAtMs === void 0 || t.lastSeenAtMs - (e.lastSeenAtMs ?? 0) < 6e4)) return; return t; } //#endregion //#region src/infra/device-pairing-resolution.ts const waiters = resolveGlobalSingleton(Symbol.for("openclaw.devicePairingResolutionWaiters"), () => /* @__PURE__ */ new Map()); function waitForDevicePairingResolution(e, t) { if (t.signal.aborted) return Promise.resolve(void 0); const n = path.resolve(t.baseDir ?? resolveStateDir()); const r = waiters.get(n) ?? /* @__PURE__ */ new Set(); waiters.set(n, r); return new Promise((i) => { let a; const finish = (e) => { clearTimeout(a); t.signal.removeEventListener("abort", abort); r.delete(o); if (r.size === 0) waiters.delete(n); i(e); }; const abort = () => finish(); const refresh = (e) => { clearTimeout(a); a = setTimeout(() => finish("expired"), Math.max(0, e - Date.now())); a.unref(); }; const o = { ...e, resolve: finish, refresh }; refresh(t.expiresAtMs); r.add(o); t.signal.addEventListener("abort", abort, { once: true }); }); } function refreshDevicePairingResolutionWaiters(e, t, n) { const r = waiters.get(path.resolve(n ?? resolveStateDir())); for (const n of r ?? []) if (n.requestId === e.requestId && n.deviceId === e.deviceId) n.refresh(t); } function publishDevicePairingResolution(e, t, n) { const r = waiters.get(path.resolve(n ?? resolveStateDir())); for (const n of r ?? []) if (n.requestId === e.requestId && n.deviceId === e.deviceId) n.resolve(t); } //#endregion //#region src/infra/device-pairing.ts /** Return whether this Gateway has a paired client that can render progress cards. */ function hasPairedCardRenderer(e) { const t = e ?? resolveStateDir(); return readPairedCardRendererCache(t, () => listDevicePairingReadOnly(t).then(({ paired: e }) => e.some(isProgressCardRendererClient)).catch(() => false)); } /** Boot/Doctor migrations retain their synchronous snapshot transaction under the pairing lock. */ async function withPairedDeviceRecords(e, t) { return await withDevicePairingLock(async () => { const n = loadDevicePairingStateForMutation(Date.now(), e); const r = await t(n.pairedByDeviceId); if (r.persist) { persistDevicePairingStoreState(n, e, "paired"); invalidatePairedCardRendererCache(); } return r.value; }); } function listDevicePairing(e) { return listDevicePairingStoreRecordsReadOnly(e, true); } /** List pairing state without creating or migrating shared state. */ function listDevicePairingReadOnly(e) { return listDevicePairingStoreRecordsReadOnly(e); } function getPairedDevice(e, t) { return loadPairedDevicePairingStoreRecordReadOnly(e, t); } function getPendingDevicePairing(e, t) { return loadPendingDevicePairingStoreRecordReadOnly(e, t); } async function requestDevicePairing(e, t, n) { return await withDevicePairingLock(async () => { const r = await executeDevicePairingMutation({ type: "devicePairing.request", input: { request: e, nowMs: Date.now() } }, { baseDir: t }); for (const e of r.superseded ?? []) publishDevicePairingResolution(e, "superseded", t); refreshDevicePairingResolutionWaiters(r.request, r.expiresAtMs, t); n?.(r); return r; }); } async function rejectDevicePairing(e, t) { return await withDevicePairingLock(async () => { const n = await executeDevicePairingMutation({ type: "devicePairing.reject", input: { requestId: e, nowMs: Date.now() } }, { baseDir: t }); if (n) publishDevicePairingResolution(n, "rejected", t); return n; }); } async function removePairedDevice(e, t) { return await executeDevicePairingMutation({ type: "devicePairing.remove", input: { deviceId: e, nowMs: Date.now() } }, { baseDir: t }); } async function pruneSupersededSilentPairedDevices(e) { return await executeDevicePairingMutation({ type: "devicePairing.pruneSilent", input: { deviceId: e.deviceId, protectedDeviceIds: e.protectedDeviceIds ?? [], nowMs: e.nowMs ?? Date.now() } }, { baseDir: e.baseDir, onAuthorityRefused: () => [], admit: (t) => { if (t.kind === "pairing-prune" && t.deviceIds.some((t) => e.isDeviceConnected?.(t))) throw new DevicePairingAuthorityRefusedError("Pairing prune candidate connected before commit"); } }); } async function removePairedDeviceRole(e) { return await executeDevicePairingMutation({ type: "devicePairing.removeRole", input: { deviceId: e.deviceId, role: "node", nowMs: Date.now() } }); } async function updatePairedDeviceMetadata(e, t, n) { return await executeDevicePairingMutation({ type: "devicePairing.updateMetadata", input: { deviceId: e, patch: t } }, { baseDir: n }); } async function updatePairedDevicePresence(e, t, n, r) { return await executeDevicePairingMutation({ type: "devicePairing.updatePresence", input: { deviceId: e, patch: t, expectedPairingGeneration: n } }, { baseDir: r }); } const reconnects = resolveGlobalSingleton(Symbol.for("openclaw.devicePairingReconnects"), () => /* @__PURE__ */ new Map()); /** One tracked post-hello writer per device; queued tabs contribute their latest metadata. */ function recordPairedDeviceAccess(e, t, n, r) { const i = captureOpenClawStateWorkerContext(r ? { env: { ...process.env, OPENCLAW_STATE_DIR: r } } : {}); const a = `${i.admission.identity.key}:${e}`; const o = reconnects.get(a); if (o) { o.patch = o.identity.publicKey === n.publicKey && o.identity.approvedAtMs === n.approvedAtMs ? { ...o.patch, ...t } : t; o.identity = n; return o.done; } const s = { patch: t, identity: n, done: Promise.resolve() }; reconnects.set(a, s); s.done = withDevicePairingLock(async () => { while (s.patch) { const t = s.patch; const n = s.identity; s.patch = void 0; i.admission.assertCurrent(); const a = await getPairedDevice(e, r); if (!a || a.publicKey !== n.publicKey || a.approvedAtMs !== n.approvedAtMs) continue; const o = preparePairedDeviceAccessPatch(a, t); if (o) await executeDevicePairingMutation({ type: "devicePairing.updateMetadata", input: { deviceId: e, patch: o, accessIdentity: n } }, { context: i }); } }).finally(() => reconnects.delete(a)); return s.done; } //#endregion export { listDevicePairingReadOnly as a, rejectDevicePairing as c, requestDevicePairing as d, updatePairedDeviceMetadata as f, waitForDevicePairingResolution as g, publishDevicePairingResolution as h, listDevicePairing as i, removePairedDevice as l, withPairedDeviceRecords as m, getPendingDevicePairing as n, pruneSupersededSilentPairedDevices as o, updatePairedDevicePresence as p, hasPairedCardRenderer as r, recordPairedDeviceAccess as s, getPairedDevice as t, removePairedDeviceRole as u };