openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
50 lines (49 loc) • 2.61 kB
JavaScript
import { r as resolveStateDir } from "./state-dir-CYkBUd_G.mjs";
import { r as assertExistingDatabaseIdentity } from "./sqlite-worker-identity-BRNRpRyc.mjs";
import { t as isPromiseLike } from "./promise-like-D7-l5Fsp.mjs";
import { a as readOpenClawAgentDatabaseIdentity } from "./openclaw-agent-db-identity-BlmER9-X.mjs";
import { l as resolveOpenClawAgentSqlitePath } from "./openclaw-agent-db.paths-BbmZlEP5.mjs";
import { r as cloneEnvWithPlatformSemantics } from "./config-env-vars-BzUE_dOh.mjs";
import { d as withOpenClawAgentDatabaseRuntime, i as getOpenClawAgentDatabaseIfOpen } from "./openclaw-agent-db-DWI9Bgur.mjs";
import { S as retainAgentDatabase } from "./openclaw-agent-db-lifecycle-C9dal1XJ.mjs";
import { n as runOpenClawAgentWriteAdmission } from "./openclaw-agent-write-admission-CXJiE5VB.mjs";
import { t as waitForAgentDatabasePreparation } from "./agent-database-preparation-context-DK375nya.mjs";
//#region src/state/openclaw-agent-db-write.ts
/** Admit a synchronous mutation without blocking a reclamation worker's parent callback. */
async function withOpenClawAgentDatabaseWrite(inputOptions, operation, expectedDatabase, signal) {
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 preparation = waitForAgentDatabasePreparation(options.agentId, {
env: options.env,
signal
});
if (preparation) await preparation;
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 withOpenClawAgentDatabaseRuntime(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, void 0, signal);
}
//#endregion
export { withOpenClawAgentDatabaseWrite as t };