Skip to main content

ai_usagebar/openai/
fetch.rs

1//! Orchestrate: read ~/.codex/auth.json → maybe refresh → fetch usage → cache.
2//!
3//! Mirrors `anthropic::fetch::fetch_snapshot` but for the Codex OAuth flow.
4
5use std::path::Path;
6use std::time::Duration;
7
8use chrono::Utc;
9
10use crate::cache::{Cache, MAX_STALE, acquire_lock_async};
11use crate::error::{AppError, Result};
12use crate::usage::OpenAiSnapshot;
13
14use super::creds::{self, Tokens};
15use super::oauth;
16use super::types::UsageResponse;
17
18pub const USAGE_URL: &str = "https://chatgpt.com/backend-api/wham/usage";
19const HTTP_TIMEOUT: Duration = Duration::from_secs(10);
20const REFRESH_TIMEOUT: Duration = Duration::from_secs(25);
21const LOCK_TIMEOUT: Duration = Duration::from_secs(45);
22
23#[derive(Debug, Clone)]
24pub struct Endpoints {
25    pub usage: String,
26    pub token: String,
27}
28
29impl Default for Endpoints {
30    fn default() -> Self {
31        Self {
32            usage: USAGE_URL.into(),
33            token: oauth::TOKEN_URL.into(),
34        }
35    }
36}
37
38#[derive(Debug, Clone)]
39pub struct FetchOutcome {
40    pub snapshot: OpenAiSnapshot,
41    pub stale: bool,
42    pub last_error: Option<(u16, String)>,
43    pub cache_age: Option<Duration>,
44}
45
46pub async fn fetch_snapshot(
47    client: &reqwest::Client,
48    creds_path: &Path,
49    cache: &Cache,
50    endpoints: &Endpoints,
51    cache_ttl: Duration,
52) -> Result<FetchOutcome> {
53    cache.ensure_dir()?;
54    let _lock = acquire_lock_async(&cache.lock_path(), LOCK_TIMEOUT).await?;
55
56    let mut auth = creds::read_from(creds_path)?;
57    let plan_hint = auth.tokens.plan_type_from_id_token();
58
59    // Corrupt fresh cache falls through to a live fetch rather than returning
60    // an all-zero snapshot.
61    if let Some(bytes) = cache.fresh_payload(cache_ttl)?
62        && let Ok(outcome) = reuse(bytes, cache, false, plan_hint.as_deref())
63    {
64        return Ok(outcome);
65    }
66
67    // Maybe refresh — Codex CLI doesn't always populate expires_at, so we use
68    // the id_token's exp claim.
69    let now = Utc::now().timestamp();
70    if oauth::needs_refresh(auth.tokens.expires_at_secs(), now) {
71        match tokio::time::timeout(
72            REFRESH_TIMEOUT,
73            oauth::refresh(client, &endpoints.token, &auth.tokens.refresh_token),
74        )
75        .await
76        {
77            Ok(Ok(rr)) => {
78                auth.tokens.access_token = rr.access_token;
79                // A rotated refresh token exists only in memory until it is
80                // persisted; losing it silently logs the user out on the next
81                // run. See the matching comment in `anthropic::fetch`.
82                let rotated = rr.refresh_token.is_some();
83                if let Some(rt) = rr.refresh_token {
84                    auth.tokens.refresh_token = rt;
85                }
86                if let Some(id) = rr.id_token {
87                    auth.tokens.id_token = id;
88                }
89                // Expiry is normally read from the id_token's exp claim, so a
90                // refresh that returns no new id_token would leave the old
91                // (expired) claim in place and make every later run refresh
92                // again. Record the response's own `expires_in` as the explicit
93                // `expires_at` so the expiry is known either way.
94                if let Some(secs) = rr.expires_in
95                    && let Some(dt) = chrono::DateTime::from_timestamp(now + secs as i64, 0)
96                {
97                    auth.tokens.expires_at = Some(dt.to_rfc3339());
98                }
99                if let Err(e) = creds::write_back(creds_path, &auth)
100                    && rotated
101                {
102                    let msg = format!(
103                        "refreshed token could not be saved ({e}); the rotated \
104                         refresh token is lost — re-run `codex login`"
105                    );
106                    cache.write_last_error(0, &msg);
107                    return handle_auth_failure(cache, plan_hint.as_deref(), false);
108                }
109            }
110            Ok(Err(AppError::Http { status, body })) => {
111                cache.write_last_error(status, &body);
112                return handle_auth_failure(cache, plan_hint.as_deref(), false);
113            }
114            Ok(Err(e)) if e.is_transient() => {
115                return handle_auth_failure(cache, plan_hint.as_deref(), true);
116            }
117            Ok(Err(e)) => {
118                cache.write_last_error(0, &e.to_string());
119                return handle_auth_failure(cache, plan_hint.as_deref(), false);
120            }
121            Err(_) => return handle_auth_failure(cache, plan_hint.as_deref(), true),
122        }
123    }
124
125    match tokio::time::timeout(
126        HTTP_TIMEOUT,
127        fetch_usage(client, &endpoints.usage, &auth.tokens),
128    )
129    .await
130    {
131        Ok(Ok(bytes)) => {
132            cache.write_payload(&bytes)?;
133            let snap = parse_payload(&bytes, plan_hint.as_deref())?;
134            Ok(FetchOutcome {
135                snapshot: snap,
136                stale: false,
137                last_error: None,
138                cache_age: Some(Duration::ZERO),
139            })
140        }
141        Ok(Err(AppError::Http { status, body })) => {
142            cache.mark_stale();
143            cache.write_last_error(status, &body);
144            fallback(cache, plan_hint.as_deref(), Some((status, body)))
145        }
146        Ok(Err(e)) if e.is_transient() => fallback_silent(cache, plan_hint.as_deref()),
147        Ok(Err(e)) => {
148            cache.mark_stale();
149            cache.write_last_error(0, &e.to_string());
150            fallback(cache, plan_hint.as_deref(), Some((0, e.to_string())))
151        }
152        Err(_) => fallback_silent(cache, plan_hint.as_deref()),
153    }
154}
155
156fn reuse(
157    bytes: Vec<u8>,
158    cache: &Cache,
159    stale: bool,
160    plan_hint: Option<&str>,
161) -> Result<FetchOutcome> {
162    let snap = parse_payload(&bytes, plan_hint)?;
163    Ok(FetchOutcome {
164        snapshot: snap,
165        stale,
166        last_error: cache.read_last_error(),
167        cache_age: cache.payload_age(),
168    })
169}
170
171fn fallback(
172    cache: &Cache,
173    plan_hint: Option<&str>,
174    last_error: Option<(u16, String)>,
175) -> Result<FetchOutcome> {
176    let Some(bytes) = cache.fallback_payload(MAX_STALE)? else {
177        return Err(AppError::Other("openai: no usable cache".into()));
178    };
179    let mut out = reuse(bytes, cache, true, plan_hint)?;
180    out.last_error = last_error;
181    Ok(out)
182}
183
184fn fallback_silent(cache: &Cache, plan_hint: Option<&str>) -> Result<FetchOutcome> {
185    let Some(bytes) = cache.fallback_payload(MAX_STALE)? else {
186        return Err(AppError::Transport(
187            "openai: no cache and network unreachable".into(),
188        ));
189    };
190    reuse(bytes, cache, true, plan_hint)
191}
192
193fn handle_auth_failure(
194    cache: &Cache,
195    plan_hint: Option<&str>,
196    transient: bool,
197) -> Result<FetchOutcome> {
198    let Some(bytes) = cache.fallback_payload(MAX_STALE)? else {
199        return if transient {
200            Err(AppError::Transport(
201                "openai: no cache and refresh failed transiently".into(),
202            ))
203        } else {
204            Err(AppError::Credentials(
205                "openai: token refresh failed; run `codex login` to re-auth".into(),
206            ))
207        };
208    };
209    reuse(bytes, cache, true, plan_hint)
210}
211
212fn parse_payload(bytes: &[u8], plan_hint: Option<&str>) -> Result<OpenAiSnapshot> {
213    let r: UsageResponse = serde_json::from_slice(bytes)?;
214    Ok(r.into_snapshot(plan_hint))
215}
216
217async fn fetch_usage(client: &reqwest::Client, url: &str, t: &Tokens) -> Result<Vec<u8>> {
218    let mut req = client
219        .get(url)
220        .header("Authorization", format!("Bearer {}", t.access_token))
221        .header("User-Agent", "codex-cli");
222    if let Some(aid) = t.account_id.as_deref() {
223        req = req.header("ChatGPT-Account-Id", aid);
224    }
225    let resp = req.send().await?;
226    let status = resp.status();
227    let bytes = crate::vendor::read_body_capped(resp, crate::vendor::MAX_BODY_BYTES).await?;
228
229    if !status.is_success() {
230        let body: String = String::from_utf8_lossy(&bytes).chars().take(200).collect();
231        return Err(AppError::Http {
232            status: status.as_u16(),
233            body,
234        });
235    }
236    let _: UsageResponse = serde_json::from_slice(&bytes)
237        .map_err(|e| AppError::Schema(format!("openai usage response: {e}")))?;
238    Ok(bytes)
239}
240
241#[cfg(test)]
242mod tests {
243    use super::*;
244    use base64::Engine;
245    use std::io::Write;
246    use tempfile::{NamedTempFile, TempDir};
247
248    fn fake_jwt(claims: serde_json::Value) -> String {
249        let h = base64::engine::general_purpose::URL_SAFE_NO_PAD
250            .encode(br#"{"alg":"none","typ":"JWT"}"#);
251        let p =
252            base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(claims.to_string().as_bytes());
253        format!("{h}.{p}.sig")
254    }
255
256    fn future_creds() -> NamedTempFile {
257        // exp 1h in the future.
258        let exp = Utc::now().timestamp() + 3600;
259        let jwt = fake_jwt(serde_json::json!({
260            "exp": exp,
261            "https://api.openai.com/auth": {"chatgpt_plan_type": "plus"}
262        }));
263        let body = format!(
264            r#"{{"tokens":{{"access_token":"AT","refresh_token":"RT","id_token":"{jwt}",
265                "account_id":"acc"}}}}"#
266        );
267        let mut f = NamedTempFile::new().unwrap();
268        f.write_all(body.as_bytes()).unwrap();
269        f.flush().unwrap();
270        f
271    }
272
273    fn cache_fixture() -> (TempDir, Cache) {
274        let td = TempDir::new().unwrap();
275        let c = Cache::at(td.path().join("openai"));
276        c.ensure_dir().unwrap();
277        (td, c)
278    }
279
280    #[tokio::test]
281    async fn live_200_returns_snapshot_with_plan_from_id_token() {
282        let mut server = mockito::Server::new_async().await;
283        server
284            .mock("GET", "/backend-api/wham/usage")
285            .with_status(200)
286            .with_body(
287                r#"{"plan_type":"plus","rate_limit":{
288                "primary_window":{"used_percent":1,"limit_window_seconds":18000,"reset_at":1779597324},
289                "secondary_window":{"used_percent":0,"limit_window_seconds":604800,"reset_at":1780184124}
290            }}"#,
291            )
292            .create_async()
293            .await;
294        let (_td, cache) = cache_fixture();
295        let creds = future_creds();
296        let client = reqwest::Client::new();
297        let endpoints = Endpoints {
298            usage: format!("{}/backend-api/wham/usage", server.url()),
299            token: format!("{}/oauth/token", server.url()),
300        };
301        let out = fetch_snapshot(
302            &client,
303            creds.path(),
304            &cache,
305            &endpoints,
306            Duration::from_secs(0),
307        )
308        .await
309        .unwrap();
310        assert_eq!(out.snapshot.plan, "ChatGPT Plus");
311        assert_eq!(out.snapshot.session.utilization_pct, 1);
312        assert!(!out.stale);
313    }
314
315    #[tokio::test]
316    async fn corrupt_fresh_cache_refetches_instead_of_showing_an_empty_snapshot() {
317        // `reuse` used to swallow a parse failure into `empty(plan_hint)` and
318        // serve that all-zero snapshot for the rest of the TTL.
319        let mut server = mockito::Server::new_async().await;
320        server
321            .mock("GET", "/backend-api/wham/usage")
322            .with_status(200)
323            .with_body(
324                r#"{"plan_type":"pro","rate_limit":{"primary_window":{"used_percent":37,"limit_window_seconds":18000}}}"#,
325            )
326            .create_async()
327            .await;
328
329        let (_td, cache) = cache_fixture();
330        cache.write_payload(b"{ truncated").unwrap();
331
332        let creds = future_creds();
333        let client = reqwest::Client::new();
334        let endpoints = Endpoints {
335            usage: format!("{}/backend-api/wham/usage", server.url()),
336            token: format!("{}/oauth/token", server.url()),
337        };
338        // A long TTL: the payload IS fresh, it is simply unusable.
339        let out = fetch_snapshot(
340            &client,
341            creds.path(),
342            &cache,
343            &endpoints,
344            Duration::from_secs(3600),
345        )
346        .await
347        .unwrap();
348        assert_eq!(out.snapshot.session.utilization_pct, 37);
349        assert!(!out.stale);
350    }
351
352    #[tokio::test]
353    async fn http_500_falls_back_to_cache_when_present() {
354        let mut server = mockito::Server::new_async().await;
355        server
356            .mock("GET", "/backend-api/wham/usage")
357            .with_status(500)
358            .with_body(r#"{"error":{"message":"upstream"}}"#)
359            .create_async()
360            .await;
361        let (_td, cache) = cache_fixture();
362        cache
363            .write_payload(
364                br#"{"plan_type":"pro","rate_limit":{"primary_window":{"used_percent":50,"limit_window_seconds":18000}}}"#,
365            )
366            .unwrap();
367        let creds = future_creds();
368        let client = reqwest::Client::new();
369        let endpoints = Endpoints {
370            usage: format!("{}/backend-api/wham/usage", server.url()),
371            token: format!("{}/oauth/token", server.url()),
372        };
373        let out = fetch_snapshot(
374            &client,
375            creds.path(),
376            &cache,
377            &endpoints,
378            Duration::from_secs(0),
379        )
380        .await
381        .unwrap();
382        assert!(out.stale);
383        assert_eq!(out.snapshot.session.utilization_pct, 50);
384        assert_eq!(out.last_error.as_ref().map(|(c, _)| *c), Some(500));
385    }
386}