use schemars::JsonSchema;
use serde::Deserialize;
use wasm_agent::{AgentConfig, LoopRunner, ToolRegistry};
#[derive(Deserialize, JsonSchema)]
struct Convert {
amount: f64,
from: String,
to: String,
}
fn main() -> Result<(), wasm_agent::AgentError> {
let mut tools = ToolRegistry::new();
tools.register_typed("convert", "Convert money between currencies", |c: Convert| {
let rate = match (c.from.as_str(), c.to.as_str()) {
("EUR", "USD") => 1.08,
("USD", "EUR") => 0.93,
_ => return Err(format!("no rate for {} to {}", c.from, c.to)),
};
Ok(format!("{:.2} {}", c.amount * rate, c.to))
})?;
println!("schema sent to the model: {}", tools.openai_tools()?[0]["function"]["parameters"]);
let config = AgentConfig::default();
let mut runner = LoopRunner::new(&config, &tools).with_system_prompt();
runner.ask("How much is 100 euros in dollars?");
runner.step(r#"Action: convert({"amount": "a hundred", "from": "EUR", "to": "USD"})"#)?; runner.step(r#"Action: convert({"amount": 100, "from": "EUR", "to": "USD"})"#)?;
runner.step("Final Answer: about 108 US dollars")?;
print!("{}", runner.transcript());
Ok(())
}