umi_memory/umi/
config.rs

1//! Memory Configuration
2//!
3//! TigerStyle: Sensible defaults, builder pattern, explicit over implicit.
4//!
5//! Provides global configuration for Memory system behavior.
6
7use std::time::Duration;
8
9// =============================================================================
10// MemoryConfig
11// =============================================================================
12
13/// Global configuration for Memory system.
14///
15/// TigerStyle:
16/// - Sensible defaults via Default impl
17/// - Builder pattern for customization
18/// - All fields public for transparency
19///
20/// # Example
21///
22/// ```rust
23/// use umi_memory::umi::MemoryConfig;
24///
25/// let config = MemoryConfig::default()
26///     .with_recall_limit(20)
27///     .without_embeddings();
28/// ```
29#[derive(Debug, Clone)]
30pub struct MemoryConfig {
31    /// Core memory size in bytes (always in LLM context).
32    ///
33    /// Default: 32KB
34    pub core_memory_bytes: usize,
35
36    /// Working memory size in bytes (session state with TTL).
37    ///
38    /// Default: 1MB
39    pub working_memory_bytes: usize,
40
41    /// Working memory time-to-live duration.
42    ///
43    /// Default: 1 hour
44    pub working_memory_ttl: Duration,
45
46    /// Whether to generate embeddings for entities.
47    ///
48    /// Default: true
49    pub generate_embeddings: bool,
50
51    /// Embedding batch size for bulk operations.
52    ///
53    /// Default: 100
54    pub embedding_batch_size: usize,
55
56    /// Default recall result limit.
57    ///
58    /// Default: 10
59    pub default_recall_limit: usize,
60
61    /// Whether to enable semantic (vector) search.
62    ///
63    /// Default: true
64    pub semantic_search_enabled: bool,
65
66    /// Whether to enable LLM query expansion for retrieval.
67    ///
68    /// Default: true (auto-enabled when beneficial)
69    pub query_expansion_enabled: bool,
70}
71
72impl Default for MemoryConfig {
73    fn default() -> Self {
74        Self {
75            core_memory_bytes: 32 * 1024,         // 32KB
76            working_memory_bytes: 1024 * 1024,    // 1MB
77            working_memory_ttl: Duration::from_secs(3600), // 1 hour
78            generate_embeddings: true,
79            embedding_batch_size: 100,
80            default_recall_limit: 10,
81            semantic_search_enabled: true,
82            query_expansion_enabled: true,
83        }
84    }
85}
86
87impl MemoryConfig {
88    /// Create a new config with default values.
89    #[must_use]
90    pub fn new() -> Self {
91        Self::default()
92    }
93
94    /// Set core memory size in bytes.
95    ///
96    /// # Arguments
97    /// - `bytes` - Core memory size
98    #[must_use]
99    pub fn with_core_memory_bytes(mut self, bytes: usize) -> Self {
100        self.core_memory_bytes = bytes;
101        self
102    }
103
104    /// Set working memory size in bytes.
105    ///
106    /// # Arguments
107    /// - `bytes` - Working memory size
108    #[must_use]
109    pub fn with_working_memory_bytes(mut self, bytes: usize) -> Self {
110        self.working_memory_bytes = bytes;
111        self
112    }
113
114    /// Set working memory TTL duration.
115    ///
116    /// # Arguments
117    /// - `ttl` - Time-to-live duration
118    #[must_use]
119    pub fn with_working_memory_ttl(mut self, ttl: Duration) -> Self {
120        self.working_memory_ttl = ttl;
121        self
122    }
123
124    /// Set default recall limit.
125    ///
126    /// # Arguments
127    /// - `limit` - Default recall result limit
128    #[must_use]
129    pub fn with_recall_limit(mut self, limit: usize) -> Self {
130        self.default_recall_limit = limit;
131        self
132    }
133
134    /// Set embedding batch size.
135    ///
136    /// # Arguments
137    /// - `size` - Batch size for embedding operations
138    #[must_use]
139    pub fn with_embedding_batch_size(mut self, size: usize) -> Self {
140        self.embedding_batch_size = size;
141        self
142    }
143
144    /// Disable embedding generation.
145    #[must_use]
146    pub fn without_embeddings(mut self) -> Self {
147        self.generate_embeddings = false;
148        self
149    }
150
151    /// Disable semantic (vector) search.
152    #[must_use]
153    pub fn without_semantic_search(mut self) -> Self {
154        self.semantic_search_enabled = false;
155        self
156    }
157
158    /// Disable query expansion.
159    #[must_use]
160    pub fn without_query_expansion(mut self) -> Self {
161        self.query_expansion_enabled = false;
162        self
163    }
164}
165
166// =============================================================================
167// Tests
168// =============================================================================
169
170#[cfg(test)]
171mod tests {
172    use super::*;
173
174    #[test]
175    fn test_default_values() {
176        let config = MemoryConfig::default();
177
178        assert_eq!(config.core_memory_bytes, 32 * 1024);
179        assert_eq!(config.working_memory_bytes, 1024 * 1024);
180        assert_eq!(config.default_recall_limit, 10);
181        assert_eq!(config.embedding_batch_size, 100);
182        assert!(config.generate_embeddings);
183        assert!(config.semantic_search_enabled);
184        assert!(config.query_expansion_enabled);
185    }
186
187    #[test]
188    fn test_builder_pattern() {
189        let config = MemoryConfig::default()
190            .with_core_memory_bytes(64 * 1024)
191            .with_recall_limit(20)
192            .without_embeddings();
193
194        assert_eq!(config.core_memory_bytes, 64 * 1024);
195        assert_eq!(config.default_recall_limit, 20);
196        assert!(!config.generate_embeddings);
197    }
198
199    #[test]
200    fn test_method_chaining() {
201        let config = MemoryConfig::new()
202            .with_core_memory_bytes(32 * 1024)
203            .with_working_memory_bytes(2 * 1024 * 1024)
204            .with_recall_limit(15)
205            .without_query_expansion();
206
207        assert_eq!(config.core_memory_bytes, 32 * 1024);
208        assert_eq!(config.working_memory_bytes, 2 * 1024 * 1024);
209        assert_eq!(config.default_recall_limit, 15);
210        assert!(!config.query_expansion_enabled);
211    }
212}