UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 3.77 kB
import{a as executeSqliteQueryTakeFirstSync,c as iterateSqliteQuerySync,s as getNodeSqliteKysely}from"./kysely-sync-R08Pt8qn.mjs";import{t as decodeMemoryEmbedding}from"./embedding-vector-Nn3j1kf3.mjs";import"./memory-core-host-engine-storage-DWADN4Bs.mjs";import"./sqlite-worker-runtime-D6xJvxBF.mjs";import{T as loadMemoryEmbeddingCache,c as readMemorySourceHash,l as MEMORY_INDEX_META_KEY,m as loadMemorySourceFileState,s as readMemorySourceChunks,u as readMemoryDatabaseFacts,w as isValidMemoryEmbedding}from"./manager-index-metadata-BEYbHt4P.mjs";import{i as resolveEmbeddingInputFormatVersion}from"./embedding-inputs-D4fq3shl.mjs";import"./memory-core-host-engine-indexing-CVAGt-IM.mjs";import"./memory-core-host-engine-schema-DrdLs2fg.mjs";import{Value}from"typebox/value";import{Type}from"typebox"; //!extensions/memory-core/src/memory/manager-rebuild-embeddings.ts const rebuildIdentitySchema=Type.Object({provider:Type.String(),model:Type.String(),providerKey:Type.String(),embeddingInputFormatVersion:Type.Optional(Type.Integer({minimum:0})),vectorDims:Type.Integer({minimum:1,maximum:Number.MAX_SAFE_INTEGER})});function readMemoryPublishedVectorMeta(e,t,n){const r=getNodeSqliteKysely(e);const i=executeSqliteQueryTakeFirstSync(e,r.selectFrom(`memory_index_meta`).select(`value`).where(`key`,`=`,MEMORY_INDEX_META_KEY));if(!i)return;let a;try{a=JSON.parse(i.value)}catch{return}if(!Value.Check(rebuildIdentitySchema,a)||a.model===`fts-only`||!t.some(e=>a.provider===e.provider&&a.model===e.model&&a.providerKey===e.providerKey)||(a.embeddingInputFormatVersion??0)!==resolveEmbeddingInputFormatVersion(a.model)||n!==void 0&&n!==a.vectorDims)return;return{meta:a,serialized:i.value}}function loadMemoryRebuildEmbeddings(e){const t=/* @__PURE__ */ new Map;for(const n of e.query.chunks){if(e.cached.has(n.hash))continue;const r=`${n.source}:${n.path}`;let i=t.get(r);if(!i){i={source:n.source,path:n.path,hashes:/* @__PURE__ */ new Set};t.set(r,i)}i.hashes.add(n.hash)}if(t.size===0)return;const n=getNodeSqliteKysely(e.db);const{query:r}=e;const i=readMemoryPublishedVectorMeta(e.db,e.identities,r.dimensions);if(!i)return;const{meta:a,serialized:o}=i;const s=/* @__PURE__ */ new Map;const c=/* @__PURE__ */ new Set;for(const r of t.values()){const t=[...r.hashes].filter(t=>!e.cached.has(t));for(let i=0;i<t.length;i+=400){const l=n.selectFrom(`memory_index_chunks`).select([`hash`,`embedding`]).where(`source`,`=`,r.source).where(`path`,`=`,r.path).where(`model`,`=`,a.model).where(`hash`,`in`,t.slice(i,i+400)).where(e=>e.exists(e.selectFrom(`memory_index_meta`).select(`key`).where(`key`,`=`,MEMORY_INDEX_META_KEY).where(`value`,`=`,o)));for(const t of iterateSqliteQuerySync(e.db,l)){const e=s.get(t.hash);if(e&&Buffer.compare(e,t.embedding)!==0)c.add(t.hash);else if(!e)s.set(t.hash,t.embedding)}}}for(const[t,n]of s){if(c.has(t))continue;const r=decodeMemoryEmbedding(n);if(isValidMemoryEmbedding(r,a.vectorDims))e.cached.set(t,r)}} //!extensions/memory-core/src/memory/manager-index-read.ts function readMemoryIndex(e,t){switch(t.type){case`index.facts`:return{type:t.type,value:readMemoryDatabaseFacts(e)};case`source.state`:return{type:t.type,value:loadMemorySourceFileState({db:e,...t.input})};case`source.hash`:return{type:t.type,value:readMemorySourceHash(e,t.input.source,t.input.path)};case`source.chunks`:return{type:t.type,value:readMemorySourceChunks(e,t.input.source,t.input.path)};case`cache.read`:{const n=t.input.cacheEnabled===false?/* @__PURE__ */ new Map:loadMemoryEmbeddingCache({...t.input,db:e});if(t.input.published)loadMemoryRebuildEmbeddings({db:e,identities:t.input.providerIdentities,query:t.input.published,cached:n});return{type:t.type,value:n}}}return t}export{readMemoryPublishedVectorMeta as n,readMemoryIndex as t};