mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
async
This commit is contained in:
+100
-23
@@ -332,6 +332,94 @@ impl App {
|
||||
}
|
||||
}
|
||||
|
||||
async fn on_session_configured_model(&mut self, model: String) {
|
||||
self.maybe_emit_pending_model_migration_notice(model.as_str())
|
||||
.await;
|
||||
self.spawn_schedule_model_migration_notice(model);
|
||||
}
|
||||
|
||||
async fn maybe_emit_pending_model_migration_notice(&mut self, used_model: &str) {
|
||||
let Some(pending) = self.config.notices.pending_model_migration.take() else {
|
||||
return;
|
||||
};
|
||||
|
||||
let should_show = pending.from_model == used_model;
|
||||
if should_show {
|
||||
let message = format!(
|
||||
"Recommended model upgrade: switch from {} to {}. Run /model to change.",
|
||||
pending.from_model, pending.to_model
|
||||
);
|
||||
self.app_event_tx.send(AppEvent::InsertHistoryCell(Box::new(
|
||||
crate::history_cell::new_warning_event(message),
|
||||
)));
|
||||
|
||||
self.config
|
||||
.notices
|
||||
.model_migrations
|
||||
.insert(pending.from_model.clone(), pending.to_model.clone());
|
||||
}
|
||||
|
||||
let mut edits = ConfigEditsBuilder::new(&self.config.codex_home);
|
||||
if should_show {
|
||||
edits = edits.record_model_migration_seen(
|
||||
pending.from_model.as_str(),
|
||||
pending.to_model.as_str(),
|
||||
);
|
||||
}
|
||||
if let Err(err) = edits.clear_pending_model_migration_notice().apply().await {
|
||||
tracing::error!(
|
||||
error = %err,
|
||||
"failed to clear pending model migration notice"
|
||||
);
|
||||
self.chat_widget.add_error_message(format!(
|
||||
"Failed to persist model migration notice state: {err}"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_schedule_model_migration_notice(&self, used_model: String) {
|
||||
let config = self.config.clone();
|
||||
let models_manager = self.server.get_models_manager();
|
||||
|
||||
tokio::spawn(async move {
|
||||
// Build the candidate migration notice using the current models list (remote if
|
||||
// refreshed, otherwise the startup fallback seeded in `ModelsManager`).
|
||||
let available_models = models_manager.list_models(&config).await;
|
||||
let upgrade = available_models
|
||||
.iter()
|
||||
.find(|preset| preset.model == used_model)
|
||||
.and_then(|preset| preset.upgrade.as_ref());
|
||||
let Some(upgrade) = upgrade else {
|
||||
return;
|
||||
};
|
||||
|
||||
if migration_prompt_hidden(&config, upgrade.migration_config_key.as_str()) {
|
||||
return;
|
||||
}
|
||||
|
||||
let target_model = upgrade.id.clone();
|
||||
if !should_show_model_migration_prompt(
|
||||
used_model.as_str(),
|
||||
target_model.as_str(),
|
||||
&config.notices.model_migrations,
|
||||
&available_models,
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Err(err) = ConfigEditsBuilder::new(&config.codex_home)
|
||||
.set_pending_model_migration_notice(used_model.as_str(), target_model.as_str())
|
||||
.apply()
|
||||
.await
|
||||
{
|
||||
tracing::error!(
|
||||
error = %err,
|
||||
"failed to persist pending model migration notice"
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn run(
|
||||
tui: &mut tui::Tui,
|
||||
@@ -352,30 +440,10 @@ impl App {
|
||||
auth_manager.clone(),
|
||||
SessionSource::Cli,
|
||||
));
|
||||
let mut model = conversation_manager
|
||||
.get_models_manager()
|
||||
.get_model(&config.model, &config)
|
||||
.await;
|
||||
let exit_info = handle_model_migration_prompt_if_needed(
|
||||
tui,
|
||||
&mut config,
|
||||
model.as_str(),
|
||||
&app_event_tx,
|
||||
conversation_manager.get_models_manager(),
|
||||
)
|
||||
.await;
|
||||
if let Some(exit_info) = exit_info {
|
||||
return Ok(exit_info);
|
||||
}
|
||||
if let Some(updated_model) = config.model.clone() {
|
||||
model = updated_model;
|
||||
}
|
||||
|
||||
let enhanced_keys_supported = tui.enhanced_keys_supported();
|
||||
let model_family = conversation_manager
|
||||
.get_models_manager()
|
||||
.construct_model_family(model.as_str(), &config)
|
||||
.await;
|
||||
let model_family =
|
||||
codex_core::models_manager::model_family::ModelFamily::placeholder(&config);
|
||||
let mut chat_widget = match resume_selection {
|
||||
ResumeSelection::StartFresh | ResumeSelection::Exit => {
|
||||
let init = crate::chatwidget::ChatWidgetInit {
|
||||
@@ -437,7 +505,7 @@ impl App {
|
||||
chat_widget,
|
||||
auth_manager: auth_manager.clone(),
|
||||
config,
|
||||
current_model: model.clone(),
|
||||
current_model: String::new(),
|
||||
active_profile,
|
||||
file_search,
|
||||
enhanced_keys_supported,
|
||||
@@ -722,12 +790,21 @@ impl App {
|
||||
self.suppress_shutdown_complete = false;
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
let configured_model = match &event.msg {
|
||||
EventMsg::SessionConfigured(ev) => Some(ev.model.clone()),
|
||||
_ => None,
|
||||
};
|
||||
if let EventMsg::ListSkillsResponse(response) = &event.msg {
|
||||
let cwd = self.chat_widget.config_ref().cwd.clone();
|
||||
let errors = errors_for_cwd(&cwd, response);
|
||||
emit_skill_load_warnings(&self.app_event_tx, &errors);
|
||||
}
|
||||
self.chat_widget.handle_codex_event(event);
|
||||
|
||||
if let Some(model) = configured_model {
|
||||
self.on_session_configured_model(model).await;
|
||||
}
|
||||
}
|
||||
AppEvent::ConversationHistory(ev) => {
|
||||
self.on_conversation_history_for_backtrack(tui, ev).await?;
|
||||
|
||||
@@ -434,6 +434,9 @@ impl ChatWidget {
|
||||
let initial_messages = event.initial_messages.clone();
|
||||
let model_for_header = event.model.clone();
|
||||
self.session_header.set_model(&model_for_header);
|
||||
// Now that Codex has selected the actual model, update the model family used for UI.
|
||||
self.app_event_tx
|
||||
.send(AppEvent::UpdateModel(model_for_header.clone()));
|
||||
self.add_to_history(history_cell::new_session_info(
|
||||
&self.config,
|
||||
&model_for_header,
|
||||
@@ -449,9 +452,15 @@ impl ChatWidget {
|
||||
cwds: Vec::new(),
|
||||
force_reload: false,
|
||||
});
|
||||
let had_initial_message = self.initial_user_message.is_some();
|
||||
if let Some(user_message) = self.initial_user_message.take() {
|
||||
self.submit_user_message(user_message);
|
||||
}
|
||||
if !had_initial_message {
|
||||
// If there are queued inputs from startup, begin the first turn now.
|
||||
// Subsequent queued inputs are sent turn-by-turn via `maybe_send_next_queued_input`.
|
||||
self.maybe_send_next_queued_input();
|
||||
}
|
||||
if !self.suppress_session_configured_redraw {
|
||||
self.request_redraw();
|
||||
}
|
||||
@@ -1418,9 +1427,7 @@ impl ChatWidget {
|
||||
is_first_run,
|
||||
model_family,
|
||||
} = common;
|
||||
let model_slug = model_family.get_model_slug().to_string();
|
||||
let mut config = config;
|
||||
config.model = Some(model_slug.clone());
|
||||
let config = config;
|
||||
let mut rng = rand::rng();
|
||||
let placeholder = EXAMPLE_PROMPTS[rng.random_range(0..EXAMPLE_PROMPTS.len())].to_string();
|
||||
let codex_op_tx = spawn_agent(config.clone(), app_event_tx.clone(), conversation_manager);
|
||||
@@ -1444,7 +1451,7 @@ impl ChatWidget {
|
||||
model_family,
|
||||
auth_manager,
|
||||
models_manager,
|
||||
session_header: SessionHeader::new(model_slug),
|
||||
session_header: SessionHeader::new("Starting...".to_string()),
|
||||
initial_user_message: create_initial_user_message(
|
||||
initial_prompt.unwrap_or_default(),
|
||||
initial_images,
|
||||
@@ -1723,7 +1730,7 @@ impl ChatWidget {
|
||||
return;
|
||||
}
|
||||
const INIT_PROMPT: &str = include_str!("../prompt_for_init_command.md");
|
||||
self.submit_user_message(INIT_PROMPT.to_string().into());
|
||||
self.queue_user_message(INIT_PROMPT.to_string().into());
|
||||
}
|
||||
SlashCommand::Compact => {
|
||||
self.clear_token_usage();
|
||||
@@ -1733,7 +1740,14 @@ impl ChatWidget {
|
||||
self.open_review_popup();
|
||||
}
|
||||
SlashCommand::Model => {
|
||||
self.open_model_popup();
|
||||
if self.conversation_id.is_none() {
|
||||
self.add_info_message(
|
||||
"`/model` is unavailable until startup finishes.".to_string(),
|
||||
None,
|
||||
);
|
||||
} else {
|
||||
self.open_model_popup();
|
||||
}
|
||||
}
|
||||
SlashCommand::Approvals => {
|
||||
self.open_approvals_popup();
|
||||
@@ -1904,7 +1918,7 @@ impl ChatWidget {
|
||||
}
|
||||
|
||||
fn queue_user_message(&mut self, user_message: UserMessage) {
|
||||
if self.bottom_pane.is_task_running() {
|
||||
if self.conversation_id.is_none() || self.bottom_pane.is_task_running() {
|
||||
self.queued_user_messages.push_back(user_message);
|
||||
self.refresh_queued_user_messages();
|
||||
} else {
|
||||
@@ -2213,7 +2227,7 @@ impl ChatWidget {
|
||||
|
||||
// If idle and there are queued inputs, submit exactly one to start the next turn.
|
||||
fn maybe_send_next_queued_input(&mut self) {
|
||||
if self.bottom_pane.is_task_running() {
|
||||
if self.conversation_id.is_none() || self.bottom_pane.is_task_running() {
|
||||
return;
|
||||
}
|
||||
if let Some(user_message) = self.queued_user_messages.pop_front() {
|
||||
|
||||
@@ -1023,6 +1023,27 @@ async fn alt_up_edits_most_recent_queued_message() {
|
||||
async fn enqueueing_history_prompt_multiple_times_is_stable() {
|
||||
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(None).await;
|
||||
|
||||
// Ensure the session is configured so the first submission is sent (not queued),
|
||||
// which seeds the prompt history for the subsequent Up-arrow recalls.
|
||||
let conversation_id = ConversationId::new();
|
||||
let rollout_file = NamedTempFile::new().unwrap();
|
||||
chat.handle_codex_event(Event {
|
||||
id: "configured".into(),
|
||||
msg: EventMsg::SessionConfigured(codex_core::protocol::SessionConfiguredEvent {
|
||||
session_id: conversation_id,
|
||||
model: "test-model".to_string(),
|
||||
model_provider_id: "test-provider".to_string(),
|
||||
approval_policy: AskForApproval::Never,
|
||||
sandbox_policy: SandboxPolicy::ReadOnly,
|
||||
cwd: PathBuf::from("/home/user/project"),
|
||||
reasoning_effort: Some(ReasoningEffortConfig::default()),
|
||||
history_log_id: 0,
|
||||
history_entry_count: 0,
|
||||
initial_messages: None,
|
||||
rollout_path: rollout_file.path().to_path_buf(),
|
||||
}),
|
||||
});
|
||||
|
||||
// Submit an initial prompt to seed history.
|
||||
chat.bottom_pane.set_composer_text("repeat me".to_string());
|
||||
chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
|
||||
|
||||
Reference in New Issue
Block a user