Skip to content

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

refactor(agent): improve message conversion and validation logic

Steven Enamakel07bc387e606d
diff --git a/crates/openhuman-core/src/agent/message_convert.rs b/crates/openhuman-core/src/agent/message_convert.rs
index f69c800e77..c187ebdcbc 100644
--- a/crates/openhuman-core/src/agent/message_convert.rs
+++ b/crates/openhuman-core/src/agent/message_convert.rs
@@ -240,9 +240,21 @@ pub(crate) fn user_message_from_parts(
.ok_or_else(|| "voice transcript is unavailable".to_owned())?;
content.push(ContentBlock::Text(transcript.to_owned()));
}
- tinychannels_bus::ContentPart::Audio { .. }
- | tinychannels_bus::ContentPart::Video { .. } => {
- return Err("resolved audio/video attachment is not supported".into());
+ tinychannels_bus::ContentPart::Audio { media, .. }
+ | tinychannels_bus::ContentPart::Video { 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),
+ _ => ContentBlock::Video(media_ref),
+ };
+ content.push(block);
}
tinychannels_bus::ContentPart::Sticker { .. }
| tinychannels_bus::ContentPart::Location { .. }
diff --git a/crates/openhuman-core/src/agent/message_convert_tests.rs b/crates/openhuman-core/src/agent/message_convert_tests.rs
index 3d0c9d5140..437991fe22 100644
--- a/crates/openhuman-core/src/agent/message_convert_tests.rs
+++ b/crates/openhuman-core/src/agent/message_convert_tests.rs
@@ -78,7 +78,7 @@ fn typed_native_parts_preserve_text_voice_transcript_and_file_order() {
}
#[test]
-fn typed_native_parts_reject_unavailable_voice_and_unsupported_audio_video() {
+fn typed_native_parts_reject_unavailable_voice_transcripts() {
let incomplete = tinychannels_bus::ContentPart::VoiceNote {
voice_note: tinychannels_bus::VoiceNote {
transcript: Some(tinychannels_bus::VoiceTranscript {
@@ -91,23 +91,6 @@ fn typed_native_parts_reject_unavailable_voice_and_unsupported_audio_video() {
},
};
assert!(user_message_from_parts("", &[incomplete]).is_err());
-
- let audio = tinychannels_bus::ContentPart::Audio {
- media: tinychannels_bus::MediaReference::default(),
- codec: None,
- duration_ms: None,
- title: None,
- transcript: None,
- };
- assert!(user_message_from_parts("", &[audio]).is_err());
-
- let video = tinychannels_bus::ContentPart::Video {
- media: tinychannels_bus::MediaReference::default(),
- caption: None,
- duration_ms: None,
- thumbnail: None,
- };
- assert!(user_message_from_parts("", &[video]).is_err());
}
#[test]
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor/native_media.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor/native_media.rs
index a7a252e868..fa4f7ca6a1 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/native_media.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/native_media.rs
@@ -37,6 +37,7 @@ pub(super) async fn resolve_typed_message(
if !requires_typed_preparation(envelope) {
return Ok(None);
}
+ validate_native_parts(&envelope.parts)?;
let account = envelope
.channel
.account_id
@@ -99,17 +100,9 @@ pub(super) async fn resolve_typed_message(
return Err("native audio/video attachments are not supported".into());
}
ContentPart::VoiceNote { voice_note } => {
- let transcript = voice_note
- .transcript
- .as_ref()
- .filter(|transcript| {
- transcript.state == tinychannels_bus::VoiceTranscriptState::Complete
- })
- .map(|transcript| transcript.text.trim())
- .filter(|text| !text.is_empty())
- .ok_or_else(|| "native voice transcript is unavailable".to_owned())?;
+ let transcript = voice_note.transcript.as_ref().expect("validated transcript");
let replacement = ContentPart::Text {
- text: transcript.to_owned(),
+ text: transcript.text.trim().to_owned(),
markdown_dialect: None,
};
*part = replacement;
@@ -174,6 +167,32 @@ pub(super) async fn resolve_typed_message(
crate::agent::message_convert::user_message_from_parts(&text, &parts).map(Some)
}
+fn validate_native_parts(parts: &[ContentPart]) -> Result<(), String> {
+ for part in parts {
+ match part {
+ ContentPart::Text { .. } | ContentPart::Image { .. } | ContentPart::File { .. } => {}
+ ContentPart::VoiceNote { voice_note } => {
+ let transcript = voice_note
+ .transcript
+ .as_ref()
+ .filter(|transcript| {
+ transcript.state == tinychannels_bus::VoiceTranscriptState::Complete
+ })
+ .map(|transcript| transcript.text.trim())
+ .filter(|text| !text.is_empty());
+ if transcript.is_none() {
+ return Err("native voice transcript is unavailable".into());
+ }
+ }
+ ContentPart::Audio { .. } | ContentPart::Video { .. } => {
+ return Err("native audio/video attachments are not supported".into());
+ }
+ _ => return Err("native channel content kind is not supported".into()),
+ }
+ }
+ Ok(())
+}
+
fn validate_reference(
media: &MediaReference,
image: bool,
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor/native_media_tests.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor/native_media_tests.rs
index 14ca7e5be6..b3a11b4703 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/native_media_tests.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/native_media_tests.rs
@@ -66,3 +66,36 @@ fn typed_native_media_is_selected_only_from_envelope_parts() {
};
assert!(!requires_typed_preparation(&text_only));
}
+
+#[test]
+fn native_audio_video_and_incomplete_voice_are_rejected_before_media_reads() {
+ let audio = ContentPart::Audio {
+ media: reference(),
+ codec: None,
+ duration_ms: None,
+ title: None,
+ transcript: None,
+ };
+ assert!(validate_native_parts(&[audio]).is_err());
+
+ let video = ContentPart::Video {
+ media: reference(),
+ caption: None,
+ duration_ms: None,
+ thumbnail: None,
+ };
+ assert!(validate_native_parts(&[video]).is_err());
+
+ let pending_voice = ContentPart::VoiceNote {
+ voice_note: tinychannels_bus::VoiceNote {
+ transcript: Some(tinychannels_bus::VoiceTranscript {
+ text: "partial".into(),
+ language: None,
+ state: tinychannels_bus::VoiceTranscriptState::Pending,
+ source: tinychannels_bus::VoiceTranscriptSource::HostStt,
+ }),
+ ..Default::default()
+ },
+ };
+ assert!(validate_native_parts(&[pending_voice]).is_err());
+}