(handle: SqliteIndexHandle, queryEmbedding: number[], topK: number)
| 183 | * project ever needs ANN, hnswlib-node can be slotted in behind this same API.) |
| 184 | */ |
| 185 | export function searchSqlite(handle: SqliteIndexHandle, queryEmbedding: number[], topK: number): SqliteScored[] { |
| 186 | const rows: any[] = handle.db.prepare('SELECT id,file,start_line,end_line,symbol,text,hash,scale,vec FROM chunks').all(); |
| 187 | const scored: SqliteScored[] = []; |
| 188 | for (const r of rows) { |
| 189 | const bytes = new Int8Array(r.vec.buffer, r.vec.byteOffset, r.vec.byteLength); |
| 190 | const score = cosineQuantized(queryEmbedding, { scale: r.scale, bytes }); |
| 191 | scored.push({ |
| 192 | chunk: { |
| 193 | file: r.file, startLine: r.start_line, endLine: r.end_line, |
| 194 | symbol: r.symbol ?? undefined, text: r.text, hash: r.hash, |
| 195 | } as Chunk, |
| 196 | score, |
| 197 | }); |
| 198 | } |
| 199 | scored.sort((a, b) => b.score - a.score); |
| 200 | return scored.slice(0, topK); |
| 201 | } |
| 202 | |
| 203 | /** Read every chunk's text (for BM25 in hybrid mode) without the vectors. */ |
| 204 | export function allChunksFromSqlite(handle: SqliteIndexHandle): Chunk[] { |
no test coverage detected