1414from .formatters import get_formatter
1515from .sqliterag import SQLiteRag
1616
17+ DEFAULT_DATABASE_PATH = "./sqliterag.sqlite"
18+
1719
1820class RAGContext :
1921 """Manage CLI state and RAG object reuse"""
2022
2123 def __init__ (self ):
2224 self .rag : Optional [SQLiteRag ] = None
2325 self .in_repl = False
24- self .database : str = ""
26+ self .database_path : str = ""
2527
2628 def enter_repl (self ):
2729 """Enter REPL mode"""
2830 self .in_repl = True
2931
30- def get_rag (self , database_path : str , require_existing : bool = False ) -> SQLiteRag :
32+ def get_rag (self , require_existing : bool = False ) -> SQLiteRag :
3133 """Create or reuse SQLiteRag instance"""
32- if not self .database :
34+ if not self .database_path :
3335 raise ValueError ("Database path not set. Use --database option." )
3436
3537 if self .in_repl :
3638 if self .rag is None :
37- typer .echo (f"Debug: Using database path: { self .database } " )
3839 self .rag = SQLiteRag .create (
39- self .database , require_existing = require_existing
40+ self .database_path , require_existing = require_existing
4041 )
41- typer .echo (f"Database: { Path (self .database ).resolve ()} " )
4242 return self .rag
4343 else :
4444 # Regular mode - create new instance
45- return SQLiteRag .create (database_path , require_existing = require_existing )
45+ typer .echo (f"Database: { Path (self .database_path ).resolve ()} " )
46+ return SQLiteRag .create (
47+ self .database_path , require_existing = require_existing
48+ )
4649
4750
4851rag_context = RAGContext ()
@@ -66,28 +69,32 @@ def __call__(self, *args, **kwds):
6669def main (
6770 ctx : typer .Context ,
6871 database : str = typer .Option (
69- "./sqliterag.sqlite" ,
72+ DEFAULT_DATABASE_PATH ,
7073 "--database" ,
7174 "-db" ,
7275 help = "Path to the SQLite database file" ,
7376 ),
7477):
7578 """SQLite RAG - Retrieval Augmented Generation with SQLite"""
7679 ctx .ensure_object (dict )
77- rag_context .database = database
7880 ctx .obj ["rag_context" ] = rag_context
7981
82+ if not rag_context .in_repl :
83+ rag_context .database_path = database
84+
8085 # If no subcommand was invoked, enter REPL mode
81- if ctx .invoked_subcommand is None :
86+ if ctx .invoked_subcommand is None and not rag_context . in_repl :
8287 rag_context .enter_repl ()
88+ typer .echo (f"Database: { Path (database ).resolve ()} " )
89+
8390 repl_mode ()
8491
8592
8693@app .command ("settings" )
8794def show_settings (ctx : typer .Context ):
8895 """Show current settings"""
8996 rag_context = ctx .obj ["rag_context" ]
90- rag = rag_context .get_rag (rag_context . database , require_existing = True )
97+ rag = rag_context .get_rag (require_existing = True )
9198 current_settings = rag .get_settings ()
9299
93100 typer .echo ("Current settings:" )
@@ -168,7 +175,7 @@ def configure_settings(
168175 show_settings (ctx )
169176 return
170177
171- conn = Database .new_connection (rag_context .database )
178+ conn = Database .new_connection (rag_context .database_path )
172179 settings_manager = SettingsManager (conn )
173180 settings_manager .configure (updates )
174181
@@ -199,7 +206,7 @@ def add(
199206 rag_context = ctx .obj ["rag_context" ]
200207 start_time = time .time ()
201208
202- rag = rag_context .get_rag (rag_context . database )
209+ rag = rag_context .get_rag ()
203210 rag .add (
204211 path ,
205212 recursive = recursive ,
@@ -225,7 +232,7 @@ def add_text(
225232):
226233 """Add a text to the database"""
227234 rag_context = ctx .obj ["rag_context" ]
228- rag = rag_context .get_rag (rag_context . database )
235+ rag = rag_context .get_rag ()
229236 rag .add_text (text , uri = uri , metadata = json .loads (metadata or "{}" ))
230237 typer .echo ("Text added." )
231238
@@ -234,7 +241,7 @@ def add_text(
234241def list_documents (ctx : typer .Context ):
235242 """List all documents in the database"""
236243 rag_context = ctx .obj ["rag_context" ]
237- rag = rag_context .get_rag (rag_context . database , require_existing = True )
244+ rag = rag_context .get_rag (require_existing = True )
238245 documents = rag .list_documents ()
239246
240247 if not documents :
@@ -266,7 +273,7 @@ def remove(
266273):
267274 """Remove document by path or UUID"""
268275 rag_context = ctx .obj ["rag_context" ]
269- rag = rag_context .get_rag (rag_context . database , require_existing = True )
276+ rag = rag_context .get_rag (require_existing = True )
270277
271278 # Find the document first
272279 document = rag .find_document (identifier )
@@ -311,7 +318,7 @@ def rebuild(
311318):
312319 """Rebuild embeddings and full-text index"""
313320 rag_context = ctx .obj ["rag_context" ]
314- rag = rag_context .get_rag (rag_context . database , require_existing = True )
321+ rag = rag_context .get_rag (require_existing = True )
315322
316323 typer .echo ("Rebuild process..." )
317324
@@ -331,7 +338,7 @@ def reset(
331338):
332339 """Reset/clear the entire database"""
333340 rag_context = ctx .obj ["rag_context" ]
334- rag = rag_context .get_rag (rag_context . database , require_existing = True )
341+ rag = rag_context .get_rag (require_existing = True )
335342
336343 # Show warning and ask for confirmation unless -y flag is used
337344 if not yes :
@@ -374,7 +381,7 @@ def search(
374381 rag_context = ctx .obj ["rag_context" ]
375382 start_time = time .time ()
376383
377- rag = rag_context .get_rag (rag_context . database , require_existing = True )
384+ rag = rag_context .get_rag (require_existing = True )
378385 results = rag .search (query , top_k = limit )
379386
380387 search_time = time .time () - start_time
@@ -397,7 +404,7 @@ def quantize(
397404):
398405 """Quantize vectors for faster search or clean up quantization structures"""
399406 rag_context = ctx .obj ["rag_context" ]
400- rag = rag_context .get_rag (rag_context . database , require_existing = True )
407+ rag = rag_context .get_rag (require_existing = True )
401408
402409 if cleanup :
403410 typer .echo ("Cleaning up quantization structures..." )
0 commit comments