mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
515b6331bd
This does not implement the full Login with ChatGPT experience, but it should unblock people. **What works** * The `codex` multitool now has a `login` subcommand, so you can run `codex login`, which should write `CODEX_HOME/auth.json` if you complete the flow successfully. The TUI will now read the `OPENAI_API_KEY` from `auth.json`. * The TUI should refresh the token if it has expired and the necessary information is in `auth.json`. * There is a `LoginScreen` in the TUI that tells you to run `codex login` if both (1) your model provider expects to use `OPENAI_API_KEY` as its env var, and (2) `OPENAI_API_KEY` is not set. **What does not work** * The `LoginScreen` does not support the login flow from within the TUI. Instead, it tells you to quit, run `codex login`, and then run `codex` again. * `codex exec` does read from `auth.json` yet, nor does it direct the user to go through the login flow if `OPENAI_API_KEY` is not be found. * The `maybeRedeemCredits()` function from `get-api-key.tsx` has not been ported from TypeScript to `login_with_chatgpt.py` yet: https://github.com/openai/codex/blob/a67a67f3258fc21e147b6786a143fe3e15e6d5ba/codex-cli/src/utils/get-api-key.tsx#L84-L89 **Implementation** Currently, the OAuth flow requires running a local webserver on `127.0.0.1:1455`. It seemed wasteful to incur the additional binary cost of a webserver dependency in the Rust CLI just to support login, so instead we implement this logic in Python, as Python has a `http.server` module as part of its standard library. Specifically, we bundle the contents of a single Python file as a string in the Rust CLI and then use it to spawn a subprocess as `python3 -c {{SOURCE_FOR_PYTHON_SERVER}}`. As such, the most significant files in this PR are: ``` codex-rs/login/src/login_with_chatgpt.py codex-rs/login/src/lib.rs ``` Now that the CLI may load `OPENAI_API_KEY` from the environment _or_ `CODEX_HOME/auth.json`, we need a new abstraction for reading/writing this variable, so we introduce: ``` codex-rs/core/src/openai_api_key.rs ``` Note that `std::env::set_var()` is [rightfully] `unsafe` in Rust 2024, so we use a LazyLock<RwLock<Option<String>>> to store `OPENAI_API_KEY` so it is read in a thread-safe manner. Ultimately, it should be possible to go through the entire login flow from the TUI. This PR introduces a placeholder `LoginScreen` UI for that right now, though the new `codex login` subcommand introduced in this PR should be a viable workaround until the UI is ready. **Testing** Because the login flow is currently implemented in a standalone Python file, you can test it without building any Rust code as follows: ``` rm -rf /tmp/codex_home && mkdir /tmp/codex_home CODEX_HOME=/tmp/codex_home python3 codex-rs/login/src/login_with_chatgpt.py ``` For reference: * the original TypeScript implementation was introduced in https://github.com/openai/codex/pull/963 * support for redeeming credits was later added in https://github.com/openai/codex/pull/974
139 lines
4.1 KiB
Rust
139 lines
4.1 KiB
Rust
use clap::Parser;
|
||
use codex_cli::LandlockCommand;
|
||
use codex_cli::SeatbeltCommand;
|
||
use codex_cli::login::run_login_with_chatgpt;
|
||
use codex_cli::proto;
|
||
use codex_common::CliConfigOverrides;
|
||
use codex_exec::Cli as ExecCli;
|
||
use codex_tui::Cli as TuiCli;
|
||
use std::path::PathBuf;
|
||
|
||
use crate::proto::ProtoCli;
|
||
|
||
/// Codex CLI
|
||
///
|
||
/// If no subcommand is specified, options will be forwarded to the interactive CLI.
|
||
#[derive(Debug, Parser)]
|
||
#[clap(
|
||
author,
|
||
version,
|
||
// If a sub‑command is given, ignore requirements of the default args.
|
||
subcommand_negates_reqs = true
|
||
)]
|
||
struct MultitoolCli {
|
||
#[clap(flatten)]
|
||
pub config_overrides: CliConfigOverrides,
|
||
|
||
#[clap(flatten)]
|
||
interactive: TuiCli,
|
||
|
||
#[clap(subcommand)]
|
||
subcommand: Option<Subcommand>,
|
||
}
|
||
|
||
#[derive(Debug, clap::Subcommand)]
|
||
enum Subcommand {
|
||
/// Run Codex non-interactively.
|
||
#[clap(visible_alias = "e")]
|
||
Exec(ExecCli),
|
||
|
||
/// Login with ChatGPT.
|
||
Login(LoginCommand),
|
||
|
||
/// Experimental: run Codex as an MCP server.
|
||
Mcp,
|
||
|
||
/// Run the Protocol stream via stdin/stdout
|
||
#[clap(visible_alias = "p")]
|
||
Proto(ProtoCli),
|
||
|
||
/// Internal debugging commands.
|
||
Debug(DebugArgs),
|
||
}
|
||
|
||
#[derive(Debug, Parser)]
|
||
struct DebugArgs {
|
||
#[command(subcommand)]
|
||
cmd: DebugCommand,
|
||
}
|
||
|
||
#[derive(Debug, clap::Subcommand)]
|
||
enum DebugCommand {
|
||
/// Run a command under Seatbelt (macOS only).
|
||
Seatbelt(SeatbeltCommand),
|
||
|
||
/// Run a command under Landlock+seccomp (Linux only).
|
||
Landlock(LandlockCommand),
|
||
}
|
||
|
||
#[derive(Debug, Parser)]
|
||
struct LoginCommand {
|
||
#[clap(skip)]
|
||
config_overrides: CliConfigOverrides,
|
||
}
|
||
|
||
fn main() -> anyhow::Result<()> {
|
||
codex_linux_sandbox::run_with_sandbox(|codex_linux_sandbox_exe| async move {
|
||
cli_main(codex_linux_sandbox_exe).await?;
|
||
Ok(())
|
||
})
|
||
}
|
||
|
||
async fn cli_main(codex_linux_sandbox_exe: Option<PathBuf>) -> anyhow::Result<()> {
|
||
let cli = MultitoolCli::parse();
|
||
|
||
match cli.subcommand {
|
||
None => {
|
||
let mut tui_cli = cli.interactive;
|
||
prepend_config_flags(&mut tui_cli.config_overrides, cli.config_overrides);
|
||
codex_tui::run_main(tui_cli, codex_linux_sandbox_exe)?;
|
||
}
|
||
Some(Subcommand::Exec(mut exec_cli)) => {
|
||
prepend_config_flags(&mut exec_cli.config_overrides, cli.config_overrides);
|
||
codex_exec::run_main(exec_cli, codex_linux_sandbox_exe).await?;
|
||
}
|
||
Some(Subcommand::Mcp) => {
|
||
codex_mcp_server::run_main(codex_linux_sandbox_exe).await?;
|
||
}
|
||
Some(Subcommand::Login(mut login_cli)) => {
|
||
prepend_config_flags(&mut login_cli.config_overrides, cli.config_overrides);
|
||
run_login_with_chatgpt(login_cli.config_overrides).await;
|
||
}
|
||
Some(Subcommand::Proto(mut proto_cli)) => {
|
||
prepend_config_flags(&mut proto_cli.config_overrides, cli.config_overrides);
|
||
proto::run_main(proto_cli).await?;
|
||
}
|
||
Some(Subcommand::Debug(debug_args)) => match debug_args.cmd {
|
||
DebugCommand::Seatbelt(mut seatbelt_cli) => {
|
||
prepend_config_flags(&mut seatbelt_cli.config_overrides, cli.config_overrides);
|
||
codex_cli::debug_sandbox::run_command_under_seatbelt(
|
||
seatbelt_cli,
|
||
codex_linux_sandbox_exe,
|
||
)
|
||
.await?;
|
||
}
|
||
DebugCommand::Landlock(mut landlock_cli) => {
|
||
prepend_config_flags(&mut landlock_cli.config_overrides, cli.config_overrides);
|
||
codex_cli::debug_sandbox::run_command_under_landlock(
|
||
landlock_cli,
|
||
codex_linux_sandbox_exe,
|
||
)
|
||
.await?;
|
||
}
|
||
},
|
||
}
|
||
|
||
Ok(())
|
||
}
|
||
|
||
/// Prepend root-level overrides so they have lower precedence than
|
||
/// CLI-specific ones specified after the subcommand (if any).
|
||
fn prepend_config_flags(
|
||
subcommand_config_overrides: &mut CliConfigOverrides,
|
||
cli_config_overrides: CliConfigOverrides,
|
||
) {
|
||
subcommand_config_overrides
|
||
.raw_overrides
|
||
.splice(0..0, cli_config_overrides.raw_overrides);
|
||
}
|