fetchium_core/research/amrs/
decompose.rs1#[derive(Debug, Clone, PartialEq)]
5pub enum QueryStatus {
6 Pending,
7 InProgress,
8 Complete,
9 Failed(String),
10}
11
12#[derive(Debug, Clone)]
14pub struct QueryNode {
15 pub query: String,
16 pub depth: usize,
17 pub parent: Option<usize>,
19 pub children: Vec<usize>,
21 pub status: QueryStatus,
22}
23
24pub fn decompose_query(query: &str, max_depth: usize) -> Vec<QueryNode> {
34 let mut nodes = Vec::new();
35
36 nodes.push(QueryNode {
38 query: query.to_string(),
39 depth: 0,
40 parent: None,
41 children: Vec::new(),
42 status: QueryStatus::Pending,
43 });
44
45 if max_depth == 0 {
46 return nodes;
47 }
48
49 let lower = query.to_lowercase();
50
51 if lower.contains(" vs ") || lower.contains(" versus ") || lower.contains("compare ") {
53 let parts = split_comparison(query);
54 if parts.len() > 1 {
55 for part in parts {
56 let child_idx = nodes.len();
57 nodes.push(QueryNode {
58 query: part,
59 depth: 1,
60 parent: Some(0),
61 children: Vec::new(),
62 status: QueryStatus::Pending,
63 });
64 nodes[0].children.push(child_idx);
65 }
66 }
67 }
68 else if lower.contains("implications")
70 || lower.contains("pros and cons")
71 || lower.contains("advantages and disadvantages")
72 {
73 let aspects = extract_aspects(query);
74 for aspect in aspects {
75 let child_idx = nodes.len();
76 nodes.push(QueryNode {
77 query: aspect,
78 depth: 1,
79 parent: Some(0),
80 children: Vec::new(),
81 status: QueryStatus::Pending,
82 });
83 nodes[0].children.push(child_idx);
84 }
85 }
86
87 nodes
88}
89
90fn split_comparison(query: &str) -> Vec<String> {
91 let lower = query.to_lowercase();
92
93 if lower.contains(" vs ") {
94 return query.split(" vs ").map(|s| s.trim().to_string()).collect();
95 }
96 if lower.contains(" versus ") {
97 return query
98 .split(" versus ")
99 .map(|s| s.trim().to_string())
100 .collect();
101 }
102 if lower.starts_with("compare ") || lower.starts_with("comparing ") {
104 let stripped = lower
105 .trim_start_matches("comparing ")
106 .trim_start_matches("compare ");
107 return stripped
108 .split(" and ")
109 .map(|s| s.trim().to_string())
110 .filter(|s| !s.is_empty())
111 .collect();
112 }
113
114 vec![query.to_string()]
115}
116
117fn extract_aspects(query: &str) -> Vec<String> {
118 let base = query
119 .to_lowercase()
120 .replace("implications of ", "")
121 .replace("pros and cons of ", "")
122 .replace("advantages and disadvantages of ", "");
123 let base = base.trim();
124 vec![
125 format!("benefits of {base}"),
126 format!("drawbacks of {base}"),
127 format!("current state of {base}"),
128 ]
129}
130
131#[cfg(test)]
132mod tests {
133 use super::*;
134
135 #[test]
136 fn simple_query_single_root() {
137 let nodes = decompose_query("what is Rust", 3);
138 assert_eq!(nodes.len(), 1);
139 assert_eq!(nodes[0].query, "what is Rust");
140 assert!(nodes[0].children.is_empty());
141 }
142
143 #[test]
144 fn vs_query_decomposes_to_two_children() {
145 let nodes = decompose_query("Rust vs Go performance", 3);
146 assert!(nodes.len() >= 2);
147 assert!(!nodes[0].children.is_empty());
148 }
149
150 #[test]
151 fn implications_decomposes_to_aspects() {
152 let nodes = decompose_query("implications of AI in healthcare", 3);
153 assert!(nodes.len() > 1);
155 }
156
157 #[test]
158 fn max_depth_zero_returns_root_only() {
159 let nodes = decompose_query("Rust vs Go", 0);
160 assert_eq!(nodes.len(), 1);
161 }
162}