|
| 1 | +use crate::types::{QueryClass, RetrievalProfileId, RouteDecision, RoutePath}; |
| 2 | + |
| 3 | +pub fn route_query(query: &str) -> RouteDecision { |
| 4 | + let lower = query.to_ascii_lowercase(); |
| 5 | + |
| 6 | + let has_compare = |
| 7 | + lower.contains("compare ") || lower.contains("tradeoff") || lower.contains("vs "); |
| 8 | + let has_how_to = |
| 9 | + lower.contains("how do i") || lower.contains("runbook") || lower.contains("steps"); |
| 10 | + let has_ambiguity = lower.contains("which ") || lower.contains("difference between"); |
| 11 | + let has_exploratory = lower.contains("find ") |
| 12 | + || lower.contains("show me") |
| 13 | + || lower.contains("search for") |
| 14 | + || lower.contains("overview of") |
| 15 | + || lower.contains("documents about") |
| 16 | + || lower.contains("docs about"); |
| 17 | + let has_time = lower.contains("today") || lower.contains("latest") || lower.contains("current"); |
| 18 | + |
| 19 | + // QueryClass uses first-match precedence. We still retain every matched |
| 20 | + // heuristic in `reasons` below so run output shows the full signal set |
| 21 | + // that the deterministic router observed. |
| 22 | + let (selected_path, query_class, retrieval_profile, needs_multi_hop) = if has_compare { |
| 23 | + ( |
| 24 | + RoutePath::AgenticSearch, |
| 25 | + QueryClass::MultiHopResearch, |
| 26 | + RetrievalProfileId::BroadThenExpand, |
| 27 | + true, |
| 28 | + ) |
| 29 | + } else if has_how_to { |
| 30 | + (RoutePath::AgenticSearch, QueryClass::Procedural, RetrievalProfileId::LexicalFirst, false) |
| 31 | + } else if has_ambiguity { |
| 32 | + ( |
| 33 | + RoutePath::AgenticSearch, |
| 34 | + QueryClass::AmbiguityDisambiguation, |
| 35 | + RetrievalProfileId::LexicalFirst, |
| 36 | + false, |
| 37 | + ) |
| 38 | + } else if has_exploratory { |
| 39 | + ( |
| 40 | + RoutePath::AgenticSearch, |
| 41 | + QueryClass::ExploratorySearch, |
| 42 | + RetrievalProfileId::BroadThenExpand, |
| 43 | + false, |
| 44 | + ) |
| 45 | + } else { |
| 46 | + (RoutePath::SinglePassRag, QueryClass::SimpleFact, RetrievalProfileId::SimpleHybrid, false) |
| 47 | + }; |
| 48 | + |
| 49 | + let mut reasons = Vec::new(); |
| 50 | + if has_compare { |
| 51 | + reasons.push("compare".to_string()); |
| 52 | + } |
| 53 | + if has_how_to { |
| 54 | + reasons.push("procedural".to_string()); |
| 55 | + } |
| 56 | + if has_ambiguity { |
| 57 | + reasons.push("ambiguity".to_string()); |
| 58 | + } |
| 59 | + if has_exploratory { |
| 60 | + reasons.push("exploratory".to_string()); |
| 61 | + } |
| 62 | + if reasons.is_empty() { |
| 63 | + reasons.push("default".to_string()); |
| 64 | + } |
| 65 | + if has_time { |
| 66 | + reasons.push("time_sensitive".to_string()); |
| 67 | + } |
| 68 | + |
| 69 | + RouteDecision { |
| 70 | + selected_path, |
| 71 | + query_class, |
| 72 | + retrieval_profile, |
| 73 | + ambiguity: has_ambiguity, |
| 74 | + needs_multi_hop, |
| 75 | + needs_high_evidence: has_compare || has_how_to || has_exploratory, |
| 76 | + time_sensitive: has_time, |
| 77 | + normalized_filters: Vec::new(), |
| 78 | + reasons, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[cfg(test)] |
| 83 | +mod tests { |
| 84 | + use super::*; |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn simple_fact_routes_to_single_pass_rag() { |
| 88 | + let decision = route_query("What is Rust?"); |
| 89 | + assert_eq!(decision.selected_path, RoutePath::SinglePassRag); |
| 90 | + assert_eq!(decision.query_class, QueryClass::SimpleFact); |
| 91 | + assert_eq!(decision.retrieval_profile, RetrievalProfileId::SimpleHybrid); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn multi_hop_query_routes_to_agentic_search() { |
| 96 | + let decision = route_query("Compare Rust and Python tradeoffs for async services"); |
| 97 | + assert_eq!(decision.selected_path, RoutePath::AgenticSearch); |
| 98 | + assert_eq!(decision.query_class, QueryClass::MultiHopResearch); |
| 99 | + assert_eq!(decision.retrieval_profile, RetrievalProfileId::BroadThenExpand); |
| 100 | + assert!(decision.needs_multi_hop); |
| 101 | + } |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn procedural_query_prefers_lexical_first() { |
| 105 | + let decision = route_query("How do I rotate API keys in the auth runbook?"); |
| 106 | + assert_eq!(decision.selected_path, RoutePath::AgenticSearch); |
| 107 | + assert_eq!(decision.query_class, QueryClass::Procedural); |
| 108 | + assert_eq!(decision.retrieval_profile, RetrievalProfileId::LexicalFirst); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn multi_signal_queries_use_first_match_precedence() { |
| 113 | + let decision = route_query("Compare steps to rotate API keys"); |
| 114 | + assert_eq!(decision.selected_path, RoutePath::AgenticSearch); |
| 115 | + assert_eq!(decision.query_class, QueryClass::MultiHopResearch); |
| 116 | + assert_eq!(decision.retrieval_profile, RetrievalProfileId::BroadThenExpand); |
| 117 | + assert_eq!(decision.reasons, vec!["compare".to_string(), "procedural".to_string()]); |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn exploratory_query_routes_to_agentic_search() { |
| 122 | + let decision = route_query("Find documents about rollback timing for release incidents"); |
| 123 | + assert_eq!(decision.selected_path, RoutePath::AgenticSearch); |
| 124 | + assert_eq!(decision.query_class, QueryClass::ExploratorySearch); |
| 125 | + assert_eq!(decision.retrieval_profile, RetrievalProfileId::BroadThenExpand); |
| 126 | + } |
| 127 | +} |
0 commit comments