UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 3.92 kB
import{r as resolveStateDir}from"./state-dir-BwIClCab.mjs";import"./paths-D_FOvGon.mjs";import{n as formatErrorMessageWithCode}from"./errors-D37BJTfI.mjs";import{n as OPENCLAW_STATE_SCHEMA_VERSION}from"./openclaw-schema-versions-rhFQpVhP.mjs";import{t as _usingCtx}from"./usingCtx-CUVyQ_FT.mjs";import{M as postgresStoreNamespace}from"./sqlite-schema-facts-CWOeTpUB.mjs";import"./openclaw-state-db-contract-G6fQNIi2.mjs";import{Jn as string,lr as uuid}from"./schemas-DuCQi-Qb.mjs";import{n as acquireStateDatabaseSchemaLease}from"./gateway-state-owner-D_klysuh.mjs";import{s as resolveOpenClawStateSqlitePath}from"./openclaw-state-db.paths-CbgXfEg1.mjs";import{t as getDatabaseEngineInput}from"./context-DZjni16e.mjs";import{l as closeOpenClawStateDatabaseByPathAsync}from"./openclaw-state-db-cache-uycu-8uy.mjs";import{o as openExistingStatePostgresConnection}from"./openclaw-state-db-engine-DiCia3Ut.mjs";import{o as readStateSchemaContentVersion}from"./openclaw-state-db-schema-version-DreEd78i.mjs";import{t as readBackupEngine}from"./physical-store-Dt80uUi6.mjs";import{d as withOpenClawStateStartupMigrationCheckpointDatabase}from"./openclaw-state-db-BlgxYkYw.mjs";import{a as assertDatabasePortStartupAllowed}from"./database-port-recovery-D6teiUHA.mjs";import{i as withPostgresConnection}from"./postgres-store-metadata-BAneIabP.mjs";import{i as readConfigSnapshotForEngineSelection}from"./config-preflight-snapshot-CTe1x93o.mjs"; //!src/commands/database-prepare-state.ts var StatePreparationError=class extends Error{};async function prepareDatabaseState(e){const t=uuid().parse(e.expectedStoreId);const n=string().regex(/^[a-z_][a-z0-9_]*$/u).parse(e.expectedNamespace);let r=`core-configuration`;try{try{var i=_usingCtx();const e=await readConfigSnapshotForEngineSelection();const a=getDatabaseEngineInput(`state`);if(!e.valid||!a)throw new StatePreparationError(`Shared-state preparation requires valid PostgreSQL configuration.`);const o=resolveOpenClawStateSqlitePath(process.env);assertDatabasePortStartupAllowed(resolveStateDir());if(postgresStoreNamespace(a.schemaPrefix,`state`,t)!==n)throw new StatePreparationError(`Expected namespace does not match the configured shared-state identity.`);r=`schema-custody`;const s=acquireStateDatabaseSchemaLease(o,{busyTimeoutMs:0});const c=i.a({...s,async[Symbol.asyncDispose](){try{await closeOpenClawStateDatabaseByPathAsync(o)}catch{throw new StatePreparationError(`Shared-state resources did not close; ownership remains retained.`)}s.release()}});c.assertCurrent();r=`anchor-validation`;const l=readBackupEngine(o);if(l.engine!==`postgres`||l.storeId!==t||l.namespace!==n)throw new StatePreparationError(`Shared-state anchor does not match the expected store and namespace.`);r=`namespace-inspection`;const u=await withPostgresConnection(a,async e=>{return(await e.query(`SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname = $1`,[n])).rows.length!==0});c.assertCurrent();if(!u){r=`writable-bootstrap`;c.run(()=>withOpenClawStateStartupMigrationCheckpointDatabase(()=>{},{env:process.env,sealOnClose:true}))}r=`ready-validation`;c.run(()=>{try{var e=_usingCtx();const t=openExistingStatePostgresConnection(o);if(!t)throw new StatePreparationError(`Matching PostgreSQL shared state is unavailable.`);const n=e.u({database:t.database,[Symbol.dispose](){if(!t.close())throw new StatePreparationError(`Shared-state reader did not close.`)}});if(readStateSchemaContentVersion(n.database.db)!==40)throw new StatePreparationError(`Shared state is not ready for this installed version.`)}catch(t){e.e=t}finally{e.d()}});return{ok:true,status:u?`ready`:`prepared`,path:o,storeId:t,namespace:n,schemaVersion:40}}catch(e){i.e=e}finally{await i.d()}}catch(e){if(e instanceof StatePreparationError)throw e;const t=formatErrorMessageWithCode(e).slice(0,2048);throw new StatePreparationError(`Shared-state preparation ${r} failed: ${t}`)}}export{prepareDatabaseState};