This commit is contained in:
Roberto Musso
2026-02-24 22:02:46 +01:00
parent 2cb2f0e4e8
commit 77b94e2b27
4 changed files with 194 additions and 15 deletions

View File

@@ -13,6 +13,13 @@ interface NoteRecord {
vector: number[];
}
export interface SearchResult {
id: string;
projectId: string;
content: string;
_distance: number;
}
let conn: lancedb.Connection | null = null;
/**
@@ -111,3 +118,30 @@ export async function migrateNotesIfNeeded(): Promise<void> {
console.log(`[VectorDB] Migration complete: ${successCount}/${allNotes.length} notes embedded`);
}
/**
* Embed the query string and perform a similarity search across all notes
* in the LanceDB 'notes' table. Returns up to `limit` results sorted by
* distance (closest first).
*
* Returns an empty array if the notes table does not exist yet.
*/
export async function searchNotes(query: string, limit = 5): Promise<SearchResult[]> {
const c = getConn();
const tableNames = await c.tableNames();
if (!tableNames.includes('notes')) {
return [];
}
const queryVector = await embedText(query);
const table = await c.openTable('notes');
const results = await table.search(queryVector).limit(limit).execute();
return results.map((r) => ({
id: r.id as string,
projectId: r.projectId as string,
content: r.content as string,
_distance: r._distance as number,
}));
}