wasm-agent 0.2.0

A ReAct and native tool-calling agent loop with no I/O, for WASM, edge workers and plugins: parse model replies, dispatch tools, keep history
Documentation
//! The README quickstart. Run with `cargo run --example quickstart`.

use wasm_agent::{AgentConfig, LoopRunner, ReActStep, ToolRegistry, ToolSpec};
use wasm_agent::types::ToolResult;

fn main() -> Result<(), wasm_agent::AgentError> {
    let mut tools = ToolRegistry::new();
    tools.register(
        ToolSpec::new("capital", "Returns the capital of a country", r#"{"type":"string"}"#),
        Box::new(|country: &str| ToolResult {
            tool_name: "capital".into(),
            output: if country == "France" { "Paris".into() } else { "unknown".into() },
            success: true,
        }),
    )?;

    // Let the model fix its own mistakes (unknown tool, missing labels)
    // instead of ending the loop with an error.
    let config = AgentConfig { recover_from_errors: true, ..Default::default() };
    let mut runner = LoopRunner::new(&config, &tools).with_system_prompt();
    runner.ask("What is the capital of France?");

    // In a real agent each string comes from your LLM call; send
    // runner.transcript() (or runner.history().to_chat_messages()) each time.
    runner.step("Thought: I should look it up.\nAction: capital(France)")?;
    match runner.step("Final Answer: Paris")? {
        ReActStep::FinalAnswer(answer) => println!("answer: {answer}"),
        other => println!("not done: {other:?}"),
    }
    print!("{}", runner.transcript());
    Ok(())
}