UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 3.21 kB
import{o as e}from"./openclaw-state-read-openclaw-state-db-cache-DyomEqgV.mjs";import{C as t,S as n,u as r}from"./openclaw-state-read-sqlite-post-commit-CsXPYVOo.mjs";import{c as i}from"./openclaw-state-read-sqlite-schema-sql-BnVx4b5A.mjs";import{n as a,r as o}from"./openclaw-state-read-open-CVc8tFBD.mjs";import{a as s,p as c,r as l}from"./openclaw-state-read-kysely-sync-Bcu0Qb7F.mjs";import{o as u}from"./openclaw-state-read-openclaw-state-db-schema-helpers-QFZd8aJI.mjs";import{r as d}from"./openclaw-state-read-openclaw-state-db-handle-CTlulhMi.mjs";import{E as f,d as p,g as m}from"./openclaw-state-read-openclaw-state-db-legacy-backfills-CP_449sp.mjs";import{Q as h,ct as g,lt as _,r as v,st as y,ut as b}from"./openclaw-state-read.worker.js";function inspectPostgresGatewayOwnerLease(r,s,c){(r.location!==r.databasePath||r.snapshotRoot)&&p();let l=d(r.databasePath,{existingOnly:!0,expectedIdentity:r.expectedIdentity}),u;return n({release:()=>t(e.closeUnpublishedOpenClawStateDatabaseHandle({db:u??l,path:r.databasePath}),`Gateway owner lease reader cleanup failed`)},`PostgreSQL Gateway owner lease inspection`,()=>{let e=a(l,`identity`,!0),t=e?.[0];if(e?.length!==1||t?.engine!==`postgres`||typeof t.store_id!=`string`||typeof t.schema!=`string`||!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(t.schema))throw Error(`Invalid PostgreSQL anchor identity during Gateway owner lease inspection`);let{store_id:n,schema:r}=t,d=o.postgres.openConnection({anchor:l,input:s,schema:m,storeId:n,namespace:r,openSqlite:()=>{}});return u=d,d.runTransaction(()=>{let e=d.prepare(`SELECT version, store_id FROM ${i(r)}.openclaw_schema_meta WHERE store = 'state' LIMIT 2`).all();if(e.length!==1||e[0]?.store_id!==n||typeof e[0]?.version!=`number`||!Number.isInteger(e[0].version)||e[0].version<1)throw Error(`PostgreSQL state identity could not be verified for Gateway owner lease inspection`);return g(d.prepare(y).all(r)),c(),readGatewayOwnerLeaseRows(d,r)},void 0,!0)})}function inspectGatewayOwnerLease(e,t,r){let i=r?f():void 0;if(i)return inspectPostgresGatewayOwnerLease(e,i,t);let a=v(e.databasePath,e.location,e.expectedIdentity,e.snapshotRoot);return n({release:()=>{a.close()}},`Gateway owner lease inspection`,()=>{let e=a.database.db,i;return r&&(i=h(e)),n({release:()=>i?.()},`Gateway owner lease schema read admission`,()=>{if(t(),u(e,`state_leases`))return readGatewayOwnerLeaseRows(e)})})}function readGatewayOwnerLeaseRows(e,t){let n=s(e),i=r(e)?(t?n.withSchema(t):n).selectFrom(`state_leases`).select([`owner`,`created_at as createdAt`,`expires_at as expiresAt`,`heartbeat_at as heartbeatAt`,`payload_json as payloadJson`]).where(`scope`,`=`,b.scope).where(`lease_key`,`=`,b.key).limit(2):{compile:()=>c`SELECT owner, created_at AS createdAt, expires_at AS expiresAt, heartbeat_at AS heartbeatAt, payload_json AS payloadJson FROM ${c`state_leases NOT INDEXED`} WHERE scope = ${b.scope} AND lease_key = ${b.key} LIMIT 2`.compile(n)},{rows:a}=l(e,i);if(a.length>1)throw Error(`Gateway owner lease identity is ambiguous during maintenance; stop OpenClaw processes and restore a verified state backup before retrying`);return _(a[0])}export{inspectGatewayOwnerLease};