use localharness::{deny_all, Agent, ClosureTool, GeminiAgentConfig, Policy};
use serde_json::json;
#[tokio::main]
async fn main() -> localharness::Result<()> {
let Ok(api_key) = std::env::var("GEMINI_API_KEY") else {
eprintln!("This example needs a Gemini API key. Set it and re-run:");
eprintln!(" GEMINI_API_KEY=your-key cargo run --example basic_agent");
eprintln!("Get a key at https://aistudio.google.com/app/apikey");
return Ok(());
};
let add = ClosureTool::new(
"add",
"Add two integers and return their sum.",
json!({
"type": "object",
"properties": {
"a": { "type": "integer" },
"b": { "type": "integer" }
},
"required": ["a", "b"]
}),
|args, _ctx| async move {
let a = args["a"].as_i64().unwrap_or(0);
let b = args["b"].as_i64().unwrap_or(0);
Ok(json!({ "sum": a + b }))
},
);
let agent = Agent::start_gemini(
GeminiAgentConfig::new(api_key)
.with_system_instructions("You are a calculator. Use the `add` tool to compute sums.")
.with_tool(add)
.with_policies(vec![
deny_all(),
Policy::allow("add"),
Policy::allow("finish"),
]),
)
.await?;
let response = agent.chat("What is 17 plus 25?").await?;
println!("{}", response.text().await?);
agent.shutdown().await?;
Ok(())
}