use agentix::{AgentEvent, Message, Request, ToolBundle, UserContent, tool};
use futures::StreamExt;
use std::env;
struct Calculator;
#[tool]
impl agentix::Tool for Calculator {
async fn add(&self, a: f64, b: f64) -> f64 {
a + b
}
async fn multiply(&self, a: f64, b: f64) -> f64 {
a * b
}
async fn divide(&self, a: f64, b: f64) -> Result<f64, String> {
if b == 0.0 {
Err("division by zero".into())
} else {
Ok(a / b)
}
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let request = if let Ok(k) = env::var("DEEPSEEK_API_KEY") {
Request::deepseek(k)
} else if let Ok(k) = env::var("OPENAI_API_KEY") {
Request::openai(k)
} else {
panic!("Set DEEPSEEK_API_KEY or OPENAI_API_KEY");
}.system_prompt("You are a math assistant. Use your tools to compute exact results.");
let http = reqwest::Client::new();
let tools = ToolBundle::default() + Calculator;
let history = vec![
Message::User(vec![UserContent::Text { text: "What is (123 + 456) * 789 / 3?".into() }]),
];
println!("Question: (123 + 456) * 789 / 3\n");
let mut stream = agentix::agent(tools, http, request, history, Some(25_000));
while let Some(event) = stream.next().await {
match event {
AgentEvent::Token(t) => print!("{t}"),
AgentEvent::Reasoning(t) => print!("\x1b[2m{t}\x1b[0m"),
AgentEvent::ToolCallStart(tc) => {
println!("\n→ {}({})", tc.name, tc.arguments);
}
AgentEvent::ToolProgress { name, progress, .. } => {
println!(" [{name}] {progress}");
}
AgentEvent::ToolResult { name, ref content, .. } => {
let text = content.iter()
.filter_map(|p| if let agentix::Content::Text { text } = p { Some(text.as_str()) } else { None })
.collect::<Vec<_>>().join(" ");
println!("← [{name}] = {text}");
}
AgentEvent::Usage(u) => {
eprintln!("\n[tokens: {}]", u.total_tokens);
}
AgentEvent::Done(total) => {
eprintln!("\n[total tokens: {}]", total.total_tokens);
}
AgentEvent::Warning(w) => eprintln!("\n[warn] {w}"),
AgentEvent::Error(e) => {
eprintln!("\n[error] {e}");
break;
}
AgentEvent::ToolCallChunk(_) => {}
}
}
println!();
Ok(())
}