omega_memory/
query.rs

1//! Memory query types and execution
2
3use crate::MemoryTier;
4use serde::{Deserialize, Serialize};
5
6/// Memory query structure
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct Query {
9    /// Text-based search
10    pub text: Option<String>,
11
12    /// Vector embedding for similarity search
13    pub embedding: Option<Vec<f32>>,
14
15    /// Minimum importance threshold
16    pub min_importance: Option<f64>,
17
18    /// Maximum number of results
19    pub limit: Option<usize>,
20
21    /// Specific tiers to search (if None, searches all)
22    pub tiers: Option<Vec<MemoryTier>>,
23
24    /// Metadata filters
25    pub metadata: Option<serde_json::Value>,
26}
27
28impl Query {
29    pub fn new() -> Self {
30        Self {
31            text: None,
32            embedding: None,
33            min_importance: None,
34            limit: None,
35            tiers: None,
36            metadata: None,
37        }
38    }
39
40    pub fn with_text(mut self, text: impl Into<String>) -> Self {
41        self.text = Some(text.into());
42        self
43    }
44
45    pub fn with_embedding(mut self, embedding: Vec<f32>) -> Self {
46        self.embedding = Some(embedding);
47        self
48    }
49
50    pub fn with_min_importance(mut self, importance: f64) -> Self {
51        self.min_importance = Some(importance);
52        self
53    }
54
55    pub fn with_limit(mut self, limit: usize) -> Self {
56        self.limit = Some(limit);
57        self
58    }
59
60    pub fn with_tiers(mut self, tiers: Vec<MemoryTier>) -> Self {
61        self.tiers = Some(tiers);
62        self
63    }
64
65    pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self {
66        self.metadata = Some(metadata);
67        self
68    }
69}
70
71impl Default for Query {
72    fn default() -> Self {
73        Self::new()
74    }
75}
76
77/// Query builder for fluent API
78pub struct QueryBuilder {
79    query: Query,
80}
81
82impl QueryBuilder {
83    pub fn new() -> Self {
84        Self {
85            query: Query::new(),
86        }
87    }
88
89    pub fn text(mut self, text: impl Into<String>) -> Self {
90        self.query.text = Some(text.into());
91        self
92    }
93
94    pub fn embedding(mut self, embedding: Vec<f32>) -> Self {
95        self.query.embedding = Some(embedding);
96        self
97    }
98
99    pub fn min_importance(mut self, importance: f64) -> Self {
100        self.query.min_importance = Some(importance);
101        self
102    }
103
104    pub fn limit(mut self, limit: usize) -> Self {
105        self.query.limit = Some(limit);
106        self
107    }
108
109    pub fn tier(mut self, tier: MemoryTier) -> Self {
110        self.query.tiers = Some(vec![tier]);
111        self
112    }
113
114    pub fn tiers(mut self, tiers: Vec<MemoryTier>) -> Self {
115        self.query.tiers = Some(tiers);
116        self
117    }
118
119    pub fn instant(self) -> Self {
120        self.tier(MemoryTier::Instant)
121    }
122
123    pub fn session(self) -> Self {
124        self.tier(MemoryTier::Session)
125    }
126
127    pub fn episodic(self) -> Self {
128        self.tier(MemoryTier::Episodic)
129    }
130
131    pub fn semantic(self) -> Self {
132        self.tier(MemoryTier::Semantic)
133    }
134
135    pub fn individual(self) -> Self {
136        self.tiers(vec![
137            MemoryTier::Instant,
138            MemoryTier::Session,
139            MemoryTier::Episodic,
140            MemoryTier::Semantic,
141        ])
142    }
143
144    pub fn species(self) -> Self {
145        self.tiers(vec![
146            MemoryTier::Collective,
147            MemoryTier::Evolutionary,
148            MemoryTier::Architectural,
149            MemoryTier::Substrate,
150        ])
151    }
152
153    pub fn cosmic(self) -> Self {
154        self.tiers(vec![
155            MemoryTier::Civilizational,
156            MemoryTier::Temporal,
157            MemoryTier::Physical,
158            MemoryTier::Omega,
159        ])
160    }
161
162    pub fn all_tiers(self) -> Self {
163        self.tiers(MemoryTier::all())
164    }
165
166    pub fn metadata(mut self, metadata: serde_json::Value) -> Self {
167        self.query.metadata = Some(metadata);
168        self
169    }
170
171    pub fn build(self) -> Query {
172        self.query
173    }
174}
175
176impl Default for QueryBuilder {
177    fn default() -> Self {
178        Self::new()
179    }
180}
181
182#[cfg(test)]
183mod tests {
184    use super::*;
185
186    #[test]
187    fn test_query_builder() {
188        let query = QueryBuilder::new()
189            .text("test")
190            .min_importance(0.5)
191            .limit(10)
192            .instant()
193            .build();
194
195        assert_eq!(query.text, Some("test".to_string()));
196        assert_eq!(query.min_importance, Some(0.5));
197        assert_eq!(query.limit, Some(10));
198        assert_eq!(query.tiers, Some(vec![MemoryTier::Instant]));
199    }
200
201    #[test]
202    fn test_query_fluent_api() {
203        let query = Query::new()
204            .with_text("search")
205            .with_limit(5)
206            .with_min_importance(0.3);
207
208        assert_eq!(query.text, Some("search".to_string()));
209        assert_eq!(query.limit, Some(5));
210        assert_eq!(query.min_importance, Some(0.3));
211    }
212}