Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

chore: files changed crates/openhuman-core/src/memory/lifecycle/hooks.rs,crates/openhuman-core/src/m

Steven Enamakel94ae1d203986
diff --git a/crates/openhuman-core/src/memory/lifecycle/hooks.rs b/crates/openhuman-core/src/memory/lifecycle/hooks.rs
index b32cc9deb7..80fc9fca2c 100644
--- a/crates/openhuman-core/src/memory/lifecycle/hooks.rs
+++ b/crates/openhuman-core/src/memory/lifecycle/hooks.rs
@@ -49,12 +49,22 @@ pub struct TurnPack {
pub refs: Vec<String>,
/// The engine that answered.
pub engine: String,
+ /// The cited items, for the chat's memory chips.
+ #[serde(skip)]
+ pub citations: Vec<crate::memory::types::TurnCitation>,
}
impl TurnPack {
fn from_packs(packs: impl IntoIterator<Item = ContextPack>) -> Option<Self> {
let mut pack: Option<Self> = None;
for next in packs.into_iter().filter(|pack| !pack.is_empty()) {
+ let cited: Vec<crate::memory::types::TurnCitation> = next
+ .sections
+ .iter()
+ .flat_map(|section| section.hits.iter())
+ .filter(|hit| next.refs.contains(&hit.id))
+ .map(crate::memory::types::TurnCitation::from)
+ .collect();
let refs = next.refs.iter().map(ToString::to_string);
match &mut pack {
Some(pack) => {
@@ -66,6 +76,11 @@ impl TurnPack {
pack.refs.push(id);
}
}
+ for citation in cited {
+ if !pack.citations.iter().any(|known| known.id == citation.id) {
+ pack.citations.push(citation);
+ }
+ }
}
None => {
pack = Some(Self {
@@ -73,6 +88,7 @@ impl TurnPack {
tokens: next.tokens,
refs: refs.collect(),
engine: next.engine,
+ citations: cited,
});
}
}
@@ -230,6 +246,9 @@ pub async fn pre_turn(
None
}
};
+ if let Some(pack) = &pack {
+ crate::memory::tools::record_pack_citations(&thread_id, pack.citations.clone());
+ }
tracing::debug!(
thread_id = %thread_id,
agent_id = %agent_id,
diff --git a/crates/openhuman-core/src/memory/tools.rs b/crates/openhuman-core/src/memory/tools.rs
index ce19a90762..4336ee72ec 100644
--- a/crates/openhuman-core/src/memory/tools.rs
+++ b/crates/openhuman-core/src/memory/tools.rs
@@ -59,8 +59,29 @@ fn record_turn_citations(thread_id: &str, citations: &[tinymemory_api::Citation]
}
}
-/// Drains the citations `recall` produced for `thread_id` since the last
-/// drain.
+/// Records `citations` for `thread_id`'s in-flight turn: what the turn's
+/// memory pack cited (`lifecycle::hooks::pre_turn`), beside what `recall`
+/// cites.
+pub fn record_pack_citations(thread_id: &str, citations: Vec<TurnCitation>) {
+ if citations.is_empty() {
+ return;
+ }
+ let mut all = TURN_CITATIONS
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ let entry = all.entry(thread_id.to_string()).or_default();
+ for citation in citations {
+ if entry.len() >= MAX_TURN_CITATIONS {
+ break;
+ }
+ if !entry.iter().any(|existing| existing.id == citation.id) {
+ entry.push(citation);
+ }
+ }
+}
+
+/// Drains the citations `recall` and the turn's pack produced for
+/// `thread_id` since the last drain.
#[must_use]
pub fn take_turn_citations(thread_id: &str) -> Vec<TurnCitation> {
TURN_CITATIONS
diff --git a/crates/openhuman-core/src/memory/types.rs b/crates/openhuman-core/src/memory/types.rs
index a695c2530a..b065274e5b 100644
--- a/crates/openhuman-core/src/memory/types.rs
+++ b/crates/openhuman-core/src/memory/types.rs
@@ -397,6 +397,26 @@ pub struct TurnCitation {
pub snippet: String,
}
+impl From<&Hit> for TurnCitation {
+ fn from(hit: &Hit) -> Self {
+ Self {
+ id: hit.id.0.clone(),
+ key: hit.kind.as_str().to_string(),
+ namespace: Some(hit.meta.source.kind.as_str().to_string()),
+ score: Some(f64::from(hit.score)).filter(|score| *score > 0.0),
+ timestamp: hit
+ .meta
+ .observed_at
+ .map(|at| at.to_rfc3339())
+ .unwrap_or_default(),
+ snippet: hit.text.chars().take(TURN_CITATION_SNIPPET_CHARS).collect(),
+ }
+ }
+}
+
+/// Longest snippet a turn citation carries, in characters.
+pub const TURN_CITATION_SNIPPET_CHARS: usize = 280;
+
impl From<&Citation> for TurnCitation {
fn from(citation: &Citation) -> Self {
Self {