openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
104 lines (103 loc) • 4.17 kB
JavaScript
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { c as resolveOpenClawAgentSqlitePath, r as isIncognitoOpenClawAgentSqlitePath } from "./openclaw-agent-db.paths-DJxv80ws.mjs";
import { r as sqlPublicationAnchor } from "./sql-connection-Beiyg5Dt.mjs";
import { u as registerNodeSqliteDisposeCallback } from "./kysely-sync-cache-state-Bb1gOqQO.mjs";
import { h as runSqliteReadOperationSync } from "./sqlite-schema-facts-B1bVE8IB.mjs";
import { n as isArtifactPreservingStateRead } from "./artifact-preserving-state-reads-BfhmBr-V.mjs";
import { n as createOpenClawAgentDatabaseClaim } from "./openclaw-agent-db-identity-C8MGU8lp.mjs";
import { i as retainAgentDatabase, n as getOpenClawAgentDatabaseIfOpen } from "./openclaw-agent-db-borrower-BrziJlyo.mjs";
import { a as withFreshOpenClawAgentDatabaseReadOnly, i as readOpenClawAgentDatabaseSnapshot, n as openOpenClawAgentDatabaseReadOnly, r as readOpenClawAgentDatabase } from "./openclaw-agent-db-readonly-open-BzFUagBt.mjs";
import { a as withScopedOpenClawAgentDatabaseReadOnly, i as retainCachedOpenClawAgentDatabaseReadOnly, t as OpenClawAgentDatabaseReadOnlyScope } from "./openclaw-agent-db-readonly-scope-B8DxQsJ2.mjs";
//#region src/state/openclaw-agent-db-readonly.ts
const companions = resolveGlobalSingleton(Symbol.for("openclaw.agentDatabaseReadOnlyCompanions"), () => /* @__PURE__ */ new WeakMap());
/**
* Look up a process-held handle without adopting writer-side failures.
*
* Read-only reads are meant to survive a latched open failure or an ownership
* mismatch that only the writable lifecycle cares about; those callers fall
* back to a fresh connection, which reports the precise reason.
*/
function findOpenAgentDatabase(e) {
try {
return getOpenClawAgentDatabaseIfOpen(e);
} catch {
return;
}
}
/** Retain an existing store across awaits without materializing a writable database. */
function retainOpenClawAgentDatabaseReadOnly(e) {
if (isArtifactPreservingStateRead("agent", resolveOpenClawAgentSqlitePath(e))) {
const t = openOpenClawAgentDatabaseReadOnly(e);
return t.found ? {
found: true,
database: t.database,
claim: createOpenClawAgentDatabaseClaim(t.database, t.database.close)
} : t;
}
const t = findOpenAgentDatabase(e);
if (t && !t.db.isTransaction) return {
found: true,
database: t,
claim: createOpenClawAgentDatabaseClaim(t, retainAgentDatabase(t.db))
};
const n = normalizeAgentId(e.agentId);
const r = resolveOpenClawAgentSqlitePath({
...e,
agentId: n
});
return retainCachedOpenClawAgentDatabaseReadOnly({
...e,
agentId: n,
path: r
});
}
/** Read agent state without creating, registering, migrating, or joining its writable lifecycle. */
function withOpenClawAgentDatabaseReadOnly(e, t, n = {}) {
const r = normalizeAgentId(t.agentId);
const i = resolveOpenClawAgentSqlitePath({
...t,
agentId: r
});
if (isArtifactPreservingStateRead("agent", i)) return withFreshOpenClawAgentDatabaseReadOnly(e, t, n);
if (isIncognitoOpenClawAgentSqlitePath(i, {
agentId: r,
env: t.env
})) return {
found: false,
reason: "database-missing"
};
const a = n.allowExtension ? void 0 : findOpenAgentDatabase({
...t,
agentId: r
});
if (a?.db.isTransaction) {
/** Keep committed reads separate from the writer using the ordinary reader lifecycle. */
const i = {
...t,
agentId: r
};
let o = companions.get(a.db);
if (!o) {
o = new OpenClawAgentDatabaseReadOnlyScope(true);
const e = o;
registerNodeSqliteDisposeCallback(sqlPublicationAnchor(a.db), () => e.close());
companions.set(a.db, o);
}
const s = o;
return s.run({
agentId: a.agentId,
path: a.path
}, () => s.read(e, i, n));
}
if (!a) return withScopedOpenClawAgentDatabaseReadOnly(e, {
...t,
agentId: r,
path: i
}, n);
if (n.snapshot) return readOpenClawAgentDatabaseSnapshot(a, e);
return runSqliteReadOperationSync(a.db, () => readOpenClawAgentDatabase(a, e));
}
//#endregion
export { withOpenClawAgentDatabaseReadOnly as n, retainOpenClawAgentDatabaseReadOnly as t };