Skip to main content

hippmem_engine/
explain_api.rs

1//! Engine::explain — explain API (05 §4, 09 §4.3).
2
3use crate::{Engine, EngineError, EngineResult, Explanation, LinkSummary};
4use hippmem_core::ids::MemoryId;
5use hippmem_core::model::links::LinkType;
6
7impl Engine {
8    /// Explains a memory: source/importance/connections/corrections/recent activations.
9    pub fn explain(
10        &self,
11        id: MemoryId,
12        _ctx: Option<crate::RetrieveContext>,
13    ) -> EngineResult<Explanation> {
14        let units = crate::retrieve_api::load_all_units(self.store.db_arc());
15        let unit = units
16            .iter()
17            .find(|u| u.id == id)
18            .cloned()
19            .ok_or(EngineError::NotFound(id))?;
20
21        let linked: Vec<LinkSummary> = unit
22            .links
23            .iter()
24            .map(|l| LinkSummary {
25                target: l.target_id,
26                link_type: l.link_type,
27                strength: l.strength.value(),
28            })
29            .collect();
30
31        let corrections: Vec<MemoryId> = unit
32            .links
33            .iter()
34            .filter(|l| l.link_type == LinkType::Correction)
35            .map(|l| l.target_id)
36            .collect();
37
38        let contradictions: Vec<MemoryId> = unit
39            .links
40            .iter()
41            .filter(|l| l.link_type == LinkType::Contradiction)
42            .map(|l| l.target_id)
43            .collect();
44
45        Ok(Explanation {
46            memory_id: unit.id,
47            content_summary: unit.content.raw.chars().take(120).collect(),
48            current_importance: unit.understanding.importance.value(),
49            linked,
50            corrections,
51            contradictions,
52            recent_activations: unit.activation.retrieval_count,
53        })
54    }
55}