mirror of
https://github.com/openai/codex.git
synced 2026-11-30 00:10:49 +00:00
Auto update approval
This commit is contained in:
@@ -19,6 +19,7 @@ use codex_exec::Cli as ExecCli;
|
||||
use codex_responses_api_proxy::Args as ResponsesApiProxyArgs;
|
||||
use codex_tui::AppExitInfo;
|
||||
use codex_tui::Cli as TuiCli;
|
||||
use codex_tui::UpdateAction;
|
||||
use owo_colors::OwoColorize;
|
||||
use std::path::PathBuf;
|
||||
use supports_color::Stream;
|
||||
@@ -200,6 +201,7 @@ fn format_exit_messages(exit_info: AppExitInfo, color_enabled: bool) -> Vec<Stri
|
||||
let AppExitInfo {
|
||||
token_usage,
|
||||
conversation_id,
|
||||
..
|
||||
} = exit_info;
|
||||
|
||||
if token_usage.is_zero() {
|
||||
@@ -260,7 +262,13 @@ async fn cli_main(codex_linux_sandbox_exe: Option<PathBuf>) -> anyhow::Result<()
|
||||
root_config_overrides.clone(),
|
||||
);
|
||||
let exit_info = codex_tui::run_main(interactive, codex_linux_sandbox_exe).await?;
|
||||
let action = exit_info.update_action;
|
||||
print_exit_messages(exit_info);
|
||||
if let Some(action) = action {
|
||||
run_update_action(action)?;
|
||||
// After update completes, exit immediately.
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
Some(Subcommand::Exec(mut exec_cli)) => {
|
||||
prepend_config_flags(
|
||||
@@ -471,6 +479,31 @@ fn print_completion(cmd: CompletionCommand) {
|
||||
generate(cmd.shell, &mut app, name, &mut std::io::stdout());
|
||||
}
|
||||
|
||||
fn run_update_action(action: UpdateAction) -> anyhow::Result<()> {
|
||||
match action {
|
||||
UpdateAction::NpmGlobalLatest => {
|
||||
println!("Updating Codex via npm (global latest)…");
|
||||
let status = std::process::Command::new("npm")
|
||||
.args(["install", "-g", "@openai/codex@latest"])
|
||||
.status()?;
|
||||
if !status.success() {
|
||||
anyhow::bail!("npm update failed with status {status}");
|
||||
}
|
||||
}
|
||||
UpdateAction::BrewUpgrade => {
|
||||
println!("Updating Codex via Homebrew…");
|
||||
let status = std::process::Command::new("brew")
|
||||
.args(["upgrade", "codex"])
|
||||
.status()?;
|
||||
if !status.success() {
|
||||
anyhow::bail!("brew upgrade failed with status {status}");
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("Update complete.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -509,6 +542,7 @@ mod tests {
|
||||
conversation_id: conversation
|
||||
.map(ConversationId::from_string)
|
||||
.map(Result::unwrap),
|
||||
update_action: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -517,6 +551,7 @@ mod tests {
|
||||
let exit_info = AppExitInfo {
|
||||
token_usage: TokenUsage::default(),
|
||||
conversation_id: None,
|
||||
update_action: None,
|
||||
};
|
||||
let lines = format_exit_messages(exit_info, false);
|
||||
assert!(lines.is_empty());
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::UpdateAction;
|
||||
use crate::app_backtrack::BacktrackState;
|
||||
use crate::app_event::AppEvent;
|
||||
use crate::app_event_sender::AppEventSender;
|
||||
@@ -43,6 +44,7 @@ use tokio::sync::mpsc::unbounded_channel;
|
||||
pub struct AppExitInfo {
|
||||
pub token_usage: TokenUsage,
|
||||
pub conversation_id: Option<ConversationId>,
|
||||
pub update_action: Option<UpdateAction>,
|
||||
}
|
||||
|
||||
pub(crate) struct App {
|
||||
@@ -71,6 +73,9 @@ pub(crate) struct App {
|
||||
|
||||
// Esc-backtracking state grouped
|
||||
pub(crate) backtrack: crate::app_backtrack::BacktrackState,
|
||||
|
||||
/// Set when the user confirms an update; propagated on exit.
|
||||
pub(crate) pending_update_action: Option<UpdateAction>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
@@ -152,8 +157,15 @@ impl App {
|
||||
has_emitted_history_lines: false,
|
||||
commit_anim_running: Arc::new(AtomicBool::new(false)),
|
||||
backtrack: BacktrackState::default(),
|
||||
pending_update_action: None,
|
||||
};
|
||||
|
||||
// Show an update approval popup at startup when an update is available.
|
||||
#[cfg(not(debug_assertions))]
|
||||
if let Some(latest_version) = crate::updates::get_upgrade_version(&app.config) {
|
||||
app.chat_widget.open_update_popup(latest_version);
|
||||
}
|
||||
|
||||
let tui_events = tui.event_stream();
|
||||
tokio::pin!(tui_events);
|
||||
|
||||
@@ -171,6 +183,7 @@ impl App {
|
||||
Ok(AppExitInfo {
|
||||
token_usage: app.token_usage(),
|
||||
conversation_id: app.chat_widget.conversation_id(),
|
||||
update_action: app.pending_update_action,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -289,6 +302,11 @@ impl App {
|
||||
AppEvent::ExitRequest => {
|
||||
return Ok(false);
|
||||
}
|
||||
AppEvent::RunUpdateAndExit(action) => {
|
||||
// Record the requested update action and exit.
|
||||
self.pending_update_action = Some(action);
|
||||
return Ok(false);
|
||||
}
|
||||
AppEvent::CodexOp(op) => self.chat_widget.submit_op(op),
|
||||
AppEvent::DiffResult(text) => {
|
||||
// Clear the in-progress state in the bottom pane
|
||||
|
||||
@@ -5,6 +5,7 @@ use codex_core::protocol::ConversationPathResponseEvent;
|
||||
use codex_core::protocol::Event;
|
||||
use codex_file_search::FileMatch;
|
||||
|
||||
use crate::UpdateAction;
|
||||
use crate::bottom_pane::ApprovalRequest;
|
||||
use crate::history_cell::HistoryCell;
|
||||
|
||||
@@ -87,4 +88,7 @@ pub(crate) enum AppEvent {
|
||||
|
||||
/// Open the approval popup.
|
||||
FullScreenApprovalRequest(ApprovalRequest),
|
||||
|
||||
/// User approved to update Codex; run the update and exit.
|
||||
RunUpdateAndExit(UpdateAction),
|
||||
}
|
||||
|
||||
@@ -95,6 +95,8 @@ use self::session_header::SessionHeader;
|
||||
use crate::streaming::controller::StreamController;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::UpdateAction;
|
||||
use crate::version::CODEX_CLI_VERSION;
|
||||
use chrono::Local;
|
||||
use codex_common::approval_presets::ApprovalPreset;
|
||||
use codex_common::approval_presets::builtin_approval_presets;
|
||||
@@ -1810,6 +1812,78 @@ impl ChatWidget {
|
||||
});
|
||||
}
|
||||
|
||||
/// Open a simple modal asking to update Codex when an update is available.
|
||||
pub(crate) fn open_update_popup(&mut self, latest_version: String) {
|
||||
use ratatui::style::Stylize as _;
|
||||
use ratatui::text::Line;
|
||||
|
||||
let current_version = CODEX_CLI_VERSION;
|
||||
let title = "Update Approval".to_string();
|
||||
|
||||
// Build header lines with the prompt and version info.
|
||||
let mut header_lines: Vec<Line<'static>> = vec![
|
||||
Line::from("Should I update Codex?".bold()),
|
||||
Line::from(""),
|
||||
Line::from(vec![
|
||||
"Update available!".bold().cyan(),
|
||||
" ".into(),
|
||||
format!("{current_version} -> {latest_version}.")
|
||||
.bold()
|
||||
.into(),
|
||||
]),
|
||||
];
|
||||
|
||||
// Determine the recommended update method.
|
||||
let exe = std::env::current_exe().unwrap_or_default();
|
||||
let managed_by_npm = std::env::var_os("CODEX_MANAGED_BY_NPM").is_some();
|
||||
let update_action = if managed_by_npm {
|
||||
Some(UpdateAction::NpmGlobalLatest)
|
||||
} else if cfg!(target_os = "macos")
|
||||
&& (exe.starts_with("/opt/homebrew") || exe.starts_with("/usr/local"))
|
||||
{
|
||||
Some(UpdateAction::BrewUpgrade)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if update_action.is_none() {
|
||||
header_lines.push(Line::from(""));
|
||||
header_lines.push(Line::from(vec![
|
||||
"See ".into(),
|
||||
"https://github.com/openai/codex/releases/latest"
|
||||
.cyan()
|
||||
.underlined(),
|
||||
" for installation options.".into(),
|
||||
]));
|
||||
}
|
||||
|
||||
let mut items: Vec<SelectionItem> = Vec::new();
|
||||
if let Some(action) = update_action {
|
||||
let action_for_closure = action;
|
||||
items.push(SelectionItem {
|
||||
name: "Yes, update now".to_string(),
|
||||
actions: vec![Box::new(move |tx: &AppEventSender| {
|
||||
tx.send(AppEvent::RunUpdateAndExit(action_for_closure));
|
||||
})],
|
||||
dismiss_on_select: true,
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
items.push(SelectionItem {
|
||||
name: "No, not now".to_string(),
|
||||
dismiss_on_select: true,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
self.bottom_pane.show_selection_view(SelectionViewParams {
|
||||
title: Some(title),
|
||||
footer_hint: Some(standard_popup_hint_line()),
|
||||
items,
|
||||
header: Box::new(ratatui::widgets::Paragraph::new(header_lines)),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
|
||||
/// Set the approval policy in the widget's config copy.
|
||||
pub(crate) fn set_approval_policy(&mut self, policy: AskForApproval) {
|
||||
self.config.approval_policy = policy;
|
||||
|
||||
@@ -71,6 +71,15 @@ mod text_formatting;
|
||||
mod tui;
|
||||
mod ui_consts;
|
||||
mod version;
|
||||
|
||||
/// Update action the CLI should perform after the TUI exits.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum UpdateAction {
|
||||
/// Update via `npm install -g @openai/codex@latest`.
|
||||
NpmGlobalLatest,
|
||||
/// Update via `brew upgrade codex`.
|
||||
BrewUpgrade,
|
||||
}
|
||||
mod wrapping;
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -397,6 +406,7 @@ async fn run_ratatui_app(
|
||||
return Ok(AppExitInfo {
|
||||
token_usage: codex_core::protocol::TokenUsage::default(),
|
||||
conversation_id: None,
|
||||
update_action: None,
|
||||
});
|
||||
}
|
||||
if should_show_windows_wsl_screen {
|
||||
@@ -441,6 +451,7 @@ async fn run_ratatui_app(
|
||||
return Ok(AppExitInfo {
|
||||
token_usage: codex_core::protocol::TokenUsage::default(),
|
||||
conversation_id: None,
|
||||
update_action: None,
|
||||
});
|
||||
}
|
||||
other => other,
|
||||
|
||||
Reference in New Issue
Block a user