Skip to main content

mentedb_core/
memory.rs

1//! MemoryNode: the fundamental unit of storage in MenteDB.
2
3use serde::{Deserialize, Serialize};
4
5use crate::types::*;
6use crate::types::{AgentId, MemoryId, SpaceId};
7
8/// The type classification of a memory.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
10pub enum MemoryType {
11    /// A specific event or interaction.
12    Episodic,
13    /// A general fact or concept.
14    Semantic,
15    /// A learned skill or procedure.
16    Procedural,
17    /// What NOT to do: learned from failures.
18    AntiPattern,
19    /// A pre-computed reasoning chain or decision template.
20    Reasoning,
21    /// A correction record: "was X, actually Y because Z".
22    Correction,
23}
24
25/// A memory node: the atomic unit of knowledge in MenteDB.
26///
27/// Combines vector embeddings, graph connections, temporal properties,
28/// and flexible attributes in a single primitive.
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct MemoryNode {
31    /// Unique identifier.
32    pub id: MemoryId,
33    /// The agent that owns this memory.
34    pub agent_id: AgentId,
35    /// Memory type classification.
36    pub memory_type: MemoryType,
37    /// Embedding vector for semantic similarity search.
38    pub embedding: Embedding,
39    /// Human-readable content.
40    pub content: String,
41    /// When this memory was created.
42    pub created_at: Timestamp,
43    /// When this memory was last accessed.
44    pub accessed_at: Timestamp,
45    /// How many times this memory has been accessed.
46    pub access_count: u32,
47    /// Current salience score (decays over time).
48    pub salience: Salience,
49    /// Confidence in this memory's accuracy.
50    pub confidence: Confidence,
51    /// The memory space this belongs to.
52    pub space_id: SpaceId,
53    /// Flexible key-value attributes.
54    pub attributes: std::collections::HashMap<String, AttributeValue>,
55    /// Tags for bitmap indexing.
56    pub tags: Vec<String>,
57}
58
59impl MemoryNode {
60    /// Create a new memory node with the given content and embedding.
61    pub fn new(
62        agent_id: AgentId,
63        memory_type: MemoryType,
64        content: String,
65        embedding: Embedding,
66    ) -> Self {
67        let now = std::time::SystemTime::now()
68            .duration_since(std::time::UNIX_EPOCH)
69            .unwrap_or_default()
70            .as_micros() as u64;
71
72        Self {
73            id: MemoryId::new(),
74            agent_id,
75            memory_type,
76            embedding,
77            content,
78            created_at: now,
79            accessed_at: now,
80            access_count: 0,
81            salience: 1.0,
82            confidence: 1.0,
83            space_id: SpaceId::nil(),
84            attributes: std::collections::HashMap::new(),
85            tags: Vec::new(),
86        }
87    }
88}
89
90/// Flexible attribute values for memory metadata.
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub enum AttributeValue {
93    /// A string value.
94    String(String),
95    /// An integer value.
96    Integer(i64),
97    /// A floating-point value.
98    Float(f64),
99    /// A boolean value.
100    Boolean(bool),
101    /// Raw bytes.
102    Bytes(Vec<u8>),
103}