feat: phase 2 — coûts par session (stats --costs) + am monitor TUI (closes #49 #50)

This commit is contained in:
2026-08-18 11:23:50 -04:00
parent 63dc36660b
commit 1b3fe09074
22 changed files with 978 additions and 23 deletions
+48 -1
View File
@@ -132,10 +132,23 @@ pub fn report_md(app: &App, days: u32) -> String {
));
}
}
// Issue #49: cost section when usage was recorded.
if let Ok(rows) = crate::costs::aggregate(app, Some(&format!("{days}d"))) {
if !rows.is_empty() {
let t = crate::costs::totals(&rows);
md.push_str(&format!(
"\n## Coûts estimés\n\n- Sessions facturées : {}\n- Tokens : {} in / {} out\n- Coût estimé : ${:.4}\n",
t.sessions, t.tokens_in, t.tokens_out, t.cost_usd
));
}
}
md
}
pub fn run(app: &App, agent: Option<&str>, period: Option<&str>) -> Result<i32> {
pub fn run(app: &App, agent: Option<&str>, period: Option<&str>, costs: bool) -> Result<i32> {
if costs {
return run_costs(app, period);
}
let rows = aggregate(app, agent, period)?;
if app.json() {
print_json(&rows);
@@ -201,6 +214,40 @@ pub fn run_top(app: &App, period: Option<&str>) -> Result<i32> {
Ok(0)
}
/// am stats --costs : token usage and estimated spend (issue #49).
pub fn run_costs(app: &App, period: Option<&str>) -> Result<i32> {
let rows = crate::costs::aggregate(app, period)?;
if app.json() {
print_json(&rows);
return Ok(0);
}
if rows.is_empty() {
app.log
.info("no cost recorded yet — agents exposing their usage (claude-code) are priced at session end");
return Ok(0);
}
let total = crate::costs::totals(&rows);
let mut table = Table::new(vec!["AGENT", "SESSIONS", "TOKENS IN", "TOKENS OUT", "COST $"]);
for r in &rows {
table.row(vec![
r.agent.clone(),
r.sessions.to_string(),
r.tokens_in.to_string(),
r.tokens_out.to_string(),
format!("{:.4}", r.cost_usd),
]);
}
table.row(vec![
"TOTAL".to_string(),
total.sessions.to_string(),
total.tokens_in.to_string(),
total.tokens_out.to_string(),
format!("{:.4}", total.cost_usd),
]);
print!("{}", table.render());
Ok(0)
}
/// am report : write the markdown digest (issue #37).
pub fn run_report(
app: &App,