token-manager 20261127.0.1

Read-only AI account balances and quotas in your terminal
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//! Cross-provider pricing from explicit canonical model identities, never fuzzy joins.
use crate::prices::Sort;
use anyhow::{Context, Result, ensure};
use reqwest::{Url, blocking::Client, redirect::Policy};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{
    collections::{BTreeMap, BTreeSet},
    fs,
    io::Read,
    path::Path,
    str::FromStr,
    time::Duration,
};

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CatalogMap {
    pub url: String,
    pub source: String,
    pub providers: String,
    pub models: String,
    pub provider_models: String,
    pub canonical_id: String,
    pub cost: String,
    #[serde(default)]
    pub direct_providers: BTreeMap<String, String>,
    #[serde(default)]
    pub vendor_aliases: BTreeMap<String, String>,
}
impl CatalogMap {
    pub fn load(root: &Path) -> Result<Self> {
        let path = root.join("providers/catalogs/models-dev.toml");
        let text = if path.exists() {
            ensure!(
                path.is_file() && !path.is_symlink(),
                "Catalog definition must be a regular file"
            );
            fs::read_to_string(path).context("Could not read catalog definition")?
        } else {
            include_str!("../providers/catalogs/models-dev.toml").to_owned()
        };
        toml::from_str(&text).context("Invalid catalog definition")
    }
    pub fn endpoint(&self, local: bool) -> Result<Url> {
        let url = Url::parse(&self.url).context("Invalid pricing catalog URL")?;
        ensure!(
            url.username().is_empty() && url.password().is_none() && url.fragment().is_none(),
            "Catalog URL must not contain credentials or a fragment"
        );
        ensure!(
            url.scheme() == "https"
                || (local
                    && url.scheme() == "http"
                    && matches!(url.host_str(), Some("127.0.0.1" | "[::1]"))),
            "Catalog requires HTTPS; loopback fixtures need --allow-local-http"
        );
        for pointer in [
            &self.providers,
            &self.models,
            &self.provider_models,
            &self.canonical_id,
            &self.cost,
        ] {
            ensure!(
                pointer.starts_with('/'),
                "Catalog mapping requires JSON pointers"
            );
        }
        for (k, v) in self
            .direct_providers
            .iter()
            .chain(self.vendor_aliases.iter())
        {
            ensure!(
                namespace(k) && namespace(v),
                "Invalid catalog vendor mapping"
            );
        }
        ensure!(
            safe_url(&self.source).is_some(),
            "Catalog source must be an HTTPS URL without credentials"
        );
        Ok(url)
    }
}
#[derive(Default)]
pub struct Filter<'a> {
    pub query: Option<&'a str>,
    pub vendor: Option<&'a str>,
    pub provider: Option<&'a str>,
}
#[derive(Serialize)]
pub struct Rates {
    pub input_per_million: Option<String>,
    pub output_per_million: Option<String>,
    pub cache_read_per_million: Option<String>,
    pub cache_write_per_million: Option<String>,
}
#[derive(Serialize)]
pub struct Tier {
    pub context_over: u64,
    pub rates: Rates,
}
#[derive(Serialize)]
pub struct Offer {
    pub provider: String,
    pub provider_name: String,
    pub model_id: String,
    pub direct: bool,
    pub rates: Rates,
    pub tiers: Vec<Tier>,
    pub status: Option<String>,
    pub note: Option<String>,
    pub provider_url: Option<String>,
    pub catalog_model_updated: Option<String>,
}
#[derive(Serialize)]
pub struct Model {
    pub id: String,
    pub name: String,
    pub vendor: Option<String>,
    pub family: Option<String>,
    pub offers: Vec<Offer>,
}
#[derive(Serialize)]
pub struct Report {
    pub source: String,
    pub observed_at: u64,
    pub currency: &'static str,
    pub sort: Sort,
    pub unlinked_offers: usize,
    pub models: Vec<Model>,
}
fn norm(s: &str) -> String {
    s.to_lowercase()
        .chars()
        .filter(|c| c.is_alphanumeric())
        .collect()
}
fn clean(s: &str) -> bool {
    !s.is_empty() && s.len() <= 512 && !s.chars().any(char::is_control)
}
fn label(value: &Value, key: &str) -> Option<String> {
    value
        .get(key)
        .and_then(Value::as_str)
        .filter(|s| clean(s))
        .map(str::to_owned)
}
fn safe_url(s: &str) -> Option<String> {
    let url = Url::parse(s).ok()?;
    (clean(s) && url.scheme() == "https" && url.username().is_empty() && url.password().is_none())
        .then(|| s.to_owned())
}
fn rate(value: &Value, key: &str) -> Option<String> {
    let raw = value.get(key)?;
    let text = match raw {
        Value::String(s) => s.clone(),
        Value::Number(n) => n.to_string(),
        _ => return None,
    };
    let number = Decimal::from_str(&text)
        .or_else(|_| Decimal::from_scientific(&text))
        .ok()?;
    (!number.is_sign_negative()).then(|| number.normalize().to_string())
}
fn rates(cost: &Value, text_tokens: bool) -> Rates {
    let get = |k| if text_tokens { rate(cost, k) } else { None };
    Rates {
        input_per_million: get("input"),
        output_per_million: get("output"),
        cache_read_per_million: get("cache_read"),
        cache_write_per_million: get("cache_write"),
    }
}
fn price(rates: &Rates, sort: Sort) -> Option<Decimal> {
    match sort {
        Sort::Input => &rates.input_per_million,
        Sort::Output => &rates.output_per_million,
    }
    .as_deref()
    .and_then(|s| Decimal::from_str(s).ok())
}
fn namespace(value: &str) -> bool {
    !value.is_empty()
        && value.len() <= 128
        && value
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || b"-_.".contains(&b))
}
fn canonical(value: &str) -> bool {
    value
        .split_once('/')
        .is_some_and(|(vendor, model)| namespace(vendor) && clean(model))
}
pub fn extract(value: &Value, map: &CatalogMap, filter: &Filter<'_>, sort: Sort) -> Result<Report> {
    for s in [filter.query, filter.vendor, filter.provider]
        .into_iter()
        .flatten()
    {
        ensure!(
            !norm(s).is_empty(),
            "Model, vendor and provider filters cannot be empty"
        );
    }
    ensure!(
        filter.query.is_some() || filter.vendor.is_some() || filter.provider.is_some(),
        "Specify a model/family, --vendor, or --provider"
    );
    let providers = value
        .pointer(&map.providers)
        .and_then(Value::as_object)
        .context("Pricing catalog provider map missing")?;
    let metadata = value
        .pointer(&map.models)
        .and_then(Value::as_object)
        .context("Pricing catalog model map missing")?;
    let vendors: BTreeSet<&str> = metadata
        .keys()
        .filter_map(|id| id.split_once('/').map(|(v, _)| v))
        .collect();
    let requested_vendor = filter.vendor.map(|v| {
        map.vendor_aliases
            .iter()
            .find(|(k, _)| norm(k) == norm(v))
            .map(|(_, v)| v.clone())
            .unwrap_or_else(|| v.to_lowercase())
    });
    let mut groups: BTreeMap<String, Model> = BTreeMap::new();
    let mut unlinked_offers = 0;
    for (provider_id, provider) in providers {
        ensure!(namespace(provider_id), "Invalid pricing provider identity");
        let provider_name = label(provider, "name").unwrap_or_else(|| provider_id.clone());
        if filter
            .provider
            .is_some_and(|p| norm(p) != norm(provider_id) && norm(p) != norm(&provider_name))
        {
            continue;
        }
        let models = provider
            .pointer(&map.provider_models)
            .and_then(Value::as_object)
            .context("Pricing catalog provider models missing")?;
        let direct_vendor = map
            .direct_providers
            .get(provider_id)
            .map(String::as_str)
            .or_else(|| {
                vendors
                    .contains(provider_id.as_str())
                    .then_some(provider_id.as_str())
            });
        for (model_id, offering) in models {
            ensure!(clean(model_id), "Invalid pricing model identity");
            let linked = match offering.pointer(&map.canonical_id) {
                Some(Value::String(id)) => {
                    ensure!(canonical(id), "Invalid canonical model identity");
                    Some(id.clone())
                }
                None | Some(Value::Null) => None,
                _ => anyhow::bail!("Invalid canonical model identity"),
            };
            // Only exact catalog identities or a known first-party namespace are joined.
            let linked = linked
                .or_else(|| metadata.contains_key(model_id).then(|| model_id.clone()))
                .or_else(|| {
                    vendors.contains(provider_id.as_str()).then(|| {
                        if model_id.starts_with(&format!("{provider_id}/")) {
                            model_id.clone()
                        } else {
                            format!("{provider_id}/{model_id}")
                        }
                    })
                });
            let vendor = linked
                .as_deref()
                .and_then(|id| id.split_once('/').map(|(v, _)| v.to_owned()));
            if vendor.is_none() {
                unlinked_offers += 1;
            }
            if requested_vendor
                .as_ref()
                .is_some_and(|v| vendor.as_ref().is_none_or(|actual| norm(v) != norm(actual)))
            {
                continue;
            }
            let id = linked
                .clone()
                .unwrap_or_else(|| format!("unlinked:{provider_id}/{model_id}"));
            let meta = linked.as_ref().and_then(|id| metadata.get(id));
            let name = meta
                .and_then(|m| label(m, "name"))
                .or_else(|| label(offering, "name"))
                .unwrap_or_else(|| model_id.clone());
            let family = meta
                .and_then(|m| label(m, "family"))
                .or_else(|| label(offering, "family"));
            let outputs = offering
                .pointer("/modalities/output")
                .and_then(Value::as_array);
            let text_tokens = outputs
                .is_some_and(|a| !a.is_empty() && a.iter().all(|s| s.as_str() == Some("text")));
            let cost = offering.pointer(&map.cost).unwrap_or(&Value::Null);
            let mut note = (!text_tokens).then(|| {
                "Non-text or unspecified output: token rates unavailable; see provider pricing"
                    .to_owned()
            });
            let mut tiers = Vec::new();
            if let Some(raw) = cost.get("tiers") {
                let raw = raw.as_array().context("Invalid pricing tiers")?;
                for tier in raw {
                    let threshold = tier
                        .pointer("/tier/size")
                        .and_then(Value::as_u64)
                        .context("Context tier threshold missing")?;
                    ensure!(
                        tier.pointer("/tier/type")
                            .and_then(Value::as_str)
                            .is_none_or(|v| v == "context"),
                        "Unsupported pricing tier type"
                    );
                    ensure!(
                        !tiers.iter().any(|t: &Tier| t.context_over == threshold),
                        "Duplicate context tier"
                    );
                    tiers.push(Tier {
                        context_over: threshold,
                        rates: rates(tier, text_tokens),
                    });
                }
            } else if let Some(tier) = cost.get("context_over_200k") {
                tiers.push(Tier {
                    context_over: 200_000,
                    rates: rates(tier, text_tokens),
                });
            }
            tiers.sort_by_key(|t| t.context_over);
            if offering.pointer("/experimental/modes").is_some() {
                let extra = "Mode-specific prices may differ; see provider pricing";
                note = Some(
                    note.map(|n| format!("{n}; {extra}"))
                        .unwrap_or_else(|| extra.into()),
                );
            }
            let direct = vendor
                .as_deref()
                .is_some_and(|v| v == provider_id || direct_vendor == Some(v));
            let group = groups.entry(id.clone()).or_insert_with(|| Model {
                id,
                name,
                vendor,
                family,
                offers: Vec::new(),
            });
            group.offers.push(Offer {
                provider: provider_id.clone(),
                provider_name: provider_name.clone(),
                model_id: model_id.clone(),
                direct,
                rates: rates(cost, text_tokens),
                tiers,
                status: label(offering, "status"),
                note,
                provider_url: provider
                    .get("doc")
                    .and_then(Value::as_str)
                    .and_then(safe_url),
                catalog_model_updated: label(offering, "last_updated"),
            });
        }
    }
    let mut models: Vec<Model> = groups.into_values().collect();
    if let Some(query) = filter.query {
        let q = norm(query);
        let exact = |m: &Model| {
            norm(&m.id) == q
                || norm(m.id.split_once('/').map(|(_, s)| s).unwrap_or(&m.id)) == q
                || norm(&m.name) == q
                || m.offers.iter().any(|o| norm(&o.model_id) == q)
        };
        if models.iter().any(exact) {
            models.retain(exact);
        } else {
            models.retain(|m| {
                norm(&m.id).contains(&q)
                    || norm(&m.name).contains(&q)
                    || m.family.as_ref().is_some_and(|f| norm(f).contains(&q))
                    || m.offers.iter().any(|o| norm(&o.model_id).contains(&q))
            });
        }
    }
    for m in &mut models {
        let vendor = m.vendor.as_deref().unwrap_or("");
        m.offers.sort_by(|a, b| {
            (b.provider == vendor)
                .cmp(&(a.provider == vendor))
                .then_with(|| b.direct.cmp(&a.direct))
                .then_with(|| price(&b.rates, sort).cmp(&price(&a.rates, sort)))
                .then_with(|| a.provider.cmp(&b.provider))
                .then_with(|| a.model_id.cmp(&b.model_id))
        });
    }
    let key = |m: &Model| m.offers.iter().find_map(|o| price(&o.rates, sort));
    models.sort_by(|a, b| key(b).cmp(&key(a)).then_with(|| a.id.cmp(&b.id)));
    ensure!(
        !models.is_empty(),
        "No models matched the model/vendor/provider filters"
    );
    Ok(Report {
        source: map.source.clone(),
        observed_at: crate::state::now(),
        currency: "USD",
        sort,
        unlinked_offers,
        models,
    })
}
pub fn fetch(map: &CatalogMap, filter: &Filter<'_>, sort: Sort, local: bool) -> Result<Report> {
    let endpoint = map.endpoint(local)?;
    let client = Client::builder()
        .timeout(Duration::from_secs(30))
        .connect_timeout(Duration::from_secs(5))
        .redirect(Policy::none())
        .build()?;
    let response = client
        .get(endpoint)
        .send()
        .map_err(|_| anyhow::anyhow!("Pricing catalog request failed or timed out"))?;
    ensure!(
        response.status().is_success(),
        "Pricing catalog returned HTTP {}",
        response.status().as_u16()
    );
    let mut body = Vec::new();
    response
        .take(32 * 1024 * 1024 + 1)
        .read_to_end(&mut body)
        .context("Could not read pricing catalog")?;
    ensure!(
        body.len() <= 32 * 1024 * 1024,
        "Pricing catalog exceeds size limit"
    );
    let value = serde_json::from_slice(&body)
        .map_err(|_| anyhow::anyhow!("Pricing catalog did not return valid JSON"))?;
    extract(&value, map, filter, sort)
}
fn row(label: &str, rates: &Rates) -> String {
    format!(
        "  {label:<44} {:>12} {:>12} {:>12}\n",
        rates.input_per_million.as_deref().unwrap_or("n/a"),
        rates.output_per_million.as_deref().unwrap_or("n/a"),
        rates.cache_read_per_million.as_deref().unwrap_or("n/a")
    )
}
pub fn table(report: &Report) -> String {
    let mut out = format!(
        "USD / 1M text tokens — direct vendor offers first\nCatalog: {} (fetched at Unix {})\n",
        report.source, report.observed_at
    );
    out.push_str("Listed catalog offers only; rates may lag vendor changes. Subscription fees, non-token charges and discounts are excluded.\n");
    for model in &report.models {
        out.push_str(&format!("\n{} — {}\n", model.id, model.name));
        out.push_str(&format!(
            "  {:<44} {:>12} {:>12} {:>12}\n",
            "PROVIDER / CONTEXT", "INPUT", "OUTPUT", "CACHE READ"
        ));
        if !model.offers.iter().any(|o| o.direct) {
            out.push_str("  Direct vendor offer not available in these results.\n");
        }
        for offer in &model.offers {
            let label = format!(
                "{}{} [{}]",
                offer.provider_name,
                if offer.direct { " (direct)" } else { "" },
                offer.model_id
            );
            out.push_str(&row(&label, &offer.rates));
            if let Some(first) = offer.tiers.first() {
                out.push_str(&format!(
                    "    Base rates above: context <= {} tokens\n",
                    first.context_over
                ));
            }
            for (i, tier) in offer.tiers.iter().enumerate() {
                let upper = offer
                    .tiers
                    .get(i + 1)
                    .map(|t| format!(" to {}", t.context_over))
                    .unwrap_or_default();
                out.push_str(&row(
                    &format!("  context > {}{upper}", tier.context_over),
                    &tier.rates,
                ));
            }
            if let Some(status) = &offer.status {
                out.push_str(&format!("    Status: {status}\n"));
            }
            if let Some(note) = &offer.note {
                out.push_str(&format!("    {note}\n"));
            }
            if let Some(url) = &offer.provider_url {
                out.push_str(&format!("    Provider details: {url}\n"));
            }
        }
    }
    if report.unlinked_offers > 0 {
        out.push_str(&format!("\n{} catalog offers lack a canonical vendor identity; they remain separate and cannot match --vendor.\n",report.unlinked_offers));
    }
    out
}