UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

7 lines • 5.5 kB
import{n as isPostgresSqlConnection}from"./sql-connection-nx8lLUJM.mjs";import{c as withSqlitePostCommitPublications}from"./sqlite-post-commit-CAVf6ZAG.mjs";import{i as executeSqliteQuerySync,p as sqliteStringSet,s as getNodeSqliteKysely}from"./kysely-sync-R08Pt8qn.mjs";import{a as tableExists}from"./openclaw-state-db-schema-helpers-rYTimzjJ.mjs";import{n as loadSqliteVecExtensionFromPath}from"./sqlite-vec-D-Kq5nmv.mjs";import{t as bindSqliteWorkerDatabase}from"./sqlite-worker-binding-CxA1GbiJ.mjs";import{i as recordMemoryEntryOriginsInDatabase,n as ensureMemoryEntryOriginsSchema,r as readMemoryEntryOriginsInDatabase}from"./memory-core-host-engine-storage-DWADN4Bs.mjs";import{r as runSqliteImmediateTransactionSync}from"./sqlite-worker-runtime-D6xJvxBF.mjs";import{t as selectedMemoryLineageIdentity}from"./memory-entry-origins-task-DBBFGfrw.mjs";import{t as advanceMemoryIndexRevision}from"./memory-index-revision.worker-CngmMyv4.mjs";import{r as recordMemorySessionTombstonesInDatabase,t as ensureMemorySessionTombstones}from"./memory-session-tombstones-DYntu_P7.mjs";import{a as extractPromotionKeys}from"./short-term-promotion-memory-write-CS9JCkvz.mjs"; //!extensions/memory-core/src/memory-entry-origins-delete.ts function deleteMemoryEntryOriginsInDatabase(e,t,n){if(t.entryKeys.length===0||t.sessionIds?.length===0||!tableExists(e,`memory_entry_origins`))return 0;return runSqliteImmediateTransactionSync(e,()=>{n?.onBegin();let r=getNodeSqliteKysely(e).deleteFrom(`memory_entry_origins`).where(`agent_id`,`=`,t.agentId).where(`entry_key`,`in`,sqliteStringSet(t.entryKeys));if(t.sessionIds)r=r.where(`session_id`,`in`,sqliteStringSet(t.sessionIds));return Number(executeSqliteQuerySync(e,r).numAffectedRows??0n)},n&&{withCommit:n.withCommit})} //!extensions/memory-core/src/memory-forget-kernel.ts function checkLineage(e,t){const n=tableExists(e,`memory_entry_origins`)?readMemoryEntryOriginsInDatabase(e,{agentId:t.agentId}):[];return selectedMemoryLineageIdentity(n,new Set(t.sessionIds),new Set(t.entryKeys))===t.identity?{current:true}:{current:false,origins:n}}function markMemoryForgotten(e,t){const n=checkLineage(e,t);if(!n.current)return n;if(recordMemorySessionTombstonesInDatabase(e,t)===0)advanceMemoryIndexRevision(e);return n}function purgeForgottenMemory(e,t){const n=checkLineage(e,t);if(!n.current)return n;const r=getNodeSqliteKysely(e);if(t.chunkIds.length>0){if(t.hasVectorTable&&!isPostgresSqlConnection(e))executeSqliteQuerySync(e,r.deleteFrom(`memory_index_chunks_vec`).where(`id`,`in`,t.chunkIds));executeSqliteQuerySync(e,r.deleteFrom(`memory_index_chunks`).where(`id`,`in`,isPostgresSqlConnection(e)?sqliteStringSet(t.chunkIds):t.chunkIds))}deleteMemoryIndexSources(e,t.sources);if(tableExists(e,`memory_embedding_cache`))executeSqliteQuerySync(e,r.deleteFrom(`memory_embedding_cache`));if(t.chunkIds.length>0||t.sources.length>0)advanceMemoryIndexRevision(e);return n}function deleteMemoryIndexSources(e,t){const n=getNodeSqliteKysely(e);for(let r=0;r<t.length;){const i=t[r];let a=r+1;while(a<t.length&&t[a].source===i.source)a+=1;executeSqliteQuerySync(e,n.deleteFrom(`memory_index_sources`).where(`path`,`in`,sqliteStringSet(t.slice(r,a).map(e=>e.path))).where(`source`,`=`,i.source));r=a}} //!extensions/memory-core/src/memory-entry-origins.worker.ts function bindSqliteWorkerBackend(e,t){const n=t.database;const r={onBegin:()=>t.admit(`transaction`),withCommit:e=>{t.admit(`commit`);e()}};const transact=e=>runSqliteImmediateTransactionSync(n,()=>{r.onBegin();return e()},{withCommit:r.withCommit});if(e.kind===`forget`){if(e.extensionPath&&!isPostgresSqlConnection(n))try{loadSqliteVecExtensionFromPath(n,e.extensionPath)}catch(e){throw new Error(`memory forget cannot purge vector index: ${e instanceof Error?e.message:String(e)}`,{cause:e})}if(e.prepareTombstones&&!isPostgresSqlConnection(n)&&!tableExists(n,`memory_session_tombstones`))withSqlitePostCommitPublications(n,()=>transact(()=>ensureMemorySessionTombstones(n)))}else if(e.kind===`origin`&&!isPostgresSqlConnection(n)&&!tableExists(n,`memory_entry_origins`))withSqlitePostCommitPublications(n,()=>transact(()=>ensureMemoryEntryOriginsSchema(n)));return bindSqliteWorkerDatabase(n,{execute(e){if(e.type===`record`)return transact(()=>recordMemoryEntryOriginsInDatabase(n,e.input));if(e.type===`reserve`){if(!tableExists(n,`memory_entry_origins`))return[];return transact(()=>{const{agentId:t,operations:r}=e.input;const i=readMemoryEntryOriginsInDatabase(n,{agentId:t,entryKeys:[...new Set(r.flatMap(e=>e.parentKeys))]});return r.flatMap(({entryKey:e,parentKeys:r})=>{const a=new Set(r);const o=i.filter(e=>a.has(e.entryKey));const s=o.length?recordMemoryEntryOriginsInDatabase(n,{agentId:t,entryKey:e,origins:o}):[];return s.length?[{agentId:t,entryKeys:[e],sessionIds:s.map(e=>e.sessionId)}]:[]})})}if(e.type===`prune`){if(!tableExists(n,`memory_entry_origins`))return 0;return transact(()=>{const t=new Set(executeSqliteQuerySync(n,getNodeSqliteKysely(n).selectFrom(`memory_index_chunks`).select(`text`).where(`source`,`=`,`memory`).where(`text`,`like`,`%openclaw-memory-promotion:%`)).rows.flatMap(({text:e})=>extractPromotionKeys(e)));return deleteMemoryEntryOriginsInDatabase(n,{agentId:e.input.agentId,entryKeys:e.input.entryKeys.filter(e=>!t.has(e))})})}if(e.type===`delete`)return deleteMemoryEntryOriginsInDatabase(n,e.input,r);return transact(()=>e.type===`forget.mark`?markMemoryForgotten(n,e.input):purgeForgottenMemory(n,e.input))}})}export{bindSqliteWorkerBackend as t};