brainwires_rag/rag/client/mod.rs
1//! Core library client for brainwires-rag
2//!
3//! This module provides the main client interface for using brainwires-rag
4//! as a library in your own Rust applications.
5//!
6//! # Structure
7//!
8//! The implementation is split across focused submodules:
9//!
10//! - `constructor` — constructors, basic utilities, path helpers
11//! - `locking` — two-layer index locking (filesystem + in-process broadcast)
12//! - `search` — indexing dispatch, semantic search, filtered search, statistics, clear, git history
13//! - `ensemble` — multi-strategy ensemble query with Reciprocal Rank Fusion
14//! - `reranking` — pluggable diversity/relevance reranking (`spectral` feature)
15//! - `code_analysis` — find definition, find references, call graph (`code-analysis` feature)
16
17use crate::code_analysis::HybridRelationsProvider;
18use crate::rag::cache::HashCache;
19use crate::rag::config::Config;
20use crate::rag::embedding::FastEmbedManager;
21use crate::rag::git_cache::GitCache;
22use crate::rag::indexer::CodeChunker;
23use brainwires_storage::databases::VectorDatabase;
24
25use std::collections::HashMap;
26use std::path::PathBuf;
27use std::sync::Arc;
28use tokio::sync::RwLock;
29
30// Filesystem locking for cross-process coordination
31mod fs_lock;
32pub(crate) use fs_lock::FsLockGuard;
33
34// Index locking mechanism (uses fs_lock for cross-process, broadcast for in-process)
35mod index_lock;
36pub(crate) use index_lock::{IndexLockGuard, IndexLockResult, IndexingOperation};
37
38// ── impl blocks, split by concern ────────────────────────────────────────────
39
40/// Constructor methods and utility helpers.
41mod constructor;
42
43/// Two-layer index locking.
44mod locking;
45
46/// Indexing dispatch, search, statistics, clear, and git-history search.
47mod search;
48
49/// Multi-strategy ensemble query with RRF fusion.
50mod ensemble;
51
52/// Pluggable diversity/relevance reranking (requires `spectral`).
53mod reranking;
54
55/// Code-navigation: find definition, find references, call graph (requires `code-analysis`).
56mod code_analysis;
57
58// ── existing public submodules ────────────────────────────────────────────────
59
60/// Indexing operations (public for MCP server binary).
61pub mod indexing;
62
63// Git indexing operations module
64pub(crate) mod git_indexing;
65
66#[cfg(test)]
67mod tests;
68
69// ── Re-exports (keeps all existing call sites working) ────────────────────────
70
71// All public types from `types` are already glob-imported above via `use crate::rag::types::*`.
72// The public API surface of RagClient itself is fully re-exported through the struct below.
73
74/// Main client for interacting with the RAG system
75///
76/// This client provides a high-level API for indexing codebases and performing
77/// semantic searches. It contains all the core functionality and can be used
78/// directly as a library or wrapped by the MCP server.
79///
80/// # Example
81///
82/// ```ignore
83/// use crate::rag::{RagClient, IndexRequest, QueryRequest};
84///
85/// #[tokio::main]
86/// async fn main() -> anyhow::Result<()> {
87/// // Create client with default configuration
88/// let client = RagClient::new().await?;
89///
90/// // Index a codebase
91/// let index_req = IndexRequest {
92/// path: "/path/to/code".to_string(),
93/// project: Some("my-project".to_string()),
94/// include_patterns: vec!["**/*.rs".to_string()],
95/// exclude_patterns: vec!["**/target/**".to_string()],
96/// max_file_size: 1_048_576,
97/// };
98/// let response = client.index_codebase(index_req).await?;
99/// println!("Indexed {} files", response.files_indexed);
100///
101/// Ok(())
102/// }
103/// ```
104#[derive(Clone)]
105pub struct RagClient {
106 pub(crate) embedding_provider: Arc<FastEmbedManager>,
107 pub(crate) vector_db: Arc<dyn VectorDatabase>,
108 pub(crate) chunker: Arc<CodeChunker>,
109 // Persistent hash cache for incremental updates
110 pub(crate) hash_cache: Arc<RwLock<HashCache>>,
111 pub(crate) cache_path: PathBuf,
112 // Git cache for git history indexing
113 pub(crate) git_cache: Arc<RwLock<GitCache>>,
114 pub(crate) git_cache_path: PathBuf,
115 // Configuration (for accessing batch sizes, timeouts, etc.)
116 pub(crate) config: Arc<Config>,
117 // In-progress indexing operations (prevents concurrent indexing and allows result sharing)
118 pub(crate) indexing_ops: Arc<RwLock<HashMap<String, IndexingOperation>>>,
119 // Relations provider for code navigation (find definition, references, call graph)
120 pub(crate) relations_provider: Arc<HybridRelationsProvider>,
121}