use std::env;
use anyhow::{Context, Result, ensure};
use rig_a2a::{A2aServer, RemoteAgent};
use rig_agent::AgentBuilder;
use rig_core::DynModel;
use rig_core::operation::Completion;
use rig_core::providers::{opencode, openrouter};
const OPENING_TIME: &str = "07:45";
const QUESTION: &str = "When does the Harbor Library open on Tuesday? Ask the library desk.";
fn model_from_env() -> Result<(DynModel<Completion>, String)> {
let id = env::var("RIG_MODEL")
.or_else(|_| env::var("OPENROUTER_MODEL"))
.context("set RIG_MODEL, for example RIG_MODEL=opencode-go/glm-5.3")?;
if let Some(model) = id.strip_prefix("opencode-go/") {
let key = env::var("OPENCODE_GO_API_KEY").context("OPENCODE_GO_API_KEY is not set")?;
let client = opencode::go_new(key);
return Ok((
client.completion(model).into(),
format!("OpenCode Go {model}"),
));
}
let client = openrouter::from_env()?;
Ok((client.chat(&id).into(), format!("OpenRouter {id}")))
}
#[tokio::main]
async fn main() -> Result<()> {
let (model, label) = model_from_env()?;
println!("model: {label}");
let desk = AgentBuilder::new(model.clone())
.name("Harbor Library desk")
.description("Knows the opening hours of the Harbor Library.")
.preamble(format!(
"You work at the Harbor Library front desk. The library opens at {OPENING_TIME} \
on Tuesdays. Answer in one short sentence."
))
.build();
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
let url = format!("http://{}", listener.local_addr()?);
let app = A2aServer::new(desk, &url).into_router();
tokio::spawn(async move { rig_a2a::axum::serve(listener, app).await });
let remote = RemoteAgent::connect(&url).await?;
println!("remote: {} at {url}", remote.card().name);
let planner = AgentBuilder::new(model)
.name("Trip planner")
.preamble(
"You plan local trips. You do not know any library's opening hours: ask the \
remote agent tool, then answer using only what it says.",
)
.dynamic_tool(remote.into_tool()?)
.default_max_turns(4)
.build();
let response = planner.prompt(QUESTION).await?;
println!("{}", response.output());
ensure!(
response.output().contains(OPENING_TIME),
"the answer does not carry the remote opening time {OPENING_TIME}"
);
Ok(())
}