openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 2.49 kB
JavaScript
import"./openclaw-state-read-subsystem-Dwoo5bcz.mjs";import{Pt as e}from"./openclaw-state-read-src-RL5xtYG2.mjs";import{s as t}from"./openclaw-state-read-openclaw-state-db.paths-7XLpcdiq.mjs";import{a as n,i as r,r as i}from"./openclaw-state-read-kysely-sync-Bcu0Qb7F.mjs";import{r as a}from"./openclaw-state-read-openclaw-state-db-schema-helpers-QFZd8aJI.mjs";import{n as o,t as s}from"./openclaw-state-read-sqlite-number-5mfxKD9j.mjs";import{t as c}from"./openclaw-state-read-plugin-blob-store.types-mRiMwmH1.mjs";function createError(e){return new c(e.message,{code:e.code,operation:e.operation,path:e.path??t(e.env??process.env),cause:e.cause})}function wrapPluginBlobError(e,n,r,i,a,o=t(a??process.env)){return e instanceof c?e:createError({code:r,operation:n,message:i,env:a,path:o,cause:e})}function readBlobInDatabase(t,n,r,i,o){try{return r()}catch(r){if(r instanceof Error&&e(r,`ERR_SQLITE_ERROR`)&&r.message===`no such table: plugin_blob_entries`&&!a(n))return;throw wrapPluginBlobError(r,t,`PLUGIN_BLOB_READ_FAILED`,t===`lookup`?`Failed to read plugin blob entry.`:`Failed to list plugin blob entries.`,i,o)}}function kysely(e){return n(e)}function decodeBlobInfo(e,t,n,r){let i;try{i=JSON.parse(e.metadata_json)}catch(e){throw createError({code:`PLUGIN_BLOB_CORRUPT`,operation:t,message:`Plugin blob entry contains corrupt metadata JSON.`,env:n,path:r,cause:e})}let a=o(e.expires_at);return{key:e.entry_key,metadata:i,sizeBytes:s(e.size_bytes),createdAt:o(e.created_at)??0,...a==null?{}:{expiresAt:a}}}function blobInfoQuery(e,t){return kysely(e).selectFrom(`plugin_blob_entries`).select([`entry_key`,`metadata_json`,`created_at`,`expires_at`]).select(e=>e.fn(`length`,[`blob`]).as(`size_bytes`)).where(`plugin_id`,`=`,t.pluginId).where(`namespace`,`=`,t.namespace)}function pluginBlobLookupInDatabase(e,t){return readBlobInDatabase(`lookup`,e,()=>{let n=Date.now(),i=r(e,blobInfoQuery(e,t).select(`blob`).where(`entry_key`,`=`,t.key).where(e=>e.or([e(`expires_at`,`is`,null),e(`expires_at`,`>`,n)])));return i?{...decodeBlobInfo(i,`lookup`,t.env,t.path),bytes:i.blob}:void 0},t.env,t.path)}function pluginBlobEntriesInDatabase(e,t){return readBlobInDatabase(`entries`,e,()=>{let n=Date.now();return i(e,blobInfoQuery(e,t).where(e=>e.or([e(`expires_at`,`is`,null),e(`expires_at`,`>`,n)])).orderBy(`created_at`,`asc`).orderBy(`entry_key`,`asc`)).rows.map(e=>decodeBlobInfo(e,`entries`,t.env,t.path))},t.env,t.path)??[]}export{pluginBlobEntriesInDatabase,pluginBlobLookupInDatabase};