ai-usagebar 0.16.0

Waybar widget + TUI for AI plan usage across Anthropic, OpenAI, Z.AI, OpenRouter, DeepSeek, Kimi, and Antigravity
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
//! xAI (Grok) fetch — reads the prepaid balance from the Management API. Two
//! steps when no team id is configured: resolve the team from the management
//! key (`/auth/management-keys/validation`), then read
//! `/v1/billing/teams/{team}/prepaid/balance`.

use std::time::Duration;

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

use super::types::{BalanceResp, Validation, to_snapshot};

pub const BASE_URL: &str = "https://management-api.x.ai";
const HTTP_TIMEOUT: Duration = Duration::from_secs(10);
const LOCK_TIMEOUT: Duration = Duration::from_secs(15);

#[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 validation_url(&self) -> String {
        format!("{}/auth/management-keys/validation", self.base)
    }
    fn balance_url(&self, team: &str) -> String {
        format!("{}/v1/billing/teams/{team}/prepaid/balance", self.base)
    }
}

#[derive(Debug, Clone)]
pub struct FetchOutcome {
    pub snapshot: GrokSnapshot,
    pub stale: bool,
    pub last_error: Option<(u16, String)>,
    pub cache_age: Option<Duration>,
}

/// `management_key` is the xAI Management API key (distinct from the inference
/// key). `team_id`, when set, skips the validation round-trip.
pub async fn fetch_snapshot(
    client: &reqwest::Client,
    management_key: &str,
    cache: &Cache,
    endpoints: &Endpoints,
    cache_ttl: Duration,
    team_id: Option<&str>,
) -> Result<FetchOutcome> {
    cache.ensure_dir()?;
    let _lock = acquire_lock_async(&cache.lock_path(), LOCK_TIMEOUT).await?;

    // Identity is derived from the *inputs*, not the resolved team, so a fresh
    // cache still costs zero network calls.
    let target = target_key(management_key, team_id);

    if let Some(bytes) = cache.fresh_payload(cache_ttl)?
        && let Ok(outcome) = reuse_cache(&bytes, cache, false, &target)
    {
        return Ok(outcome);
    }

    let team = match resolve_team(client, endpoints, management_key, team_id).await {
        Ok(t) => t,
        Err(e) if e.is_transient() => return fallback_silent(cache, &target, e),
        Err(e) => {
            cache.mark_stale();
            cache.write_last_error(0, &e.to_string());
            let diag = (0, e.to_string());
            return fallback_with_error(cache, Some(diag), &target, e);
        }
    };

    match fetch_live(client, endpoints, management_key, &team).await {
        Ok(snap) => {
            let bytes = serde_json::to_vec(&serde_json::json!({
                "target": target,
                "team": team,
                "snapshot": { "balance": snap.balance },
            }))?;
            cache.write_payload(&bytes)?;
            Ok(FetchOutcome {
                snapshot: snap,
                stale: false,
                last_error: None,
                cache_age: Some(Duration::ZERO),
            })
        }
        Err(e) if e.is_transient() => fallback_silent(cache, &target, e),
        Err(AppError::Http { status, body }) => {
            cache.mark_stale();
            cache.write_last_error(status, &body);
            let diag = (status, body.clone());
            fallback_with_error(cache, Some(diag), &target, AppError::Http { status, body })
        }
        Err(e) => {
            cache.mark_stale();
            cache.write_last_error(0, &e.to_string());
            let diag = (0, e.to_string());
            fallback_with_error(cache, Some(diag), &target, e)
        }
    }
}

/// Identity of the inputs that determine *whose* balance gets fetched. With an
/// explicit `team_id` that is the team; otherwise the team is derived from the
/// management key, so the key itself is the identity — fingerprinted, never
/// stored in clear. (The fingerprint is only a change detector: if its value
/// ever shifts, the effect is one extra refetch.)
fn target_key(management_key: &str, team_id: Option<&str>) -> String {
    match team_id.filter(|t| !t.is_empty()) {
        Some(t) => format!("team:{t}"),
        None => {
            use std::hash::{Hash, Hasher};
            let mut h = std::collections::hash_map::DefaultHasher::new();
            management_key.hash(&mut h);
            format!("key:{:016x}", h.finish())
        }
    }
}

/// An explicit `team_id` wins; otherwise introspect the management key. The
/// scope decides whether `scopeId` is a team at all — see [`Validation`].
async fn resolve_team(
    client: &reqwest::Client,
    endpoints: &Endpoints,
    key: &str,
    team_id: Option<&str>,
) -> Result<String> {
    match team_id {
        Some(t) if !t.is_empty() => Ok(t.to_string()),
        _ => {
            let v: Validation = get_json(client, &endpoints.validation_url(), key).await?;
            v.resolved_team()
        }
    }
}

fn fallback_silent(cache: &Cache, target: &str, original: AppError) -> Result<FetchOutcome> {
    let Some(bytes) = cache.fallback_payload(MAX_STALE)? else {
        return Err(original);
    };
    reuse_cache(&bytes, cache, true, target)
}

/// On failure we show the last good figure with the error alongside it. With
/// nothing usable cached there is nothing to show, so the **original** error is
/// returned — a first run against an organization-scoped key must reach the
/// user with its guidance intact, not as a generic "no usable cache".
fn fallback_with_error(
    cache: &Cache,
    last_error: Option<(u16, String)>,
    target: &str,
    original: AppError,
) -> Result<FetchOutcome> {
    let Some(bytes) = cache.fallback_payload(MAX_STALE)? else {
        return Err(original);
    };
    // A cache we cannot attribute to this target is no better than no cache.
    let Ok(mut outcome) = reuse_cache(&bytes, cache, true, target) else {
        return Err(original);
    };
    outcome.last_error = last_error;
    Ok(outcome)
}

fn reuse_cache(bytes: &[u8], cache: &Cache, stale: bool, target: &str) -> Result<FetchOutcome> {
    let snap = parse_cache(bytes, target)?;
    Ok(FetchOutcome {
        snapshot: snap,
        stale,
        last_error: cache.read_last_error(),
        cache_age: cache.payload_age(),
    })
}

fn parse_cache(bytes: &[u8], target: &str) -> Result<GrokSnapshot> {
    let v: serde_json::Value = serde_json::from_slice(bytes)?;
    // A balance cached for another team (or fetched with a different key) is
    // not this team's money. Payloads written before the target was recorded
    // are equally unattributable.
    let cached_target = v.get("target").and_then(serde_json::Value::as_str);
    if cached_target != Some(target) {
        return Err(AppError::Schema(format!(
            "grok cache belongs to a different team ({}); refetching",
            v.get("team")
                .and_then(serde_json::Value::as_str)
                .or(cached_target)
                .unwrap_or("unknown")
        )));
    }
    let s = v
        .get("snapshot")
        .ok_or_else(|| AppError::Schema("grok cache missing 'snapshot' field".into()))?;
    let balance = s["balance"]
        .as_f64()
        .ok_or_else(|| AppError::Schema("grok cache missing 'balance'".into()))?;
    Ok(GrokSnapshot {
        balance: finite_amount("grok cache", "balance", balance)?,
    })
}

async fn fetch_live(
    client: &reqwest::Client,
    endpoints: &Endpoints,
    key: &str,
    team: &str,
) -> Result<GrokSnapshot> {
    let resp: BalanceResp = get_json(client, &endpoints.balance_url(team), key).await?;
    to_snapshot(resp)
}

async fn get_json<T: for<'de> serde::Deserialize<'de>>(
    client: &reqwest::Client,
    url: &str,
    key: &str,
) -> Result<T> {
    let resp = tokio::time::timeout(
        HTTP_TIMEOUT,
        client
            .get(url)
            .header("Authorization", format!("Bearer {key}"))
            .send(),
    )
    .await
    .map_err(|_| AppError::Transport(format!("grok timeout: {url}")))??;

    let status = resp.status();
    let bytes = read_body_capped(resp, MAX_BODY_BYTES).await?;
    if !status.is_success() {
        let body = String::from_utf8_lossy(&bytes).chars().take(200).collect();
        return Err(AppError::Http {
            status: status.as_u16(),
            body,
        });
    }
    serde_json::from_slice(&bytes).map_err(|e| AppError::Schema(format!("grok {url}: {e}")))
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    fn cache_fixture() -> (TempDir, Cache) {
        let td = TempDir::new().unwrap();
        let cache = Cache::at(td.path().join("grok"));
        cache.ensure_dir().unwrap();
        (td, cache)
    }

    #[tokio::test]
    async fn resolves_team_then_reads_balance() {
        let mut server = mockito::Server::new_async().await;
        server
            .mock("GET", "/auth/management-keys/validation")
            .with_status(200)
            .with_body(r#"{"scopeId":"team-xyz","teamId":"team-xyz"}"#)
            .create_async()
            .await;
        server
            .mock("GET", "/v1/billing/teams/team-xyz/prepaid/balance")
            .match_header("authorization", "Bearer xai-mgmt")
            .with_status(200)
            .with_body(r#"{"changes":[],"total":{"val":"-2500"}}"#)
            .create_async()
            .await;

        let (_td, cache) = cache_fixture();
        let client = reqwest::Client::new();
        let endpoints = Endpoints { base: server.url() };
        let out = fetch_snapshot(
            &client,
            "xai-mgmt",
            &cache,
            &endpoints,
            Duration::from_secs(0),
            None,
        )
        .await
        .unwrap();
        assert!((out.snapshot.balance - 25.0).abs() < 1e-9);
        assert!(!out.stale);
    }

    #[tokio::test]
    async fn configured_team_skips_validation() {
        let mut server = mockito::Server::new_async().await;
        server
            .mock("GET", "/v1/billing/teams/my-team/prepaid/balance")
            .with_status(200)
            .with_body(r#"{"total":{"val":"-500"}}"#)
            .create_async()
            .await;

        let (_td, cache) = cache_fixture();
        let client = reqwest::Client::new();
        let endpoints = Endpoints { base: server.url() };
        let out = fetch_snapshot(
            &client,
            "xai-mgmt",
            &cache,
            &endpoints,
            Duration::from_secs(0),
            Some("my-team"),
        )
        .await
        .unwrap();
        assert!((out.snapshot.balance - 5.0).abs() < 1e-9);
    }

    #[tokio::test]
    async fn http_404_falls_back_to_cache_when_present() {
        let mut server = mockito::Server::new_async().await;
        server
            .mock("GET", "/v1/billing/teams/t/prepaid/balance")
            .with_status(404)
            .with_body(r#"{"error":"no team"}"#)
            .create_async()
            .await;

        let (_td, cache) = cache_fixture();
        cache
            .write_payload(
                serde_json::json!({
                    "target": "team:t",
                    "team": "t",
                    "snapshot": { "balance": 12.0 },
                })
                .to_string()
                .as_bytes(),
            )
            .unwrap();

        let client = reqwest::Client::new();
        let endpoints = Endpoints { base: server.url() };
        let out = fetch_snapshot(
            &client,
            "k",
            &cache,
            &endpoints,
            Duration::from_secs(0),
            Some("t"),
        )
        .await
        .unwrap();
        assert!(out.stale);
        assert_eq!(out.snapshot.balance, 12.0);
        assert_eq!(out.last_error.as_ref().map(|(c, _)| *c), Some(404));
    }

    #[tokio::test]
    async fn switching_team_refetches_instead_of_reusing_the_cache() {
        let mut server = mockito::Server::new_async().await;
        server
            .mock("GET", "/v1/billing/teams/team-b/prepaid/balance")
            .with_status(200)
            .with_body(r#"{"total":{"val":"-300"}}"#)
            .create_async()
            .await;

        let (_td, cache) = cache_fixture();
        // Fresh, but it is team-a's money.
        cache
            .write_payload(
                serde_json::json!({
                    "target": "team:team-a",
                    "team": "team-a",
                    "snapshot": { "balance": 999.0 },
                })
                .to_string()
                .as_bytes(),
            )
            .unwrap();

        let client = reqwest::Client::new();
        let endpoints = Endpoints { base: server.url() };
        let out = fetch_snapshot(
            &client,
            "k",
            &cache,
            &endpoints,
            Duration::from_secs(3600),
            Some("team-b"),
        )
        .await
        .unwrap();
        assert!((out.snapshot.balance - 3.0).abs() < 1e-9);
        assert!(!out.stale);
    }

    #[tokio::test]
    async fn fresh_cache_makes_no_network_call() {
        // No mocks registered: any request would fail the test.
        let server = mockito::Server::new_async().await;
        let (_td, cache) = cache_fixture();
        cache
            .write_payload(
                serde_json::json!({
                    "target": "team:t",
                    "team": "t",
                    "snapshot": { "balance": 4.5 },
                })
                .to_string()
                .as_bytes(),
            )
            .unwrap();

        let client = reqwest::Client::new();
        let endpoints = Endpoints { base: server.url() };
        let out = fetch_snapshot(
            &client,
            "k",
            &cache,
            &endpoints,
            Duration::from_secs(3600),
            Some("t"),
        )
        .await
        .unwrap();
        assert_eq!(out.snapshot.balance, 4.5);
    }

    #[tokio::test]
    async fn organization_scoped_key_reports_an_actionable_error() {
        let mut server = mockito::Server::new_async().await;
        server
            .mock("GET", "/auth/management-keys/validation")
            .with_status(200)
            .with_body(r#"{"scope":"SCOPE_ORGANIZATION","scopeId":"org-77"}"#)
            .create_async()
            .await;

        let (_td, cache) = cache_fixture();
        let client = reqwest::Client::new();
        let endpoints = Endpoints { base: server.url() };
        // No cache to fall back on, so the guidance must surface to the user.
        let out = fetch_snapshot(
            &client,
            "k",
            &cache,
            &endpoints,
            Duration::from_secs(0),
            None,
        )
        .await;
        let err = out.unwrap_err().to_string();
        assert!(err.contains("team_id"), "unhelpful error: {err}");
        assert!(!err.contains("org-77"), "must not adopt the org id: {err}");
    }

    #[tokio::test]
    async fn malformed_200_balance_does_not_become_zero() {
        let mut server = mockito::Server::new_async().await;
        server
            .mock("GET", "/v1/billing/teams/t/prepaid/balance")
            .with_status(200)
            .with_body(r#"{"error":"forbidden"}"#)
            .create_async()
            .await;

        let (_td, cache) = cache_fixture();
        let client = reqwest::Client::new();
        let endpoints = Endpoints { base: server.url() };
        let out = fetch_snapshot(
            &client,
            "k",
            &cache,
            &endpoints,
            Duration::from_secs(0),
            Some("t"),
        )
        .await;
        assert!(out.is_err(), "expected a schema error, got {out:?}");
    }
}