openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
58 lines (57 loc) • 4.19 kB
JavaScript
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import { n as isPostgresSqlConnection } from "./sql-connection-Beiyg5Dt.mjs";
import { t as OPENCLAW_AGENT_SCHEMA_VERSION } from "./openclaw-schema-versions-E_10NoI6.mjs";
import { o as getAdmittedSqliteSchemaFacts, w as getPostgresPhysicalFacts } from "./sqlite-schema-facts-B1bVE8IB.mjs";
import { c as SqliteSchemaMismatchError } from "./openclaw-state-db-schema-helpers-nImZNMq_.mjs";
import { n as createNewerSqliteSchemaVersionError, o as readSqliteUserVersion, s as readSqliteWriterAppVersion } from "./sqlite-user-version-C02a4KwH.mjs";
import { n as StartupMaintenanceRequiredError } from "./startup-maintenance-required-DEn4Krys.mjs";
import "./openclaw-agent-db-metadata-C3YvwHLh.mjs";
import { n as OpenClawAgentDatabaseMediaMigrationRequiredError } from "./session-metadata-unavailable-error-ClU33Nog.mjs";
import "./openclaw-agent-db-contract-aCH9b8Uc.mjs";
//#region src/state/openclaw-agent-db-schema-read.ts
function readAgentDatabaseSchemaVersion(e) {
if (isPostgresSqlConnection(e)) {
const t = getPostgresPhysicalFacts(e);
if (!t) throw new Error("Agent database has no PostgreSQL schema admission");
return t.version;
}
return getAdmittedSqliteSchemaFacts(e)?.userVersion ?? readSqliteUserVersion(e);
}
function assertSupportedAgentSchemaVersion(e, t, n = readAgentDatabaseSchemaVersion(e)) {
if (n > 38) throw createNewerSqliteSchemaVersionError("OpenClaw agent database", t, n, 38, readSqliteWriterAppVersion(e));
return n;
}
function hasAgentDatabaseApplicationSchema(e) {
if (isPostgresSqlConnection(e)) return Boolean(getPostgresPhysicalFacts(e)?.catalog.some((e) => e.kind === "table"));
const t = getAdmittedSqliteSchemaFacts(e);
if (t) {
for (const e of [
t.tables,
t.views,
t.indexes,
t.triggers.keys()
]) for (const t of e) if (!t.startsWith("sqlite_")) return true;
return false;
}
return Boolean(e.prepare("SELECT 1 FROM sqlite_master WHERE substr(name, 1, 7) <> 'sqlite_' LIMIT 1").get());
}
/** Persistence gates consume the catalog published by admission or schema maintenance. */
function assertCanonicalAgentPersistenceVersion(e, t, n = readAgentDatabaseSchemaVersion(e)) {
if (getAdmittedSqliteSchemaFacts(e)?.tables.has("openclaw_engine_anchor")) throw new Error("This agent store lives in PostgreSQL; set database.engine to postgres and configure database.postgres.connection.");
const r = n === 0 && !hasAgentDatabaseApplicationSchema(e);
if (n < 17 && !r) throw new OpenClawAgentDatabaseMediaMigrationRequiredError(t, n);
if (n < 38 && !r) throw new StartupMaintenanceRequiredError("state-migrations", `OpenClaw agent database ${t} uses schema version ${n}; stop active agents and run openclaw doctor --fix to upgrade it to schema version ${38} before using it.`);
}
function assertExistingAgentSchemaOwner(e, t, n) {
if (!e) return;
if (e.role !== "agent") throw new SqliteSchemaMismatchError(`OpenClaw agent database ${n} has schema role ${e.role ?? "unknown"}; expected agent. Run openclaw doctor --fix to inspect and repair its ownership.`);
if (!e.agentId) throw new SqliteSchemaMismatchError(`OpenClaw agent database ${n} has no agent owner. Run openclaw doctor --fix to inspect and repair its ownership.`);
if (normalizeAgentId(e.agentId) !== t) throw new SqliteSchemaMismatchError(`OpenClaw agent database ${n} belongs to agent ${e.agentId}; requested agent ${t}.`);
}
function assertCurrentAgentSchemaMetadata(e, t, n, r = 38) {
if (!e) throw new SqliteSchemaMismatchError(`OpenClaw agent database ${n} has no schema ownership metadata. Run openclaw doctor --fix to inspect and repair its ownership.`);
assertExistingAgentSchemaOwner(e, t, n);
if (e.schemaVersion !== r) throw new SqliteSchemaMismatchError(`OpenClaw agent database ${n} metadata schema version ${e.schemaVersion ?? "invalid"} does not match ${r}; run openclaw doctor --fix before using it.`);
}
//#endregion
export { hasAgentDatabaseApplicationSchema as a, assertSupportedAgentSchemaVersion as i, assertCurrentAgentSchemaMetadata as n, readAgentDatabaseSchemaVersion as o, assertExistingAgentSchemaOwner as r, assertCanonicalAgentPersistenceVersion as t };