openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
158 lines (157 loc) • 5.18 kB
JavaScript
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs";
import { n as freezeTrustedJsonSnapshot } from "./immutable-data-Y7xVjZoy.mjs";
import { s as resolveOpenClawStateSqlitePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs";
import { b as registerOpenClawStateDatabaseAsyncResource, x as registerOpenClawStateDatabaseLifecycleListener } from "./openclaw-state-db-cache-CkwtTqzF.mjs";
//#region src/infra/device-pairing-publication.ts
const publications = resolveGlobalSingleton(Symbol.for("openclaw.devicePairingPublications"), () => {
const e = /* @__PURE__ */ new Map();
registerOpenClawStateDatabaseAsyncResource({
phase: "after-resources",
async close(t) {
for (const [n, r] of e) if (!t || r.identity === t.key || r.canonicalPath === t.canonicalPath) e.delete(n);
}
});
registerOpenClawStateDatabaseLifecycleListener((t) => {
if (t.kind === "opened") return;
for (const [n, r] of e) if (n === t.path || r.identity === t.identity?.key) e.delete(n);
});
return e;
});
/** Native pairing writers retire the node projection after their transaction commits. */
function invalidateDevicePairingNodeSnapshot(e) {
const t = publications.get(e);
if (t) {
t.epoch++;
t.nodes = void 0;
t.devices = Object.create(null);
t.complete = false;
}
}
function captureDevicePairingPublication(e) {
const t = e.databasePath;
const { identity: n } = e;
let r = publications.get(n.key) ?? publications.get(n.canonicalPath);
if (r && r.identity !== n.key) {
for (const [e, t] of publications) if (t === r) publications.delete(e);
r = void 0;
}
if (!r) r = {
identity: n.key,
canonicalPath: n.canonicalPath,
epoch: 0,
blocked: false,
complete: false,
rows: /* @__PURE__ */ new Map(),
devices: Object.create(null),
pending: /* @__PURE__ */ new Set()
};
publications.set(t, r);
publications.set(n.canonicalPath, r);
publications.set(n.key, r);
const i = r;
const a = i.epoch;
const install = (e) => {
for (const t of e) {
i.rows.set(t.deviceId, t.binding ? { ...t.binding } : null);
i.devices[t.deviceId] = structuredClone(t.device);
}
};
return {
readDevice(e) {
for (const e of i.pending) e();
if (publications.get(t) !== i || i.blocked) return;
const n = i.devices[e];
return n === void 0 && !i.complete ? void 0 : structuredClone(n ?? null);
},
isCurrent: () => publications.get(t) === i && i.epoch === a && !i.mutation,
completeRevision: () => !i.blocked && i.complete ? i.revision : void 0,
readNodes() {
for (const e of i.pending) e();
if (publications.get(t) !== i || i.blocked || !i.complete) return;
if (!i.nodes) {
const e = /* @__PURE__ */ new Map();
for (const [t, n] of i.rows) if (n) e.set(t, Object.freeze({ ...n }));
const t = Object.values(i.devices).filter((e) => e !== null).toSorted((e, t) => t.approvedAtMs - e.approvedAtMs);
i.nodes = Object.freeze({
paired: freezeTrustedJsonSnapshot(t),
bindings: e
});
}
return i.nodes;
},
fail() {
if (publications.get(t) === i && i.epoch === a) {
i.blocked = true;
i.nodes = void 0;
}
},
publish(e, n, r = false) {
if (publications.get(t) !== i || i.epoch !== a || i.mutation) return false;
if (!n) {
if (i.revision !== e || !i.complete) throw new Error("Pairing publication cannot reuse an unknown revision");
i.blocked = false;
return true;
}
if (i.revision !== e) {
i.epoch++;
i.nodes = void 0;
i.complete = false;
}
if (r) {
i.rows.clear();
i.devices = Object.create(null);
}
i.revision = e;
install(n);
i.complete ||= r;
i.blocked = false;
return true;
},
beginMutation() {
i.epoch++;
const e = {};
i.mutation = e;
return {
publish(n) {
if (publications.get(t) !== i || i.mutation !== e) return;
i.complete &&= n.beforeRevision === i.revision;
if (n.revision !== i.revision) i.nodes = void 0;
install(n.changed);
i.revision = n.revision;
i.blocked = false;
i.mutation = void 0;
i.epoch++;
},
finish(t) {
if (t && i.mutation === e) {
i.mutation = void 0;
i.epoch++;
} else if (!t && i.mutation === e) i.blocked = true;
}
};
},
servicePending(e) {
i.pending.add(e);
return () => i.pending.delete(e);
}
};
}
/**
* Unknown facts suppress use without declaring an otherwise live node revoked.
* In-flight writes are not unknown: rows stay authoritative until COMMIT, and the pending
* services below install a committed receipt before this read. Gateway revocations also
* retire live clients directly.
*/
function getPublishedPairedDeviceBinding(e, t) {
const n = resolveOpenClawStateSqlitePath(t ? {
...process.env,
OPENCLAW_STATE_DIR: t
} : process.env);
const r = publications.get(n);
for (const e of r?.pending ?? []) e();
if (!r || r.blocked || !r.complete && !r.rows.has(e)) throw new Error("Device pairing authority requires a current worker publication");
const i = r.rows.get(e);
return i ? { ...i } : null;
}
//#endregion
export { getPublishedPairedDeviceBinding as n, invalidateDevicePairingNodeSnapshot as r, captureDevicePairingPublication as t };