openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
127 lines (126 loc) • 5.53 kB
JavaScript
import { n as resolveIdentityPathViaExistingAncestorSync } from "./boundary-path-C2gXEiXe.mjs";
import { a as isSameOpenClawAgentDatabasePath, i as isPersistentOpenClawAgentDatabasePath } from "./openclaw-agent-db.paths-DJxv80ws.mjs";
import { t as OPENCLAW_AGENT_SCHEMA_VERSION } from "./openclaw-schema-versions-E_10NoI6.mjs";
import { s as stageSqliteTransactionState } from "./sqlite-post-commit-BrUVQObC.mjs";
import { i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs";
import { a as resolveOpenClawRegisteredAgentDatabasePath, i as resolveOpenClawAgentDatabaseStoredPath } from "./openclaw-state-db.paths-R3ArRCcz.mjs";
import { w as requireOpenClawStateDatabaseIdentity } from "./openclaw-state-db-cache-CkwtTqzF.mjs";
import "./openclaw-agent-db-contract-aCH9b8Uc.mjs";
import { l as runOpenClawStateWriteTransaction } from "./openclaw-state-db-ARhwdPJg.mjs";
import { d as prepareAgentDeletionPathFence, n as assertAgentDeletionPathFence } from "./agent-deletion-journal-DgNbVVSp.mjs";
import { m as recordOpenClawAgentDatabaseRegistryMutation, n as emitOpenClawAgentDatabaseRegistryChange } from "./openclaw-agent-db-registry-listing-CbXBL50a.mjs";
import { h as invalidateOpenClawAgentDatabaseValidationsForAgent, m as invalidateOpenClawAgentDatabaseValidation, t as adoptOpenClawAgentDatabaseSchema, y as setOpenClawAgentDatabaseValidation } from "./openclaw-agent-db-validation-cache-lnjWuypr.mjs";
import { statSync } from "node:fs";
import path from "node:path";
//#region src/state/openclaw-agent-db-registry.ts
function resolveRegisteredAgentDatabaseStoredPath(e, t) {
const n = resolveOpenClawAgentDatabaseStoredPath(e.path, t.path);
if (t.path !== resolveIdentityPathViaExistingAncestorSync(t.path)) return n;
const r = getNodeSqliteKysely(e.db);
const { rows: i } = executeSqliteQuerySync(e.db, r.selectFrom("agent_databases").select("path").where("agent_id", "=", t.agentId));
return i.some((e) => e.path === n) ? n : i.find((n) => isSameOpenClawAgentDatabasePath(resolveOpenClawRegisteredAgentDatabasePath(e.path, n.path), t.path))?.path ?? n;
}
function registerOpenClawAgentDatabase(e, t) {
if (!isPersistentOpenClawAgentDatabasePath(e.path, e.env)) {
if (e.admittedDb) setOpenClawAgentDatabaseValidation({
...e,
db: e.admittedDb
});
return;
}
const n = prepareAgentDeletionPathFence({
agentId: e.agentId,
path: e.path
}, { env: e.env });
let r = null;
try {
r = statSync(e.path).size;
} catch {
r = null;
}
const i = Date.now();
t?.starting?.();
runOpenClawStateWriteTransaction((a) => {
assertAgentDeletionPathFence(a, n);
const o = resolveRegisteredAgentDatabaseStoredPath(a, e);
const s = getNodeSqliteKysely(a.db);
executeSqliteQuerySync(a.db, s.insertInto("agent_databases").values({
agent_id: e.agentId,
path: o,
schema_version: e.schemaVersion ?? 38,
last_seen_at: i,
size_bytes: r
}).onConflict((t) => t.columns(["agent_id", "path"]).doUpdateSet({
schema_version: e.schemaVersion ?? 38,
last_seen_at: i,
size_bytes: r
})));
recordOpenClawAgentDatabaseRegistryMutation(a, "upsert", [e]);
const c = t?.committed;
if (c) {
const t = Object.freeze({
agentId: e.agentId,
agentPath: e.path,
stateDatabasePath: a.path,
stateDatabaseIdentity: requireOpenClawStateDatabaseIdentity(a).key
});
if (!stageSqliteTransactionState(a.db, {
stage() {},
rollback() {},
commit: () => c(t)
})) throw new Error("Agent registration requires its canonical transaction publication scope");
}
emitOpenClawAgentDatabaseRegistryChange(e.agentId, a.db);
}, {
env: e.env,
initializationAgentPaths: [e.path]
});
if (e.admittedDb) {
const t = {
...e,
db: e.admittedDb
};
if (!adoptOpenClawAgentDatabaseSchema(t)) setOpenClawAgentDatabaseValidation(t);
} else invalidateOpenClawAgentDatabaseValidation(e.path);
}
function unregisterOpenClawAgentDatabase(e) {
runOpenClawStateWriteTransaction((t) => {
const n = resolveRegisteredAgentDatabaseStoredPath(t, e);
const r = [.../* @__PURE__ */ new Set([
n,
e.path,
path.resolve(e.path)
])];
const i = getNodeSqliteKysely(t.db);
executeSqliteQuerySync(t.db, i.deleteFrom("agent_databases").where("agent_id", "=", e.agentId).where("path", "in", r));
recordOpenClawAgentDatabaseRegistryMutation(t, "remove", [e]);
emitOpenClawAgentDatabaseRegistryChange(e.agentId, t.db);
}, {
env: e.env,
initializationAgentPaths: [e.path]
});
invalidateOpenClawAgentDatabaseValidation(e.path);
}
/** Remove every durable database registration owned by a deleted agent. */
function unregisterOpenClawAgentDatabases(e) {
const t = {
env: e.env,
...e.database ? {
database: e.database,
path: e.database.path
} : {}
};
const n = runOpenClawStateWriteTransaction((t) => {
const n = getNodeSqliteKysely(t.db);
const r = executeSqliteQuerySync(t.db, n.deleteFrom("agent_databases").where("agent_id", "=", e.agentId).returning("path"));
recordOpenClawAgentDatabaseRegistryMutation(t, "remove", r.rows.map((n) => ({
agentId: e.agentId,
path: resolveOpenClawRegisteredAgentDatabasePath(t.path, n.path)
})));
emitOpenClawAgentDatabaseRegistryChange(e.agentId, t.db);
return r.rows.map((e) => resolveOpenClawRegisteredAgentDatabasePath(t.path, e.path));
}, t);
invalidateOpenClawAgentDatabaseValidationsForAgent(e.agentId, n);
}
//#endregion
export { unregisterOpenClawAgentDatabase as n, unregisterOpenClawAgentDatabases as r, registerOpenClawAgentDatabase as t };