|
| 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