UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

67 lines (66 loc) • 3.01 kB
import { s as normalizeNullableString } from "./string-coerce-CCIiB4H7.mjs"; import { n as isPostgresSqlConnection } from "./sql-connection-Beiyg5Dt.mjs"; import { b as publishSqliteDatabaseAdmission, o as getSqliteDatabaseAdmission } from "./sqlite-database-admission-DQIqro3d.mjs"; import { b as schemaAdmission, o as getAdmittedSqliteSchemaFacts } from "./sqlite-schema-facts-B1bVE8IB.mjs"; import { a as tableExists, t as classifySqliteTableReadError } from "./openclaw-state-db-schema-helpers-nImZNMq_.mjs"; //#region src/state/openclaw-agent-db-metadata.ts const agentSchemaMetadataKey = { name: "agent.schema-metadata", lifetime: "process", schemaDependent: true, read(e) { if (typeof e !== "object" || e === null || !("agentId" in e) || e.agentId !== null && typeof e.agentId !== "string" || !("role" in e) || e.role !== null && typeof e.role !== "string" || !("schemaVersion" in e) || e.schemaVersion !== null && typeof e.schemaVersion !== "number") return; return { agentId: e.agentId, role: e.role, schemaVersion: e.schemaVersion }; } }; function schemaMetadataKey(e) { return { name: `agent.schema-metadata:${e}`, read: agentSchemaMetadataKey.read }; } /** The metadata writer publishes its committed format facts without a readback. */ function publishAgentSchemaMetadata(e, t) { publishSqliteDatabaseAdmission(e, agentSchemaMetadataKey, t); const n = getAdmittedSqliteSchemaFacts(e); if (n) publishSqliteDatabaseAdmission(e, schemaMetadataKey(n.admissionId), t); } /** Read ownership metadata without loading runtime schema or migration owners. */ function readExistingAgentSchemaMeta(e) { const t = getAdmittedSqliteSchemaFacts(e); const n = t && getSqliteDatabaseAdmission(e, schemaMetadataKey(t.admissionId)); if (n) return n; const r = !t || getSqliteDatabaseAdmission(e, schemaAdmission)?.admissionId === t.admissionId ? getSqliteDatabaseAdmission(e, agentSchemaMetadataKey) : void 0; if (r) { if (t) publishSqliteDatabaseAdmission(e, schemaMetadataKey(t.admissionId), r); return r; } if (!tableExists(e, "schema_meta")) return null; let i; try { i = e.prepare("SELECT role, schema_version, agent_id FROM schema_meta WHERE meta_key = 'primary'").get(); } catch (t) { throw classifySqliteTableReadError(e, "schema_meta", [ "meta_key", "role", "schema_version", "agent_id" ], t); } if (!i) return null; const a = { agentId: normalizeNullableString(i.agent_id), role: typeof i.role === "string" ? i.role : null, schemaVersion: typeof i.schema_version === "number" ? i.schema_version : null }; const o = t && getAdmittedSqliteSchemaFacts(e) === t; if (isPostgresSqlConnection(e) || o && getSqliteDatabaseAdmission(e, schemaAdmission)?.admissionId === t.admissionId) publishAgentSchemaMetadata(e, a); else if (o) publishSqliteDatabaseAdmission(e, schemaMetadataKey(t.admissionId), a); return a; } //#endregion export { publishAgentSchemaMetadata as n, readExistingAgentSchemaMeta as r, agentSchemaMetadataKey as t };