use crate::agent::find_agent;
use crate::run::{RunBaseOptions, RunSubAgentParams, run_agent};
use crate::types::RunAgentResponse;
use crate::{Error, Result};
pub async fn exec_run_sub_agent(params: RunSubAgentParams) -> Result<()> {
let RunSubAgentParams {
runtime,
parent_uid,
agent_dir,
agent_name,
inputs,
agent_options,
response_shot,
} = params;
let result: Result<RunAgentResponse> = (async || {
let agent = find_agent(&agent_name, &runtime, agent_dir.as_ref())
.map_err(|e| Error::custom(format!("Failed to find agent '{agent_name}': {e}")))?;
let agent = match agent_options {
Some(agent_options) => agent.new_merge(agent_options)?,
None => agent,
};
let run_base_options = RunBaseOptions::default();
let res = run_agent(&runtime, Some(parent_uid), agent, inputs, &run_base_options, true)
.await
.map_err(|e| Error::custom(format!("Failed to run agent '{agent_name}': {e}")))?;
Ok(res)
})()
.await;
match response_shot {
Some(response_shot) => {
match result {
Ok(result) => {
if let Err(err) = response_shot.send(Ok(result)).await {
return Err(Error::custom(format!(
"Failed to send response to agent '{agent_name}': {err}"
)));
}
}
Err(err) => {
if let Err(err) = response_shot.send(Err(Error::custom(err.to_string()))).await {
return Err(Error::custom(format!(
"Failed to send response to agent '{agent_name}': {err}"
)));
}
}
}
}
None => {
result?;
}
}
Ok(())
}