fetching rate limits when calling /status

This commit is contained in:
kevin zhao
2025-11-17 10:47:28 -08:00
parent a941ae7632
commit 57c3632a9e
4 changed files with 54 additions and 2 deletions

1
codex-rs/Cargo.lock generated
View File

@@ -1470,6 +1470,7 @@ dependencies = [
"codex-ansi-escape",
"codex-app-server-protocol",
"codex-arg0",
"codex-backend-client",
"codex-common",
"codex-core",
"codex-feedback",

View File

@@ -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",

2
codex-rs/tui/config.toml Normal file
View File

@@ -0,0 +1,2 @@
[projects."/Users/zhao/code/codex"]
trust_level = "untrusted"

View File

@@ -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<RateLimitSnapshotDisplay> {
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<ModelPreset> {
let auth_mode = self.auth_manager.auth().map(|auth| auth.mode);