ai-usagebar 1.24.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
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
//! Omarchy-style provider summary for the macOS status item.
//!
//! The report owns provider names and metric values. This module only chooses
//! the visible entry and quota window, so the native host can repaint without
//! asking the WebView to be open.

use serde_json::Value;

pub const HIGHEST_PROVIDER: &str = "highest";

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum UsageWindow {
    #[default]
    Auto,
    Session,
    Weekly,
    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
    Monthly,
}

impl UsageWindow {
    /// Read by the macOS host's set-menu-bar-window IPC handler; the Linux
    /// test build never calls it.
    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
    pub fn parse(value: &str) -> Self {
        match value {
            "session" => Self::Session,
            "weekly" => Self::Weekly,
            "monthly" => Self::Monthly,
            _ => Self::Auto,
        }
    }

    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Auto => "auto",
            Self::Session => "session",
            Self::Weekly => "weekly",
            Self::Monthly => "monthly",
        }
    }
}

pub fn selected_id<'a>(
    payload: &'a Value,
    remembered: &str,
    window: UsageWindow,
    visible: Option<&[String]>,
) -> Option<&'a str> {
    let entries = eligible_entries(payload, visible);
    if entries.is_empty() {
        return None;
    }
    let has_id = |id: &str| {
        entries
            .iter()
            .any(|entry| entry.get("id").and_then(Value::as_str) == Some(id))
    };
    if !remembered.is_empty() && remembered != HIGHEST_PROVIDER && has_id(remembered) {
        return entries
            .iter()
            .find(|entry| entry.get("id").and_then(Value::as_str) == Some(remembered))
            .and_then(|entry| entry.get("id").and_then(Value::as_str));
    }
    let mut best: Option<(&Value, f64)> = None;
    for entry in &entries {
        let Some(percent) = highest_percent(entry, window) else {
            continue;
        };
        if best.is_none_or(|(_, previous)| percent > previous) {
            best = Some((entry, percent));
        }
    }
    if let Some((entry, _)) = best {
        return entry.get("id").and_then(Value::as_str);
    }
    if let Some(primary) = payload.get("primary").and_then(Value::as_str) {
        if has_id(primary) {
            return Some(primary);
        }
        if let Some(entry) = entries.iter().find(|entry| {
            entry
                .get("id")
                .and_then(Value::as_str)
                .is_some_and(|id| id.split('@').next() == Some(primary))
        }) {
            return entry.get("id").and_then(Value::as_str);
        }
    }
    entries
        .iter()
        .find(|entry| is_ready(entry))
        .or_else(|| entries.first())?
        .get("id")
        .and_then(Value::as_str)
}

pub fn next_id(
    payload: &Value,
    remembered: &str,
    window: UsageWindow,
    visible: Option<&[String]>,
) -> Option<String> {
    let ids: Vec<&str> = eligible_entries(payload, visible)
        .into_iter()
        .filter_map(|entry| entry.get("id").and_then(Value::as_str))
        .collect();
    if ids.is_empty() {
        return None;
    }
    let current = selected_id(payload, remembered, window, visible).unwrap_or(ids[0]);
    let index = ids.iter().position(|id| *id == current).unwrap_or(0);
    Some(ids[(index + 1) % ids.len()].to_owned())
}

pub fn title(
    payload: &Value,
    remembered: &str,
    show_all: bool,
    show_value: bool,
    window: UsageWindow,
    visible: Option<&[String]>,
) -> String {
    let chips: Vec<String> = displayed_entries(payload, remembered, show_all, window, visible)
        .into_iter()
        .map(|entry| chip(entry, show_value, window))
        .filter(|chip| !chip.is_empty())
        .collect();
    chips.join("   ")
}

/// Rendered by the macOS status item only; the Linux test build never calls it.
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
pub fn tooltip(
    payload: &Value,
    remembered: &str,
    show_all: bool,
    window: UsageWindow,
    visible: Option<&[String]>,
) -> String {
    let lines: Vec<String> = displayed_entries(payload, remembered, show_all, window, visible)
        .into_iter()
        .map(|entry| {
            let name = entry
                .get("display_name")
                .or_else(|| entry.get("name"))
                .and_then(Value::as_str)
                .unwrap_or("AI Usage");
            let stale = if entry.get("stale").and_then(Value::as_bool) == Some(true) {
                " · cached"
            } else {
                ""
            };
            format!(
                "{} · {}{stale}",
                safe_text(name, 48),
                headline(entry, window)
            )
        })
        .collect();
    if lines.is_empty() {
        "AI Usage".into()
    } else {
        lines.join("\n")
    }
}

fn displayed_entries<'a>(
    payload: &'a Value,
    remembered: &'a str,
    show_all: bool,
    window: UsageWindow,
    visible: Option<&[String]>,
) -> Vec<&'a Value> {
    let entries = eligible_entries(payload, visible);
    if show_all {
        let ready: Vec<&Value> = entries
            .iter()
            .copied()
            .filter(|entry| entry.get("status").and_then(Value::as_str) != Some("error"))
            .collect();
        if !ready.is_empty() {
            return ready;
        }
    }
    let selected = selected_id(payload, remembered, window, visible);
    entries
        .into_iter()
        .filter(|entry| entry.get("id").and_then(Value::as_str) == selected)
        .collect()
}

fn eligible_entries<'a>(payload: &'a Value, visible: Option<&[String]>) -> Vec<&'a Value> {
    let Some(entries) = payload.get("entries").and_then(Value::as_array) else {
        return Vec::new();
    };
    entries
        .iter()
        .filter(|entry| {
            let Some(id) = entry.get("id").and_then(Value::as_str) else {
                return false;
            };
            visible.is_none_or(|ids| ids.iter().any(|allowed| allowed == id))
        })
        .collect()
}

fn is_ready(entry: &Value) -> bool {
    entry.get("status").and_then(Value::as_str) != Some("error")
        && !entry
            .get("error")
            .and_then(Value::as_str)
            .is_some_and(|error| !error.is_empty())
}

fn highest_percent(entry: &Value, window: UsageWindow) -> Option<f64> {
    if !is_ready(entry) {
        return None;
    }
    best_metric(entry, window, false)?.get("percent")?.as_f64()
}

fn best_metric(entry: &Value, window: UsageWindow, fallback: bool) -> Option<&Value> {
    let metrics: Vec<&Value> = entry
        .get("sections")?
        .as_array()?
        .iter()
        .filter(|section| section.get("type").and_then(Value::as_str) == Some("metric"))
        .collect();
    let candidates: Vec<&Value> = metrics
        .iter()
        .copied()
        .filter(|metric| matches_window(metric, window))
        .collect();
    let pool = if candidates.is_empty() {
        if !fallback {
            return None;
        }
        &metrics
    } else {
        &candidates
    };
    pool.iter().copied().max_by(|a, b| {
        let a = a.get("percent").and_then(Value::as_f64).unwrap_or(0.0);
        let b = b.get("percent").and_then(Value::as_f64).unwrap_or(0.0);
        a.total_cmp(&b)
    })
}

fn chip(entry: &Value, show_value: bool, window: UsageWindow) -> String {
    let id = entry.get("id").and_then(Value::as_str).unwrap_or("");
    let name = entry
        .get("display_name")
        .or_else(|| entry.get("name"))
        .or_else(|| entry.get("short_name"))
        .and_then(Value::as_str)
        .filter(|name| !name.is_empty())
        .unwrap_or_else(|| id.split('@').next().unwrap_or(""));
    let name = safe_text(name, 24);
    if name.is_empty() || !show_value {
        return name;
    }
    let summary = headline(entry, window);
    format!("{name} {summary}")
}

fn headline(entry: &Value, window: UsageWindow) -> String {
    if entry.get("status").and_then(Value::as_str) == Some("error")
        || entry
            .get("error")
            .and_then(Value::as_str)
            .is_some_and(|error| !error.is_empty())
    {
        return "!".into();
    }
    if let Some(metric) = best_metric(entry, window, true) {
        if metric.get("headline").and_then(Value::as_str) == Some("value")
            && let Some(value) = metric.get("value").and_then(Value::as_str)
            && !value.is_empty()
        {
            return safe_text(value, 24);
        }
        if let Some(percent) = metric.get("percent") {
            return format!("{percent}%");
        }
    }
    if let Some(sections) = entry.get("sections").and_then(Value::as_array) {
        for section in sections {
            if section.get("type").and_then(Value::as_str) != Some("text") {
                continue;
            }
            let label = section
                .get("label")
                .and_then(Value::as_str)
                .unwrap_or("")
                .to_ascii_lowercase();
            if ["balance", "available", "spend", "prepaid"]
                .iter()
                .any(|term| label.contains(term))
                && let Some(value) = section.get("value").and_then(Value::as_str)
                && !value.is_empty()
            {
                return safe_text(value, 24);
            }
        }
    }
    "Ready".into()
}

fn matches_window(metric: &Value, window: UsageWindow) -> bool {
    if window == UsageWindow::Auto {
        return true;
    }
    let seconds = metric.get("window_secs").and_then(Value::as_u64);
    if let Some(18_000 | 604_800) = seconds {
        return match window {
            UsageWindow::Session => seconds == Some(18_000),
            UsageWindow::Weekly => seconds == Some(604_800),
            _ => false,
        };
    }
    let label = metric
        .get("label")
        .and_then(Value::as_str)
        .unwrap_or("")
        .to_ascii_lowercase();
    match window {
        UsageWindow::Session => ["5h", "5-hour", "session", "rolling"]
            .iter()
            .any(|term| label.contains(term)),
        UsageWindow::Weekly => ["week", "7d", "7-day"]
            .iter()
            .any(|term| label.contains(term)),
        UsageWindow::Monthly => ["month", "30d", "30-day", "spend (mo)"]
            .iter()
            .any(|term| label.contains(term)),
        UsageWindow::Auto => true,
    }
}

fn safe_text(value: &str, limit: usize) -> String {
    value
        .chars()
        .filter(|c| !c.is_control())
        .take(limit)
        .collect::<String>()
        .trim()
        .to_owned()
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn remembers_a_provider_and_cycles_in_report_order() {
        let report = json!({"primary":"claude", "entries":[
            {"id":"claude", "short_name":"cld"},
            {"id":"openai@work", "short_name":"cdx"},
            {"id":"cursor", "short_name":"cur"}
        ]});
        assert_eq!(
            selected_id(&report, "openai@work", UsageWindow::Auto, None),
            Some("openai@work")
        );
        assert_eq!(
            next_id(&report, "openai@work", UsageWindow::Auto, None),
            Some("cursor".into())
        );
        assert_eq!(
            next_id(&report, "cursor", UsageWindow::Auto, None),
            Some("claude".into())
        );
        assert_eq!(
            selected_id(&report, "missing", UsageWindow::Auto, None),
            Some("claude")
        );
    }

    #[test]
    fn pinned_window_falls_back_and_value_headlines_stay_values() {
        let report = json!({"primary":"openai", "entries":[
            {"id":"openai", "short_name":"cdx", "sections":[
                {"type":"metric", "label":"5h", "percent":20, "window_secs":18000},
                {"type":"metric", "label":"Weekly", "percent":80, "window_secs":604800}
            ]},
            {"id":"openrouter", "short_name":"opr", "sections":[
                {"type":"metric", "label":"Balance", "percent":40, "headline":"value", "value":"$12.50"}
            ]}
        ]});
        assert_eq!(
            title(&report, "", false, true, UsageWindow::Auto, None),
            "cdx 80%"
        );
        assert_eq!(
            title(&report, "", false, true, UsageWindow::Session, None),
            "cdx 20%"
        );
        assert_eq!(
            title(
                &report,
                "openrouter",
                false,
                true,
                UsageWindow::Weekly,
                None
            ),
            "opr $12.50"
        );
        assert_eq!(
            title(&report, "", true, true, UsageWindow::Auto, None),
            "cdx 80%   opr $12.50"
        );
    }

    #[test]
    fn default_strip_shows_ready_providers_and_skips_disabled_failures() {
        let report = json!({"primary":"anthropic", "entries":[
            {"id":"anthropic", "display_name":"Claude", "status":"ready", "sections":[
                {"type":"metric", "label":"Session (5h)", "percent":17},
                {"type":"metric", "label":"Weekly (7d)", "percent":21}
            ]},
            {"id":"openai", "display_name":"Codex", "status":"ready", "sections":[
                {"type":"metric", "label":"Codex weekly", "percent":15}
            ]},
            {"id":"zai", "display_name":"Z.AI", "status":"error", "sections":[]}
        ]});
        assert_eq!(
            title(&report, "", true, true, UsageWindow::Auto, None),
            "Claude 21%   Codex 15%"
        );
        assert_eq!(
            title(&report, "", true, true, UsageWindow::Session, None),
            "Claude 17%   Codex 15%"
        );
        assert_eq!(
            title(&report, "zai", false, true, UsageWindow::Auto, None),
            "Z.AI !"
        );
    }

    #[test]
    fn highest_consumption_tracks_window_and_enabled_providers() {
        let mut report = json!({"primary":"anthropic", "entries":[
            {"id":"anthropic", "display_name":"Claude", "status":"ready", "sections":[
                {"type":"metric", "label":"Session", "percent":70, "window_secs":18000},
                {"type":"metric", "label":"Weekly", "percent":20, "window_secs":604800}
            ]},
            {"id":"openai", "display_name":"Codex", "status":"ready", "sections":[
                {"type":"metric", "label":"Session", "percent":30, "window_secs":18000},
                {"type":"metric", "label":"Weekly", "percent":80, "window_secs":604800}
            ]},
            {"id":"zai", "display_name":"Z.AI", "status":"error", "sections":[
                {"type":"metric", "label":"Session", "percent":99, "window_secs":18000}
            ]}
        ]});
        assert_eq!(
            title(
                &report,
                HIGHEST_PROVIDER,
                false,
                true,
                UsageWindow::Session,
                None
            ),
            "Claude 70%"
        );
        assert_eq!(
            title(
                &report,
                HIGHEST_PROVIDER,
                false,
                true,
                UsageWindow::Weekly,
                None
            ),
            "Codex 80%"
        );
        assert_eq!(
            title(&report, "anthropic", false, true, UsageWindow::Weekly, None),
            "Claude 20%"
        );

        let visible = vec!["anthropic".into()];
        assert_eq!(
            title(
                &report,
                HIGHEST_PROVIDER,
                false,
                true,
                UsageWindow::Weekly,
                Some(&visible)
            ),
            "Claude 20%"
        );
        report["entries"][0]["sections"][1]["percent"] = json!(90);
        assert_eq!(
            title(
                &report,
                HIGHEST_PROVIDER,
                false,
                true,
                UsageWindow::Weekly,
                None
            ),
            "Claude 90%"
        );
    }
}