Skip to content

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

feat(attachments): support reading staged native attachments

Steven Enamakeld70f2727333c
diff --git a/crates/openhuman-core/src/agent/attachments/provider_source.rs b/crates/openhuman-core/src/agent/attachments/provider_source.rs
index 588a4209ef..52e8925ba2 100644
--- a/crates/openhuman-core/src/agent/attachments/provider_source.rs
+++ b/crates/openhuman-core/src/agent/attachments/provider_source.rs
@@ -27,6 +27,33 @@ impl AttachmentModel {
ContentBlock::Document(media) => Self::media_source(InputModality::Document, media)?,
_ => return Ok(None),
};
+ if let Some(opaque_id) = source.strip_prefix("openhuman-staged://") {
+ let thread_id = scope.thread_id.as_deref().ok_or_else(|| {
+ tinyinference_llm::Error::Model("native attachment session is unavailable".into())
+ })?;
+ let resolved = crate::threads::attachments::read_authorized_native_attachment(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &self.config,
+ thread_id,
+ opaque_id,
+ &scope.native_staged_parts,
+ )
+ .await
+ .map_err(tinyinference_llm::Error::Model)?;
+ let expected_image = modality == InputModality::Image;
+ if expected_image != resolved.mime.starts_with("image/") {
+ return Err(tinyinference_llm::Error::Model(
+ "native attachment type does not match its staged record".into(),
+ ));
+ }
+ return Ok(Some((
+ modality,
+ resolved.path,
+ resolved.mime,
+ resolved.bytes,
+ false,
+ )));
+ }
if self.config.multimodal_files.max_files == 0 {
return Err(tinyinference_llm::Error::Model(
"attachments are disabled for this origin".into(),
diff --git a/crates/openhuman-core/src/threads/attachments/staged.rs b/crates/openhuman-core/src/threads/attachments/staged.rs
index e217c5b2d6..4499041e56 100644
--- a/crates/openhuman-core/src/threads/attachments/staged.rs
+++ b/crates/openhuman-core/src/threads/attachments/staged.rs
@@ -338,39 +338,48 @@ pub(crate) async fn stage_native_channel_part(
)
.await
.map_err(|_| "native attachment could not be staged safely".to_owned())?;
- let mut part = input.clone();
- let media = opaque_media_mut(&mut part)
- .ok_or_else(|| "native attachment kind cannot be staged".to_owned())?;
- let opaque_id = uuid::Uuid::new_v4().simple().to_string();
- media.opaque_id = Some(opaque_id.clone());
- media.path = None;
- media.url = None;
- media.local_cache_path = None;
- media.content_type = Some(file.mime.clone());
- media.size_bytes = Some(file.size_bytes as u64);
+ let file_path = file.path.clone();
+ let result = async {
+ let mut part = input.clone();
+ let media = opaque_media_mut(&mut part)
+ .ok_or_else(|| "native attachment kind cannot be staged".to_owned())?;
+ let opaque_id = uuid::Uuid::new_v4().simple().to_string();
+ media.opaque_id = Some(opaque_id.clone());
+ media.path = None;
+ media.url = None;
+ media.local_cache_path = None;
+ media.content_type = Some(file.mime.clone());
+ media.size_bytes = Some(file.size_bytes as u64);
- let tenant = tenant_for(ctx)?;
- let repo = staged_repo(ctx, &config.workspace_dir)?;
- let id = staged_doc_id(&opaque_id)?;
- let manifest = StagedAttachment {
- tenant,
- thread_id: thread_id.to_owned(),
- attachment: StoredAttachment {
- part: part.clone(),
- path: file.path,
- },
- expires_at: chrono::Utc::now().timestamp() + STAGED_TTL_SECS,
- claim: None,
- };
- let body = serde_json::to_value(&manifest)
- .map_err(|error| format!("serialize native attachment record: {error}"))?;
- repo.run(|docs| async move {
- docs.put(STAGED_COLLECTION, &id, body, Precondition::Absent)
- .await?;
- Ok(())
- })
- .map_err(|error| format!("persist native attachment record: {error}"))?;
- Ok(part)
+ let tenant = tenant_for(ctx)?;
+ let repo = staged_repo(ctx, &config.workspace_dir)?;
+ let id = staged_doc_id(&opaque_id)?;
+ let manifest = StagedAttachment {
+ tenant,
+ thread_id: thread_id.to_owned(),
+ attachment: StoredAttachment {
+ part: part.clone(),
+ path: file.path,
+ },
+ expires_at: chrono::Utc::now().timestamp() + STAGED_TTL_SECS,
+ claim: None,
+ };
+ let body = serde_json::to_value(&manifest)
+ .map_err(|error| format!("serialize native attachment record: {error}"))?;
+ repo.run(|docs| async move {
+ docs.put(STAGED_COLLECTION, &id, body, Precondition::Absent)
+ .await?;
+ Ok(())
+ })
+ .map_err(|error| format!("persist native attachment record: {error}"))?;
+ Ok(part)
+ }
+ .await;
+ if result.is_err() {
+ let path = staged_config.action_dir.join(file_path);
+ let _ = tokio::fs::remove_file(path).await;
+ }
+ result
}
pub(crate) async fn commit_native_channel_parts(