From 57c3632a9e79a6208ea82b0b5115a1cf35ccabfa Mon Sep 17 00:00:00 2001 From: kevin zhao Date: Mon, 17 Nov 2025 10:47:28 -0800 Subject: [PATCH] fetching rate limits when calling /status --- codex-rs/Cargo.lock | 1 + codex-rs/tui/Cargo.toml | 1 + codex-rs/tui/config.toml | 2 ++ codex-rs/tui/src/chatwidget.rs | 52 ++++++++++++++++++++++++++++++++-- 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 codex-rs/tui/config.toml diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index cca5ca0b43..2b0080e049 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -1470,6 +1470,7 @@ dependencies = [ "codex-ansi-escape", "codex-app-server-protocol", "codex-arg0", + "codex-backend-client", "codex-common", "codex-core", "codex-feedback", diff --git a/codex-rs/tui/Cargo.toml b/codex-rs/tui/Cargo.toml index b524d8bfd4..b8b2ae2623 100644 --- a/codex-rs/tui/Cargo.toml +++ b/codex-rs/tui/Cargo.toml @@ -29,6 +29,7 @@ clap = { workspace = true, features = ["derive"] } codex-ansi-escape = { workspace = true } codex-app-server-protocol = { workspace = true } codex-arg0 = { workspace = true } +codex-backend-client = { workspace = true } codex-common = { workspace = true, features = [ "cli", "elapsed", diff --git a/codex-rs/tui/config.toml b/codex-rs/tui/config.toml new file mode 100644 index 0000000000..ca4c341c5d --- /dev/null +++ b/codex-rs/tui/config.toml @@ -0,0 +1,2 @@ +[projects."/Users/zhao/code/codex"] +trust_level = "untrusted" diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 79781f3352..475e73898f 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -2,7 +2,10 @@ use std::collections::HashMap; use std::collections::VecDeque; use std::path::PathBuf; use std::sync::Arc; +use std::time::Duration; +use codex_app_server_protocol::AuthMode; +use codex_backend_client::Client as BackendClient; use codex_core::config::Config; use codex_core::config::types::Notifications; use codex_core::git_info::current_branch_name; @@ -1727,16 +1730,61 @@ impl ChatWidget { } else { (&default_usage, Some(&default_usage)) }; + let rate_limit_snapshot = self + .fetch_rate_limits_from_usage() + .or_else(|| self.rate_limit_snapshot.clone()); + + if let Some(snapshot) = rate_limit_snapshot.clone() { + self.rate_limit_snapshot = Some(snapshot); + } + + let now = Local::now(); self.add_to_history(crate::status::new_status_output( &self.config, self.auth_manager.as_ref(), total_usage, context_usage, &self.conversation_id, - self.rate_limit_snapshot.as_ref(), - Local::now(), + rate_limit_snapshot.as_ref(), + now, )); } + fn fetch_rate_limits_from_usage(&self) -> Option { + let auth = self.auth_manager.auth()?; + if auth.mode != AuthMode::ChatGPT { + return None; + } + + let base_url = self.config.chatgpt_base_url.clone(); + let auth = auth; + let result = tokio::task::block_in_place(|| { + tokio::runtime::Handle::current().block_on(async { + let fetch = async { + let client = BackendClient::from_auth(base_url, &auth).await?; + client.get_rate_limits().await + }; + tokio::time::timeout(Duration::from_secs(1), fetch).await + }) + }); + + match result { + Ok(Ok(snapshot)) => Some(crate::status::rate_limit_snapshot_display( + &snapshot, + Local::now(), + )), + Ok(Err(err)) => { + tracing::debug!( + error = ?err, + "failed to fetch rate limits from /usage within timeout" + ); + None + } + Err(_) => { + tracing::debug!("timed out fetching rate limits from /usage"); + None + } + } + } fn lower_cost_preset(&self) -> Option { let auth_mode = self.auth_manager.auth().map(|auth| auth.mode);