reasonkit-mem 0.1.7

High-performance vector database & RAG memory layer - hybrid search, embeddings, RAPTOR trees, BM25 fusion, and semantic retrieval for AI systems
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
//! Core types for memory and retrieval operations.
//!
//! This module defines the fundamental data structures used throughout the
//! `reasonkit-mem` crate. These types represent documents, chunks, metadata,
//! and retrieval configurations.
//!
//! # Overview
//!
//! The type hierarchy is:
//!
//! ```text
//! Document
//! +-- id: Uuid
//! +-- doc_type: DocumentType
//! +-- source: Source
//! +-- content: DocumentContent
//! +-- metadata: Metadata
//! +-- processing: ProcessingStatus
//! +-- chunks: Vec<Chunk>
//!     +-- id: Uuid
//!     +-- text: String
//!     +-- embedding_ids: EmbeddingIds
//! ```
//!
//! # Example
//!
//! ```rust
//! use reasonkit_mem::{Document, DocumentType, Source, SourceType, Metadata, Author};
//! use chrono::Utc;
//!
//! // Create a source
//! let source = Source {
//!     source_type: SourceType::Arxiv,
//!     url: Some("https://arxiv.org/abs/2401.18059".to_string()),
//!     path: None,
//!     arxiv_id: Some("2401.18059".to_string()),
//!     github_repo: None,
//!     retrieved_at: Utc::now(),
//!     version: None,
//! };
//!
//! // Create a document
//! let mut doc = Document::new(DocumentType::Paper, source)
//!     .with_content("Abstract: This paper presents...".to_string());
//!
//! // Add metadata
//! doc.metadata = Metadata {
//!     title: Some("RAPTOR: Recursive Abstractive Processing".to_string()),
//!     authors: vec![Author {
//!         name: "Sarthi et al.".to_string(),
//!         affiliation: Some("Stanford University".to_string()),
//!         email: None,
//!     }],
//!     ..Default::default()
//! };
//! ```

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Document type categorization.
///
/// Categorizes documents by their type to enable type-specific processing
/// and retrieval strategies.
///
/// # Example
///
/// ```rust
/// use reasonkit_mem::DocumentType;
///
/// let doc_type = DocumentType::Paper;
/// assert_eq!(doc_type, DocumentType::Paper);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DocumentType {
    /// Academic paper (PDF, typically from arXiv or journals)
    Paper,
    /// Technical documentation (README, API docs, guides)
    Documentation,
    /// Source code (Rust, Python, etc.)
    Code,
    /// User notes (personal notes, meeting notes)
    Note,
    /// Meeting/interview transcript
    Transcript,
    /// Benchmark data (performance metrics, test results)
    Benchmark,
}

/// Source information for a document.
///
/// Tracks where a document came from, when it was retrieved, and any
/// relevant identifiers (arXiv ID, GitHub repo, etc.).
///
/// # Example
///
/// ```rust
/// use reasonkit_mem::{Source, SourceType};
/// use chrono::Utc;
///
/// let source = Source {
///     source_type: SourceType::Github,
///     url: Some("https://github.com/org/repo".to_string()),
///     path: None,
///     arxiv_id: None,
///     github_repo: Some("org/repo".to_string()),
///     retrieved_at: Utc::now(),
///     version: Some("v1.0.0".to_string()),
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Source {
    /// Source type (arXiv, GitHub, local file, etc.)
    #[serde(rename = "type")]
    pub source_type: SourceType,
    /// Original URL where the document was retrieved
    pub url: Option<String>,
    /// Local file path (if applicable)
    pub path: Option<String>,
    /// arXiv ID (e.g., "2401.18059")
    pub arxiv_id: Option<String>,
    /// GitHub repository (e.g., "anthropics/claude-code")
    pub github_repo: Option<String>,
    /// When the document was retrieved
    pub retrieved_at: DateTime<Utc>,
    /// Version or commit hash
    pub version: Option<String>,
}

/// Source type enumeration.
///
/// Identifies the origin of a document for provenance tracking.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SourceType {
    /// arXiv paper
    Arxiv,
    /// GitHub repository
    Github,
    /// Website
    Website,
    /// Local file
    Local,
    /// API response
    Api,
}

/// Document metadata.
///
/// Contains bibliographic and descriptive metadata about a document,
/// including title, authors, abstract, publication information, and tags.
///
/// # Example
///
/// ```rust
/// use reasonkit_mem::{Metadata, Author};
///
/// let metadata = Metadata {
///     title: Some("Machine Learning Fundamentals".to_string()),
///     authors: vec![
///         Author {
///             name: "Alice Smith".to_string(),
///             affiliation: Some("MIT".to_string()),
///             email: None,
///         }
///     ],
///     tags: vec!["ml".to_string(), "tutorial".to_string()],
///     ..Default::default()
/// };
/// ```
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Metadata {
    /// Document title
    pub title: Option<String>,
    /// Authors of the document
    pub authors: Vec<Author>,
    /// Abstract or summary
    #[serde(rename = "abstract")]
    pub abstract_text: Option<String>,
    /// Publication/creation date (ISO 8601 string)
    pub date: Option<String>,
    /// Publication venue (journal, conference, etc.)
    pub venue: Option<String>,
    /// Citation count (if available)
    pub citations: Option<i32>,
    /// User-defined tags for categorization
    pub tags: Vec<String>,
    /// ReasonKit-specific categories
    pub categories: Vec<String>,
    /// Extracted keywords
    pub keywords: Vec<String>,
    /// Digital Object Identifier
    pub doi: Option<String>,
    /// Content license
    pub license: Option<String>,
}

/// Author information.
///
/// Represents an author with name, affiliation, and contact information.
///
/// # Example
///
/// ```rust
/// use reasonkit_mem::Author;
///
/// let author = Author {
///     name: "John Doe".to_string(),
///     affiliation: Some("Stanford University".to_string()),
///     email: Some("jdoe@stanford.edu".to_string()),
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Author {
    /// Author name
    pub name: String,
    /// Institutional affiliation
    pub affiliation: Option<String>,
    /// Email address
    pub email: Option<String>,
}

/// A chunk of text from a document.
///
/// Documents are split into chunks for efficient retrieval. Each chunk
/// contains the text content, position information, and references to
/// associated embeddings.
///
/// # Example
///
/// ```rust
/// use reasonkit_mem::{Chunk, EmbeddingIds};
/// use uuid::Uuid;
///
/// let chunk = Chunk {
///     id: Uuid::new_v4(),
///     text: "This is the chunk content.".to_string(),
///     index: 0,
///     start_char: 0,
///     end_char: 26,
///     token_count: Some(6),
///     section: Some("Introduction".to_string()),
///     page: Some(1),
///     embedding_ids: EmbeddingIds::default(),
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Chunk {
    /// Unique chunk identifier
    pub id: Uuid,
    /// Chunk text content
    pub text: String,
    /// Position in document (0-indexed)
    pub index: usize,
    /// Start character offset in original document
    pub start_char: usize,
    /// End character offset in original document
    pub end_char: usize,
    /// Approximate token count
    pub token_count: Option<usize>,
    /// Section heading (if applicable)
    pub section: Option<String>,
    /// Page number (for PDFs)
    pub page: Option<usize>,
    /// Associated embedding IDs
    pub embedding_ids: EmbeddingIds,
}

/// References to different embedding types for a chunk.
///
/// Tracks the IDs of embeddings stored in different vector stores.
///
/// # Example
///
/// ```rust
/// use reasonkit_mem::EmbeddingIds;
///
/// let ids = EmbeddingIds {
///     dense: Some("emb_123456".to_string()),
///     sparse: None,
///     colbert: None,
/// };
/// ```
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct EmbeddingIds {
    /// Dense embedding ID (in Qdrant)
    pub dense: Option<String>,
    /// Sparse embedding ID (for BM25/SPLADE)
    pub sparse: Option<String>,
    /// ColBERT embedding ID (for late interaction)
    pub colbert: Option<String>,
}

/// Processing status for a document.
///
/// Tracks the processing pipeline status for a document, including
/// chunking, embedding, indexing, and RAPTOR tree building.
///
/// # Example
///
/// ```rust
/// use reasonkit_mem::{ProcessingStatus, ProcessingState};
///
/// let status = ProcessingStatus {
///     status: ProcessingState::Completed,
///     chunked: true,
///     embedded: true,
///     indexed: true,
///     raptor_processed: false,
///     errors: vec![],
/// };
/// ```
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ProcessingStatus {
    /// Overall processing status
    pub status: ProcessingState,
    /// Whether the document has been chunked
    pub chunked: bool,
    /// Whether embeddings have been generated
    pub embedded: bool,
    /// Whether the document has been indexed (BM25)
    pub indexed: bool,
    /// Whether RAPTOR tree has been built
    pub raptor_processed: bool,
    /// Error messages from processing
    pub errors: Vec<String>,
}

/// Processing state enumeration.
///
/// Represents the current state of document processing.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProcessingState {
    /// Not yet processed
    #[default]
    Pending,
    /// Currently processing
    Processing,
    /// Successfully completed
    Completed,
    /// Failed with errors
    Failed,
}

/// A document in the knowledge base.
///
/// The primary data structure for storing documents. Contains all information
/// about a document including content, metadata, processing status, and chunks.
///
/// # Example
///
/// ```rust
/// use reasonkit_mem::{Document, DocumentType, Source, SourceType};
/// use chrono::Utc;
///
/// let source = Source {
///     source_type: SourceType::Local,
///     url: None,
///     path: Some("/path/to/doc.md".to_string()),
///     arxiv_id: None,
///     github_repo: None,
///     retrieved_at: Utc::now(),
///     version: None,
/// };
///
/// let doc = Document::new(DocumentType::Note, source)
///     .with_content("Document content here.".to_string());
///
/// assert_eq!(doc.doc_type, DocumentType::Note);
/// assert_eq!(doc.content.word_count, 3);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Document {
    /// Unique document identifier
    pub id: Uuid,
    /// Document type
    #[serde(rename = "type")]
    pub doc_type: DocumentType,
    /// Source information
    pub source: Source,
    /// Raw content
    pub content: DocumentContent,
    /// Metadata
    pub metadata: Metadata,
    /// Processing status
    pub processing: ProcessingStatus,
    /// Document chunks
    pub chunks: Vec<Chunk>,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
    /// Last update timestamp
    pub updated_at: Option<DateTime<Utc>>,
}

/// Document content.
///
/// Contains the raw text content of a document along with format
/// information and statistics.
///
/// # Example
///
/// ```rust
/// use reasonkit_mem::{DocumentContent, ContentFormat};
///
/// let content = DocumentContent {
///     raw: "Hello, world!".to_string(),
///     format: ContentFormat::Text,
///     language: "en".to_string(),
///     word_count: 2,
///     char_count: 13,
/// };
/// ```
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DocumentContent {
    /// Full extracted text
    pub raw: String,
    /// Content format
    pub format: ContentFormat,
    /// Language code (ISO 639-1)
    pub language: String,
    /// Word count
    pub word_count: usize,
    /// Character count
    pub char_count: usize,
}

/// Content format.
///
/// Identifies the format of the document content for proper rendering.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContentFormat {
    /// Plain text
    #[default]
    Text,
    /// Markdown
    Markdown,
    /// HTML
    Html,
    /// LaTeX
    Latex,
}

impl Document {
    /// Create a new document with the given type and source.
    ///
    /// # Arguments
    ///
    /// * `doc_type` - The type of document (Paper, Code, Note, etc.)
    /// * `source` - Source information for the document
    ///
    /// # Returns
    ///
    /// A new `Document` with default content, metadata, and processing status.
    ///
    /// # Example
    ///
    /// ```rust
    /// use reasonkit_mem::{Document, DocumentType, Source, SourceType};
    /// use chrono::Utc;
    ///
    /// let source = Source {
    ///     source_type: SourceType::Local,
    ///     url: None,
    ///     path: Some("/path/to/file.md".to_string()),
    ///     arxiv_id: None,
    ///     github_repo: None,
    ///     retrieved_at: Utc::now(),
    ///     version: None,
    /// };
    ///
    /// let doc = Document::new(DocumentType::Note, source);
    /// ```
    pub fn new(doc_type: DocumentType, source: Source) -> Self {
        Self {
            id: Uuid::new_v4(),
            doc_type,
            source,
            content: DocumentContent::default(),
            metadata: Metadata::default(),
            processing: ProcessingStatus::default(),
            chunks: Vec::new(),
            created_at: Utc::now(),
            updated_at: None,
        }
    }

    /// Set the raw content of the document.
    ///
    /// Automatically computes word and character counts.
    ///
    /// # Arguments
    ///
    /// * `raw` - The raw text content
    ///
    /// # Returns
    ///
    /// The document with updated content.
    ///
    /// # Example
    ///
    /// ```rust
    /// use reasonkit_mem::{Document, DocumentType, Source, SourceType};
    /// use chrono::Utc;
    ///
    /// let source = Source {
    ///     source_type: SourceType::Local,
    ///     url: None,
    ///     path: None,
    ///     arxiv_id: None,
    ///     github_repo: None,
    ///     retrieved_at: Utc::now(),
    ///     version: None,
    /// };
    ///
    /// let doc = Document::new(DocumentType::Note, source)
    ///     .with_content("Hello, world!".to_string());
    ///
    /// assert_eq!(doc.content.word_count, 2);
    /// assert_eq!(doc.content.char_count, 13);
    /// ```
    pub fn with_content(mut self, raw: String) -> Self {
        let word_count = raw.split_whitespace().count();
        let char_count = raw.len();
        self.content = DocumentContent {
            raw,
            format: ContentFormat::Text,
            language: "en".to_string(),
            word_count,
            char_count,
        };
        self
    }

    /// Set the metadata for the document.
    ///
    /// # Arguments
    ///
    /// * `metadata` - The document metadata
    ///
    /// # Returns
    ///
    /// The document with updated metadata.
    ///
    /// # Example
    ///
    /// ```rust
    /// use reasonkit_mem::{Document, DocumentType, Source, SourceType, Metadata};
    /// use chrono::Utc;
    ///
    /// let source = Source {
    ///     source_type: SourceType::Local,
    ///     url: None,
    ///     path: None,
    ///     arxiv_id: None,
    ///     github_repo: None,
    ///     retrieved_at: Utc::now(),
    ///     version: None,
    /// };
    ///
    /// let metadata = Metadata {
    ///     title: Some("My Document".to_string()),
    ///     ..Default::default()
    /// };
    ///
    /// let doc = Document::new(DocumentType::Note, source)
    ///     .with_metadata(metadata);
    ///
    /// assert_eq!(doc.metadata.title, Some("My Document".to_string()));
    /// ```
    pub fn with_metadata(mut self, metadata: Metadata) -> Self {
        self.metadata = metadata;
        self
    }
}

/// Search result from a query.
///
/// Contains the matched chunk, relevance score, and match source information.
///
/// # Example
///
/// ```rust
/// use reasonkit_mem::{SearchResult, Chunk, EmbeddingIds, MatchSource};
/// use uuid::Uuid;
///
/// let chunk = Chunk {
///     id: Uuid::new_v4(),
///     text: "Matched content".to_string(),
///     index: 0,
///     start_char: 0,
///     end_char: 15,
///     token_count: Some(2),
///     section: None,
///     page: None,
///     embedding_ids: EmbeddingIds::default(),
/// };
///
/// let result = SearchResult {
///     score: 0.95,
///     document_id: Uuid::new_v4(),
///     chunk,
///     match_source: MatchSource::Hybrid,
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
    /// Relevance score (higher is more relevant)
    pub score: f32,
    /// Document ID containing the matched chunk
    pub document_id: Uuid,
    /// The matched chunk
    pub chunk: Chunk,
    /// Source of the match (dense, sparse, hybrid, raptor)
    pub match_source: MatchSource,
}

/// Source of a search match.
///
/// Indicates which retrieval method produced the match.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MatchSource {
    /// Dense vector match (semantic similarity)
    Dense,
    /// Sparse (BM25) match (keyword matching)
    Sparse,
    /// Hybrid search (combined dense + sparse)
    Hybrid,
    /// RAPTOR tree match (hierarchical retrieval)
    Raptor,
}

/// Configuration for retrieval operations.
///
/// Controls the behavior of search operations including result count,
/// scoring thresholds, and retrieval method weights.
///
/// # Example
///
/// ```rust
/// use reasonkit_mem::RetrievalConfig;
///
/// // Default configuration (favors semantic search)
/// let config = RetrievalConfig::default();
/// assert_eq!(config.top_k, 10);
/// assert_eq!(config.alpha, 0.7);
///
/// // Custom configuration for keyword-heavy search
/// let keyword_config = RetrievalConfig {
///     top_k: 20,
///     alpha: 0.3,  // More weight on BM25
///     rerank: true,
///     ..Default::default()
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetrievalConfig {
    /// Number of results to return
    pub top_k: usize,
    /// Minimum score threshold (results below this are filtered)
    pub min_score: f32,
    /// Alpha for hybrid search weight.
    /// - `0.0` = sparse (BM25) only
    /// - `1.0` = dense (vector) only
    /// - `0.7` (default) = 70% dense, 30% sparse
    pub alpha: f32,
    /// Whether to use RAPTOR tree for hierarchical retrieval
    pub use_raptor: bool,
    /// Whether to apply cross-encoder reranking
    pub rerank: bool,
}

impl Default for RetrievalConfig {
    fn default() -> Self {
        Self {
            top_k: 10,
            min_score: 0.0,
            alpha: 0.7, // Favor semantic search
            use_raptor: false,
            rerank: false,
        }
    }
}

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

    #[test]
    fn test_document_creation() {
        let source = Source {
            source_type: SourceType::Arxiv,
            url: Some("https://arxiv.org/abs/2401.18059".to_string()),
            path: None,
            arxiv_id: Some("2401.18059".to_string()),
            github_repo: None,
            retrieved_at: Utc::now(),
            version: None,
        };

        let doc = Document::new(DocumentType::Paper, source)
            .with_content("This is a test paper about RAPTOR.".to_string());

        assert_eq!(doc.doc_type, DocumentType::Paper);
        assert_eq!(doc.content.word_count, 7);
        assert!(doc.content.raw.contains("RAPTOR"));
    }

    #[test]
    fn test_retrieval_config_default() {
        let config = RetrievalConfig::default();
        assert_eq!(config.top_k, 10);
        assert_eq!(config.alpha, 0.7);
        assert!(!config.use_raptor);
    }
}