ai-usagebar 1.20.2

Omarchy/Waybar widgets + TUI for tracking multi-provider AI plan usage
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//! Fetch Command Code usage, with the same cache/lock discipline as every
//! other vendor.
//!
//! The official CLI's `/usage` view is assembled from three calls: `whoami`
//! names the org that scopes the rest, the credit ledger carries the balance
//! and the rolling spend windows, and the subscription names the plan. Only
//! the ledger is load-bearing — a failure anywhere else costs its own detail
//! and nothing more.

use std::fmt::Write as _;
use std::time::Duration;

use chrono::DateTime;
use serde_json::Value;
use sha2::{Digest, Sha256};

use crate::cache::{Cache, acquire_lock_async};
use crate::error::{AppError, Result};
use crate::vendor::{MAX_BODY_BYTES, read_body_capped};

use super::types::{Snapshot, apply_subscription, parse_credits};

pub const BASE_URL: &str = "https://api.commandcode.ai";

pub const WHOAMI_PATH: &str = "/alpha/whoami";
pub const CREDITS_PATH: &str = "/alpha/billing/credits";
pub const SUBSCRIPTIONS_PATH: &str = "/alpha/billing/subscriptions";

const HTTP_TIMEOUT: Duration = Duration::from_secs(10);
const LOCK_TIMEOUT: Duration = Duration::from_secs(15);
const SCHEMA_ERROR: &str = "Command Code usage response schema mismatch";

#[derive(Debug, Clone)]
pub struct Endpoints {
    pub base: String,
}

impl Default for Endpoints {
    fn default() -> Self {
        Self {
            base: BASE_URL.to_string(),
        }
    }
}

impl Endpoints {
    fn url(&self, path: &str) -> String {
        format!("{}{path}", self.base.trim_end_matches('/'))
    }
}

/// This vendor's [`Outcome`](crate::outcome::Outcome) — the shared shape,
/// specialised to its snapshot.
pub type FetchOutcome = crate::outcome::Outcome<Snapshot>;

pub async fn fetch_snapshot(
    client: &reqwest::Client,
    token: &str,
    cache: &Cache,
    endpoints: &Endpoints,
    ttl: Duration,
) -> Result<FetchOutcome> {
    cache.ensure_dir()?;
    let _lock = acquire_lock_async(&cache.lock_path(), LOCK_TIMEOUT).await?;
    let target = target_key(endpoints, token);

    if let Some(bytes) = cache.fresh_payload(ttl)?
        && let Ok(snapshot) = parse_cache(&bytes, &target)
    {
        return Ok(crate::outcome::Outcome::cached(snapshot, cache, false));
    }

    match fetch_live(client, token, endpoints).await {
        Ok(snapshot) => {
            let body = serde_json::to_vec(&serde_json::json!({
                "target": target,
                "response": snapshot_repr(&snapshot),
            }))?;
            cache.write_payload(&body)?;
            Ok(crate::outcome::Outcome::fresh(snapshot))
        }
        Err(error @ AppError::Transport(_)) => fallback_or_error(cache, None, &target, error),
        Err(AppError::Http { status, .. }) => {
            let message = status_message(status).to_string();
            cache.mark_stale();
            cache.write_last_error(status, &message);
            fallback_or_error(
                cache,
                Some((status, message.clone())),
                &target,
                AppError::Http {
                    status,
                    body: message,
                },
            )
        }
        Err(AppError::Schema(_)) => {
            let message = SCHEMA_ERROR.to_string();
            cache.mark_stale();
            cache.write_last_error(0, &message);
            fallback_or_error(
                cache,
                Some((0, message.clone())),
                &target,
                AppError::Schema(message),
            )
        }
        Err(error) => fallback_or_error(cache, None, &target, error),
    }
}

async fn fetch_live(
    client: &reqwest::Client,
    token: &str,
    endpoints: &Endpoints,
) -> Result<Snapshot> {
    // whoami scopes the ledger to an org. A personal account has none, and a
    // failure here only costs that scoping, so the error is not propagated.
    let org_id = get_json(client, token, &endpoints.url(WHOAMI_PATH), &[])
        .await
        .ok()
        .and_then(|value| value.get("org")?.get("id")?.as_str().map(str::to_string));
    let scope: Vec<(&str, String)> = org_id.iter().map(|id| ("orgId", id.clone())).collect();

    let credits = get_json(client, token, &endpoints.url(CREDITS_PATH), &scope).await?;
    let mut snapshot =
        parse_credits(&credits).map_err(|error| AppError::Schema(error.to_string()))?;

    // The plan is presentation detail; without it the windows still render.
    if let Ok(subscription) =
        get_json(client, token, &endpoints.url(SUBSCRIPTIONS_PATH), &scope).await
    {
        apply_subscription(&mut snapshot, &subscription);
    }

    Ok(snapshot)
}

async fn get_json(
    client: &reqwest::Client,
    token: &str,
    url: &str,
    query: &[(&str, String)],
) -> Result<Value> {
    let response = tokio::time::timeout(
        HTTP_TIMEOUT,
        client
            .get(url)
            .bearer_auth(token)
            .query(query)
            .header(reqwest::header::ACCEPT, "application/json")
            .send(),
    )
    .await
    .map_err(|_| AppError::Transport("Command Code request timed out".to_string()))??;

    let status = response.status();
    let body = read_body_capped(response, MAX_BODY_BYTES).await?;
    if !status.is_success() {
        return Err(AppError::Http {
            status: status.as_u16(),
            body: status_message(status.as_u16()).to_string(),
        });
    }
    serde_json::from_slice(&body).map_err(|error| AppError::Schema(error.to_string()))
}

/// Stable, non-secret identity for the endpoint and the account the token
/// resolves to. Cache reuse must fail closed when either input changes.
fn target_key(endpoints: &Endpoints, token: &str) -> String {
    let digest = Sha256::digest(token.as_bytes());
    let mut fingerprint = String::with_capacity(digest.len() * 2);
    for byte in digest {
        let _ = write!(fingerprint, "{byte:02x}");
    }
    format!("{}|key:{fingerprint}", endpoints.base)
}

fn status_message(status: u16) -> &'static str {
    match status {
        401 | 403 => crate::error::AUTH_FAILURE_MESSAGE,
        429 => "Command Code rate limited the usage request",
        500..=599 => "Command Code usage endpoint is unavailable",
        _ => "Command Code usage request failed",
    }
}

/// Cache the parsed snapshot rather than the raw bodies: the raw ledger is
/// account data with no reason to sit on disk longer than it must.
fn snapshot_repr(snapshot: &Snapshot) -> Value {
    let window = |window: &Option<super::types::SpendWindow>| {
        window.as_ref().map(|w| {
            serde_json::json!({
                "used": w.used,
                "cap": w.cap,
                "resetAt": w.resets_at.map(|at| at.timestamp_millis()),
            })
        })
    };
    serde_json::json!({
        "plan": snapshot.plan,
        "creditPool": snapshot.credit_pool,
        "periodEnd": snapshot.period_end.map(|at| at.timestamp_millis()),
        "credits": snapshot.credits.as_ref().map(|c| serde_json::json!({
            "monthlyCredits": c.monthly,
            "purchasedCredits": c.purchased,
            "freeCredits": c.free,
        })),
        "windowLimits": {
            "fiveHour": window(&snapshot.five_hour),
            "weekly": window(&snapshot.weekly),
        },
    })
}

fn parse_cache(bytes: &[u8], target: &str) -> Result<Snapshot> {
    let value: Value = serde_json::from_slice(bytes)
        .map_err(|_| AppError::Schema("Command Code cache is invalid".into()))?;
    if value.get("target").and_then(Value::as_str) != Some(target) {
        return Err(AppError::Schema(
            "Command Code cache belongs to a different account".into(),
        ));
    }
    let response = value
        .get("response")
        .ok_or_else(|| AppError::Schema("Command Code cache is missing its response".into()))?;
    let mut snapshot =
        parse_credits(response).map_err(|error| AppError::Schema(error.to_string()))?;
    snapshot.plan = response
        .get("plan")
        .and_then(Value::as_str)
        .map(str::to_string);
    snapshot.credit_pool = response.get("creditPool").and_then(Value::as_f64);
    // Older caches predate the field; a missing entry simply clears it and
    // the next live refresh restores it.
    snapshot.period_end = response
        .get("periodEnd")
        .and_then(Value::as_i64)
        .and_then(DateTime::from_timestamp_millis);
    Ok(snapshot)
}

fn fallback_or_error(
    cache: &Cache,
    last_error: Option<(u16, String)>,
    target: &str,
    error: AppError,
) -> Result<FetchOutcome> {
    crate::outcome::fallback(cache, last_error, error, |body| parse_cache(body, target))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cache::Cache;

    const CREDITS_BODY: &str = include_str!("../../tests/fixtures/commandcode/credits.json");
    const SUBSCRIPTION_BODY: &str =
        include_str!("../../tests/fixtures/commandcode/subscriptions.json");

    fn cache_in(dir: &std::path::Path) -> Cache {
        Cache::at(dir.join("commandcode"))
    }

    fn endpoints(server: &mockito::Server) -> Endpoints {
        Endpoints { base: server.url() }
    }

    #[tokio::test]
    async fn walks_the_chain_and_scopes_calls_to_the_org() {
        let mut server = mockito::Server::new_async().await;
        let whoami = server
            .mock("GET", WHOAMI_PATH)
            .with_body(r#"{"org":{"id":"org-42"}}"#)
            .create_async()
            .await;
        let credits = server
            .mock("GET", CREDITS_PATH)
            .match_query(mockito::Matcher::UrlEncoded(
                "orgId".into(),
                "org-42".into(),
            ))
            .with_body(CREDITS_BODY)
            .create_async()
            .await;
        let subscription = server
            .mock("GET", SUBSCRIPTIONS_PATH)
            .match_query(mockito::Matcher::UrlEncoded(
                "orgId".into(),
                "org-42".into(),
            ))
            .with_body(SUBSCRIPTION_BODY)
            .create_async()
            .await;
        let dir = tempfile::tempdir().unwrap();

        let outcome = fetch_snapshot(
            &reqwest::Client::new(),
            "tok",
            &cache_in(dir.path()),
            &endpoints(&server),
            Duration::from_secs(60),
        )
        .await
        .expect("fetch must succeed");

        whoami.assert_async().await;
        credits.assert_async().await;
        subscription.assert_async().await;
        assert_eq!(outcome.snapshot.plan.as_deref(), Some("GOAT"));
        assert_eq!(outcome.snapshot.five_hour.unwrap().pct(), 25);
        assert!(!outcome.stale);
    }

    #[tokio::test]
    async fn a_personal_account_without_an_org_still_gets_its_ledger() {
        let mut server = mockito::Server::new_async().await;
        server
            .mock("GET", WHOAMI_PATH)
            .with_body(r#"{"org":null}"#)
            .create_async()
            .await;
        let credits = server
            .mock("GET", CREDITS_PATH)
            .match_query(mockito::Matcher::Missing)
            .with_body(CREDITS_BODY)
            .create_async()
            .await;
        server
            .mock("GET", SUBSCRIPTIONS_PATH)
            .with_body(SUBSCRIPTION_BODY)
            .create_async()
            .await;
        let dir = tempfile::tempdir().unwrap();

        let outcome = fetch_snapshot(
            &reqwest::Client::new(),
            "tok",
            &cache_in(dir.path()),
            &endpoints(&server),
            Duration::from_secs(60),
        )
        .await
        .expect("fetch must succeed without an org");

        credits.assert_async().await;
        assert!(outcome.snapshot.weekly.is_some());
    }

    #[tokio::test]
    async fn a_failing_subscription_costs_the_plan_not_the_windows() {
        let mut server = mockito::Server::new_async().await;
        server
            .mock("GET", WHOAMI_PATH)
            .with_body("{}")
            .create_async()
            .await;
        server
            .mock("GET", CREDITS_PATH)
            .with_body(CREDITS_BODY)
            .create_async()
            .await;
        server
            .mock("GET", SUBSCRIPTIONS_PATH)
            .with_status(500)
            .create_async()
            .await;
        let dir = tempfile::tempdir().unwrap();

        let outcome = fetch_snapshot(
            &reqwest::Client::new(),
            "tok",
            &cache_in(dir.path()),
            &endpoints(&server),
            Duration::from_secs(60),
        )
        .await
        .expect("windows must survive a subscription failure");

        assert!(outcome.snapshot.plan.is_none());
        assert_eq!(outcome.snapshot.weekly.unwrap().pct(), 30);
    }

    #[tokio::test]
    async fn a_failing_ledger_is_fatal_and_maps_401_to_the_auth_message() {
        let mut server = mockito::Server::new_async().await;
        server
            .mock("GET", WHOAMI_PATH)
            .with_body("{}")
            .create_async()
            .await;
        server
            .mock("GET", CREDITS_PATH)
            .with_status(401)
            .create_async()
            .await;
        let dir = tempfile::tempdir().unwrap();

        let error = fetch_snapshot(
            &reqwest::Client::new(),
            "tok",
            &cache_in(dir.path()),
            &endpoints(&server),
            Duration::from_secs(60),
        )
        .await
        .expect_err("the ledger is load-bearing");

        assert!(
            error
                .to_string()
                .contains(crate::error::AUTH_FAILURE_MESSAGE),
            "{error}"
        );
    }

    #[tokio::test]
    async fn a_fresh_cache_is_served_without_touching_the_network() {
        let mut server = mockito::Server::new_async().await;
        let whoami = server
            .mock("GET", WHOAMI_PATH)
            .expect(1)
            .with_body("{}")
            .create_async()
            .await;
        server
            .mock("GET", CREDITS_PATH)
            .expect(1)
            .with_body(CREDITS_BODY)
            .create_async()
            .await;
        server
            .mock("GET", SUBSCRIPTIONS_PATH)
            .expect(1)
            .with_body(SUBSCRIPTION_BODY)
            .create_async()
            .await;
        let dir = tempfile::tempdir().unwrap();
        let cache = cache_in(dir.path());
        let endpoints = endpoints(&server);
        let client = reqwest::Client::new();

        let first = fetch_snapshot(&client, "tok", &cache, &endpoints, Duration::from_secs(600))
            .await
            .unwrap();
        let second = fetch_snapshot(&client, "tok", &cache, &endpoints, Duration::from_secs(600))
            .await
            .unwrap();

        // Each endpoint was called exactly once across both fetches.
        whoami.assert_async().await;
        assert_eq!(first.snapshot, second.snapshot);
        assert_eq!(second.snapshot.plan.as_deref(), Some("GOAT"));
    }

    #[tokio::test]
    async fn a_cache_written_for_another_token_is_not_reused() {
        let mut server = mockito::Server::new_async().await;
        server
            .mock("GET", WHOAMI_PATH)
            .with_body("{}")
            .expect_at_least(2)
            .create_async()
            .await;
        server
            .mock("GET", CREDITS_PATH)
            .with_body(CREDITS_BODY)
            .expect_at_least(2)
            .create_async()
            .await;
        server
            .mock("GET", SUBSCRIPTIONS_PATH)
            .with_body(SUBSCRIPTION_BODY)
            .expect_at_least(2)
            .create_async()
            .await;
        let dir = tempfile::tempdir().unwrap();
        let cache = cache_in(dir.path());
        let endpoints = endpoints(&server);
        let client = reqwest::Client::new();

        fetch_snapshot(
            &client,
            "token-a",
            &cache,
            &endpoints,
            Duration::from_secs(600),
        )
        .await
        .unwrap();
        // A different account must re-fetch rather than read the first's cache.
        fetch_snapshot(
            &client,
            "token-b",
            &cache,
            &endpoints,
            Duration::from_secs(600),
        )
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn a_stale_cache_carries_the_widget_through_an_outage() {
        let mut server = mockito::Server::new_async().await;
        let whoami = server
            .mock("GET", WHOAMI_PATH)
            .with_body("{}")
            .create_async()
            .await;
        let credits = server
            .mock("GET", CREDITS_PATH)
            .with_body(CREDITS_BODY)
            .create_async()
            .await;
        let subscription = server
            .mock("GET", SUBSCRIPTIONS_PATH)
            .with_body(SUBSCRIPTION_BODY)
            .create_async()
            .await;
        let dir = tempfile::tempdir().unwrap();
        let cache = cache_in(dir.path());
        let endpoints = endpoints(&server);
        let client = reqwest::Client::new();

        fetch_snapshot(&client, "tok", &cache, &endpoints, Duration::ZERO)
            .await
            .unwrap();

        // The endpoint starts failing; the cached snapshot still renders.
        whoami.remove_async().await;
        credits.remove_async().await;
        subscription.remove_async().await;
        server
            .mock("GET", CREDITS_PATH)
            .with_status(503)
            .create_async()
            .await;
        server
            .mock("GET", WHOAMI_PATH)
            .with_status(503)
            .create_async()
            .await;

        let outcome = fetch_snapshot(&client, "tok", &cache, &endpoints, Duration::ZERO)
            .await
            .expect("a stale snapshot beats no snapshot");

        assert!(outcome.stale);
        assert_eq!(outcome.snapshot.plan.as_deref(), Some("GOAT"));
        assert_eq!(outcome.last_error.unwrap().0, 503);
    }

    #[tokio::test]
    async fn a_schema_mismatch_is_reported_as_drift_not_as_success() {
        let mut server = mockito::Server::new_async().await;
        server
            .mock("GET", WHOAMI_PATH)
            .with_body("{}")
            .create_async()
            .await;
        server
            .mock("GET", CREDITS_PATH)
            .with_body(r#"{"unexpected":true}"#)
            .create_async()
            .await;
        let dir = tempfile::tempdir().unwrap();

        let error = fetch_snapshot(
            &reqwest::Client::new(),
            "tok",
            &cache_in(dir.path()),
            &endpoints(&server),
            Duration::from_secs(60),
        )
        .await
        .expect_err("an unrecognised ledger must not pass as usage");

        assert!(error.to_string().contains("schema"), "{error}");
    }
}