1515 load_sqlite_vector_extension as _load_sqlite_vector_extension ,
1616 ensure_chunks_and_meta as _ensure_chunks_and_meta ,
1717 insert_chunk_vector_with_retry as _insert_chunk_vector_with_retry ,
18- search_vectors as _search_vectors ,
1918 get_chunk_text as _get_chunk_text ,
2019)
21- from .openai import call_coding_api , EmbeddingClient
20+ from .openai import call_coding_api
21+ from .llama_embeddings import OpenAICompatibleEmbedding
22+ from .llama_chunker import chunk_with_llama_index
2223from llama_index .core import Document
2324from utils .logger import get_logger
24- from utils import compute_file_hash , chunk_text , norm , cosine
25- from .smart_chunker import smart_chunk
25+ from utils import compute_file_hash , norm , cosine
2626import logging
2727
2828# reduce noise from httpx used by external libs
6464
6565logger = get_logger (__name__ )
6666
67- # Initialize EmbeddingClient for structured logging and retry logic
68- _embedding_client = EmbeddingClient ()
67+ # Initialize llama-index embedding client
68+ _embedding_client = OpenAICompatibleEmbedding ()
6969
7070# Thread-local storage to track execution state inside futures
7171_thread_state = threading .local ()
@@ -86,7 +86,8 @@ def _get_embedding_with_semaphore(semaphore: threading.Semaphore, text: str, fil
8686 semaphore .acquire ()
8787 try :
8888 _thread_state .stage = "calling_embed_text"
89- result = _embedding_client .embed_text (text , file_path = file_path , chunk_index = chunk_index )
89+ # Use llama-index embedding client
90+ result = _embedding_client ._get_text_embedding (text )
9091 _thread_state .stage = "completed"
9192 return result
9293 except Exception as e :
@@ -171,14 +172,8 @@ def _process_file_sync(
171172 if isinstance (cfg , dict ):
172173 embedding_model = cfg .get ("embedding_model" )
173174
174- # Use smart chunking for supported code languages
175- use_smart_chunking = cfg .get ("smart_chunking" , True ) if isinstance (cfg , dict ) else True
176- supported_languages = ["python" , "javascript" , "typescript" , "java" , "go" , "rust" , "c" , "cpp" ]
177-
178- if use_smart_chunking and lang in supported_languages :
179- chunks = smart_chunk (content , language = lang , chunk_size = CHUNK_SIZE , overlap = CHUNK_OVERLAP )
180- else :
181- chunks = chunk_text (content , chunk_size = CHUNK_SIZE , overlap = CHUNK_OVERLAP )
175+ # Use llama-index chunking for all content
176+ chunks = chunk_with_llama_index (content , language = lang , chunk_size = CHUNK_SIZE , chunk_overlap = CHUNK_OVERLAP )
182177
183178 if not chunks :
184179 chunks = [content ]
@@ -395,11 +390,13 @@ def analyze_local_path_sync(
395390
396391 try :
397392 # Use batch update for efficiency - single database transaction
393+ # Store total_files for performance (avoid re-scanning directory on every request)
398394 set_project_metadata_batch (database_path , {
399395 "last_indexed_at" : time .strftime ("%Y-%m-%d %H:%M:%S" ),
400396 "last_index_duration" : str (duration ),
401397 "files_indexed" : str (file_count ),
402- "files_skipped" : str (skipped_count )
398+ "files_skipped" : str (skipped_count ),
399+ "total_files" : str (total_files ) # Store total files found during indexing
403400 })
404401 except Exception :
405402 logger .exception ("Failed to store indexing metadata" )
@@ -442,16 +439,40 @@ def analyze_local_path_background(local_path: str, database_path: str, venv_path
442439
443440def search_semantic (query : str , database_path : str , top_k : int = 5 ):
444441 """
445- Uses sqlite-vector's vector_full_scan to retrieve best-matching chunks and returns
446- a list of {file_id, path, chunk_index, score}.
442+ Uses llama-index with sqlite-vector backend to retrieve best-matching chunks.
443+ Always includes content as it's needed for the coding model context.
444+
445+ Args:
446+ query: Search query text
447+ database_path: Path to the SQLite database
448+ top_k: Number of results to return
449+
450+ Returns:
451+ List of dicts with file_id, path, chunk_index, score, and content
447452 """
448- q_emb = _embedding_client .embed_text (query , file_path = "<query>" , chunk_index = 0 )
449- if not q_emb :
450- return []
451-
452453 try :
453- return _search_vectors (database_path , q_emb , top_k = top_k )
454- except Exception :
454+ # Use llama-index for semantic search
455+ from .llama_integration import llama_index_search
456+
457+ docs = llama_index_search (query , database_path , top_k = top_k )
458+
459+ results = []
460+ for doc in docs :
461+ metadata = doc .metadata or {}
462+ result = {
463+ "file_id" : metadata .get ("file_id" , 0 ),
464+ "path" : metadata .get ("path" , "" ),
465+ "chunk_index" : metadata .get ("chunk_index" , 0 ),
466+ "score" : metadata .get ("score" , 0.0 ),
467+ "content" : doc .text or "" # Always include content for LLM context
468+ }
469+ results .append (result )
470+
471+ logger .info (f"llama-index search returned { len (results )} results" )
472+ return results
473+
474+ except Exception as e :
475+ logger .exception (f"Semantic search failed: { e } " )
455476 raise
456477
457478
0 commit comments