1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
//! Core library client for project-rag
//!
//! This module provides the main client interface for using project-rag
//! as a library in your own Rust applications.
use crate::cache::HashCache;
use crate::config::Config;
use crate::embedding::{EmbeddingProvider, FastEmbedManager};
use crate::git_cache::GitCache;
use crate::indexer::{CodeChunker, FileInfo, detect_language};
use crate::relations::{
DefinitionResult, HybridRelationsProvider, ReferenceResult, RelationsProvider,
};
use crate::types::*;
use crate::vector_db::VectorDatabase;
// Conditionally import the appropriate vector database backend
#[cfg(feature = "qdrant-backend")]
use crate::vector_db::QdrantVectorDB;
#[cfg(not(feature = "qdrant-backend"))]
use crate::vector_db::LanceVectorDB;
use anyhow::{Context, Result};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::RwLock;
use tokio::sync::broadcast;
// Filesystem locking for cross-process coordination
mod fs_lock;
pub(crate) use fs_lock::FsLockGuard;
// Index locking mechanism (uses fs_lock for cross-process, broadcast for in-process)
mod index_lock;
pub(crate) use index_lock::{IndexLockGuard, IndexLockResult, IndexingOperation};
/// Main client for interacting with the RAG system
///
/// This client provides a high-level API for indexing codebases and performing
/// semantic searches. It contains all the core functionality and can be used
/// directly as a library or wrapped by the MCP server.
///
/// # Example
///
/// ```no_run
/// use project_rag::{RagClient, IndexRequest, QueryRequest};
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
/// // Create client with default configuration
/// let client = RagClient::new().await?;
///
/// // Index a codebase
/// let index_req = IndexRequest {
/// path: "/path/to/code".to_string(),
/// project: Some("my-project".to_string()),
/// include_patterns: vec!["**/*.rs".to_string()],
/// exclude_patterns: vec!["**/target/**".to_string()],
/// max_file_size: 1_048_576,
/// };
/// let response = client.index_codebase(index_req).await?;
/// println!("Indexed {} files", response.files_indexed);
///
/// Ok(())
/// }
/// ```
#[derive(Clone)]
pub struct RagClient {
pub(crate) embedding_provider: Arc<FastEmbedManager>,
#[cfg(feature = "qdrant-backend")]
pub(crate) vector_db: Arc<QdrantVectorDB>,
#[cfg(not(feature = "qdrant-backend"))]
pub(crate) vector_db: Arc<LanceVectorDB>,
pub(crate) chunker: Arc<CodeChunker>,
// Persistent hash cache for incremental updates
pub(crate) hash_cache: Arc<RwLock<HashCache>>,
pub(crate) cache_path: PathBuf,
// Git cache for git history indexing
pub(crate) git_cache: Arc<RwLock<GitCache>>,
pub(crate) git_cache_path: PathBuf,
// Configuration (for accessing batch sizes, timeouts, etc.)
pub(crate) config: Arc<Config>,
// In-progress indexing operations (prevents concurrent indexing and allows result sharing)
pub(crate) indexing_ops: Arc<RwLock<HashMap<String, IndexingOperation>>>,
// Relations provider for code navigation (find definition, references, call graph)
pub(crate) relations_provider: Arc<HybridRelationsProvider>,
}
impl RagClient {
/// Create a new RAG client with default configuration
///
/// This will initialize the embedding model, vector database, and load
/// any existing caches from disk.
///
/// # Errors
///
/// Returns an error if:
/// - Configuration cannot be loaded
/// - Embedding model cannot be initialized
/// - Vector database cannot be initialized
pub async fn new() -> Result<Self> {
let config = Config::new().context("Failed to load configuration")?;
Self::with_config(config).await
}
/// Create a new RAG client with custom configuration
///
/// # Example
///
/// ```no_run
/// use project_rag::{RagClient, Config};
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
/// let mut config = Config::default();
/// config.embedding.model_name = "BAAI/bge-small-en-v1.5".to_string();
///
/// let client = RagClient::with_config(config).await?;
/// Ok(())
/// }
/// ```
pub async fn with_config(config: Config) -> Result<Self> {
tracing::info!("Initializing RAG client with configuration");
tracing::debug!("Vector DB backend: {}", config.vector_db.backend);
tracing::debug!("Embedding model: {}", config.embedding.model_name);
tracing::debug!("Chunk size: {}", config.indexing.chunk_size);
// Initialize embedding provider with configured model
let embedding_provider = Arc::new(
FastEmbedManager::from_model_name(&config.embedding.model_name)
.context("Failed to initialize embedding provider")?,
);
// Initialize the appropriate vector database backend
#[cfg(feature = "qdrant-backend")]
let vector_db = {
tracing::info!(
"Using Qdrant vector database backend at {}",
config.vector_db.qdrant_url
);
Arc::new(
QdrantVectorDB::with_url(&config.vector_db.qdrant_url)
.await
.context("Failed to initialize Qdrant vector database")?,
)
};
#[cfg(not(feature = "qdrant-backend"))]
let vector_db = {
tracing::info!(
"Using LanceDB vector database backend at {}",
config.vector_db.lancedb_path.display()
);
Arc::new(
LanceVectorDB::with_path(&config.vector_db.lancedb_path.to_string_lossy())
.await
.context("Failed to initialize LanceDB vector database")?,
)
};
// Initialize the database with the embedding dimension
vector_db
.initialize(embedding_provider.dimension())
.await
.context("Failed to initialize vector database collections")?;
// Create chunker with configured chunk size
let chunker = Arc::new(CodeChunker::default_strategy());
// Load persistent hash cache
let cache_path = config.cache.hash_cache_path.clone();
let hash_cache = HashCache::load(&cache_path).unwrap_or_else(|e| {
tracing::warn!("Failed to load hash cache: {}, starting fresh", e);
HashCache::default()
});
tracing::info!("Using hash cache file: {:?}", cache_path);
// Load persistent git cache
let git_cache_path = config.cache.git_cache_path.clone();
let git_cache = GitCache::load(&git_cache_path).unwrap_or_else(|e| {
tracing::warn!("Failed to load git cache: {}, starting fresh", e);
GitCache::default()
});
tracing::info!("Using git cache file: {:?}", git_cache_path);
// Initialize relations provider for code navigation
let relations_provider = Arc::new(
HybridRelationsProvider::new(false) // stack-graphs disabled by default
.context("Failed to initialize relations provider")?,
);
Ok(Self {
embedding_provider,
vector_db,
chunker,
hash_cache: Arc::new(RwLock::new(hash_cache)),
cache_path,
git_cache: Arc::new(RwLock::new(git_cache)),
git_cache_path,
config: Arc::new(config),
indexing_ops: Arc::new(RwLock::new(HashMap::new())),
relations_provider,
})
}
/// Create a new client with custom database path (for testing)
#[cfg(test)]
pub async fn new_with_db_path(db_path: &str, cache_path: PathBuf) -> Result<Self> {
// Create a test config with custom paths
let mut config = Config::default();
config.vector_db.lancedb_path = PathBuf::from(db_path);
config.cache.hash_cache_path = cache_path.clone();
config.cache.git_cache_path = cache_path.parent().unwrap().join("git_cache.json");
Self::with_config(config).await
}
/// Create FileInfo from a file path for relations analysis
fn create_file_info(&self, file_path: &str, project: Option<String>) -> Result<FileInfo> {
use std::path::Path;
let path = Path::new(file_path);
let canonical = std::fs::canonicalize(path)
.with_context(|| format!("Failed to canonicalize path: {}", file_path))?;
let content = std::fs::read_to_string(&canonical)
.with_context(|| format!("Failed to read file: {}", file_path))?;
let extension = canonical
.extension()
.and_then(|e| e.to_str())
.map(|s| s.to_string());
let language = extension.as_ref().and_then(|ext| {
detect_language(ext)
});
// Compute file hash
use sha2::{Sha256, Digest};
let mut hasher = Sha256::new();
hasher.update(content.as_bytes());
let hash = format!("{:x}", hasher.finalize());
// Determine root path (parent directory)
let root_path = canonical
.parent()
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_else(|| "/".to_string());
let relative_path = canonical
.file_name()
.map(|n| n.to_string_lossy().to_string())
.unwrap_or_else(|| file_path.to_string());
Ok(FileInfo {
path: canonical,
relative_path,
root_path,
project,
extension,
language,
content,
hash,
})
}
/// Normalize a path to a canonical absolute form for consistent cache lookups
pub fn normalize_path(path: &str) -> Result<String> {
let path_buf = PathBuf::from(path);
let canonical = std::fs::canonicalize(&path_buf)
.with_context(|| format!("Failed to canonicalize path: {}", path))?;
Ok(canonical.to_string_lossy().to_string())
}
/// Check if a specific path's index is dirty (incomplete/corrupted)
///
/// Returns true if the path is marked as dirty, meaning a previous indexing
/// operation was interrupted and the data may be inconsistent.
pub async fn is_index_dirty(&self, path: &str) -> bool {
if let Ok(normalized) = Self::normalize_path(path) {
let cache = self.hash_cache.read().await;
cache.is_dirty(&normalized)
} else {
false
}
}
/// Check if any indexed paths are dirty
///
/// Returns a list of paths that have dirty indexes.
pub async fn get_dirty_paths(&self) -> Vec<String> {
let cache = self.hash_cache.read().await;
cache.get_dirty_roots().keys().cloned().collect()
}
/// Check if searching on a specific path should be blocked due to dirty state
///
/// Returns an error if the path is dirty, otherwise Ok(())
async fn check_path_not_dirty(&self, path: Option<&str>) -> Result<()> {
if let Some(p) = path {
if self.is_index_dirty(p).await {
anyhow::bail!(
"Index for '{}' is dirty (previous indexing was interrupted). \
Please re-run index_codebase to rebuild the index before querying.",
p
);
}
}
Ok(())
}
/// Try to acquire an indexing lock for a given path
///
/// This uses a two-layer locking strategy:
/// 1. Filesystem lock (flock) for cross-process coordination
/// 2. In-memory lock for broadcasting results to waiters in the same process
///
/// Returns either:
/// - `IndexLockResult::Acquired(guard)` if we should perform the indexing
/// - `IndexLockResult::WaitForResult(receiver)` if another task in THIS process is indexing
/// - `IndexLockResult::WaitForFilesystemLock(path)` if ANOTHER PROCESS is indexing
///
/// The lock is automatically released when the returned guard is dropped.
pub(crate) async fn try_acquire_index_lock(&self, path: &str) -> Result<IndexLockResult> {
use std::sync::atomic::Ordering;
use std::time::Instant;
// Normalize the path to ensure consistent locking across different path formats
let normalized_path = Self::normalize_path(path)?;
// STEP 1: Try to acquire filesystem lock first (cross-process coordination)
// This must happen BEFORE checking in-memory state to prevent race conditions
let fs_lock = {
let path_clone = normalized_path.clone();
tokio::task::spawn_blocking(move || FsLockGuard::try_acquire(&path_clone))
.await
.context("Filesystem lock task panicked")??
};
// If we couldn't get the filesystem lock, another PROCESS is indexing
let fs_lock = match fs_lock {
Some(lock) => lock,
None => {
tracing::info!(
"Another process is indexing {} - returning WaitForFilesystemLock",
normalized_path
);
return Ok(IndexLockResult::WaitForFilesystemLock(normalized_path));
}
};
// STEP 2: We have the filesystem lock, now check in-memory state
// This handles the case where another task in THIS process is indexing
// Acquire write lock on the ops map
let mut ops = self.indexing_ops.write().await;
// Check if an operation is already in progress for this path (in this process)
if let Some(existing_op) = ops.get(&normalized_path) {
// Check if the operation is stale (timed out or crashed)
if existing_op.is_stale() {
tracing::warn!(
"Removing stale indexing lock for {} (operation timed out after {:?})",
normalized_path,
existing_op.started_at.elapsed()
);
ops.remove(&normalized_path);
} else if existing_op.active.load(Ordering::Acquire) {
// Operation is still active and not stale, subscribe to receive the result
// Note: We drop the filesystem lock here since we won't be indexing
drop(fs_lock);
let receiver = existing_op.result_tx.subscribe();
tracing::info!(
"Indexing already in progress in this process for {} (started {:?} ago), waiting for result",
normalized_path,
existing_op.started_at.elapsed()
);
return Ok(IndexLockResult::WaitForResult(receiver));
} else {
// Operation completed but cleanup hasn't happened yet
tracing::debug!(
"Removing completed indexing lock for {} (cleanup pending)",
normalized_path
);
ops.remove(&normalized_path);
}
}
// STEP 3: We have both locks, register the operation
// Create a new broadcast channel for this operation
// Capacity of 1 is enough since we only send one result
let (result_tx, _) = broadcast::channel(1);
// Create the active flag - starts as true (active)
let active_flag = Arc::new(std::sync::atomic::AtomicBool::new(true));
// Register this operation with timestamp
ops.insert(
normalized_path.clone(),
IndexingOperation {
result_tx: result_tx.clone(),
active: active_flag.clone(),
started_at: Instant::now(),
},
);
// Drop the write lock on the map
drop(ops);
Ok(IndexLockResult::Acquired(IndexLockGuard::new(
normalized_path,
self.indexing_ops.clone(),
result_tx,
active_flag,
fs_lock,
)))
}
/// Index a codebase directory
///
/// This automatically performs full indexing for new codebases or incremental
/// updates for previously indexed codebases.
///
/// # Example
///
/// ```no_run
/// use project_rag::{RagClient, IndexRequest};
///
/// # async fn example() -> anyhow::Result<()> {
/// let client = RagClient::new().await?;
///
/// let request = IndexRequest {
/// path: "/path/to/code".to_string(),
/// project: Some("my-project".to_string()),
/// include_patterns: vec!["**/*.rs".to_string()],
/// exclude_patterns: vec!["**/target/**".to_string()],
/// max_file_size: 1_048_576,
/// };
///
/// let response = client.index_codebase(request).await?;
/// println!("Indexed {} files in {} ms",
/// response.files_indexed,
/// response.duration_ms);
/// # Ok(())
/// # }
/// ```
pub async fn index_codebase(&self, request: IndexRequest) -> Result<IndexResponse> {
// Validate request
request.validate().map_err(|e| anyhow::anyhow!(e))?;
// Use the smart indexing logic without progress notifications
// Default cancellation token - not cancellable from this API
let cancel_token = tokio_util::sync::CancellationToken::new();
indexing::do_index_smart(
self,
request.path,
request.project,
request.include_patterns,
request.exclude_patterns,
request.max_file_size,
None, // No peer
None, // No progress token
cancel_token,
)
.await
}
/// Query the indexed codebase using semantic search
///
/// # Example
///
/// ```no_run
/// use project_rag::{RagClient, QueryRequest};
///
/// # async fn example() -> anyhow::Result<()> {
/// let client = RagClient::new().await?;
///
/// let request = QueryRequest {
/// query: "authentication logic".to_string(),
/// project: Some("my-project".to_string()),
/// limit: 10,
/// min_score: 0.7,
/// hybrid: true,
/// };
///
/// let response = client.query_codebase(request).await?;
/// for result in response.results {
/// println!("Found in {}: {:.2}", result.file_path, result.score);
/// println!("{}", result.content);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn query_codebase(&self, request: QueryRequest) -> Result<QueryResponse> {
request.validate().map_err(|e| anyhow::anyhow!(e))?;
// Check if the target path is dirty (if path filter is specified)
self.check_path_not_dirty(request.path.as_deref()).await?;
let start = Instant::now();
let query_embedding = self
.embedding_provider
.embed_batch(vec![request.query.clone()])
.context("Failed to generate query embedding")?
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("No embedding generated"))?;
let original_threshold = request.min_score;
let mut threshold_used = original_threshold;
let mut threshold_lowered = false;
let mut results = self
.vector_db
.search(
query_embedding.clone(),
&request.query,
request.limit,
threshold_used,
request.project.clone(),
request.path.clone(),
request.hybrid,
)
.await
.context("Failed to search")?;
if results.is_empty() && original_threshold > 0.3 {
let fallback_thresholds = [0.6, 0.5, 0.4, 0.3];
for &threshold in &fallback_thresholds {
if threshold >= original_threshold {
continue;
}
results = self
.vector_db
.search(
query_embedding.clone(),
&request.query,
request.limit,
threshold,
request.project.clone(),
request.path.clone(),
request.hybrid,
)
.await
.context("Failed to search")?;
if !results.is_empty() {
threshold_used = threshold;
threshold_lowered = true;
break;
}
}
}
Ok(QueryResponse {
results,
duration_ms: start.elapsed().as_millis() as u64,
threshold_used,
threshold_lowered,
})
}
/// Advanced search with filters for file type, language, and path patterns
pub async fn search_with_filters(
&self,
request: AdvancedSearchRequest,
) -> Result<QueryResponse> {
request.validate().map_err(|e| anyhow::anyhow!(e))?;
// Check if the target path is dirty (if path filter is specified)
self.check_path_not_dirty(request.path.as_deref()).await?;
let start = Instant::now();
let query_embedding = self
.embedding_provider
.embed_batch(vec![request.query.clone()])
.context("Failed to generate query embedding")?
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("No embedding generated"))?;
let original_threshold = request.min_score;
let mut threshold_used = original_threshold;
let mut threshold_lowered = false;
let mut results = self
.vector_db
.search_filtered(
query_embedding.clone(),
&request.query,
request.limit,
threshold_used,
request.project.clone(),
request.path.clone(),
true,
request.file_extensions.clone(),
request.languages.clone(),
request.path_patterns.clone(),
)
.await
.context("Failed to search with filters")?;
// Adaptive threshold lowering if no results found
if results.is_empty() && original_threshold > 0.3 {
let fallback_thresholds = [0.6, 0.5, 0.4, 0.3];
for &threshold in &fallback_thresholds {
if threshold >= original_threshold {
continue;
}
results = self
.vector_db
.search_filtered(
query_embedding.clone(),
&request.query,
request.limit,
threshold,
request.project.clone(),
request.path.clone(),
true,
request.file_extensions.clone(),
request.languages.clone(),
request.path_patterns.clone(),
)
.await
.context("Failed to search with filters")?;
if !results.is_empty() {
threshold_used = threshold;
threshold_lowered = true;
break;
}
}
}
Ok(QueryResponse {
results,
duration_ms: start.elapsed().as_millis() as u64,
threshold_used,
threshold_lowered,
})
}
/// Get statistics about the indexed codebase
pub async fn get_statistics(&self) -> Result<StatisticsResponse> {
let stats = self
.vector_db
.get_statistics()
.await
.context("Failed to get statistics")?;
let language_breakdown = stats
.language_breakdown
.into_iter()
.map(|(language, count)| LanguageStats {
language,
file_count: count,
chunk_count: count,
})
.collect();
Ok(StatisticsResponse {
total_files: stats.total_points,
total_chunks: stats.total_vectors,
total_embeddings: stats.total_vectors,
database_size_bytes: 0,
language_breakdown,
})
}
/// Clear all indexed data from the vector database
pub async fn clear_index(&self) -> Result<ClearResponse> {
match self.vector_db.clear().await {
Ok(_) => {
let mut cache = self.hash_cache.write().await;
cache.roots.clear();
if let Err(e) = cache.save(&self.cache_path) {
tracing::warn!("Failed to save cleared cache: {}", e);
}
if let Err(e) = self
.vector_db
.initialize(self.embedding_provider.dimension())
.await
{
Ok(ClearResponse {
success: false,
message: format!("Cleared but failed to reinitialize: {}", e),
})
} else {
Ok(ClearResponse {
success: true,
message: "Successfully cleared all indexed data and cache".to_string(),
})
}
}
Err(e) => Ok(ClearResponse {
success: false,
message: format!("Failed to clear index: {}", e),
}),
}
}
/// Search git commit history using semantic search
///
/// # Example
///
/// ```no_run
/// use project_rag::{RagClient, SearchGitHistoryRequest};
///
/// # async fn example() -> anyhow::Result<()> {
/// let client = RagClient::new().await?;
///
/// let request = SearchGitHistoryRequest {
/// query: "bug fix authentication".to_string(),
/// path: "/path/to/repo".to_string(),
/// project: None,
/// branch: None,
/// max_commits: 100,
/// limit: 10,
/// min_score: 0.7,
/// author: None,
/// since: None,
/// until: None,
/// file_pattern: None,
/// };
///
/// let response = client.search_git_history(request).await?;
/// for result in response.results {
/// println!("Commit {}: {}", result.commit_hash, result.commit_message);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn search_git_history(
&self,
request: SearchGitHistoryRequest,
) -> Result<SearchGitHistoryResponse> {
// Validate request
request.validate().map_err(|e| anyhow::anyhow!(e))?;
// Forward to git indexing implementation
git_indexing::do_search_git_history(
self.embedding_provider.clone(),
self.vector_db.clone(),
self.git_cache.clone(),
&self.git_cache_path,
request,
)
.await
}
/// Get the configuration used by this client
pub fn config(&self) -> &Config {
&self.config
}
/// Get the embedding dimension used by this client
pub fn embedding_dimension(&self) -> usize {
self.embedding_provider.dimension()
}
/// Find the definition of a symbol at a given file location
///
/// This method looks up the symbol at the specified location and returns
/// its definition information if found.
///
/// # Arguments
///
/// * `request` - The find definition request containing file path, line, and column
///
/// # Returns
///
/// A response containing the definition if found, along with precision info
pub async fn find_definition(&self, request: FindDefinitionRequest) -> Result<FindDefinitionResponse> {
let start = Instant::now();
// Validate request
request.validate().map_err(|e| anyhow::anyhow!(e))?;
// Create FileInfo for the file
let file_info = self.create_file_info(&request.file_path, request.project.clone())?;
// Get precision level for this language
let language = file_info.language.as_deref().unwrap_or("Unknown");
let precision = self.relations_provider.precision_level(language);
// Extract definitions from the file
let definitions = self
.relations_provider
.extract_definitions(&file_info)
.context("Failed to extract definitions")?;
// Find the definition at the requested position
let definition = definitions.into_iter().find(|def| {
request.line >= def.symbol_id.start_line
&& request.line <= def.end_line
&& (request.column == 0 || request.column >= def.symbol_id.start_col)
});
let result = definition.map(|def| DefinitionResult::from(&def));
Ok(FindDefinitionResponse {
definition: result,
precision: format!("{:?}", precision).to_lowercase(),
duration_ms: start.elapsed().as_millis() as u64,
})
}
/// Find all references to a symbol at a given file location
///
/// This method finds all locations where the symbol at the given position
/// is referenced throughout the indexed codebase.
///
/// # Arguments
///
/// * `request` - The find references request containing file path, line, column, and limit
///
/// # Returns
///
/// A response containing the list of references found
pub async fn find_references(&self, request: FindReferencesRequest) -> Result<FindReferencesResponse> {
let start = Instant::now();
// Validate request
request.validate().map_err(|e| anyhow::anyhow!(e))?;
// Create FileInfo for the file
let file_info = self.create_file_info(&request.file_path, request.project.clone())?;
// Get precision level for this language
let language = file_info.language.as_deref().unwrap_or("Unknown");
let precision = self.relations_provider.precision_level(language);
// Extract definitions from the file to find the symbol at the position
let definitions = self
.relations_provider
.extract_definitions(&file_info)
.context("Failed to extract definitions")?;
// Find the symbol at the requested position
let target_symbol = definitions.iter().find(|def| {
request.line >= def.symbol_id.start_line
&& request.line <= def.end_line
&& (request.column == 0 || request.column >= def.symbol_id.start_col)
});
let symbol_name = target_symbol.map(|def| def.symbol_id.name.clone());
// If no symbol found at position, return empty result
if symbol_name.is_none() {
return Ok(FindReferencesResponse {
symbol_name: None,
references: Vec::new(),
total_count: 0,
precision: format!("{:?}", precision).to_lowercase(),
duration_ms: start.elapsed().as_millis() as u64,
});
}
let symbol_name_str = symbol_name.clone().unwrap();
// Build symbol index from definitions
let mut symbol_index: std::collections::HashMap<String, Vec<crate::relations::Definition>> =
std::collections::HashMap::new();
for def in definitions {
symbol_index
.entry(def.symbol_id.name.clone())
.or_default()
.push(def);
}
// Find references in the same file
let references = self
.relations_provider
.extract_references(&file_info, &symbol_index)
.context("Failed to extract references")?;
// Filter to references matching our target symbol
let matching_refs: Vec<ReferenceResult> = references
.iter()
.filter(|r| {
// Check if this reference points to our target symbol
r.target_symbol_id.contains(&symbol_name_str)
})
.take(request.limit)
.map(|r| ReferenceResult::from(r))
.collect();
let total_count = matching_refs.len();
Ok(FindReferencesResponse {
symbol_name,
references: matching_refs,
total_count,
precision: format!("{:?}", precision).to_lowercase(),
duration_ms: start.elapsed().as_millis() as u64,
})
}
/// Get the call graph for a function at a given file location
///
/// This method returns the callers (incoming calls) and callees (outgoing calls)
/// for the function at the specified location.
///
/// # Arguments
///
/// * `request` - The call graph request containing file path, line, column, and depth
///
/// # Returns
///
/// A response containing the root symbol and its call graph
pub async fn get_call_graph(&self, request: GetCallGraphRequest) -> Result<GetCallGraphResponse> {
let start = Instant::now();
// Validate request
request.validate().map_err(|e| anyhow::anyhow!(e))?;
// Create FileInfo for the file
let file_info = self.create_file_info(&request.file_path, request.project.clone())?;
// Get precision level for this language
let language = file_info.language.as_deref().unwrap_or("Unknown");
let precision = self.relations_provider.precision_level(language);
// Extract definitions from the file to find the function at the position
let definitions = self
.relations_provider
.extract_definitions(&file_info)
.context("Failed to extract definitions")?;
// Find the function at the requested position
let target_function = definitions.iter().find(|def| {
// Only consider functions/methods
matches!(
def.symbol_id.kind,
crate::relations::SymbolKind::Function | crate::relations::SymbolKind::Method
) && request.line >= def.symbol_id.start_line
&& request.line <= def.end_line
&& (request.column == 0 || request.column >= def.symbol_id.start_col)
});
// If no function found at position, return empty result
let root_symbol = match target_function {
Some(func) => crate::relations::SymbolInfo {
name: func.symbol_id.name.clone(),
kind: func.symbol_id.kind.clone(),
file_path: request.file_path.clone(),
start_line: func.symbol_id.start_line,
end_line: func.end_line,
signature: func.signature.clone(),
},
None => {
return Ok(GetCallGraphResponse {
root_symbol: None,
callers: Vec::new(),
callees: Vec::new(),
precision: format!("{:?}", precision).to_lowercase(),
duration_ms: start.elapsed().as_millis() as u64,
});
}
};
let function_name = root_symbol.name.clone();
// Build symbol index from definitions
let mut symbol_index: std::collections::HashMap<String, Vec<crate::relations::Definition>> =
std::collections::HashMap::new();
for def in &definitions {
symbol_index
.entry(def.symbol_id.name.clone())
.or_default()
.push(def.clone());
}
// Find references in the same file to identify callers
let references = self
.relations_provider
.extract_references(&file_info, &symbol_index)
.context("Failed to extract references")?;
// Find callers (references with Call kind pointing to our function)
let mut seen_callers = std::collections::HashSet::new();
let callers: Vec<crate::relations::CallGraphNode> = references
.iter()
.filter(|r| {
r.reference_kind == crate::relations::ReferenceKind::Call
&& r.target_symbol_id.contains(&function_name)
})
.filter_map(|r| {
// Try to find which function contains this call
definitions.iter().find(|def| {
matches!(
def.symbol_id.kind,
crate::relations::SymbolKind::Function | crate::relations::SymbolKind::Method
) && r.start_line >= def.symbol_id.start_line
&& r.start_line <= def.end_line
})
})
.filter(|def| seen_callers.insert(def.symbol_id.name.clone()))
.map(|def| crate::relations::CallGraphNode {
name: def.symbol_id.name.clone(),
kind: def.symbol_id.kind.clone(),
file_path: request.file_path.clone(),
line: def.symbol_id.start_line,
children: Vec::new(),
})
.collect();
// Find callees (calls made from within our function)
let target_func = target_function.unwrap();
let mut seen_callees = std::collections::HashSet::new();
let callees: Vec<crate::relations::CallGraphNode> = references
.iter()
.filter(|r| {
r.reference_kind == crate::relations::ReferenceKind::Call
&& r.start_line >= target_func.symbol_id.start_line
&& r.start_line <= target_func.end_line
})
.filter_map(|r| {
// Extract the called function name from target_symbol_id
let parts: Vec<&str> = r.target_symbol_id.split(':').collect();
if parts.len() >= 2 {
Some(parts[1].to_string())
} else {
None
}
})
.filter(|name| seen_callees.insert(name.clone()))
.filter_map(|name| {
// Find the definition of the called function
symbol_index.get(&name).and_then(|defs| defs.first()).cloned()
})
.map(|def| crate::relations::CallGraphNode {
name: def.symbol_id.name.clone(),
kind: def.symbol_id.kind.clone(),
file_path: request.file_path.clone(),
line: def.symbol_id.start_line,
children: Vec::new(),
})
.collect();
Ok(GetCallGraphResponse {
root_symbol: Some(root_symbol),
callers,
callees,
precision: format!("{:?}", precision).to_lowercase(),
duration_ms: start.elapsed().as_millis() as u64,
})
}
}
// Indexing operations module
pub(crate) mod indexing;
// Git indexing operations module
pub(crate) mod git_indexing;
#[cfg(test)]
mod tests;