codex-memory 3.0.15

A simple memory storage service with MCP interface for Claude Desktop
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
use crate::error::Result;
use regex::Regex;

/// A content chunk with semantic boundaries
#[derive(Debug, Clone)]
pub struct ContentChunk {
    pub content: String,
    pub start_byte: usize,
    pub end_byte: usize,
    pub chunk_index: usize,
}

/// Chunking strategy for semantic boundary preservation
#[derive(Debug, Clone, Default)]
pub enum ChunkingStrategy {
    /// Split by sentences (preserves sentence boundaries)
    Sentence,
    /// Split by paragraphs (preserves paragraph boundaries)
    Paragraph,
    /// Semantic boundaries using NLP heuristics
    Semantic,
    /// Hybrid approach: size-based with semantic boundary adjustment
    #[default]
    Hybrid,
}

impl std::str::FromStr for ChunkingStrategy {
    type Err = ();

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "sentence" => Ok(ChunkingStrategy::Sentence),
            "paragraph" => Ok(ChunkingStrategy::Paragraph),
            "semantic" => Ok(ChunkingStrategy::Semantic),
            "hybrid" => Ok(ChunkingStrategy::Hybrid),
            _ => Ok(ChunkingStrategy::Hybrid), // Default for unknown values
        }
    }
}

/// Advanced file chunker with semantic boundary preservation
pub struct FileChunker {
    chunk_size: usize,
    overlap_size: usize,
    strategy: ChunkingStrategy,
}

impl FileChunker {
    /// Create a new file chunker with specified chunk and overlap sizes
    pub fn new(chunk_size: usize, overlap_size: usize) -> Self {
        Self {
            chunk_size,
            overlap_size,
            strategy: ChunkingStrategy::default(),
        }
    }

    /// Create a new file chunker with specified strategy
    pub fn with_strategy(chunk_size: usize, overlap_size: usize, strategy: ChunkingStrategy) -> Self {
        Self {
            chunk_size,
            overlap_size,
            strategy,
        }
    }

    /// Create a default chunker with 8KB chunks and 200 byte overlap
    pub fn with_defaults() -> Self {
        Self::new(8192, 200)
    }

    /// Chunk content into overlapping pieces using the configured strategy
    pub fn chunk_content(&self, content: &str) -> Result<Vec<ContentChunk>> {
        match self.strategy {
            ChunkingStrategy::Sentence => self.chunk_by_sentences(content),
            ChunkingStrategy::Paragraph => self.chunk_by_paragraphs(content),
            ChunkingStrategy::Semantic => self.chunk_semantic(content),
            ChunkingStrategy::Hybrid => self.chunk_hybrid(content),
        }
    }

    /// Chunk content by sentence boundaries
    fn chunk_by_sentences(&self, content: &str) -> Result<Vec<ContentChunk>> {
        let sentence_regex = Regex::new(r"[.!?]+\s+").map_err(|e| {
            crate::error::Error::InternalError(format!("Failed to create sentence regex: {}", e))
        })?;

        let sentences: Vec<&str> = sentence_regex
            .split(content)
            .filter(|s| !s.trim().is_empty())
            .collect();

        self.group_sentences_into_chunks(&sentences, content)
    }

    /// Chunk content by paragraph boundaries
    fn chunk_by_paragraphs(&self, content: &str) -> Result<Vec<ContentChunk>> {
        let paragraphs: Vec<&str> = content
            .split("\n\n")
            .filter(|p| !p.trim().is_empty())
            .collect();

        self.group_paragraphs_into_chunks(&paragraphs, content)
    }

    /// Semantic chunking using NLP heuristics
    fn chunk_semantic(&self, content: &str) -> Result<Vec<ContentChunk>> {
        // Use a combination of semantic boundaries:
        // 1. Code blocks (```...```)
        // 2. Headers (# ## ###)
        // 3. List items
        // 4. Paragraph breaks
        
        let semantic_boundaries = self.find_semantic_boundaries(content)?;
        self.create_chunks_from_boundaries(content, &semantic_boundaries)
    }

    /// Hybrid approach: size-based with semantic boundary adjustment
    fn chunk_hybrid(&self, content: &str) -> Result<Vec<ContentChunk>> {
        let mut chunks = Vec::new();
        let content_bytes = content.as_bytes();
        let mut start = 0;
        let mut chunk_index = 0;

        while start < content_bytes.len() {
            let initial_end = (start + self.chunk_size).min(content_bytes.len());
            
            // Find the best semantic boundary near the target size
            let semantic_end = self.find_best_semantic_boundary(
                content, 
                start, 
                initial_end, 
                content_bytes.len()
            );

            // Extract chunk content
            let chunk_content = content[start..semantic_end].to_string();

            chunks.push(ContentChunk {
                content: chunk_content,
                start_byte: start,
                end_byte: semantic_end,
                chunk_index,
            });

            // Move to next chunk with overlap, but respect word boundaries
            if semantic_end >= content_bytes.len() {
                break;
            }

            start = self.calculate_semantic_overlap_start(content, semantic_end);
            chunk_index += 1;
        }

        Ok(chunks)
    }

    /// Group sentences into chunks respecting size limits
    fn group_sentences_into_chunks(&self, sentences: &[&str], _original: &str) -> Result<Vec<ContentChunk>> {
        let mut chunks = Vec::new();
        let mut current_chunk = String::new();
        let mut chunk_start = 0;
        let mut chunk_index = 0;

        for sentence in sentences {
            let potential_chunk = if current_chunk.is_empty() {
                sentence.to_string()
            } else {
                format!("{} {}", current_chunk, sentence)
            };

            if potential_chunk.len() <= self.chunk_size || current_chunk.is_empty() {
                current_chunk = potential_chunk;
            } else {
                // Finalize current chunk
                let chunk_end = chunk_start + current_chunk.len();
                chunks.push(ContentChunk {
                    content: current_chunk.trim().to_string(),
                    start_byte: chunk_start,
                    end_byte: chunk_end,
                    chunk_index,
                });

                // Start new chunk with overlap
                let overlap_content = self.calculate_sentence_overlap(&current_chunk);
                current_chunk = if overlap_content.is_empty() {
                    sentence.to_string()
                } else {
                    format!("{} {}", overlap_content, sentence)
                };
                
                chunk_start = chunk_end - overlap_content.len();
                chunk_index += 1;
            }
        }

        // Add the last chunk if not empty
        if !current_chunk.trim().is_empty() {
            let chunk_end = chunk_start + current_chunk.len();
            chunks.push(ContentChunk {
                content: current_chunk.trim().to_string(),
                start_byte: chunk_start,
                end_byte: chunk_end,
                chunk_index,
            });
        }

        Ok(chunks)
    }

    /// Group paragraphs into chunks respecting size limits
    fn group_paragraphs_into_chunks(&self, paragraphs: &[&str], _original: &str) -> Result<Vec<ContentChunk>> {
        let mut chunks = Vec::new();
        let mut current_chunk = String::new();
        let mut chunk_start = 0;
        let mut chunk_index = 0;

        for paragraph in paragraphs {
            let potential_chunk = if current_chunk.is_empty() {
                paragraph.to_string()
            } else {
                format!("{}\n\n{}", current_chunk, paragraph)
            };

            if potential_chunk.len() <= self.chunk_size || current_chunk.is_empty() {
                current_chunk = potential_chunk;
            } else {
                // Finalize current chunk
                let chunk_end = chunk_start + current_chunk.len();
                chunks.push(ContentChunk {
                    content: current_chunk.trim().to_string(),
                    start_byte: chunk_start,
                    end_byte: chunk_end,
                    chunk_index,
                });

                // Start new chunk (no overlap for paragraph chunking)
                current_chunk = paragraph.to_string();
                chunk_start = chunk_end;
                chunk_index += 1;
            }
        }

        // Add the last chunk if not empty
        if !current_chunk.trim().is_empty() {
            let chunk_end = chunk_start + current_chunk.len();
            chunks.push(ContentChunk {
                content: current_chunk.trim().to_string(),
                start_byte: chunk_start,
                end_byte: chunk_end,
                chunk_index,
            });
        }

        Ok(chunks)
    }

    /// Find semantic boundaries in the text
    fn find_semantic_boundaries(&self, content: &str) -> Result<Vec<usize>> {
        let mut boundaries = vec![0]; // Start with the beginning
        
        // Find code blocks
        let code_block_regex = Regex::new(r"```[\s\S]*?```").map_err(|e| {
            crate::error::Error::InternalError(format!("Failed to create code block regex: {}", e))
        })?;
        
        for mat in code_block_regex.find_iter(content) {
            boundaries.push(mat.start());
            boundaries.push(mat.end());
        }
        
        // Find headers
        let header_regex = Regex::new(r"(?m)^#{1,6}\s").map_err(|e| {
            crate::error::Error::InternalError(format!("Failed to create header regex: {}", e))
        })?;
        
        for mat in header_regex.find_iter(content) {
            boundaries.push(mat.start());
        }
        
        // Find paragraph breaks
        let paragraph_regex = Regex::new(r"\n\s*\n").map_err(|e| {
            crate::error::Error::InternalError(format!("Failed to create paragraph regex: {}", e))
        })?;
        
        for mat in paragraph_regex.find_iter(content) {
            boundaries.push(mat.end());
        }
        
        boundaries.push(content.len()); // End with the content length
        boundaries.sort_unstable();
        boundaries.dedup();
        
        Ok(boundaries)
    }

    /// Create chunks from semantic boundaries
    fn create_chunks_from_boundaries(&self, content: &str, boundaries: &[usize]) -> Result<Vec<ContentChunk>> {
        let mut chunks = Vec::new();
        let mut chunk_index = 0;
        
        for window in boundaries.windows(2) {
            let start = window[0];
            let end = window[1];
            let chunk_content = content[start..end].trim();
            
            if !chunk_content.is_empty() && chunk_content.len() >= 10 {
                chunks.push(ContentChunk {
                    content: chunk_content.to_string(),
                    start_byte: start,
                    end_byte: end,
                    chunk_index,
                });
                chunk_index += 1;
            }
        }
        
        Ok(chunks)
    }

    /// Find the best semantic boundary near the target position
    fn find_best_semantic_boundary(&self, content: &str, start: usize, target_end: usize, content_len: usize) -> usize {
        if target_end >= content_len {
            return content_len;
        }
        
        // Search window around target_end
        let search_start = (target_end.saturating_sub(200)).max(start);
        let search_end = (target_end + 200).min(content_len);
        
        let search_text = &content[search_start..search_end];
        
        // Look for good boundaries in order of preference:
        // 1. Double newlines (paragraph breaks)
        // 2. Single newlines
        // 3. Sentence endings
        // 4. Word boundaries
        
        let relative_target = target_end - search_start;
        
        // Paragraph breaks
        if let Some(pos) = self.find_nearest_match(search_text, r"\n\s*\n", relative_target) {
            return search_start + pos;
        }
        
        // Single newlines
        if let Some(pos) = self.find_nearest_match(search_text, r"\n", relative_target) {
            return search_start + pos;
        }
        
        // Sentence endings
        if let Some(pos) = self.find_nearest_match(search_text, r"[.!?]\s+", relative_target) {
            return search_start + pos;
        }
        
        // Word boundaries
        if let Some(pos) = self.find_nearest_match(search_text, r"\s+", relative_target) {
            return search_start + pos;
        }
        
        // Fallback to original target
        target_end
    }

    /// Find the nearest regex match to a target position
    fn find_nearest_match(&self, text: &str, pattern: &str, target: usize) -> Option<usize> {
        let regex = Regex::new(pattern).ok()?;
        let mut closest_pos = None;
        let mut closest_distance = usize::MAX;
        
        for mat in regex.find_iter(text) {
            let distance = if mat.end() > target {
                mat.end() - target
            } else {
                target - mat.end()
            };
            
            if distance < closest_distance {
                closest_distance = distance;
                closest_pos = Some(mat.end());
            }
        }
        
        closest_pos
    }

    /// Calculate semantic overlap start position
    fn calculate_semantic_overlap_start(&self, content: &str, end: usize) -> usize {
        let overlap_target = end.saturating_sub(self.overlap_size);
        
        // Find word boundary for overlap
        let search_start = overlap_target.saturating_sub(50);
        let search_end = end.min(search_start + 100);
        
        if search_start >= search_end {
            return overlap_target;
        }
        
        let search_text = &content[search_start..search_end];
        let relative_target = overlap_target - search_start;
        
        if let Some(pos) = self.find_nearest_match(search_text, r"\s+", relative_target) {
            search_start + pos
        } else {
            overlap_target
        }
    }

    /// Calculate overlap for sentence-based chunking
    fn calculate_sentence_overlap(&self, chunk: &str) -> String {
        let words: Vec<&str> = chunk.split_whitespace().collect();
        let overlap_words = (words.len() * self.overlap_size / chunk.len()).min(words.len() / 4);
        
        if overlap_words > 0 {
            words[words.len().saturating_sub(overlap_words)..]
                .join(" ")
        } else {
            String::new()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[allow(clippy::needless_borrow)]
    fn test_basic_chunking() {
        let chunker = FileChunker::new(100, 20);
        let content = "a".repeat(250);
        let chunks = chunker.chunk_content(&content).unwrap();

        assert!(chunks.len() >= 3);
        assert_eq!(chunks[0].chunk_index, 0);
        assert_eq!(chunks[1].chunk_index, 1);
    }

    #[test]
    #[allow(clippy::needless_borrow)]
    fn test_utf8_boundary_safety() {
        let chunker = FileChunker::new(10, 2);
        let content = "Hello 世界 World";
        let chunks = chunker.chunk_content(&content).unwrap();

        // Ensure all chunks are valid UTF-8
        for chunk in chunks {
            assert!(
                chunk.content.is_ascii()
                    || chunk
                        .content
                        .chars()
                        .all(|c| c.is_alphabetic() || c.is_whitespace())
            );
        }
    }

    #[test]
    #[allow(clippy::needless_borrow)]
    fn test_overlap() {
        let chunker = FileChunker::new(50, 10);
        let content = "a".repeat(100);
        let chunks = chunker.chunk_content(&content).unwrap();

        // Check that chunks have overlap
        if chunks.len() > 1 {
            let overlap_start = chunks[1].start_byte;
            let first_end = chunks[0].end_byte;
            assert!(overlap_start < first_end);
        }
    }
}