Skip to main content

meerkat_core/
memory.rs

1//! MemoryStore trait — semantic memory indexing for discarded conversation history.
2//!
3//! Implementations live in `meerkat-memory` crate.
4
5use async_trait::async_trait;
6use serde::{Deserialize, Serialize};
7
8/// Metadata associated with an indexed memory entry.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct MemoryMetadata {
11    /// The session ID this memory originated from.
12    pub session_id: crate::types::SessionId,
13    /// Turn number within the session.
14    pub turn: Option<u32>,
15    /// When the memory was indexed.
16    pub indexed_at: crate::time_compat::SystemTime,
17}
18
19/// A memory search result.
20#[derive(Debug, Clone)]
21pub struct MemoryResult {
22    /// The text content of the memory.
23    pub content: String,
24    /// Metadata about the source.
25    pub metadata: MemoryMetadata,
26    /// Relevance score (0.0 = no match, 1.0 = perfect match).
27    pub score: f32,
28}
29
30/// Semantic memory store for indexing and searching conversation history.
31#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
32#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
33pub trait MemoryStore: Send + Sync {
34    /// Index text content with associated metadata.
35    async fn index(&self, content: &str, metadata: MemoryMetadata) -> Result<(), MemoryStoreError>;
36
37    /// Search for memories matching the query.
38    async fn search(
39        &self,
40        query: &str,
41        limit: usize,
42    ) -> Result<Vec<MemoryResult>, MemoryStoreError>;
43}
44
45/// Errors from memory store operations.
46#[derive(Debug, thiserror::Error)]
47pub enum MemoryStoreError {
48    #[error("Embedding error: {0}")]
49    Embedding(String),
50
51    #[error("Index error: {0}")]
52    Index(String),
53
54    #[error("IO error: {0}")]
55    Io(#[from] std::io::Error),
56}