class l{opts;conn;constructor(e={}){this.opts={mode:e.mode??"memory",filename:e.filename??"wkms-vectors.duckdb",dimensions:e.dimensions??384,resolveNode:e.resolveNode}}async connect(){if(this.conn)return;const o=await import("@duckdb/duckdb-wasm"),t=o.getJsDelivrBundles(),i=await o.selectBundle(t),s=URL.createObjectURL(new Blob([`importScripts("${i.mainWorker}");`],{type:"text/javascript"})),n=new Worker(s),a=new o.ConsoleLogger,c=new o.AsyncDuckDB(a,n);await c.instantiate(i.mainModule,i.pthreadWorker),this.opts.mode==="opfs"&&await c.open({path:`opfs://${this.opts.filename}`});const m=await c.connect();await m.query(`CREATE TABLE IF NOT EXISTS vectors (
      id VARCHAR PRIMARY KEY,
      node_id VARCHAR,
      text VARCHAR,
      embedding FLOAT[${this.opts.dimensions}],
      metadata JSON
    )`),this.conn=m,URL.revokeObjectURL(s)}async close(){this.conn?.close&&await this.conn.close(),this.conn=void 0}dimensions(){return this.opts.dimensions}async upsert(e){const o=this.requireConn();for(const t of e){const i=t.embedding?`[${[...t.embedding].join(",")}]`:"NULL",s=r(t.text),n=r(JSON.stringify(t.metadata));await o.query(`INSERT OR REPLACE INTO vectors (id, node_id, text, embedding, metadata) VALUES ('${r(t.id)}', '${r(t.node_id)}', '${s}', ${i}, '${n}')`)}}async delete(e){if(e.length===0)return 0;const o=this.requireConn(),t=e.map(i=>`'${r(i)}'`).join(",");return await o.query(`DELETE FROM vectors WHERE id IN (${t})`),e.length}async deleteByNode(e){return await this.requireConn().query(`DELETE FROM vectors WHERE node_id='${r(e)}'`),0}async search(e){const o=this.requireConn(),t=e.topK??10,i=`%${r(e.query)}%`;return(await o.query(`SELECT id, node_id, text, metadata FROM vectors WHERE text LIKE '${i}' LIMIT ${t}`)).toArray().map((n,a)=>{const c=String(n.node_id);return{node:this.opts.resolveNode?.(c)??u(c),score:1/(a+1),score_breakdown:{bm25:1/(a+1)},snippet:String(n.text??"").slice(0,280)}})}async*scan(e){const o=this.requireConn(),t=e?.missingEmbeddingOnly?"WHERE embedding IS NULL":"",i=e?.limit?`LIMIT ${e.limit}`:"",s=await o.query(`SELECT id, node_id, text, embedding, metadata FROM vectors ${t} ${i}`);for(const n of s.toArray())yield{id:String(n.id),node_id:String(n.node_id),text:String(n.text??""),embedding:n.embedding?Float32Array.from(n.embedding):null,metadata:n.metadata?JSON.parse(String(n.metadata)):{}}}requireConn(){if(!this.conn)throw new Error("DuckDbVectorStore: connect() not called");return this.conn}}function r(d){return d.replace(/'/g,"''")}function u(d){const e=new Date(0).toISOString();return{id:d,kind:"website",name:d,tags:[],created_at:e,updated_at:e,metadata:{}}}export{l as DuckDbVectorStore};
//# sourceMappingURL=duckdb_vector-kxJZCYew.js.map
