Skip to main content

nexus_memory_core/
traits.rs

1//! Core traits for Nexus Memory System
2
3use crate::{
4    AgentNamespace, Memory, NamespaceStats, SearchQuery, SearchResult, StoreMemoryRequest,
5};
6use async_trait::async_trait;
7
8/// Trait for memory storage backends
9#[async_trait]
10pub trait MemoryStorage: Send + Sync {
11    /// Store a new memory
12    async fn store(&self, request: StoreMemoryRequest) -> crate::Result<Memory>;
13
14    /// Search memories
15    async fn search(&self, query: SearchQuery) -> crate::Result<SearchResult>;
16
17    /// Get a memory by ID
18    async fn get_by_id(&self, id: i64) -> crate::Result<Option<Memory>>;
19
20    /// Delete a memory
21    async fn delete(&self, id: i64) -> crate::Result<bool>;
22
23    /// Update a memory
24    async fn update(&self, memory: Memory) -> crate::Result<Memory>;
25}
26
27/// Trait for namespace management
28#[async_trait]
29pub trait NamespaceManager: Send + Sync {
30    /// Get or create a namespace
31    async fn get_or_create(&self, name: &str, agent_type: &str) -> crate::Result<AgentNamespace>;
32
33    /// Get a namespace by name
34    async fn get_by_name(&self, name: &str) -> crate::Result<Option<AgentNamespace>>;
35
36    /// List all namespaces
37    async fn list_all(&self) -> crate::Result<Vec<AgentNamespace>>;
38
39    /// Get statistics for a namespace
40    async fn stats(&self, name: &str) -> crate::Result<NamespaceStats>;
41}
42
43/// Trait for embedding services
44#[async_trait]
45pub trait EmbeddingService: Send + Sync {
46    /// Generate embedding for a single text
47    async fn embed(&self, text: &str) -> crate::Result<Vec<f32>>;
48
49    /// Generate embeddings for multiple texts
50    async fn embed_batch(&self, texts: &[String]) -> crate::Result<Vec<Vec<f32>>>;
51
52    /// Get the dimension of embeddings
53    fn dimension(&self) -> usize;
54
55    /// Get the model name
56    fn model_name(&self) -> &str;
57}
58
59/// Trait for vector search operations
60#[async_trait]
61pub trait VectorSearch: Send + Sync {
62    /// Find similar vectors
63    async fn find_similar(
64        &self,
65        embedding: &[f32],
66        namespace_id: i64,
67        limit: usize,
68        threshold: f32,
69    ) -> crate::Result<Vec<(i64, f32)>>;
70
71    /// Store a vector
72    async fn store_vector(&self, memory_id: i64, embedding: &[f32]) -> crate::Result<()>;
73
74    /// Delete a vector
75    async fn delete_vector(&self, memory_id: i64) -> crate::Result<()>;
76}