-
-
Notifications
You must be signed in to change notification settings - Fork 359
feat(settings): add Codex update button (brew/npm) #364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b6d97f9
feat: add Codex update button
166dab5
feat: add Codex update button
0f0cc74
fix: wire codex update state
97ecd9e
fix: wire codex update handler in settings
88c0c39
Merge branch 'main' into feat/codex-update
Dimillian 3b33998
fix: fallback when brew or npm is missing
3887895
fix: make codex precheck best-effort
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,249 @@ | ||
| use serde_json::Value; | ||
| use std::time::Duration; | ||
|
|
||
| use tokio::sync::Mutex; | ||
| use tokio::time::timeout; | ||
|
|
||
| use crate::backend::app_server::check_codex_installation; | ||
| use crate::shared::process_core::tokio_command; | ||
| use crate::types::AppSettings; | ||
|
|
||
| #[derive(serde::Serialize)] | ||
| #[serde(rename_all = "camelCase")] | ||
| struct CodexUpdateResult { | ||
| ok: bool, | ||
| method: String, | ||
| package: Option<String>, | ||
| before_version: Option<String>, | ||
| after_version: Option<String>, | ||
| upgraded: bool, | ||
| output: Option<String>, | ||
| details: Option<String>, | ||
| } | ||
|
|
||
| fn trim_lines(value: &str, max_len: usize) -> String { | ||
| let trimmed = value.trim(); | ||
| if trimmed.len() <= max_len { | ||
| return trimmed.to_string(); | ||
| } | ||
|
|
||
| let mut shortened = trimmed[..max_len].to_string(); | ||
| shortened.push_str("…"); | ||
| shortened | ||
| } | ||
|
|
||
| async fn run_brew_info(args: &[&str]) -> Result<bool, String> { | ||
| let mut command = tokio_command("brew"); | ||
| command.arg("info"); | ||
| command.args(args); | ||
| command.stdout(std::process::Stdio::piped()); | ||
| command.stderr(std::process::Stdio::piped()); | ||
|
|
||
| let output = match timeout(Duration::from_secs(8), command.output()).await { | ||
| Ok(result) => match result { | ||
| Ok(output) => output, | ||
| Err(err) => { | ||
| if err.kind() == std::io::ErrorKind::NotFound { | ||
| return Ok(false); | ||
| } | ||
| return Err(err.to_string()); | ||
| } | ||
| }, | ||
| Err(_) => return Ok(false), | ||
| }; | ||
|
|
||
| Ok(output.status.success()) | ||
| } | ||
|
|
||
| async fn detect_brew_cask(name: &str) -> Result<bool, String> { | ||
| run_brew_info(&["--cask", name]).await | ||
| } | ||
|
|
||
| async fn detect_brew_formula(name: &str) -> Result<bool, String> { | ||
| run_brew_info(&["--formula", name]).await | ||
| } | ||
|
|
||
| async fn run_brew_upgrade(args: &[&str]) -> Result<(bool, String), String> { | ||
| let mut command = tokio_command("brew"); | ||
| command.arg("upgrade"); | ||
| command.args(args); | ||
| command.stdout(std::process::Stdio::piped()); | ||
| command.stderr(std::process::Stdio::piped()); | ||
|
|
||
| let output = match timeout(Duration::from_secs(60 * 10), command.output()).await { | ||
| Ok(result) => result.map_err(|err| err.to_string())?, | ||
| Err(_) => return Err("Timed out while running `brew upgrade`.".to_string()), | ||
| }; | ||
|
|
||
| let stdout = String::from_utf8_lossy(&output.stdout); | ||
| let stderr = String::from_utf8_lossy(&output.stderr); | ||
| let combined = format!("{}\n{}", stdout.trim_end(), stderr.trim_end()); | ||
| Ok((output.status.success(), combined.trim().to_string())) | ||
| } | ||
|
|
||
| fn brew_output_indicates_upgrade(output: &str) -> bool { | ||
| let lower = output.to_ascii_lowercase(); | ||
| if lower.contains("already up-to-date") { | ||
| return false; | ||
| } | ||
| if lower.contains("already installed") && lower.contains("latest") { | ||
| return false; | ||
| } | ||
| if lower.contains("upgraded") { | ||
| return true; | ||
| } | ||
| if lower.contains("installing") || lower.contains("pouring") { | ||
| return true; | ||
| } | ||
| false | ||
| } | ||
|
|
||
| async fn npm_has_package(package: &str) -> Result<bool, String> { | ||
| let mut command = tokio_command("npm"); | ||
| command.arg("list"); | ||
| command.arg("-g"); | ||
| command.arg(package); | ||
| command.arg("--depth=0"); | ||
| command.stdout(std::process::Stdio::piped()); | ||
| command.stderr(std::process::Stdio::piped()); | ||
|
|
||
| let output = match timeout(Duration::from_secs(10), command.output()).await { | ||
| Ok(result) => match result { | ||
| Ok(output) => output, | ||
| Err(err) => { | ||
| if err.kind() == std::io::ErrorKind::NotFound { | ||
| return Ok(false); | ||
| } | ||
| return Err(err.to_string()); | ||
| } | ||
| }, | ||
| Err(_) => return Ok(false), | ||
| }; | ||
|
|
||
| Ok(output.status.success()) | ||
| } | ||
|
|
||
| async fn run_npm_install_latest(package: &str) -> Result<(bool, String), String> { | ||
| let mut command = tokio_command("npm"); | ||
| command.arg("install"); | ||
| command.arg("-g"); | ||
| command.arg(format!("{package}@latest")); | ||
| command.stdout(std::process::Stdio::piped()); | ||
| command.stderr(std::process::Stdio::piped()); | ||
|
|
||
| let output = match timeout(Duration::from_secs(60 * 10), command.output()).await { | ||
| Ok(result) => result.map_err(|err| err.to_string())?, | ||
| Err(_) => return Err("Timed out while running `npm install -g`.".to_string()), | ||
| }; | ||
|
|
||
| let stdout = String::from_utf8_lossy(&output.stdout); | ||
| let stderr = String::from_utf8_lossy(&output.stderr); | ||
| let combined = format!("{}\n{}", stdout.trim_end(), stderr.trim_end()); | ||
| Ok((output.status.success(), combined.trim().to_string())) | ||
| } | ||
|
|
||
| pub(crate) async fn codex_update_core( | ||
| app_settings: &Mutex<AppSettings>, | ||
| codex_bin: Option<String>, | ||
| codex_args: Option<String>, | ||
| ) -> Result<Value, String> { | ||
| let (default_bin, default_args) = { | ||
| let settings = app_settings.lock().await; | ||
| (settings.codex_bin.clone(), settings.codex_args.clone()) | ||
| }; | ||
| let resolved = codex_bin | ||
| .clone() | ||
| .filter(|value| !value.trim().is_empty()) | ||
| .or(default_bin); | ||
| let resolved_args = codex_args | ||
| .clone() | ||
| .filter(|value| !value.trim().is_empty()) | ||
| .or(default_args); | ||
| let _ = resolved_args; | ||
|
|
||
| let before_version = check_codex_installation(resolved.clone()) | ||
| .await | ||
| .ok() | ||
| .flatten(); | ||
|
|
||
| let (method, package, upgrade_ok, output, upgraded) = if detect_brew_cask("codex").await? { | ||
| let (ok, output) = run_brew_upgrade(&["--cask", "codex"]).await?; | ||
| let upgraded = brew_output_indicates_upgrade(&output); | ||
| ( | ||
| "brew_cask".to_string(), | ||
| Some("codex".to_string()), | ||
| ok, | ||
| output, | ||
| upgraded, | ||
| ) | ||
| } else if detect_brew_formula("codex").await? { | ||
| let (ok, output) = run_brew_upgrade(&["codex"]).await?; | ||
| let upgraded = brew_output_indicates_upgrade(&output); | ||
| ( | ||
| "brew_formula".to_string(), | ||
| Some("codex".to_string()), | ||
| ok, | ||
| output, | ||
| upgraded, | ||
| ) | ||
| } else if npm_has_package("@openai/codex").await? { | ||
| let (ok, output) = run_npm_install_latest("@openai/codex").await?; | ||
| ( | ||
| "npm".to_string(), | ||
| Some("@openai/codex".to_string()), | ||
| ok, | ||
| output, | ||
| ok, | ||
| ) | ||
| } else { | ||
| ( | ||
| "unknown".to_string(), | ||
| None, | ||
| false, | ||
| String::new(), | ||
| false, | ||
| ) | ||
| }; | ||
|
|
||
| let after_version = if method == "unknown" { | ||
| None | ||
| } else { | ||
| match check_codex_installation(resolved.clone()).await { | ||
| Ok(version) => version, | ||
| Err(err) => { | ||
| let result = CodexUpdateResult { | ||
| ok: false, | ||
| method, | ||
| package, | ||
| before_version, | ||
| after_version: None, | ||
| upgraded, | ||
| output: Some(trim_lines(&output, 8000)), | ||
| details: Some(err), | ||
| }; | ||
| return serde_json::to_value(result).map_err(|e| e.to_string()); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| let details = if method == "unknown" { | ||
| Some("Unable to detect Codex installation method (brew/npm).".to_string()) | ||
| } else if upgrade_ok { | ||
| None | ||
| } else { | ||
| Some("Codex update failed.".to_string()) | ||
| }; | ||
|
|
||
| let result = CodexUpdateResult { | ||
| ok: upgrade_ok, | ||
| method, | ||
| package, | ||
| before_version, | ||
| after_version, | ||
| upgraded, | ||
| output: Some(trim_lines(&output, 8000)), | ||
| details, | ||
| }; | ||
|
|
||
| serde_json::to_value(result).map_err(|err| err.to_string()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.