lc-agents 0.14.0

Agent system for langchainrust — ReAct, FunctionCalling, PlanExecute, CRAG, AdaptiveRAG, DeepResearch, Handoffs, Streaming
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
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
// src/agents/adaptive_rag.rs
//! Adaptive RAG implementation.
//!
//! Uses an LLM to decide whether retrieval is needed and what strategy to use.
//! Three decision branches:
//! - **NoRetrieval**: The query can be answered from general knowledge.
//! - **SingleSearch**: A single search is sufficient.
//! - **MultiQuery**: The query is complex and needs multiple search angles.

use lc_core::language_models::BaseChatModel;
use lc_core::tools::ToolDefinition;
use lc_rag::{RetrieverError, RetrieverTrait};
use lc_schema::Message;
use lc_vector_stores::Document;
use serde_json::json;

// ---------------------------------------------------------------------------
// Decision enum
// ---------------------------------------------------------------------------

/// Decision made by the adaptive router.
#[derive(Debug, Clone, PartialEq)]
pub enum RagDecision {
    /// No retrieval needed - LLM can answer directly.
    NoRetrieval,
    /// Single search query sufficient.
    SingleSearch,
    /// Complex query - use multi-query expansion.
    MultiQuery,
}

impl std::fmt::Display for RagDecision {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RagDecision::NoRetrieval => write!(f, "no_retrieval"),
            RagDecision::SingleSearch => write!(f, "single_search"),
            RagDecision::MultiQuery => write!(f, "multi_query"),
        }
    }
}

// ---------------------------------------------------------------------------
// Result
// ---------------------------------------------------------------------------

/// Result returned by [`AdaptiveRAG::invoke`].
#[derive(Debug, Clone)]
pub struct AdaptiveRAGResult {
    /// The generated answer.
    pub answer: String,
    /// The routing decision that was made.
    pub decision: RagDecision,
    /// Source documents used (empty when `decision` is `NoRetrieval`).
    pub sources: Vec<Document>,
}

// ---------------------------------------------------------------------------
// Error
// ---------------------------------------------------------------------------

/// Errors produced by [`AdaptiveRAG`].
#[derive(Debug, thiserror::Error)]
pub enum AdaptiveRAGError {
    /// LLM invocation failed.
    #[error("LLM error: {0}")]
    Llm(String),

    /// Retrieval failed.
    #[error("retrieval error: {0}")]
    Retrieval(#[from] RetrieverError),

    /// Failed to parse the routing decision from the LLM response.
    #[error("decision parse error: {0}")]
    DecisionParse(String),
}

// ---------------------------------------------------------------------------
// AdaptiveRAG
// ---------------------------------------------------------------------------

/// Adaptive RAG that routes queries to the most appropriate strategy.
///
/// # Overview
///
/// 1. The LLM classifies the query into one of three buckets:
///    `no_retrieval`, `single_search`, or `multi_query`.
/// 2. Based on the decision:
///    - **NoRetrieval**: call the LLM directly.
///    - **SingleSearch**: retrieve documents, then generate.
///    - **MultiQuery**: generate multiple query variants, retrieve for each,
///      merge results, then generate.
///
/// # Example
///
/// ```ignore
/// use langchainrust::agents::adaptive_rag::{AdaptiveRAG, RagDecision};
/// use langchainrust::OpenAIChat;
/// use langchainrust::retrieval::SimilarityRetriever;
///
/// let rag = AdaptiveRAG::new(llm, retriever);
/// let result = rag.invoke("What is the capital of France?").await?;
/// assert_eq!(result.decision, RagDecision::NoRetrieval);
/// ```
pub struct AdaptiveRAG<M: BaseChatModel, R: RetrieverTrait> {
    llm: M,
    retriever: R,
    /// Number of documents to retrieve per query.
    retrieve_k: usize,
    /// Number of alternative queries to generate for multi-query mode.
    multi_query_count: usize,
}

// ---------------------------------------------------------------------------
// Routing prompt
// ---------------------------------------------------------------------------

const ROUTING_PROMPT: &str = r#"Given the following query, decide whether retrieval is needed:
- "no_retrieval": The query can be answered from general knowledge
- "single_search": A single search is sufficient
- "multi_query": The query is complex and needs multiple search angles

Query: {query}

Respond with exactly one of: no_retrieval, single_search, multi_query"#;

// ---------------------------------------------------------------------------
// Generation prompts
// ---------------------------------------------------------------------------

const GENERATE_SYSTEM_PROMPT: &str = r#"You are a helpful assistant. Answer the user's question based on the provided context when available. If no context is provided, use your general knowledge. Be concise and accurate."#;

const MULTI_QUERY_PROMPT: &str = r#"You are an AI language model assistant. Your task is to generate {count} different versions of the given user question to retrieve relevant documents from a vector database.

By generating multiple perspectives on the user question, your goal is to help overcome some of the limitations of distance-based similarity search.

Provide these alternative questions separated by newlines.

Original question: {question}

Alternative questions:"#;

// ---------------------------------------------------------------------------
// Impl
// ---------------------------------------------------------------------------

impl<M: BaseChatModel, R: RetrieverTrait> AdaptiveRAG<M, R> {
    /// Creates a new `AdaptiveRAG` with the given LLM and retriever.
    pub fn new(llm: M, retriever: R) -> Self {
        Self {
            llm,
            retriever,
            retrieve_k: 4,
            multi_query_count: 3,
        }
    }

    /// Sets the number of documents to retrieve per query.
    pub fn with_retrieve_k(mut self, k: usize) -> Self {
        self.retrieve_k = k;
        self
    }

    /// Sets the number of alternative queries for multi-query mode.
    pub fn with_multi_query_count(mut self, count: usize) -> Self {
        self.multi_query_count = count;
        self
    }

    /// Invokes the adaptive RAG pipeline for the given query.
    pub async fn invoke(&self, query: &str) -> Result<AdaptiveRAGResult, AdaptiveRAGError> {
        // Step 1: Route the query.
        let decision = self.route(query).await?;

        match decision {
            RagDecision::NoRetrieval => self.generate_no_retrieval(query).await,
            RagDecision::SingleSearch => self.generate_single_search(query).await,
            RagDecision::MultiQuery => self.generate_multi_query(query).await,
        }
    }

    /// Streams the AdaptiveRAG execution, emitting pipeline step events.
    ///
    /// Emits `AgentStreamEvent::PipelineStep` events for routing and generation,
    /// and `AgentStreamEvent::FinalAnswer` when the answer is ready.
    pub async fn stream(
        &self,
        query: &str,
    ) -> Result<
        std::pin::Pin<
            Box<dyn futures_util::Stream<Item = crate::streaming::AgentStreamEvent> + Send>,
        >,
        AdaptiveRAGError,
    > {
        use crate::streaming::AgentStreamEvent;

        let mut events: Vec<AgentStreamEvent> = Vec::new();

        // Step 1: Route
        events.push(AgentStreamEvent::PipelineStep {
            step: "routing".to_string(),
            detail: Some("Classifying query...".to_string()),
        });

        let decision = self.route(query).await?;

        events.push(AgentStreamEvent::PipelineStep {
            step: "routed".to_string(),
            detail: Some(format!("Decision: {}", decision)),
        });

        // Step 2: Execute based on decision
        let result = match decision {
            RagDecision::NoRetrieval => {
                events.push(AgentStreamEvent::PipelineStep {
                    step: "generating".to_string(),
                    detail: Some("No retrieval needed, generating directly...".to_string()),
                });
                self.generate_no_retrieval(query).await?
            }
            RagDecision::SingleSearch => {
                events.push(AgentStreamEvent::PipelineStep {
                    step: "retrieving".to_string(),
                    detail: Some("Single search retrieval...".to_string()),
                });
                let result = self.generate_single_search(query).await?;
                events.push(AgentStreamEvent::PipelineStep {
                    step: "generating".to_string(),
                    detail: Some(format!("Sources: {} documents", result.sources.len())),
                });
                result
            }
            RagDecision::MultiQuery => {
                events.push(AgentStreamEvent::PipelineStep {
                    step: "multi_query".to_string(),
                    detail: Some("Generating multiple queries...".to_string()),
                });
                let result = self.generate_multi_query(query).await?;
                events.push(AgentStreamEvent::PipelineStep {
                    step: "generating".to_string(),
                    detail: Some(format!("Sources: {} documents", result.sources.len())),
                });
                result
            }
        };

        // Final answer
        events.push(AgentStreamEvent::FinalAnswer {
            content: result.answer,
        });

        Ok(Box::pin(futures_util::stream::iter(events)))
    }

    // -- Routing -----------------------------------------------------------

    /// Asks the LLM to classify the query.
    async fn route(&self, query: &str) -> Result<RagDecision, AdaptiveRAGError> {
        let prompt = ROUTING_PROMPT.replace("{query}", query);
        let messages = vec![Message::human(&prompt)];

        // P1-3:优先 tool_calls 结构化路由,不支持绑定时回落文本解析。
        let structured = crate::structured::chat_structured(
            &self.llm,
            Some(route_tool()),
            messages,
            None,
            &crate::retry::RetryConfig::default(),
        )
        .await
        .map_err(|e| AdaptiveRAGError::Llm(e.to_string()))?;

        if let Some(args) = &structured.tool_args {
            if let Some(decision) = args.get("decision").and_then(|v| v.as_str()) {
                return parse_decision(decision);
            }
        }
        parse_decision(&structured.content)
    }

    // -- No retrieval ------------------------------------------------------

    /// Generates an answer without any retrieval.
    async fn generate_no_retrieval(
        &self,
        query: &str,
    ) -> Result<AdaptiveRAGResult, AdaptiveRAGError> {
        let messages = vec![Message::human(query)];
        let result = self
            .llm
            .chat_with_system(GENERATE_SYSTEM_PROMPT.to_string(), messages)
            .await
            .map_err(|e| AdaptiveRAGError::Llm(e.to_string()))?;

        Ok(AdaptiveRAGResult {
            answer: result.content,
            decision: RagDecision::NoRetrieval,
            sources: Vec::new(),
        })
    }

    // -- Single search -----------------------------------------------------

    /// Retrieves documents with a single query and generates an answer.
    async fn generate_single_search(
        &self,
        query: &str,
    ) -> Result<AdaptiveRAGResult, AdaptiveRAGError> {
        let docs = self.retriever.retrieve(query, self.retrieve_k).await?;

        let context = build_context(&docs);
        let user_msg = format!("Context:\n{}\n\nQuestion: {}\n\nAnswer:", context, query);
        let messages = vec![Message::human(&user_msg)];

        let result = self
            .llm
            .chat_with_system(GENERATE_SYSTEM_PROMPT.to_string(), messages)
            .await
            .map_err(|e| AdaptiveRAGError::Llm(e.to_string()))?;

        Ok(AdaptiveRAGResult {
            answer: result.content,
            decision: RagDecision::SingleSearch,
            sources: docs,
        })
    }

    // -- Multi query -------------------------------------------------------

    /// Generates multiple query variants, retrieves for each, merges, and
    /// generates an answer.
    async fn generate_multi_query(
        &self,
        query: &str,
    ) -> Result<AdaptiveRAGResult, AdaptiveRAGError> {
        let alternative_queries = self.generate_queries(query).await?;

        // Combine original + alternatives.
        let all_queries: Vec<String> = std::iter::once(query.to_string())
            .chain(alternative_queries)
            .collect();

        // Retrieve and merge.
        let docs = self.retrieve_and_merge(&all_queries).await?;

        let context = build_context(&docs);
        let user_msg = format!("Context:\n{}\n\nQuestion: {}\n\nAnswer:", context, query);
        let messages = vec![Message::human(&user_msg)];

        let result = self
            .llm
            .chat_with_system(GENERATE_SYSTEM_PROMPT.to_string(), messages)
            .await
            .map_err(|e| AdaptiveRAGError::Llm(e.to_string()))?;

        Ok(AdaptiveRAGResult {
            answer: result.content,
            decision: RagDecision::MultiQuery,
            sources: docs,
        })
    }

    /// Asks the LLM to generate alternative query variants.
    async fn generate_queries(&self, query: &str) -> Result<Vec<String>, AdaptiveRAGError> {
        let prompt = MULTI_QUERY_PROMPT
            .replace("{count}", &self.multi_query_count.to_string())
            .replace("{question}", query);

        let messages = vec![Message::human(&prompt)];
        let result = crate::retry::retry_chat(
            &self.llm,
            messages,
            None,
            &crate::retry::RetryConfig::default(),
        )
        .await
        .map_err(|e| AdaptiveRAGError::Llm(e.to_string()))?;

        let queries: Vec<String> = result
            .content
            .lines()
            .filter(|line| !line.trim().is_empty())
            .map(|line| line.trim().to_string())
            .collect();

        Ok(queries)
    }

    /// Retrieves documents for each query and merges/deduplicates by content.
    async fn retrieve_and_merge(
        &self,
        queries: &[String],
    ) -> Result<Vec<Document>, AdaptiveRAGError> {
        // M11: Parallel retrieval instead of sequential.
        let futures: Vec<_> = queries
            .iter()
            .map(|q| self.retriever.retrieve(q, self.retrieve_k))
            .collect();
        let all_results = futures_util::future::join_all(futures).await;

        let mut seen_content: std::collections::HashSet<String> = std::collections::HashSet::new();
        let mut merged: Vec<Document> = Vec::new();

        for result in all_results {
            let docs = result?;
            for doc in docs {
                // M6 fix: deduplicate by full content hash instead of first 80 chars
                // to avoid false collisions on documents with common prefixes.
                let key = {
                    use std::hash::Hasher;
                    let mut hasher = std::collections::hash_map::DefaultHasher::new();
                    hasher.write(doc.content.as_bytes());
                    format!("{:016x}", hasher.finish())
                };
                if seen_content.insert(key) {
                    merged.push(doc);
                }
            }
        }

        Ok(merged)
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// 路由工具定义:强制 LLM 输出三态决策(P1-3)。
fn route_tool() -> ToolDefinition {
    ToolDefinition::new(
        "route_decision",
        "判断查询是否需要检索及检索策略:no_retrieval / single_search / multi_query",
    )
    .with_parameters(json!({
        "type": "object",
        "properties": {
            "decision": {
                "type": "string",
                "enum": ["no_retrieval", "single_search", "multi_query"]
            }
        },
        "required": ["decision"]
    }))
}

/// Parses the routing decision from the LLM response.
fn parse_decision(response: &str) -> Result<RagDecision, AdaptiveRAGError> {
    let lower = response.to_lowercase();

    // Check for exact or contained keywords, with precedence.
    if lower.contains("no_retrieval") {
        return Ok(RagDecision::NoRetrieval);
    }
    if lower.contains("multi_query") {
        return Ok(RagDecision::MultiQuery);
    }
    if lower.contains("single_search") {
        return Ok(RagDecision::SingleSearch);
    }

    Err(AdaptiveRAGError::DecisionParse(response.to_string()))
}

/// Builds a context string from source documents.
fn build_context(docs: &[Document]) -> String {
    docs.iter()
        .enumerate()
        .map(|(i, doc)| format!("[Document {}]: {}", i + 1, doc.content))
        .collect::<Vec<_>>()
        .join("\n\n")
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use lc_core::language_models::{BaseChatModel, BaseLanguageModel, LLMResult};
    use lc_core::runnables::{Runnable, RunnableConfig};
    use std::sync::{Arc, Mutex};

    // -- Mock LLM ---------------------------------------------------------

    /// A mock LLM that returns pre-configured responses in sequence.
    #[derive(Clone)]
    struct MockLLM {
        responses: Arc<Mutex<Vec<String>>>,
    }

    impl MockLLM {
        fn new(responses: Vec<String>) -> Self {
            Self {
                responses: Arc::new(Mutex::new(responses)),
            }
        }
    }

    #[derive(Debug, thiserror::Error)]
    #[error("mock llm error")]
    struct MockLlmError;

    #[async_trait]
    impl Runnable<Vec<Message>, LLMResult> for MockLLM {
        type Error = MockLlmError;

        async fn invoke(
            &self,
            _input: Vec<Message>,
            _config: Option<RunnableConfig>,
        ) -> Result<LLMResult, Self::Error> {
            let content = {
                let mut guard = self.responses.lock().unwrap();
                if guard.is_empty() {
                    "mock response".to_string()
                } else {
                    guard.remove(0)
                }
            };
            Ok(LLMResult {
                content,
                model: "mock".to_string(),
                token_usage: None,
                tool_calls: None,
                thinking_content: None,
            })
        }
    }

    #[async_trait]
    impl BaseLanguageModel<Vec<Message>, LLMResult> for MockLLM {
        fn model_name(&self) -> &str {
            "mock"
        }

        fn get_num_tokens(&self, text: &str) -> usize {
            text.split_whitespace().count()
        }

        fn with_temperature(self, _temp: f32) -> Self
        where
            Self: Sized,
        {
            self
        }

        fn with_max_tokens(self, _max: usize) -> Self
        where
            Self: Sized,
        {
            self
        }
    }

    #[async_trait]
    impl BaseChatModel for MockLLM {
        async fn chat(
            &self,
            _messages: Vec<Message>,
            _config: Option<RunnableConfig>,
        ) -> Result<LLMResult, Self::Error> {
            let content = {
                let mut guard = self.responses.lock().unwrap();
                if guard.is_empty() {
                    "mock response".to_string()
                } else {
                    guard.remove(0)
                }
            };
            Ok(LLMResult {
                content,
                model: "mock".to_string(),
                token_usage: None,
                tool_calls: None,
                thinking_content: None,
            })
        }

        async fn stream_chat(
            &self,
            _messages: Vec<Message>,
            _config: Option<RunnableConfig>,
        ) -> Result<
            std::pin::Pin<Box<dyn futures_util::Stream<Item = Result<String, Self::Error>> + Send>>,
            Self::Error,
        > {
            let content = {
                let mut guard = self.responses.lock().unwrap();
                if guard.is_empty() {
                    "mock response".to_string()
                } else {
                    guard.remove(0)
                }
            };
            let stream = futures_util::stream::once(async move { Ok(content) });
            Ok(Box::pin(stream))
        }
    }

    /// P1-3:chat() 直接返回结构化 tool_call 决策的 mock(不走 bind_tools)。
    #[derive(Clone)]
    struct MockToolCallLLM {
        decision: String,
    }

    #[async_trait]
    impl Runnable<Vec<Message>, LLMResult> for MockToolCallLLM {
        type Error = MockLlmError;

        async fn invoke(
            &self,
            _input: Vec<Message>,
            _config: Option<RunnableConfig>,
        ) -> Result<LLMResult, Self::Error> {
            Ok(LLMResult {
                content: String::new(),
                model: "mock".to_string(),
                token_usage: None,
                tool_calls: Some(vec![lc_core::tools::ToolCall::new(
                    "call_route",
                    "route_decision",
                    format!(r#"{{"decision": "{}"}}"#, self.decision),
                )]),
                thinking_content: None,
            })
        }
    }

    #[async_trait]
    impl BaseLanguageModel<Vec<Message>, LLMResult> for MockToolCallLLM {
        fn model_name(&self) -> &str {
            "mock"
        }

        fn get_num_tokens(&self, text: &str) -> usize {
            text.split_whitespace().count()
        }

        fn with_temperature(self, _temp: f32) -> Self
        where
            Self: Sized,
        {
            self
        }

        fn with_max_tokens(self, _max: usize) -> Self
        where
            Self: Sized,
        {
            self
        }
    }

    #[async_trait]
    impl BaseChatModel for MockToolCallLLM {
        async fn chat(
            &self,
            _messages: Vec<Message>,
            _config: Option<RunnableConfig>,
        ) -> Result<LLMResult, Self::Error> {
            self.invoke(_messages, _config).await
        }

        async fn stream_chat(
            &self,
            _messages: Vec<Message>,
            _config: Option<RunnableConfig>,
        ) -> Result<
            std::pin::Pin<Box<dyn futures_util::Stream<Item = Result<String, Self::Error>> + Send>>,
            Self::Error,
        > {
            let decision = self.decision.clone();
            let stream = futures_util::stream::once(async move { Ok(decision) });
            Ok(Box::pin(stream))
        }
    }

    // -- Mock Retriever ---------------------------------------------------

    struct MockRetriever {
        documents: Vec<Document>,
    }

    impl MockRetriever {
        fn new(documents: Vec<Document>) -> Self {
            Self { documents }
        }
    }

    #[async_trait]
    impl RetrieverTrait for MockRetriever {
        async fn retrieve(&self, _query: &str, k: usize) -> Result<Vec<Document>, RetrieverError> {
            Ok(self.documents.iter().take(k).cloned().collect())
        }

        async fn retrieve_with_scores(
            &self,
            _query: &str,
            k: usize,
        ) -> Result<Vec<lc_vector_stores::SearchResult>, RetrieverError> {
            Ok(self
                .documents
                .iter()
                .take(k)
                .enumerate()
                .map(|(i, doc)| lc_vector_stores::SearchResult {
                    document: doc.clone(),
                    score: 1.0 - i as f32 * 0.1,
                })
                .collect())
        }

        async fn add_documents(&self, _documents: Vec<Document>) -> Result<(), RetrieverError> {
            Ok(())
        }
    }

    // -- parse_decision tests ----------------------------------------------

    #[test]
    fn test_parse_decision_no_retrieval() {
        let result = parse_decision("no_retrieval").unwrap();
        assert_eq!(result, RagDecision::NoRetrieval);
    }

    #[test]
    fn test_parse_decision_single_search() {
        let result = parse_decision("single_search").unwrap();
        assert_eq!(result, RagDecision::SingleSearch);
    }

    #[test]
    fn test_parse_decision_multi_query() {
        let result = parse_decision("multi_query").unwrap();
        assert_eq!(result, RagDecision::MultiQuery);
    }

    #[test]
    fn test_parse_decision_case_insensitive() {
        assert_eq!(
            parse_decision("NO_RETRIEVAL").unwrap(),
            RagDecision::NoRetrieval
        );
        assert_eq!(
            parse_decision("Single_Search").unwrap(),
            RagDecision::SingleSearch
        );
        assert_eq!(
            parse_decision("MULTI_QUERY").unwrap(),
            RagDecision::MultiQuery
        );
    }

    #[test]
    fn test_parse_decision_embedded_in_text() {
        assert_eq!(
            parse_decision("The answer is: no_retrieval").unwrap(),
            RagDecision::NoRetrieval
        );
        assert_eq!(
            parse_decision("I think multi_query is best").unwrap(),
            RagDecision::MultiQuery
        );
    }

    #[test]
    fn test_parse_decision_invalid() {
        let result = parse_decision("something else entirely");
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            AdaptiveRAGError::DecisionParse(_)
        ));
    }

    // -- RagDecision Display test ------------------------------------------

    #[tokio::test]
    async fn test_route_uses_tool_call_decision() {
        // P1-3:LLM 以 tool_calls 返回结构化决策,route 应直接采用,不靠文本关键字。
        let llm = MockToolCallLLM {
            decision: "multi_query".to_string(),
        };
        let retriever = MockRetriever::new(vec![]);
        let rag = AdaptiveRAG::new(llm, retriever);
        let decision = rag.route("some query").await.unwrap();
        assert_eq!(decision, RagDecision::MultiQuery);
    }

    #[test]
    fn test_route_tool_schema() {
        let tool = route_tool();
        assert_eq!(tool.function.name, "route_decision");
        assert!(tool.function.parameters.is_some());
    }

    #[test]
    fn test_rag_decision_display() {
        assert_eq!(format!("{}", RagDecision::NoRetrieval), "no_retrieval");
        assert_eq!(format!("{}", RagDecision::SingleSearch), "single_search");
        assert_eq!(format!("{}", RagDecision::MultiQuery), "multi_query");
    }

    // -- build_context test ------------------------------------------------

    #[test]
    fn test_build_context() {
        let docs = vec![
            Document::new("First document content"),
            Document::new("Second document content"),
        ];
        let context = build_context(&docs);
        assert!(context.contains("[Document 1]: First document content"));
        assert!(context.contains("[Document 2]: Second document content"));
    }

    #[test]
    fn test_build_context_empty() {
        let context = build_context(&[]);
        assert!(context.is_empty());
    }

    // -- Integration tests with mock LLM & retriever ----------------------

    #[tokio::test]
    async fn test_adaptive_rag_no_retrieval() {
        // Router returns "no_retrieval", then the generate call returns an answer.
        let llm = MockLLM::new(vec![
            "no_retrieval".to_string(),
            "Paris is the capital of France.".to_string(),
        ]);
        let retriever = MockRetriever::new(vec![Document::new("irrelevant doc")]);

        let rag = AdaptiveRAG::new(llm, retriever);
        let result = rag.invoke("What is the capital of France?").await.unwrap();

        assert_eq!(result.decision, RagDecision::NoRetrieval);
        assert!(result.answer.contains("Paris"));
        assert!(result.sources.is_empty());
    }

    #[tokio::test]
    async fn test_adaptive_rag_single_search() {
        // Router returns "single_search", then generate returns an answer.
        let llm = MockLLM::new(vec![
            "single_search".to_string(),
            "Rust is a systems programming language.".to_string(),
        ]);
        let retriever = MockRetriever::new(vec![Document::new(
            "Rust emphasizes safety and performance.",
        )]);

        let rag = AdaptiveRAG::new(llm, retriever);
        let result = rag.invoke("Tell me about Rust").await.unwrap();

        assert_eq!(result.decision, RagDecision::SingleSearch);
        assert!(result.answer.contains("Rust"));
        assert_eq!(result.sources.len(), 1);
    }

    #[tokio::test]
    async fn test_adaptive_rag_multi_query() {
        // Router returns "multi_query",
        // then generate_queries returns alternatives,
        // then generate returns the final answer.
        let llm = MockLLM::new(vec![
            "multi_query".to_string(),
            "How does Rust memory management work?\nWhat is ownership in Rust?".to_string(),
            "Rust uses ownership and borrowing for memory management.".to_string(),
        ]);
        let retriever = MockRetriever::new(vec![
            Document::new("Rust ownership model"),
            Document::new("Borrowing and lifetimes"),
        ]);

        let rag = AdaptiveRAG::new(llm, retriever);
        let result = rag.invoke("Explain Rust memory management").await.unwrap();

        assert_eq!(result.decision, RagDecision::MultiQuery);
        assert!(result.answer.contains("ownership"));
        assert!(!result.sources.is_empty());
    }

    #[tokio::test]
    async fn test_adaptive_rag_llm_error() {
        // Use a mock that returns an error by exhausting responses
        // and having the code call chat which would still succeed
        // with "mock response" (fallback). We test parse failure instead.
        let llm = MockLLM::new(vec!["something_unrelated".to_string()]);
        let retriever = MockRetriever::new(vec![]);

        let rag = AdaptiveRAG::new(llm, retriever);
        let result = rag.invoke("test query").await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            AdaptiveRAGError::DecisionParse(_)
        ));
    }

    #[tokio::test]
    async fn test_adaptive_rag_with_retrieve_k() {
        let llm = MockLLM::new(vec![
            "single_search".to_string(),
            "Answer based on context.".to_string(),
        ]);
        let retriever = MockRetriever::new(vec![
            Document::new("Doc 1"),
            Document::new("Doc 2"),
            Document::new("Doc 3"),
        ]);

        let rag = AdaptiveRAG::new(llm, retriever).with_retrieve_k(2);
        let result = rag.invoke("test query").await.unwrap();

        assert_eq!(result.decision, RagDecision::SingleSearch);
        assert_eq!(result.sources.len(), 2); // limited by retrieve_k
    }

    // -- Error Display tests -----------------------------------------------

    #[test]
    fn test_adaptive_rag_error_display() {
        let err = AdaptiveRAGError::Llm("timeout".to_string());
        assert!(err.to_string().contains("LLM error"));
        assert!(err.to_string().contains("timeout"));

        let err = AdaptiveRAGError::DecisionParse("bad output".to_string());
        assert!(err.to_string().contains("decision parse error"));
        assert!(err.to_string().contains("bad output"));
    }

    #[tokio::test]
    async fn test_adaptive_rag_stream_no_retrieval() {
        use futures_util::StreamExt;

        let llm = MockLLM::new(vec![
            "no_retrieval".to_string(),
            "Direct answer here.".to_string(),
        ]);
        let retriever = MockRetriever::new(vec![]);

        let agent = AdaptiveRAG::new(llm, retriever);

        let stream = agent.stream("What is 2+2?").await.unwrap();
        let events: Vec<_> = stream.collect().await;

        let step_names: Vec<&str> = events
            .iter()
            .filter_map(|e| match e {
                crate::streaming::AgentStreamEvent::PipelineStep { step, .. } => {
                    Some(step.as_str())
                }
                _ => None,
            })
            .collect();

        assert!(
            step_names.contains(&"routing"),
            "Expected 'routing' step, got: {:?}",
            step_names
        );
        assert!(
            step_names.contains(&"routed"),
            "Expected 'routed' step, got: {:?}",
            step_names
        );
        assert!(matches!(
            events.last().unwrap(),
            crate::streaming::AgentStreamEvent::FinalAnswer { .. }
        ));
    }

    #[tokio::test]
    async fn test_adaptive_rag_stream_single_search() {
        use futures_util::StreamExt;

        let llm = MockLLM::new(vec![
            "single_search".to_string(),
            "Relevance: relevant\nScore: 0.9\nReasoning: Direct match.".to_string(),
            "Rust is a systems programming language.".to_string(),
            "grounded".to_string(),
        ]);
        let retriever = MockRetriever::new(vec![Document::new("Rust is safe.")]);

        let agent = AdaptiveRAG::new(llm, retriever);

        let stream = agent.stream("What is Rust?").await.unwrap();
        let events: Vec<_> = stream.collect().await;

        let step_names: Vec<&str> = events
            .iter()
            .filter_map(|e| match e {
                crate::streaming::AgentStreamEvent::PipelineStep { step, .. } => {
                    Some(step.as_str())
                }
                _ => None,
            })
            .collect();

        assert!(
            step_names.contains(&"retrieving"),
            "Expected 'retrieving' step, got: {:?}",
            step_names
        );
        assert!(matches!(
            events.last().unwrap(),
            crate::streaming::AgentStreamEvent::FinalAnswer { .. }
        ));
    }
}