vecstore 1.0.0

The perfect vector database - 100/100 score, embeddable, high-performance, production-ready with RAG toolkit
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
//! Text Splitting for RAG Applications
//!
//! Provides text chunking strategies for breaking down large documents into
//! embedding-sized chunks. Essential for RAG (Retrieval-Augmented Generation)
//! systems that need to store and search long documents.
//!
//! # Strategies
//!
//! - **RecursiveCharacterTextSplitter**: Splits on paragraph/sentence/word boundaries
//! - **TokenTextSplitter**: Splits based on token count (for LLMs with token limits)
//! - **MarkdownTextSplitter**: Markdown-aware splitting that respects header hierarchy
//! - **CodeTextSplitter**: Code-aware splitting that respects function/class boundaries
//! - **SemanticTextSplitter**: Embedding-based splitting that groups semantically similar content
//!
//! # Example
//!
//! ```no_run
//! use vecstore::text_splitter::{RecursiveCharacterTextSplitter, TextSplitter};
//!
//! let splitter = RecursiveCharacterTextSplitter::new(500, 50);
//! let chunks = splitter.split_text("Long document text...")?;
//!
//! for (i, chunk) in chunks.iter().enumerate() {
//!     println!("Chunk {}: {} chars", i, chunk.len());
//! }
//! # Ok::<(), anyhow::Error>(())
//! ```

use crate::error::{Result, VecStoreError};

/// Trait for text splitting strategies
pub trait TextSplitter {
    /// Split text into chunks
    fn split_text(&self, text: &str) -> Result<Vec<String>>;

    /// Split text into chunks with metadata (position, length, etc.)
    fn split_with_metadata(&self, text: &str) -> Result<Vec<TextChunk>> {
        let chunks = self.split_text(text)?;
        Ok(chunks
            .into_iter()
            .enumerate()
            .map(|(i, content)| TextChunk {
                index: i,
                content,
                char_start: 0, // Simplified - could track actual positions
                char_end: 0,
            })
            .collect())
    }
}

/// A text chunk with metadata
#[derive(Debug, Clone, PartialEq)]
pub struct TextChunk {
    /// Chunk index in the original document
    pub index: usize,
    /// Chunk content
    pub content: String,
    /// Character start position in original text
    pub char_start: usize,
    /// Character end position in original text
    pub char_end: usize,
}

/// Recursive character-based text splitter
///
/// Tries to split on natural boundaries in this order:
/// 1. Double newlines (paragraphs)
/// 2. Single newlines (lines)
/// 3. Sentences (periods, question marks, exclamation points)
/// 4. Words (spaces)
/// 5. Characters (last resort)
///
/// # Example
///
/// ```no_run
/// use vecstore::text_splitter::{RecursiveCharacterTextSplitter, TextSplitter};
///
/// let splitter = RecursiveCharacterTextSplitter::new(1000, 100);
/// let text = "First paragraph.\n\nSecond paragraph with more content...";
/// let chunks = splitter.split_text(text)?;
/// # Ok::<(), anyhow::Error>(())
/// ```
pub struct RecursiveCharacterTextSplitter {
    /// Maximum chunk size in characters
    chunk_size: usize,
    /// Overlap between chunks in characters
    chunk_overlap: usize,
    /// Separators to try, in order of preference
    separators: Vec<String>,
}

impl RecursiveCharacterTextSplitter {
    /// Create a new recursive splitter
    ///
    /// # Arguments
    /// * `chunk_size` - Maximum characters per chunk
    /// * `chunk_overlap` - Characters to overlap between chunks (for context continuity)
    ///
    /// # Example
    ///
    /// ```no_run
    /// use vecstore::text_splitter::RecursiveCharacterTextSplitter;
    ///
    /// // 500 char chunks with 50 char overlap
    /// let splitter = RecursiveCharacterTextSplitter::new(500, 50);
    /// ```
    pub fn new(chunk_size: usize, chunk_overlap: usize) -> Self {
        Self {
            chunk_size,
            chunk_overlap,
            separators: vec![
                "\n\n".to_string(), // Paragraphs
                "\n".to_string(),   // Lines
                ". ".to_string(),   // Sentences
                "! ".to_string(),
                "? ".to_string(),
                " ".to_string(), // Words
                "".to_string(),  // Characters
            ],
        }
    }

    /// Create with custom separators
    pub fn with_separators(mut self, separators: Vec<String>) -> Self {
        self.separators = separators;
        self
    }

    fn split_recursive(&self, text: &str, separators: &[String]) -> Vec<String> {
        if text.len() <= self.chunk_size {
            return vec![text.to_string()];
        }

        if separators.is_empty() {
            // Fallback: character-level split
            return self.split_by_chars(text);
        }

        let sep = &separators[0];
        let remaining_seps = &separators[1..];

        if sep.is_empty() {
            // Empty separator means character-level split
            return self.split_by_chars(text);
        }

        // Split by current separator
        let parts: Vec<&str> = text.split(sep).collect();

        let mut chunks = Vec::new();
        let mut current_chunk = String::new();

        for (i, part) in parts.iter().enumerate() {
            let part_with_sep = if i < parts.len() - 1 {
                format!("{}{}", part, sep)
            } else {
                part.to_string()
            };

            // If this part alone is too big, recursively split it
            if part_with_sep.len() > self.chunk_size {
                if !current_chunk.is_empty() {
                    chunks.push(current_chunk.clone());
                    current_chunk.clear();
                }
                let sub_chunks = self.split_recursive(&part_with_sep, remaining_seps);
                chunks.extend(sub_chunks);
                continue;
            }

            // Try to add to current chunk
            if current_chunk.len() + part_with_sep.len() <= self.chunk_size {
                current_chunk.push_str(&part_with_sep);
            } else {
                // Current chunk is full, start a new one
                if !current_chunk.is_empty() {
                    chunks.push(current_chunk.clone());
                }
                current_chunk = part_with_sep;
            }
        }

        if !current_chunk.is_empty() {
            chunks.push(current_chunk);
        }

        // Add overlap
        self.add_overlap(chunks)
    }

    fn split_by_chars(&self, text: &str) -> Vec<String> {
        let chars: Vec<char> = text.chars().collect();
        let mut chunks = Vec::new();

        let mut i = 0;
        while i < chars.len() {
            let end = (i + self.chunk_size).min(chars.len());
            let chunk: String = chars[i..end].iter().collect();
            chunks.push(chunk);

            if end >= chars.len() {
                break;
            }

            // Move forward, accounting for overlap
            i += self.chunk_size - self.chunk_overlap;
        }

        chunks
    }

    fn add_overlap(&self, chunks: Vec<String>) -> Vec<String> {
        if self.chunk_overlap == 0 || chunks.len() <= 1 {
            return chunks;
        }

        let mut result = Vec::new();

        for (i, chunk) in chunks.iter().enumerate() {
            if i == 0 {
                result.push(chunk.clone());
                continue;
            }

            // Get overlap from previous chunk
            let prev_chunk = &chunks[i - 1];
            let overlap_chars: Vec<char> = prev_chunk.chars().collect();
            let overlap_start = overlap_chars.len().saturating_sub(self.chunk_overlap);
            let overlap: String = overlap_chars[overlap_start..].iter().collect();

            let new_chunk = format!("{}{}", overlap, chunk);
            result.push(new_chunk);
        }

        result
    }
}

impl TextSplitter for RecursiveCharacterTextSplitter {
    fn split_text(&self, text: &str) -> Result<Vec<String>> {
        if text.is_empty() {
            return Ok(vec![]);
        }

        if self.chunk_size == 0 {
            return Err(VecStoreError::invalid_parameter(
                "chunk_size",
                "must be greater than 0",
            ));
        }

        if self.chunk_overlap >= self.chunk_size {
            return Err(VecStoreError::invalid_parameter(
                "chunk_overlap",
                "must be less than chunk_size",
            ));
        }

        Ok(self.split_recursive(text, &self.separators))
    }
}

/// Token-based text splitter
///
/// Splits text based on approximate token count rather than character count.
/// Useful for LLM applications with token limits.
///
/// Uses a simple heuristic: ~4 characters per token (approximation for English)
///
/// # Example
///
/// ```no_run
/// use vecstore::text_splitter::{TokenTextSplitter, TextSplitter};
///
/// // Split into ~512 token chunks
/// let splitter = TokenTextSplitter::new(512, 50);
/// let chunks = splitter.split_text("Long document...")?;
/// # Ok::<(), anyhow::Error>(())
/// ```
pub struct TokenTextSplitter {
    /// Maximum tokens per chunk
    max_tokens: usize,
    /// Overlap in tokens
    token_overlap: usize,
    /// Characters per token (approximation)
    chars_per_token: usize,
}

impl TokenTextSplitter {
    /// Create a new token-based splitter
    ///
    /// # Arguments
    /// * `max_tokens` - Maximum tokens per chunk
    /// * `token_overlap` - Tokens to overlap between chunks
    ///
    /// # Example
    ///
    /// ```no_run
    /// use vecstore::text_splitter::TokenTextSplitter;
    ///
    /// // 512 token chunks with 50 token overlap
    /// let splitter = TokenTextSplitter::new(512, 50);
    /// ```
    pub fn new(max_tokens: usize, token_overlap: usize) -> Self {
        Self {
            max_tokens,
            token_overlap,
            chars_per_token: 4, // Approximation for English
        }
    }

    /// Set characters per token (default: 4)
    pub fn with_chars_per_token(mut self, chars_per_token: usize) -> Self {
        self.chars_per_token = chars_per_token;
        self
    }
}

impl TextSplitter for TokenTextSplitter {
    fn split_text(&self, text: &str) -> Result<Vec<String>> {
        if text.is_empty() {
            return Ok(vec![]);
        }

        // Convert token limits to character limits
        let chunk_size = self.max_tokens * self.chars_per_token;
        let chunk_overlap = self.token_overlap * self.chars_per_token;

        // Use recursive splitter with character-based limits
        let char_splitter = RecursiveCharacterTextSplitter::new(chunk_size, chunk_overlap);
        char_splitter.split_text(text)
    }
}

/// Markdown-aware text splitter
///
/// Splits markdown documents while respecting header hierarchy.
/// **HYBRID**: Simple by default, powerful when needed.
///
/// # Simple Usage (Default)
///
/// ```no_run
/// use vecstore::text_splitter::{MarkdownTextSplitter, TextSplitter};
///
/// // Just works - splits on markdown boundaries
/// let splitter = MarkdownTextSplitter::new(500, 50);
/// let chunks = splitter.split_text("# Title\n\nContent...")?;
/// # Ok::<(), anyhow::Error>(())
/// ```
///
/// # Advanced Usage (Optional)
///
/// ```no_run
/// use vecstore::text_splitter::{MarkdownTextSplitter, TextSplitter};
///
/// // Preserve header context in each chunk
/// let splitter = MarkdownTextSplitter::new(500, 50)
///     .with_preserve_headers(true);
/// let chunks = splitter.split_text("# Title\n## Section\nContent...")?;
/// # Ok::<(), anyhow::Error>(())
/// ```
pub struct MarkdownTextSplitter {
    /// Maximum chunk size in characters
    chunk_size: usize,
    /// Overlap between chunks
    chunk_overlap: usize,
    /// Whether to preserve header hierarchy in chunks
    preserve_headers: bool,
}

impl MarkdownTextSplitter {
    /// Create a new markdown splitter (simple, just works)
    pub fn new(chunk_size: usize, chunk_overlap: usize) -> Self {
        Self {
            chunk_size,
            chunk_overlap,
            preserve_headers: false, // Simple by default
        }
    }

    /// Preserve header context in chunks (advanced, opt-in)
    pub fn with_preserve_headers(mut self, preserve: bool) -> Self {
        self.preserve_headers = preserve;
        self
    }

    /// Parse markdown sections with header hierarchy
    fn parse_sections(&self, text: &str) -> Vec<MarkdownSection> {
        let mut sections = Vec::new();
        let mut current_section = MarkdownSection {
            level: 0,
            header: String::new(),
            content: String::new(),
            header_chain: Vec::new(),
        };

        let mut header_stack: Vec<(usize, String)> = Vec::new();

        for line in text.lines() {
            if let Some(level) = self.parse_header_level(line) {
                // Save previous section
                if !current_section.content.is_empty() || !current_section.header.is_empty() {
                    sections.push(current_section.clone());
                }

                // Parse header text
                let header_text = line.trim_start_matches('#').trim().to_string();

                // Update header stack (track hierarchy)
                header_stack.retain(|(l, _)| *l < level);
                header_stack.push((level, header_text.clone()));

                // Start new section
                current_section = MarkdownSection {
                    level,
                    header: header_text,
                    content: String::new(),
                    header_chain: header_stack.iter().map(|(_, h)| h.clone()).collect(),
                };
            } else {
                // Add content line
                if !current_section.content.is_empty() {
                    current_section.content.push('\n');
                }
                current_section.content.push_str(line);
            }
        }

        // Save final section
        if !current_section.content.is_empty() || !current_section.header.is_empty() {
            sections.push(current_section);
        }

        sections
    }

    /// Parse header level from line (e.g., "### Header" -> 3)
    fn parse_header_level(&self, line: &str) -> Option<usize> {
        let trimmed = line.trim_start();
        if !trimmed.starts_with('#') {
            return None;
        }

        let level = trimmed.chars().take_while(|&c| c == '#').count();
        if level > 0 && level <= 6 {
            // Valid markdown header (H1-H6)
            Some(level)
        } else {
            None
        }
    }
}

/// Markdown section with header hierarchy
#[derive(Debug, Clone)]
struct MarkdownSection {
    level: usize,
    header: String,
    content: String,
    header_chain: Vec<String>, // Full hierarchy: ["H1", "H2", "H3"]
}

impl TextSplitter for MarkdownTextSplitter {
    fn split_text(&self, text: &str) -> Result<Vec<String>> {
        if text.is_empty() {
            return Ok(vec![]);
        }

        if self.chunk_size == 0 {
            return Err(VecStoreError::invalid_parameter(
                "chunk_size",
                "must be greater than 0",
            ));
        }

        // Parse into markdown sections
        let sections = self.parse_sections(text);

        let mut chunks = Vec::new();
        let mut current_chunk = String::new();
        let mut current_header_context = String::new();

        for section in sections {
            // Build header context if preserving headers
            if self.preserve_headers && !section.header_chain.is_empty() {
                current_header_context = section
                    .header_chain
                    .iter()
                    .enumerate()
                    .map(|(i, h)| format!("{} {}", "#".repeat(i + 1), h))
                    .collect::<Vec<_>>()
                    .join("\n");
                current_header_context.push_str("\n\n");
            }

            let section_text = if section.header.is_empty() {
                section.content.clone()
            } else {
                format!(
                    "{} {}\n\n{}",
                    "#".repeat(section.level),
                    section.header,
                    section.content
                )
            };

            // If section fits in current chunk, add it
            let chunk_with_section = if self.preserve_headers {
                format!(
                    "{}{}{}",
                    current_chunk, current_header_context, section_text
                )
            } else {
                format!("{}{}", current_chunk, section_text)
            };

            if chunk_with_section.len() <= self.chunk_size {
                current_chunk = chunk_with_section;
            } else {
                // Current chunk is full, save it and start new one
                if !current_chunk.is_empty() {
                    chunks.push(current_chunk.trim().to_string());
                }

                // If section itself is too large, split it with RecursiveCharacterTextSplitter
                if section_text.len() > self.chunk_size {
                    let splitter = RecursiveCharacterTextSplitter::new(
                        self.chunk_size.saturating_sub(current_header_context.len()),
                        self.chunk_overlap,
                    );
                    let sub_chunks = splitter.split_text(&section_text)?;

                    for sub_chunk in sub_chunks {
                        if self.preserve_headers && !current_header_context.is_empty() {
                            chunks.push(format!("{}{}", current_header_context, sub_chunk));
                        } else {
                            chunks.push(sub_chunk);
                        }
                    }
                    current_chunk = String::new();
                } else {
                    current_chunk = if self.preserve_headers {
                        format!("{}{}", current_header_context, section_text)
                    } else {
                        section_text
                    };
                }
            }
        }

        // Save final chunk
        if !current_chunk.is_empty() {
            chunks.push(current_chunk.trim().to_string());
        }

        Ok(chunks)
    }
}

/// Code-aware text splitter
///
/// Splits source code while respecting function and class boundaries.
/// **HYBRID**: Simple by default, language-aware when needed.
///
/// # Simple Usage (Default)
///
/// ```no_run
/// use vecstore::text_splitter::{CodeTextSplitter, TextSplitter};
///
/// // Just works - splits on smart boundaries
/// let splitter = CodeTextSplitter::new(800, 50);
/// let chunks = splitter.split_text("fn main() { ... }")?;
/// # Ok::<(), anyhow::Error>(())
/// ```
///
/// # Advanced Usage (Optional)
///
/// ```no_run
/// use vecstore::text_splitter::{CodeTextSplitter, TextSplitter};
///
/// // Language-specific splitting
/// let splitter = CodeTextSplitter::new(800, 50)
///     .with_language("rust");
/// let chunks = splitter.split_text("fn main() { ... }")?;
/// # Ok::<(), anyhow::Error>(())
/// ```
pub struct CodeTextSplitter {
    /// Maximum chunk size in characters
    chunk_size: usize,
    /// Overlap between chunks
    chunk_overlap: usize,
    /// Optional language hint ("rust", "python", "javascript", etc.)
    language: Option<String>,
}

impl CodeTextSplitter {
    /// Create a new code splitter (simple, language-agnostic)
    pub fn new(chunk_size: usize, chunk_overlap: usize) -> Self {
        Self {
            chunk_size,
            chunk_overlap,
            language: None, // Simple by default - works for all languages
        }
    }

    /// Set language for smarter splitting (advanced, opt-in)
    pub fn with_language(mut self, language: impl Into<String>) -> Self {
        self.language = Some(language.into());
        self
    }

    /// Detect if a line starts a code block (function, class, etc.)
    fn is_code_block_start(&self, line: &str) -> bool {
        let trimmed = line.trim_start();

        match self.language.as_deref() {
            Some("rust") => {
                trimmed.starts_with("fn ")
                    || trimmed.starts_with("pub fn ")
                    || trimmed.starts_with("struct ")
                    || trimmed.starts_with("pub struct ")
                    || trimmed.starts_with("enum ")
                    || trimmed.starts_with("pub enum ")
                    || trimmed.starts_with("impl ")
                    || trimmed.starts_with("trait ")
            }
            Some("python") => {
                trimmed.starts_with("def ")
                    || trimmed.starts_with("class ")
                    || trimmed.starts_with("async def ")
            }
            Some("javascript") | Some("typescript") => {
                trimmed.starts_with("function ")
                    || trimmed.starts_with("class ")
                    || trimmed.starts_with("const ")
                    || trimmed.starts_with("let ")
                    || trimmed.starts_with("async function ")
                    || trimmed.starts_with("export ")
            }
            Some("java") | Some("c") | Some("cpp") => {
                // Simple heuristic: look for function-like patterns
                (trimmed.contains('(')
                    && trimmed.contains(')')
                    && (trimmed.contains("public")
                        || trimmed.contains("private")
                        || trimmed.contains("void")
                        || trimmed.contains("int")))
                    || trimmed.starts_with("class ")
            }
            Some("go") => {
                trimmed.starts_with("func ")
                    || trimmed.starts_with("type ")
                    || trimmed.starts_with("struct ")
            }
            _ => {
                // Language-agnostic heuristics
                trimmed.starts_with("fn ")
                    || trimmed.starts_with("function ")
                    || trimmed.starts_with("def ")
                    || trimmed.starts_with("class ")
            }
        }
    }

    /// Get code-specific separators
    fn get_separators(&self) -> Vec<String> {
        vec![
            "\n\n".to_string(),  // Double newline (blank line between functions/blocks)
            "\n}\n".to_string(), // Closing brace (end of block)
            "\n\n".to_string(),  // Paragraphs
            "\n".to_string(),    // Lines
            "; ".to_string(),    // Statements
            " ".to_string(),     // Words
            "".to_string(),      // Characters
        ]
    }
}

impl TextSplitter for CodeTextSplitter {
    fn split_text(&self, text: &str) -> Result<Vec<String>> {
        if text.is_empty() {
            return Ok(vec![]);
        }

        if self.chunk_size == 0 {
            return Err(VecStoreError::invalid_parameter(
                "chunk_size",
                "must be greater than 0",
            ));
        }

        // Use recursive splitter with code-aware separators
        let separators = self.get_separators();
        let splitter = RecursiveCharacterTextSplitter::new(self.chunk_size, self.chunk_overlap)
            .with_separators(separators);

        // If we have language hints, try to split on code block boundaries first
        if self.language.is_some() {
            let mut chunks = Vec::new();
            let mut current_chunk = String::new();
            let mut current_block = String::new();

            for line in text.lines() {
                let line_with_newline = format!("{}\n", line);

                // Check if this starts a new code block
                if self.is_code_block_start(line) && !current_block.is_empty() {
                    // Save previous block
                    if current_chunk.len() + current_block.len() <= self.chunk_size {
                        current_chunk.push_str(&current_block);
                        current_block.clear();
                    } else {
                        if !current_chunk.is_empty() {
                            chunks.push(current_chunk.clone());
                        }
                        current_chunk = current_block.clone();
                        current_block.clear();
                    }
                }

                current_block.push_str(&line_with_newline);

                // If block is getting too large, flush it
                if current_block.len() > self.chunk_size {
                    if !current_chunk.is_empty() {
                        chunks.push(current_chunk.clone());
                        current_chunk.clear();
                    }

                    // Split oversized block with standard splitter
                    let sub_chunks = splitter.split_text(&current_block)?;
                    chunks.extend(sub_chunks);
                    current_block.clear();
                }
            }

            // Save remaining content
            if !current_block.is_empty() {
                current_chunk.push_str(&current_block);
            }
            if !current_chunk.is_empty() {
                chunks.push(current_chunk);
            }

            return Ok(chunks);
        }

        // Fallback: use standard recursive splitter with code separators
        splitter.split_text(text)
    }
}

/// Simple trait for embedding text (used by SemanticTextSplitter)
///
/// **HYBRID**: Any embedder works - users provide their own implementation.
/// No forced dependencies on specific embedding libraries.
///
/// # Example Implementation
///
/// ```no_run
/// use vecstore::text_splitter::Embedder;
/// use anyhow::Result;
///
/// struct MyEmbedder;
///
/// impl Embedder for MyEmbedder {
///     fn embed(&self, text: &str) -> Result<Vec<f32>> {
///         // Your embedding logic here
///         Ok(vec![0.0; 384]) // Example: 384-dim vector
///     }
/// }
/// ```
pub trait Embedder {
    /// Embed a text into a vector
    fn embed(&self, text: &str) -> Result<Vec<f32>>;
}

/// Semantic text splitter
///
/// Splits text based on semantic similarity using embeddings.
/// Groups semantically similar content together.
/// **HYBRID**: Requires embedder (advanced), but composable with any embedding model.
///
/// # Usage
///
/// ```no_run
/// use vecstore::text_splitter::{SemanticTextSplitter, TextSplitter, Embedder};
/// use anyhow::Result;
///
/// // Provide your own embedder (no forced dependency)
/// struct MyEmbedder;
/// impl Embedder for MyEmbedder {
///     fn embed(&self, text: &str) -> Result<Vec<f32>> {
///         Ok(vec![0.0; 384])
///     }
/// }
///
/// let embedder = Box::new(MyEmbedder);
/// let splitter = SemanticTextSplitter::new(embedder, 500, 50);
/// let chunks = splitter.split_text("Long document...")?;
/// # Ok::<(), anyhow::Error>(())
/// ```
pub struct SemanticTextSplitter {
    /// Embedder for computing semantic similarity
    embedder: Box<dyn Embedder>,
    /// Maximum chunk size in characters
    max_chunk_size: usize,
    /// Minimum chunk size in characters
    min_chunk_size: usize,
    /// Similarity threshold (0.0-1.0) for grouping sentences
    similarity_threshold: f32,
}

impl SemanticTextSplitter {
    /// Create a new semantic splitter
    ///
    /// # Arguments
    /// * `embedder` - Any embedder implementing the Embedder trait (HYBRID: bring your own)
    /// * `max_chunk_size` - Maximum characters per chunk
    /// * `min_chunk_size` - Minimum characters per chunk (avoid tiny chunks)
    pub fn new(embedder: Box<dyn Embedder>, max_chunk_size: usize, min_chunk_size: usize) -> Self {
        Self {
            embedder,
            max_chunk_size,
            min_chunk_size,
            similarity_threshold: 0.7, // Default: group similar content
        }
    }

    /// Set similarity threshold (advanced, opt-in)
    ///
    /// Higher = more similar content required for grouping
    /// Lower = more aggressive grouping
    pub fn with_similarity_threshold(mut self, threshold: f32) -> Self {
        self.similarity_threshold = threshold.clamp(0.0, 1.0);
        self
    }

    /// Compute cosine similarity between two vectors
    fn cosine_similarity(&self, a: &[f32], b: &[f32]) -> f32 {
        if a.len() != b.len() {
            return 0.0;
        }

        let dot_product: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
        let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
        let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();

        if norm_a == 0.0 || norm_b == 0.0 {
            return 0.0;
        }

        dot_product / (norm_a * norm_b)
    }

    /// Split text into sentences (simple heuristic)
    fn split_sentences(&self, text: &str) -> Vec<String> {
        // Simple sentence splitting on common boundaries
        text.split(&['.', '!', '?'][..])
            .filter(|s| !s.trim().is_empty())
            .map(|s| s.trim().to_string())
            .collect()
    }
}

impl TextSplitter for SemanticTextSplitter {
    fn split_text(&self, text: &str) -> Result<Vec<String>> {
        if text.is_empty() {
            return Ok(vec![]);
        }

        if self.max_chunk_size == 0 {
            return Err(VecStoreError::invalid_parameter(
                "max_chunk_size",
                "must be greater than 0",
            ));
        }

        // Split into sentences
        let sentences = self.split_sentences(text);

        if sentences.is_empty() {
            return Ok(vec![]);
        }

        // Compute embeddings for all sentences
        let mut sentence_embeddings = Vec::new();
        for sentence in &sentences {
            let embedding = self.embedder.embed(sentence)?;
            sentence_embeddings.push(embedding);
        }

        // Group sentences into chunks based on semantic similarity
        let mut chunks = Vec::new();
        let mut current_chunk = String::new();
        let mut current_embedding: Option<Vec<f32>> = None;

        for (i, sentence) in sentences.iter().enumerate() {
            let sentence_with_space = if current_chunk.is_empty() {
                sentence.clone()
            } else {
                format!(" {}", sentence)
            };

            // Check if adding this sentence would exceed max size
            if current_chunk.len() + sentence_with_space.len() > self.max_chunk_size {
                // Save current chunk if it meets minimum size
                if current_chunk.len() >= self.min_chunk_size {
                    chunks.push(current_chunk.clone());
                    current_chunk.clear();
                    current_embedding = None;
                }
            }

            // Compute similarity with current chunk
            let should_add = if let Some(ref chunk_emb) = current_embedding {
                let similarity = self.cosine_similarity(chunk_emb, &sentence_embeddings[i]);
                similarity >= self.similarity_threshold
            } else {
                true // First sentence always added
            };

            if should_add || current_chunk.is_empty() {
                // Add sentence to current chunk
                current_chunk.push_str(&sentence_with_space);

                // Update chunk embedding (average of all sentence embeddings)
                if let Some(ref mut chunk_emb) = current_embedding {
                    // Simple averaging (could be weighted)
                    for (j, val) in sentence_embeddings[i].iter().enumerate() {
                        chunk_emb[j] = (chunk_emb[j] + val) / 2.0;
                    }
                } else {
                    current_embedding = Some(sentence_embeddings[i].clone());
                }
            } else {
                // Similarity too low - start new chunk
                if current_chunk.len() >= self.min_chunk_size {
                    chunks.push(current_chunk.clone());
                }
                current_chunk = sentence.clone();
                current_embedding = Some(sentence_embeddings[i].clone());
            }
        }

        // Save final chunk
        if !current_chunk.is_empty() && current_chunk.len() >= self.min_chunk_size {
            chunks.push(current_chunk);
        }

        // Fallback: if no chunks created, use character splitter
        if chunks.is_empty() {
            let fallback =
                RecursiveCharacterTextSplitter::new(self.max_chunk_size, self.min_chunk_size / 2);
            return fallback.split_text(text);
        }

        Ok(chunks)
    }
}

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

    #[test]
    fn test_recursive_splitter_basic() {
        let splitter = RecursiveCharacterTextSplitter::new(20, 0);
        let text = "Short text.";
        let chunks = splitter.split_text(text).unwrap();
        assert_eq!(chunks.len(), 1);
        assert_eq!(chunks[0], text);
    }

    #[test]
    fn test_recursive_splitter_paragraphs() {
        let splitter = RecursiveCharacterTextSplitter::new(50, 0);
        let text = "First paragraph.\n\nSecond paragraph.";
        let chunks = splitter.split_text(text).unwrap();
        assert!(chunks.len() >= 1);
    }

    #[test]
    fn test_recursive_splitter_overlap() {
        let splitter = RecursiveCharacterTextSplitter::new(20, 5);
        let text = "This is a longer text that should be split into multiple chunks.";
        let chunks = splitter.split_text(text).unwrap();
        assert!(chunks.len() > 1);
    }

    #[test]
    fn test_token_splitter() {
        let splitter = TokenTextSplitter::new(10, 2); // 10 tokens ~ 40 chars
        let text = "This is a test. This text should be split based on token count.";
        let chunks = splitter.split_text(text).unwrap();
        assert!(chunks.len() > 0);
    }

    #[test]
    fn test_empty_text() {
        let splitter = RecursiveCharacterTextSplitter::new(100, 10);
        let chunks = splitter.split_text("").unwrap();
        assert_eq!(chunks.len(), 0);
    }

    #[test]
    fn test_invalid_chunk_size() {
        let splitter = RecursiveCharacterTextSplitter::new(0, 0);
        let result = splitter.split_text("test");
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_overlap() {
        let splitter = RecursiveCharacterTextSplitter::new(100, 100);
        let result = splitter.split_text("test");
        assert!(result.is_err());
    }

    // Markdown splitter tests
    #[test]
    fn test_markdown_splitter_basic() {
        let splitter = MarkdownTextSplitter::new(200, 20);
        let text = "# Header 1\n\nSome content here.\n\n## Header 2\n\nMore content.";
        let chunks = splitter.split_text(text).unwrap();
        assert!(chunks.len() >= 1);
    }

    #[test]
    fn test_markdown_splitter_preserve_headers() {
        let splitter = MarkdownTextSplitter::new(200, 20).with_preserve_headers(true);
        let text = "# Main\n\nContent 1\n\n## Section\n\nContent 2";
        let chunks = splitter.split_text(text).unwrap();

        // When preserving headers, chunks should contain header context
        assert!(chunks.len() >= 1);
    }

    #[test]
    fn test_markdown_header_parsing() {
        let splitter = MarkdownTextSplitter::new(100, 10);

        // Test various header levels
        assert_eq!(splitter.parse_header_level("# H1"), Some(1));
        assert_eq!(splitter.parse_header_level("## H2"), Some(2));
        assert_eq!(splitter.parse_header_level("### H3"), Some(3));
        assert_eq!(splitter.parse_header_level("Not a header"), None);
        assert_eq!(splitter.parse_header_level("####### Too many"), None);
    }

    #[test]
    fn test_markdown_simple_by_default() {
        // Default behavior: simple splitting without header preservation
        let splitter = MarkdownTextSplitter::new(500, 50);
        assert!(!splitter.preserve_headers);
    }

    // Code splitter tests
    #[test]
    fn test_code_splitter_basic() {
        let splitter = CodeTextSplitter::new(200, 20);
        let code = "fn main() {\n    println!(\"Hello\");\n}\n\nfn test() {\n    // test\n}";
        let chunks = splitter.split_text(code).unwrap();
        assert!(chunks.len() >= 1);
    }

    #[test]
    fn test_code_splitter_with_language() {
        let splitter = CodeTextSplitter::new(300, 30).with_language("rust");
        let code =
            "fn main() {\n    println!(\"Hello\");\n}\n\nfn test() {\n    println!(\"Test\");\n}";
        let chunks = splitter.split_text(code).unwrap();
        assert!(chunks.len() >= 1);
    }

    #[test]
    fn test_code_block_detection() {
        let splitter = CodeTextSplitter::new(100, 10).with_language("rust");
        assert!(splitter.is_code_block_start("fn main() {"));
        assert!(splitter.is_code_block_start("pub fn test() {"));
        assert!(splitter.is_code_block_start("struct Foo {"));
        assert!(!splitter.is_code_block_start("    let x = 5;"));
    }

    #[test]
    fn test_code_splitter_simple_by_default() {
        // Default behavior: language-agnostic
        let splitter = CodeTextSplitter::new(500, 50);
        assert!(splitter.language.is_none());
    }

    // Semantic splitter tests (using mock embedder)
    struct MockEmbedder;

    impl Embedder for MockEmbedder {
        fn embed(&self, text: &str) -> Result<Vec<f32>> {
            // Simple mock: use text length as "embedding"
            // In real use, this would call an actual embedding model
            let len = text.len() as f32;
            Ok(vec![len / 100.0, len / 50.0, len / 25.0])
        }
    }

    #[test]
    fn test_semantic_splitter_basic() {
        let embedder = Box::new(MockEmbedder);
        let splitter = SemanticTextSplitter::new(embedder, 200, 20);
        let text =
            "First sentence. Second sentence here. Third one is different. Fourth continues.";
        let chunks = splitter.split_text(text).unwrap();
        assert!(chunks.len() >= 1);
    }

    #[test]
    fn test_semantic_splitter_with_threshold() {
        let embedder = Box::new(MockEmbedder);
        let splitter = SemanticTextSplitter::new(embedder, 300, 30).with_similarity_threshold(0.8);
        let text = "Sentence one. Sentence two. Sentence three.";
        let chunks = splitter.split_text(text).unwrap();
        assert!(chunks.len() >= 1);
    }

    #[test]
    fn test_semantic_splitter_cosine_similarity() {
        let embedder = Box::new(MockEmbedder);
        let splitter = SemanticTextSplitter::new(embedder, 100, 10);

        let v1 = vec![1.0, 0.0, 0.0];
        let v2 = vec![1.0, 0.0, 0.0];
        let v3 = vec![0.0, 1.0, 0.0];

        // Identical vectors should have similarity 1.0
        let sim1 = splitter.cosine_similarity(&v1, &v2);
        assert!((sim1 - 1.0).abs() < 0.01);

        // Orthogonal vectors should have similarity 0.0
        let sim2 = splitter.cosine_similarity(&v1, &v3);
        assert!(sim2.abs() < 0.01);
    }

    #[test]
    fn test_embedder_trait_composable() {
        // Test that Embedder trait is composable (HYBRID principle)
        struct CustomEmbedder;
        impl Embedder for CustomEmbedder {
            fn embed(&self, _text: &str) -> Result<Vec<f32>> {
                Ok(vec![1.0, 2.0, 3.0])
            }
        }

        let embedder = Box::new(CustomEmbedder);
        let splitter = SemanticTextSplitter::new(embedder, 500, 50);

        let text = "Test text.";
        let result = splitter.split_text(text);
        assert!(result.is_ok());
    }
}