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;
#[derive(Debug, Clone)]
pub struct MemoryModule {
inner: Arc<InnerClient>,
}
impl MemoryModule {
pub(crate) fn new(inner: Arc<InnerClient>) -> Self {
Self { inner }
}
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)
}
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)
}
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)
}
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)
}
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)
}
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)
}
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)
}
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)
}
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)
}
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)
}
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)
}
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)
}
}