Skip to content

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

refactor(threads): simplify attachment resolution validation

Steven Enamakel9fa6cf51e6c6
diff --git a/crates/openhuman-core/src/threads/attachments.rs b/crates/openhuman-core/src/threads/attachments.rs
index c8469c63c7..afeb4de9c4 100644
--- a/crates/openhuman-core/src/threads/attachments.rs
+++ b/crates/openhuman-core/src/threads/attachments.rs
@@ -582,50 +582,12 @@ pub(crate) async fn resolve_persisted_parts(
return Err("stored attachment refs do not match the persisted user row".into());
}
- let image_count = entries
- .iter()
- .filter(|entry| matches!(entry.part, ContentPart::Image { .. }))
- .count();
- let file_count = entries.len() - image_count;
- let (max_images, image_mb) = config.multimodal.effective_limits();
- let (max_files, file_mb, _) = config.multimodal_files.effective_limits();
- if (image_count > 0 && (config.multimodal.max_images == 0 || image_count > max_images))
- || (entries.len() > 0 && config.multimodal_files.max_files == 0)
- || file_count > max_files
- {
- return Err("stored attachment count exceeds configured limit".into());
- }
+ validate_staged_files(ctx, thread_id, &config, &entries)
+ .await
+ .map_err(|error| error.replace("staged", "stored"))?;
let mut resolved = Vec::with_capacity(entries.len());
for mut entry in entries {
- let scope = crate::agent::attachments::AttachmentAccessScope {
- external_channel: false,
- workspace: Some(workspace.clone()),
- };
- let path = crate::agent::attachments::resolve_path(&config, &entry.path, &scope)
- .await
- .map_err(|error| format!("stored attachment is unavailable: {error}"))?;
- let metadata = tokio::fs::metadata(&path)
- .await
- .map_err(|error| format!("stored attachment is unavailable: {error}"))?;
- if !metadata.is_file() {
- return Err("stored attachment is not a regular file".into());
- }
- let media = opaque_media(&entry.part)
- .ok_or_else(|| "stored attachment has an unsupported kind".to_string())?;
- let is_image = matches!(entry.part, ContentPart::Image { .. });
- let mime = media
- .content_type
- .as_deref()
- .filter(|mime| !mime.is_empty())
- .ok_or_else(|| "stored attachment MIME type is missing".to_string())?;
- if is_image && !mime.starts_with("image/") {
- return Err("stored image attachment has an invalid MIME type".into());
- }
- let cap = if is_image { image_mb } else { file_mb } * 1024 * 1024;
- if metadata.len() > cap as u64 {
- return Err("stored attachment exceeds configured file limit".into());
- }
let media = opaque_media_mut(&mut entry.part)
.ok_or_else(|| "stored attachment has an unsupported kind".to_string())?;
media.path = Some(entry.path);