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/agent/tinyagents/middleware/memory_pack.rs

Steven Enamakel921c1c41fd47
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/memory_pack.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/memory_pack.rs
new file mode 100644
index 0000000000..2fa10129f5
--- /dev/null
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/memory_pack.rs
@@ -0,0 +1,56 @@
+//! Request-scoped delivery of the turn's memory pack.
+//!
+//! The session host recalls one pack per turn before the model runs
+//! (`memory::lifecycle::hooks::pre_turn`) and parks it on the run context
+//! (`OpenHumanRunContext::memory_pack`). This middleware adds it to every model
+//! request of that turn with [`push_ephemeral_instruction`]: a tail system
+//! message, or on a model that hoists system turns (DeepSeek, native
+//! Anthropic) a note on the tail user or tool message. The request is built
+//! from a copy of the working transcript, so the pack is never committed —
+//! the thread's persisted history and the provider's cached prefix stay
+//! exactly as they were.
+
+use async_trait::async_trait;
+
+use tinyagents_harness::context::RunContext;
+use tinyagents_harness::error::Result as TaResult;
+use tinyagents_harness::middleware::{push_ephemeral_instruction, Middleware};
+use tinyinference_llm::model::ModelRequest;
+
+use crate::agent::tinyagents::host::OpenHumanRunContext;
+
+/// Adds the turn's memory pack to each of its model requests.
+pub(crate) struct MemoryPackMiddleware;
+
+#[async_trait]
+impl Middleware<(), OpenHumanRunContext> for MemoryPackMiddleware {
+ fn name(&self) -> &str {
+ "memory_pack"
+ }
+
+ fn is_observer(&self) -> bool {
+ true
+ }
+
+ async fn before_model(
+ &self,
+ ctx: &mut RunContext<OpenHumanRunContext>,
+ _state: &(),
+ request: &mut ModelRequest,
+ ) -> TaResult<()> {
+ let Some(pack) = ctx.data.memory_pack.clone() else {
+ return Ok(());
+ };
+ tracing::trace!(
+ tokens = pack.tokens,
+ refs = pack.refs.len(),
+ "[tinyagents::mw] memory pack added to the model request"
+ );
+ push_ephemeral_instruction(request, pack.injection(), ctx.model_profile.as_ref());
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+#[path = "memory_pack_tests.rs"]
+mod tests;