1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! [`MemoryRecall`]: plugs [`Memory`] into
//! `aidaemon_policies::MemoryInjection` through the `Recall` trait.
use aidaemon_policies::Recall;
use async_trait::async_trait;
use crate::memory::Memory;
/// An `aidaemon_policies::Recall` backed by [`Memory`]: top facts first,
/// then relevant episodes, as one-line snippets. Errors are logged and
/// yield no snippets, so a memory outage never breaks a run.
///
/// ```
/// use aidaemon_memory::{HashEmbedder, InMemoryMemoryStore, Memory, MemoryRecall};
///
/// let memory = Memory::new(InMemoryMemoryStore::new(), HashEmbedder::default());
/// let hook = aidaemon_policies::MemoryInjection::new(MemoryRecall::new(memory));
/// # let _ = hook;
/// ```
pub struct MemoryRecall {
memory: Memory,
max_episodes: usize,
}
impl MemoryRecall {
pub fn new(memory: Memory) -> Self {
Self {
memory,
max_episodes: 2,
}
}
/// At most this many episode snippets (default 2), within the limit.
pub fn max_episodes(mut self, n: usize) -> Self {
self.max_episodes = n;
self
}
}
#[async_trait]
impl Recall for MemoryRecall {
async fn recall(&self, session_id: &str, query: &str, limit: usize) -> Vec<String> {
if limit == 0 || query.trim().is_empty() {
return Vec::new();
}
let scope = self.memory.scope_for(session_id);
let audience = self.memory.audience_for(session_id);
let mut out: Vec<String> = match self
.memory
.recall_facts_for(&scope, query, limit, &audience)
.await
{
Ok(facts) => facts
.into_iter()
.map(|f| {
format!(
"{} {}: {}",
f.fact.category,
f.fact.key.replace('_', " "),
f.fact.value
)
})
.collect(),
Err(error) => {
tracing::warn!(%error, "fact recall failed");
Vec::new()
}
};
let room = limit.saturating_sub(out.len()).min(self.max_episodes);
if room > 0 {
match self
.memory
.recall_episodes_for(&scope, query, room, &audience)
.await
{
Ok(episodes) => out.extend(episodes.into_iter().map(|e| {
format!(
"Earlier conversation ({}): {}",
e.episode.created_at.format("%Y-%m-%d"),
e.episode.summary
)
})),
Err(error) => tracing::warn!(%error, "episode recall failed"),
}
}
out
}
}