openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
66 lines (65 loc) • 2.68 kB
JavaScript
import { t as pruneMapToMaxSize } from "./map-size-BlrH_CnE.mjs";
import { r as captureOpenClawStateWorkerContext } from "./openclaw-state-worker-context-BvvgY6-t.mjs";
import { o as runOpenClawStateWorkerOperation } from "./openclaw-state-worker-store-BxZXTKna.mjs";
import { c as resolveDeviceIdentityStore, n as assertNoPendingLegacyIdentity } from "./device-identity-store-BO1tgcFb.mjs";
//#region src/infra/device-identity-process-cache.ts
const identities = /* @__PURE__ */ new Map();
function readProcessDeviceIdentity(cacheKey) {
return identities.get(cacheKey);
}
function cacheProcessDeviceIdentity(cacheKey, identity) {
const cached = identities.get(cacheKey);
if (cached) return cached;
pruneMapToMaxSize(identities, 31);
identities.set(cacheKey, identity);
return identity;
}
//#endregion
//#region src/infra/device-identity-async.ts
/** Bootstrap through the identity owner before the shared actor opens a missing database. */
async function loadOrCreateDeviceIdentityAsync(options = {}) {
const { databasePath, identityKey } = resolveDeviceIdentityStore(options);
const context = captureOpenClawStateWorkerContext({
...options,
path: databasePath
});
return await runOpenClawStateWorkerOperation(context, (scope) => scope.execute({
type: "deviceIdentity.load",
input: { identityKey }
}), { preparation: {
type: "deviceIdentity",
identityKey
} });
}
/** Keep the existing process-lifetime identity cache without opening SQLite on a warm hit. */
async function loadOrCreateProcessDeviceIdentityAsync(options = {}) {
const { databasePath, identityKey } = resolveDeviceIdentityStore(options);
const cacheKey = `${databasePath}\0${identityKey}`;
const cached = readProcessDeviceIdentity(cacheKey);
if (cached) return cached;
return cacheProcessDeviceIdentity(cacheKey, await loadOrCreateDeviceIdentityAsync({
...options,
path: databasePath,
identityKey
}));
}
/** Read the captured identity on the shared worker without creating SQLite state. */
async function loadDeviceIdentityIfPresentAsync(options = {}) {
const { databasePath, identityKey } = resolveDeviceIdentityStore(options);
const context = captureOpenClawStateWorkerContext({
...options,
path: databasePath
});
const identity = await runOpenClawStateWorkerOperation(context, (scope) => scope.execute({
type: "deviceIdentity.read",
input: { identityKey }
}), { existingOnly: true });
if (identity !== void 0) return identity;
assertNoPendingLegacyIdentity({
path: databasePath,
identityKey
});
return null;
}
//#endregion
export { loadOrCreateDeviceIdentityAsync as n, loadOrCreateProcessDeviceIdentityAsync as r, loadDeviceIdentityIfPresentAsync as t };