use crate::args::{Format, Opts, parse};
use crate::cli::Output;
use crate::estimate::{Estimate, measure, over_budget};
use crate::json;
#[cfg(feature = "bpe")]
use crate::render::O200K;
use crate::render::{DUMMY, Method, Style, report};
#[cfg(feature = "ollama")]
use std::path::Path;
use std::path::PathBuf;
pub(crate) fn estimate(rest: &[String]) -> Output {
match parse(rest) {
Ok(opts) => graded(&opts),
Err(e) => Output::usage_err(format!("itok: {e}")),
}
}
fn graded(opts: &Opts) -> Output {
let root = PathBuf::from(opts.chdir.as_deref().unwrap_or("."));
#[cfg(feature = "ollama")]
if opts.is_ollama() {
return exact(opts, &root);
}
match measure(opts, &root) {
Ok(ests) => present(&ests, opts, method(opts)),
Err(e) => Output::usage_err(format!("itok: {e}")),
}
}
#[cfg(feature = "ollama")]
fn exact(opts: &Opts, root: &Path) -> Output {
match exact_measure(opts, root) {
Ok((ests, m)) => present(&ests, opts, &m),
Err(f) => Output {
out: String::new(),
err: format!("itok: {}\n", f.msg),
code: f.code,
},
}
}
fn present(ests: &[Estimate], opts: &Opts, m: &Method) -> Output {
let out = rendered(ests, opts, m);
let breaches = over_budget(ests, opts.budget);
if breaches.is_empty() {
Output::ok(out)
} else {
Output::breach(out, breach_msg(&breaches, opts.budget.unwrap_or(0)))
}
}
#[cfg(feature = "ollama")]
struct Fail {
msg: String,
code: u8,
}
#[cfg(feature = "ollama")]
impl From<String> for Fail {
fn from(msg: String) -> Self {
Self { msg, code: 7 }
}
}
#[cfg(feature = "ollama")]
fn exact_measure(
opts: &Opts,
root: &Path,
) -> Result<(Vec<Estimate>, Method), Fail> {
let fleet =
crate::ollama::hosts::bases(opts.ollama_hosts.as_deref(), root)?;
let base = fleet.first().ok_or_else(|| "no ollama host".to_owned())?;
let model = pick_model(opts, base)?;
let mut ests = Vec::new();
let files = crate::estimate::select(opts, root)
.map_err(|msg| Fail { msg, code: 2 })?;
for f in files {
let text = std::fs::read_to_string(root.join(&f))
.map_err(|e| format!("{f}: {e}"))?;
let tokens = crate::ollama::count(base, &model, &text)?;
ests.push(Estimate { path: f, tokens });
}
let m = crate::render::exact_via(&model, base);
Ok((crate::estimate::cap(ests, opts.top), m))
}
#[cfg(feature = "ollama")]
fn pick_model(opts: &Opts, base: &str) -> Result<String, Fail> {
let served = crate::ollama::models(base)?;
let names: Vec<&str> = served.iter().map(String::as_str).collect();
let Some(want) = opts.model.as_deref() else {
return names
.first()
.map(|m| (*m).to_owned())
.ok_or_else(|| "ollama host serves no models".to_owned().into());
};
resolved(want, &names)
}
#[cfg(feature = "ollama")]
fn resolved(want: &str, names: &[&str]) -> Result<String, Fail> {
use crate::ollama::pick::{self, Resolved};
match pick::resolve(names, want) {
Resolved::One(m) => Ok(m.to_owned()),
Resolved::Ambiguous(c) => Err(Fail {
msg: pick::ambiguous_msg(want, &c),
code: 2,
}),
Resolved::Missing => Err(Fail {
msg: pick::missing_msg(want, names, 1, 1),
code: 2,
}),
}
}
fn breach_msg(breaches: &[&Estimate], budget: u64) -> String {
let mut s =
format!("itok: {} file(s) over budget {budget}:\n", breaches.len());
for e in breaches {
s.push_str(&format!(" {} itok {}\n", e.tokens, e.path));
}
s
}
fn method(opts: &Opts) -> &'static Method {
match opts.tier {
#[cfg(feature = "bpe")]
crate::args::Tier::Bpe => &O200K,
#[cfg(feature = "ollama")]
crate::args::Tier::Ollama => &DUMMY, crate::args::Tier::Dummy => &DUMMY,
}
}
fn rendered(ests: &[Estimate], opts: &Opts, m: &Method) -> String {
match opts.format {
Format::Json => json::report(ests, m),
Format::Human => {
let style = Style {
summarize: opts.summarize,
human: opts.human,
};
report(ests, &style, m)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const DIR: &str = env!("CARGO_MANIFEST_DIR");
fn args(parts: &[&str]) -> Vec<String> {
parts.iter().map(|s| (*s).to_owned()).collect()
}
#[test]
fn names_unit_and_method() {
let o = estimate(&args(&["-C", DIR, "Cargo.toml"]));
assert!(o.out.contains("Cargo.toml"));
assert!(o.out.contains("itok"));
assert!(o.out.contains("total (bytes/4)"));
assert_eq!(o.code, 0);
}
#[test]
fn summarize_hides_the_per_file_lines() {
let o = estimate(&args(&["-s", "-C", DIR, "Cargo.toml"]));
assert!(!o.out.contains("Cargo.toml"));
assert!(o.out.contains("total"));
}
#[test]
fn human_abbreviates_the_count() {
let o = estimate(&args(&["-h", "-C", DIR, "SPEC.md"]));
assert!(o.out.contains('k'));
}
#[test]
fn json_carries_the_stable_schema() {
let o = estimate(&args(&["--format", "json", "-C", DIR, "Cargo.toml"]));
assert!(o.out.contains("\"unit\":\"input_tokens\""));
assert!(o.out.contains("\"estimated\":true"));
}
#[test]
fn json_is_one_object_per_file() {
let o = estimate(&args(&[
"--format",
"json",
"-C",
DIR,
"Cargo.toml",
"SPEC.md",
]));
assert_eq!(o.out.lines().count(), 2);
}
#[test]
fn an_unknown_format_is_a_usage_error() {
assert_eq!(estimate(&args(&["--format", "yaml"])).code, 2);
}
#[test]
fn top_without_a_number_is_a_usage_error() {
assert_eq!(estimate(&args(&["--top"])).code, 2);
}
#[test]
fn no_paths_uses_the_tracked_set() {
let o = estimate(&args(&["-C", DIR]));
assert!(o.out.contains("total"));
assert_eq!(o.code, 0);
}
#[test]
fn over_budget_exits_one_and_still_reports() {
let o = estimate(&args(&["--budget", "1k", "-C", DIR, "SPEC.md"]));
assert_eq!(o.code, 1);
assert!(o.err.contains("over budget"));
assert!(o.err.contains("SPEC.md"));
assert!(o.out.contains("itok"), "report still prints");
}
#[test]
fn under_budget_exits_zero() {
let o = estimate(&args(&["--budget", "1M", "-C", DIR, "Cargo.toml"]));
assert_eq!(o.code, 0);
assert!(o.err.is_empty());
}
#[cfg(feature = "bpe")]
#[test]
fn bpe_gives_a_true_count_without_a_tilde() {
let o = estimate(&args(&["--bpe", "-C", DIR, "Cargo.toml"]));
assert_eq!(o.code, 0);
assert!(o.out.contains("o200k"));
assert!(!o.out.contains('~'), "true count, no tilde: {:?}", o.out);
}
#[cfg(feature = "bpe")]
#[test]
fn bpe_json_is_not_estimated() {
let o = estimate(&args(&[
"--bpe",
"--format",
"json",
"-C",
DIR,
"Cargo.toml",
]));
assert!(o.out.contains("\"estimated\":false"));
assert!(o.out.contains("\"method\":\"o200k\""));
}
#[cfg(feature = "ollama")]
const FLEET: [&str; 3] =
["gpt-oss:20b", "gpt-oss:120b", "qwen2.5-coder:7b"];
#[cfg(feature = "ollama")]
#[test]
fn an_exact_model_name_resolves_to_itself() {
let got = resolved("qwen2.5-coder:7b", &FLEET);
assert_eq!(got.ok(), Some("qwen2.5-coder:7b".to_owned()));
}
#[cfg(feature = "ollama")]
#[test]
fn a_unique_prefix_resolves() {
let got = resolved("qwen", &FLEET);
assert_eq!(got.ok(), Some("qwen2.5-coder:7b".to_owned()));
}
#[cfg(feature = "ollama")]
#[test]
fn an_ambiguous_prefix_errors_and_names_the_candidates() {
let f = resolved("gpt-oss", &FLEET).err().unwrap_or_else(|| Fail {
msg: "resolved cleanly, but two models share this prefix"
.to_owned(),
code: 0,
});
assert_eq!(f.code, 2, "{}", f.msg);
assert!(f.msg.contains("gpt-oss:20b"), "msg: {}", f.msg);
assert!(f.msg.contains("gpt-oss:120b"), "msg: {}", f.msg);
}
#[cfg(feature = "ollama")]
#[test]
fn a_missing_model_errors_and_names_what_is_served() {
let f = resolved("llama", &FLEET).err().unwrap_or_else(|| Fail {
msg: "resolved cleanly, but this fleet serves no llama".to_owned(),
code: 0,
});
assert_eq!(f.code, 2, "{}", f.msg);
assert!(f.msg.contains("gpt-oss:20b"), "msg: {}", f.msg);
}
#[cfg(feature = "ollama")]
#[test]
fn a_plain_message_becomes_a_host_failure() {
let f = Fail::from("ollama host serves no models".to_owned());
assert_eq!(f.code, 7);
assert_eq!(f.msg, "ollama host serves no models");
}
}