openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
44 lines (43 loc) • 2.33 kB
JavaScript
import { r as resolveStateDir } from "./state-dir-CfAW6S2K.mjs";
import { t as assertExistingDatabaseIdentity } from "./sqlite-worker-identity-B_aK_kcL.mjs";
import { t as isPromiseLike } from "./promise-like-D7-l5Fsp.mjs";
import { _ as readOpenClawAgentDatabaseIdentity } from "./openclaw-agent-db-validation-cache-D5sa3PLF.mjs";
import { r as cloneEnvWithPlatformSemantics } from "./config-env-vars-oFV7TqIg.mjs";
import { c as resolveOpenClawAgentSqlitePath } from "./openclaw-agent-db.paths-DiWMV5LG.mjs";
import { y as retainAgentDatabase } from "./openclaw-agent-db-lifecycle-BmHpWusP.mjs";
import { o as getOpenClawAgentDatabaseIfOpen, p as withOpenClawAgentDatabaseAsync } from "./openclaw-agent-db-CFSa_Qik.mjs";
import { r as runOpenClawAgentWriteAdmission } from "./openclaw-agent-write-admission-xWSWPO-q.mjs";
//#region src/state/openclaw-agent-db-write.ts
/** Admit a synchronous mutation without blocking a reclamation worker's parent callback. */
function withOpenClawAgentDatabaseWrite(inputOptions, operation, expectedDatabase) {
const options = {
...inputOptions,
env: cloneEnvWithPlatformSemantics(inputOptions.env ?? process.env)
};
options.env.OPENCLAW_STATE_DIR = resolveStateDir(options.env);
const pathname = resolveOpenClawAgentSqlitePath(options);
options.path = pathname;
const run = async (database) => {
const { identity, birthtime } = readOpenClawAgentDatabaseIdentity(database);
if (typeof identity === "string") assertExistingDatabaseIdentity(pathname, `file:${identity}`, birthtime);
const result = operation(database);
if (isPromiseLike(result)) {
await result;
throw new Error("Agent database write callbacks must remain synchronous");
}
return result;
};
return runOpenClawAgentWriteAdmission(options, async (_identity, assertCurrent) => {
if (!expectedDatabase) return await withOpenClawAgentDatabaseAsync(options, run, assertCurrent);
const database = getOpenClawAgentDatabaseIfOpen(options);
if (!database || database.db !== expectedDatabase || !expectedDatabase.isOpen) throw new Error("Borrowed agent database closed or changed before write admission");
const release = retainAgentDatabase(expectedDatabase);
try {
return await run(database);
} finally {
release();
}
}, true);
}
//#endregion
export { withOpenClawAgentDatabaseWrite as t };