UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.01 kB
import{n as isPostgresSqlConnection}from"./sql-connection-D2qSU3Um.mjs";import{n as createNewerSqliteSchemaVersionError,o as readSqliteUserVersion,s as readSqliteWriterAppVersion}from"./sqlite-user-version-B1PC_YBu.mjs";import"./openclaw-state-db-contract-lz-66XzZ.mjs";import{a as getNodeSqliteKysely,n as createSqliteQueryCache,s as prepareSqliteQuerySync}from"./kysely-sync-BHQlXmVT.mjs";import{g as getPostgresPhysicalFacts,r as getAdmittedSqliteSchemaFacts,t as admitSqliteSchema}from"./sqlite-schema-facts-x2rWNNBl.mjs";import{t as SqliteSchemaMismatchError}from"./sqlite-schema-issues-CTIzzA67.mjs";import{i as tableExists,m as rememberStateSchemaVersionAdmission,u as getStateSchemaVersionAdmission}from"./openclaw-state-db-schema-helpers-BKB_fYhN.mjs";import{r as normalizeOpenClawStateSchemaReadError}from"./openclaw-state-db-schema-migration-required-7GgH-T_Y.mjs"; //!src/state/openclaw-state-db-schema-version.ts const CONTENT_VERSION_KEY=`state.schema.contentVersion`;var PostgresSchemaAdmissionUnavailableError=class extends Error{};function requireStatePostgresPhysicalFacts(e){const t=getPostgresPhysicalFacts(e);if(!t)throw new PostgresSchemaAdmissionUnavailableError(`Missing PostgreSQL physical schema admission`);return t}const contentVersionQuery=createSqliteQueryCache(e=>prepareSqliteQuerySync(e,()=>getNodeSqliteKysely(e).selectFrom(`config_machine_state`).select(`value_json`).where(`state_key`,`=`,CONTENT_VERSION_KEY)));function readStateSchemaContentVersion(e,t,n=readStateSchemaContentVersionRow,r){if(isPostgresSqlConnection(e)){const n=getStateSchemaVersionAdmission(e);if(!n)throw new PostgresSchemaAdmissionUnavailableError(`Missing PostgreSQL state schema admission; use the matching OpenClaw version or restore the matching PostgreSQL schema and anchor.`);return Math.max(t??n.userVersion,n.contentVersion)}const i=getStateSchemaVersionAdmission(e);if(i)return Math.max(t??i.userVersion,i.contentVersion);const a=r??getAdmittedSqliteSchemaFacts(e)?.userVersion??readSqliteUserVersion(e);const o=parseStateSchemaContentVersion(tableExists(e,`config_machine_state`)?n(e)?.value_json:void 0);rememberStateSchemaVersionAdmission(e,{userVersion:a,contentVersion:o});return Math.max(t??a,o)}function readStateSchemaContentVersionRow(e){return contentVersionQuery(e)().rows[0]}function parseStateSchemaContentVersion(e){if(e===void 0)return 0;let t;try{t=e===null?null:JSON.parse(e)}catch(e){throw new SqliteSchemaMismatchError(`Invalid shared state schema content version in ${CONTENT_VERSION_KEY}.`,{cause:e})}if(typeof t!==`number`||!Number.isSafeInteger(t)||t<0)throw new SqliteSchemaMismatchError(`Invalid shared state schema content version in ${CONTENT_VERSION_KEY}.`);return t}function admitStateSchemaFacts(e,t,n=`runtime`){if(isPostgresSqlConnection(e)){requireStatePostgresPhysicalFacts(e);return}try{admitSqliteSchema(e,r=>assertSupportedStateSchemaVersion(e,t,{userVersion:r,contentVersion:r},void 0,n))}catch(r){let i;try{i=readSqliteUserVersion(e)}catch{throw normalizeOpenClawStateSchemaReadError(r,t)}if(i>40)throw createNewerSqliteSchemaVersionError(`OpenClaw state database`,t,i,40,readSqliteWriterAppVersion(e),n);throw normalizeOpenClawStateSchemaReadError(r,t)}}function assertSupportedStateSchemaVersion(e,t,n,r,i=`runtime`){if(isPostgresSqlConnection(e))return readStateSchemaContentVersion(e);try{const a=n?.userVersion??getStateSchemaVersionAdmission(e)?.userVersion??getAdmittedSqliteSchemaFacts(e)?.userVersion??readSqliteUserVersion(e);const o=n?.contentVersion??(a>40?a:readStateSchemaContentVersion(e,a,r,a));if(o>40)throw createNewerSqliteSchemaVersionError(`OpenClaw state database`,t,o,40,readSqliteWriterAppVersion(e),i);return a}catch(e){throw normalizeOpenClawStateSchemaReadError(e,t)}}export{parseStateSchemaContentVersion as a,assertSupportedStateSchemaVersion as i,PostgresSchemaAdmissionUnavailableError as n,readStateSchemaContentVersion as o,admitStateSchemaFacts as r,requireStatePostgresPhysicalFacts as s,CONTENT_VERSION_KEY as t};