Skip to main content

foxstash_core/
lib.rs

1//! Foxstash - Core library
2//!
3//! High-performance vector search and embedding generation for local-first AI.
4
5// Allow some clippy lints for now - will address in future cleanup
6#![allow(clippy::non_canonical_partial_ord_impl)]
7#![allow(clippy::manual_is_multiple_of)]
8#![allow(clippy::manual_div_ceil)]
9#![allow(clippy::derivable_impls)]
10#![allow(clippy::needless_range_loop)]
11#![allow(clippy::unused_enumerate_index)]
12
13pub mod embedding;
14pub mod index;
15pub mod storage;
16pub mod vector;
17
18use thiserror::Error;
19
20/// Result type for RAG operations
21pub type Result<T> = std::result::Result<T, RagError>;
22
23/// Error types for RAG operations
24#[derive(Debug, Error)]
25pub enum RagError {
26    #[error("Vector dimension mismatch: expected {expected}, got {actual}")]
27    DimensionMismatch { expected: usize, actual: usize },
28
29    #[error("Index error: {0}")]
30    IndexError(String),
31
32    #[error("Embedding error: {0}")]
33    EmbeddingError(String),
34
35    #[error("Storage error: {0}")]
36    StorageError(String),
37
38    #[error("IO error: {0}")]
39    IoError(#[from] std::io::Error),
40
41    #[error("Serialization error: {0}")]
42    SerializationError(#[from] bincode::Error),
43
44    #[error("Compression error: {0}")]
45    CompressionError(#[from] storage::compression::CompressionError),
46
47    #[error("Invalid input: {0}")]
48    InvalidInput(String),
49}
50
51/// Document with embedding
52#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
53pub struct Document {
54    pub id: String,
55    pub content: String,
56    pub embedding: Vec<f32>,
57    pub metadata: Option<serde_json::Value>,
58}
59
60/// Search result with similarity score
61#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
62pub struct SearchResult {
63    pub id: String,
64    pub content: String,
65    pub score: f32,
66    pub metadata: Option<serde_json::Value>,
67}
68
69/// Configuration for RAG system
70#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
71pub struct RagConfig {
72    pub embedding_dim: usize,
73    pub max_documents: usize,
74    pub index_type: IndexType,
75}
76
77#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
78pub enum IndexType {
79    Flat,
80    HNSW,
81}
82
83impl Default for RagConfig {
84    fn default() -> Self {
85        Self {
86            embedding_dim: 384, // MiniLM-L6-v2
87            max_documents: 10_000,
88            index_type: IndexType::HNSW,
89        }
90    }
91}
92
93// Re-export commonly used items
94pub use vector::{cosine_similarity, dot_product, l2_distance, normalize};