11#!/usr/bin/env python3
2+ import json
23import shlex
34import sys
45from typing import Optional
@@ -31,17 +32,47 @@ def add(
3132 recursive : bool = typer .Option (
3233 False , "-r" , "--recursive" , help = "Recursively add all files in directories"
3334 ),
35+ absolute_paths : bool = typer .Option (
36+ False ,
37+ "--absolute-paths" ,
38+ help = "Store absolute paths instead of relative paths" ,
39+ is_flag = True ,
40+ ),
41+ metadata : Optional [str ] = typer .Option (
42+ None ,
43+ "--metadata" ,
44+ help = "Optional metadata in JSON format to associate with the document" ,
45+ metavar = "JSON" ,
46+ show_default = False ,
47+ prompt = "Metadata (JSON format, e.g. {'author': 'John Doe', 'date': '2023-10-01'}'" ,
48+ ),
3449):
3550 """Add a file path to the database"""
3651 rag = SQLiteRag ()
37- rag .add (path , recursive = recursive )
52+ rag .add (
53+ path ,
54+ recursive = recursive ,
55+ absolute_paths = absolute_paths ,
56+ metadata = json .loads (metadata or "{}" ),
57+ )
3858
3959
4060@app .command ()
41- def add_text (text : str , uri : Optional [str ] = None ):
61+ def add_text (
62+ text : str ,
63+ uri : Optional [str ] = None ,
64+ metadata : Optional [str ] = typer .Option (
65+ None ,
66+ "--metadata" ,
67+ help = "Optional metadata in JSON format to associate with the document" ,
68+ metavar = "JSON" ,
69+ show_default = False ,
70+ prompt = "Metadata (JSON format, e.g. {'author': 'John Doe', 'date': '2023-10-01'}'" ,
71+ ),
72+ ):
4273 """Add a text to the database"""
4374 rag = SQLiteRag ()
44- rag .add_text (text , uri = uri , metadata = {} )
75+ rag .add_text (text , uri = uri , metadata = json . loads ( metadata or "{}" ) )
4576
4677
4778@app .command ("list" )
@@ -86,7 +117,7 @@ def remove(
86117 raise typer .Exit (1 )
87118
88119 # Show document details
89- typer .echo (f "Found document:" )
120+ typer .echo ("Found document:" )
90121 typer .echo (f"ID: { document .id } " )
91122 typer .echo (f"URI: { document .uri or 'N/A' } " )
92123 typer .echo (
@@ -165,7 +196,11 @@ def reset(
165196
166197@app .command ()
167198def search (
168- query : str , limit : int = typer .Option (10 , help = "Number of results to return" )
199+ query : str ,
200+ limit : int = typer .Option (10 , help = "Number of results to return" ),
201+ debug : bool = typer .Option (
202+ False , "-d" , "--debug" , help = "Print extra debug information"
203+ ),
169204):
170205 """Search for documents using hybrid vector + full-text search"""
171206 rag = SQLiteRag ()
@@ -176,12 +211,56 @@ def search(
176211 return
177212
178213 typer .echo (f"Found { len (results )} documents:" )
179- typer .echo (f"{ 'Pos' :<4} { 'Preview' :<60} { 'URI' :<50} " )
180- typer .echo ("-" * 116 )
181- for idx , doc in enumerate (results , 1 ):
182- snippet = f"{ doc .snippet [:57 ]!r} " + "..." if len (doc .snippet ) > 60 else f"{ doc .snippet !r} "
183- uri = doc .document .uri or "N/A"
184- typer .echo (f"{ idx :<4} { snippet :<60} { uri :<50} " )
214+
215+ if debug :
216+ # Enhanced debug table with better formatting
217+ typer .echo (
218+ f"{ '#' :<3} { 'Preview' :<55} { 'URI' :<35} { 'C.Rank' :<33} { 'V.Rank' :<8} { 'FTS.Rank' :<9} { 'V.Dist' :<18} { 'FTS.Score' :<18} "
219+ )
220+ typer .echo ("─" * 180 )
221+
222+ for idx , doc in enumerate (results , 1 ):
223+ # Clean snippet display
224+ snippet = doc .snippet .replace ("\n " , " " ).replace ("\r " , "" )
225+ if len (snippet ) > 52 :
226+ snippet = snippet [:49 ] + "..."
227+
228+ # Clean URI display
229+ uri = doc .document .uri or "N/A"
230+ if len (uri ) > 32 :
231+ uri = "..." + uri [- 29 :]
232+
233+ # Format debug values with proper precision
234+ c_rank = (
235+ f"{ doc .combined_rank :.17f} " if doc .combined_rank is not None else "N/A"
236+ )
237+ v_rank = str (doc .vec_rank ) if doc .vec_rank is not None else "N/A"
238+ fts_rank = str (doc .fts_rank ) if doc .fts_rank is not None else "N/A"
239+ v_dist = (
240+ f"{ doc .vec_distance :.6f} " if doc .vec_distance is not None else "N/A"
241+ )
242+ fts_score = f"{ doc .fts_score :.6f} " if doc .fts_score is not None else "N/A"
243+
244+ typer .echo (
245+ f"{ idx :<3} { snippet :<55} { uri :<35} { c_rank :<33} { v_rank :<8} { fts_rank :<9} { v_dist :<18} { fts_score :<18} "
246+ )
247+ else :
248+ # Clean simple table for normal view
249+ typer .echo (f"{ '#' :<3} { 'Preview' :<60} { 'URI' :<40} " )
250+ typer .echo ("─" * 105 )
251+
252+ for idx , doc in enumerate (results , 1 ):
253+ # Clean snippet display
254+ snippet = doc .snippet .replace ("\n " , " " ).replace ("\r " , "" )
255+ if len (snippet ) > 57 :
256+ snippet = snippet [:54 ] + "..."
257+
258+ # Clean URI display
259+ uri = doc .document .uri or "N/A"
260+ if len (uri ) > 37 :
261+ uri = "..." + uri [- 34 :]
262+
263+ typer .echo (f"{ idx :<3} { snippet :<60} { uri :<40} " )
185264
186265
187266def repl_mode ():
0 commit comments