ares-http 0.11.0

HTTP adapter plugin for ARES (Axum router, auth, overlay)
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
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
//! 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::{tenant_allowlist as allowlist, AresVectorStore, VectorStore},
    rag::{
        chunker::{ChunkingStrategy, TextChunker},
        search::{HybridWeights, SearchEngine, SearchStrategy},
    },
    types::{
        AppError, Document, DocumentMetadata, RagDeleteCollectionRequest,
        RagDeleteCollectionResponse, RagIngestRequest, RagIngestResponse, RagSearchRequest,
        RagSearchResponse, RagSearchResult,
    },
    HttpError, Result,
};
#[cfg(feature = "local-embeddings")]
use crate::rag::{
    embeddings::{EmbeddingModelType, EmbeddingService},
    reranker::{Reranker, RerankerConfig, RerankerModelType},
};
use axum::{extract::State, Json};
use chrono::Utc;
use cordis::Context;
use std::sync::Arc;
use std::time::Instant;
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())
}

fn filter_collections_by_allowed_sources(
    collections: Vec<ares_store::CollectionInfo>,
    allowed_sources: &[allowlist::TenantRagAllowlistItem],
) -> Vec<ares_store::CollectionInfo> {
    if allowed_sources.is_empty() {
        return Vec::new();
    }

    let allowed: std::collections::HashSet<&str> = allowed_sources
        .iter()
        .filter(|source| source.enabled)
        .map(|source| source.rag_source.as_str())
        .collect();

    collections
        .into_iter()
        .filter(|info| allowed.contains(info.name.as_str()))
        .collect()
}

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

/// Construct EmbeddingService without a process-global cache.
///
/// Pre-downloads model files via lancor (reqwest) before fastembed init,
/// because fastembed's hf-hub/ureq client fails on HuggingFace's xethub CDN.
/// Context is the singleton: callers must `provide_arc` after construction.
#[cfg(feature = "local-embeddings")]
async fn construct_embedding_service() -> ares_types::types::Result<Arc<EmbeddingService>> {
    // 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(Arc::new(service))
}

/// Resolve EmbeddingService from Context, constructing on miss.
/// Construction (model download) happens on first RAG request, not at boot.
/// Context is the singleton; there is no process-global cache.
#[cfg(feature = "local-embeddings")]
pub async fn embedding_service_from_ctx(
    ctx: &std::sync::Arc<cordis::Context>,
) -> ares_types::types::Result<Arc<EmbeddingService>> {
    if let Some(existing) = ctx.get::<EmbeddingService>() {
        return Ok(existing);
    }
    let created = construct_embedding_service().await?;
    ctx.provide_arc(created.clone());
    Ok(created)
}

/// Resolve AresVectorStore from Context, constructing on miss.
/// Context is the singleton; there is no process-global cache.
pub async fn vector_store_from_ctx(
    ctx: &std::sync::Arc<cordis::Context>,
    vector_path: &str,
) -> ares_types::types::Result<Arc<AresVectorStore>> {
    if let Some(existing) = ctx.get::<AresVectorStore>() {
        return Ok(existing);
    }
    let store = Arc::new(AresVectorStore::new(Some(vector_path.to_string())).await?);
    ctx.provide_arc(store.clone());
    Ok(store)
}

/// Embed texts for RAG ingest and search.
///
/// Local path uses [`EmbeddingService`]. Remote path uses [`ares_rag::embed_with_llm`].
async fn embed_for_rag(
    ctx: &Arc<Context>,
    texts: &[String],
) -> ares_types::types::Result<Vec<Vec<f32>>> {
    #[cfg(feature = "local-embeddings")]
    {
        let embedding_service = embedding_service_from_ctx(ctx).await?;
        embedding_service.embed_texts(texts).await
    }
    #[cfg(not(feature = "local-embeddings"))]
    {
        ares_rag::embed_with_llm(ctx, texts).await
    }
}

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

/// Ingest a document into the RAG system.
///
/// Chunks the document and stores embeddings for later retrieval.
pub async fn ingest(
    State(ctx): State<Arc<Context>>,
    AuthUser(claims): AuthUser,
    Json(payload): Json<RagIngestRequest>,
) -> Result<Json<RagIngestResponse>> {
    let start = Instant::now();

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

    let pool = ctx
        .get::<ares_store::TenantDb>()
        .expect("not provided")
        .pool()
        .clone();
    let allowlist_store = allowlist::TenantAllowlistStore::new(&pool);
    if !allowlist_store
        .is_rag_source_allowed(&claims.sub, &payload.collection)
        .await?
    {
        return Err(HttpError::from(AppError::Auth(
            format!(
                "RAG source '{}' is not allowed for this tenant",
                payload.collection
            )
            .into(),
        )));
    }

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

    // Get services
    let config = ctx
        .get::<crate::overlay::AresConfigManager>()
        .expect("not provided")
        .config();
    let vector_path = &config.rag.vector.vector_path;
    let vector_store = vector_store_from_ctx(&ctx, 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(HttpError::from(AppError::InvalidInput(
            "Content too small to chunk".to_string(),
        )));
    }

    // Generate embeddings for each chunk, then size the collection from the first vector.
    let chunk_texts: Vec<String> = chunks.iter().map(|c| c.content.clone()).collect();
    let embeddings = embed_for_rag(&ctx, &chunk_texts).await?;
    let dimensions = embeddings
        .first()
        .map(|v| v.len())
        .ok_or_else(|| AppError::Internal("No embedding generated".to_string()))?;
    if !vector_store.collection_exists(&scoped_collection).await? {
        vector_store
            .create_collection(&scoped_collection, dimensions)
            .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.
pub async fn search(
    State(ctx): State<Arc<Context>>,
    AuthUser(claims): AuthUser,
    Json(payload): Json<RagSearchRequest>,
) -> Result<Json<RagSearchResponse>> {
    let start = Instant::now();
    // Respect RAG feature flag
    if !ctx
        .get::<crate::overlay::AresConfigManager>()
        .expect("not provided")
        .config()
        .rag
        .vector
        .enabled
    {
        return Err(HttpError::from(AppError::FeatureDisabled(
            "RAG feature is disabled. Set `[rag.vector] enabled = true` in ares.toml".into(),
        )));
    }

    let pool = ctx
        .get::<ares_store::TenantDb>()
        .expect("not provided")
        .pool()
        .clone();
    let allowlist_store = allowlist::TenantAllowlistStore::new(&pool);
    if !allowlist_store
        .is_rag_source_allowed(&claims.sub, &payload.collection)
        .await?
    {
        return Err(HttpError::from(AppError::Auth(
            format!(
                "RAG source '{}' is not allowed for this tenant",
                payload.collection
            )
            .into(),
        )));
    }

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

    // Get services
    let config = ctx
        .get::<crate::overlay::AresConfigManager>()
        .expect("not provided")
        .config();
    let vector_path = &config.rag.vector.vector_path;
    let vector_store = vector_store_from_ctx(&ctx, vector_path).await?;

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

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

    // Generate query embedding
    let embeddings = embed_for_rag(&ctx, std::slice::from_ref(&payload.query)).await?;
    let query_embedding = embeddings
        .into_iter()
        .next()
        .ok_or_else(|| AppError::Internal("No embedding generated".to_string()))?;

    // 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
    #[cfg_attr(not(feature = "local-embeddings"), allow(unused_mut))]
    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. Remote embeddings skip the cross-encoder.
    #[cfg(feature = "local-embeddings")]
    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
    };
    #[cfg(not(feature = "local-embeddings"))]
    let reranked = 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.
pub async fn delete_collection(
    State(ctx): State<Arc<Context>>,
    AuthUser(claims): AuthUser,
    Json(payload): Json<RagDeleteCollectionRequest>,
) -> Result<Json<RagDeleteCollectionResponse>> {
    // Validate input
    if payload.collection.is_empty() {
        return Err(HttpError::from(AppError::InvalidInput(
            "Collection name required".to_string(),
        )));
    }

    let pool = ctx
        .get::<ares_store::TenantDb>()
        .expect("not provided")
        .pool()
        .clone();
    let allowlist_store = allowlist::TenantAllowlistStore::new(&pool);
    if !allowlist_store
        .is_rag_source_allowed(&claims.sub, &payload.collection)
        .await?
    {
        return Err(HttpError::from(AppError::Auth(
            format!(
                "RAG source '{}' is not allowed for this tenant",
                payload.collection
            )
            .into(),
        )));
    }

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

    let config = ctx
        .get::<crate::overlay::AresConfigManager>()
        .expect("not provided")
        .config();
    let vector_path = &config.rag.vector.vector_path;
    let vector_store = vector_store_from_ctx(&ctx, vector_path).await?;

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

    // 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.
pub async fn list_collections(
    State(ctx): State<Arc<Context>>,
    AuthUser(claims): AuthUser,
) -> Result<Json<Vec<ares_store::CollectionInfo>>> {
    let config = ctx
        .get::<crate::overlay::AresConfigManager>()
        .expect("not provided")
        .config();
    let vector_path = &config.rag.vector.vector_path;
    let vector_store = vector_store_from_ctx(&ctx, 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();

    let pool = ctx
        .get::<ares_store::TenantDb>()
        .expect("not provided")
        .pool()
        .clone();
    let allowlist_store = allowlist::TenantAllowlistStore::new(&pool);
    let db_sources = allowlist_store.list_rag_sources(&claims.sub).await?;
    let user_collections = filter_collections_by_allowed_sources(user_collections, &db_sources);

    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);
    }

    #[test]
    fn test_list_collections_allowlist_filtering() {
        let collections = vec![
            ares_store::CollectionInfo {
                name: "docs".into(),
                document_count: 1,
                dimensions: 384,
            },
            ares_store::CollectionInfo {
                name: "images".into(),
                document_count: 2,
                dimensions: 384,
            },
            ares_store::CollectionInfo {
                name: "wiki".into(),
                document_count: 3,
                dimensions: 384,
            },
        ];
        let allowed_sources = vec![
            allowlist::TenantRagAllowlistItem {
                id: "allow-1".into(),
                tenant_id: "tenant-1".into(),
                rag_source: "docs".into(),
                enabled: true,
                created_at: 1,
                updated_at: 1,
            },
            allowlist::TenantRagAllowlistItem {
                id: "allow-2".into(),
                tenant_id: "tenant-1".into(),
                rag_source: "wiki".into(),
                enabled: true,
                created_at: 1,
                updated_at: 1,
            },
            allowlist::TenantRagAllowlistItem {
                id: "disabled-1".into(),
                tenant_id: "tenant-1".into(),
                rag_source: "images".into(),
                enabled: false,
                created_at: 1,
                updated_at: 1,
            },
        ];

        let filtered = filter_collections_by_allowed_sources(collections, &allowed_sources);
        assert_eq!(filtered.len(), 2);
        assert!(filtered.iter().any(|c| c.name == "docs"));
        assert!(filtered.iter().any(|c| c.name == "wiki"));
        assert!(!filtered.iter().any(|c| c.name == "images"));
    }

    #[test]
    fn test_list_collections_empty_allowlist_default_denies() {
        let collections = vec![ares_store::CollectionInfo {
            name: "docs".into(),
            document_count: 1,
            dimensions: 384,
        }];

        let filtered = filter_collections_by_allowed_sources(collections, &[]);
        assert!(filtered.is_empty());
    }

    #[cfg(feature = "local-embeddings")]
    #[tokio::test]
    async fn embedding_service_from_ctx_none_without_provide() {
        let ctx = cordis::Context::new_root();
        // Reuse is ctx.get / provide_arc; do not construct here (model init is heavy).
        assert!(ctx.get::<EmbeddingService>().is_none());
    }

    #[tokio::test]
    async fn vector_store_from_ctx_none_without_provide() {
        let ctx = cordis::Context::new_root();
        assert!(ctx.get::<AresVectorStore>().is_none());
    }

    #[test]
    fn ares_vector_store_impls_cordis_service() {
        fn assert_service<T: cordis::Service>() {}
        assert_service::<AresVectorStore>();
    }

    #[cfg(feature = "local-embeddings")]
    #[tokio::test(flavor = "multi_thread")]
    async fn embedding_service_from_ctx_reuses_provided_instance() {
        let ctx = cordis::Context::new_root();
        let service = Arc::new(EmbeddingService::with_default_model().expect("default model"));
        ctx.provide_arc(service.clone());
        let got = embedding_service_from_ctx(&ctx).await.expect("from ctx");
        assert!(Arc::ptr_eq(&service, &got));
    }

    #[cfg(feature = "local-embeddings")]
    #[test]
    fn embedding_service_impls_cordis_service() {
        fn assert_service<T: cordis::Service>() {}
        assert_service::<EmbeddingService>();
    }

    #[cfg(not(feature = "local-embeddings"))]
    #[tokio::test]
    async fn embed_for_rag_errors_when_llm_missing() {
        let ctx = Context::new_root();
        let err = embed_for_rag(&ctx, &[String::from("hello")])
            .await
            .expect_err("missing Llm must fail closed");
        match err {
            AppError::Configuration(msg) => {
                assert_eq!(msg, "Llm service is not provided for remote embeddings");
            }
            other => panic!("expected Configuration, got {other:?}"),
        }
    }

    #[cfg(not(feature = "local-embeddings"))]
    #[tokio::test]
    async fn embed_for_rag_llm_short_circuit_returns_vectors() {
        use ares_llm::{ConversationMessage, LLMClient, LLMResponse, Llm};
        use ares_types::types::ToolDefinition;
        use async_trait::async_trait;
        use cordis::EventsService;

        struct DummyEmbedClient;

        #[async_trait]
        impl LLMClient for DummyEmbedClient {
            async fn generate(&self, _prompt: &str) -> ares_types::types::Result<String> {
                Err(AppError::Internal("embed-only mock".into()))
            }
            async fn generate_with_system(
                &self,
                _system: &str,
                _prompt: &str,
            ) -> ares_types::types::Result<String> {
                Err(AppError::Internal("embed-only mock".into()))
            }
            async fn generate_with_history(
                &self,
                _messages: &[(String, String)],
            ) -> ares_types::types::Result<LLMResponse> {
                Err(AppError::Internal("embed-only mock".into()))
            }
            async fn generate_with_tools(
                &self,
                _prompt: &str,
                _tools: &[ToolDefinition],
            ) -> ares_types::types::Result<LLMResponse> {
                Err(AppError::Internal("embed-only mock".into()))
            }
            async fn generate_with_tools_and_history(
                &self,
                _messages: &[ConversationMessage],
                _tools: &[ToolDefinition],
            ) -> ares_types::types::Result<LLMResponse> {
                Err(AppError::Internal("embed-only mock".into()))
            }
            async fn stream(
                &self,
                _prompt: &str,
            ) -> ares_types::types::Result<
                Box<dyn futures::Stream<Item = ares_types::types::Result<String>> + Send + Unpin>,
            > {
                Err(AppError::Internal("embed-only mock".into()))
            }
            async fn stream_with_system(
                &self,
                _system: &str,
                _prompt: &str,
            ) -> ares_types::types::Result<
                Box<dyn futures::Stream<Item = ares_types::types::Result<String>> + Send + Unpin>,
            > {
                Err(AppError::Internal("embed-only mock".into()))
            }
            async fn stream_with_history(
                &self,
                _messages: &[(String, String)],
            ) -> ares_types::types::Result<
                Box<dyn futures::Stream<Item = ares_types::types::Result<String>> + Send + Unpin>,
            > {
                Err(AppError::Internal("embed-only mock".into()))
            }
            fn model_name(&self) -> &str {
                "embed-mock"
            }
        }

        let ctx = Context::new_root();
        ctx.provide(Llm::from_client(Arc::new(DummyEmbedClient)));
        let events = ctx.provide(EventsService::new());
        events.on_waterfall(
            cordis::events_catalog::ev::LLM_EMBED.to_string(),
            |_payload, _next| async move { Ok(serde_json::json!({ "embeddings": [[1.0, 2.0]] })) },
        );
        let out = embed_for_rag(&ctx, &[String::from("hi")])
            .await
            .expect("short-circuit embed");
        assert_eq!(out, vec![vec![1.0, 2.0]]);
    }
}