cencori 1.4.0

Official Rust SDK for Cencori - AI Infrastructure for Production
Documentation
/// Module for Cencori Memory - Vector storage for RAG, conversation history,
/// and semantic search.
use std::sync::Arc;

use crate::client::{parse_response, InnerClient};
use crate::error::CencoriError;
use crate::types::{
    CreateNamespaceOptions, Memory, MemoryNamespace, SearchMemoryOptions, SearchResult,
    StoreMemoryOptions,
};
use serde_json::Value;

/// Module for Cencori Memory operations.
///
/// Vector storage for RAG, conversation history, and semantic search.
#[derive(Debug, Clone)]
pub struct MemoryModule {
    inner: Arc<InnerClient>,
}

impl MemoryModule {
    pub(crate) fn new(inner: Arc<InnerClient>) -> Self {
        Self { inner }
    }

    // ==================
    // Namespace Methods
    // ==================

    /// Create a new memory namespace.
    pub fn create_namespace(
        &self,
        options: &CreateNamespaceOptions,
    ) -> Result<MemoryNamespace, CencoriError> {
        let url = self.inner.url("/api/memory/namespaces");
        let response = self
            .inner
            .blocking_client
            .post(&url)
            .json(options)
            .send()
            .map_err(CencoriError::Http)?;

        let status = response.status();
        let body = response.text().map_err(CencoriError::Http)?;
        let data = parse_response(status, &body)?;

        serde_json::from_value(data).map_err(CencoriError::Json)
    }

    /// List all namespaces.
    pub fn list_namespaces(&self) -> Result<Vec<MemoryNamespace>, CencoriError> {
        let url = self.inner.url("/api/memory/namespaces");
        let response = self
            .inner
            .blocking_client
            .get(&url)
            .send()
            .map_err(CencoriError::Http)?;

        let status = response.status();
        let body = response.text().map_err(CencoriError::Http)?;
        let data = parse_response(status, &body)?;

        let namespaces = data["namespaces"]
            .as_array()
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| serde_json::from_value(v.clone()).ok())
                    .collect()
            })
            .unwrap_or_default();

        Ok(namespaces)
    }

    // ==================
    // Memory Methods
    // ==================

    /// Store a memory in a namespace.
    pub fn store(&self, options: &StoreMemoryOptions) -> Result<Memory, CencoriError> {
        let url = self.inner.url("/api/memory/store");
        let response = self
            .inner
            .blocking_client
            .post(&url)
            .json(options)
            .send()
            .map_err(CencoriError::Http)?;

        let status = response.status();
        let body = response.text().map_err(CencoriError::Http)?;
        let data = parse_response(status, &body)?;

        serde_json::from_value(data).map_err(CencoriError::Json)
    }

    /// Semantic search across memories.
    pub fn search(&self, options: &SearchMemoryOptions) -> Result<SearchResult, CencoriError> {
        let url = self.inner.url("/api/memory/search");
        let response = self
            .inner
            .blocking_client
            .post(&url)
            .json(options)
            .send()
            .map_err(CencoriError::Http)?;

        let status = response.status();
        let body = response.text().map_err(CencoriError::Http)?;
        let data = parse_response(status, &body)?;

        serde_json::from_value(data).map_err(CencoriError::Json)
    }

    /// Get a memory by ID.
    pub fn get(&self, id: &str) -> Result<Memory, CencoriError> {
        let path = format!("/api/memory/{id}");
        let url = self.inner.url(&path);
        let response = self
            .inner
            .blocking_client
            .get(&url)
            .send()
            .map_err(CencoriError::Http)?;

        let status = response.status();
        let body = response.text().map_err(CencoriError::Http)?;
        let data = parse_response(status, &body)?;

        serde_json::from_value(data).map_err(CencoriError::Json)
    }

    /// Delete a memory by ID.
    pub fn delete(&self, id: &str) -> Result<Value, CencoriError> {
        let path = format!("/api/memory/{id}");
        let url = self.inner.url(&path);
        let response = self
            .inner
            .blocking_client
            .delete(&url)
            .send()
            .map_err(CencoriError::Http)?;

        let status = response.status();
        let body = response.text().map_err(CencoriError::Http)?;
        let data = parse_response(status, &body)?;

        Ok(data)
    }

    // =========================================================================
    // Asynchronous Methods
    // =========================================================================

    /// Create a new memory namespace asynchronously.
    pub async fn async_create_namespace(
        &self,
        options: &CreateNamespaceOptions,
    ) -> Result<MemoryNamespace, CencoriError> {
        let url = self.inner.url("/api/memory/namespaces");
        let response = self
            .inner
            .http_client
            .post(&url)
            .json(options)
            .send()
            .await
            .map_err(CencoriError::Http)?;

        let status = response.status();
        let body = response.text().await.map_err(CencoriError::Http)?;
        let data = parse_response(status, &body)?;

        serde_json::from_value(data).map_err(CencoriError::Json)
    }

    /// List all namespaces asynchronously.
    pub async fn async_list_namespaces(&self) -> Result<Vec<MemoryNamespace>, CencoriError> {
        let url = self.inner.url("/api/memory/namespaces");
        let response = self
            .inner
            .http_client
            .get(&url)
            .send()
            .await
            .map_err(CencoriError::Http)?;

        let status = response.status();
        let body = response.text().await.map_err(CencoriError::Http)?;
        let data = parse_response(status, &body)?;

        let namespaces = data["namespaces"]
            .as_array()
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| serde_json::from_value(v.clone()).ok())
                    .collect()
            })
            .unwrap_or_default();

        Ok(namespaces)
    }

    /// Store a memory asynchronously.
    pub async fn async_store(&self, options: &StoreMemoryOptions) -> Result<Memory, CencoriError> {
        let url = self.inner.url("/api/memory/store");
        let response = self
            .inner
            .http_client
            .post(&url)
            .json(options)
            .send()
            .await
            .map_err(CencoriError::Http)?;

        let status = response.status();
        let body = response.text().await.map_err(CencoriError::Http)?;
        let data = parse_response(status, &body)?;

        serde_json::from_value(data).map_err(CencoriError::Json)
    }

    /// Semantic search asynchronously.
    pub async fn async_search(
        &self,
        options: &SearchMemoryOptions,
    ) -> Result<SearchResult, CencoriError> {
        let url = self.inner.url("/api/memory/search");
        let response = self
            .inner
            .http_client
            .post(&url)
            .json(options)
            .send()
            .await
            .map_err(CencoriError::Http)?;

        let status = response.status();
        let body = response.text().await.map_err(CencoriError::Http)?;
        let data = parse_response(status, &body)?;

        serde_json::from_value(data).map_err(CencoriError::Json)
    }

    /// Get a memory by ID asynchronously.
    pub async fn async_get(&self, id: &str) -> Result<Memory, CencoriError> {
        let path = format!("/api/memory/{id}");
        let url = self.inner.url(&path);
        let response = self
            .inner
            .http_client
            .get(&url)
            .send()
            .await
            .map_err(CencoriError::Http)?;

        let status = response.status();
        let body = response.text().await.map_err(CencoriError::Http)?;
        let data = parse_response(status, &body)?;

        serde_json::from_value(data).map_err(CencoriError::Json)
    }

    /// Delete a memory by ID asynchronously.
    pub async fn async_delete(&self, id: &str) -> Result<Value, CencoriError> {
        let path = format!("/api/memory/{id}");
        let url = self.inner.url(&path);
        let response = self
            .inner
            .http_client
            .delete(&url)
            .send()
            .await
            .map_err(CencoriError::Http)?;

        let status = response.status();
        let body = response.text().await.map_err(CencoriError::Http)?;
        let data = parse_response(status, &body)?;

        Ok(data)
    }
}