mirror of
https://github.com/openai/codex.git
synced 2026-10-20 17:38:45 +00:00
flipping rate limit status bar to match chat.com/codex/settings/usage <img width="848" height="420" alt="Screenshot 2025-11-10 at 4 53 41 PM" src="https://github.com/user-attachments/assets/e326db3f-4405-412d-9e62-337282ec9a35" />
154 lines
4.8 KiB
Rust
154 lines
4.8 KiB
Rust
use crate::chatwidget::get_limits_duration;
|
|
|
|
use super::helpers::format_reset_timestamp;
|
|
use chrono::DateTime;
|
|
use chrono::Duration as ChronoDuration;
|
|
use chrono::Local;
|
|
use chrono::Utc;
|
|
use codex_core::protocol::RateLimitSnapshot;
|
|
use codex_core::protocol::RateLimitWindow;
|
|
|
|
const STATUS_LIMIT_BAR_SEGMENTS: usize = 20;
|
|
const STATUS_LIMIT_BAR_FILLED: &str = "█";
|
|
const STATUS_LIMIT_BAR_EMPTY: &str = "░";
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub(crate) struct StatusRateLimitRow {
|
|
pub label: String,
|
|
pub percent_used: f64,
|
|
pub resets_at: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub(crate) enum StatusRateLimitData {
|
|
Available(Vec<StatusRateLimitRow>),
|
|
Stale(Vec<StatusRateLimitRow>),
|
|
Missing,
|
|
}
|
|
|
|
pub(crate) const RATE_LIMIT_STALE_THRESHOLD_MINUTES: i64 = 15;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub(crate) struct RateLimitWindowDisplay {
|
|
pub used_percent: f64,
|
|
pub resets_at: Option<String>,
|
|
pub window_minutes: Option<i64>,
|
|
}
|
|
|
|
impl RateLimitWindowDisplay {
|
|
fn from_window(window: &RateLimitWindow, captured_at: DateTime<Local>) -> Self {
|
|
let resets_at = window
|
|
.resets_at
|
|
.and_then(|seconds| DateTime::<Utc>::from_timestamp(seconds, 0))
|
|
.map(|dt| dt.with_timezone(&Local))
|
|
.map(|dt| format_reset_timestamp(dt, captured_at));
|
|
|
|
Self {
|
|
used_percent: window.used_percent,
|
|
resets_at,
|
|
window_minutes: window.window_minutes,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub(crate) struct RateLimitSnapshotDisplay {
|
|
pub captured_at: DateTime<Local>,
|
|
pub primary: Option<RateLimitWindowDisplay>,
|
|
pub secondary: Option<RateLimitWindowDisplay>,
|
|
}
|
|
|
|
pub(crate) fn rate_limit_snapshot_display(
|
|
snapshot: &RateLimitSnapshot,
|
|
captured_at: DateTime<Local>,
|
|
) -> RateLimitSnapshotDisplay {
|
|
RateLimitSnapshotDisplay {
|
|
captured_at,
|
|
primary: snapshot
|
|
.primary
|
|
.as_ref()
|
|
.map(|window| RateLimitWindowDisplay::from_window(window, captured_at)),
|
|
secondary: snapshot
|
|
.secondary
|
|
.as_ref()
|
|
.map(|window| RateLimitWindowDisplay::from_window(window, captured_at)),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn compose_rate_limit_data(
|
|
snapshot: Option<&RateLimitSnapshotDisplay>,
|
|
now: DateTime<Local>,
|
|
) -> StatusRateLimitData {
|
|
match snapshot {
|
|
Some(snapshot) => {
|
|
let mut rows = Vec::with_capacity(2);
|
|
|
|
if let Some(primary) = snapshot.primary.as_ref() {
|
|
let label: String = primary
|
|
.window_minutes
|
|
.map(get_limits_duration)
|
|
.unwrap_or_else(|| "5h".to_string());
|
|
let label = capitalize_first(&label);
|
|
rows.push(StatusRateLimitRow {
|
|
label: format!("{label} limit"),
|
|
percent_used: primary.used_percent,
|
|
resets_at: primary.resets_at.clone(),
|
|
});
|
|
}
|
|
|
|
if let Some(secondary) = snapshot.secondary.as_ref() {
|
|
let label: String = secondary
|
|
.window_minutes
|
|
.map(get_limits_duration)
|
|
.unwrap_or_else(|| "weekly".to_string());
|
|
let label = capitalize_first(&label);
|
|
rows.push(StatusRateLimitRow {
|
|
label: format!("{label} limit"),
|
|
percent_used: secondary.used_percent,
|
|
resets_at: secondary.resets_at.clone(),
|
|
});
|
|
}
|
|
|
|
let is_stale = now.signed_duration_since(snapshot.captured_at)
|
|
> ChronoDuration::minutes(RATE_LIMIT_STALE_THRESHOLD_MINUTES);
|
|
|
|
if rows.is_empty() {
|
|
StatusRateLimitData::Available(vec![])
|
|
} else if is_stale {
|
|
StatusRateLimitData::Stale(rows)
|
|
} else {
|
|
StatusRateLimitData::Available(rows)
|
|
}
|
|
}
|
|
None => StatusRateLimitData::Missing,
|
|
}
|
|
}
|
|
|
|
pub(crate) fn render_status_limit_progress_bar(percent_remaining: f64) -> String {
|
|
let ratio = (percent_remaining / 100.0).clamp(0.0, 1.0);
|
|
let filled = (ratio * STATUS_LIMIT_BAR_SEGMENTS as f64).round() as usize;
|
|
let filled = filled.min(STATUS_LIMIT_BAR_SEGMENTS);
|
|
let empty = STATUS_LIMIT_BAR_SEGMENTS.saturating_sub(filled);
|
|
format!(
|
|
"[{}{}]",
|
|
STATUS_LIMIT_BAR_FILLED.repeat(filled),
|
|
STATUS_LIMIT_BAR_EMPTY.repeat(empty)
|
|
)
|
|
}
|
|
|
|
pub(crate) fn format_status_limit_summary(percent_remaining: f64) -> String {
|
|
format!("{percent_remaining:.0}% left")
|
|
}
|
|
|
|
fn capitalize_first(label: &str) -> String {
|
|
let mut chars = label.chars();
|
|
match chars.next() {
|
|
Some(first) => {
|
|
let mut capitalized = first.to_uppercase().collect::<String>();
|
|
capitalized.push_str(chars.as_str());
|
|
capitalized
|
|
}
|
|
None => String::new(),
|
|
}
|
|
}
|