rsclaw 0.0.1-alpha.1

rsclaw: High-performance AI agent (BETA). Optimized for M4 Max and 2GB VPS. 100% compatible with openclaw
Documentation
use super::database::Store;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// Search document.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchDocument {
    pub id: Arc<str>,
    pub content: Arc<str>,
    pub doc_type: Arc<str>,
    pub timestamp: u64,
}

/// Simple in-memory search index (tantivy alternative for low memory).
pub struct SearchIndex {
    store: Arc<Store>,
    documents: Vec<SearchDocument>,
}

impl SearchIndex {
    /// Create a new search index.
    pub fn new(store: Arc<Store>) -> Self {
        Self {
            store,
            documents: Vec::new(),
        }
    }

    /// Index a document.
    pub fn index(&mut self, doc: SearchDocument) {
        self.documents.push(doc);
    }

    /// Search documents by content.
    pub fn search(&self, query: &str, limit: usize) -> Vec<&SearchDocument> {
        let query_lower = query.to_lowercase();

        self.documents
            .iter()
            .filter(|doc| doc.content.to_lowercase().contains(&query_lower))
            .take(limit)
            .collect()
    }

    /// Get document count.
    pub fn count(&self) -> usize {
        self.documents.len()
    }

    /// Clear all documents.
    pub fn clear(&mut self) {
        self.documents.clear();
    }
}

impl Default for SearchIndex {
    fn default() -> Self {
        Self::new(Arc::new(
            Store::new(&dirs::home_dir().unwrap().join(".rsclaw").join("data")).unwrap(),
        ))
    }
}