Skip to main content

substrate_core/
memory_port.rs

1//! MemoryPort — two-tier agent memory (recent ring + persistent history).
2//!
3//! Core defines the contract; `substrate-memory` provides ring-buffer and
4//! composed implementations; `store-sqlite` may back the persistent tier.
5
6use serde::{Deserialize, Serialize};
7use uuid::Uuid;
8
9/// A single memory record.
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct MemoryEntry {
12    /// Stable record id.
13    pub id: Uuid,
14    /// Logical key (topic, session, etc.).
15    pub key: String,
16    /// Stored text payload.
17    pub content: String,
18    /// Unix epoch seconds when the entry was written.
19    pub created_at: i64,
20}
21
22/// Agent memory: recent window + durable history.
23pub trait MemoryPort: Send + Sync {
24    /// Error type returned by memory operations.
25    type Error: std::error::Error + Send + Sync + 'static;
26
27    /// Append a new entry under `key` with `content`. Returns the new id.
28    fn append(&self, key: &str, content: &str) -> Result<Uuid, Self::Error>;
29
30    /// Return the latest value for `key`, if any.
31    fn get(&self, key: &str) -> Result<Option<String>, Self::Error>;
32
33    /// Return up to `limit` most-recent entries (newest first).
34    fn recent(&self, limit: usize) -> Result<Vec<MemoryEntry>, Self::Error>;
35
36    /// Return full durable history (newest first).
37    fn history(&self) -> Result<Vec<MemoryEntry>, Self::Error>;
38}