UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

210 lines (209 loc) • 8.82 kB
import { t as hasErrnoCode } from "./errno-DFcoqIT9.mjs"; import { t as observeOwnedDirectory } from "./owned-directory-observation-2VESyPSQ.mjs"; import { a as getOpenClawDatabaseMaintenanceScope } from "./openclaw-state-maintenance-context-D6i7Zdhn.mjs"; import { a as databaseFileIdentityKey, c as inspectDatabasePathIdentitySync, f as readDatabaseIdentityBirthtime, p as readDatabasePathIdentity } from "./sqlite-worker-identity-BH8ZkJU9.mjs"; import { n as isArtifactPreservingStateRead } from "./artifact-preserving-state-reads-BfhmBr-V.mjs"; import { x as readOnlyWorkerScope, y as captureSqliteReadOnlyWorkerScope } from "./sqlite-readonly-worker-D3pRJU7r.mjs"; import { n as runWithSpawnBroker, t as getSpawnBroker } from "./context-Btn9lVDh.mjs"; import { c as registerAgentDatabaseReaderCloser, s as matchesAgentDatabaseReadCandidatePath } from "./agent-database-readers-kAORIt-A.mjs"; import { b as registerOpenClawStateDatabaseAsyncResource } from "./openclaw-state-db-cache-CkwtTqzF.mjs"; import fs from "node:fs"; import path from "node:path"; import { stat as stat$1 } from "node:fs/promises"; import { isMainThread } from "node:worker_threads"; //#region src/agents/auth-profiles/file-state.ts const directories = /* @__PURE__ */ new Map(); let gatewayFileChangePublisher; const authProfileFileChanges = {}; const legacyNames = /* @__PURE__ */ new Set([ "auth-profiles.json", "auth-state.json", "auth.json", "oauth.json" ]); function missingFile(e) { return hasErrnoCode(e, "ENOENT") ? null : void 0; } function retireDirectory(e, t) { t.observation.close(); directories.delete(e); } registerOpenClawStateDatabaseAsyncResource({ close: async () => closeAuthProfileFileState() }); registerAgentDatabaseReaderCloser((e, t) => { for (const [n, r] of directories) { for (const n of r.identities.keys()) if (!t?.has(path.resolve(n)) && e.some((e) => matchesAgentDatabaseReadCandidatePath(e, n))) r.identities.delete(n); if (!r.identities.size && e.some(({ path: e }) => path.dirname(path.resolve(e)) === n && !t?.has(path.resolve(e)))) retireDirectory(n, r); } }); function refreshDirectory(e, t) { t.dirty = true; t.refresh ??= Promise.resolve().then(async () => { try { while (t.dirty && directories.get(e) === t) { t.dirty = false; let n = false; const r = []; const i = /* @__PURE__ */ new Map(); for (const [i, a] of t.identities) { const o = await stat$1(i, { bigint: true }).catch(missingFile); const s = o === void 0 ? void 0 : o?.isFile() && a?.key === `file:${databaseFileIdentityKey(o)}` && a.birthtime === readDatabaseIdentityBirthtime(o) ? a : await readDatabasePathIdentity(i).catch(() => void 0); if (directories.get(e) !== t) return; if (t.identities.get(i) !== a) { t.dirty = true; continue; } if (s?.key !== a?.key || s?.birthtime !== a?.birthtime) { if (a?.key.startsWith("path:") && s?.key.startsWith("file:")) r.push(i); else n = true; t.identities.set(i, s); } } for (const [n, r] of t.presence) { const a = await stat$1(n).then(() => true, () => false); if (directories.get(e) !== t) return; if (a !== r) i.set(n, a); } const a = i.size > 0; if (a) { await Promise.all([...t.sourcePreparations].map((n) => n(i, () => directories.get(e) === t))); if (directories.get(e) !== t) return; for (const [e, n] of i) t.presence.set(e, n); } if (t.identities.size || a) await t.publishChange({ directory: e, identityChanged: n, sourcesChanged: a, createdPaths: r }); } } finally { t.refresh = void 0; } }); return t.refresh; } function captureFileChangePublisher() { const e = getSpawnBroker(); const t = captureSqliteReadOnlyWorkerScope(); const n = readOnlyWorkerScope.getStore()?.controller.signal; return async (r) => { if (n?.aborted) return; await runWithSpawnBroker(e, () => t(() => authProfileFileChanges.onChange?.(r))); }; } /** Retained observers must use the current Gateway's host bindings after a restart. */ function bindAuthProfileFileObservationReaders() { const e = captureFileChangePublisher(); gatewayFileChangePublisher = e; for (const t of directories.values()) t.publishChange = e; } function directoryState(e) { const t = directories.get(e); if (t) return t; const n = /* @__PURE__ */ new Map(); const r = { identities: n, presence: /* @__PURE__ */ new Map(), dirty: false, sourcePreparations: /* @__PURE__ */ new Set(), publishChange: gatewayFileChangePublisher ?? captureFileChangePublisher(), observation: observeOwnedDirectory(e, (t, i) => { if (i === "change" && t !== void 0) return; if (t === void 0 || legacyNames.has(t)) { refreshDirectory(e, r); return; } for (const i of n.keys()) if (t === path.basename(i)) { refreshDirectory(e, r); return; } }) }; directories.set(e, r); return r; } /** File facts belong to the auth owner; observation re-admits changed paths off-thread. */ function readAuthProfileDatabaseIdentity(e) { const t = directoryState(path.dirname(e)); if (!t.identities.has(e)) { const n = inspectDatabasePathIdentitySync(e); t.identities.set(e, n); } return t.identities.get(e); } /** Cold runtime preparation joins the existing file-fact owner without blocking the host. */ async function readAuthProfileDatabaseIdentityAsync(e) { const t = path.dirname(e); const n = directoryState(t); if (!n.identities.has(e)) { const r = await readDatabasePathIdentity(e); if (directories.get(t) !== n) throw new Error("Auth profile file owner retired during admission"); if (!n.identities.has(e)) n.identities.set(e, r); } return n.identities.get(e); } async function captureAuthProfileFileIdentityAsync(e) { const t = directoryState(path.dirname(e)); await readAuthProfileDatabaseIdentityAsync(e); const n = t.identities.get(e); return { identity: n, assertCurrent() { if (!t.identities.has(e) || t.identities.get(e) !== n || directories.get(path.dirname(e)) !== t) throw new Error("Auth profile database file identity changed"); } }; } /** A first owner write makes an admitted absence available to subsequent readers immediately. */ function noteAuthProfileFileWrite(e) { const t = [...directories.values()].flatMap((e) => [...e.identities].flatMap(([t, n]) => n?.key.startsWith("file:") ? [] : [{ state: e, pathname: t, identity: n }])); if (!t.length) return; const n = inspectDatabasePathIdentitySync(e); for (const { state: r, pathname: i, identity: a } of t) if (i === e || n && a?.canonicalPath === n.canonicalPath) r.identities.set(i, n); } function authProfileSourceExists(e) { if (!isMainThread || isArtifactPreservingStateRead() || getOpenClawDatabaseMaintenanceScope()) return fs.existsSync(e); const t = directoryState(path.dirname(e)); if (!t.presence.has(e)) t.presence.set(e, fs.existsSync(e)); return t.presence.get(e); } /** Seed cold runtime source facts through the existing observation owner. */ async function prepareAuthProfileLegacySources(e) { for (const t of e) { const e = path.dirname(t); const n = directoryState(e); if (!n.presence.has(t)) { const r = await stat$1(t).then(() => true, () => false); if (directories.get(e) !== n) throw new Error("Auth profile file owner retired during admission"); if (!n.presence.has(t)) n.presence.set(t, r); } } } /** Only a live read/effect scope may discover migration metadata for its captured candidates. */ function observeAuthProfileLegacySources(e, t) { const n = new Set([...e].map((e) => directoryState(path.dirname(e)))); let r = true; const observed = (e, n) => t(e, () => r && n()); for (const e of n) e.sourcePreparations.add(observed); return () => { r = false; for (const e of n) e.sourcePreparations.delete(observed); }; } /** Cold credential admission revalidates migration inputs before publishing a new owner. */ function admitAuthProfileLegacySources(e) { for (const t of new Set(e)) directoryState(path.dirname(t)).presence.set(t, fs.existsSync(t)); } /** Explicit refresh joins observation without retiring unchanged file authority. */ async function refreshAuthProfileFileState() { await Promise.all([...directories].map(([e, t]) => refreshDirectory(e, t))); } function closeAuthProfileFileState() { gatewayFileChangePublisher = void 0; for (const [e, t] of directories) retireDirectory(e, t); } //#endregion export { captureAuthProfileFileIdentityAsync as a, prepareAuthProfileLegacySources as c, refreshAuthProfileFileState as d, bindAuthProfileFileObservationReaders as i, readAuthProfileDatabaseIdentity as l, authProfileFileChanges as n, noteAuthProfileFileWrite as o, authProfileSourceExists as r, observeAuthProfileLegacySources as s, admitAuthProfileLegacySources as t, readAuthProfileDatabaseIdentityAsync as u };