UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

70 lines (69 loc) • 3.13 kB
import { d as readDatabasePathIdentitySync } from "./sqlite-worker-identity-BRNRpRyc.mjs"; import { n as normalizeAgentId } from "./agent-id-D1-Q6vR4.mjs"; import "./session-key-DY3MOX4N.mjs"; import { n as createOpenClawAgentDatabaseClaim } from "./openclaw-agent-db-identity-BlmER9-X.mjs"; import { c as registerOpenClawAgentDatabaseSyncResource } from "./openclaw-agent-db-resources-Cm-sH7kg.mjs"; import { i as isIncognitoOpenClawAgentSqlitePath } from "./openclaw-agent-db.paths-BbmZlEP5.mjs"; import { S as retainAgentDatabase, m as isIncognitoOpenClawAgentDatabase, t as cache } from "./openclaw-agent-db-lifecycle-C9dal1XJ.mjs"; import { r as captureSessionStoreReadCandidate } from "./session-store-read-candidates-CXD-JYGz.mjs"; import path from "node:path"; //#region src/gateway/control-ui-session-pr-source.ts /** Retain source lifetime through the caller's last assertion and publication. */ async function withControlUiSessionPrSource(source, operation) { const target = { agentId: normalizeAgentId(source.agentId), path: path.resolve(source.path) }; const unregister = []; let active = true; let releaseNative = () => {}; const changed = () => /* @__PURE__ */ new Error("Session PR source changed or closed. Retry the request."); try { let paths; let assertSource; let sourceIdentity; if (isIncognitoOpenClawAgentSqlitePath(target.path, target)) { const database = cache.databases.get(target.path); if (!database?.db.isOpen || database.agentId !== target.agentId || !isIncognitoOpenClawAgentDatabase(database)) throw changed(); releaseNative = retainAgentDatabase(database.db); const claim = createOpenClawAgentDatabaseClaim(database, releaseNative); releaseNative = claim.release; sourceIdentity = `incognito:${claim.incarnation}`; paths = [target.path]; assertSource = () => { claim.assertCurrent(); if (cache.databases.get(target.path) !== database || !isIncognitoOpenClawAgentDatabase(database)) throw changed(); }; } else { const candidate = captureSessionStoreReadCandidate(target.path); const identity = readDatabasePathIdentitySync(candidate.path); if (!identity.key.startsWith("file:") || identity.canonicalPath !== candidate.physicalPath) throw changed(); sourceIdentity = identity.key; paths = [.../* @__PURE__ */ new Set([candidate.path, candidate.physicalPath])]; assertSource = () => { const current = readDatabasePathIdentitySync(candidate.path); if (current.key !== identity.key || current.canonicalPath !== candidate.physicalPath) throw changed(); }; } for (const pathname of paths) unregister.push(registerOpenClawAgentDatabaseSyncResource({ agentId: target.agentId, path: pathname, revoke: () => { active = false; }, close: () => releaseNative() })); const assertCurrent = () => { if (!active) throw changed(); assertSource(); }; assertCurrent(); return await operation(assertCurrent, sourceIdentity); } finally { active = false; for (const release of unregister.toReversed()) release(); releaseNative(); } } //#endregion export { withControlUiSessionPrSource as t };