Skip to content

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

refactor(media): update media artifact copying logic

Steven Enamakel7657bf0247f9
diff --git a/crates/openhuman-core/src/media/generation/artifact_tool.rs b/crates/openhuman-core/src/media/generation/artifact_tool.rs
index a478c7c7a7..afe3dc536e 100644
--- a/crates/openhuman-core/src/media/generation/artifact_tool.rs
+++ b/crates/openhuman-core/src/media/generation/artifact_tool.rs
@@ -29,6 +29,7 @@ use std::path::{Path, PathBuf};
use std::sync::Arc;
use async_trait::async_trait;
+use futures_util::StreamExt;
use serde_json::{json, Value};
use tinytools::{
PermissionLevel, Tool, ToolCallOptions, ToolCategory, ToolContent, ToolPolicy, ToolResult,
@@ -417,14 +418,15 @@ impl<T: Tool> MediaArtifactTool<T> {
.await
.map_err(|error| format!("failed to read generated media blob {key:?}: {error}"))?
.ok_or_else(|| format!("generated media blob {key:?} was not found"))?;
- copy_blob_to_file(&mut *blob.reader, dest, key).await
+ copy_blob_to_file(&mut blob.reader, dest, key).await
}
}
-async fn copy_blob_to_file<R>(reader: &mut R, dest: &Path, key: &str) -> Result<u64, String>
-where
- R: tokio::io::AsyncRead + Unpin + ?Sized,
-{
+async fn copy_blob_to_file(
+ reader: &mut tinyagents_storage_kit::BlobReader,
+ dest: &Path,
+ key: &str,
+) -> Result<u64, String> {
let mut file = tokio::fs::OpenOptions::new()
.write(true)
.truncate(true)
@@ -436,16 +438,27 @@ where
dest.display()
)
})?;
- let size = match tokio::io::copy(reader, &mut file).await {
- Ok(size) => size,
- Err(error) => {
+ let mut size = 0_u64;
+ while let Some(chunk) = reader.next().await {
+ let chunk = match chunk {
+ Ok(chunk) => chunk,
+ Err(error) => {
+ drop(file);
+ let _ = tokio::fs::remove_file(dest).await;
+ return Err(format!(
+ "failed to copy generated media blob {key:?}: {error}"
+ ));
+ }
+ };
+ if let Err(error) = file.write_all(&chunk).await {
drop(file);
let _ = tokio::fs::remove_file(dest).await;
return Err(format!(
- "failed to copy generated media blob {key:?}: {error}"
+ "failed to write generated media blob {key:?}: {error}"
));
}
- };
+ size = size.saturating_add(chunk.len() as u64);
+ }
if let Err(error) = file.flush().await {
drop(file);
let _ = tokio::fs::remove_file(dest).await;
diff --git a/crates/openhuman-core/src/media/generation/tools.rs b/crates/openhuman-core/src/media/generation/tools.rs
index fc5397782a..d8209dfd3b 100644
--- a/crates/openhuman-core/src/media/generation/tools.rs
+++ b/crates/openhuman-core/src/media/generation/tools.rs
@@ -104,7 +104,11 @@ fn media_tools_from_with_allowlist(
) -> Vec<Box<dyn Tool>> {
let files = files_dir.into();
let generators = Arc::new(generators);
- let output = MediaOutput::new(action_dir).with_reference_policy(reference_policy(
+ let Some(blobs) = crate::storage::sessions::artifact_blobs_for(workspace_dir) else {
+ log::warn!("[media] scoped blob storage unavailable; media tools are not registered");
+ return Vec::new();
+ };
+ let output = MediaOutput::new(action_dir, blobs).with_reference_policy(reference_policy(
action_dir,
workspace_dir,
Arc::clone(&allowed_artifacts),