mem0_rust/vector_stores/
traits.rs

1//! Vector store trait definition.
2
3use async_trait::async_trait;
4use crate::errors::VectorStoreError;
5use crate::models::{Filters, MemoryRecord, Payload, ScoredMemory};
6
7/// Search result from vector store
8#[derive(Debug, Clone)]
9pub struct VectorSearchResult {
10    /// Record ID
11    pub id: String,
12    /// Similarity score
13    pub score: f32,
14    /// Payload data
15    pub payload: Payload,
16}
17
18/// Trait for vector storage backends
19#[async_trait]
20pub trait VectorStore: Send + Sync {
21    /// Insert a record with its embedding
22    async fn insert(
23        &self,
24        id: &str,
25        embedding: Vec<f32>,
26        payload: Payload,
27    ) -> Result<(), VectorStoreError>;
28
29    /// Search for similar vectors
30    async fn search(
31        &self,
32        embedding: &[f32],
33        limit: usize,
34        filters: Option<&Filters>,
35    ) -> Result<Vec<VectorSearchResult>, VectorStoreError>;
36
37    /// Get a single record by ID
38    async fn get(&self, id: &str) -> Result<Option<VectorSearchResult>, VectorStoreError>;
39
40    /// Delete a record by ID
41    async fn delete(&self, id: &str) -> Result<(), VectorStoreError>;
42
43    /// Update a record
44    async fn update(
45        &self,
46        id: &str,
47        embedding: Option<Vec<f32>>,
48        payload: Payload,
49    ) -> Result<(), VectorStoreError>;
50
51    /// List all records with optional filters
52    async fn list(
53        &self,
54        filters: Option<&Filters>,
55        limit: usize,
56    ) -> Result<Vec<VectorSearchResult>, VectorStoreError>;
57
58    /// Delete all records matching filters
59    async fn delete_all(&self, filters: Option<&Filters>) -> Result<usize, VectorStoreError>;
60
61    /// Check if collection/index exists
62    async fn collection_exists(&self) -> Result<bool, VectorStoreError>;
63
64    /// Create collection/index if it doesn't exist
65    async fn create_collection(&self) -> Result<(), VectorStoreError>;
66}
67
68/// Convert vector search result to scored memory
69impl VectorSearchResult {
70    /// Convert to a MemoryRecord
71    pub fn to_memory_record(&self) -> MemoryRecord {
72        MemoryRecord {
73            id: uuid::Uuid::parse_str(&self.id).unwrap_or_else(|_| uuid::Uuid::new_v4()),
74            content: self.payload.data.clone(),
75            metadata: self.payload.metadata.clone(),
76            user_id: self.payload.user_id.clone(),
77            agent_id: self.payload.agent_id.clone(),
78            run_id: self.payload.run_id.clone(),
79            hash: self.payload.hash.clone(),
80            created_at: self.payload.created_at,
81            updated_at: self.payload.created_at, // Use created_at as fallback
82        }
83    }
84
85    /// Convert to ScoredMemory
86    pub fn to_scored_memory(&self) -> ScoredMemory {
87        ScoredMemory {
88            record: self.to_memory_record(),
89            score: self.score,
90        }
91    }
92}