use std::error::Error;
use std::sync::Arc;
use troupekit::agent::{Limits, PeriodLimit};
use troupekit::core::store::UsageLedger;
use troupekit::prelude::*;
use troupekit::SqliteStore;
const MODEL: &str = "anthropic/claude-haiku-5-5";
const TEAM: &str = "team:release-notes";
const TEAM_CAP: u64 = 600;
fn member(name: &str, instructions: &str) -> Agent {
Agent::new(name)
.model(MODEL)
.instructions(instructions)
.max_turns(2)
.max_tokens(500)
}
async fn team(ledger: Arc<SqliteStore>) -> Result<BuiltGraph, KitError> {
Graph::new("release-team")
.agent("plan", member("planner", "Turn the change into three bullet points a user cares about."))
.agent("write", member("writer", "Write a release note of at most 60 words from the bullet points."))
.agent("review", member("reviewer", "Return the release note with any jargon replaced by plain words, and nothing else."))
.limits(Limits {
run: Some(10_000),
scope: Some(PeriodLimit { key: TEAM.into(), max_usd_micros: TEAM_CAP }),
..Limits::default()
})
.ledger(ledger)
.build()
.await
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let dir = tempfile::tempdir()?;
let ledger = Arc::new(SqliteStore::open(dir.path().join("team.db")).await?);
let team = team(ledger.clone()).await?;
let changes = [
"Runs now resume after the process is killed.",
"Tool arguments are checked against their schema before the tool runs.",
"Agents can search a folder of Markdown files.",
"Traces record sizes by default and text only when asked.",
];
for change in changes {
let spent = ledger.period_total(TEAM).await?;
println!("team has spent {spent} of {TEAM_CAP}");
match team.run_with(change).await {
Ok(report) => println!(
"{}\n({} steps, {} millionths of a dollar)\n",
report.output().as_str().unwrap_or_default(),
report.steps(),
report.usage().cost_usd_micros
),
Err(KitError::Paused { run_id, reason }) => {
println!("run {run_id} paused before its next model call: {reason:?}");
return Ok(());
}
Err(KitError::Failed { run_id, reason }) => {
println!("run {run_id} stopped before its next model call: {reason}");
return Ok(());
}
Err(other) => return Err(other.into()),
}
}
println!("all changes written under the cap");
Ok(())
}