use crate::agent::Agent;
use crate::error::Result;
#[derive(Debug, Clone)]
pub struct IsolatedSubAgentConfig {
pub system_prompt: String,
pub max_iterations: usize,
pub token_budget: usize,
pub tool_call_limit: usize,
pub timeout_secs: u64,
}
impl Default for IsolatedSubAgentConfig {
fn default() -> Self {
Self {
system_prompt:
"You are a helpful sub-agent. Complete the task and report only the result.".into(),
max_iterations: 5,
token_budget: 16_000,
tool_call_limit: 20,
timeout_secs: 120,
}
}
}
pub struct IsolatedSubAgentResult {
pub output: String,
pub success: bool,
}
pub async fn run_isolated(
parent: &crate::prelude::ReactAgent,
task: &str,
config: &IsolatedSubAgentConfig,
) -> Result<IsolatedSubAgentResult> {
let sub = crate::prelude::ReactAgentBuilder::new()
.model(parent.model_name())
.system_prompt(&config.system_prompt)
.max_iterations(config.max_iterations)
.token_limit(config.token_budget)
.enable_tools()
.build()?;
let result = tokio::time::timeout(
std::time::Duration::from_secs(config.timeout_secs),
sub.execute(task),
)
.await;
match result {
Ok(Ok(output)) => Ok(IsolatedSubAgentResult {
output,
success: true,
}),
Ok(Err(e)) => Ok(IsolatedSubAgentResult {
output: format!("Sub-agent error: {e}"),
success: false,
}),
Err(_) => Ok(IsolatedSubAgentResult {
output: format!("Sub-agent timed out after {}s", config.timeout_secs),
success: false,
}),
}
}