Skip to main content

base/
memory.rs

1//! Durable memory data — what one memory is and how storing it can fail. The
2//! store, the secret scan, the prompt and the selection call live in the
3//! `memory` crate.
4
5use serde::{Deserialize, Serialize};
6
7/// A single durable memory entry — stored as a .md file.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct DurableMemory {
10    /// Short kebab-case slug (used as filename: `memory-dir/<slug>.md`).
11    pub name: String,
12    /// One-line summary — used to decide relevance during recall.
13    pub description: String,
14    /// Memory type.
15    #[serde(default)]
16    pub memory_type: MemoryType,
17    /// The memory content (body after frontmatter).
18    pub content: String,
19    /// Session that produced this memory.
20    #[serde(default)]
21    pub source_session_id: String,
22    /// Confidence score 0.0–1.0. Entries below 0.3 are not saved.
23    #[serde(default = "default_confidence")]
24    pub confidence: f64,
25    /// ISO-8601 timestamp of last write.
26    #[serde(default)]
27    pub last_seen: String,
28    /// Number of times this memory has been surfaced to the model (recall count).
29    #[serde(default)]
30    pub recall_count: u32,
31}
32
33fn default_confidence() -> f64 {
34    0.8
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
38#[serde(rename_all = "snake_case")]
39#[non_exhaustive]
40pub enum MemoryType {
41    /// Who the user is (role, expertise, preferences).
42    #[default]
43    User,
44    /// Guidance the user has given on how you should work.
45    Feedback,
46    /// Ongoing work, goals, or constraints not derivable from code/git.
47    Project,
48    /// Pointers to external resources (URLs, dashboards, tickets).
49    Reference,
50}
51
52impl MemoryType {
53    pub fn as_str(&self) -> &'static str {
54        match self {
55            MemoryType::User => "user",
56            MemoryType::Feedback => "feedback",
57            MemoryType::Project => "project",
58            MemoryType::Reference => "reference",
59        }
60    }
61}
62
63#[derive(Debug, thiserror::Error)]
64#[non_exhaustive]
65pub enum MemoryError {
66    #[error("IO error: {0}")]
67    Io(String),
68    #[error("serialization error: {0}")]
69    Serde(String),
70}