use super::SourceController; use gpui::Context; impl SourceController { pub(crate) fn load_startup_data(&self, cx: &mut Context) { self.load_account_rate_limits(cx); let source = self.state.clone(); let source = &source; let load_generation = source.update(cx, |source, _| { source.history_load_generation += 1; source.history_load_generation }); if source.read(cx).archived_loaded || source.read(cx).archived_loading { self.load_archived_chats(cx); } let source = source.clone(); let bridge = source.read(cx).client.connection(); let generation = bridge.request_generation(); let cwd = source.read(cx).default_cwd.clone(); let read_state_versions = self.read_state_versions(cx); cx.spawn(async move |this, cx| { let models = bridge.list_models().await; let profiles = bridge.list_permission_profiles(cwd).await; let threads = bridge.list_threads(false).await; let _ = this.update(cx, |view, cx| { if bridge.generation() != generation || source.read(cx).history_load_generation != load_generation { return; } view.apply_models_result(models, cx); view.apply_permission_profiles_result(profiles, cx); view.apply_threads_result(threads, &read_state_versions, cx); // Loaded server sessions can own processes even without an open view. // Register them before resuming so their subsequent lifecycle events arrive. for chat in view.source_chats(cx) { if chat.read(cx).thread.as_ref().is_some_and(|thread| { !matches!( thread.status, codex_app_server_protocol::ThreadStatus::NotLoaded ) }) { view.chat(&chat, cx); } } let controllers = view.chats.values().cloned().collect::>(); for controller in controllers { if !controller.read(cx).closed { controller.update(cx, |controller, cx| controller.resume(cx)); } } }); }) .detach(); } fn load_account_rate_limits(&self, cx: &mut Context) { let source = self.state.clone(); let bridge = source.read(cx).client.connection(); let generation = bridge.request_generation(); let previous = source.read(cx).rate_limits.clone(); cx.spawn(async move |this, cx| { let result = bridge.read_account_rate_limits().await; let _ = this.update(cx, |view, cx| { if bridge.generation() != generation { return; } match result { Ok(response) => { source.update(cx, |state, cx| { // A notification received during the read is newer than // the requested snapshot; never roll its usage back. if state.rate_limits != previous { return; } let snapshot = response .rate_limits_by_limit_id .and_then(|mut limits| limits.remove("codex")) .unwrap_or(response.rate_limits); state.rate_limits = None; state.apply_rate_limits(snapshot); cx.notify(); }); } Err(error) => view .apply_bridge_error(format!("Failed to read account usage: {error}"), cx), } }); }) .detach(); } pub(crate) fn load_archived_chats(&self, cx: &mut Context) { let source = self.state.clone(); let source = &source; let load_generation = source.update(cx, |s, cx| { s.archive_load_generation += 1; s.archived_loading = true; cx.notify(); s.archive_load_generation }); let source = source.clone(); let bridge = source.read(cx).client.connection(); let generation = bridge.request_generation(); let read_state_versions = self.read_state_versions(cx); cx.spawn(async move |this, cx| { let result = bridge.list_threads(true).await; let _ = this.update(cx, |app, cx| { if source.read(cx).archive_load_generation != load_generation { return; } source.update(cx, |s, cx| { s.archived_loading = false; cx.notify(); }); if bridge.generation() != generation { return; } match result { Ok(response) => { let mut read_states = response.read_states.unwrap_or_default(); let existing = app .source_chats(cx) .into_iter() .map(|chat| (chat.read(cx).id.clone(), chat)) .collect::>(); let settings = source.read(cx).new_chat_settings.clone(); let chats = response .data .into_iter() .map(|thread| { let receipt = read_states.remove(&thread.id); let chat = if let Some(chat) = existing.get(&thread.id).cloned() { let title = crate::state::mapping::thread_title( thread.name.as_deref(), &thread.preview, ); let subtitle = format!( "{} - {}", crate::state::mapping::thread_status_label(&thread.status), thread.cwd.display(), ); chat.update(cx, |chat, cx| { chat.adopt_thread(thread, title.into(), subtitle.into()); cx.notify(); }); chat } else { crate::state::mapping::chat_entity_from_thread( &source, thread, settings.clone(), cx, ) }; app.apply_catalog_read_state( &chat, receipt, &read_state_versions, cx, ); chat }) .collect(); source.update(cx, |s, cx| { s.archived_chats = chats; s.archived_loaded = true; cx.notify(); }); for chat in source.read(cx).archived_chats.clone() { if chat.read(cx).thread.as_ref().is_some_and(|thread| { !matches!( thread.status, codex_app_server_protocol::ThreadStatus::NotLoaded ) }) { app.chat(&chat, cx) .update(cx, |controller, cx| controller.resume(cx)); } } app.load_fork_sources(cx); } Err(error) => app.apply_bridge_error(error.to_string(), cx), } }); }) .detach(); } }