codex-as-api 0.9.6__tar.gz → 0.10.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/PKG-INFO +133 -25
  2. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/README.md +131 -24
  3. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/RELEASES.md +39 -0
  4. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/THIRD_PARTY_NOTICES.md +26 -0
  5. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/pyproject.toml +2 -1
  6. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/requirements.txt +1 -0
  7. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/rust/THIRD_PARTY_NOTICES.md +26 -0
  8. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/src/codex_as_api/__init__.py +1 -1
  9. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/src/codex_as_api/auth.py +122 -72
  10. codex_as_api-0.10.1/src/codex_as_api/browser_login.py +265 -0
  11. codex_as_api-0.10.1/src/codex_as_api/device_login.py +286 -0
  12. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/src/codex_as_api/provider.py +9 -3
  13. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/src/codex_as_api/server.py +192 -28
  14. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_auth.py +98 -18
  15. codex_as_api-0.10.1/tests/test_browser_login.py +259 -0
  16. codex_as_api-0.10.1/tests/test_device_login.py +304 -0
  17. codex_as_api-0.10.1/tests/test_login_commit_races.py +138 -0
  18. codex_as_api-0.10.1/tests/test_login_generation.py +89 -0
  19. codex_as_api-0.10.1/tests/test_login_http_real_store.py +176 -0
  20. codex_as_api-0.10.1/tests/test_management_body.py +91 -0
  21. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_provider.py +43 -5
  22. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_server.py +7 -3
  23. codex_as_api-0.10.1/tests/test_standalone_login_api.py +178 -0
  24. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/.gitignore +0 -0
  25. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/LICENSE +0 -0
  26. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/config/codex-upstream-contract.json +0 -0
  27. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/config/o200k_base.tiktoken +0 -0
  28. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/config/verified-hidden-models.json +0 -0
  29. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/scripts/check_no_static_model_catalog.py +0 -0
  30. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/scripts/check_package_versions.py +0 -0
  31. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/scripts/check_registry_release.py +0 -0
  32. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/scripts/check_release_artifacts.py +0 -0
  33. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/scripts/generate_rust_third_party_notices.py +0 -0
  34. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/scripts/package_rust_release.py +0 -0
  35. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/src/codex_as_api/anthropic_adapter.py +0 -0
  36. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/src/codex_as_api/codex_config.py +0 -0
  37. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/src/codex_as_api/codex_version.py +0 -0
  38. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/src/codex_as_api/messages.py +0 -0
  39. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/src/codex_as_api/model_capabilities.py +0 -0
  40. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/src/codex_as_api/o200k_tokenizer.py +0 -0
  41. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/src/codex_as_api/protocol.py +0 -0
  42. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/src/codex_as_api/reasoning_signature.py +0 -0
  43. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/src/codex_as_api/strict_json.py +0 -0
  44. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/__init__.py +0 -0
  45. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/conftest.py +0 -0
  46. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/fixtures/o200k_base_encode_ordinary.json +0 -0
  47. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_anthropic_adapter.py +0 -0
  48. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_codex_config.py +0 -0
  49. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_codex_version.py +0 -0
  50. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_messages.py +0 -0
  51. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_model_capabilities.py +0 -0
  52. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_no_static_model_catalog.py +0 -0
  53. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_o200k_tokenizer.py +0 -0
  54. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_package_versions.py +0 -0
  55. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_protocol.py +0 -0
  56. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_registry_release.py +0 -0
  57. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_release_artifacts.py +0 -0
  58. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_rust_third_party_notices.py +0 -0
  59. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_strict_json.py +0 -0
  60. {codex_as_api-0.9.6 → codex_as_api-0.10.1}/tests/test_ultrafast.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: codex-as-api
3
- Version: 0.9.6
3
+ Version: 0.10.1
4
4
  Summary: Use ChatGPT/Codex OAuth as a local OpenAI-compatible API server.
5
5
  Project-URL: Homepage, https://github.com/Eunho-J/codex-as-api
6
6
  Project-URL: Repository, https://github.com/Eunho-J/codex-as-api
@@ -19,6 +19,7 @@ Classifier: Programming Language :: Python :: 3.13
19
19
  Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
20
20
  Requires-Python: >=3.10
21
21
  Requires-Dist: fastapi>=0.100
22
+ Requires-Dist: httpx>=0.27
22
23
  Requires-Dist: pydantic>=2.0
23
24
  Requires-Dist: regex
24
25
  Requires-Dist: tomli>=2; python_version < '3.11'
@@ -52,35 +53,31 @@ Use ChatGPT / Codex OAuth as a local OpenAI-compatible API server.
52
53
  - **Codex features** — live authenticated model discovery, session-aware `prompt_cache_key`, account-scoped Chat continuation, subagent headers, and remote compaction
53
54
  - **Codex config aware** — reads `CODEX_HOME` / `~/.codex/config.toml` for model, reasoning-effort, and context-window settings
54
55
  - **Token estimate & compaction helpers** — Anthropic-compatible `/v1/messages/count_tokens` and `/v1/messages/compact`
55
- - **Managed ChatGPT auth** — reads official `~/.codex/auth.json` token data and refreshes managed OAuth tokens
56
+ - **Standalone ChatGPT login** — ordinary browser OAuth by default, optional device-code login for remote servers, a private application-owned credential store, and managed refresh; no installed Codex CLI or existing Codex login required
56
57
  - **3 implementations** — Python, TypeScript (npm), and Rust — identical behavior
57
58
 
58
59
  ## What it does
59
60
 
60
- Runs a lightweight HTTP server on `localhost` that translates standard OpenAI API calls into authenticated requests against the ChatGPT / Codex backend using your existing `~/.codex/auth.json` OAuth credentials.
61
+ Runs a lightweight HTTP server on `localhost` that translates standard OpenAI API calls into authenticated requests against the ChatGPT / Codex backend. Sign in explicitly through this server's browser OAuth API; it stores and refreshes its own OAuth credentials without reading or changing your Codex login.
61
62
 
62
63
  Python, Rust, and TypeScript (npm) implementations are provided — identical functionality, same endpoints, same behavior.
63
64
 
64
65
  ## Prerequisites
65
66
 
66
- Install the official Codex CLI and log in so that `~/.codex/auth.json` exists:
67
+ A ChatGPT account eligible for the Codex backend and a runtime for your chosen
68
+ implementation are required. The official Codex CLI is **not** required.
67
69
 
68
- ```bash
69
- npm install -g @openai/codex
70
- codex login
71
- ```
72
-
73
- The server reads the official nested `tokens` object from that file and refreshes
74
- managed ChatGPT OAuth tokens automatically. `auth_mode` may be omitted, `null`,
75
- or `"chatgpt"`.
70
+ After starting the server, use [Standalone login](#standalone-login) to open
71
+ OpenAI's ordinary sign-in page in your browser and authorize this server. Credentials are saved privately to
72
+ `~/.codex-as-api/auth.json` by default, and the server handles refresh itself.
73
+ It does not discover, import or fall back to `~/.codex/auth.json`, `CODEX_HOME`
74
+ credentials or a Codex keychain entry. Upgrading from older versions therefore
75
+ requires a fresh login for codex-as-api; your existing Codex login is left alone.
76
76
 
77
- Unofficial root-level `access_token` / `refresh_token` / `id_token` fields and
78
- noncanonical auth-mode aliases are rejected. The official
79
- `"chatgptAuthTokens"` mode is also unsupported: those credentials are owned and
80
- refreshed by an external host application, while this standalone proxy has no
81
- host refresh callback. `personal_access_token`-, `agent_identity`-, and
82
- Bedrock-only auth files are not ChatGPT OAuth credentials; run `codex login` to
83
- create the supported managed-token file.
77
+ Use one server process per credential store. To run independent instances,
78
+ assign each its own `CODEX_AS_API_HOME` or `CODEX_AS_API_AUTH_PATH`.
79
+ Model settings may still be read from `~/.codex/config.toml`; that separate
80
+ configuration behavior is unchanged and does not require installing Codex.
84
81
 
85
82
  ## Install & Run
86
83
 
@@ -163,6 +160,97 @@ app.listen(18080);
163
160
 
164
161
  All versions bind to `127.0.0.1:18080` (localhost only) by default.
165
162
 
163
+ ## Standalone login
164
+
165
+ Login is an API operation; the `codex-as-api` command starts the server, not the
166
+ Codex CLI. Before starting, set `CODEX_AS_API_ADMIN_KEY` to a private random
167
+ administrator key. Supply that same key as a bearer credential on `/auth/*`.
168
+ This key is independent of both the OpenAI OAuth credentials and any inference
169
+ client authentication; never use your access or refresh token as the admin key.
170
+
171
+ 1. Start the server with the key configured, then initiate login from a trusted
172
+ local terminal or authenticated management client:
173
+
174
+ ```bash
175
+ curl -sS -X POST http://127.0.0.1:18080/auth/login \
176
+ -H "Authorization: Bearer $CODEX_AS_API_ADMIN_KEY" \
177
+ -H 'Content-Type: application/json' -d '{}'
178
+ ```
179
+
180
+ 2. The `202` response contains `id`, `method: "browser"`, `status: "pending"`,
181
+ `auth_url` and `expires_at`. Your app opens `auth_url` in the system browser;
182
+ sign in normally on OpenAI's page. No one-time device code or Codex CLI is
183
+ needed. On approval, the browser returns to a separate loopback callback
184
+ listener on the **same machine as the server**, and credentials are saved
185
+ privately. The server tries registered callback port 1455, then 1457; if both
186
+ are occupied it reports `409 callback_port_unavailable` instead of interfering
187
+ with another app. The API itself never launches a browser on a remote server.
188
+ The authorization URL contains temporary sensitive state; do not log or share
189
+ it. Only start login on a server you control.
190
+
191
+ 3. Poll the login ID returned by step 1 (the placeholder below is not a real ID):
192
+
193
+ ```bash
194
+ curl -sS http://127.0.0.1:18080/auth/login/LOGIN_ID \
195
+ -H "Authorization: Bearer $CODEX_AS_API_ADMIN_KEY"
196
+ ```
197
+
198
+ Status becomes `succeeded` only after valid credentials are durably saved.
199
+ Other terminal states are `failed`, `cancelled` and `expired`. Routine status
200
+ responses include the method but never repeat the authorization URL or user
201
+ code, or return OAuth tokens, authorization codes, verifiers, device IDs or
202
+ storage paths.
203
+
204
+ 4. To stop an outstanding attempt, send `DELETE /auth/login/LOGIN_ID` with the same
205
+ administrator bearer. Repeating cancellation is safe. A login already saved
206
+ remains `succeeded`; cancellation is not logout or provider-side revocation.
207
+
208
+ `GET /auth/status` with the administrator bearer returns only
209
+ `{"status":"authenticated"}`, `unauthenticated` or `reauthentication_required`.
210
+ This checks the local application-owned credential state, not provider-side
211
+ revocation or remote account validity; refresh is handled privately when needed.
212
+ Validation, cancellation and failures before credential replacement preserve a
213
+ prior valid login. If replacement succeeds but the final directory sync fails,
214
+ `login_persistence_uncertain` reports that durability could not be confirmed; a
215
+ new local credential may already exist. Cancellation is not a rollback.
216
+
217
+ ### Explicit device login for remote/headless use
218
+
219
+ When the browser and API server are on different machines, a loopback browser
220
+ callback would reach the wrong machine. Explicitly send `{"method":"device"}`
221
+ to `POST /auth/login` instead. It returns `method: "device"`, `verification_url`,
222
+ `user_code`, `interval` and the common session fields. Open
223
+ `https://auth.openai.com/codex/device` in your own browser and enter the code.
224
+ Only approve a code from a login you initiated on a trusted server; never post
225
+ it in public channels or logs. Device-code login may need enabling in your
226
+ OpenAI account. No method silently falls back to another.
227
+
228
+ All management responses use `Cache-Control: no-store`. An unset admin key returns
229
+ `503 management_disabled`; a missing/wrong bearer returns `401 invalid_admin_key`.
230
+ An empty request body, `{}` or `{"method":"browser"}` selects browser login;
231
+ `{"method":"device"}` selects device login. No other body fields are accepted.
232
+ The API does not accept credentials to import, token export switches,
233
+ authentication endpoints or credential paths from a request. One active attempt per store is allowed; another
234
+ start returns `409 login_in_progress`. Unknown/retired IDs return `404`.
235
+
236
+ Keep the default loopback binding unless you deliberately deploy authenticated
237
+ HTTPS administration behind a trusted reverse proxy. A browser `Origin` must
238
+ match the request's own scheme and Host; the API does not blindly trust forwarded
239
+ headers. Configure proxy origins consistently. Origin checking supplements the
240
+ admin bearer and is not a replacement for authentication or network controls.
241
+ Each credential store must be owned by one server process, not shared between
242
+ independent workers. Choose separate store paths for separate instances.
243
+
244
+ ### Note on OpenAI "Sign in with ChatGPT" (SIWC) preview
245
+
246
+ OpenAI's public [Sign in with ChatGPT (`chatgpt.tokens.use.direct`)](https://developers.openai.com/cookbook/articles/sign-in-with-chatgpt)
247
+ preview route (`https://api.openai.com/v1/responses` with dynamic client registration)
248
+ is intentionally kept on hold and not used as the default transport:
249
+
250
+ - Function tools on the SIWC preview route must be wrapped inside a `namespace` tool object rather than sent as top-level function definitions.
251
+ - Remote compaction (`/responses/compact`), `Responses Lite`, hosted `image_generation`, device-code login, and ChatGPT Live / Realtime WebRTC voice calls are not supported on the SIWC preview route.
252
+ - Reliable prompt cache hits on SIWC currently require reusing a persistent WebSocket (`wss://api.openai.com/v1/responses`) rather than stateless HTTP `POST /v1/responses` requests.
253
+
166
254
  ## Configuration
167
255
 
168
256
  Environment variables (Python, Rust, and TypeScript):
@@ -172,10 +260,12 @@ Environment variables (Python, Rust, and TypeScript):
172
260
  | `CODEX_AS_API_HOST` | `127.0.0.1` | Bind address |
173
261
  | `CODEX_AS_API_PORT` | `18080` | Listen port |
174
262
  | `CODEX_AS_API_MODEL` | `~/.codex/config.toml` `model`, else live default | Model identifier passed to Codex backend |
175
- | `CODEX_AS_API_AUTH_PATH` | `~/.codex/auth.json` | Path to OAuth credentials file |
263
+ | `CODEX_AS_API_AUTH_PATH` | `CODEX_AS_API_HOME/auth.json`, else `~/.codex-as-api/auth.json` | Explicit application-owned OAuth store path; not an import command |
264
+ | `CODEX_AS_API_HOME` | `~/.codex-as-api` | Application-owned credential directory |
265
+ | `CODEX_AS_API_ADMIN_KEY` | unset (management disabled) | Independent bearer key for `/auth/*`; never use an OAuth token as this key |
176
266
  | `CODEX_AS_API_RESPONSES_LITE` | `auto` | Responses Lite mode: `auto`, `on`, or `off` |
177
267
  | `CODEX_AS_API_CODEX_METADATA` | `off` | Add Codex-style per-turn `client_metadata` and related backend headers |
178
- | `CODEX_HOME` | `~/.codex` | Codex home directory used for `auth.json` and `config.toml` discovery |
268
+ | `CODEX_HOME` | `~/.codex` | Optional Codex model/config discovery only; never used to find credentials |
179
269
 
180
270
  The server also reads root-level Codex CLI settings from `~/.codex/config.toml`:
181
271
 
@@ -371,6 +461,10 @@ null, or server-tool caller variants still fail before transport.
371
461
 
372
462
  ### Identifier and cache behavior
373
463
 
464
+ For chat requests, initial system messages form `instructions`. System
465
+ messages added after conversation history starts stay at their original positions
466
+ as developer messages, so appending runtime notes preserves the earlier history prefix.
467
+
374
468
  The two compatibility facades intentionally use the similarly named fields for
375
469
  different jobs:
376
470
 
@@ -865,7 +959,7 @@ HTTP Server (FastAPI / Axum / Express)
865
959
  |
866
960
  +---> ChatGPTOAuthProvider
867
961
  |
868
- +---> ~/.codex/auth.json (managed ChatGPT OAuth tokens, auto-refresh)
962
+ +---> ~/.codex-as-api/auth.json (own OAuth credentials, auto-refresh)
869
963
  +---> https://chatgpt.com/backend-api/codex/models
870
964
  +---> https://chatgpt.com/backend-api/codex/responses
871
965
  ```
@@ -912,9 +1006,9 @@ registry setup is still required:
912
1006
  After bumping every version and passing the local gate, push the matching tag:
913
1007
 
914
1008
  ```bash
915
- python scripts/check_package_versions.py --tag v0.8.0
916
- git tag v0.8.0
917
- git push origin v0.8.0
1009
+ python scripts/check_package_versions.py --tag v0.10.1
1010
+ git tag v0.10.1
1011
+ git push origin v0.10.1
918
1012
  ```
919
1013
 
920
1014
  ## Tests
@@ -945,6 +1039,20 @@ npm test
945
1039
 
946
1040
  ## Release Notes
947
1041
 
1042
+ ### v0.10.1
1043
+
1044
+ - Preserve mid-history `system` messages added after conversation history starts as in-place `developer` input items across Python, TypeScript, and Rust chat requests, keeping the earlier conversation history prefix stable for prompt caching while leaving remote compaction (`compact_messages`) unchanged.
1045
+ - Document current preview limitations of OpenAI's public "Sign in with ChatGPT" (`chatgpt.tokens.use.direct`) route and why the Codex OAuth transport remains the default.
1046
+
1047
+ ### v0.10.0
1048
+
1049
+ - Sign in directly with ordinary browser OAuth; no Codex installation or existing
1050
+ Codex credentials are required. Device-code login is an explicit remote option.
1051
+ - Add independent admin-key-protected login start/status/cancel endpoints and a
1052
+ private application-owned credential store across Python, TypeScript and Rust.
1053
+ - Upgrading requires a new codex-as-api login; existing Codex credentials remain
1054
+ untouched. See [Standalone login](#standalone-login) and [full release notes](RELEASES.md).
1055
+
948
1056
  ### v0.8.0
949
1057
 
950
1058
  - Add ChatGPT Live realtime voice to the TypeScript runtime: `POST
@@ -18,35 +18,31 @@ Use ChatGPT / Codex OAuth as a local OpenAI-compatible API server.
18
18
  - **Codex features** — live authenticated model discovery, session-aware `prompt_cache_key`, account-scoped Chat continuation, subagent headers, and remote compaction
19
19
  - **Codex config aware** — reads `CODEX_HOME` / `~/.codex/config.toml` for model, reasoning-effort, and context-window settings
20
20
  - **Token estimate & compaction helpers** — Anthropic-compatible `/v1/messages/count_tokens` and `/v1/messages/compact`
21
- - **Managed ChatGPT auth** — reads official `~/.codex/auth.json` token data and refreshes managed OAuth tokens
21
+ - **Standalone ChatGPT login** — ordinary browser OAuth by default, optional device-code login for remote servers, a private application-owned credential store, and managed refresh; no installed Codex CLI or existing Codex login required
22
22
  - **3 implementations** — Python, TypeScript (npm), and Rust — identical behavior
23
23
 
24
24
  ## What it does
25
25
 
26
- Runs a lightweight HTTP server on `localhost` that translates standard OpenAI API calls into authenticated requests against the ChatGPT / Codex backend using your existing `~/.codex/auth.json` OAuth credentials.
26
+ Runs a lightweight HTTP server on `localhost` that translates standard OpenAI API calls into authenticated requests against the ChatGPT / Codex backend. Sign in explicitly through this server's browser OAuth API; it stores and refreshes its own OAuth credentials without reading or changing your Codex login.
27
27
 
28
28
  Python, Rust, and TypeScript (npm) implementations are provided — identical functionality, same endpoints, same behavior.
29
29
 
30
30
  ## Prerequisites
31
31
 
32
- Install the official Codex CLI and log in so that `~/.codex/auth.json` exists:
32
+ A ChatGPT account eligible for the Codex backend and a runtime for your chosen
33
+ implementation are required. The official Codex CLI is **not** required.
33
34
 
34
- ```bash
35
- npm install -g @openai/codex
36
- codex login
37
- ```
38
-
39
- The server reads the official nested `tokens` object from that file and refreshes
40
- managed ChatGPT OAuth tokens automatically. `auth_mode` may be omitted, `null`,
41
- or `"chatgpt"`.
35
+ After starting the server, use [Standalone login](#standalone-login) to open
36
+ OpenAI's ordinary sign-in page in your browser and authorize this server. Credentials are saved privately to
37
+ `~/.codex-as-api/auth.json` by default, and the server handles refresh itself.
38
+ It does not discover, import or fall back to `~/.codex/auth.json`, `CODEX_HOME`
39
+ credentials or a Codex keychain entry. Upgrading from older versions therefore
40
+ requires a fresh login for codex-as-api; your existing Codex login is left alone.
42
41
 
43
- Unofficial root-level `access_token` / `refresh_token` / `id_token` fields and
44
- noncanonical auth-mode aliases are rejected. The official
45
- `"chatgptAuthTokens"` mode is also unsupported: those credentials are owned and
46
- refreshed by an external host application, while this standalone proxy has no
47
- host refresh callback. `personal_access_token`-, `agent_identity`-, and
48
- Bedrock-only auth files are not ChatGPT OAuth credentials; run `codex login` to
49
- create the supported managed-token file.
42
+ Use one server process per credential store. To run independent instances,
43
+ assign each its own `CODEX_AS_API_HOME` or `CODEX_AS_API_AUTH_PATH`.
44
+ Model settings may still be read from `~/.codex/config.toml`; that separate
45
+ configuration behavior is unchanged and does not require installing Codex.
50
46
 
51
47
  ## Install & Run
52
48
 
@@ -129,6 +125,97 @@ app.listen(18080);
129
125
 
130
126
  All versions bind to `127.0.0.1:18080` (localhost only) by default.
131
127
 
128
+ ## Standalone login
129
+
130
+ Login is an API operation; the `codex-as-api` command starts the server, not the
131
+ Codex CLI. Before starting, set `CODEX_AS_API_ADMIN_KEY` to a private random
132
+ administrator key. Supply that same key as a bearer credential on `/auth/*`.
133
+ This key is independent of both the OpenAI OAuth credentials and any inference
134
+ client authentication; never use your access or refresh token as the admin key.
135
+
136
+ 1. Start the server with the key configured, then initiate login from a trusted
137
+ local terminal or authenticated management client:
138
+
139
+ ```bash
140
+ curl -sS -X POST http://127.0.0.1:18080/auth/login \
141
+ -H "Authorization: Bearer $CODEX_AS_API_ADMIN_KEY" \
142
+ -H 'Content-Type: application/json' -d '{}'
143
+ ```
144
+
145
+ 2. The `202` response contains `id`, `method: "browser"`, `status: "pending"`,
146
+ `auth_url` and `expires_at`. Your app opens `auth_url` in the system browser;
147
+ sign in normally on OpenAI's page. No one-time device code or Codex CLI is
148
+ needed. On approval, the browser returns to a separate loopback callback
149
+ listener on the **same machine as the server**, and credentials are saved
150
+ privately. The server tries registered callback port 1455, then 1457; if both
151
+ are occupied it reports `409 callback_port_unavailable` instead of interfering
152
+ with another app. The API itself never launches a browser on a remote server.
153
+ The authorization URL contains temporary sensitive state; do not log or share
154
+ it. Only start login on a server you control.
155
+
156
+ 3. Poll the login ID returned by step 1 (the placeholder below is not a real ID):
157
+
158
+ ```bash
159
+ curl -sS http://127.0.0.1:18080/auth/login/LOGIN_ID \
160
+ -H "Authorization: Bearer $CODEX_AS_API_ADMIN_KEY"
161
+ ```
162
+
163
+ Status becomes `succeeded` only after valid credentials are durably saved.
164
+ Other terminal states are `failed`, `cancelled` and `expired`. Routine status
165
+ responses include the method but never repeat the authorization URL or user
166
+ code, or return OAuth tokens, authorization codes, verifiers, device IDs or
167
+ storage paths.
168
+
169
+ 4. To stop an outstanding attempt, send `DELETE /auth/login/LOGIN_ID` with the same
170
+ administrator bearer. Repeating cancellation is safe. A login already saved
171
+ remains `succeeded`; cancellation is not logout or provider-side revocation.
172
+
173
+ `GET /auth/status` with the administrator bearer returns only
174
+ `{"status":"authenticated"}`, `unauthenticated` or `reauthentication_required`.
175
+ This checks the local application-owned credential state, not provider-side
176
+ revocation or remote account validity; refresh is handled privately when needed.
177
+ Validation, cancellation and failures before credential replacement preserve a
178
+ prior valid login. If replacement succeeds but the final directory sync fails,
179
+ `login_persistence_uncertain` reports that durability could not be confirmed; a
180
+ new local credential may already exist. Cancellation is not a rollback.
181
+
182
+ ### Explicit device login for remote/headless use
183
+
184
+ When the browser and API server are on different machines, a loopback browser
185
+ callback would reach the wrong machine. Explicitly send `{"method":"device"}`
186
+ to `POST /auth/login` instead. It returns `method: "device"`, `verification_url`,
187
+ `user_code`, `interval` and the common session fields. Open
188
+ `https://auth.openai.com/codex/device` in your own browser and enter the code.
189
+ Only approve a code from a login you initiated on a trusted server; never post
190
+ it in public channels or logs. Device-code login may need enabling in your
191
+ OpenAI account. No method silently falls back to another.
192
+
193
+ All management responses use `Cache-Control: no-store`. An unset admin key returns
194
+ `503 management_disabled`; a missing/wrong bearer returns `401 invalid_admin_key`.
195
+ An empty request body, `{}` or `{"method":"browser"}` selects browser login;
196
+ `{"method":"device"}` selects device login. No other body fields are accepted.
197
+ The API does not accept credentials to import, token export switches,
198
+ authentication endpoints or credential paths from a request. One active attempt per store is allowed; another
199
+ start returns `409 login_in_progress`. Unknown/retired IDs return `404`.
200
+
201
+ Keep the default loopback binding unless you deliberately deploy authenticated
202
+ HTTPS administration behind a trusted reverse proxy. A browser `Origin` must
203
+ match the request's own scheme and Host; the API does not blindly trust forwarded
204
+ headers. Configure proxy origins consistently. Origin checking supplements the
205
+ admin bearer and is not a replacement for authentication or network controls.
206
+ Each credential store must be owned by one server process, not shared between
207
+ independent workers. Choose separate store paths for separate instances.
208
+
209
+ ### Note on OpenAI "Sign in with ChatGPT" (SIWC) preview
210
+
211
+ OpenAI's public [Sign in with ChatGPT (`chatgpt.tokens.use.direct`)](https://developers.openai.com/cookbook/articles/sign-in-with-chatgpt)
212
+ preview route (`https://api.openai.com/v1/responses` with dynamic client registration)
213
+ is intentionally kept on hold and not used as the default transport:
214
+
215
+ - Function tools on the SIWC preview route must be wrapped inside a `namespace` tool object rather than sent as top-level function definitions.
216
+ - Remote compaction (`/responses/compact`), `Responses Lite`, hosted `image_generation`, device-code login, and ChatGPT Live / Realtime WebRTC voice calls are not supported on the SIWC preview route.
217
+ - Reliable prompt cache hits on SIWC currently require reusing a persistent WebSocket (`wss://api.openai.com/v1/responses`) rather than stateless HTTP `POST /v1/responses` requests.
218
+
132
219
  ## Configuration
133
220
 
134
221
  Environment variables (Python, Rust, and TypeScript):
@@ -138,10 +225,12 @@ Environment variables (Python, Rust, and TypeScript):
138
225
  | `CODEX_AS_API_HOST` | `127.0.0.1` | Bind address |
139
226
  | `CODEX_AS_API_PORT` | `18080` | Listen port |
140
227
  | `CODEX_AS_API_MODEL` | `~/.codex/config.toml` `model`, else live default | Model identifier passed to Codex backend |
141
- | `CODEX_AS_API_AUTH_PATH` | `~/.codex/auth.json` | Path to OAuth credentials file |
228
+ | `CODEX_AS_API_AUTH_PATH` | `CODEX_AS_API_HOME/auth.json`, else `~/.codex-as-api/auth.json` | Explicit application-owned OAuth store path; not an import command |
229
+ | `CODEX_AS_API_HOME` | `~/.codex-as-api` | Application-owned credential directory |
230
+ | `CODEX_AS_API_ADMIN_KEY` | unset (management disabled) | Independent bearer key for `/auth/*`; never use an OAuth token as this key |
142
231
  | `CODEX_AS_API_RESPONSES_LITE` | `auto` | Responses Lite mode: `auto`, `on`, or `off` |
143
232
  | `CODEX_AS_API_CODEX_METADATA` | `off` | Add Codex-style per-turn `client_metadata` and related backend headers |
144
- | `CODEX_HOME` | `~/.codex` | Codex home directory used for `auth.json` and `config.toml` discovery |
233
+ | `CODEX_HOME` | `~/.codex` | Optional Codex model/config discovery only; never used to find credentials |
145
234
 
146
235
  The server also reads root-level Codex CLI settings from `~/.codex/config.toml`:
147
236
 
@@ -337,6 +426,10 @@ null, or server-tool caller variants still fail before transport.
337
426
 
338
427
  ### Identifier and cache behavior
339
428
 
429
+ For chat requests, initial system messages form `instructions`. System
430
+ messages added after conversation history starts stay at their original positions
431
+ as developer messages, so appending runtime notes preserves the earlier history prefix.
432
+
340
433
  The two compatibility facades intentionally use the similarly named fields for
341
434
  different jobs:
342
435
 
@@ -831,7 +924,7 @@ HTTP Server (FastAPI / Axum / Express)
831
924
  |
832
925
  +---> ChatGPTOAuthProvider
833
926
  |
834
- +---> ~/.codex/auth.json (managed ChatGPT OAuth tokens, auto-refresh)
927
+ +---> ~/.codex-as-api/auth.json (own OAuth credentials, auto-refresh)
835
928
  +---> https://chatgpt.com/backend-api/codex/models
836
929
  +---> https://chatgpt.com/backend-api/codex/responses
837
930
  ```
@@ -878,9 +971,9 @@ registry setup is still required:
878
971
  After bumping every version and passing the local gate, push the matching tag:
879
972
 
880
973
  ```bash
881
- python scripts/check_package_versions.py --tag v0.8.0
882
- git tag v0.8.0
883
- git push origin v0.8.0
974
+ python scripts/check_package_versions.py --tag v0.10.1
975
+ git tag v0.10.1
976
+ git push origin v0.10.1
884
977
  ```
885
978
 
886
979
  ## Tests
@@ -911,6 +1004,20 @@ npm test
911
1004
 
912
1005
  ## Release Notes
913
1006
 
1007
+ ### v0.10.1
1008
+
1009
+ - Preserve mid-history `system` messages added after conversation history starts as in-place `developer` input items across Python, TypeScript, and Rust chat requests, keeping the earlier conversation history prefix stable for prompt caching while leaving remote compaction (`compact_messages`) unchanged.
1010
+ - Document current preview limitations of OpenAI's public "Sign in with ChatGPT" (`chatgpt.tokens.use.direct`) route and why the Codex OAuth transport remains the default.
1011
+
1012
+ ### v0.10.0
1013
+
1014
+ - Sign in directly with ordinary browser OAuth; no Codex installation or existing
1015
+ Codex credentials are required. Device-code login is an explicit remote option.
1016
+ - Add independent admin-key-protected login start/status/cancel endpoints and a
1017
+ private application-owned credential store across Python, TypeScript and Rust.
1018
+ - Upgrading requires a new codex-as-api login; existing Codex credentials remain
1019
+ untouched. See [Standalone login](#standalone-login) and [full release notes](RELEASES.md).
1020
+
914
1021
  ### v0.8.0
915
1022
 
916
1023
  - Add ChatGPT Live realtime voice to the TypeScript runtime: `POST
@@ -1,5 +1,44 @@
1
1
  # Release Notes
2
2
 
3
+ ## v0.10.1
4
+
5
+ ### Preserve late system notes across Python, TypeScript and Rust chat histories
6
+
7
+ - Keep initial `system` messages in top-level `instructions`, and preserve `system`
8
+ messages appended after conversation history starts at their original positions as
9
+ `developer` input items across Python, TypeScript and Rust chat requests.
10
+ Appending per-turn runtime notes after tool results no longer mutates
11
+ `instructions` or invalidates the cached prefix of the prior conversation history.
12
+ - Remote compaction (`/v1/compact` and `/v1/messages/compact`) remains unchanged so
13
+ system directives continue to form `instructions` during compaction.
14
+ - Document current preview constraints of OpenAI's public "Sign in with ChatGPT"
15
+ (`chatgpt.tokens.use.direct`) route in `README.md`.
16
+
17
+ ## v0.10.0
18
+
19
+ ### Standalone browser login — no Codex installation required
20
+
21
+ - Python, TypeScript and Rust now own their ChatGPT OAuth login and refresh
22
+ lifecycle. `POST /auth/login` starts ordinary browser OAuth with PKCE and a
23
+ loopback callback by default. The trusted application opens the returned
24
+ `auth_url`; progress and cancellation are available through the management API.
25
+ - Use `CODEX_AS_API_ADMIN_KEY` as the independent management bearer credential.
26
+ Management is disabled without it; status responses never export OAuth tokens.
27
+ - Credentials are privately and atomically stored in
28
+ `CODEX_AS_API_AUTH_PATH`, `CODEX_AS_API_HOME/auth.json`, or
29
+ `~/.codex-as-api/auth.json`. Existing Codex credentials are neither discovered
30
+ nor imported. **Upgrading requires signing in once for codex-as-api.**
31
+ - Explicit `{"method":"device"}` remains available for remote/headless servers.
32
+ There is no silent fallback between login methods. Ordinary browser login
33
+ expects the browser and server to run on the same machine, using registered
34
+ callback port 1455 or 1457.
35
+ - Cancellation, expiry and failures before replacement preserve the previous
36
+ login. A post-replacement durability failure is reported separately, and an
37
+ already committed login is not rolled back by cancellation. Local status is
38
+ not a provider-side validity or revocation check.
39
+ - Preserve the pinned OpenAI Codex Apache-2.0 attribution in distributed artifacts.
40
+ Model configuration and existing inference/Realtime behavior are unchanged.
41
+
3
42
  ## v0.9.6
4
43
 
5
44
  ### Sideband join failure says why
@@ -1,5 +1,31 @@
1
1
  # Third-Party Notices
2
2
 
3
+ ## OpenAI Codex — standalone OAuth login protocol port
4
+
5
+ The standalone browser OAuth and device-code login implementations are adapted
6
+ from OpenAI Codex (`openai/codex`, Apache License 2.0), pinned to
7
+ `rust-v0.164.0`, commit `6aaac694803004a63c6f393d153c96d22521a6b9`, in particular
8
+ `codex-rs/login/src/server.rs`, `codex-rs/login/src/device_code_auth.rs`, and
9
+ `codex-rs/login/src/oauth/{authorization,pkce,client}.rs`.
10
+ The Apache License 2.0 is included in this distribution's `LICENSE` file.
11
+
12
+ Local modifications implement equivalent Python, TypeScript and Rust login
13
+ managers and authenticated REST start/status/cancel endpoints; use a dedicated
14
+ codex-as-api credential store with no discovery/import of existing Codex
15
+ credentials; add bounded deadlines, cancellation, atomic storage and private
16
+ refresh ownership. This is not an unmodified distribution of the Codex CLI.
17
+
18
+ The upstream NOTICE is retained below. The login port does not include the
19
+ Codex terminal UI or use Ratatui; its attribution is retained as part of the
20
+ upstream notice rather than a claim that the terminal UI is bundled here.
21
+
22
+ > OpenAI Codex
23
+ > Copyright 2025 OpenAI
24
+ >
25
+ > This project includes code derived from [Ratatui](https://github.com/ratatui/ratatui), licensed under the MIT license.
26
+ > Copyright (c) 2016-2022 Florian Dehau
27
+ > Copyright (c) 2023-2025 The Ratatui Developers
28
+
3
29
  ## OpenAI tiktoken
4
30
 
5
31
  This project includes a port of the `o200k_base` ordinary-encoding logic and
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "codex-as-api"
7
- version = "0.9.6"
7
+ version = "0.10.1"
8
8
  description = "Use ChatGPT/Codex OAuth as a local OpenAI-compatible API server."
9
9
  readme = "README.md"
10
10
  license = { text = "Apache-2.0" }
@@ -26,6 +26,7 @@ classifiers = [
26
26
  ]
27
27
  dependencies = [
28
28
  "fastapi>=0.100",
29
+ "httpx>=0.27",
29
30
  "pydantic>=2.0",
30
31
  "regex",
31
32
  "tomli>=2; python_version < '3.11'",
@@ -1,6 +1,7 @@
1
1
  regex
2
2
 
3
3
  fastapi>=0.100
4
+ httpx>=0.27
4
5
  pydantic>=2.0
5
6
  uvicorn>=0.23
6
7
 
@@ -6,6 +6,32 @@ Generated by `python scripts/generate_rust_third_party_notices.py`.
6
6
 
7
7
  # Third-Party Notices
8
8
 
9
+ ## OpenAI Codex — standalone OAuth login protocol port
10
+
11
+ The standalone browser OAuth and device-code login implementations are adapted
12
+ from OpenAI Codex (`openai/codex`, Apache License 2.0), pinned to
13
+ `rust-v0.164.0`, commit `6aaac694803004a63c6f393d153c96d22521a6b9`, in particular
14
+ `codex-rs/login/src/server.rs`, `codex-rs/login/src/device_code_auth.rs`, and
15
+ `codex-rs/login/src/oauth/{authorization,pkce,client}.rs`.
16
+ The Apache License 2.0 is included in this distribution's `LICENSE` file.
17
+
18
+ Local modifications implement equivalent Python, TypeScript and Rust login
19
+ managers and authenticated REST start/status/cancel endpoints; use a dedicated
20
+ codex-as-api credential store with no discovery/import of existing Codex
21
+ credentials; add bounded deadlines, cancellation, atomic storage and private
22
+ refresh ownership. This is not an unmodified distribution of the Codex CLI.
23
+
24
+ The upstream NOTICE is retained below. The login port does not include the
25
+ Codex terminal UI or use Ratatui; its attribution is retained as part of the
26
+ upstream notice rather than a claim that the terminal UI is bundled here.
27
+
28
+ > OpenAI Codex
29
+ > Copyright 2025 OpenAI
30
+ >
31
+ > This project includes code derived from [Ratatui](https://github.com/ratatui/ratatui), licensed under the MIT license.
32
+ > Copyright (c) 2016-2022 Florian Dehau
33
+ > Copyright (c) 2023-2025 The Ratatui Developers
34
+
9
35
  ## OpenAI tiktoken
10
36
 
11
37
  This project includes a port of the `o200k_base` ordinary-encoding logic and
@@ -1,3 +1,3 @@
1
1
  """codex-as-api: Use ChatGPT/Codex OAuth as a local OpenAI-compatible API server."""
2
2
 
3
- __version__ = "0.9.6"
3
+ __version__ = "0.10.1"