Skip to content

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

feat(media): accept closures for per-call inner tool builders

Steven Enamakelb27bb224f562
diff --git a/crates/openhuman-core/src/media/generation/artifact_tool.rs b/crates/openhuman-core/src/media/generation/artifact_tool.rs
index dd63462a1c..6417393fdd 100644
--- a/crates/openhuman-core/src/media/generation/artifact_tool.rs
+++ b/crates/openhuman-core/src/media/generation/artifact_tool.rs
@@ -10,8 +10,7 @@ use std::sync::{Arc, RwLock};
use async_trait::async_trait;
use serde_json::{json, Value};
use tinyagents_harness_tools::media::{
- ArtifactFiling, FiledArtifact, GeneratedArtifact, MediaArtifactKind,
- MediaInnerFactory,
+ ArtifactFiling, FiledArtifact, GeneratedArtifact, MediaArtifactKind, MediaInnerFactory,
};
use tinytools::{
PermissionLevel, Tool, ToolCallOptions, ToolCategory, ToolPolicy, ToolResult, ToolRunContext,
@@ -95,8 +94,15 @@ impl<T: Tool> MediaArtifactTool<T> {
/// Builds the inner generator for one call around its provider progress
/// observer and the call's own host context.
#[must_use]
- pub fn with_per_call_inner(self, build: MediaInnerFactory<T>) -> Self {
+ pub fn with_per_call_inner(
+ self,
+ build: impl for<'a> Fn(MediaProgressFn, Option<&'a dyn ToolRunContext>) -> T
+ + Send
+ + Sync
+ + 'static,
+ ) -> Self {
let Self { inner, policy } = self;
+ let build: MediaInnerFactory<T> = Arc::new(build);
let inner = inner.with_per_call_inner(Arc::new(move |progress, context| {
ReferenceRefreshTool::new(build(progress, context), Arc::clone(&policy))
}));
@@ -177,7 +183,9 @@ impl<T: Tool> Tool for MediaArtifactTool<T> {
options: ToolCallOptions,
context: Option<&dyn ToolRunContext>,
) -> anyhow::Result<ToolResult> {
- self.inner.execute_with_context(args, options, context).await
+ self.inner
+ .execute_with_context(args, options, context)
+ .await
}
}
@@ -269,11 +277,7 @@ impl ArtifactFiling for OpenHumanArtifactFiling {
}
impl OpenHumanArtifactFiling {
- async fn delete_blob_after_finalize(
- &self,
- policy: &MediaArtifactPolicy,
- source: &Path,
- ) {
+ async fn delete_blob_after_finalize(&self, policy: &MediaArtifactPolicy, source: &Path) {
if source.is_absolute() {
return;
}
@@ -445,10 +449,15 @@ impl<T: Tool> Tool for ReferenceRefreshTool<T> {
}
async fn execute(&self, args: Value) -> anyhow::Result<ToolResult> {
- self.execute_with_context(args, ToolCallOptions::default(), None).await
+ self.execute_with_context(args, ToolCallOptions::default(), None)
+ .await
}
- async fn execute_with_options(&self, args: Value, options: ToolCallOptions) -> anyhow::Result<ToolResult> {
+ async fn execute_with_options(
+ &self,
+ args: Value,
+ options: ToolCallOptions,
+ ) -> anyhow::Result<ToolResult> {
self.execute_with_context(args, options, None).await
}
@@ -466,9 +475,13 @@ impl<T: Tool> Tool for ReferenceRefreshTool<T> {
.unwrap_or_else(std::sync::PoisonError::into_inner)
.files_dir,
) {
- self.refresh_allowed_references().await.map_err(anyhow::Error::msg)?;
+ self.refresh_allowed_references()
+ .await
+ .map_err(anyhow::Error::msg)?;
}
- self.inner.execute_with_context(args, options, context).await
+ self.inner
+ .execute_with_context(args, options, context)
+ .await
}
}
@@ -486,9 +499,15 @@ const FILE_REFERENCE_FIELDS: [&str; 8] = [
/// Whether a media call references an absolute path under a trusted Files root.
fn has_reusable_files_reference(args: &Value, files: &crate::artifacts::FileRoots) -> bool {
FILE_REFERENCE_FIELDS.iter().any(|field| {
- let Some(value) = args.get(field) else { return false; };
+ let Some(value) = args.get(field) else {
+ return false;
+ };
let candidates = value.as_str().into_iter().chain(
- value.as_array().into_iter().flatten().filter_map(Value::as_str),
+ value
+ .as_array()
+ .into_iter()
+ .flatten()
+ .filter_map(Value::as_str),
);
candidates.into_iter().any(|candidate| {
let path = Path::new(candidate);
@@ -506,19 +525,29 @@ where
.truncate(true)
.open(destination)
.await
- .map_err(|error| format!("failed to open artifact destination {}: {error}", destination.display()))?;
+ .map_err(|error| {
+ format!(
+ "failed to open artifact destination {}: {error}",
+ destination.display()
+ )
+ })?;
let size = match tokio::io::copy(reader, &mut file).await {
Ok(size) => size,
Err(error) => {
drop(file);
let _ = tokio::fs::remove_file(destination).await;
- return Err(format!("failed to copy generated media blob {key:?}: {error}"));
+ return Err(format!(
+ "failed to copy generated media blob {key:?}: {error}"
+ ));
}
};
if let Err(error) = file.flush().await {
drop(file);
let _ = tokio::fs::remove_file(destination).await;
- return Err(format!("failed to flush artifact destination {}: {error}", destination.display()));
+ return Err(format!(
+ "failed to flush artifact destination {}: {error}",
+ destination.display()
+ ));
}
Ok(size)
}
@@ -526,9 +555,15 @@ where
/// Moves a generated file into the Files folder, copying across mounts.
async fn move_or_copy(source: &Path, destination: &Path) -> Result<u64, String> {
if tokio::fs::rename(source, destination).await.is_err() {
- tokio::fs::copy(source, destination).await.map_err(|error| {
- format!("failed to copy {} -> {}: {error}", source.display(), destination.display())
- })?;
+ tokio::fs::copy(source, destination)
+ .await
+ .map_err(|error| {
+ format!(
+ "failed to copy {} -> {}: {error}",
+ source.display(),
+ destination.display()
+ )
+ })?;
let _ = tokio::fs::remove_file(source).await;
}
let stat = tokio::fs::metadata(destination)
diff --git a/crates/openhuman-core/src/media/generation/artifact_tool_tests.rs b/crates/openhuman-core/src/media/generation/artifact_tool_tests.rs
index 919b67df3c..633a29da3f 100644
--- a/crates/openhuman-core/src/media/generation/artifact_tool_tests.rs
+++ b/crates/openhuman-core/src/media/generation/artifact_tool_tests.rs
@@ -3,6 +3,7 @@ use serde_json::{json, Value};
use std::path::Path;
use std::sync::Arc;
use tinyagents_harness::media::MediaOutput;
+use tinyagents_harness_tools::media::MediaJobProgress;
use tinystoragedrivers::BlobStore;
use tinytools::{PermissionLevel, Tool, ToolCategory, ToolContent, ToolResult};
@@ -548,9 +549,23 @@ async fn per_call_observer_reports_each_calls_own_job_state() {
root.path().join("workspace"),
root.path().join("Files"),
)
- .with_per_call_inner(move |observer, _chat| PollingTool {
- observer,
- state: states.lock().unwrap().pop().unwrap(),
+ .with_per_call_inner(move |progress, _context| {
+ let observer = std::sync::Arc::new(
+ move |status: &tinyagents_harness::tinyinference_video::VideoJobStatus| {
+ progress(&MediaJobProgress {
+ id: status.id.clone(),
+ state: status.state.to_string(),
+ outputs: status.outputs,
+ output_indices: status.output_indices.clone(),
+ cost_usd: status.cost_usd,
+ error: status.error.clone(),
+ });
+ },
+ );
+ PollingTool {
+ observer,
+ state: states.lock().unwrap().pop().unwrap(),
+ }
});
let (a, b) = (Recorder::default(), Recorder::default());
let (ra, rb) = tokio::join!(
diff --git a/crates/openhuman-core/src/media/generation/tools.rs b/crates/openhuman-core/src/media/generation/tools.rs
index 807b1fa25e..3bfa77e01b 100644
--- a/crates/openhuman-core/src/media/generation/tools.rs
+++ b/crates/openhuman-core/src/media/generation/tools.rs
@@ -161,16 +161,18 @@ fn media_tools_from_with_allowlist(
let approval =
crate::agent::tinyagents::host::run_context::approval_for_tool(context);
let routed = generators.routed(approval.chat.as_ref());
- let observer = Arc::new(move |status: &tinyagents_harness::tinyinference_video::VideoJobStatus| {
- progress(&MediaJobProgress {
- id: status.id.clone(),
- state: status.state.to_string(),
- outputs: status.outputs,
- output_indices: status.output_indices.clone(),
- cost_usd: status.cost_usd,
- error: status.error.clone(),
- });
- });
+ let observer = Arc::new(
+ move |status: &tinyagents_harness::tinyinference_video::VideoJobStatus| {
+ progress(&MediaJobProgress {
+ id: status.id.clone(),
+ state: status.state.to_string(),
+ outputs: status.outputs,
+ output_indices: status.output_indices.clone(),
+ cost_usd: status.cost_usd,
+ error: status.error.clone(),
+ });
+ },
+ );
video_tool(
&routed.video,
&output,