Skip to content

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

feat(agent): build user turns from typed attachment parts

Steven Enamakelb7acc4b6afb4
diff --git a/crates/openhuman-core/src/agent/message_convert.rs b/crates/openhuman-core/src/agent/message_convert.rs
index b58174eb1b..5c5b9aa778 100644
--- a/crates/openhuman-core/src/agent/message_convert.rs
+++ b/crates/openhuman-core/src/agent/message_convert.rs
@@ -176,6 +176,64 @@ pub(crate) fn user_message_from_text(text: &str) -> Message {
Message::User(UserMessage { content: blocks })
}
+/// Build the user turn from marker-free persisted text and trusted media refs.
+/// Callers must resolve opaque refs against the exact persisted message under
+/// its DispatchCtx before passing parts here; paths on these internal parts are
+/// never accepted from the RPC caller.
+pub(crate) fn user_message_from_parts(
+ text: &str,
+ parts: &[tinychannels_bus::ContentPart],
+) -> Result<Message, String> {
+ let mut content = Vec::new();
+ if !text.is_empty() {
+ content.push(ContentBlock::Text(text.to_string()));
+ }
+ for part in parts {
+ match part {
+ tinychannels_bus::ContentPart::Image {
+ media,
+ caption,
+ alt_text,
+ } => {
+ let path = media
+ .path
+ .clone()
+ .ok_or_else(|| "resolved image attachment has no trusted path".to_string())?;
+ if let Some(caption) = caption.as_ref().filter(|text| !text.is_empty()) {
+ content.push(ContentBlock::Text(caption.clone()));
+ }
+ if let Some(alt) = alt_text.as_ref().filter(|text| !text.is_empty()) {
+ content.push(ContentBlock::Text(alt.clone()));
+ }
+ content.push(ContentBlock::Image(ImageRef {
+ url: path,
+ mime_type: media.content_type.clone(),
+ }));
+ }
+ tinychannels_bus::ContentPart::Audio { media, .. }
+ | tinychannels_bus::ContentPart::Video { media, .. }
+ | tinychannels_bus::ContentPart::File { media, .. } => {
+ let path = media
+ .path
+ .clone()
+ .ok_or_else(|| "resolved media attachment has no trusted path".to_string())?;
+ let media_ref = MediaRef::Path {
+ path,
+ media_type: media.content_type.clone(),
+ };
+ let block = match part {
+ tinychannels_bus::ContentPart::Audio { .. } => ContentBlock::Audio(media_ref),
+ tinychannels_bus::ContentPart::Video { .. } => ContentBlock::Video(media_ref),
+ _ => ContentBlock::Document(media_ref),
+ };
+ content.push(block);
+ }
+ _ => return Err("resolved attachment has an unsupported kind".into()),
+ }
+ }
+ Ok(Message::User(UserMessage { content }))
+}
+
/// The inverse of [`user_message_from_text`]: a user message's text with each
/// image block rendered back as an `[IMAGE:<url>]` marker. A text-only message
/// is exactly [`Message::text`].
diff --git a/crates/openhuman-core/src/threads/ops/crud_message_append_tests.rs b/crates/openhuman-core/src/threads/ops/crud_message_append_tests.rs
index 74e51bedb7..10c7bdf58c 100644
--- a/crates/openhuman-core/src/threads/ops/crud_message_append_tests.rs
+++ b/crates/openhuman-core/src/threads/ops/crud_message_append_tests.rs
@@ -45,6 +45,7 @@ fn request(id: &str, content: &str, sender: &str) -> AppendConversationMessageRe
message: ConversationMessageRecord {
id: id.into(),
content: content.into(),
+ attachments: vec![],
message_type: "text".into(),
extra_metadata: json!({"attachmentNames":["image.png"], "attachmentKinds":["image"],
"attachmentDataUris":["data:image/png;base64,iVBORw0KGgo="], "attachmentData":"bytes",
@@ -350,11 +351,35 @@ async fn typed_attachment_is_staged_like_a_marker_and_leaves_the_text_clean() {
saved.content, "look",
"modern typed image uploads must not persist a staging marker"
);
- let (text, files) = crate::agent::attachments::parse(&saved.content);
- assert_eq!(text.trim(), "look");
- assert_eq!(files.len(), 1);
+ assert_eq!(saved.content, "look");
+ assert_eq!(saved.attachments.len(), 1);
+ assert!(saved.extra_metadata.get("__openhumanStoredAttachments").is_none());
+ let opaque_id = match &saved.attachments[0] {
+ ContentPart::Image { media, .. } => {
+ assert!(media.path.is_none());
+ assert!(media.url.is_none());
+ assert_eq!(media.content_type.as_deref(), Some("image/png"));
+ media.opaque_id.as_deref().expect("opaque attachment id")
+ }
+ other => panic!("expected image attachment, got {other:?}"),
+ };
+ let stored = crate::threads::store::get_messages(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ config.workspace_dir.clone(),
+ "uploads",
+ )
+ .unwrap()
+ .into_iter()
+ .find(|message| message.id == "typed")
+ .unwrap();
+ let entries = stored.extra_metadata["__openhumanStoredAttachments"]
+ .as_array()
+ .unwrap();
+ assert_eq!(entries.len(), 1);
+ assert_eq!(entries[0]["part"]["media"]["opaqueId"], opaque_id);
+ let path = entries[0]["path"].as_str().unwrap();
assert_eq!(
- tokio::fs::read(config.action_dir.join(&files[0].path))
+ tokio::fs::read(config.action_dir.join(path))
.await
.unwrap(),
b"\x89PNG\r\n\x1a\n"
diff --git a/crates/openhuman-core/src/threads/ops_conversion_tests.rs b/crates/openhuman-core/src/threads/ops_conversion_tests.rs
index eff5f7a27b..00aa071ef9 100644
--- a/crates/openhuman-core/src/threads/ops_conversion_tests.rs
+++ b/crates/openhuman-core/src/threads/ops_conversion_tests.rs
@@ -148,6 +148,7 @@ fn record_to_message_preserves_null_extra_metadata() {
let rec = ConversationMessageRecord {
id: "m-2".into(),
content: "x".into(),
+ attachments: vec![],
message_type: "text".into(),
extra_metadata: Value::Null,
sender: "agent".into(),
@@ -193,6 +194,7 @@ async fn message_append_returns_typed_not_found_for_stale_thread() {
message: ConversationMessageRecord {
id: "msg-1".to_string(),
content: "hello".to_string(),
+ attachments: vec![],
message_type: "text".to_string(),
extra_metadata: Value::Null,
sender: "user".to_string(),