burnrate 0.1.14

Desktop usage monitor for Claude Code, Codex, GitHub Copilot, OpenRouter, Runpod, and AWS quotas, credits, spend, and subscription limits, with claudex-powered local usage insights.
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
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum ProviderKind {
    ClaudeCode,
    Codex,
    #[serde(rename = "openrouter", alias = "open-router")]
    OpenRouter,
    Runpod,
    Aws,
    Copilot,
}

impl ProviderKind {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            ProviderKind::ClaudeCode => "claude-code",
            ProviderKind::Codex => "codex",
            ProviderKind::OpenRouter => "openrouter",
            ProviderKind::Runpod => "runpod",
            ProviderKind::Aws => "aws",
            ProviderKind::Copilot => "copilot",
        }
    }

    /// Human-facing provider name for user-visible messages (the wire id from
    /// [`Self::as_str`] mirrors `src-ui/constants.ts` `providerLabels`).
    pub(crate) fn display_name(self) -> &'static str {
        match self {
            ProviderKind::ClaudeCode => "Claude Code",
            ProviderKind::Codex => "Codex",
            ProviderKind::OpenRouter => "OpenRouter",
            ProviderKind::Runpod => "Runpod",
            ProviderKind::Aws => "AWS",
            ProviderKind::Copilot => "GitHub Copilot",
        }
    }
}

/// GitHub Copilot subscription tier, which determines the monthly premium
/// request allowance. `Custom` defers to the account's
/// `copilot_custom_limit` for plans with negotiated or unknown quotas.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum CopilotPlan {
    Free,
    Pro,
    ProPlus,
    Business,
    Enterprise,
    Custom,
}

impl CopilotPlan {
    /// Monthly premium-request allowance per GitHub's published plan quotas.
    /// `Custom` has no built-in allowance — the account's
    /// `copilot_custom_limit` applies instead.
    pub(crate) fn monthly_limit(self) -> Option<f64> {
        match self {
            CopilotPlan::Free => Some(50.0),
            CopilotPlan::Pro | CopilotPlan::Business => Some(300.0),
            CopilotPlan::ProPlus => Some(1500.0),
            CopilotPlan::Enterprise => Some(1000.0),
            CopilotPlan::Custom => None,
        }
    }

    pub(crate) fn label(self) -> &'static str {
        match self {
            CopilotPlan::Free => "Free",
            CopilotPlan::Pro => "Pro",
            CopilotPlan::ProPlus => "Pro+",
            CopilotPlan::Business => "Business",
            CopilotPlan::Enterprise => "Enterprise",
            CopilotPlan::Custom => "Custom",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum SecretStorageMode {
    Keyring,
    Plaintext,
}

/// Release channel the in-app auto-updater follows. `Stable` tracks the
/// `releases/latest` manifest; `Nightly` follows the rolling `nightly` tag.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum UpdateChannel {
    #[default]
    Stable,
    Nightly,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct AppSettings {
    pub hide_from_dock: bool,
    /// Release channel for automatic updates. Defaults to `Stable`; older
    /// config files without this field deserialize to the default.
    #[serde(default)]
    pub update_channel: UpdateChannel,
    /// Manual tray content scale. `1.0` is native size (disabled); users can
    /// lower it to `0.5` to fit dense popovers before scrolling.
    #[serde(default = "default_tray_scale")]
    pub tray_scale: f64,
    /// Whether claudex-backed local usage insights are collected and shown.
    /// On by default; the opt-out exists because indexing builds (and keeps)
    /// `~/.claudex/index.db` from local CLI session logs.
    #[serde(default = "default_true")]
    pub local_insights: bool,
}

impl Default for AppSettings {
    fn default() -> Self {
        Self {
            hide_from_dock: true,
            update_channel: UpdateChannel::default(),
            tray_scale: default_tray_scale(),
            local_insights: true,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct AccountConfig {
    pub id: String,
    pub provider: ProviderKind,
    pub label: String,
    pub enabled: bool,
    pub auto_detected: bool,
    pub credential_path: Option<String>,
    pub endpoint_override: Option<String>,
    pub secret_storage: SecretStorageMode,
    pub keyring_account: Option<String>,
    pub plaintext_secret: Option<String>,
    /// Email address associated with the signed-in account, when known.
    #[serde(default)]
    pub email: Option<String>,
    /// Per-account CLI home (`CLAUDE_CONFIG_DIR` / `CODEX_HOME`). `None` means the
    /// system default location (`~/.claude` / `~/.codex`).
    #[serde(default)]
    pub config_dir: Option<String>,
    /// AWS profile name. `None` uses the SDK default credential chain and
    /// current `AWS_PROFILE` environment, without storing static keys.
    #[serde(default)]
    pub aws_profile: Option<String>,
    /// AWS region for SDK configuration. Cost Explorer itself is global; when
    /// unset Burnrate defaults to `us-east-1` so the SDK has a region.
    #[serde(default)]
    pub aws_region: Option<String>,
    /// Optional monthly budget in USD used to calculate remaining/warning state.
    #[serde(default)]
    pub aws_monthly_budget_usd: Option<f64>,
    /// User-configurable Cost Explorer categories shown as sub-buckets.
    #[serde(default)]
    pub aws_categories: Vec<AwsCategoryConfig>,
    /// GitHub Copilot plan, which sets the monthly premium request allowance.
    /// `None` shows usage without a limit.
    #[serde(default)]
    pub copilot_plan: Option<CopilotPlan>,
    /// Monthly premium request allowance when `copilot_plan` is `Custom`.
    #[serde(default)]
    pub copilot_custom_limit: Option<f64>,
    /// Global display order; lower sorts first. `None` is legacy/unset and sorts
    /// after explicitly ordered accounts.
    #[serde(default)]
    pub order_index: Option<i64>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

impl AccountConfig {
    /// The per-account CLI home, or `None` for the system-default account.
    pub(crate) fn cli_config_dir(&self) -> Option<&str> {
        self.config_dir.as_deref()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct AccountInput {
    pub id: Option<String>,
    pub provider: ProviderKind,
    pub label: String,
    pub enabled: bool,
    pub endpoint_override: Option<String>,
    pub secret_storage: SecretStorageMode,
    pub secret: Option<String>,
    #[serde(default)]
    pub aws_profile: Option<String>,
    #[serde(default)]
    pub aws_region: Option<String>,
    #[serde(default)]
    pub aws_monthly_budget_usd: Option<f64>,
    #[serde(default)]
    pub aws_categories: Vec<AwsCategoryConfig>,
    #[serde(default)]
    pub copilot_plan: Option<CopilotPlan>,
    #[serde(default)]
    pub copilot_custom_limit: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct AccountView {
    pub id: String,
    pub provider: ProviderKind,
    pub label: String,
    pub enabled: bool,
    pub auto_detected: bool,
    pub credential_path: Option<String>,
    pub endpoint_override: Option<String>,
    pub secret_storage: SecretStorageMode,
    pub has_secret: bool,
    #[serde(default)]
    pub email: Option<String>,
    #[serde(default)]
    pub config_dir: Option<String>,
    #[serde(default)]
    pub aws_profile: Option<String>,
    #[serde(default)]
    pub aws_region: Option<String>,
    #[serde(default)]
    pub aws_monthly_budget_usd: Option<f64>,
    #[serde(default)]
    pub aws_categories: Vec<AwsCategoryConfig>,
    #[serde(default)]
    pub copilot_plan: Option<CopilotPlan>,
    #[serde(default)]
    pub copilot_custom_limit: Option<f64>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct AwsCategoryConfig {
    pub id: String,
    pub label: String,
    #[serde(default = "default_true")]
    pub enabled: bool,
    pub filter: AwsCostFilter,
    #[serde(default)]
    pub group_by: Option<AwsGroupBy>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase")]
pub(crate) enum AwsCostFilter {
    Dimension { key: String, values: Vec<String> },
    Tag { key: String, values: Vec<String> },
    CostCategory { key: String, values: Vec<String> },
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct AwsGroupBy {
    pub kind: AwsGroupByKind,
    pub key: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum AwsGroupByKind {
    Dimension,
    Tag,
    CostCategory,
}

fn default_true() -> bool {
    true
}

fn default_tray_scale() -> f64 {
    1.0
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct UsageSnapshot {
    pub account_id: String,
    pub provider: ProviderKind,
    pub label: String,
    pub status: SnapshotStatus,
    #[serde(default)]
    pub email: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subscription: Option<SubscriptionSnapshot>,
    #[serde(default)]
    pub usage_buckets: Vec<UsageBucketSnapshot>,
    pub quota: Option<QuotaSnapshot>,
    pub message: Option<String>,
    pub fetched_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum SnapshotStatus {
    Healthy,
    Warning,
    Exhausted,
    Error,
    Stale,
    NotConfigured,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum SubscriptionPlan {
    Free,
    Pro,
    Max,
    Team,
    Enterprise,
    Unknown,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct SubscriptionSnapshot {
    pub plan: SubscriptionPlan,
    pub plan_label: String,
    pub rate_limit_tier: Option<String>,
    pub extra_usage_enabled: Option<bool>,
    pub source: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct UsageBucketSnapshot {
    pub id: String,
    pub label: String,
    pub window: Option<String>,
    pub used: f64,
    pub limit: Option<f64>,
    pub remaining: Option<f64>,
    pub unit: String,
    pub reset_at: Option<DateTime<Utc>>,
    pub status: SnapshotStatus,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct QuotaSnapshot {
    pub used: f64,
    pub limit: Option<f64>,
    pub remaining: Option<f64>,
    pub unit: String,
    pub reset_at: Option<DateTime<Utc>>,
}

/// Emitted as `burnrate-login-complete` when an interactive sign-in succeeds.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct LoginComplete {
    pub id: String,
    pub account: AccountView,
}

/// Emitted as `burnrate-login-failed` when a sign-in errors or is cancelled.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct LoginFailed {
    pub id: String,
    pub error: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct DashboardState {
    pub accounts: Vec<AccountView>,
    pub snapshots: Vec<UsageSnapshot>,
    pub tray_summary: TraySummary,
    pub settings: AppSettings,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct TraySummary {
    pub label: String,
    pub status: SnapshotStatus,
    pub critical_count: usize,
    pub warning_count: usize,
    pub updated_at: DateTime<Utc>,
}

/// claudex-backed local usage metrics, aggregated per provider. Local session
/// history cannot be split between multiple accounts of one provider, so these
/// are provider-level — the UI says so rather than implying per-account
/// precision.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct LocalUsageReport {
    /// False when insights are disabled, claudex has no data, or collection
    /// failed; `message` carries the human-readable reason.
    pub available: bool,
    pub message: Option<String>,
    pub providers: Vec<ProviderLocalUsage>,
    pub generated_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ProviderLocalUsage {
    pub provider: ProviderKind,
    pub today_cost_usd: f64,
    pub today_sessions: i64,
    pub week_cost_usd: f64,
    pub month_cost_usd: f64,
    /// Linear month-end extrapolation of `month_cost_usd`; `None` when there
    /// is no spend yet.
    pub projected_month_cost_usd: Option<f64>,
    pub month_input_tokens: i64,
    pub month_output_tokens: i64,
    pub top_model: Option<String>,
    pub model_distribution: Vec<LocalModelUsage>,
    pub top_projects: Vec<LocalProjectCost>,
    /// Daily cost buckets, ascending by date (sparkline source).
    pub daily: Vec<LocalDailyUsage>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct LocalDailyUsage {
    /// ISO date (`YYYY-MM-DD`).
    pub date: String,
    pub cost_usd: f64,
    pub sessions: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct LocalModelUsage {
    pub model: String,
    pub sessions: i64,
    pub cost_usd: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct LocalProjectCost {
    pub project: String,
    pub sessions: i64,
    pub cost_usd: f64,
}

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

    #[test]
    fn provider_kind_uses_stable_config_names() {
        assert_eq!(ProviderKind::ClaudeCode.as_str(), "claude-code");
        assert_eq!(ProviderKind::Codex.as_str(), "codex");
        assert_eq!(ProviderKind::OpenRouter.as_str(), "openrouter");
        assert_eq!(ProviderKind::Runpod.as_str(), "runpod");
        assert_eq!(ProviderKind::Aws.as_str(), "aws");
        assert_eq!(ProviderKind::Copilot.as_str(), "copilot");
        assert_eq!(
            serde_json::to_string(&ProviderKind::OpenRouter).unwrap(),
            "\"openrouter\""
        );
        assert_eq!(
            serde_json::from_str::<ProviderKind>("\"open-router\"").unwrap(),
            ProviderKind::OpenRouter
        );
        assert_eq!(
            serde_json::to_string(&ProviderKind::Copilot).unwrap(),
            "\"copilot\""
        );
    }

    #[test]
    fn copilot_plans_use_stable_wire_names() {
        assert_eq!(
            serde_json::to_string(&CopilotPlan::ProPlus).unwrap(),
            "\"pro-plus\""
        );
        assert_eq!(
            serde_json::from_str::<CopilotPlan>("\"enterprise\"").unwrap(),
            CopilotPlan::Enterprise
        );
    }

    #[test]
    fn settings_without_local_insights_field_default_to_enabled() {
        let settings: AppSettings = serde_json::from_str(r#"{"hideFromDock":false}"#).unwrap();
        assert!(settings.local_insights);
        assert!(AppSettings::default().local_insights);
    }

    #[test]
    fn default_settings_hide_dock_for_tray_first_launch() {
        assert!(AppSettings::default().hide_from_dock);
    }

    #[test]
    fn default_update_channel_is_stable() {
        assert_eq!(AppSettings::default().update_channel, UpdateChannel::Stable);
        assert_eq!(
            serde_json::to_string(&UpdateChannel::Nightly).unwrap(),
            "\"nightly\""
        );
    }

    #[test]
    fn default_tray_scale_is_native_size() {
        assert_eq!(AppSettings::default().tray_scale, 1.0);
    }

    #[test]
    fn settings_without_channel_field_default_to_stable() {
        // Config files written before the updater shipped have no
        // `updateChannel`; they must still deserialize.
        let settings: AppSettings = serde_json::from_str(r#"{"hideFromDock":false}"#).unwrap();
        assert!(!settings.hide_from_dock);
        assert_eq!(settings.update_channel, UpdateChannel::Stable);
        assert_eq!(settings.tray_scale, 1.0);
    }

    #[test]
    fn subscription_plans_use_stable_wire_names() {
        assert_eq!(
            serde_json::to_string(&SubscriptionPlan::Max).unwrap(),
            "\"max\""
        );
        assert_eq!(
            serde_json::to_string(&SubscriptionPlan::Unknown).unwrap(),
            "\"unknown\""
        );
    }
}