ai-usagebar 1.41.0

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
//! Wire types for `https://ollama.com/api/balance` and historical quota
//! payloads cached from `/api/usage`. Current usage-history responses are
//! deliberately rejected: request/token totals are not quota limits.

use serde::Deserialize;

use crate::usage::{OllamaCredits, OllamaModelUsage, OllamaSnapshot, UsageWindow};

#[derive(Debug, Clone, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum Body {
    Balance(BalanceBody),
    Legacy(LegacyBody),
}

impl Body {
    pub fn into_snapshot(self, plan: String) -> OllamaSnapshot {
        match self {
            Self::Balance(body) => body.into_snapshot(plan),
            Self::Legacy(body) => body.into_snapshot(plan),
        }
    }
}

/// Required discriminator prevents unrelated JSON from becoming empty quotas.
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct LegacyBody {
    pub limits: Limits,
    #[serde(default)]
    pub activity: Option<Activity>,
}

#[derive(Debug, Clone, Default, Deserialize, PartialEq)]
pub struct Limits {
    #[serde(default)]
    pub session: Option<Window>,
    #[serde(default)]
    pub weekly: Option<Window>,
    /// Calendar-month quota (`limits.monthly`). Reported instead of
    /// `session`/`weekly` on at least some Pro accounts — the two shapes are
    /// mutually observed, never combined in one response so far.
    #[serde(default)]
    pub monthly: Option<Window>,
}

/// One quota window. `usage` is a fraction in `[0.0, 1.0]`.
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct Window {
    #[serde(default)]
    pub usage: Option<f64>,
    #[serde(default)]
    pub models: Vec<ModelUsage>,
}

#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct ModelUsage {
    pub name: String,
    #[serde(default)]
    pub request_count: u64,
}

#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct Activity {
    /// Dollars as a string, e.g. `"0.00000"`. Kept exact so a tooltip can
    /// show what the server sent without rounding drift.
    #[serde(default)]
    pub cost: Option<String>,
    #[serde(default)]
    pub period: Option<Period>,
}

#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct Period {
    #[serde(rename = "type")]
    pub kind: String,
    #[serde(default)]
    pub starting_at: Option<chrono::DateTime<chrono::Utc>>,
    #[serde(default)]
    pub ending_at: Option<chrono::DateTime<chrono::Utc>>,
}

impl LegacyBody {
    /// Fraction in `[0, 1]` → percent `0..=100`, saturated. Missing usage is 0%.
    fn pct(frac: Option<f64>) -> i32 {
        let f = frac.unwrap_or(0.0).clamp(0.0, 1.0);
        (f * 100.0).round() as i32
    }

    fn models(w: Option<&Window>) -> Vec<OllamaModelUsage> {
        w.map(|window| {
            window
                .models
                .iter()
                .map(|m| OllamaModelUsage {
                    name: m.name.clone(),
                    request_count: m.request_count,
                })
                .collect()
        })
        .unwrap_or_default()
    }

    fn window(w: Option<Window>, duration: chrono::Duration) -> Option<UsageWindow> {
        let w = w?;
        Some(UsageWindow {
            utilization_pct: Self::pct(w.usage),
            // The JSON payload does not carry a reset timestamp (the HTML UI
            // does). Renderers pace against the window length only.
            resets_at: None,
            window_duration: duration,
        })
    }

    /// Project the wire payload into the cacheable snapshot. `plan` comes from
    /// config — the server does not send a plan field on this route.
    pub fn into_snapshot(self, plan: String) -> OllamaSnapshot {
        let session_models = Self::models(self.limits.session.as_ref());
        let weekly_models = Self::models(self.limits.weekly.as_ref());
        let monthly_models = Self::models(self.limits.monthly.as_ref());
        let (cost, period_kind) = match self.activity {
            Some(a) => (a.cost, a.period.map(|p| p.kind)),
            None => (None, None),
        };

        OllamaSnapshot {
            plan,
            session: Self::window(self.limits.session, chrono::Duration::hours(5)),
            weekly: Self::window(self.limits.weekly, chrono::Duration::days(7)),
            // Nominal length only — the API gives no cycle-start date, so
            // pacing against a real subscription month is not possible. Kept
            // consistent with session/weekly, which also carry no reset time.
            monthly: Self::window(self.limits.monthly, chrono::Duration::days(30)),
            session_models,
            weekly_models,
            monthly_models,
            activity_cost: cost,
            activity_period: period_kind,
            credits: None,
        }
    }
}

#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct BalanceBody {
    pub included: Included,
    #[serde(default)]
    pub purchased: Option<Purchased>,
}

#[derive(Debug, Clone, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum Included {
    Credits {
        balance_usd: f64,
        allowance_usd: f64,
        period: BillingPeriod,
    },
    Legacy {
        session: RemainingWindow,
        weekly: RemainingWindow,
    },
}

#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct BillingPeriod {
    pub from: chrono::DateTime<chrono::Utc>,
    pub until: chrono::DateTime<chrono::Utc>,
}

#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct Purchased {
    pub balance_usd: f64,
}

#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct RemainingWindow {
    pub remaining_percent: f64,
    pub resets_at: chrono::DateTime<chrono::Utc>,
}

impl RemainingWindow {
    fn into_window(self, duration: chrono::Duration) -> UsageWindow {
        UsageWindow {
            utilization_pct: (100.0 - self.remaining_percent).clamp(0.0, 100.0).round() as i32,
            resets_at: Some(self.resets_at),
            window_duration: duration,
        }
    }
}

impl BalanceBody {
    fn into_snapshot(self, plan: String) -> OllamaSnapshot {
        let mut snap = LegacyBody {
            limits: Limits::default(),
            activity: None,
        }
        .into_snapshot(plan);
        match self.included {
            Included::Credits {
                balance_usd,
                allowance_usd,
                period,
            } => {
                // No measurable percentage for a zero allowance; still show
                // the actual dollar balance, without dividing by zero.
                if allowance_usd > 0.0 {
                    snap.monthly = Some(UsageWindow {
                        utilization_pct: LegacyBody::pct(Some(1.0 - balance_usd / allowance_usd)),
                        resets_at: Some(period.until),
                        window_duration: period.until - period.from,
                    });
                }
                snap.credits = Some(OllamaCredits {
                    balance: crate::format::usd(balance_usd),
                    allowance: crate::format::usd(allowance_usd),
                    purchased: self.purchased.map(|p| crate::format::usd(p.balance_usd)),
                });
            }
            Included::Legacy { session, weekly } => {
                snap.session = Some(session.into_window(chrono::Duration::hours(5)));
                snap.weekly = Some(weekly.into_window(chrono::Duration::days(7)));
            }
        }
        snap
    }
}

#[cfg(test)]
mod tests {
    /// A real `/api/balance` payload from a session/weekly account, captured by
    /// a third party on #387. The credits branch of `Included` had a committed
    /// capture; this one did not, and it is the branch every pre-existing user
    /// depends on — so the shape is pinned by the bytes the API actually sent
    /// rather than by a hand-written sample.
    #[test]
    fn a_real_quota_account_balance_payload_keeps_its_windows() {
        let raw = include_str!("../../tests/fixtures/ollama/balance_quota.json");
        let body: Body = serde_json::from_str(raw).expect("captured payload must parse");
        let snap = body.into_snapshot("pro".into());

        // `remaining_percent` is what is LEFT, so utilization is its complement:
        // 100 remaining is 0 used, and 59.19 remaining rounds to 41 used.
        let session = snap.session.expect("session window");
        assert_eq!(session.utilization_pct, 0);
        let weekly = snap.weekly.expect("weekly window");
        assert_eq!(weekly.utilization_pct, 41);

        // A quota account has no credits block and no monthly window; a zero
        // purchased balance must not invent one.
        assert!(
            snap.monthly.is_none(),
            "quota accounts report no monthly window"
        );
        assert!(snap.credits.is_none(), "a quota account has no credits");

        // The resets travel with the windows rather than being guessed.
        assert!(
            session.resets_at.is_some(),
            "session reset comes from the payload"
        );
        assert!(
            weekly.resets_at.is_some(),
            "weekly reset comes from the payload"
        );
    }

    use super::*;

    /// Real 200 body captured 2026-09-09 against a Pro account (numbers
    /// redacted only by rounding — structure is verbatim).
    const LIVE: &str = r#"{
      "activity": {
        "cost": "0.00000",
        "period": {
          "type": "last_4_weeks",
          "starting_at": "2026-08-17T00:00:00Z",
          "ending_at": "2026-09-09T18:28:57.120401373Z"
        },
        "models": []
      },
      "limits": {
        "session": {
          "usage": 0.819,
          "models": [
            {"name": "kimi-k3", "request_count": 180},
            {"name": "deepseek-v4-flash:0731", "request_count": 27},
            {"name": "minimax-m3", "request_count": 8},
            {"name": "glm-5.3-flash", "request_count": 8},
            {"name": "gpt-oss:120b", "request_count": 2}
          ]
        },
        "weekly": {
          "usage": 0.23,
          "models": [
            {"name": "kimi-k3", "request_count": 180},
            {"name": "minimax-m3", "request_count": 554},
            {"name": "deepseek-v4-flash:0731", "request_count": 27},
            {"name": "qwen3.5:397b", "request_count": 2},
            {"name": "glm-5.3-flash", "request_count": 8},
            {"name": "gpt-oss:120b", "request_count": 2}
          ]
        }
      }
    }"#;

    #[test]
    fn parses_live_captured_body() {
        let body: Body = serde_json::from_str(LIVE).unwrap();
        let snap = body.into_snapshot("pro".into());
        assert_eq!(snap.plan, "pro");
        assert_eq!(snap.session.as_ref().unwrap().utilization_pct, 82);
        assert_eq!(snap.session_models[0].name, "kimi-k3");
        assert_eq!(snap.session_models[0].request_count, 180);
        assert_eq!(snap.session_models.len(), 5);
        assert_eq!(snap.weekly.as_ref().unwrap().utilization_pct, 23);
        assert_eq!(snap.weekly_models[1].name, "minimax-m3");
        assert_eq!(snap.weekly_models[1].request_count, 554);
        assert_eq!(snap.activity_cost.as_deref(), Some("0.00000"));
        assert_eq!(snap.activity_period.as_deref(), Some("last_4_weeks"));
    }

    #[test]
    fn missing_windows_are_none() {
        let body: Body = serde_json::from_str(r#"{"limits":{}}"#).unwrap();
        let snap = body.into_snapshot("free".into());
        assert!(snap.session.is_none());
        assert!(snap.weekly.is_none());
        assert!(snap.monthly.is_none());
        assert!(snap.session_models.is_empty());
        assert!(snap.weekly_models.is_empty());
        assert!(snap.monthly_models.is_empty());
    }

    /// Real 200 body captured 2026-09-16 against a different Pro account —
    /// this shape reports `limits.monthly` instead of `session`/`weekly`.
    /// Both shapes exist in the wild for the same "pro" plan label.
    const LIVE_MONTHLY: &str = r#"{
      "activity": {
        "cost": "0.00000",
        "period": {
          "type": "last_4_weeks",
          "starting_at": "2026-08-24T00:00:00Z",
          "ending_at": "2026-09-16T08:55:34.663902649Z"
        }
      },
      "limits": {
        "monthly": {
          "usage": 0.003,
          "models": [
            {"name": "gpt-oss:120b", "request_count": 100},
            {"name": "gpt-oss:20b", "request_count": 2}
          ]
        }
      }
    }"#;

    #[test]
    fn parses_live_captured_monthly_body() {
        let body: Body = serde_json::from_str(LIVE_MONTHLY).unwrap();
        let snap = body.into_snapshot("pro".into());
        assert!(snap.session.is_none());
        assert!(snap.weekly.is_none());
        assert_eq!(snap.monthly.as_ref().unwrap().utilization_pct, 0);
        assert_eq!(snap.monthly_models[0].name, "gpt-oss:120b");
        assert_eq!(snap.monthly_models[0].request_count, 100);
        assert_eq!(snap.monthly_models.len(), 2);
        assert_eq!(snap.activity_cost.as_deref(), Some("0.00000"));
    }

    #[test]
    fn credit_balance_projects_monthly_usage_and_exact_billing_period() {
        let body: Body = serde_json::from_str(include_str!(
            "../../tests/fixtures/ollama/balance_credits.json"
        ))
        .unwrap();
        let snap = body.into_snapshot("pro".into());
        let monthly = snap.monthly.unwrap();
        assert_eq!(monthly.utilization_pct, 25);
        assert_eq!(
            monthly.resets_at.unwrap().to_rfc3339(),
            "2026-11-08T08:00:00+00:00"
        );
        assert_eq!(monthly.window_duration, chrono::Duration::days(31));
        assert!(snap.session.is_none());
        assert!(snap.weekly.is_none());
        let credits = snap.credits.unwrap();
        assert_eq!(credits.balance, "$45.00");
        assert_eq!(credits.allowance, "$60.00");
        assert_eq!(credits.purchased.as_deref(), Some("$12.50"));
        assert!(
            snap.activity_cost.is_none(),
            "balance is not usage-history cost"
        );
    }

    #[test]
    fn legacy_balance_inverts_remaining_percent_and_keeps_resets() {
        let body: Body = serde_json::from_str(
            r#"{
          "included": {
            "session": {"remaining_percent":75,"resets_at":"2026-10-01T07:00:00Z"},
            "weekly": {"remaining_percent":40,"resets_at":"2026-10-05T00:00:00Z"}
          },
          "purchased": {"balance_usd":25}
        }"#,
        )
        .unwrap();
        let snap = body.into_snapshot("pro".into());
        let session = snap.session.unwrap();
        assert_eq!(session.utilization_pct, 25);
        assert_eq!(session.window_duration, chrono::Duration::hours(5));
        assert_eq!(
            session.resets_at.unwrap().to_rfc3339(),
            "2026-10-01T07:00:00+00:00"
        );
        assert_eq!(snap.weekly.unwrap().utilization_pct, 60);
        assert!(snap.monthly.is_none());
        assert!(snap.credits.is_none());
    }

    #[test]
    fn zero_allowance_is_not_a_fabricated_percentage() {
        let body: Body = serde_json::from_str(
            r#"{
          "included": {"balance_usd":0,"allowance_usd":0,
            "period":{"from":"2026-10-01T00:00:00Z","until":"2026-11-01T00:00:00Z"}}
        }"#,
        )
        .unwrap();
        let snap = body.into_snapshot("free".into());
        assert!(snap.monthly.is_none());
        assert_eq!(snap.credits.unwrap().balance, "$0.00");
    }

    #[test]
    fn usage_history_and_unknown_shapes_are_rejected() {
        for payload in [
            r#"{"range":"7d","totals":{"request_count":213,"usage_usd":0.2},"buckets":[]}"#,
            r#"{}"#,
            r#"{"included":{}}"#,
            r#"{"included":{"balance_usd":45}}"#,
        ] {
            assert!(serde_json::from_str::<Body>(payload).is_err(), "{payload}");
        }
    }

    #[test]
    fn clamps_over_full_and_negative_usage() {
        assert_eq!(LegacyBody::pct(Some(1.4)), 100);
        assert_eq!(LegacyBody::pct(Some(-0.2)), 0);
        assert_eq!(LegacyBody::pct(None), 0);
        assert_eq!(LegacyBody::pct(Some(0.5)), 50);
    }
}