@@ -337,28 +337,51 @@ export class SqliteDatabaseAdapter extends DatabaseAdapter {
337
337
query_field_sub_name : string ;
338
338
query_match_count : number ;
339
339
} ) : Promise < { embedding : number [ ] ; levenshtein_score : number } [ ] > {
340
+ // First get content text and calculate Levenshtein distance
340
341
const sql = `
342
+ WITH content_text AS (
343
+ SELECT
344
+ embedding,
345
+ json_extract(
346
+ json(content),
347
+ '$.' || ? || '.' || ?
348
+ ) as content_text
349
+ FROM memories
350
+ WHERE type = ?
351
+ AND json_extract(
352
+ json(content),
353
+ '$.' || ? || '.' || ?
354
+ ) IS NOT NULL
355
+ )
341
356
SELECT
342
357
embedding,
343
- 0 as levenshtein_score -- Using 0 as placeholder score
344
- FROM memories
345
- WHERE type = ?
346
- AND json_extract(content, '$.' || ? || '.' || ?) IS NOT NULL
358
+ length(?) + length(content_text) - (
359
+ length(?) + length(content_text) - (
360
+ length(replace(lower(?), lower(content_text), '')) +
361
+ length(replace(lower(content_text), lower(?), ''))
362
+ ) / 2
363
+ ) as levenshtein_score
364
+ FROM content_text
365
+ ORDER BY levenshtein_score ASC
347
366
LIMIT ?
348
367
` ;
349
368
350
- const params = [
369
+ const rows = this . db . prepare ( sql ) . all (
370
+ opts . query_field_name ,
371
+ opts . query_field_sub_name ,
351
372
opts . query_table_name ,
352
373
opts . query_field_name ,
353
374
opts . query_field_sub_name ,
375
+ opts . query_input ,
376
+ opts . query_input ,
377
+ opts . query_input ,
378
+ opts . query_input ,
354
379
opts . query_match_count
355
- ] ;
356
-
357
- const rows = this . db . prepare ( sql ) . all ( ...params ) ;
380
+ ) as { embedding : Buffer ; levenshtein_score : number } [ ] ;
358
381
359
- return rows . map ( ( row ) => ( {
360
- embedding : row . embedding ,
361
- levenshtein_score : 0
382
+ return rows . map ( row => ( {
383
+ embedding : Array . from ( new Float32Array ( row . embedding as Buffer ) ) ,
384
+ levenshtein_score : row . levenshtein_score
362
385
} ) ) ;
363
386
}
364
387
0 commit comments