|
| 1 | +use crate::auth_mode::auth_mode_to_api; |
1 | 2 | use crate::bespoke_event_handling::apply_bespoke_event_handling; |
2 | 3 | use crate::command_exec::CommandExecManager; |
3 | 4 | use crate::command_exec::StartCommandExecParams; |
@@ -380,6 +381,7 @@ use codex_git_utils::git_diff_to_remote; |
380 | 381 | use codex_git_utils::resolve_root_git_project_for_trust; |
381 | 382 | use codex_login::AuthManager; |
382 | 383 | use codex_login::CODEX_OPEN_APP_URL; |
| 384 | +use codex_login::AuthReloadStatus; |
383 | 385 | use codex_login::CodexAuth; |
384 | 386 | use codex_login::LoginSuccessPage; |
385 | 387 | use codex_login::LoginSuccessPageBrand; |
@@ -495,6 +497,81 @@ use uuid::Uuid; |
495 | 497 | #[cfg(test)] |
496 | 498 | use codex_app_server_protocol::ServerRequest; |
497 | 499 |
|
| 500 | +async fn reload_auth_from_storage_if_idle( |
| 501 | + auth_manager: &Arc<AuthManager>, |
| 502 | + thread_manager: &Arc<ThreadManager>, |
| 503 | + config_manager: &ConfigManager, |
| 504 | + outgoing: &OutgoingMessageSender, |
| 505 | + thread_watch_manager: &ThreadWatchManager, |
| 506 | + chatgpt_base_url: &str, |
| 507 | + reason: &str, |
| 508 | +) { |
| 509 | + if *thread_watch_manager.subscribe_running_turn_count().borrow() != 0 { |
| 510 | + return; |
| 511 | + } |
| 512 | + |
| 513 | + let status = auth_manager.reload_with_status().await; |
| 514 | + match handle_auth_reload_status( |
| 515 | + status, |
| 516 | + auth_manager, |
| 517 | + thread_manager, |
| 518 | + config_manager, |
| 519 | + outgoing, |
| 520 | + chatgpt_base_url, |
| 521 | + reason, |
| 522 | + ) |
| 523 | + .await |
| 524 | + { |
| 525 | + AuthReloadStatus::Reloaded { .. } => {} |
| 526 | + AuthReloadStatus::Failed => { |
| 527 | + warn!("failed to reload auth from storage before {reason}"); |
| 528 | + } |
| 529 | + } |
| 530 | +} |
| 531 | + |
| 532 | +async fn handle_auth_reload_status( |
| 533 | + status: AuthReloadStatus, |
| 534 | + auth_manager: &Arc<AuthManager>, |
| 535 | + thread_manager: &Arc<ThreadManager>, |
| 536 | + config_manager: &ConfigManager, |
| 537 | + outgoing: &OutgoingMessageSender, |
| 538 | + chatgpt_base_url: &str, |
| 539 | + reason: &str, |
| 540 | +) -> AuthReloadStatus { |
| 541 | + match status { |
| 542 | + AuthReloadStatus::Reloaded { changed } => { |
| 543 | + if changed { |
| 544 | + let invalidated_thread_count = |
| 545 | + thread_manager.invalidate_model_transport_caches().await; |
| 546 | + info!( |
| 547 | + "auth reloaded from storage before {reason}; invalidated model transport caches for {invalidated_thread_count} tracked thread(s)" |
| 548 | + ); |
| 549 | + config_manager.replace_cloud_config_bundle_loader( |
| 550 | + Arc::clone(auth_manager), |
| 551 | + chatgpt_base_url.to_string(), |
| 552 | + ); |
| 553 | + config_manager |
| 554 | + .sync_default_client_residency_requirement() |
| 555 | + .await; |
| 556 | + let auth = auth_manager.auth_cached(); |
| 557 | + outgoing |
| 558 | + .send_server_notification(ServerNotification::AccountUpdated( |
| 559 | + AccountUpdatedNotification { |
| 560 | + auth_mode: auth |
| 561 | + .as_ref() |
| 562 | + .map(CodexAuth::api_auth_mode) |
| 563 | + .map(auth_mode_to_api), |
| 564 | + plan_type: auth.as_ref().and_then(CodexAuth::account_plan_type), |
| 565 | + }, |
| 566 | + )) |
| 567 | + .await; |
| 568 | + } |
| 569 | + AuthReloadStatus::Reloaded { changed } |
| 570 | + } |
| 571 | + AuthReloadStatus::Failed => AuthReloadStatus::Failed, |
| 572 | + } |
| 573 | +} |
| 574 | + |
498 | 575 | mod account_processor; |
499 | 576 | mod apps_processor; |
500 | 577 | mod catalog_processor; |
|
0 commit comments