tiny-agent 0.3.0

一个小而完整的 Rust LLM Agent 运行时:可中断、可恢复、可观测、可插拔的 agent loop / A small but complete LLM agent runtime in Rust — an interruptible, resumable, observable, pluggable agent loop.
Documentation
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 {
    /// 要执行的 bash 命令
    pub command: String,
}

pub async fn bash(
    sandbox: Arc<dyn Sandbox>,
    args: BashArgs,
    ctx: ToolCtx,
) -> Result<ToolOutcome, String> {
    // 实时回调:每行输出进入通用 progress 通道。若该工具被后台化,progress 会被
    // `check(id)` 按增量读回;同时也会推 messages 供 UI 实时刷屏。
    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())
}