opencode2api-proxy 0.0.0-stage → 1.0.0
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.
- package/LICENSE +621 -0
- package/README.md +541 -2
- package/config.example.json +61 -0
- package/dist/cli.js +10331 -0
- package/dist/cli.js.map +7 -0
- package/package.json +49 -4
- package/worker/worker.js +2621 -0
package/README.md
CHANGED
|
@@ -1,3 +1,542 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<img src="assets/images/OpenCode2API_Banner.webp" alt="OpenCode2API Banner" width="100%">
|
|
4
|
+
|
|
5
|
+
# OpenCode2API-Proxy
|
|
6
|
+
|
|
7
|
+
[](https://www.gnu.org/licenses/gpl-3.0)
|
|
8
|
+
[](https://www.npmjs.com/package/opencode2api-proxy)
|
|
9
|
+
[](https://github.com/IMROVOID/OpenCode2API-Proxy/actions/workflows/test.yml)
|
|
10
|
+
[](https://nodejs.org/)
|
|
11
|
+
[](https://www.typescriptlang.org/)
|
|
12
|
+
[](https://workers.cloudflare.com/)
|
|
13
|
+
[](https://opencode.ai/)
|
|
14
|
+
[](https://platform.openai.com/docs/api-reference)
|
|
15
|
+
|
|
16
|
+
<p align="center">
|
|
17
|
+
OpenCode Zen and Go to a universal OpenAI, Responses and Anthropic compatible API router. Serve <b>Zen</b> and <b>Go</b> models from one endpoint with <b>protocol conversion, streaming, key rotation and a built-in control WebUI</b>.
|
|
18
|
+
</p>
|
|
19
|
+
|
|
20
|
+
[Overview](#what-is-opencode2api-proxy) • [Features](#key-features) • [Quick Start](#quick-start) • [Models](#model-catalog--aliases) • [Clients](#client-integration) • [Architecture](#architecture--structure) • [Guides](#advanced-guides) • [Disclaimer](#disclaimer--legal-notice) • [References](#references--acknowledgements) • [License](#license)
|
|
21
|
+
|
|
22
|
+
</div>
|
|
23
|
+
|
|
24
|
+
## Table of Contents
|
|
25
|
+
|
|
26
|
+
- [What is OpenCode2API-Proxy?](#what-is-opencode2api-proxy)
|
|
27
|
+
- [Key Features](#key-features)
|
|
28
|
+
- [Quick Start](#quick-start)
|
|
29
|
+
- [Method 1: Local Daemon](#method-1-local-daemon)
|
|
30
|
+
- [Method 2: Built Release](#method-2-built-release)
|
|
31
|
+
- [Method 3: Cloudflare Workers (Serverless)](#method-3-cloudflare-workers-serverless)
|
|
32
|
+
- [Method 4: npm](#method-4-npm)
|
|
33
|
+
- [Model Catalog & Aliases](#model-catalog--aliases)
|
|
34
|
+
- [Client Integration](#client-integration)
|
|
35
|
+
- [Architecture & Structure](#architecture--structure)
|
|
36
|
+
- [Advanced Guides](#advanced-guides)
|
|
37
|
+
- [Protocol Conversion](#protocol-conversion)
|
|
38
|
+
- [Key Rotation & Failure Handling](#key-rotation--failure-handling)
|
|
39
|
+
- [Environment Variables](#environment-variables)
|
|
40
|
+
- [Development & Testing](#development--testing)
|
|
41
|
+
- [Continuous Integration (CI)](#continuous-integration-ci)
|
|
42
|
+
- [Building the Release Archives](#building-the-release-archives)
|
|
43
|
+
- [Disclaimer & Legal Notice](#disclaimer--legal-notice)
|
|
44
|
+
- [References & Acknowledgements](#references--acknowledgements)
|
|
45
|
+
- [License](#license)
|
|
46
|
+
|
|
47
|
+
## What is OpenCode2API-Proxy?
|
|
48
|
+
|
|
49
|
+
[OpenCode](https://opencode.ai/) serves its **Zen** and **Go** model families behind two different wire shapes. The Zen lane expects the OpenCode 1.x request format, the Go lane the OpenCode 2.x CLI format, and neither is directly consumable by the OpenAI-compatible clients developers actually use.
|
|
50
|
+
|
|
51
|
+
**OpenCode2API-Proxy** bridges that gap:
|
|
52
|
+
|
|
53
|
+
1. It discovers the models OpenCode publishes, per tier, and learns each model's **native protocol** from OpenCode's own capability catalog.
|
|
54
|
+
2. It serves a single endpoint that speaks **OpenAI Chat Completions**, **OpenAI Responses** and **Anthropic Messages** at the same time, converting a request from the client's protocol to the upstream's and converting the answer back.
|
|
55
|
+
3. It rotates across several upstream keys, cools down the ones that fail, and falls back from the anonymous lane to the authenticated tiers.
|
|
56
|
+
4. It ships a **control WebUI** with a greyscale dark theme on every deployment method, so the same dashboard works locally, from an archive and on Cloudflare Workers.
|
|
57
|
+
|
|
58
|
+
> **Note**: The Go lane requires an active OpenCode Go subscription and a workspace in the Global region. The free tier is served from the official OpenCode client only, so it needs that client's TLS fingerprint. See [Disclaimer & Legal Notice](#disclaimer--legal-notice).
|
|
59
|
+
|
|
60
|
+
> **Reference**: Inspired by and referencing the original [jasonxu114514/opencode2api](https://github.com/jasonxu114514/opencode2api) repository.
|
|
61
|
+
|
|
62
|
+
## Key Features
|
|
63
|
+
|
|
64
|
+
- **Three Protocols, One Endpoint**: `/v1/chat/completions`, `/v1/responses` and `/v1/messages` all accept the same request body and answer in the protocol the client asked for.
|
|
65
|
+
- **Four Deployment Methods**:
|
|
66
|
+
- **Local Daemon**: Runs on `127.0.0.1:8080` with a control WebUI on `127.0.0.1:8081`.
|
|
67
|
+
- **Built Release**: Cross-platform ZIP archives for Linux, Windows and macOS on x64 and arm64, each with an example configuration.
|
|
68
|
+
- **Cloudflare Worker**: A standalone 82 KB bundle with no Node built-ins.
|
|
69
|
+
- **npm**: `npm install -g opencode2api-proxy`.
|
|
70
|
+
- **Model Catalog Discovery**: The Zen and Go listings refresh on a timer, and OpenCode's capability catalog teaches the router which protocol each model actually speaks.
|
|
71
|
+
- **Protocol-Native Routing**: A request is sent in the upstream's own protocol, so no conversion happens on the hot path unless the client asked for a different one.
|
|
72
|
+
- **Key Rotation & Cooldown**: A 401, 403 or 429 rotates to the next key of the tier; the failing key is cooled down with an exponential backoff, and a non-retryable 4xx leaves the tier immediately.
|
|
73
|
+
- **Full SSE Streaming**: Reasoning deltas, tool call arguments and the usage report survive the trip, and a non-streaming client gets the same answer collapsed back into one document.
|
|
74
|
+
- **Control WebUI**: A greyscale dark dashboard with the overview, the model routing table, the API keys, the quota, a Playground, the rotation groups, the configuration, the account and a live log stream.
|
|
75
|
+
- **Safe & Resource-Bounded**:
|
|
76
|
+
- 32 MB request body limit and 64 MB response body limit.
|
|
77
|
+
- Client disconnect cancellation through `AbortSignal`.
|
|
78
|
+
- An upstream timeout that ends a stalled request instead of hanging it.
|
|
79
|
+
- Secrets masked out of every log line and every WebUI view.
|
|
80
|
+
|
|
81
|
+
## Quick Start
|
|
82
|
+
|
|
83
|
+
### Method 1: Local Daemon
|
|
84
|
+
|
|
85
|
+
#### Step 1: Get an API Key
|
|
86
|
+
|
|
87
|
+
Create a key in the [OpenCode Console](https://opencode.ai/) and copy it. You need a key for the **Zen** tier, the **Go** tier, or both.
|
|
88
|
+
|
|
89
|
+
#### Step 2: Install
|
|
90
|
+
|
|
91
|
+
Install globally via npm:
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
npm install -g opencode2api-proxy
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Or run directly with npx without installation:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
npx opencode2api-proxy start
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Or when running from a local clone:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
npm install
|
|
107
|
+
npm run build
|
|
108
|
+
node dist/cli.js start
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
#### Step 3: Configure
|
|
112
|
+
|
|
113
|
+
Copy the example configuration and edit it:
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
cp config.example.json config.json
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
```json
|
|
120
|
+
{
|
|
121
|
+
"listen": "127.0.0.1:8080",
|
|
122
|
+
"server_keys": ["sk-your-local-key"],
|
|
123
|
+
"zen_keys": ["sk-your-zen-key"],
|
|
124
|
+
"go_keys": ["sk-your-go-key"],
|
|
125
|
+
"webui": {
|
|
126
|
+
"enabled": true,
|
|
127
|
+
"listen": "127.0.0.1:8081",
|
|
128
|
+
"username": "admin",
|
|
129
|
+
"password": "change-this-admin-password"
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
`server_keys` are the keys your clients authenticate with. `zen_keys` and `go_keys` are the keys OpenCode issued you.
|
|
135
|
+
|
|
136
|
+
#### Step 4: Start the Router
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
opencode2api-proxy start
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Or specify a custom port and key:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
opencode2api-proxy start --port 8080 --key sk-my-secret-key
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Your local endpoint is available at `http://127.0.0.1:8080/v1`, and the control WebUI at `http://127.0.0.1:8081`.
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
curl http://127.0.0.1:8080/v1/chat/completions \
|
|
152
|
+
-H "Authorization: Bearer sk-your-local-key" \
|
|
153
|
+
-H "Content-Type: application/json" \
|
|
154
|
+
-d '{"model":"deepseek-v4.1-flash","messages":[{"role":"user","content":"Hello"}]}'
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Method 2: Built Release
|
|
158
|
+
|
|
159
|
+
Prebuilt archives for every supported platform are attached to each release.
|
|
160
|
+
|
|
161
|
+
#### Step 1: Download
|
|
162
|
+
|
|
163
|
+
Grab the archive that matches your platform from the [releases page](https://github.com/IMROVOID/OpenCode2API-Proxy/releases):
|
|
164
|
+
|
|
165
|
+
| Target | File |
|
|
166
|
+
|---|---|
|
|
167
|
+
| Linux x64 | `opencode2api-proxy-v1.0.0-linux-x64.zip` |
|
|
168
|
+
| Linux arm64 | `opencode2api-proxy-v1.0.0-linux-arm64.zip` |
|
|
169
|
+
| Windows x64 | `opencode2api-proxy-v1.0.0-win-x64.zip` |
|
|
170
|
+
| Windows arm64 | `opencode2api-proxy-v1.0.0-win-arm64.zip` |
|
|
171
|
+
| macOS (Intel) | `opencode2api-proxy-v1.0.0-macos-x64.zip` |
|
|
172
|
+
| macOS (Apple Silicon) | `opencode2api-proxy-v1.0.0-macos-arm64.zip` |
|
|
173
|
+
|
|
174
|
+
#### Step 2: Extract and configure
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
unzip opencode2api-proxy-v1.0.0-linux-x64.zip -d opencode2api-proxy
|
|
178
|
+
cd opencode2api-proxy
|
|
179
|
+
mv config.example.json config.json
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Each archive ships a **separate example configuration** named `config.example.json`, so the proxy never overwrites a configuration you already wrote. Rename it to `config.json` and fill in your keys.
|
|
183
|
+
|
|
184
|
+
#### Step 3: Run
|
|
185
|
+
|
|
186
|
+
```bash
|
|
187
|
+
node opencode2api-proxy.js
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
On Windows:
|
|
191
|
+
|
|
192
|
+
```powershell
|
|
193
|
+
node opencode2api-proxy.js
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
The archive runs on any system with **Node.js 22 or newer**. Install it from [nodejs.org](https://nodejs.org/) if you do not have it.
|
|
197
|
+
|
|
198
|
+
### Method 3: Cloudflare Workers (Serverless)
|
|
199
|
+
|
|
200
|
+
Deploy a 24/7 serverless gateway without keeping your computer running.
|
|
201
|
+
|
|
202
|
+
#### Step 1: Copy the Code
|
|
203
|
+
|
|
204
|
+
Open [`worker/worker.js`](./worker/worker.js) and copy the entire file contents.
|
|
205
|
+
|
|
206
|
+
#### Step 2: Create Worker in Cloudflare Dashboard
|
|
207
|
+
|
|
208
|
+
1. Log into the [Cloudflare Dashboard](https://dash.cloudflare.com).
|
|
209
|
+
2. Go to **Workers & Pages** -> **Create application** -> **Create Worker**.
|
|
210
|
+
3. Set name to `opencode2api-proxy` and click **Deploy**.
|
|
211
|
+
4. Click **Edit code**, select all existing code, delete it, and paste the copied contents of `worker/worker.js`.
|
|
212
|
+
5. Click **Deploy** in the top right.
|
|
213
|
+
|
|
214
|
+
#### Step 3: Add Variables & Secrets
|
|
215
|
+
|
|
216
|
+
In your Worker, go to **Settings** -> **Variables and Secrets** and add:
|
|
217
|
+
|
|
218
|
+
| Variable | Type | Required | Description |
|
|
219
|
+
|---|---|---|---|
|
|
220
|
+
| `OPENCODE_ZEN_KEYS` | Secret | Optional | Your Zen key. Several keys are separated by commas. |
|
|
221
|
+
| `OPENCODE_GO_KEYS` | Secret | Optional | Your Go key. Several keys are separated by commas. |
|
|
222
|
+
| `API_KEY` | Secret | Optional | Client bearer key (e.g. `sk-local-key`). |
|
|
223
|
+
| `UPSTREAM_ZEN` | Text | Optional | Zen base URL (defaults to `https://opencode.ai/zen`). |
|
|
224
|
+
| `UPSTREAM_GO` | Text | Optional | Go base URL (defaults to `https://opencode.ai/zen/go`). |
|
|
225
|
+
| `WEBUI_ENABLED` | Text | Optional | Set to `false` to hide the control WebUI. |
|
|
226
|
+
|
|
227
|
+
Click **Save and deploy**.
|
|
228
|
+
|
|
229
|
+
Your Cloudflare Worker API URL:
|
|
230
|
+
|
|
231
|
+
```
|
|
232
|
+
https://opencode2api-proxy.<your-subdomain>.workers.dev/v1
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Method 4: npm
|
|
236
|
+
|
|
237
|
+
```bash
|
|
238
|
+
npm install -g opencode2api-proxy
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
opencode2api-proxy start --config ./config.json
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
The package ships the CLI bundle, the Cloudflare Worker bundle and the example configuration. `npm run build:release` produces the same cross-platform archives the release workflow uploads.
|
|
246
|
+
|
|
247
|
+
## Model Catalog & Aliases
|
|
248
|
+
|
|
249
|
+
The router discovers the models OpenCode publishes on each tier and learns each model's native protocol from OpenCode's capability catalog. You always request a **model id**, and the router picks the lane and the protocol.
|
|
250
|
+
|
|
251
|
+
| Requested model | Lane | Notes |
|
|
252
|
+
|---|---|---|
|
|
253
|
+
| `deepseek-v4.1-flash` | Zen or Go | Served by the tier that advertises it. |
|
|
254
|
+
| `glm-5.3` | Zen or Go | Part of the SOTA rotation group. |
|
|
255
|
+
| `grok-4.6` | Zen or Go | Part of the SOTA rotation group. |
|
|
256
|
+
| `kimi-k3` | Zen or Go | Part of the SOTA rotation group. |
|
|
257
|
+
| `qwen3.8-max` | Zen or Go | Part of the SOTA rotation group. |
|
|
258
|
+
| `claude-haiku-5-5` | Zen or Go | Native protocol is Anthropic Messages. |
|
|
259
|
+
| `*free` | Anonymous | Served on the anonymous lane when it is enabled. |
|
|
260
|
+
|
|
261
|
+
The **SOTA rotation group** holds exactly `deepseek-v4.1-flash`, `glm-5.3`, `grok-4.6`, `kimi-k3` and `qwen3.8-max`. The **sweet** group holds the models you add yourself. A rotation alias rotates across the models of its group, so a request for `sota` picks the group member that is currently healthy.
|
|
262
|
+
|
|
263
|
+
`GET /v1/models` lists everything the router can serve, with each model's native protocol, its context window and its supported endpoints:
|
|
264
|
+
|
|
265
|
+
```bash
|
|
266
|
+
curl http://127.0.0.1:8080/v1/models -H "Authorization: Bearer sk-your-local-key"
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## Client Integration
|
|
270
|
+
|
|
271
|
+
<details>
|
|
272
|
+
<summary><b>Claude Code</b></summary>
|
|
273
|
+
|
|
274
|
+
```bash
|
|
275
|
+
export ANTHROPIC_BASE_URL="http://127.0.0.1:8080"
|
|
276
|
+
export ANTHROPIC_AUTH_TOKEN="sk-your-local-key"
|
|
277
|
+
export ANTHROPIC_MODEL="deepseek-v4.1-flash"
|
|
278
|
+
claude
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
</details>
|
|
282
|
+
|
|
283
|
+
<details>
|
|
284
|
+
<summary><b>Codex CLI</b></summary>
|
|
285
|
+
|
|
286
|
+
```toml
|
|
287
|
+
# ~/.codex/config.toml
|
|
288
|
+
model = "deepseek-v4.1-flash"
|
|
289
|
+
model_provider = "opencode2api"
|
|
290
|
+
|
|
291
|
+
[model_providers.opencode2api]
|
|
292
|
+
name = "OpenCode2API"
|
|
293
|
+
base_url = "http://127.0.0.1:8080/v1"
|
|
294
|
+
env_key = "OPENCODE2API_KEY"
|
|
295
|
+
wire_api = "responses"
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
</details>
|
|
299
|
+
|
|
300
|
+
<details>
|
|
301
|
+
<summary><b>OpenCode</b></summary>
|
|
302
|
+
|
|
303
|
+
```json
|
|
304
|
+
{
|
|
305
|
+
"$schema": "https://opencode.ai/config.json",
|
|
306
|
+
"provider": {
|
|
307
|
+
"opencode2api": {
|
|
308
|
+
"npm": "@ai-sdk/openai-compatible",
|
|
309
|
+
"options": {
|
|
310
|
+
"baseURL": "http://127.0.0.1:8080/v1",
|
|
311
|
+
"apiKey": "sk-your-local-key"
|
|
312
|
+
},
|
|
313
|
+
"models": {
|
|
314
|
+
"deepseek-v4.1-flash": { "name": "DeepSeek v4.1 Flash" }
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
},
|
|
318
|
+
"model": "opencode2api/deepseek-v4.1-flash"
|
|
319
|
+
}
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
</details>
|
|
323
|
+
|
|
324
|
+
<details>
|
|
325
|
+
<summary><b>Cline / Roo Code / Continue</b></summary>
|
|
326
|
+
|
|
327
|
+
```json
|
|
328
|
+
{
|
|
329
|
+
"apiProvider": "openai",
|
|
330
|
+
"openaiApiBase": "http://127.0.0.1:8080/v1",
|
|
331
|
+
"openaiApiKey": "sk-your-local-key",
|
|
332
|
+
"openaiModelId": "deepseek-v4.1-flash"
|
|
333
|
+
}
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
</details>
|
|
337
|
+
|
|
338
|
+
<details>
|
|
339
|
+
<summary><b>Cherry Studio / LobeChat / NextChat</b></summary>
|
|
340
|
+
|
|
341
|
+
Add a custom OpenAI provider:
|
|
342
|
+
|
|
343
|
+
| Field | Value |
|
|
344
|
+
|---|---|
|
|
345
|
+
| API address | `http://127.0.0.1:8080/v1` |
|
|
346
|
+
| API key | `sk-your-local-key` |
|
|
347
|
+
| Model | `deepseek-v4.1-flash` |
|
|
348
|
+
|
|
349
|
+
</details>
|
|
350
|
+
|
|
351
|
+
<details>
|
|
352
|
+
<summary><b>Any OpenAI-compatible client</b></summary>
|
|
353
|
+
|
|
354
|
+
Point the client at `http://127.0.0.1:8080/v1` and use `sk-your-local-key` as the API key. Every OpenAI-compatible client works, because the router speaks the client's protocol.
|
|
355
|
+
|
|
356
|
+
</details>
|
|
357
|
+
|
|
358
|
+
## Architecture & Structure
|
|
359
|
+
|
|
360
|
+
```
|
|
361
|
+
OpenCode2API-Proxy/
|
|
362
|
+
├── src/
|
|
363
|
+
│ ├── types/
|
|
364
|
+
│ │ ├── config.ts # Configuration, tier, protocol and log-level contracts
|
|
365
|
+
│ │ └── inference.ts # The protocol-neutral bridge model
|
|
366
|
+
│ ├── config/
|
|
367
|
+
│ │ ├── defaults.ts # Default configuration and the SOTA model list
|
|
368
|
+
│ │ ├── schema.ts # The strict configuration schema
|
|
369
|
+
│ │ ├── normalize.ts # The single validation point
|
|
370
|
+
│ │ ├── secrets.ts # Masking, fingerprinting and redaction
|
|
371
|
+
│ │ └── store.ts # Atomic configuration persistence
|
|
372
|
+
│ ├── protocol/
|
|
373
|
+
│ │ ├── identity.ts # Session, message and project id derivation
|
|
374
|
+
│ │ ├── sha256.ts # Portable SHA-256 for the Worker bundle
|
|
375
|
+
│ │ ├── decode-request.ts # Client body -> bridge model
|
|
376
|
+
│ │ ├── encode-request.ts # Bridge model -> upstream body
|
|
377
|
+
│ │ ├── response.ts # Upstream body -> client body
|
|
378
|
+
│ │ ├── stream-parser.ts # Upstream SSE -> bridge events
|
|
379
|
+
│ │ ├── stream-encoder.ts # Bridge events -> client SSE
|
|
380
|
+
│ │ ├── collapse.ts # SSE stream -> one response document
|
|
381
|
+
│ │ ├── rotation-stream.ts # Rotation with a commit point
|
|
382
|
+
│ │ ├── tools.ts # The v1 read gate and the v2 core tool gate
|
|
383
|
+
│ │ └── lanes.ts # The v1 and v2 upstream lanes
|
|
384
|
+
│ ├── proxy/
|
|
385
|
+
│ │ ├── model-catalog.ts # The discovered models and the routing rules
|
|
386
|
+
│ │ ├── model-discovery.ts # The listings and the capability catalog
|
|
387
|
+
│ │ ├── catalog-refresh.ts # The refresh timer
|
|
388
|
+
│ │ ├── attempt-runner.ts # The key rotation and the cooldown
|
|
389
|
+
│ │ ├── request-shaping.ts # The per-lane request shaping
|
|
390
|
+
│ │ └── delivery.ts # The response relay and the conversion
|
|
391
|
+
│ ├── auth/
|
|
392
|
+
│ │ └── key-pool.ts # The key pool with its cooldown
|
|
393
|
+
│ ├── server/
|
|
394
|
+
│ │ ├── dispatcher.ts # Route matching, CORS and client authentication
|
|
395
|
+
│ │ ├── gateway.ts # The runtime that owns the pools and the catalog
|
|
396
|
+
│ │ ├── completion-handler.ts # The core completion flow
|
|
397
|
+
│ │ └── admin/ # The control WebUI and the admin API
|
|
398
|
+
│ ├── telemetry/
|
|
399
|
+
│ │ ├── metrics.ts # The request counters, the latency and the usage
|
|
400
|
+
│ │ ├── log-broker.ts # The log ring and the live subscribers
|
|
401
|
+
│ │ └── logger.ts # The structured logger
|
|
402
|
+
│ ├── webui/
|
|
403
|
+
│ │ ├── document.ts # The single-file control WebUI
|
|
404
|
+
│ │ ├── theme.ts # The greyscale dark tokens
|
|
405
|
+
│ │ └── styles.ts # The stylesheet
|
|
406
|
+
│ ├── cli/
|
|
407
|
+
│ │ ├── index.ts # The CLI entrypoint
|
|
408
|
+
│ │ └── commands/start.ts # The daemon command
|
|
409
|
+
│ └── worker/
|
|
410
|
+
│ └── index.ts # The Cloudflare Workers fetch entrypoint
|
|
411
|
+
├── worker/
|
|
412
|
+
│ └── worker.js # Standalone Cloudflare Worker bundle (82 KB)
|
|
413
|
+
├── dist/
|
|
414
|
+
│ └── cli.js # Standalone Node.js CLI executable bundle (301 KB)
|
|
415
|
+
├── tests/ # 131 automated unit & integration tests
|
|
416
|
+
├── scripts/
|
|
417
|
+
│ ├── build.ts # The esbuild build pipeline
|
|
418
|
+
│ ├── build-release.ts # The cross-platform archive builder
|
|
419
|
+
│ └── zip.ts # The dependency-free ZIP writer
|
|
420
|
+
├── .github/
|
|
421
|
+
│ └── workflows/
|
|
422
|
+
│ ├── test.yml # The CI workflow with skip flags
|
|
423
|
+
│ └── release.yml # The manual multi-OS draft release workflow
|
|
424
|
+
├── config.example.json
|
|
425
|
+
└── package.json
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
## Advanced Guides
|
|
429
|
+
|
|
430
|
+
### Protocol Conversion
|
|
431
|
+
|
|
432
|
+
The router sends a request in the **upstream's own protocol** and only converts when the client asked for a different one.
|
|
433
|
+
|
|
434
|
+
| Client endpoint | Upstream lane | What happens |
|
|
435
|
+
|---|---|---|
|
|
436
|
+
| `/v1/chat/completions` | v1 (Zen) | The body is sent as Chat Completions, with a `read` tool guaranteed. |
|
|
437
|
+
| `/v1/chat/completions` | v2 (Go) | The body is sent as Chat Completions, with the five core tools guaranteed. |
|
|
438
|
+
| `/v1/responses` | any | The body is sent as Responses, and the answer is converted back. |
|
|
439
|
+
| `/v1/messages` | any | The body is sent as Anthropic Messages, and the answer is converted back. |
|
|
440
|
+
|
|
441
|
+
A request to a different protocol than the upstream's is decoded into the bridge model, re-encoded for the upstream, and the answer is converted back on the way out. The same protocol on both sides is passed through untouched, so the common case costs nothing.
|
|
442
|
+
|
|
443
|
+
**The v1 read gate.** The v1 free tier requires a tool named `read`. A client tool that reads files (`read_file`, `ReadFile`, `read-text-file` and similar) is renamed to `read` on the wire, and the original name is restored in the answer.
|
|
444
|
+
|
|
445
|
+
**The v2 core tool gate.** The v2 free tier requires the five core tools `shell`, `edit`, `glob`, `grep` and `read`, and compares their description and parameter schema against the official CLI. The genuine definitions ship with the router, so a client that declares its own tools keeps them and the missing core tools are added.
|
|
446
|
+
|
|
447
|
+
### Key Rotation & Failure Handling
|
|
448
|
+
|
|
449
|
+
Put several keys in `zen_keys` or `go_keys` and the router distributes the requests across them.
|
|
450
|
+
|
|
451
|
+
1. **Round-robin**: Each request starts on a different key.
|
|
452
|
+
2. **Failure detection**: A 401, 403 or 429 rotates to the next key of the tier.
|
|
453
|
+
3. **Cooldown**: The failing key is held back with an exponential backoff, up to eight times the base cooldown. A longer `Retry-After` is honored in full.
|
|
454
|
+
4. **Immediate exit**: Any other 4xx is deterministic, so the router leaves the tier at once instead of burning the other keys.
|
|
455
|
+
5. **Tier fallback**: When the first tier is exhausted, the next tier in the prefer order takes over.
|
|
456
|
+
6. **Anonymous first**: A free model starts on the anonymous lane when `anonymous` is enabled, and falls back to the authenticated tiers when the anonymous lane rejects it.
|
|
457
|
+
|
|
458
|
+
The rotation groups add one more layer: an alias like `sota` rotates across the models of its group, so a request keeps working when one model is rate-limited.
|
|
459
|
+
|
|
460
|
+
### Environment Variables
|
|
461
|
+
|
|
462
|
+
| Variable | Mode | Default | Description |
|
|
463
|
+
|---|---|---|---|
|
|
464
|
+
| `OPENCODE2API_CONFIG` | Local | `./config.json` | Path to the configuration file |
|
|
465
|
+
| `API_KEY` | Worker | *(None)* | Client authorization bearer token |
|
|
466
|
+
| `OPENCODE_ZEN_KEYS` | Worker | *(None)* | Comma-separated Zen keys |
|
|
467
|
+
| `OPENCODE_GO_KEYS` | Worker | *(None)* | Comma-separated Go keys |
|
|
468
|
+
| `UPSTREAM_ZEN` | Worker | `https://opencode.ai/zen` | Zen base URL |
|
|
469
|
+
| `UPSTREAM_GO` | Worker | `https://opencode.ai/zen/go` | Go base URL |
|
|
470
|
+
| `UPSTREAM_ANONYMOUS` | Worker | `https://opencode.ai/inference` | Anonymous base URL |
|
|
471
|
+
| `WEBUI_ENABLED` | Worker | `true` | Set to `false` to hide the control WebUI |
|
|
472
|
+
| `ADMIN_PASSWORD` | Worker | *(None)* | Control WebUI password |
|
|
473
|
+
|
|
474
|
+
## Development & Testing
|
|
475
|
+
|
|
476
|
+
Run typecheck:
|
|
477
|
+
|
|
478
|
+
```bash
|
|
479
|
+
npm run typecheck
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
Run the automated test suite:
|
|
483
|
+
|
|
484
|
+
```bash
|
|
485
|
+
npm test
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
Build the standalone bundles:
|
|
489
|
+
|
|
490
|
+
```bash
|
|
491
|
+
npm run build
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
### Continuous Integration (CI)
|
|
495
|
+
|
|
496
|
+
Every commit and pull request runs automated verification through the [Test & Verify](.github/workflows/test.yml) GitHub Actions workflow (Node.js 22):
|
|
497
|
+
|
|
498
|
+
- **Typecheck**: `npm run typecheck`
|
|
499
|
+
- **Unit & Integration Tests**: `npm test`
|
|
500
|
+
- **Bundle Verification**: `npm run build`, plus a check that the Worker bundle carries no Node built-in.
|
|
501
|
+
|
|
502
|
+
#### Skipping Automated Tests
|
|
503
|
+
|
|
504
|
+
To skip the test suite on specific commits (e.g. for docs updates, asset additions, or fast iterations while still verifying types and builds), include any of the following tags in the commit message:
|
|
505
|
+
|
|
506
|
+
- `[skip test]` or `[skip tests]`
|
|
507
|
+
- `[skip-test]` or `[skip-tests]`
|
|
508
|
+
|
|
509
|
+
When triggered manually via **Actions** -> **Run workflow**, check the **Skip running automated tests** input option.
|
|
510
|
+
|
|
511
|
+
### Building the Release Archives
|
|
512
|
+
|
|
513
|
+
The [Build release archives](.github/workflows/release.yml) workflow is manual only. Trigger it from **Actions** -> **Build release archives** -> **Run workflow** and supply a semantic version tag.
|
|
514
|
+
|
|
515
|
+
It builds Linux, Windows and macOS archives for x64 and arm64, writes a **Draft** release with the archives attached, and each archive holds the executable, a separate `config.example.json`, the README and the license.
|
|
516
|
+
|
|
517
|
+
To build the same archives locally:
|
|
518
|
+
|
|
519
|
+
```bash
|
|
520
|
+
npm run build
|
|
521
|
+
npm run build:release
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
The archives land in `release/`.
|
|
525
|
+
|
|
526
|
+
## Disclaimer & Legal Notice
|
|
527
|
+
|
|
528
|
+
> **IMPORTANT**: Please read this notice carefully before using or deploying OpenCode2API-Proxy.
|
|
529
|
+
|
|
530
|
+
1. **Educational & Research Purposes Only**: This project is developed and distributed exclusively for personal educational, research, and non-commercial API interoperability testing purposes.
|
|
531
|
+
2. **Upstream Terms of Service**: Routing requests to OpenCode's Zen, Go or free tier through a third-party client may violate OpenCode's Terms of Service. The free tier explicitly restricts access to the official OpenCode client, and the Go tier requires a paid subscription. OpenCode may rate-limit, revoke keys, or suspend accounts that use the upstream outside its own client.
|
|
532
|
+
3. **No Warranty & No Guarantee**: The author and contributors make no claims, promises, or guarantees regarding the safety, status, or longevity of your OpenCode account or subscription. This software is provided "AS IS", without warranty of any kind, express or implied.
|
|
533
|
+
4. **Assumption of Risk**: You assume full and sole responsibility for any outcomes or damages resulting from using this software, including but not limited to account termination, loss of API access, or data loss. Use strictly at your own risk.
|
|
534
|
+
5. **Trademark Attribution**: All product names, logos, and brands (such as "OpenCode", "OpenAI", "Anthropic") are trademarks or registered trademarks of their respective owners. OpenCode2API-Proxy is an independent open-source project and is neither affiliated with, maintained by, nor endorsed by any of them.
|
|
535
|
+
|
|
536
|
+
## References & Acknowledgements
|
|
537
|
+
|
|
538
|
+
- [jasonxu114514/opencode2api](https://github.com/jasonxu114514/opencode2api) — Original reference repository for OpenCode API proxying and reverse engineering.
|
|
539
|
+
|
|
540
|
+
## License
|
|
541
|
+
|
|
542
|
+
This project is licensed under the **GNU General Public License v3.0 (GPLv3)**. See the [LICENSE](./LICENSE) file for details.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"listen": "127.0.0.1:8080",
|
|
3
|
+
"server_keys": ["change-this-local-key"],
|
|
4
|
+
"zen_keys": ["sk-your-zen-key"],
|
|
5
|
+
"go_keys": [],
|
|
6
|
+
"anonymous": false,
|
|
7
|
+
"proxies": ["direct"],
|
|
8
|
+
"proxyfile": "",
|
|
9
|
+
"upstream": {
|
|
10
|
+
"zen": "https://opencode.ai/zen",
|
|
11
|
+
"go": "https://opencode.ai/zen/go",
|
|
12
|
+
"anonymous": "https://opencode.ai/inference"
|
|
13
|
+
},
|
|
14
|
+
"retry": {
|
|
15
|
+
"max_attempts": 3,
|
|
16
|
+
"timeout_seconds": 300
|
|
17
|
+
},
|
|
18
|
+
"models": {
|
|
19
|
+
"refresh_seconds": 300,
|
|
20
|
+
"protocols": {},
|
|
21
|
+
"free_only": false
|
|
22
|
+
},
|
|
23
|
+
"performance": {
|
|
24
|
+
"max_idle_conns": 2048,
|
|
25
|
+
"max_idle_conns_per_host": 256,
|
|
26
|
+
"max_conns_per_host": 0,
|
|
27
|
+
"idle_conn_timeout_seconds": 120,
|
|
28
|
+
"connect_timeout_seconds": 5,
|
|
29
|
+
"failure_cooldown_seconds": 15,
|
|
30
|
+
"attempt_timeout_seconds": 0,
|
|
31
|
+
"first_event_timeout_seconds": 0
|
|
32
|
+
},
|
|
33
|
+
"logging": {
|
|
34
|
+
"level": "info",
|
|
35
|
+
"ring_size": 2000,
|
|
36
|
+
"dump_request_bodies": false
|
|
37
|
+
},
|
|
38
|
+
"webui": {
|
|
39
|
+
"enabled": true,
|
|
40
|
+
"listen": "0.0.0.0:8081",
|
|
41
|
+
"username": "admin",
|
|
42
|
+
"password": "change-this-admin-password",
|
|
43
|
+
"session_ttl_minutes": 720
|
|
44
|
+
},
|
|
45
|
+
"prefer": "go",
|
|
46
|
+
"reasoning": {
|
|
47
|
+
"effort": "high",
|
|
48
|
+
"effort_by_model": {}
|
|
49
|
+
},
|
|
50
|
+
"rotation": {
|
|
51
|
+
"sota": {
|
|
52
|
+
"alias": "sota",
|
|
53
|
+
"order": ["deepseek-v4.1-flash", "glm-5.3", "grok-4.6", "kimi-k3", "qwen3.8-max"]
|
|
54
|
+
},
|
|
55
|
+
"sweet": {
|
|
56
|
+
"alias": "sweet",
|
|
57
|
+
"order": []
|
|
58
|
+
},
|
|
59
|
+
"first_output_seconds": 30
|
|
60
|
+
}
|
|
61
|
+
}
|