Skip to main content

nexus_memory_agent/
recall.rs

1//! Recall service - provides read operations for memory retrieval tools.
2
3use crate::error::AgentError;
4use nexus_core::{CognitiveLevel, Memory, PerspectiveKey};
5use nexus_storage::{MemoryLineageEntry, MemoryRepository};
6
7/// Service for recall operations used by MCP tools.
8pub struct RecallToolService;
9
10impl RecallToolService {
11    /// Create a new RecallToolService.
12    pub fn new() -> Self {
13        Self
14    }
15
16    /// Search memories by perspective, optionally filtered by cognitive level.
17    pub async fn search_memory(
18        &self,
19        repo: &MemoryRepository,
20        namespace_id: i64,
21        perspective: &PerspectiveKey,
22        cognitive_level: Option<CognitiveLevel>,
23        limit: i64,
24    ) -> Result<Vec<Memory>, AgentError> {
25        let memories = match cognitive_level {
26            Some(level) => repo
27                .get_by_cognitive_level_with_perspective(namespace_id, level, perspective, limit)
28                .await
29                .map_err(|e| AgentError::Storage(e.to_string()))?,
30            None => repo
31                .get_recent_by_perspective(namespace_id, perspective, limit)
32                .await
33                .map_err(|e| AgentError::Storage(e.to_string()))?,
34        };
35        Ok(memories)
36    }
37
38    /// Get session digests (short and long) for a given session.
39    pub async fn get_session_digest(
40        &self,
41        repo: &MemoryRepository,
42        namespace_id: i64,
43        session_key: &str,
44    ) -> Result<(Option<Memory>, Option<Memory>), AgentError> {
45        let short = repo
46            .latest_digest_for_session(namespace_id, session_key, "short")
47            .await
48            .map_err(|e| AgentError::Storage(e.to_string()))?;
49        let long = repo
50            .latest_digest_for_session(namespace_id, session_key, "long")
51            .await
52            .map_err(|e| AgentError::Storage(e.to_string()))?;
53        Ok((short, long))
54    }
55
56    /// Get the lineage (evidence links) for a memory.
57    pub async fn get_memory_lineage(
58        &self,
59        repo: &MemoryRepository,
60        memory_id: i64,
61    ) -> Result<Vec<MemoryLineageEntry>, AgentError> {
62        let lineage = repo
63            .load_lineage(memory_id)
64            .await
65            .map_err(|e| AgentError::Storage(e.to_string()))?;
66        Ok(lineage)
67    }
68}
69
70impl Default for RecallToolService {
71    fn default() -> Self {
72        Self::new()
73    }
74}