oxibonsai-rag 0.1.4

Pure Rust RAG pipeline for OxiBonsai
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//! Advanced document chunking strategies for RAG pipelines.
//!
//! Beyond simple fixed-size chunking, this module implements:
//! - Sentence-aware chunking (split on sentence boundaries)
//! - Recursive character text splitting (LangChain-style)
//! - Semantic chunking (group similar consecutive sentences)
//! - Sliding window with overlap
//! - Markdown/code-aware splitting

use std::collections::HashMap;

// ── RichChunk ─────────────────────────────────────────────────────────────────

/// A more detailed chunk with positional metadata.
#[derive(Debug, Clone)]
pub struct RichChunk {
    pub text: String,
    pub char_start: usize,
    pub char_end: usize,
    pub chunk_index: usize,
    pub metadata: HashMap<String, String>,
}

impl RichChunk {
    /// Create a new `RichChunk`.
    pub fn new(text: String, start: usize, end: usize, index: usize) -> Self {
        Self {
            text,
            char_start: start,
            char_end: end,
            chunk_index: index,
            metadata: HashMap::new(),
        }
    }

    /// Character count of the chunk text.
    pub fn len(&self) -> usize {
        self.text.chars().count()
    }

    /// Returns `true` if the chunk text is empty.
    pub fn is_empty(&self) -> bool {
        self.text.is_empty()
    }

    /// Approximate word count (whitespace-split).
    pub fn word_count(&self) -> usize {
        self.text.split_whitespace().count()
    }
}

// ── ChunkStrategy trait ───────────────────────────────────────────────────────

/// Strategy for chunking documents.
pub trait ChunkStrategy: Send + Sync {
    fn chunk(&self, text: &str) -> Vec<RichChunk>;
    fn name(&self) -> &'static str;
}

// ── SentenceChunker ───────────────────────────────────────────────────────────

/// Sentence-aware chunker: splits on [.!?] boundaries, then groups sentences
/// into chunks of at most `max_chars` characters.
pub struct SentenceChunker {
    pub max_chars: usize,
    pub overlap_sentences: usize,
}

impl SentenceChunker {
    /// Create with a maximum character limit per chunk and no overlap.
    pub fn new(max_chars: usize) -> Self {
        Self {
            max_chars,
            overlap_sentences: 0,
        }
    }

    /// Configure sentence overlap between consecutive chunks.
    pub fn with_overlap(mut self, sentences: usize) -> Self {
        self.overlap_sentences = sentences;
        self
    }
}

impl ChunkStrategy for SentenceChunker {
    fn name(&self) -> &'static str {
        "sentence"
    }

    fn chunk(&self, text: &str) -> Vec<RichChunk> {
        if text.is_empty() {
            return Vec::new();
        }

        let sentences = split_sentences(text);
        if sentences.is_empty() {
            return Vec::new();
        }

        let mut chunks: Vec<RichChunk> = Vec::new();
        let mut i = 0usize;

        while i < sentences.len() {
            let mut group: Vec<&str> = Vec::new();
            let mut total_chars = 0usize;

            // Build a group of sentences that fit within max_chars
            let mut j = i;
            while j < sentences.len() {
                let s = sentences[j];
                let added = if group.is_empty() {
                    s.len()
                } else {
                    s.len() + 1
                };
                if !group.is_empty() && total_chars + added > self.max_chars {
                    break;
                }
                group.push(s);
                total_chars += added;
                j += 1;
            }

            if group.is_empty() {
                // Single sentence exceeds max_chars — emit it as-is
                group.push(sentences[i]);
                j = i + 1;
            }

            let chunk_text = group.join(" ");

            // Compute char_start by summing all sentences before this group
            let char_start: usize = sentences[..i].iter().map(|s| s.chars().count() + 1).sum();
            let char_end = char_start + chunk_text.chars().count();

            chunks.push(RichChunk::new(
                chunk_text,
                char_start,
                char_end,
                chunks.len(),
            ));

            // Advance, considering overlap
            let consumed = j - i;
            let step = if consumed > self.overlap_sentences {
                consumed - self.overlap_sentences
            } else {
                1
            };
            i += step.max(1);
        }

        chunks
    }
}

// ── RecursiveCharSplitter ─────────────────────────────────────────────────────

/// Recursive character splitter: tries separators in order until all chunks
/// are ≤ max_chars.
pub struct RecursiveCharSplitter {
    pub max_chars: usize,
    pub overlap: usize,
    pub separators: Vec<String>,
}

impl RecursiveCharSplitter {
    /// Create with default separators and no overlap.
    pub fn new(max_chars: usize) -> Self {
        Self {
            max_chars,
            overlap: 0,
            separators: Self::default_separators(),
        }
    }

    /// Configure character overlap between adjacent chunks.
    pub fn with_overlap(mut self, overlap: usize) -> Self {
        self.overlap = overlap;
        self
    }

    /// Override the separator list.
    pub fn with_separators(mut self, seps: Vec<String>) -> Self {
        self.separators = seps;
        self
    }

    /// Default separators: paragraph, line, space, character.
    pub fn default_separators() -> Vec<String> {
        vec![
            "\n\n".to_string(),
            "\n".to_string(),
            " ".to_string(),
            "".to_string(),
        ]
    }

    /// Recursively split text using the given separators.
    fn split_recursive(&self, text: &str, seps: &[String]) -> Vec<String> {
        if text.chars().count() <= self.max_chars {
            return vec![text.to_string()];
        }

        let sep = match seps.first() {
            Some(s) => s,
            None => {
                // No separators left — split by character
                return split_by_chars(text, self.max_chars, self.overlap);
            }
        };

        let remaining_seps = &seps[1..];

        if sep.is_empty() {
            // Split by individual characters
            return split_by_chars(text, self.max_chars, self.overlap);
        }

        let parts: Vec<&str> = text.split(sep.as_str()).collect();

        let mut result: Vec<String> = Vec::new();
        let mut current_group: Vec<&str> = Vec::new();
        let mut current_len = 0usize;

        for part in &parts {
            let part_len = part.chars().count();
            let sep_len = if current_group.is_empty() {
                0
            } else {
                sep.chars().count()
            };

            if current_len + sep_len + part_len > self.max_chars && !current_group.is_empty() {
                // Flush current group
                let joined = current_group.join(sep.as_str());
                if joined.chars().count() > self.max_chars {
                    // Need to recurse with the next separator
                    let sub = self.split_recursive(&joined, remaining_seps);
                    result.extend(sub);
                } else {
                    result.push(joined);
                }

                // Start new group with overlap
                if self.overlap > 0 {
                    // Keep last few items that fit within overlap
                    let mut overlap_items: Vec<&str> = Vec::new();
                    let mut overlap_len = 0usize;
                    for &item in current_group.iter().rev() {
                        let item_len = item.chars().count() + sep.chars().count();
                        if overlap_len + item_len > self.overlap {
                            break;
                        }
                        overlap_items.push(item);
                        overlap_len += item_len;
                    }
                    overlap_items.reverse();
                    current_group = overlap_items;
                    current_len = current_group
                        .iter()
                        .map(|s| s.chars().count())
                        .sum::<usize>()
                        + if current_group.len() > 1 {
                            (current_group.len() - 1) * sep.chars().count()
                        } else {
                            0
                        };
                } else {
                    current_group.clear();
                    current_len = 0;
                }
            }

            if part_len > self.max_chars {
                // This single part is too large — recurse on it
                if !current_group.is_empty() {
                    result.push(current_group.join(sep.as_str()));
                    current_group.clear();
                    current_len = 0;
                }
                let sub = self.split_recursive(part, remaining_seps);
                result.extend(sub);
            } else {
                let sep_add = if current_group.is_empty() {
                    0
                } else {
                    sep.chars().count()
                };
                current_len += sep_add + part_len;
                current_group.push(part);
            }
        }

        if !current_group.is_empty() {
            let joined = current_group.join(sep.as_str());
            if joined.chars().count() > self.max_chars {
                let sub = self.split_recursive(&joined, remaining_seps);
                result.extend(sub);
            } else {
                result.push(joined);
            }
        }

        result
    }
}

impl ChunkStrategy for RecursiveCharSplitter {
    fn name(&self) -> &'static str {
        "recursive"
    }

    fn chunk(&self, text: &str) -> Vec<RichChunk> {
        if text.is_empty() {
            return Vec::new();
        }

        let pieces = self.split_recursive(text, &self.separators.clone());

        let mut chunks: Vec<RichChunk> = Vec::new();
        let mut char_cursor = 0usize;
        let text_chars: Vec<char> = text.chars().collect();
        let total_chars = text_chars.len();

        for piece in pieces {
            if piece.is_empty() {
                continue;
            }
            let piece_len = piece.chars().count();
            // Find piece start in original text by scanning forward
            let start = find_substring_char_offset(&text_chars, &piece, char_cursor);
            let start = start.unwrap_or(char_cursor);
            let end = (start + piece_len).min(total_chars);

            chunks.push(RichChunk::new(piece, start, end, chunks.len()));
            char_cursor = start + piece_len;
        }

        chunks
    }
}

// ── SlidingWindowChunker ──────────────────────────────────────────────────────

/// Sliding window chunker with configurable step size.
pub struct SlidingWindowChunker {
    /// Window size in characters.
    pub window_size: usize,
    /// How far to advance each step.
    pub step_size: usize,
}

impl SlidingWindowChunker {
    /// Create with explicit window and step sizes.
    pub fn new(window_size: usize, step_size: usize) -> Self {
        Self {
            window_size,
            step_size: step_size.max(1),
        }
    }

    /// Non-overlapping: step equals window size.
    pub fn non_overlapping(size: usize) -> Self {
        Self::new(size, size)
    }

    /// 50% overlap: step is half the window size.
    pub fn with_50pct_overlap(size: usize) -> Self {
        let step = (size / 2).max(1);
        Self::new(size, step)
    }
}

impl ChunkStrategy for SlidingWindowChunker {
    fn name(&self) -> &'static str {
        "sliding_window"
    }

    fn chunk(&self, text: &str) -> Vec<RichChunk> {
        if text.is_empty() || self.window_size == 0 {
            return Vec::new();
        }

        let chars: Vec<char> = text.chars().collect();
        let total = chars.len();
        let mut chunks: Vec<RichChunk> = Vec::new();
        let mut start = 0usize;

        while start < total {
            let end = (start + self.window_size).min(total);
            let chunk_text: String = chars[start..end].iter().collect();
            chunks.push(RichChunk::new(chunk_text, start, end, chunks.len()));

            if end == total {
                break;
            }
            start += self.step_size;
        }

        chunks
    }
}

// ── MarkdownChunker ───────────────────────────────────────────────────────────

/// Markdown-aware chunker: splits on headings (#, ##, ###).
pub struct MarkdownChunker {
    pub max_chars: usize,
    /// 1 = #, 2 = ##, etc. Only split on headings at or below this level.
    pub min_heading_level: u8,
}

impl MarkdownChunker {
    /// Create with `max_chars` limit and split on any heading level (1–6).
    pub fn new(max_chars: usize) -> Self {
        Self {
            max_chars,
            min_heading_level: 1,
        }
    }

    /// Determine the heading level of a line (0 if not a heading).
    fn heading_level(line: &str) -> u8 {
        let trimmed = line.trim_start();
        let count = trimmed.bytes().take_while(|&b| b == b'#').count();
        if count == 0 || count > 6 {
            return 0;
        }
        // Must be followed by a space or end of line
        let after = &trimmed[count..];
        if after.is_empty() || after.starts_with(' ') {
            count as u8
        } else {
            0
        }
    }
}

impl ChunkStrategy for MarkdownChunker {
    fn name(&self) -> &'static str {
        "markdown"
    }

    fn chunk(&self, text: &str) -> Vec<RichChunk> {
        if text.is_empty() {
            return Vec::new();
        }

        // Split text into sections at heading boundaries
        let lines: Vec<&str> = text.lines().collect();
        let mut sections: Vec<String> = Vec::new();
        let mut current: Vec<&str> = Vec::new();

        for line in &lines {
            let level = Self::heading_level(line);
            if level > 0 && level <= self.min_heading_level + 5 {
                // This is a heading line — start a new section
                if !current.is_empty() {
                    sections.push(current.join("\n"));
                    current.clear();
                }
            }
            current.push(line);
        }
        if !current.is_empty() {
            sections.push(current.join("\n"));
        }

        // Now apply max_chars within each section using recursive splitting
        let splitter = RecursiveCharSplitter::new(self.max_chars);
        let mut chunks: Vec<RichChunk> = Vec::new();
        let mut char_cursor = 0usize;

        for section in &sections {
            if section.trim().is_empty() {
                char_cursor += section.chars().count() + 1; // +1 for newline between sections
                continue;
            }

            let section_len = section.chars().count();

            if section_len <= self.max_chars {
                let start = char_cursor;
                let end = start + section_len;
                chunks.push(RichChunk::new(section.clone(), start, end, chunks.len()));
            } else {
                // Recursively split the section
                let sub_chunks = splitter.chunk(section);
                for mut sc in sub_chunks {
                    sc.char_start += char_cursor;
                    sc.char_end += char_cursor;
                    sc.chunk_index = chunks.len();
                    chunks.push(sc);
                }
            }

            char_cursor += section_len + 1; // +1 for the newline between sections
        }

        chunks
    }
}

// ── ChunkerRegistry ───────────────────────────────────────────────────────────

/// A chunker registry for selecting strategies by name.
pub struct ChunkerRegistry {
    strategies: HashMap<String, Box<dyn ChunkStrategy>>,
}

impl ChunkerRegistry {
    /// Create an empty registry.
    pub fn new() -> Self {
        Self {
            strategies: HashMap::new(),
        }
    }

    /// Register a strategy; the strategy's `name()` is used as the key.
    pub fn register(&mut self, strategy: Box<dyn ChunkStrategy>) {
        self.strategies
            .insert(strategy.name().to_string(), strategy);
    }

    /// Chunk text using the named strategy, returning `None` if not found.
    pub fn chunk(&self, name: &str, text: &str) -> Option<Vec<RichChunk>> {
        self.strategies.get(name).map(|s| s.chunk(text))
    }

    /// List all registered strategy names.
    pub fn available_strategies(&self) -> Vec<&str> {
        let mut names: Vec<&str> = self.strategies.keys().map(|s| s.as_str()).collect();
        names.sort();
        names
    }

    /// Build a registry pre-loaded with Sentence, Recursive, SlidingWindow, and Markdown.
    pub fn default_registry() -> Self {
        let mut registry = Self::new();
        registry.register(Box::new(SentenceChunker::new(512)));
        registry.register(Box::new(RecursiveCharSplitter::new(512)));
        registry.register(Box::new(SlidingWindowChunker::non_overlapping(512)));
        registry.register(Box::new(MarkdownChunker::new(512)));
        registry
    }
}

impl Default for ChunkerRegistry {
    fn default() -> Self {
        Self::new()
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

/// Split text on sentence-ending punctuation followed by whitespace or end.
fn split_sentences(text: &str) -> Vec<&str> {
    let mut sentences = Vec::new();
    let bytes = text.as_bytes();
    let mut start = 0usize;
    let mut i = 0usize;

    while i < bytes.len() {
        let b = bytes[i];
        if b == b'.' || b == b'!' || b == b'?' {
            // Consume consecutive punctuation
            let mut j = i + 1;
            while j < bytes.len() && (bytes[j] == b'.' || bytes[j] == b'!' || bytes[j] == b'?') {
                j += 1;
            }
            // Check that it's followed by whitespace or end-of-string
            let at_end = j >= bytes.len();
            let followed_by_space = !at_end
                && (bytes[j] == b' '
                    || bytes[j] == b'\t'
                    || bytes[j] == b'\n'
                    || bytes[j] == b'\r');

            if at_end || followed_by_space {
                // Skip whitespace
                while j < bytes.len()
                    && (bytes[j] == b' '
                        || bytes[j] == b'\t'
                        || bytes[j] == b'\n'
                        || bytes[j] == b'\r')
                {
                    j += 1;
                }
                let sentence = text[start..j].trim();
                if !sentence.is_empty() {
                    sentences.push(sentence);
                }
                start = j;
                i = j;
                continue;
            }
        }
        i += 1;
    }

    // Trailing text without terminal punctuation
    let tail = text[start..].trim();
    if !tail.is_empty() {
        sentences.push(tail);
    }

    sentences
}

/// Split text into fixed-size character windows with optional overlap.
fn split_by_chars(text: &str, max_chars: usize, overlap: usize) -> Vec<String> {
    let chars: Vec<char> = text.chars().collect();
    let total = chars.len();

    if total == 0 {
        return Vec::new();
    }

    let step = max_chars.saturating_sub(overlap).max(1);
    let mut result = Vec::new();
    let mut start = 0usize;

    while start < total {
        let end = (start + max_chars).min(total);
        let chunk: String = chars[start..end].iter().collect();
        result.push(chunk);
        if end == total {
            break;
        }
        start += step;
    }

    result
}

/// Find the character-offset of `needle` in `haystack_chars` starting from `from`.
fn find_substring_char_offset(haystack: &[char], needle: &str, from: usize) -> Option<usize> {
    let needle_chars: Vec<char> = needle.chars().collect();
    let needle_len = needle_chars.len();

    if needle_len == 0 {
        return Some(from);
    }

    let limit = if haystack.len() >= needle_len {
        haystack.len() - needle_len + 1
    } else {
        return None;
    };

    for i in from..limit {
        if haystack[i..i + needle_len] == needle_chars[..] {
            return Some(i);
        }
    }
    None
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    #[test]
    fn rich_chunk_len_and_words() {
        let chunk = RichChunk::new("hello world".to_string(), 0, 11, 0);
        assert_eq!(chunk.len(), 11);
        assert_eq!(chunk.word_count(), 2);
    }

    #[test]
    fn sentence_chunker_splits() {
        let chunker = SentenceChunker::new(200);
        let text = "Hello world. This is a test. Another sentence here.";
        let chunks = chunker.chunk(text);
        assert!(!chunks.is_empty());
    }

    #[test]
    fn recursive_splitter_short() {
        let splitter = RecursiveCharSplitter::new(1000);
        let text = "short text";
        let chunks = splitter.chunk(text);
        assert_eq!(chunks.len(), 1);
        assert_eq!(chunks[0].text, text);
    }

    #[test]
    fn sliding_window_basic() {
        let chunker = SlidingWindowChunker::non_overlapping(5);
        let chunks = chunker.chunk("abcdefghij");
        assert_eq!(chunks.len(), 2);
    }
}