use crate::sandbox::Sandbox;
use crate::tools::{ToolCtx, ToolOutcome};
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;
#[derive(Deserialize, JsonSchema)]
pub struct BashArgs {
pub command: String,
}
pub async fn bash(
sandbox: Arc<dyn Sandbox>,
args: BashArgs,
ctx: ToolCtx,
) -> Result<ToolOutcome, String> {
let on_output = {
let progress = ctx.progress.clone();
Arc::new(move |_stream, chunk: &str| {
let line = format!("{chunk}\n");
progress.emit_text(line);
})
};
let output = sandbox
.execute(&args.command, Some(on_output))
.await
.map_err(|e| e.to_string())?;
Ok(json!({
"exit_code": output.exit_code,
})
.into())
}