|
| 1 | +# CodexSwitch |
| 2 | + |
| 3 | +[English](README.md) | [简体中文](README.zh-CN.md) |
| 4 | + |
| 5 | +[](https://github.com/AIDotNet/CodexSwitch/stargazers) |
| 6 | +[](https://github.com/AIDotNet/CodexSwitch/actions/workflows/ci.yml) |
| 7 | +[](https://dotnet.microsoft.com/) |
| 8 | +[](https://avaloniaui.net/) |
| 9 | + |
| 10 | +CodexSwitch is a cross-platform Avalonia desktop app that turns Codex into a local, switchable AI provider workstation. It runs an OpenAI Responses-compatible proxy on your machine, writes a managed Codex configuration, routes requests across multiple upstream providers, converts protocol dialects when needed, and keeps local usage and cost records. |
| 11 | + |
| 12 | +The default local endpoint is: |
| 13 | + |
| 14 | +```text |
| 15 | +http://127.0.0.1:12785/v1 |
| 16 | +``` |
| 17 | + |
| 18 | +## What It Does |
| 19 | + |
| 20 | +Codex talks to CodexSwitch as if it were an OpenAI Responses API endpoint. CodexSwitch then selects the right provider for the requested model, rewrites model and service-tier settings, converts requests and streaming responses for providers that use different APIs, and records token usage in a local JSONL log. |
| 21 | + |
| 22 | +```mermaid |
| 23 | +flowchart LR |
| 24 | + Codex["Codex client"] --> Proxy["CodexSwitch local /v1 proxy"] |
| 25 | + Proxy --> Router["Provider and model routing"] |
| 26 | + Router --> OpenAI["OpenAI Responses"] |
| 27 | + Router --> Chat["OpenAI-compatible Chat"] |
| 28 | + Router --> Anthropic["Anthropic Messages"] |
| 29 | + OpenAI --> Usage["Usage and cost log"] |
| 30 | + Chat --> Usage |
| 31 | + Anthropic --> Usage |
| 32 | +``` |
| 33 | + |
| 34 | +## Features |
| 35 | + |
| 36 | +- Local proxy with `/health`, `/v1/models`, and `/v1/responses` endpoints. |
| 37 | +- One-click active provider switching from the desktop UI. |
| 38 | +- Built-in provider templates for RoutinAI, RoutinAI Plan, OpenAI Official, Anthropic Messages, DeepSeek, and Xiaomi MiMo. |
| 39 | +- Dedicated Codex OAuth login flow for the ChatGPT Codex backend, including multi-account management. |
| 40 | +- Custom provider support with configurable base URL, API key, protocol, model routes, upstream model names, and service tiers. |
| 41 | +- Protocol adapters for OpenAI Responses, OpenAI-compatible Chat Completions, and Anthropic Messages. |
| 42 | +- Streaming support with Responses-style SSE events where the upstream protocol allows it. |
| 43 | +- Model routing by requested model, including aliases and upstream model remapping. |
| 44 | +- Local model pricing catalog with tiered pricing, cache-read pricing, Claude cache-creation billing, and fast-mode multipliers. |
| 45 | +- Usage dashboard with request logs, provider statistics, model statistics, 24-hour, 7-day, and 30-day trends. |
| 46 | +- Managed Codex config and auth file writing with backup and restore support. |
| 47 | +- Light, dark, and system themes with localized UI resources. |
| 48 | +- GitHub Release update checks from inside the app. |
| 49 | + |
| 50 | +## Supported Protocols |
| 51 | + |
| 52 | +| Provider protocol | Upstream endpoint | Notes | |
| 53 | +| --- | --- | --- | |
| 54 | +| `OpenAiResponses` | `/responses` | Passes Responses requests through with model, service-tier, and request override handling. | |
| 55 | +| `OpenAiChat` | `/chat/completions` | Converts inbound Responses requests to Chat Completions and converts the result back to Responses shape. | |
| 56 | +| `AnthropicMessages` | `/messages` | Converts inbound Responses requests to Anthropic Messages, including tool calls, thinking/reasoning mapping, and usage normalization where supported. | |
| 57 | + |
| 58 | +CodexSwitch currently exposes Responses-style inbound routes. It does not expose `/v1/chat/completions` as a public inbound API. |
| 59 | + |
| 60 | +## Built-In Providers |
| 61 | + |
| 62 | +| Provider | Default protocol | Example default model | |
| 63 | +| --- | --- | --- | |
| 64 | +| RoutinAI | OpenAI Responses | `gpt-5.5` | |
| 65 | +| RoutinAI Plan | OpenAI Responses | `gpt-5.5` | |
| 66 | +| OpenAI Official | OpenAI Responses | `gpt-5.5` | |
| 67 | +| Anthropic Messages | Anthropic Messages | `claude-sonnet-4-5` | |
| 68 | +| DeepSeek | OpenAI Chat | `deepseek-v4-flash` | |
| 69 | +| Xiaomi MiMo | OpenAI Chat | `mimo-v2.5-pro` | |
| 70 | +| Codex OAuth | OpenAI Responses | `gpt-5.1-codex` | |
| 71 | + |
| 72 | +You can add custom providers when an upstream service is OpenAI Responses-compatible, OpenAI Chat-compatible, or Anthropic Messages-compatible. |
| 73 | + |
| 74 | +## Installation |
| 75 | + |
| 76 | +Download published builds from [GitHub Releases](https://github.com/AIDotNet/CodexSwitch/releases) when release artifacts are available. |
| 77 | + |
| 78 | +Current CI publish targets are: |
| 79 | + |
| 80 | +- `win-x64` |
| 81 | +- `linux-x64` |
| 82 | +- `osx-x64` |
| 83 | +- `osx-arm64` |
| 84 | + |
| 85 | +## Run From Source |
| 86 | + |
| 87 | +Prerequisite: .NET SDK `10.0.203`, as pinned by `global.json`. |
| 88 | + |
| 89 | +```powershell |
| 90 | +git clone https://github.com/AIDotNet/CodexSwitch.git |
| 91 | +cd CodexSwitch |
| 92 | +dotnet restore CodexSwitch.Tests/CodexSwitch.Tests.csproj |
| 93 | +dotnet run --project CodexSwitch/CodexSwitch.csproj |
| 94 | +``` |
| 95 | + |
| 96 | +## First Run |
| 97 | + |
| 98 | +1. Open CodexSwitch. |
| 99 | +2. Choose a built-in provider template or create a custom provider. |
| 100 | +3. Add the provider API key, or use the Codex OAuth login button for the Codex OAuth provider. |
| 101 | +4. Set the provider as active. |
| 102 | +5. Keep the local proxy enabled. Codex clients can use `http://127.0.0.1:12785/v1`. |
| 103 | + |
| 104 | +When the proxy starts, CodexSwitch writes a managed Codex profile to the user's `.codex` directory. When the proxy stops, the original Codex files are restored from the local backup state. |
| 105 | + |
| 106 | +## Local Files |
| 107 | + |
| 108 | +CodexSwitch stores application state in the user's application data folder. |
| 109 | + |
| 110 | +On Windows, the main files are: |
| 111 | + |
| 112 | +| File | Purpose | |
| 113 | +| --- | --- | |
| 114 | +| `%APPDATA%\CodexSwitch\config.json` | Providers, routing, UI, proxy, OAuth account metadata, and local settings. | |
| 115 | +| `%APPDATA%\CodexSwitch\model-pricing.json` | Editable pricing catalog used for cost estimates. | |
| 116 | +| `%APPDATA%\CodexSwitch\usage-log.jsonl` | Local request usage log. | |
| 117 | +| `%APPDATA%\CodexSwitch\icons\` | Cached provider and model icons. | |
| 118 | +| `%APPDATA%\CodexSwitch\backups\` | Backups made before Codex config/auth writes. | |
| 119 | +| `%APPDATA%\CodexSwitch\codex-restore-state.json` | State used to restore the original Codex files. | |
| 120 | +| `%USERPROFILE%\.codex\config.toml` | Managed Codex config while the proxy is enabled. | |
| 121 | +| `%USERPROFILE%\.codex\auth.json` | Managed Codex auth while the proxy is enabled. | |
| 122 | + |
| 123 | +Treat the config files as sensitive because they can contain API keys and OAuth tokens. |
| 124 | + |
| 125 | +## Development |
| 126 | + |
| 127 | +Restore, build, and test: |
| 128 | + |
| 129 | +```powershell |
| 130 | +dotnet restore CodexSwitch.Tests/CodexSwitch.Tests.csproj |
| 131 | +dotnet build CodexSwitch.Tests/CodexSwitch.Tests.csproj -c Release --no-restore |
| 132 | +dotnet test CodexSwitch.Tests/CodexSwitch.Tests.csproj -c Release --no-build --no-restore |
| 133 | +``` |
| 134 | + |
| 135 | +Publish a self-contained build: |
| 136 | + |
| 137 | +```powershell |
| 138 | +dotnet publish CodexSwitch/CodexSwitch.csproj -c Release -r win-x64 --self-contained true |
| 139 | +``` |
| 140 | + |
| 141 | +Validate the changelog before release work: |
| 142 | + |
| 143 | +```powershell |
| 144 | +./build/Validate-Changelog.ps1 |
| 145 | +``` |
| 146 | + |
| 147 | +## Project Structure |
| 148 | + |
| 149 | +```text |
| 150 | +CodexSwitch/ |
| 151 | + Controls/ Reusable Avalonia controls |
| 152 | + I18n/ Runtime localization services and markup extension |
| 153 | + Models/ App config, provider, pricing, and usage models |
| 154 | + Proxy/ Local proxy host, routing, protocol adapters, payload builders |
| 155 | + Services/ Config storage, Codex config writing, usage, pricing, icons, updates |
| 156 | + Styles/ Avalonia theme and component styles |
| 157 | + ViewModels/ Main desktop application state and commands |
| 158 | + Views/ Windows, pages, dialogs, and shell components |
| 159 | +CodexSwitch.Tests/ xUnit coverage for routing, pricing, config writing, usage, i18n, and updates |
| 160 | +build/ Release and validation scripts |
| 161 | +docs/ Release process notes |
| 162 | +``` |
| 163 | + |
| 164 | +## CI and Release Flow |
| 165 | + |
| 166 | +The `ci` workflow validates the changelog, restores packages, builds the test project, runs xUnit tests, and publishes zipped artifacts for Windows, Linux, and macOS runners. GitHub Releases are created manually using the artifacts produced by CI. See [docs/release.md](docs/release.md). |
| 167 | + |
| 168 | +## Current Scope |
| 169 | + |
| 170 | +- The Claude Code page exists, but this build does not write Claude-specific local configuration yet. |
| 171 | +- Keep the proxy host on `127.0.0.1` unless you intentionally want to expose it beyond your machine. |
| 172 | +- Cost estimates depend on the local pricing catalog and should be treated as estimates, not billing statements. |
| 173 | + |
| 174 | +## Contributing |
| 175 | + |
| 176 | +Issues and pull requests are welcome. For changes that affect routing, protocol conversion, pricing, usage accounting, or config writing, please include focused tests in `CodexSwitch.Tests`. |
| 177 | + |
| 178 | +Before opening a pull request: |
| 179 | + |
| 180 | +```powershell |
| 181 | +dotnet test CodexSwitch.Tests/CodexSwitch.Tests.csproj -c Release |
| 182 | +./build/Validate-Changelog.ps1 |
| 183 | +``` |
| 184 | + |
| 185 | +## License |
| 186 | + |
| 187 | +This repository does not currently declare a license. Add a `LICENSE` file before distributing the project as open-source software or accepting external contributions. |
| 188 | + |
| 189 | +## Star History |
| 190 | + |
| 191 | +[](https://www.star-history.com/#AIDotNet/CodexSwitch&Date) |
0 commit comments