ai-usagebar 1.19.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
//! Read kiro-cli's own local SQLite state — `~/.local/share/kiro-cli/data.sqlite3`
//! — for the AWS SSO OIDC token and CodeWhisperer profile ARN it already
//! obtained via `kiro-cli login`. Opened read-only and never written back to:
//! it's kiro-cli's own live WAL-mode database (the `-wal`/`-shm` sidecar files
//! sit next to it), the same "not ours to lock for writing" treatment
//! `cursor::db` gives Cursor's `state.vscdb`.
//!
//! Two tables matter:
//! - `auth_kv` — `kirocli:odic:token` (access/refresh token pair, RFC3339
//!   expiry) and `kirocli:odic:device-registration` (the OAuth client
//!   id/secret kiro-cli registered for itself, needed to refresh the token —
//!   see `oauth.rs`).
//! - `state` — `api.codewhisperer.profile`, the resolved IAM Identity Center
//!   profile ARN. Empty for AWS Builder ID accounts with no IdC profile; the
//!   API accepts an empty `profileArn`.

use std::path::{Path, PathBuf};

use chrono::{DateTime, Utc};
use rusqlite::{Connection, OpenFlags};
use serde::Deserialize;
use sha1::{Digest, Sha1};

use crate::error::{AppError, Result};

const TOKEN_KEY: &str = "kirocli:odic:token";
const DEVICE_REGISTRATION_KEY: &str = "kirocli:odic:device-registration";
const PROFILE_STATE_KEY: &str = "api.codewhisperer.profile";
const LOGIN_HINT: &str = "kiro-cli login";

/// Default location of kiro-cli's local database. Verified on Linux
/// (`~/.local/share/kiro-cli/data.sqlite3`, i.e. `directories::BaseDirs::data_dir()`
/// joined with `kiro-cli/data.sqlite3`). macOS/Windows follow the same
/// `directories` convention every other local-file vendor here uses, but are
/// unverified against a real kiro-cli install on those platforms.
pub fn default_db_path() -> Result<PathBuf> {
    let base = directories::BaseDirs::new().ok_or_else(|| {
        AppError::Other("could not resolve the platform data directory (no HOME?)".into())
    })?;
    Ok(base.data_dir().join("kiro-cli").join("data.sqlite3"))
}

#[derive(Debug, Deserialize)]
struct TokenRow {
    access_token: String,
    refresh_token: String,
    expires_at: String,
    region: String,
}

#[derive(Debug, Deserialize)]
struct DeviceRegistrationRow {
    client_id: String,
    client_secret: String,
}

#[derive(Debug, Default, Deserialize)]
struct ProfileRow {
    #[serde(default)]
    arn: String,
}

/// Everything a `GetUsageLimits` call needs, read out of kiro-cli's own local
/// state in one pass.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KiroCredentials {
    pub access_token: String,
    pub refresh_token: String,
    pub expires_at: DateTime<Utc>,
    pub region: String,
    pub client_id: String,
    pub client_secret: String,
    /// CodeWhisperer profile ARN; empty for accounts with no IdC profile.
    pub profile_arn: String,
    /// Non-plaintext cache identity for the signed-in account. Never
    /// displayed — exists so a cache written for one account is not served
    /// for another after a `kiro-cli login` switch.
    pub account_key: String,
}

pub fn read_credentials(path: &Path) -> Result<KiroCredentials> {
    if !path.exists() {
        return Err(AppError::Credentials(format!(
            "Kiro CLI database not found at {}. Run `{LOGIN_HINT}`, then try again.",
            path.display()
        )));
    }
    let conn =
        Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY).map_err(|e| {
            AppError::Credentials(format!(
                "could not open Kiro CLI database at {}: {e}",
                path.display()
            ))
        })?;

    let token: TokenRow = read_auth_kv(&conn, TOKEN_KEY)?;
    let device: DeviceRegistrationRow = read_auth_kv(&conn, DEVICE_REGISTRATION_KEY)?;
    let profile_arn = read_optional_state::<ProfileRow>(&conn, PROFILE_STATE_KEY)?
        .map(|p| p.arn)
        .unwrap_or_default();

    for (label, value) in [
        ("access token", token.access_token.as_str()),
        ("refresh token", token.refresh_token.as_str()),
        ("client id", device.client_id.as_str()),
        ("client secret", device.client_secret.as_str()),
    ] {
        if value.trim().is_empty() {
            return Err(AppError::Credentials(format!(
                "Kiro CLI {label} is empty. Run `{LOGIN_HINT}` again."
            )));
        }
    }
    super::oauth::validate_region(&token.region)?;

    let expires_at = DateTime::parse_from_rfc3339(&token.expires_at)
        .map_err(|e| {
            AppError::Credentials(format!(
                "Kiro CLI token has an unreadable expiry ({:?}): {e}. Run `{LOGIN_HINT}` again.",
                token.expires_at
            ))
        })?
        .with_timezone(&Utc);

    let account_key = account_key(&profile_arn, &token.region, &token.refresh_token);

    Ok(KiroCredentials {
        access_token: token.access_token,
        refresh_token: token.refresh_token,
        expires_at,
        region: token.region,
        client_id: device.client_id,
        client_secret: device.client_secret,
        profile_arn,
        account_key,
    })
}

fn read_auth_kv<T: for<'de> Deserialize<'de>>(conn: &Connection, key: &str) -> Result<T> {
    let raw: String =
        match conn.query_row("SELECT value FROM auth_kv WHERE key = ?1", [key], |row| {
            row.get(0)
        }) {
            Ok(raw) => raw,
            Err(rusqlite::Error::QueryReturnedNoRows) => {
                return Err(AppError::Credentials(format!(
                    "no Kiro CLI `{key}` entry found. Run `{LOGIN_HINT}`, then try again."
                )));
            }
            Err(e) => {
                return Err(AppError::Credentials(format!(
                    "could not read Kiro CLI `{key}` entry: {e}"
                )));
            }
        };
    serde_json::from_str(&raw)
        .map_err(|e| AppError::Credentials(format!("Kiro CLI `{key}` entry is malformed: {e}")))
}

fn read_optional_state<T: for<'de> Deserialize<'de>>(
    conn: &Connection,
    key: &str,
) -> Result<Option<T>> {
    let raw: String = match conn.query_row("SELECT value FROM state WHERE key = ?1", [key], |row| {
        row.get(0)
    }) {
        Ok(raw) => raw,
        Err(rusqlite::Error::QueryReturnedNoRows) => return Ok(None),
        Err(e) => {
            return Err(AppError::Credentials(format!(
                "could not read Kiro CLI `{key}` entry: {e}"
            )));
        }
    };
    serde_json::from_str(&raw)
        .map(Some)
        .map_err(|e| AppError::Credentials(format!("Kiro CLI `{key}` entry is malformed: {e}")))
}

fn account_key(profile_arn: &str, region: &str, refresh_token: &str) -> String {
    let mut digest = Sha1::new();
    if profile_arn.is_empty() {
        // Builder ID/social accounts have no profile ARN. The refresh token is
        // the only account-specific value available locally; fingerprint it so
        // two accounts in the same region cannot share cache state.
        digest.update(b"builder-id\0");
        digest.update(region.as_bytes());
        digest.update(b"\0");
        digest.update(refresh_token.as_bytes());
    } else {
        digest.update(b"profile\0");
        digest.update(profile_arn.as_bytes());
    }
    let digest = digest.finalize();
    let mut encoded = String::with_capacity(digest.len() * 2);
    const HEX: &[u8; 16] = b"0123456789abcdef";
    for byte in digest {
        encoded.push(HEX[(byte >> 4) as usize] as char);
        encoded.push(HEX[(byte & 0x0f) as usize] as char);
    }
    encoded
}

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

    fn seed_db(
        path: &Path,
        token: Option<&str>,
        device: Option<&str>,
        profile: Option<&str>,
    ) -> Connection {
        let conn = Connection::open(path).unwrap();
        conn.execute("CREATE TABLE auth_kv (key TEXT, value TEXT)", [])
            .unwrap();
        conn.execute("CREATE TABLE state (key TEXT, value TEXT)", [])
            .unwrap();
        if let Some(t) = token {
            conn.execute(
                "INSERT INTO auth_kv (key, value) VALUES (?1, ?2)",
                rusqlite::params![TOKEN_KEY, t],
            )
            .unwrap();
        }
        if let Some(d) = device {
            conn.execute(
                "INSERT INTO auth_kv (key, value) VALUES (?1, ?2)",
                rusqlite::params![DEVICE_REGISTRATION_KEY, d],
            )
            .unwrap();
        }
        if let Some(p) = profile {
            conn.execute(
                "INSERT INTO state (key, value) VALUES (?1, ?2)",
                rusqlite::params![PROFILE_STATE_KEY, p],
            )
            .unwrap();
        }
        conn
    }

    fn token_json() -> String {
        serde_json::json!({
            "access_token": "AT",
            "refresh_token": "RT",
            "expires_at": "2030-01-01T00:00:00Z",
            "region": "us-east-1",
        })
        .to_string()
    }

    fn device_json() -> String {
        serde_json::json!({"client_id": "CID", "client_secret": "CSECRET"}).to_string()
    }

    fn profile_json() -> String {
        serde_json::json!({"arn": "arn:aws:codewhisperer:us-east-1:123:profile/ABC"}).to_string()
    }

    #[test]
    fn default_db_path_ends_with_kiro_cli_data_sqlite3() {
        let p = default_db_path().unwrap();
        assert!(
            p.ends_with(std::path::Path::new("kiro-cli").join("data.sqlite3")),
            "{}",
            p.display()
        );
    }

    #[test]
    fn missing_file_is_a_credentials_error_naming_the_path() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("data.sqlite3");
        let err = read_credentials(&path).unwrap_err();
        match err {
            AppError::Credentials(m) => assert!(m.contains(&path.display().to_string())),
            other => panic!("expected Credentials error, got {other:?}"),
        }
    }

    #[test]
    fn reads_token_device_and_profile_out_of_the_db() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("data.sqlite3");
        seed_db(
            &path,
            Some(&token_json()),
            Some(&device_json()),
            Some(&profile_json()),
        );
        let creds = read_credentials(&path).unwrap();
        assert_eq!(creds.access_token, "AT");
        assert_eq!(creds.refresh_token, "RT");
        assert_eq!(creds.region, "us-east-1");
        assert_eq!(creds.client_id, "CID");
        assert_eq!(creds.client_secret, "CSECRET");
        assert_eq!(
            creds.profile_arn,
            "arn:aws:codewhisperer:us-east-1:123:profile/ABC"
        );
        assert_eq!(creds.expires_at.to_rfc3339(), "2030-01-01T00:00:00+00:00");
    }

    #[test]
    fn missing_profile_row_falls_back_to_empty_arn_not_an_error() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("data.sqlite3");
        seed_db(&path, Some(&token_json()), Some(&device_json()), None);
        let creds = read_credentials(&path).unwrap();
        assert_eq!(creds.profile_arn, "");
    }

    #[test]
    fn missing_token_row_is_a_credentials_error() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("data.sqlite3");
        seed_db(&path, None, Some(&device_json()), Some(&profile_json()));
        assert!(matches!(
            read_credentials(&path),
            Err(AppError::Credentials(_))
        ));
    }

    #[test]
    fn missing_device_registration_is_a_credentials_error() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("data.sqlite3");
        seed_db(&path, Some(&token_json()), None, Some(&profile_json()));
        assert!(matches!(
            read_credentials(&path),
            Err(AppError::Credentials(_))
        ));
    }

    #[test]
    fn malformed_token_json_is_a_credentials_error() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("data.sqlite3");
        seed_db(&path, Some("not json"), Some(&device_json()), None);
        assert!(matches!(
            read_credentials(&path),
            Err(AppError::Credentials(_))
        ));
    }

    #[test]
    fn malformed_profile_json_is_not_treated_as_a_builder_id_account() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("data.sqlite3");
        seed_db(
            &path,
            Some(&token_json()),
            Some(&device_json()),
            Some("not json"),
        );
        let err = read_credentials(&path).unwrap_err();
        assert!(matches!(err, AppError::Credentials(_)));
        assert!(err.to_string().contains(PROFILE_STATE_KEY));
    }

    #[test]
    fn unparseable_expiry_is_a_credentials_error() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("data.sqlite3");
        let bad_token = serde_json::json!({
            "access_token": "AT", "refresh_token": "RT",
            "expires_at": "not-a-date", "region": "us-east-1",
        })
        .to_string();
        seed_db(&path, Some(&bad_token), Some(&device_json()), None);
        assert!(matches!(
            read_credentials(&path),
            Err(AppError::Credentials(_))
        ));
    }

    #[test]
    fn account_key_is_stable_and_account_specific() {
        let dir = TempDir::new().unwrap();
        let path1 = dir.path().join("a.sqlite3");
        seed_db(
            &path1,
            Some(&token_json()),
            Some(&device_json()),
            Some(&profile_json()),
        );
        let one = read_credentials(&path1).unwrap();

        let path2 = dir.path().join("b.sqlite3");
        seed_db(
            &path2,
            Some(&token_json()),
            Some(&device_json()),
            Some(&profile_json()),
        );
        let one_again = read_credentials(&path2).unwrap();
        assert_eq!(one.account_key, one_again.account_key);
        assert_eq!(one.account_key, "8b4d254cee50ffb1116ee414d5b91b93239e7507");

        let other_profile =
            serde_json::json!({"arn": "arn:aws:codewhisperer:us-east-1:999:profile/XYZ"})
                .to_string();
        let path3 = dir.path().join("c.sqlite3");
        seed_db(
            &path3,
            Some(&token_json()),
            Some(&device_json()),
            Some(&other_profile),
        );
        let two = read_credentials(&path3).unwrap();
        assert_ne!(one.account_key, two.account_key);
        assert!(!one.account_key.contains("ABC"));
    }

    #[test]
    fn builder_id_accounts_in_the_same_region_have_distinct_keys() {
        let dir = TempDir::new().unwrap();
        let path1 = dir.path().join("a.sqlite3");
        seed_db(&path1, Some(&token_json()), Some(&device_json()), None);
        let one = read_credentials(&path1).unwrap();

        let path2 = dir.path().join("b.sqlite3");
        let other_token = serde_json::json!({
            "access_token": "AT2",
            "refresh_token": "OTHER-RT",
            "expires_at": "2030-01-01T00:00:00Z",
            "region": "us-east-1",
        })
        .to_string();
        seed_db(&path2, Some(&other_token), Some(&device_json()), None);
        let two = read_credentials(&path2).unwrap();

        assert_ne!(one.account_key, two.account_key);
        assert!(!one.account_key.contains("RT"));
        assert!(!two.account_key.contains("OTHER-RT"));
    }

    #[test]
    fn unsafe_region_is_rejected_before_endpoint_construction() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("data.sqlite3");
        let token = serde_json::json!({
            "access_token": "AT",
            "refresh_token": "RT",
            "expires_at": "2030-01-01T00:00:00Z",
            "region": "evil.example/#",
        })
        .to_string();
        seed_db(&path, Some(&token), Some(&device_json()), None);

        let err = read_credentials(&path).unwrap_err();
        assert!(matches!(err, AppError::Credentials(_)));
        assert!(err.to_string().contains("region"));
    }
}