openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
50 lines (49 loc) • 3.34 kB
JavaScript
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { n as isPostgresSqlConnection } from "./sql-connection-Beiyg5Dt.mjs";
import { a as executeSqliteQueryTakeFirstSync, i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs";
import { D as requireNativeDatabaseFile, o as getAdmittedSqliteSchemaFacts } from "./sqlite-schema-facts-B1bVE8IB.mjs";
import { a as tableExists } from "./openclaw-state-db-schema-helpers-nImZNMq_.mjs";
import { r as getStateRuntimeSchemaAdmission } from "./openclaw-state-db-admission-Bw1Mtc4x.mjs";
import { a as resolveOpenClawRegisteredAgentDatabasePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs";
import { i as detectOpenClawStateDatabaseSchemaMigrationsFromDatabase, n as assertCanonicalAgentDatabasesPrimaryKey, t as STATE_SCHEMA_MIGRATION_READ_TABLES } from "./openclaw-state-db-schema-repair-aWqRraBE.mjs";
//#region src/state/openclaw-agent-db-registry.read.ts
const readableSchemas = /* @__PURE__ */ new WeakSet();
/** Read durable registrations from an already opened live or captured database. */
function readOpenClawAgentDatabaseRegistryRows(e, t) {
const n = getNodeSqliteKysely(e);
const r = getAdmittedSqliteSchemaFacts(e);
const i = isPostgresSqlConnection(e) ? tableExists(e, "agent_databases") ? { type: "table" } : void 0 : r ? r.tables.has("agent_databases") ? { type: "table" } : r.views.has("agent_databases") || r.indexes.has("agent_databases") || r.triggers.has("agent_databases") ? { type: "invalid" } : void 0 : executeSqliteQueryTakeFirstSync(e, n.selectFrom("sqlite_master").select("type").where("name", "=", "agent_databases"));
if (!i) return [];
if (i.type !== "table") throw new Error(`OpenClaw state database ${t} has an invalid agent registry.`);
return executeSqliteQuerySync(e, n.selectFrom("agent_databases").selectAll().orderBy("agent_id", "asc").orderBy("path", "asc")).rows;
}
function readAgentDatabasePreflightTargets(e, t) {
return readOpenClawAgentDatabaseRegistryRows(e, t).flatMap((e) => typeof e.agent_id === "string" && typeof e.path === "string" ? [{
agentId: e.agent_id,
path: resolveOpenClawRegisteredAgentDatabasePath(t, e.path)
}] : []);
}
function readRegisteredAgentDatabaseRows(e, t, n) {
if (isPostgresSqlConnection(e)) {
if (n) requireNativeDatabaseFile("postgres", "file-snapshot");
} else if (!getStateRuntimeSchemaAdmission(e)) {
if (n) assertCanonicalAgentDatabasesPrimaryKey(e, t);
else {
const n = getAdmittedSqliteSchemaFacts(e);
if (!n || !readableSchemas.has(n)) {
if (detectOpenClawStateDatabaseSchemaMigrationsFromDatabase(e, t).some(({ kind: e }) => STATE_SCHEMA_MIGRATION_READ_TABLES[e].includes("agent_databases"))) throw new Error(`OpenClaw state database ${t} has a legacy agent database registry schema; run openclaw doctor --fix to migrate it.`);
if (n) readableSchemas.add(n);
}
}
}
return readOpenClawAgentDatabaseRegistryRows(e, t).map((e) => ({
agentId: normalizeAgentId(e.agent_id),
path: resolveOpenClawRegisteredAgentDatabasePath(t, e.path),
schemaVersion: e.schema_version,
lastSeenAt: e.last_seen_at,
sizeBytes: e.size_bytes
}));
}
//#endregion
export { readOpenClawAgentDatabaseRegistryRows as n, readRegisteredAgentDatabaseRows as r, readAgentDatabasePreflightTargets as t };