use clap::{Parser, Subcommand};
use obol_core::{estimate_cost, refresh_pricing_tables, Dialect, PricingSource};
use std::path::PathBuf;
use std::process::ExitCode;
#[derive(Parser)]
#[command(name = "obol", about = "Estimate agent-transcript token cost")]
struct Cli {
#[command(subcommand)]
cmd: Cmd,
}
#[derive(Subcommand)]
enum Cmd {
Estimate {
path: PathBuf,
#[arg(long, value_parser = ["claude", "codex", "pi", "gemini", "opencode", "copilot", "kimi"])]
dialect: Option<String>,
#[arg(long)]
json: bool,
},
Refresh {
#[arg(long)]
as_of: String,
},
}
fn run() -> Result<(), String> {
let cli = Cli::parse();
match cli.cmd {
Cmd::Estimate {
path,
dialect,
json,
} => {
let dialect = match dialect.as_deref() {
Some(d) => match d {
"claude" => Dialect::Claude,
"codex" => Dialect::Codex,
"pi" => Dialect::Pi,
"gemini" => Dialect::Gemini,
"opencode" => Dialect::Opencode,
"copilot" => Dialect::Copilot,
"kimi" => Dialect::Kimi,
other => unreachable!("clap value_parser restricts dialect; got {other:?}"),
},
None => {
let bytes = std::fs::read(&path).map_err(|e| e.to_string())?;
obol_core::transcript::detect(&bytes)
.map_err(|e| format!("{e}; pass --dialect to choose one explicitly"))?
}
};
let est = estimate_cost(&path, dialect).map_err(|e| e.to_string())?;
if json {
println!(
"{}",
serde_json::to_string_pretty(&est).map_err(|e| e.to_string())?
);
} else {
let src = match est.pricing_source {
PricingSource::Bundled => "bundled",
PricingSource::Local => "local",
};
println!(
"total: ${:.4} (pricing as of {}, {})",
est.total_usd, est.pricing_as_of, src
);
for m in &est.per_model {
println!(" {:30} ${:.4}", m.model, m.subtotal_usd);
}
if !est.unpriced_models.is_empty() {
println!(
" unpriced (run `obol refresh`?): {}",
est.unpriced_models.join(", ")
);
}
}
Ok(())
}
Cmd::Refresh { as_of } => {
let r = refresh_pricing_tables(&as_of).map_err(|e| e.to_string())?;
println!(
"refreshed {} models -> {}",
r.models,
r.written_to.display()
);
Ok(())
}
}
}
fn main() -> ExitCode {
match run() {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("error: {e}");
ExitCode::FAILURE
}
}
}