ares-server 0.7.5

A.R.E.S - Agentic Retrieval Enhanced Server: A production-grade agentic chatbot server with multi-provider LLM support, tool calling, RAG, and MCP integration
Documentation
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
//! RAG (Retrieval Augmented Generation) API handlers.
//!
//! Provides endpoints for:
//! - Document ingestion with chunking
//! - Multi-strategy search (semantic, BM25, fuzzy, hybrid)
//! - Collection management

use crate::{
    auth::middleware::AuthUser,
    db::{AresVectorStore, VectorStore},
    rag::{
        chunker::{ChunkingStrategy, TextChunker},
        embeddings::{EmbeddingModelType, EmbeddingService},
        reranker::{Reranker, RerankerConfig, RerankerModelType},
        search::{HybridWeights, SearchEngine, SearchStrategy},
    },
    types::{
        AppError, Document, DocumentMetadata, RagDeleteCollectionRequest,
        RagDeleteCollectionResponse, RagIngestRequest, RagIngestResponse, RagSearchRequest,
        RagSearchResponse, RagSearchResult, Result,
    },
    AppState,
};
use axum::{extract::State, Json};
use chrono::Utc;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::OnceCell;
use uuid::Uuid;

// ============================================================================
// User Isolation
// ============================================================================

/// Prefix collection name with user ID for isolation.
/// All RAG collections are scoped per-user to prevent data leakage.
fn user_scoped_collection(user_id: &str, collection: &str) -> String {
    format!("user_{}_{}", user_id, collection)
}

/// Extract user-friendly collection name from scoped name.
/// Returns None if the collection doesn't belong to the user.
fn extract_user_collection(user_id: &str, scoped_name: &str) -> Option<String> {
    let prefix = format!("user_{}_", user_id);
    scoped_name.strip_prefix(&prefix).map(|s| s.to_string())
}

// ============================================================================
// Shared RAG Services
// ============================================================================

/// Global embedding service (lazy initialized).
static EMBEDDING_SERVICE: OnceCell<Arc<EmbeddingService>> = OnceCell::const_new();

/// Get or create the embedding service.
///
/// Pre-downloads model files via lancor (reqwest) before fastembed init,
/// because fastembed's hf-hub/ureq client fails on HuggingFace's xethub CDN.
async fn get_embedding_service() -> Result<Arc<EmbeddingService>> {
    EMBEDDING_SERVICE
        .get_or_try_init(|| async {
            // Pre-download ONNX model via lancor before fastembed tries with ureq
            let model = EmbeddingModelType::default();
            let cache_dir = std::env::var("FASTEMBED_CACHE_DIR")
                .unwrap_or_else(|_| ".fastembed_cache".to_string());
            let cache_path = std::path::PathBuf::from(&cache_dir);

            match lancor::hub::HubClient::with_cache_dir(cache_path.clone()) {
                Err(e) => tracing::error!("Failed to create lancor HubClient: {}", e),
                Ok(hub) => {
                let repo_id = model.hf_repo_id();
                for filename in &["onnx/model.onnx", "tokenizer.json", "config.json", "tokenizer_config.json"] {
                    // Build HF cache path
                    let folder = format!("models--{}", repo_id.replace('/', "--"));
                    let snapshot_dir = cache_path.join(&folder).join("snapshots").join("lancor");
                    let target = snapshot_dir.join(filename);

                    if target.exists() && std::fs::metadata(&target).map(|m| m.len() > 0).unwrap_or(false) {
                        tracing::debug!("Model file cached: {}", target.display());
                        continue;
                    }

                    tracing::info!("Downloading {}/{} via lancor...", repo_id, filename);
                    if let Some(parent) = target.parent() {
                        std::fs::create_dir_all(parent).ok();
                    }

                    match hub.download(repo_id, filename, None).await {
                        Ok(dl_path) => {
                            if dl_path != target {
                                std::fs::copy(&dl_path, &target).ok();
                            }
                            tracing::info!("Downloaded: {} ({} bytes)", filename,
                                std::fs::metadata(&target).map(|m| m.len()).unwrap_or(0));
                        }
                        Err(e) => tracing::warn!("Could not download {}: {}", filename, e),
                    }
                }

                // Write refs/main
                let refs_dir = cache_path.join(format!("models--{}", repo_id.replace('/', "--"))).join("refs");
                std::fs::create_dir_all(&refs_dir).ok();
                std::fs::write(refs_dir.join("main"), "lancor").ok();
                }
            }

            let service = EmbeddingService::with_model(model)
                .map_err(|e| AppError::Internal(format!("Failed to init embeddings: {}", e)))?;
            Ok::<_, AppError>(Arc::new(service))
        })
        .await
        .cloned()
}

/// Global vector store (lazy initialized).
/// Uses a Mutex to allow late initialization with config-driven path.
static VECTOR_STORE: OnceCell<Arc<AresVectorStore>> = OnceCell::const_new();

/// Get or create the vector store with the configured path.
/// The path is read from config on first initialization.
async fn get_vector_store(vector_path: &str) -> Result<Arc<AresVectorStore>> {
    VECTOR_STORE
        .get_or_try_init(|| async {
            let store = AresVectorStore::new(Some(vector_path.to_string())).await?;
            Ok::<_, AppError>(Arc::new(store))
        })
        .await
        .cloned()
}

// ============================================================================
// Ingest Endpoint
// ============================================================================

/// Ingest a document into the RAG system.
///
/// Chunks the document and stores embeddings for later retrieval.
#[utoipa::path(
    post,
    path = "/api/rag/ingest",
    request_body = RagIngestRequest,
    responses(
        (status = 200, description = "Document ingested successfully", body = RagIngestResponse),
        (status = 400, description = "Invalid request"),
        (status = 401, description = "Unauthorized"),
        (status = 500, description = "Internal server error")
    ),
    tag = "rag",
    security(("bearer" = []))
)]
pub async fn ingest(
    State(state): State<AppState>,
    AuthUser(claims): AuthUser,
    Json(payload): Json<RagIngestRequest>,
) -> Result<Json<RagIngestResponse>> {
    let start = Instant::now();

    // Validate input
    if payload.collection.is_empty() {
        return Err(AppError::InvalidInput("Collection name required".into()));
    }
    if payload.content.is_empty() {
        return Err(AppError::InvalidInput("Content required".into()));
    }

    // Scope collection to user for isolation
    let scoped_collection = user_scoped_collection(&claims.sub, &payload.collection);

    // Get services
    let embedding_service = get_embedding_service().await?;
    let vector_path = &state.config_manager.config().rag.vector_path;
    let vector_store = get_vector_store(vector_path).await?;

    // Parse chunking strategy
    let strategy: ChunkingStrategy = payload
        .chunking_strategy
        .as_ref()
        .map(|s| s.parse())
        .transpose()?
        .unwrap_or_default();

    // Create chunker
    let chunker = match strategy {
        ChunkingStrategy::Word => TextChunker::with_word_chunking(200, 50),
        ChunkingStrategy::Semantic => TextChunker::with_semantic_chunking(500),
        ChunkingStrategy::Character => TextChunker::with_character_chunking(500, 100),
    };

    // Chunk the content
    let chunks = chunker.chunk_with_metadata(&payload.content);

    if chunks.is_empty() {
        return Err(AppError::InvalidInput("Content too small to chunk".into()));
    }

    // Ensure collection exists
    let dimensions = embedding_service.dimensions();
    if !vector_store.collection_exists(&scoped_collection).await? {
        vector_store
            .create_collection(&scoped_collection, dimensions)
            .await?;
    }

    // Generate embeddings for each chunk
    let chunk_texts: Vec<String> = chunks.iter().map(|c| c.content.clone()).collect();
    let embeddings = embedding_service.embed_texts(&chunk_texts).await?;

    // Create documents
    let base_id = Uuid::new_v4().to_string();
    let mut documents = Vec::with_capacity(chunks.len());
    let mut document_ids = Vec::with_capacity(chunks.len());

    for (i, (chunk, embedding)) in chunks.iter().zip(embeddings.into_iter()).enumerate() {
        let doc_id = format!("{}_{}", base_id, i);
        document_ids.push(doc_id.clone());

        documents.push(Document {
            id: doc_id,
            content: chunk.content.clone(),
            metadata: DocumentMetadata {
                title: payload.title.clone().unwrap_or_default(),
                source: payload.source.clone().unwrap_or_default(),
                created_at: Utc::now(),
                tags: payload.tags.clone(),
            },
            embedding: Some(embedding),
        });
    }

    // Upsert to vector store
    let count = vector_store.upsert(&scoped_collection, &documents).await?;

    tracing::info!(
        user_id = %claims.sub,
        collection = %payload.collection,
        scoped_collection = %scoped_collection,
        chunks = count,
        duration_ms = start.elapsed().as_millis() as u64,
        "Document ingested"
    );

    Ok(Json(RagIngestResponse {
        chunks_created: count,
        document_ids,
        collection: payload.collection, // Return user-facing name, not scoped
    }))
}

// ============================================================================
// Search Endpoint
// ============================================================================

/// Search the RAG system.
///
/// Supports multiple search strategies: semantic, BM25, fuzzy, and hybrid.
#[utoipa::path(
    post,
    path = "/api/rag/search",
    request_body = RagSearchRequest,
    responses(
        (status = 200, description = "Search completed", body = RagSearchResponse),
        (status = 400, description = "Invalid request"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Collection not found"),
        (status = 500, description = "Internal server error")
    ),
    tag = "rag",
    security(("bearer" = []))
)]
pub async fn search(
    State(state): State<AppState>,
    AuthUser(claims): AuthUser,
    Json(payload): Json<RagSearchRequest>,
) -> Result<Json<RagSearchResponse>> {
    let start = Instant::now();

    // Validate input
    if payload.collection.is_empty() {
        return Err(AppError::InvalidInput("Collection name required".into()));
    }
    if payload.query.is_empty() {
        return Err(AppError::InvalidInput("Query required".into()));
    }

    // Scope collection to user for isolation
    let scoped_collection = user_scoped_collection(&claims.sub, &payload.collection);

    // Get services
    let embedding_service = get_embedding_service().await?;
    let vector_path = &state.config_manager.config().rag.vector_path;
    let vector_store = get_vector_store(vector_path).await?;

    // Check collection exists
    if !vector_store.collection_exists(&scoped_collection).await? {
        return Err(AppError::NotFound(format!(
            "Collection '{}' not found",
            payload.collection
        )));
    }

    // Parse search strategy
    let strategy: SearchStrategy = payload
        .strategy
        .as_ref()
        .map(|s| s.parse())
        .transpose()?
        .unwrap_or(SearchStrategy::Semantic);

    // Generate query embedding
    let query_embedding = embedding_service.embed_text(&payload.query).await?;

    // Perform vector search
    let vector_results = vector_store
        .search(
            &scoped_collection,
            &query_embedding,
            payload.limit * 2, // Fetch extra for filtering/reranking
            payload.threshold,
        )
        .await?;

    // Apply additional search strategies if needed
    let mut results: Vec<RagSearchResult> = match strategy {
        SearchStrategy::Semantic => {
            // Pure semantic search - already done
            vector_results
                .iter()
                .take(payload.limit)
                .map(|r| RagSearchResult {
                    id: r.document.id.clone(),
                    content: r.document.content.clone(),
                    score: r.score,
                    metadata: r.document.metadata.clone(),
                })
                .collect()
        }
        SearchStrategy::Bm25 | SearchStrategy::Fuzzy | SearchStrategy::Hybrid => {
            // For BM25, fuzzy, or hybrid, we need to build an index over the results
            let mut search_engine = SearchEngine::new();

            // Index the vector search results as Document structs
            for r in &vector_results {
                search_engine.index_document(&r.document);
            }

            // Get strategy-specific results
            let strategy_results = match strategy {
                SearchStrategy::Bm25 => search_engine.search_bm25(&payload.query, payload.limit),
                SearchStrategy::Fuzzy => search_engine.search_fuzzy(&payload.query, payload.limit),
                SearchStrategy::Hybrid => {
                    // Combine semantic and BM25 using hybrid search
                    let semantic_scores: Vec<_> = vector_results
                        .iter()
                        .map(|r| (r.document.id.clone(), r.score))
                        .collect();
                    let weights = HybridWeights::default();
                    search_engine.search_hybrid(
                        &payload.query,
                        &semantic_scores,
                        &weights,
                        payload.limit,
                    )
                }
                _ => vec![], // Already handled above
            };

            // Map back to full documents
            strategy_results
                .iter()
                .filter_map(|(id, score)| {
                    vector_results
                        .iter()
                        .find(|r| r.document.id == *id)
                        .map(|r| RagSearchResult {
                            id: r.document.id.clone(),
                            content: r.document.content.clone(),
                            score: *score,
                            metadata: r.document.metadata.clone(),
                        })
                })
                .collect()
        }
    };

    // Apply reranking if requested
    let reranked = if payload.rerank && !results.is_empty() {
        // Parse reranker model
        let model_type: RerankerModelType = payload
            .reranker_model
            .as_ref()
            .map(|s| s.parse())
            .transpose()?
            .unwrap_or_default();

        // Create reranker with config
        let config = RerankerConfig {
            model: model_type,
            ..Default::default()
        };
        let reranker = Reranker::new(config);

        // Prepare results for reranking: (id, content, score)
        let rerank_input: Vec<_> = results
            .iter()
            .map(|r| (r.id.clone(), r.content.clone(), r.score))
            .collect();

        // Rerank results
        let reranked_results = reranker
            .rerank(&payload.query, &rerank_input, Some(payload.limit))
            .await
            .map_err(|e| AppError::Internal(format!("Reranking failed: {}", e)))?;

        // Convert to RagSearchResult
        results = reranked_results
            .into_iter()
            .filter_map(|rr| {
                results
                    .iter()
                    .find(|r| r.id == rr.id)
                    .map(|r| RagSearchResult {
                        id: r.id.clone(),
                        content: r.content.clone(),
                        score: rr.final_score,
                        metadata: r.metadata.clone(),
                    })
            })
            .collect();
        true
    } else {
        false
    };

    let total = results.len();
    let strategy_name = format!("{:?}", strategy).to_lowercase();

    tracing::info!(
        user_id = %claims.sub,
        collection = %payload.collection,
        strategy = %strategy_name,
        results = total,
        reranked = reranked,
        duration_ms = start.elapsed().as_millis() as u64,
        "Search completed"
    );

    Ok(Json(RagSearchResponse {
        results,
        total,
        strategy: strategy_name,
        reranked,
        duration_ms: start.elapsed().as_millis() as u64,
    }))
}

// ============================================================================
// Delete Collection Endpoint
// ============================================================================

/// Delete a RAG collection.
#[utoipa::path(
    delete,
    path = "/api/rag/collection",
    request_body = RagDeleteCollectionRequest,
    responses(
        (status = 200, description = "Collection deleted", body = RagDeleteCollectionResponse),
        (status = 400, description = "Invalid request"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Collection not found"),
        (status = 500, description = "Internal server error")
    ),
    tag = "rag",
    security(("bearer" = []))
)]
pub async fn delete_collection(
    State(state): State<AppState>,
    AuthUser(claims): AuthUser,
    Json(payload): Json<RagDeleteCollectionRequest>,
) -> Result<Json<RagDeleteCollectionResponse>> {
    // Validate input
    if payload.collection.is_empty() {
        return Err(AppError::InvalidInput("Collection name required".into()));
    }

    // Scope collection to user for isolation
    let scoped_collection = user_scoped_collection(&claims.sub, &payload.collection);

    let vector_path = &state.config_manager.config().rag.vector_path;
    let vector_store = get_vector_store(vector_path).await?;

    // Check collection exists
    if !vector_store.collection_exists(&scoped_collection).await? {
        return Err(AppError::NotFound(format!(
            "Collection '{}' not found",
            payload.collection
        )));
    }

    // Get document count before deletion
    let stats = vector_store.collection_stats(&scoped_collection).await?;
    let doc_count = stats.document_count;

    // Delete the collection
    vector_store.delete_collection(&scoped_collection).await?;

    tracing::info!(
        user_id = %claims.sub,
        collection = %payload.collection,
        documents = doc_count,
        "Collection deleted"
    );

    Ok(Json(RagDeleteCollectionResponse {
        success: true,
        collection: payload.collection, // Return user-facing name
        documents_deleted: doc_count,
    }))
}

// ============================================================================
// List Collections Endpoint
// ============================================================================

/// List all RAG collections.
#[utoipa::path(
    get,
    path = "/api/rag/collections",
    responses(
        (status = 200, description = "Collections listed", body = Vec<String>),
        (status = 401, description = "Unauthorized"),
        (status = 500, description = "Internal server error")
    ),
    tag = "rag",
    security(("bearer" = []))
)]
pub async fn list_collections(
    State(state): State<AppState>,
    AuthUser(claims): AuthUser,
) -> Result<Json<Vec<crate::db::CollectionInfo>>> {
    let vector_path = &state.config_manager.config().rag.vector_path;
    let vector_store = get_vector_store(vector_path).await?;
    let all_collections = vector_store.list_collections().await?;

    // Filter to only collections belonging to this user and unscope names
    let user_collections: Vec<_> = all_collections
        .into_iter()
        .filter_map(|mut info| {
            extract_user_collection(&claims.sub, &info.name).map(|user_name| {
                info.name = user_name;
                info
            })
        })
        .collect();

    Ok(Json(user_collections))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_search_strategy() {
        let strategy: SearchStrategy = "semantic".parse().unwrap();
        assert_eq!(strategy, SearchStrategy::Semantic);

        let strategy: SearchStrategy = "bm25".parse().unwrap();
        assert_eq!(strategy, SearchStrategy::Bm25);

        let strategy: SearchStrategy = "hybrid".parse().unwrap();
        assert_eq!(strategy, SearchStrategy::Hybrid);
    }

    #[test]
    fn test_default_chunking_strategy() {
        let strategy: ChunkingStrategy = "word".parse().unwrap();
        assert_eq!(strategy, ChunkingStrategy::Word);

        let strategy: ChunkingStrategy = "semantic".parse().unwrap();
        assert_eq!(strategy, ChunkingStrategy::Semantic);
    }
}