neomemx 0.1.2

A high-performance memory library for AI agents with semantic search
Documentation
//! Storage builder for fluent API

use crate::core::{IngestionOutcome, ScopeIdentifiers};
use crate::engine::NeomemxEngine;
use crate::error::{NeomemxError, Result};
use std::collections::HashMap;

/// Metadata map type
pub type MetadataMap = HashMap<String, serde_json::Value>;

/// Builder for storage operations
pub struct StorageBuilder {
    engine: NeomemxEngine,
    content: String,
    scope: Option<ScopeIdentifiers>,
    metadata: Option<MetadataMap>,
    enable_extraction: bool,
    enable_graph: bool,
}

impl StorageBuilder {
    /// Create a new storage builder
    pub(crate) fn new(engine: NeomemxEngine, content: impl Into<String>) -> Self {
        Self {
            engine,
            content: content.into(),
            scope: None,
            metadata: None,
            enable_extraction: true,
            enable_graph: false,
        }
    }

    /// Set scoping identifiers
    pub fn with_scope(mut self, scope: ScopeIdentifiers) -> Self {
        self.scope = Some(scope);
        self
    }

    /// Add metadata
    pub fn with_metadata(mut self, metadata: MetadataMap) -> Self {
        self.metadata = Some(metadata);
        self
    }

    /// Enable/disable fact extraction
    pub fn enable_extraction(mut self, enabled: bool) -> Self {
        self.enable_extraction = enabled;
        self
    }

    /// Enable/disable graph relationship building
    pub fn enable_graph(mut self, enabled: bool) -> Self {
        self.enable_graph = enabled;
        self
    }

    /// Execute the storage operation
    pub async fn execute(self) -> Result<IngestionOutcome> {
        let scope = self.scope.ok_or_else(|| NeomemxError::required("scope"))?;

        scope.validate()?;

        self.engine
            .store_internal(
                &self.content,
                scope,
                self.metadata,
                self.enable_extraction,
                self.enable_graph,
            )
            .await
    }
}