core-agent-ide/codex-cli/src/utils
Michael Bolin 515b6331bd
feat: add support for login with ChatGPT (#1212)
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:


a67a67f325/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
2025-06-04 08:44:17 -07:00
..
agent add: sign in with chatgpt (#963) 2025-05-16 12:28:54 -07:00
logger chore: improve storage/ implementation; use log(...) consistently (#473) 2025-04-21 09:51:34 -04:00
singlepass refactor: improve performance of renderFilesToXml using Array.join (#127) 2025-04-16 16:14:57 -07:00
storage chore: improve storage/ implementation; use log(...) consistently (#473) 2025-04-21 09:51:34 -04:00
approximate-tokens-used.ts fix(cli): ensure /clear resets context and exclude system messages from approximateTokenUsed count (#443) 2025-04-20 08:52:14 -07:00
auto-approval-mode.js Initial commit 2025-04-16 12:56:08 -04:00
auto-approval-mode.ts Initial commit 2025-04-16 12:56:08 -04:00
bug-report.ts fix: /bug report command, thinking indicator (#381) 2025-04-18 18:13:34 -07:00
check-in-git.ts Initial commit 2025-04-16 12:56:08 -04:00
check-updates.ts fix: always load version from package.json at runtime (#909) 2025-05-12 21:27:15 -07:00
compact-summary.ts Adds Azure OpenAI support (#769) 2025-05-09 18:11:32 -07:00
config.ts add: sign in with chatgpt (#963) 2025-05-16 12:28:54 -07:00
extract-applied-patches.ts feat: /diff command to view git diff (#426) 2025-04-19 16:23:27 -07:00
file-system-suggestions.ts feat: @mention files in codex (#701) 2025-04-30 16:19:55 -07:00
file-tag-utils.ts feat: @mention files in codex (#701) 2025-04-30 16:19:55 -07:00
get-api-key-components.tsx add: sign in with chatgpt (#963) 2025-05-16 12:28:54 -07:00
get-api-key.tsx feat: add support for login with ChatGPT (#1212) 2025-06-04 08:44:17 -07:00
get-diff.ts fix: diff command for filenames with special characters (#954) 2025-05-16 09:10:44 -07:00
input-utils.ts Add fallback text for missing images (#397) 2025-04-18 22:55:24 -07:00
model-info.ts add: codex-mini-latest (#951) 2025-05-16 08:04:00 -07:00
model-utils.ts Adds Azure OpenAI support (#769) 2025-05-09 18:11:32 -07:00
openai-client.ts Adds Azure OpenAI support (#769) 2025-05-09 18:11:32 -07:00
package-manager-detector.ts refactor(updates): fetch version from registry instead of npm CLI to support multiple managers (#446) 2025-04-21 00:00:20 -07:00
parsers.ts fix: reasoning default to medium, show workdir when supplied (#931) 2025-05-14 08:38:41 -07:00
providers.ts feat: added arceeai as a provider (#818) 2025-05-10 16:16:28 -07:00
responses.ts fix: guard against missing choices (#817) 2025-05-10 16:16:19 -07:00
session.ts fix: always load version from package.json at runtime (#909) 2025-05-12 21:27:15 -07:00
short-path.ts Initial commit 2025-04-16 12:56:08 -04:00
slash-commands.ts add: session history viewer (#912) 2025-05-16 12:28:22 -07:00
terminal-chat-utils.ts chore: consolidate model utils and drive-by cleanups (#476) 2025-04-21 12:33:57 -04:00
terminal.ts fix: /clear now clears terminal screen and resets context left indicator (#425) 2025-04-21 12:39:46 -04:00