Skip to content

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

refactor(session_host): extract typed input enrichment into its own module

Steven Enamakel4430b39de8b8
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session.rs b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
index d4605ccda8..d121ed25a0 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
@@ -7,6 +7,8 @@
#[path = "runtime_session_attachment_input.rs"]
mod attachment_input;
+#[path = "runtime_session_enrichment.rs"]
+mod enrichment;
mod memory_ingest;
mod permanent;
mod post_commit;
@@ -24,7 +26,7 @@ use tinyagents_runtime::{
CommitReceipt, ResumePreparation, SessionBuilder, SessionTerminal, ToolSnapshot,
TranscriptTarget, TurnPreparation,
};
-use tinyinference_llm::message::{ContentBlock, Message, UserMessage};
+use tinyinference_llm::message::Message;
use super::announcement_notes::{
integration_announcement_note, mcp_announcement_note, skill_announcement_note,
@@ -65,41 +67,6 @@ pub(super) struct OpenHumanSessionState {
pending_post_commit: Option<tokio::task::JoinHandle<()>>,
}
-/// Keep host-added context while retaining already-resolved, ordered media
-/// blocks on a trusted typed user input. The string enrichment path remains
-/// authoritative for text-only turns.
-fn enriched_typed_user_input(
- original: &Message,
- original_user_message: &str,
- enriched: &str,
-) -> Result<Option<Message>, ()> {
- let Message::User(user) = original else {
- return Ok(None);
- };
- let has_typed_media = user.content.iter().any(|block| {
- matches!(
- block,
- ContentBlock::Image(_)
- | ContentBlock::Audio(_)
- | ContentBlock::Video(_)
- | ContentBlock::Document(_)
- )
- });
- if !has_typed_media {
- return Ok(None);
- }
- // `enrich_request` only prepends host context and the turn preamble to
- // `original_user_message`. If that invariant changes, fail the turn rather
- // than downgrade trusted typed media into a string.
- let host_prefix = enriched.strip_suffix(original_user_message).ok_or(())?;
- let mut content = Vec::with_capacity(user.content.len() + 1);
- if !host_prefix.is_empty() {
- content.push(ContentBlock::Text(host_prefix.to_owned()));
- }
- content.extend(user.content.iter().cloned());
- Ok(Some(Message::User(UserMessage { content })))
-}
-
/// Owned host-only inputs used by the async runtime preparation hook.
/// It deliberately excludes transcript/history/tool snapshots, which belong to
/// `tinyagents_runtime::Session`.
@@ -899,7 +866,7 @@ impl OpenHumanSessionHost {
)),
);
options.run_context.data.memory_turn = memory_turn;
- request.input = match enriched_typed_user_input(
+ request.input = match enrichment::enriched_typed_user_input(
&original_input,
&original_user_message,
&enriched,
@@ -1064,6 +1031,10 @@ mod parent_snapshot;
#[path = "prelude_integrations.rs"]
mod prelude_integrations;
+#[cfg(test)]
+#[path = "runtime_session_enrichment_tests.rs"]
+mod enrichment_tests;
+
#[cfg(test)]
#[path = "runtime_session_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session_enrichment.rs b/crates/openhuman-core/src/agent/session_host/runtime_session_enrichment.rs
new file mode 100644
index 0000000000..aeb6f9746c
--- /dev/null
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session_enrichment.rs
@@ -0,0 +1,37 @@
+use anyhow::Result;
+use tinyinference_llm::message::{ContentBlock, Message, UserMessage};
+
+/// Keep host-added context while retaining already-resolved, ordered media
+/// blocks on a trusted typed user input. The string enrichment path remains
+/// authoritative for text-only turns.
+pub(super) fn enriched_typed_user_input(
+ original: &Message,
+ original_user_message: &str,
+ enriched: &str,
+) -> Result<Option<Message>, ()> {
+ let Message::User(user) = original else {
+ return Ok(None);
+ };
+ let has_typed_media = user.content.iter().any(|block| {
+ matches!(
+ block,
+ ContentBlock::Image(_)
+ | ContentBlock::Audio(_)
+ | ContentBlock::Video(_)
+ | ContentBlock::Document(_)
+ )
+ });
+ if !has_typed_media {
+ return Ok(None);
+ }
+ // `enrich_request` only prepends host context and the turn preamble to
+ // `original_user_message`. If that invariant changes, fail the turn rather
+ // than downgrade trusted typed media into a string.
+ let host_prefix = enriched.strip_suffix(original_user_message).ok_or(())?;
+ let mut content = Vec::with_capacity(user.content.len() + 1);
+ if !host_prefix.is_empty() {
+ content.push(ContentBlock::Text(host_prefix.to_owned()));
+ }
+ content.extend(user.content.iter().cloned());
+ Ok(Some(Message::User(UserMessage { content })))
+}
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session_enrichment_tests.rs b/crates/openhuman-core/src/agent/session_host/runtime_session_enrichment_tests.rs
new file mode 100644
index 0000000000..7f3c100cbd
--- /dev/null
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session_enrichment_tests.rs
@@ -0,0 +1,73 @@
+use super::enrichment::enriched_typed_user_input;
+
+#[test]
+fn host_enrichment_preserves_ordered_typed_media_once() {
+ use tinyinference_llm::message::{ContentBlock, ImageRef, MediaRef, Message, UserMessage};
+
+ let original = Message::User(UserMessage {
+ content: vec![
+ ContentBlock::Text("[Channel context]".into()),
+ ContentBlock::Text("before image".into()),
+ ContentBlock::Image(ImageRef {
+ url: "openhuman-staged://image".into(),
+ mime_type: Some("image/png".into()),
+ }),
+ ContentBlock::Text("between media".into()),
+ ContentBlock::Document(MediaRef::Path {
+ path: "openhuman-staged://document".into(),
+ media_type: Some("text/plain".into()),
+ }),
+ ContentBlock::Text("after document".into()),
+ ],
+ });
+ let original_text = crate::agent::message_convert::user_text_with_markers(&original);
+ let enriched = format!("turn preamble\n\nhost channel context\n{original_text}");
+ let Message::User(actual) = enriched_typed_user_input(&original, &original_text, &enriched)
+ .expect("prefix is constructed before original text")
+ .expect("typed media is retained")
+ else {
+ panic!("enriched input remains a user message");
+ };
+
+ assert!(
+ matches!(&actual.content[0], ContentBlock::Text(text) if text == "turn preamble\n\nhost channel context\n")
+ );
+ assert!(matches!(&actual.content[1], ContentBlock::Text(text) if text == "[Channel context]"));
+ assert!(matches!(&actual.content[2], ContentBlock::Text(text) if text == "before image"));
+ assert!(
+ matches!(&actual.content[3], ContentBlock::Image(image) if image.url == "openhuman-staged://image")
+ );
+ assert!(matches!(&actual.content[4], ContentBlock::Text(text) if text == "between media"));
+ assert!(
+ matches!(&actual.content[5], ContentBlock::Document(MediaRef::Path { path, .. }) if path == "openhuman-staged://document")
+ );
+ assert!(matches!(&actual.content[6], ContentBlock::Text(text) if text == "after document"));
+ assert_eq!(actual.content.len(), 7, "original blocks are appended once");
+}
+
+#[test]
+fn host_enrichment_fails_closed_if_typed_input_suffix_changes() {
+ use tinyinference_llm::message::{ContentBlock, ImageRef, Message, UserMessage};
+
+ let original = Message::User(UserMessage {
+ content: vec![ContentBlock::Image(ImageRef {
+ url: "openhuman-staged://image".into(),
+ mime_type: Some("image/png".into()),
+ })],
+ });
+ assert!(enriched_typed_user_input(&original, "original", "changed input").is_err());
+}
+
+#[test]
+fn host_enrichment_keeps_text_only_path_unchanged() {
+ use tinyinference_llm::message::{ContentBlock, Message, UserMessage};
+
+ let original = Message::User(UserMessage {
+ content: vec![ContentBlock::Text("hello".into())],
+ });
+ assert!(
+ enriched_typed_user_input(&original, "hello", "prefix\n\nhello")
+ .expect("text-only input has no typed-media transformation")
+ .is_none()
+ );
+}
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session_tests.rs b/crates/openhuman-core/src/agent/session_host/runtime_session_tests.rs
index 25513a7099..6ea2b59540 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session_tests.rs
@@ -5,79 +5,6 @@ use std::sync::Arc;
use tinyagents_runtime::ToolSnapshot;
use tinytools::ToolSpec;
-#[test]
-fn host_enrichment_preserves_ordered_typed_media_once() {
- use tinyinference_llm::message::{ContentBlock, ImageRef, MediaRef, Message, UserMessage};
-
- let original = Message::User(UserMessage {
- content: vec![
- ContentBlock::Text("[Channel context]".into()),
- ContentBlock::Text("before image".into()),
- ContentBlock::Image(ImageRef {
- url: "openhuman-staged://image".into(),
- mime_type: Some("image/png".into()),
- }),
- ContentBlock::Text("between media".into()),
- ContentBlock::Document(MediaRef::Path {
- path: "openhuman-staged://document".into(),
- media_type: Some("text/plain".into()),
- }),
- ContentBlock::Text("after document".into()),
- ],
- });
- let original_text = crate::agent::message_convert::user_text_with_markers(&original);
- let enriched = format!("turn preamble\n\nhost channel context\n{original_text}");
- let Message::User(actual) =
- super::enriched_typed_user_input(&original, &original_text, &enriched)
- .expect("prefix is constructed before original text")
- .expect("typed media is retained")
- else {
- panic!("enriched input remains a user message");
- };
-
- assert!(
- matches!(&actual.content[0], ContentBlock::Text(text) if text == "turn preamble\n\nhost channel context\n")
- );
- assert!(matches!(&actual.content[1], ContentBlock::Text(text) if text == "[Channel context]"));
- assert!(matches!(&actual.content[2], ContentBlock::Text(text) if text == "before image"));
- assert!(
- matches!(&actual.content[3], ContentBlock::Image(image) if image.url == "openhuman-staged://image")
- );
- assert!(matches!(&actual.content[4], ContentBlock::Text(text) if text == "between media"));
- assert!(
- matches!(&actual.content[5], ContentBlock::Document(MediaRef::Path { path, .. }) if path == "openhuman-staged://document")
- );
- assert!(matches!(&actual.content[6], ContentBlock::Text(text) if text == "after document"));
- assert_eq!(actual.content.len(), 7, "original blocks are appended once");
-}
-
-#[test]
-fn host_enrichment_fails_closed_if_typed_input_suffix_changes() {
- use tinyinference_llm::message::{ContentBlock, ImageRef, Message, UserMessage};
-
- let original = Message::User(UserMessage {
- content: vec![ContentBlock::Image(ImageRef {
- url: "openhuman-staged://image".into(),
- mime_type: Some("image/png".into()),
- })],
- });
- assert!(super::enriched_typed_user_input(&original, "original", "changed input").is_err());
-}
-
-#[test]
-fn host_enrichment_keeps_text_only_path_unchanged() {
- use tinyinference_llm::message::{ContentBlock, Message, UserMessage};
-
- let original = Message::User(UserMessage {
- content: vec![ContentBlock::Text("hello".into())],
- });
- assert!(
- super::enriched_typed_user_input(&original, "hello", "prefix\n\nhello")
- .expect("text-only input has no typed-media transformation")
- .is_none()
- );
-}
-
#[tokio::test]
async fn committed_progress_uses_each_turns_receipt_sender() {
use tinyagents_harness::progress::AgentProgress;