kai-tool 0.1.10

CLI helpers for AI coding, Codex credentials, and git worktree management.
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
use std::io::{self, IsTerminal, Write};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::Result;
use capulus::ui::{Color, RenderTarget, stderr_render_target, stdout_render_target};
use chrono::{DateTime, Local, Utc};
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
use serde::Serialize;

use super::quota;

const FIELD_SEPARATOR: &str = " · ";
const ACTIVE_LABEL: &str = "active";
const QUOTA_BAR_SEGMENTS: usize = 16;

#[derive(Debug, Serialize)]
pub struct ListView {
    pub active: Option<String>,
    pub next: Option<String>,
    pub accounts: Vec<AccountView>,
}

#[derive(Debug, Serialize)]
pub struct AccountView {
    pub email: String,
    pub active: bool,
    pub plan: Option<String>,
    pub access_expires_at: Option<i64>,
    pub last_refresh: Option<String>,
    pub status: AccountStatus,
    pub quota: QuotaStatus,
}

#[derive(Debug, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum AccountStatus {
    Ready,
    Invalid { error: String },
}

#[derive(Debug, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum QuotaStatus {
    Loading,
    Available {
        #[serde(flatten)]
        snapshot: quota::Snapshot,
    },
    Unavailable {
        error: String,
    },
}

pub struct LiveList {
    progress: MultiProgress,
    bars: Vec<ProgressBar>,
    email_width: usize,
}

impl AccountView {
    pub fn set_quota(&mut self, result: Result<quota::Snapshot>) {
        self.quota = match result {
            Ok(snapshot) => QuotaStatus::Available { snapshot },
            Err(err) => QuotaStatus::Unavailable {
                error: format!("{err:#}"),
            },
        };
    }
}

impl LiveList {
    pub fn start(view: &ListView) -> Option<Self> {
        if !io::stdout().is_terminal() || view.accounts.is_empty() {
            return None;
        }
        Some(Self::with_draw_target(view, ProgressDrawTarget::stdout()))
    }

    fn with_draw_target(view: &ListView, target: ProgressDrawTarget) -> Self {
        let progress = MultiProgress::with_draw_target(target);
        progress.set_move_cursor(true);
        let email_width = email_width(view);
        let bars = (0..view.accounts.len())
            .map(|_| progress.add(ProgressBar::new_spinner()))
            .collect::<Vec<_>>();
        let live = Self {
            progress,
            bars,
            email_width,
        };
        for (bar, account) in live.bars.iter().zip(&view.accounts) {
            render_live_account(bar, account, email_width);
        }
        live
    }

    pub fn update(&self, index: usize, account: &AccountView) {
        if let Some(bar) = self.bars.get(index) {
            render_live_account(bar, account, self.email_width);
        }
    }

    pub fn finish(self, view: &ListView) -> Result<()> {
        self.finish_with(view, |view| print_list(view, false))
    }

    fn finish_with(
        self,
        view: &ListView,
        write_final: impl FnOnce(&ListView) -> Result<()>,
    ) -> Result<()> {
        for (bar, account) in self.bars.iter().zip(&view.accounts) {
            if matches!(account.quota, QuotaStatus::Loading) {
                render_live_account(bar, account, self.email_width);
            }
        }
        self.progress.clear()?;
        drop(self.bars);
        drop(self.progress);
        write_final(view)
    }
}

pub fn print_list(view: &ListView, json: bool) -> Result<()> {
    io::stdout().write_all(render_list(view, json)?.as_bytes())?;
    Ok(())
}

fn render_list(view: &ListView, json: bool) -> Result<String> {
    if json {
        return Ok(format!("{}\n", serde_json::to_string_pretty(view)?));
    }
    if view.accounts.is_empty() {
        return Ok(concat!(
            "No Codex accounts enrolled.\n",
            "Run `kai cred add <email>` to import the current account or enroll a new one.\n",
        )
        .to_owned());
    }

    let email_width = email_width(view);
    let mut lines = view
        .accounts
        .iter()
        .map(|account| render_account(account, email_width))
        .collect::<Vec<_>>();
    lines.push(String::new());
    lines.push(render_summary(view));
    let mut output = lines.join("\n");
    output.push('\n');
    Ok(output)
}

pub fn print_quota(snapshot: &quota::Snapshot) {
    capulus::ui::detail(&format!(
        "Quota: {}",
        render_quota(snapshot, &stderr_render_target())
    ));
}

fn render_live_account(bar: &ProgressBar, account: &AccountView, email_width: usize) {
    bar.disable_steady_tick();
    match (&account.status, &account.quota) {
        (AccountStatus::Ready, QuotaStatus::Loading) => {
            bar.set_style(
                ProgressStyle::with_template("{msg} · {spinner:.cyan} loading quota")
                    .expect("valid quota loading progress template")
                    .tick_strings(&["", "", "", "", "", "", "", "", "", ""]),
            );
            bar.set_message(render_account_base(
                account,
                email_width,
                &stdout_render_target(),
            ));
            bar.enable_steady_tick(Duration::from_millis(80));
        }
        _ => {
            bar.set_style(
                ProgressStyle::with_template("{msg}").expect("valid quota result template"),
            );
            bar.finish_with_message(render_account(account, email_width));
        }
    }
}

fn render_account(account: &AccountView, email_width: usize) -> String {
    let target = stdout_render_target();
    let base = render_account_base(account, email_width, &target);
    match (&account.status, &account.quota) {
        (AccountStatus::Ready, QuotaStatus::Available { snapshot }) => {
            format!(
                "{base}{}{}",
                FIELD_SEPARATOR,
                render_quota(snapshot, &target)
            )
        }
        (AccountStatus::Ready, QuotaStatus::Loading) => {
            format!("{base}{FIELD_SEPARATOR}loading quota")
        }
        (AccountStatus::Ready, QuotaStatus::Unavailable { error }) => {
            format!("{base}{FIELD_SEPARATOR}quota unavailable: {error}")
        }
        (AccountStatus::Invalid { .. }, _) => base,
    }
}

fn render_account_base(
    account: &AccountView,
    email_width: usize,
    target: &impl RenderTarget,
) -> String {
    let (bullet, color) = match (&account.status, account.active) {
        (AccountStatus::Invalid { .. }, _) => ("×", Color::Red),
        (_, true) => ("", Color::Green),
        _ => ("", Color::Cyan),
    };
    let mut fields = vec![if account.active {
        target.paint(ACTIVE_LABEL, Color::Green)
    } else {
        " ".repeat(ACTIVE_LABEL.len())
    }];
    if let Some(plan) = &account.plan {
        fields.push(plan.to_ascii_uppercase());
    }
    match &account.status {
        AccountStatus::Ready => {
            if let Some(expires_at) = account.access_expires_at {
                fields.push(render_access_expiry(expires_at, target));
            }
        }
        AccountStatus::Invalid { error } => {
            fields.push(target.paint(&format!("invalid: {error}"), Color::Red));
        }
    }
    format!(
        "{} {:email_width$}{}{}",
        target.paint(bullet, color),
        account.email,
        if fields.is_empty() {
            ""
        } else {
            FIELD_SEPARATOR
        },
        fields.join(FIELD_SEPARATOR),
    )
}

fn render_quota(snapshot: &quota::Snapshot, target: &impl RenderTarget) -> String {
    render_quota_values(
        snapshot.remaining_percent,
        snapshot.resets_at,
        snapshot.window_seconds,
        target,
    )
}

fn render_quota_values(
    remaining_percent: f64,
    resets_at: i64,
    window_seconds: Option<i64>,
    target: &impl RenderTarget,
) -> String {
    let rounded_percent = remaining_percent.clamp(0.0, 100.0).round() as u64;
    format!(
        "{} [{}] {rounded_percent:>3}% remaining · resets {}",
        quota_window_label(window_seconds),
        render_quota_bar(remaining_percent, target),
        format_reset_datetime(resets_at),
    )
}

fn render_quota_bar(remaining_percent: f64, target: &impl RenderTarget) -> String {
    let rounded_percent = remaining_percent.clamp(0.0, 100.0).round() as u64;
    let fill = rounded_percent as f64 / 100.0 * QUOTA_BAR_SEGMENTS as f64;
    let entirely_filled = fill as usize;
    let current = (fill > 0.0 && entirely_filled < QUOTA_BAR_SEGMENTS) as usize;
    let completed = format!("{}{}", "".repeat(entirely_filled), "".repeat(current));
    format!(
        "{}{}",
        target.paint(&completed, quota_color(rounded_percent)),
        target.paint(
            &"".repeat(
                QUOTA_BAR_SEGMENTS
                    .saturating_sub(entirely_filled)
                    .saturating_sub(current)
            ),
            Color::Blue
        )
    )
}

fn quota_window_label(window_seconds: Option<i64>) -> String {
    match window_seconds {
        Some(seconds) if seconds % 86_400 == 0 => format!("{}d quota", seconds / 86_400),
        Some(seconds) if seconds % 3_600 == 0 => format!("{}h quota", seconds / 3_600),
        Some(seconds) if seconds % 60 == 0 => format!("{}m quota", seconds / 60),
        _ => "quota".to_owned(),
    }
}

fn quota_color(remaining_percent: u64) -> Color {
    if remaining_percent <= 20 {
        Color::Red
    } else if remaining_percent <= 50 {
        Color::Yellow
    } else {
        Color::Green
    }
}

fn format_reset_datetime(resets_at: i64) -> String {
    DateTime::<Utc>::from_timestamp(resets_at, 0)
        .map(|datetime| {
            datetime
                .with_timezone(&Local)
                .format("%Y-%m-%d %H:%M %Z")
                .to_string()
        })
        .unwrap_or_else(|| format!("Unix timestamp {resets_at}"))
}

fn email_width(view: &ListView) -> usize {
    view.accounts
        .iter()
        .map(|account| account.email.chars().count())
        .max()
        .unwrap_or_default()
}

fn render_summary(view: &ListView) -> String {
    let noun = if view.accounts.len() == 1 {
        "account"
    } else {
        "accounts"
    };
    match &view.next {
        Some(next) => format!("{} {noun} enrolled · next: {next}", view.accounts.len()),
        None => format!("{} {noun} enrolled", view.accounts.len()),
    }
}

fn render_access_expiry(expires_at: i64, target: &impl RenderTarget) -> String {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs() as i64;
    let remaining = expires_at - now;
    if remaining <= 0 {
        return target.paint("access refresh due", Color::Yellow);
    }
    if remaining < 120 {
        return "access <2m".to_owned();
    }
    if remaining < 7200 {
        return format!("access {}m", remaining / 60);
    }
    format!("access {}h", remaining / 3600)
}

#[cfg(test)]
mod tests {
    use capulus::ui::TextEffect;
    use indicatif::InMemoryTerm;

    use super::*;

    struct PlainTarget;

    impl RenderTarget for PlainTarget {
        fn style(&self, text: &str, _color: Option<Color>, _effect: TextEffect) -> String {
            text.to_owned()
        }
    }

    #[test]
    fn quota_bar_and_summary_show_remaining_capacity() {
        assert_eq!(render_quota_bar(75.0, &PlainTarget), "████████████▓░░░");
        assert!(
            render_quota_values(75.0, 2_000_000_000, Some(18_000), &PlainTarget)
                .contains("5h quota")
        );
        assert!(
            render_quota_values(75.0, 2_000_000_000, Some(18_000), &PlainTarget)
                .contains(" 75% remaining")
        );
    }

    #[test]
    fn quota_bar_clamps_backend_percentages() {
        assert_eq!(render_quota_bar(-5.0, &PlainTarget), "░░░░░░░░░░░░░░░░");
        assert_eq!(render_quota_bar(105.0, &PlainTarget), "████████████████");
    }

    #[test]
    fn inactive_accounts_reserve_the_active_column() {
        let account = |email: &str, active| AccountView {
            email: email.to_owned(),
            active,
            plan: Some("pro".to_owned()),
            access_expires_at: None,
            last_refresh: None,
            status: AccountStatus::Ready,
            quota: QuotaStatus::Loading,
        };
        let inactive = account("one@example.com", false);
        let active = account("two@example.com", true);

        assert_eq!(
            render_account_base(&inactive, inactive.email.len(), &PlainTarget),
            "○ one@example.com ·        · PRO"
        );
        assert_eq!(
            render_account_base(&active, active.email.len(), &PlainTarget),
            "● two@example.com · active · PRO"
        );
    }

    #[test]
    fn finished_live_rows_are_replaced_with_static_lines() {
        let view = ListView {
            active: Some("alice@example.com".to_owned()),
            next: None,
            accounts: vec![AccountView {
                email: "alice@example.com".to_owned(),
                active: true,
                plan: Some("pro".to_owned()),
                access_expires_at: None,
                last_refresh: None,
                status: AccountStatus::Ready,
                quota: QuotaStatus::Available {
                    snapshot: quota::Snapshot {
                        remaining_percent: 75.0,
                        resets_at: 2_000_000_000,
                        window_seconds: Some(18_000),
                    },
                },
            }],
        };
        let terminal = InMemoryTerm::new(10, 160);
        let live = LiveList::with_draw_target(
            &view,
            ProgressDrawTarget::term_like(Box::new(terminal.clone())),
        );
        let account = render_account(&view.accounts[0], email_width(&view));
        assert_eq!(terminal.contents(), account);

        let mut final_output = None;
        live.finish_with(&view, |view| {
            final_output = Some(render_list(view, false)?);
            Ok(())
        })
        .unwrap();

        assert_eq!(terminal.contents(), "");
        assert_eq!(
            final_output.unwrap(),
            format!("{account}\n\n1 account enrolled\n")
        );
    }
}