Skip to content

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

feat(agent): add attachment parts module

Steven Enamakel3acaf414e5da
diff --git a/crates/openhuman-core/src/agent/attachments/mod.rs b/crates/openhuman-core/src/agent/attachments/mod.rs
index f306a3775b..ab1366c6b2 100644
--- a/crates/openhuman-core/src/agent/attachments/mod.rs
+++ b/crates/openhuman-core/src/agent/attachments/mod.rs
@@ -14,6 +14,7 @@ use crate::security::SecurityPolicy;
pub(crate) mod host;
pub(crate) mod legacy;
+pub(crate) mod parts;
pub(crate) mod provider;
/// Per-model-call attachment authority copied from the live agent context.
diff --git a/crates/openhuman-core/src/agent/attachments/parts.rs b/crates/openhuman-core/src/agent/attachments/parts.rs
new file mode 100644
index 0000000000..c1638f92fa
--- /dev/null
+++ b/crates/openhuman-core/src/agent/attachments/parts.rs
@@ -0,0 +1,121 @@
+//! Typed attachments on a chat request.
+//!
+//! The web chat RPC (`channel.web_chat`), `threads.message_append` and
+//! `threads.edit_message` take attachments as typed
+//! [`tinychannels_bus::ContentPart`]s next to the message text, instead of
+//! `[IMAGE:…]` / `[FILE:…]` markers a client has to splice into it. The marker
+//! grammar stays an internal staging format: [`fold_into_message`] renders the
+//! parts into it once, at the RPC edge, and everything downstream (staging,
+//! limits, conversion, the durable `[ATTACHMENT:…]` references) is unchanged.
+//!
+//! Only parts that carry their bytes inline (`data:` URI) or by remote URL are
+//! accepted. A `path` or `localCachePath` is refused so the typed field cannot
+//! be used to name a local file the way a forged marker could.
+
+use tinychannels_bus::{ContentPart, MediaReference};
+
+const MAX_PARTS: usize = 32;
+
+/// Why a typed attachment could not be accepted.
+#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
+pub enum AttachmentPartError {
+ #[error("too many attachments ({count}, max {MAX_PARTS})")]
+ TooMany { count: usize },
+ #[error("attachment {index}: {kind} parts are not supported as chat attachments")]
+ UnsupportedKind { index: usize, kind: &'static str },
+ #[error("attachment {index}: media needs an inline data: URI or an http(s) url")]
+ NoSource { index: usize },
+ #[error("attachment {index}: local paths are not accepted")]
+ LocalPath { index: usize },
+ #[error("attachment {index}: url must not contain whitespace or ']'")]
+ BadUrl { index: usize },
+}
+
+fn kind_name(part: &ContentPart) -> &'static str {
+ match part {
+ ContentPart::Text { .. } => "text",
+ ContentPart::Image { .. } => "image",
+ ContentPart::Video { .. } => "video",
+ ContentPart::Audio { .. } => "audio",
+ ContentPart::File { .. } => "file",
+ ContentPart::VoiceNote { .. } => "voice_note",
+ ContentPart::Sticker { .. } => "sticker",
+ ContentPart::Location { .. } => "location",
+ ContentPart::Contact { .. } => "contact",
+ ContentPart::Buttons { .. } => "buttons",
+ ContentPart::Poll { .. } => "poll",
+ ContentPart::Presentation { .. } => "presentation",
+ ContentPart::Native { .. } => "native",
+ }
+}
+
+fn source_of(media: &MediaReference, index: usize) -> Result<&str, AttachmentPartError> {
+ if media.path.is_some() || media.local_cache_path.is_some() {
+ return Err(AttachmentPartError::LocalPath { index });
+ }
+ let url = media
+ .url
+ .as_deref()
+ .ok_or(AttachmentPartError::NoSource { index })?;
+ let remote = url.starts_with("https://") || url.starts_with("http://");
+ if !url.starts_with("data:") && !remote {
+ return Err(AttachmentPartError::NoSource { index });
+ }
+ if url.contains(']') || url.chars().any(char::is_whitespace) {
+ return Err(AttachmentPartError::BadUrl { index });
+ }
+ Ok(url)
+}
+
+/// Render `parts` after `text` as the staging markers, in order.
+///
+/// `Text` parts join the message text. Images become `[IMAGE:…]`; video,
+/// audio and files become `[FILE:…]`. Any other part kind is refused.
+pub fn fold_into_message(
+ text: &str,
+ parts: &[ContentPart],
+) -> Result<String, AttachmentPartError> {
+ if parts.is_empty() {
+ return Ok(text.to_string());
+ }
+ if parts.len() > MAX_PARTS {
+ return Err(AttachmentPartError::TooMany { count: parts.len() });
+ }
+ let mut words: Vec<String> = Vec::new();
+ let mut markers: Vec<String> = Vec::new();
+ for (index, part) in parts.iter().enumerate() {
+ match part {
+ ContentPart::Text { text, .. } => words.push(text.trim().to_string()),
+ ContentPart::Image { media, .. } => {
+ markers.push(format!("[IMAGE:{}]", source_of(media, index)?));
+ }
+ ContentPart::Video { media, .. }
+ | ContentPart::Audio { media, .. }
+ | ContentPart::File { media, .. } => {
+ markers.push(format!("[FILE:{}]", source_of(media, index)?));
+ }
+ other => {
+ return Err(AttachmentPartError::UnsupportedKind {
+ index,
+ kind: kind_name(other),
+ })
+ }
+ }
+ }
+ let mut out = text.trim().to_string();
+ for chunk in words.into_iter().filter(|chunk| !chunk.is_empty()).chain(markers) {
+ if !out.is_empty() {
+ out.push(' ');
+ }
+ out.push_str(&chunk);
+ }
+ tracing::debug!(
+ parts = parts.len(),
+ "[attachments] folded typed attachments into message"
+ );
+ Ok(out)
+}
+
+#[cfg(test)]
+#[path = "parts_tests.rs"]
+mod tests;