Skip to content

Commit b94e169

Browse files
author
Numman Ali
committed
v1.0.0: Production-ready OpenAI Codex OAuth plugin
Features: - ChatGPT Plus/Pro OAuth authentication with auto-refresh - Auto-updating Codex instructions from GitHub (24h cache) - Zero dependencies except @openauthjs/openauth - Modular architecture (auth, codex, server modules) - Full tool support with automatic remapping - High reasoning effort with detailed thinking blocks Package size: 13.5 kB compressed, 36.7 kB unpacked
0 parents  commit b94e169

File tree

10 files changed

+999
-0
lines changed

10 files changed

+999
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules/
2+
bun.lockb
3+
pnpm-lock.yaml
4+
package-lock.json
5+
dist/
6+
.DS_Store
7+

.npmignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.git
2+
.gitignore
3+
.DS_Store
4+
node_modules/
5+
bun.lockb
6+
pnpm-lock.yaml
7+
package-lock.json
8+
opencode.json
9+
test-*.mjs
10+
*.log
11+
.vscode/
12+
.idea/
13+
docs/
14+
.github/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Numman Ali
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# OpenAI ChatGPT OAuth Plugin for opencode
2+
3+
This plugin enables opencode to use OpenAI's Codex backend via ChatGPT Plus/Pro OAuth authentication, allowing you to use your ChatGPT subscription instead of OpenAI Platform API credits.
4+
5+
## Features
6+
7+
- ✅ ChatGPT Plus/Pro OAuth authentication
8+
-**Zero external dependencies** - Lightweight with only @openauthjs/openauth
9+
-**Auto-refreshing tokens** - Handles token expiration automatically
10+
-**Auto-updating Codex instructions** - Fetches latest from OpenAI's Codex repo (cached 24h)
11+
- ✅ Full tool support (write, edit, bash, grep, etc.)
12+
- ✅ Automatic tool remapping (Codex tools → opencode tools)
13+
- ✅ High reasoning effort with detailed thinking blocks
14+
- ✅ Modular architecture for easy maintenance
15+
16+
## Installation
17+
18+
### From npm (Recommended)
19+
20+
```bash
21+
npm install opencode-openai-codex-auth
22+
```
23+
24+
Add the plugin to your `opencode.json`:
25+
26+
```json
27+
{
28+
"$schema": "https://opencode.ai/config.json",
29+
"plugin": [
30+
"opencode-openai-codex-auth"
31+
],
32+
"model": "openai/gpt-5-codex"
33+
}
34+
```
35+
36+
### From source
37+
38+
1. Clone this repository:
39+
40+
```bash
41+
git clone https://github.com/numman/opencode-openai-codex-auth.git
42+
cd opencode-openai-codex-auth
43+
npm install
44+
```
45+
46+
2. Add the plugin to your `opencode.json`:
47+
48+
```json
49+
{
50+
"$schema": "https://opencode.ai/config.json",
51+
"plugin": [
52+
"file:///absolute/path/to/opencode-openai-codex-auth"
53+
],
54+
"model": "openai/gpt-5-codex"
55+
}
56+
```
57+
58+
## Authentication
59+
60+
Login with ChatGPT OAuth:
61+
62+
```bash
63+
opencode auth login
64+
```
65+
66+
Select "OpenAI" and choose:
67+
- **"ChatGPT Plus/Pro (Codex Subscription)"** - Opens browser automatically for OAuth flow
68+
69+
> **Important**: Make sure the official Codex CLI is not running during first login, as both use port 1455 for OAuth callback. After initial authentication, this won't be an issue.
70+
71+
## Usage
72+
73+
```bash
74+
# Use gpt-5-codex with high reasoning (default)
75+
opencode run "create a hello world file" --model=openai/gpt-5-codex
76+
77+
# Or set as default in opencode.json
78+
opencode run "solve this complex algorithm problem"
79+
```
80+
81+
The plugin automatically configures:
82+
- **High reasoning effort** for deep thinking
83+
- **Detailed reasoning summaries** to show thought process
84+
- **Medium text verbosity** for balanced output
85+
86+
## How It Works
87+
88+
The plugin:
89+
90+
1. **Authentication**: Uses ChatGPT OAuth flow with PKCE for secure authentication
91+
2. **Token Management**: Auto-refreshes tokens using `ai-sdk-provider-chatgpt-oauth`
92+
3. **Codex Instructions**: Automatically fetches latest instructions from [openai/codex](https://github.com/openai/codex) repository
93+
- Cached locally in `~/.opencode/cache/` for 24 hours
94+
- Falls back to bundled version if GitHub is unavailable
95+
4. **Request Transformation**: Routes requests to `https://chatgpt.com/backend-api/codex/responses`
96+
5. **Model Normalization**: Maps all model names to `gpt-5-codex` (the Codex backend model)
97+
6. **Tool Remapping**: Injects instructions to map Codex tools to opencode tools:
98+
- `apply_patch``edit`
99+
- `update_plan``todowrite`
100+
7. **Reasoning Configuration**: Forces high reasoning effort with detailed summaries
101+
8. **History Filtering**: Removes stored conversation IDs since Codex uses `store: false`
102+
103+
## Limitations
104+
105+
- **ChatGPT Plus/Pro required**: Must have an active ChatGPT Plus or Pro subscription
106+
- **Medium text verbosity**: Codex only supports `medium` for text verbosity
107+
108+
## Troubleshooting
109+
110+
### Authentication Issues
111+
112+
- Ensure you have an active ChatGPT Plus or Pro subscription
113+
- Try re-logging in with `opencode auth login`
114+
- Check browser console during OAuth flow if auto-login fails
115+
116+
### Tool Execution Issues
117+
118+
- Verify plugin is loaded in `opencode.json`
119+
- Check that model is set to `openai/gpt-5-codex`
120+
- Check `~/.opencode/cache/` for cached instructions (auto-downloads from GitHub)
121+
122+
### Request Errors
123+
124+
- **401 Unauthorized**: Token expired, run `opencode auth login` again
125+
- **400 Bad Request**: Check console output for specific error details
126+
- **403 Forbidden**: Subscription may be expired or invalid
127+
128+
## Project Structure
129+
130+
```
131+
opencode-openai-codex-auth/
132+
├── index.mjs # Main plugin entry point
133+
├── lib/
134+
│ ├── auth.mjs # OAuth authentication logic
135+
│ ├── codex.mjs # Codex instructions & tool remapping
136+
│ ├── server.mjs # Local OAuth callback server
137+
│ └── codex-instructions.md # Bundled Codex instructions (fallback)
138+
├── package.json
139+
├── README.md
140+
└── LICENSE
141+
```
142+
143+
### Module Overview
144+
145+
- **index.mjs**: Main plugin export and request transformation
146+
- **lib/auth.mjs**: OAuth flow, PKCE, token exchange, JWT decoding
147+
- **lib/codex.mjs**: Fetches/caches Codex instructions from GitHub, tool remapping
148+
- **lib/server.mjs**: Local HTTP server for OAuth callback handling
149+
150+
## Credits
151+
152+
Based on research and working implementations from:
153+
- [ben-vargas/ai-sdk-provider-chatgpt-oauth](https://github.com/ben-vargas/ai-sdk-provider-chatgpt-oauth)
154+
- [ben-vargas/ai-opencode-chatgpt-auth](https://github.com/ben-vargas/ai-opencode-chatgpt-auth)
155+
- [openai/codex](https://github.com/openai/codex) OAuth flow
156+
- [sst/opencode](https://github.com/sst/opencode)
157+
158+
## License
159+
160+
MIT

0 commit comments

Comments
 (0)