Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(auth): fetch Codex account models from the ChatGPT backend
thinhlpg30163342726b
diff --git a/src-tauri/src/core/cli/auth/account.rs b/src-tauri/src/core/cli/auth/account.rsindex abe5c33cf..f011f2927 100644--- a/src-tauri/src/core/cli/auth/account.rs+++ b/src-tauri/src/core/cli/auth/account.rs@@ -604,10 +604,28 @@ async fn complete_code_login(match codex_chatgpt_account_id(&token.access_token) {Ok(account_id) => {debug_log(&format!("codex: verified chatgpt_account_id {account_id}"));- (- vec!["gpt-5-chat-latest".to_string()],- Some("openai-responses".to_string()),+ // Fetch the account's real model roster from the ChatGPT backend+ // (a Codex account token is a ChatGPT credential, not an OpenAI+ // API key, so `api.openai.com/v1/models` rejects it). Fall back+ // to a single default model so a transient roster failure never+ // hard-fails an otherwise valid login.+ let models = crate::core::cli::auth::providers::discover_codex_models(+ &token.access_token,+ &definition.default_base_url,)+ .await+ .map(|mut m| {+ if m.is_empty() {+ vec!["gpt-5-chat-latest".to_string()]+ } else {+ m+ }+ })+ .unwrap_or_else(|error| {+ debug_log(&format!("codex: model discovery failed: {error:?}"));+ vec!["gpt-5-chat-latest".to_string()]+ });+ (models, Some("openai-responses".to_string()))}Err(error) => {debug_log(&format!("codex: account validation failed: {error}"));@@ -1195,17 +1213,23 @@ mod tests {fn codex_login_persists_token_and_responses_config() {let _tmp = TempSecrets::new();with_temp_home(|_| {- // Codex does not call /v1/models - it verifies the account via the- // JWT chatgpt_account_id claim and configures the Responses API.- let (models_base_url, request) = account_models_server("200 OK", "{}");+ // Codex verifies the account via the JWT chatgpt_account_id claim+ // and fetches its real model roster from the (ChatGPT-backed)+ // `/models` endpoint served here by the mock, then configures the+ // Responses API with that roster.+ let roster = r#"{"models":[{"slug":"gpt-5.5","display_name":"GPT-5.5"},{"slug":"gpt-5.4","display_name":"GPT-5.4"}]}"#;+ let (models_base_url, request) = account_models_server("200 OK", roster);assert_eq!(complete_account_for_test(models_base_url.clone()).unwrap(),AccountProvider::Codex);- // No /v1/models discovery should have happened.- assert!(request.try_recv().is_err(), "Codex must not call /v1/models");+ // Codex does perform model discovery (against the Codex backend).+ assert!(+ request.try_recv().is_ok(),+ "Codex must call the backend /models endpoint"+ );let token = OAuthToken {access_token: codex_jwt("account-321").into(),@@ -1227,6 +1251,28 @@ mod tests {assert!(cfg.api_key.is_none());assert!(cfg.api_keys.is_empty());assert_eq!(cfg.base_url.as_deref(), Some(models_base_url.as_str()));+ assert_eq!(+ cfg.models,+ vec!["gpt-5.4".to_string(), "gpt-5.5".to_string()]+ );+ assert_eq!(cfg.api_type.as_deref(), Some("openai-responses"));+ });+ }++ #[test]+ fn codex_login_falls_back_to_default_when_roster_unavailable() {+ let _tmp = TempSecrets::new();+ with_temp_home(|_| {+ // A failed/empty roster must not hard-fail an otherwise valid+ // login; the account persists with a single usable default model.+ let (models_base_url, _request) = account_models_server("200 OK", "{}");++ assert_eq!(+ complete_account_for_test(models_base_url.clone()).unwrap(),+ AccountProvider::Codex+ );++ let cfg = load_global_config().unwrap().get("openai").unwrap().clone();assert_eq!(cfg.models, vec!["gpt-5-chat-latest".to_string()]);assert_eq!(cfg.api_type.as_deref(), Some("openai-responses"));});@@ -1236,15 +1282,19 @@ mod tests {fn manual_codex_login_persists_token_and_responses_config() {let _tmp = TempSecrets::new();with_temp_home(|_| {- let (models_base_url, request) = account_models_server("200 OK", "{}");+ let roster = r#"{"models":[{"slug":"gpt-5.5","display_name":"GPT-5.5"},{"slug":"gpt-5.4","display_name":"GPT-5.4"}]}"#;+ let (models_base_url, request) = account_models_server("200 OK", roster);assert_eq!(complete_account_manually_for_test(models_base_url.clone()).unwrap(),AccountProvider::Codex);- // No /v1/models discovery should have happened for a Codex account.- assert!(request.try_recv().is_err(), "Codex must not call /v1/models");+ // Codex does perform model discovery (against the Codex backend).+ assert!(+ request.try_recv().is_ok(),+ "Codex must call the backend /models endpoint"+ );let token = OAuthToken {access_token: codex_jwt("account-321").into(),@@ -1266,11 +1316,15 @@ mod tests {assert!(cfg.api_key.is_none());assert!(cfg.api_keys.is_empty());assert_eq!(cfg.base_url.as_deref(), Some(models_base_url.as_str()));- assert_eq!(cfg.models, vec!["gpt-5-chat-latest".to_string()]);+ assert_eq!(+ cfg.models,+ vec!["gpt-5.4".to_string(), "gpt-5.5".to_string()]+ );assert_eq!(cfg.api_type.as_deref(), Some("openai-responses"));});}+#[test]fn account_model_discovery_unauthorized_leaves_no_account_state() {let _tmp = TempSecrets::new();diff --git a/src-tauri/src/core/cli/auth/providers.rs b/src-tauri/src/core/cli/auth/providers.rsindex 7fdc82c99..185f0f7a3 100644--- a/src-tauri/src/core/cli/auth/providers.rs+++ b/src-tauri/src/core/cli/auth/providers.rs@@ -111,6 +111,74 @@ pub(crate) async fn discover_models(Ok(parse_models(&parsed))}+/// Discover the models available to a Codex (ChatGPT account) OAuth token.+///+/// A Codex account token is a ChatGPT account credential, not an OpenAI API+/// key, so `api.openai.com/v1/models` rejects it with 401. The models are+/// instead served by ChatGPT's own backend at `chatgpt.com/backend-api/models`,+/// which the Codex provider (OAuth/accounts) hits with the same token. The+/// response shape differs from OpenAI's (`{"models":[{slug,display_name}]}`+/// instead of `{"data":[...]}`), so it is parsed separately. `base_url` is the+/// ChatGPT backend origin (injectable in tests).+pub(crate) async fn discover_codex_models(+ credential: &str,+ base_url: &str,+) -> Result<Vec<String>, LoginError> {+ // The persisted Codex default_base_url points at the OpenAI API-key+ // surface (`api.openai.com/v1`), which rejects an account token. Discovery+ // must target the ChatGPT backend instead; a test override (the mock+ // model server) is used verbatim.+ let backend = if base_url.trim_end_matches('/').ends_with("api.openai.com/v1") {+ "https://chatgpt.com/backend-api"+ } else {+ base_url+ };+ let url = format!("{}/models", backend.trim_end_matches('/'));+ let client = reqwest::Client::builder()+ .timeout(VERIFY_TIMEOUT)+ .build()+ .map_err(|e| LoginError::Unavailable(format!("could not build an HTTP client: {e}")))?;+ let response = client+ .get(&url)+ .header("Authorization", format!("Bearer {credential}"))+ .header("Accept", "application/json")+ .send()+ .await+ .map_err(|e| {+ LoginError::Unavailable(format!("could not reach the ChatGPT backend: {e}"))+ })?;+ let status = response.status();+ let body = response.text().await.unwrap_or_default();+ if !status.is_success() {+ return Err(match status.as_u16() {+ 401 | 403 => LoginError::Unauthorized,+ 429 => LoginError::RateLimited,+ 500..=599 => LoginError::Unavailable(format!(+ "the ChatGPT backend is unavailable right now (HTTP {status})."+ )),+ _ => LoginError::Unavailable(format!(+ "the ChatGPT backend returned HTTP {status}."+ )),+ });+ }+ let parsed: serde_json::Value = serde_json::from_str(&body).map_err(|e| {+ LoginError::Unavailable(format!("the ChatGPT backend returned a response we could not read: {e}"))+ })?;+ let mut ids: Vec<String> = parsed+ .get("models")+ .and_then(|m| m.as_array())+ .into_iter()+ .flatten()+ .filter_map(|entry| entry.get("slug").and_then(|s| s.as_str()))+ .map(str::trim)+ .filter(|s| !s.is_empty())+ .map(String::from)+ .collect();+ ids.sort();+ ids.dedup();+ Ok(ids)+}+/// Human-readable reason a verification request was rejected. Never includes/// the key.fn failure_for(definition: &ProviderDefinition, status: u16, _body: &str) -> LoginError {