From 19928bc2573549c6bc54e531c935b6cd234b6360 Mon Sep 17 00:00:00 2001 From: Fouad Matin <169186268+fouad-openai@users.noreply.github.com> Date: Mon, 28 Apr 2025 21:42:06 -0700 Subject: [PATCH] [codex-rs] fix: exit code 1 if no api key (#697) --- codex-rs/Cargo.lock | 13 ++++--------- codex-rs/exec/Cargo.toml | 1 + codex-rs/exec/src/lib.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index ef98511f..15125354 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -528,6 +528,7 @@ dependencies = [ "anyhow", "clap", "codex-core", + "owo-colors", "tokio", "tracing", "tracing-subscriber", @@ -560,7 +561,7 @@ dependencies = [ "anyhow", "clap", "codex-core", - "owo-colors 4.2.0", + "owo-colors", "rand", "tokio", "tracing", @@ -598,7 +599,7 @@ dependencies = [ "eyre", "indenter", "once_cell", - "owo-colors 3.5.0", + "owo-colors", "tracing-error", ] @@ -609,7 +610,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd6be1b2a7e382e2b98b43b2adcca6bb0e465af0bdd38123873ae61eb17a72c2" dependencies = [ "once_cell", - "owo-colors 3.5.0", + "owo-colors", "tracing-core", "tracing-error", ] @@ -2224,12 +2225,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" -[[package]] -name = "owo-colors" -version = "3.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" - [[package]] name = "owo-colors" version = "4.2.0" diff --git a/codex-rs/exec/Cargo.toml b/codex-rs/exec/Cargo.toml index 491dd4c1..05f9ea40 100644 --- a/codex-rs/exec/Cargo.toml +++ b/codex-rs/exec/Cargo.toml @@ -24,3 +24,4 @@ tokio = { version = "1", features = [ ] } tracing = { version = "0.1.41", features = ["log"] } tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } +owo-colors = "4.2.0" diff --git a/codex-rs/exec/src/lib.rs b/codex-rs/exec/src/lib.rs index d37e5a95..7874f44b 100644 --- a/codex-rs/exec/src/lib.rs +++ b/codex-rs/exec/src/lib.rs @@ -12,11 +12,23 @@ use codex_core::protocol::FileChange; use codex_core::protocol::InputItem; use codex_core::protocol::Op; use codex_core::util::is_inside_git_repo; +use owo_colors::OwoColorize; use tracing::debug; use tracing::error; use tracing::info; use tracing_subscriber::EnvFilter; +/// Returns `true` if a recognised API key is present in the environment. +/// +/// At present we only support `OPENAI_API_KEY`, mirroring the behaviour of the +/// Node-based `codex-cli`. Additional providers can be added here when the +/// Rust implementation gains first-class support for them. +fn has_api_key() -> bool { + std::env::var("OPENAI_API_KEY") + .map(|s| !s.trim().is_empty()) + .unwrap_or(false) +} + pub async fn run_main(cli: Cli) -> anyhow::Result<()> { // TODO(mbolin): Take a more thoughtful approach to logging. let default_level = "error"; @@ -41,6 +53,20 @@ pub async fn run_main(cli: Cli) -> anyhow::Result<()> { .. } = cli; + // --------------------------------------------------------------------- + // API key handling + // --------------------------------------------------------------------- + + if !has_api_key() { + eprintln!( + "\n{msg}\n\nSet the environment variable {var} and re-run this command.\nYou can create a key here: {url}\n", + msg = "Missing OpenAI API key.".red(), + var = "OPENAI_API_KEY".bold(), + url = "https://platform.openai.com/account/api-keys".bold().underline(), + ); + std::process::exit(1); + } + if !skip_git_repo_check && !is_inside_git_repo() { eprintln!("Not inside a Git repo and --skip-git-repo-check was not specified."); std::process::exit(1);