openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 7.03 kB
JavaScript
import{u as e}from"./sqlite-readonly-subsystem-C-VJT-xk.mjs";import{Ot as t,Tt as n}from"./sqlite-readonly-runtime-process-manifest-CLhsN5hb.mjs";import{r}from"./sqlite-readonly-sqlite-wal-CjNCU3qS.mjs";import{_ as i,o as a}from"./sqlite-readonly-sqlite-user-version-DDTE9T44.mjs";import{Ct as o,Tn as s,_n as c,a as l,c as u,d,gn as f,it as p,o as m,pn as h}from"./sqlite-readonly-node-sqlite-u3RAlsBE.mjs";import{v as g}from"./sqlite-readonly-location.worker.js";import{i as _,n as v,r as y}from"./sqlite-readonly-agent-database-readers-DdK8rgO4.mjs";import{i as b,n as x,r as S}from"./sqlite-readonly-kysely-sync-DYhhbB39.mjs";import"./sqlite-readonly-openclaw-agent-db-contract-Bp5NJ0My.mjs";import C from"node:path";import w from"node:fs";function readCacheToken(e){if(e.isTransaction)return;let t=p(e),n=d(e);return t===void 0||n===void 0?void 0:`${t}:${n}`}function prepareSqliteReadCache(e,t){let n;try{let r=e.location();r&&l(C.resolve(r))===l(C.resolve(t))&&(n=readCacheToken(e))}catch(e){if(o(e))throw e}return()=>{if(n===void 0)return!1;try{return readCacheToken(e)===n}catch(e){if(o(e))throw e;return!1}}}const T=/* @__PURE__ */ new Map;let E=null;function closeAuthProfileReadDatabase(e){if(g(`agent`,e))return;let t=C.resolve(e),n=T.get(t);n&&(h(n.db),n.db.isOpen&&n.db.close(),clearTimeout(n.idleTimer),n.idleTimer=void 0,T.delete(t),T.size===0&&(E?.(),E=null))}function closeAuthProfileReadPool(e){if(e?.kind===`database`)closeAuthProfileReadDatabase(e.databasePath);else for(let n of T.keys())(e?.kind!==`root`||t(e.rootPath,n))&&closeAuthProfileReadDatabase(n)}_((e,t)=>{for(let n of T.keys())!t?.has(n)&&e.some(e=>y(e,n))&&closeAuthProfileReadDatabase(n)});function armReadHandleIdleClose(t,n){if(n.idleTimer){n.idleTimer.refresh();return}let r=e(()=>setTimeout(()=>{if(T.get(t)===n&&n.idleTimer===r)try{closeAuthProfileReadDatabase(t)}catch(e){r.refresh(),process.emitWarning(`Failed to close idle auth profile reader: ${String(e)}`,{type:`AuthProfileReadPoolError`})}},18e5));r.unref(),n.idleTimer=r}function isMissingDatabasePath(e){try{return w.statSync(e),!1}catch(e){return n(e,`ENOENT`)}}function acquireAuthProfileReadDatabase(e,t,n,o){let s=C.resolve(e);if(v(s))return{status:`missing`};let c=g(`agent`,s);if(!c&&n&&n.userVersion>38)return closeAuthProfileReadDatabase(s),{status:`unreadable`};let l=c?void 0:T.get(s);if(l?.ready&&l.db.isOpen&&(o===void 0||l.identity===o))return T.delete(s),T.set(s,l),armReadHandleIdleClose(s,l),{status:`readable`,db:l.db};l&&closeAuthProfileReadDatabase(s);for(let[e,t]of c?[]:T)t.ready||closeAuthProfileReadDatabase(e);let u;try{u=t(s,{timeout:i})}catch{return isMissingDatabasePath(s)?{status:`missing`}:{status:`unreadable`}}let d={db:u,identity:o,ready:!1};c||(T.set(s,d),E??=r(closeAuthProfileReadPool),armReadHandleIdleClose(s,d));let p=!1;try{f(u),c?p=a(u)<=38:(n||m(u,e=>{if(e>38)throw Error(`Auth profile database uses a newer agent schema`)}),p=!0)}catch{}let closeCandidate=()=>c?u.close():closeAuthProfileReadDatabase(s);if(!p)return closeCandidate(),{status:`unreadable`};if(c)return{status:`readable`,db:u};try{for(;T.size>64;){let e=T.keys().next().value;if(e===void 0)break;closeAuthProfileReadDatabase(e)}}catch(e){try{closeCandidate()}catch(t){throw AggregateError([e,t],`Unable to close auth profile readers`,{cause:t})}throw e}return d.ready=!0,{status:`readable`,db:u}}const D=`primary`,O={store:{table:`auth_profile_store`,key:`store_key`,value:`store_json`},state:{table:`auth_profile_state`,key:`state_key`,value:`state_json`}},k={store:`authProfiles.store`,state:`authProfiles.state`};function authProfileTableName(e,t){return t===`shared-state`?`config_machine_state`:O[e].table}function inspectAuthProfileTableType(e){return e===void 0?{status:`missing`,reason:`table`}:e===`table`?null:{status:`unreadable`}}function authProfileSchemaObjectType(e,t){return e.tables.has(t)?`table`:e.views.has(t)||e.indexes.has(t)||e.triggers.has(t)?`other`:void 0}function readAuthProfileJsonCellText(e,t,n){if(n===`shared-state`)return S(e,b(e).selectFrom(`config_machine_state`).select(`value_json`).where(`state_key`,`=`,k[t]))?.value_json;let r=O[t];return S(e,b(e).selectFrom(r.table).select(r.value).where(r.key,`=`,D))?.[r.value]}function readAuthProfileJsonCellTexts(e,t){let n=t===`shared-state`?x(e,b(e).selectFrom(`config_machine_state`).select([`state_key as target`,`value_json as contents`]).where(`state_key`,`in`,[k.store,k.state])).rows:x(e,b(e).selectFrom(`auth_profile_store`).select(`store_json as contents`).select(e=>e.val(`store`).as(`target`)).where(`store_key`,`=`,D).unionAll(b(e).selectFrom(`auth_profile_state`).select(`state_json as contents`).select(e=>e.val(`state`).as(`target`)).where(`state_key`,`=`,D))).rows,r=new Map(n.map(e=>[e.target,e.contents]));return{store:r.get(t===`shared-state`?k.store:`store`),state:r.get(t===`shared-state`?k.state:`state`)}}function inspectAuthProfileJsonCellContents(e,t,n){return inspectAuthProfileJsonText(readAuthProfileJsonCellText(e,t,n))}function inspectAuthProfileJsonText(e){if(e===void 0)return{status:`missing`,reason:`row`};try{return{status:`readable`,raw:JSON.parse(e)}}catch{return{status:`unreadable`}}}function readAuthProfileRowsReadOnly(e,t,n,r,i=!0){let a=acquireAuthProfileReadDatabase(e,t,n,r);if(a.status!==`readable`){let e=a.status===`missing`?{status:`missing`,reason:`database`}:{status:`unreadable`};return{store:e,state:e,cacheable:!1}}let o=i;try{let t=readAuthProfileRows(a.db,e,`agent`,n);return t.store.status===`missing`&&t.store.reason===`table`&&t.state.status===`missing`&&t.state.reason===`table`&&(o=!1),t}catch{return closeAuthProfileReadDatabase(e),{store:{status:`unreadable`},state:{status:`unreadable`},cacheable:!1}}finally{g(`agent`,e)?a.db.close():o||closeAuthProfileReadDatabase(e)}}function readAuthProfileRows(e,t,n,r){let i=s(e)?()=>!1:prepareSqliteReadCache(e,t),a=authProfileTableName(`store`,n),o=authProfileTableName(`state`,n),l;try{let t=r?void 0:u(e);l=s(e)?[a,o].map(e=>({name:e,type:`table`})):r?[{name:a,type:r.store===`missing`?void 0:r.store},{name:o,type:r.state===`missing`?void 0:r.state}]:t?[a,o].map(e=>({name:e,type:authProfileSchemaObjectType(t,e)})):c(e,`SELECT name, type FROM sqlite_master WHERE name IN (?, ?)`,[a,o],e=>e.all(a,o))}catch(e){if(n===`shared-state`)throw e;return{store:{status:`unreadable`},state:{status:`unreadable`},cacheable:!1}}let inspect=t=>{let r=t===`store`?a:o,i=inspectAuthProfileTableType(l.find(e=>e.name===r)?.type);if(i)return i;try{return inspectAuthProfileJsonCellContents(e,t,n)}catch(e){if(n===`shared-state`)throw e;return{status:`unreadable`}}},d,f;if([a,o].every(e=>l.some(t=>t.name===e&&t.type===`table`)))try{let t=readAuthProfileJsonCellTexts(e,n);d=inspectAuthProfileJsonText(t.store),f=inspectAuthProfileJsonText(t.state)}catch(e){if(n===`shared-state`)throw e;d=inspect(`store`),f=inspect(`state`)}else d=inspect(`store`),f=inspect(`state`);return{store:d,state:f,cacheable:d.status!==`unreadable`&&f.status!==`unreadable`&&i()}}export{readAuthProfileRowsReadOnly};