xz-rag 0.1.1

Multi-channel Retrieval-Augmented Generation engine
Documentation
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Document chunk with metadata and optional embedding.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Chunk {
    /// Unique chunk identifier.
    pub id: String,
    /// ID of the parent document.
    pub document_id: String,
    /// Index of this chunk within the document.
    pub chunk_index: u32,
    /// Text content of the chunk.
    pub content: String,
    /// Optional summary of the chunk content.
    pub summary: Option<String>,
    /// Metadata associated with this chunk.
    pub metadata: ChunkMetadata,
    /// Optional embedding vector for vector search.
    pub embedding: Option<Vec<f32>>,
    /// Timestamp when the chunk was created (milliseconds since epoch).
    pub created_at: u64,
    /// Optional expiry timestamp (milliseconds since epoch).
    pub expires_at: Option<u64>,
}

/// Metadata attached to each chunk.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ChunkMetadata {
    /// Source of the document (e.g. file path, URL).
    pub source: Option<String>,
    /// Title of the parent document.
    pub document_title: Option<String>,
    /// Author of the document.
    pub author: Option<String>,
    /// Timestamp when the chunk was created (milliseconds since epoch).
    pub created_at: Option<u64>,
    /// List of tags associated with the chunk.
    pub tags: Vec<String>,
    /// Namespace for scoping the chunk.
    pub namespace: Option<String>,
    /// Extra key-value metadata.
    pub extra: HashMap<String, String>,
}

impl Chunk {
    /// Create a new chunk with the given id, document_id, content, and index.
    pub fn new(id: String, document_id: String, content: String, chunk_index: u32) -> Self {
        Self {
            id,
            document_id,
            chunk_index,
            content,
            summary: None,
            metadata: ChunkMetadata::default(),
            embedding: None,
            created_at: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_millis() as u64,
            expires_at: None,
        }
    }
}