betahi-copilot-bridge 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 betaHi
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.
package/README.md ADDED
@@ -0,0 +1,213 @@
1
+ # copilot-bridge
2
+
3
+ Run GitHub Copilot as a local OpenAI Responses– and Anthropic-compatible
4
+ endpoint, so [Codex CLI](https://github.com/openai/codex) and
5
+ [Claude Code](https://docs.anthropic.com/en/docs/claude-code/overview) can
6
+ talk to Copilot transparently.
7
+
8
+ **Supports:** Codex CLI and Claude Code out of the box.
9
+
10
+ > [!WARNING]
11
+ > This is a reverse-engineered bridge for the GitHub Copilot API. It is not
12
+ > supported by GitHub, and may break unexpectedly. Use at your own risk.
13
+
14
+ > [!WARNING]
15
+ > **GitHub Security Notice:**
16
+ > Excessive automated or scripted use of Copilot (including rapid or bulk
17
+ > requests, such as via automated tools) may trigger GitHub's abuse-detection
18
+ > systems. You may receive a warning from GitHub Security, and further
19
+ > anomalous activity could result in temporary suspension of your Copilot
20
+ > access.
21
+ >
22
+ > GitHub prohibits use of their servers for excessive automated bulk activity
23
+ > or any activity that places undue burden on their infrastructure.
24
+ >
25
+ > Please review:
26
+ >
27
+ > - [GitHub Acceptable Use Policies](https://docs.github.com/site-policy/acceptable-use-policies/github-acceptable-use-policies#4-spam-and-inauthentic-activity-on-github)
28
+ > - [GitHub Copilot Terms](https://docs.github.com/site-policy/github-terms/github-terms-for-additional-products-and-features#github-copilot)
29
+ >
30
+ > Use this bridge responsibly to avoid account restrictions. The
31
+ > `--rate-limit <seconds>` flag is provided to help throttle upstream traffic.
32
+
33
+ ## Demo
34
+
35
+ ### Codex CLI
36
+
37
+ ![Codex demo](assets/screenshots/codex_demo.png)
38
+
39
+ ### Claude Code
40
+
41
+ ![Claude demo](assets/screenshots/claude_demo.png)
42
+
43
+ ## Install & run
44
+
45
+ ```sh
46
+ # one-time GitHub device login
47
+ npx betahi-copilot-bridge@latest auth
48
+
49
+ # start the bridge on 127.0.0.1:4142
50
+ npx betahi-copilot-bridge@latest start
51
+ ```
52
+
53
+ `start` flags: `--host`, `--port`, `--show-token`, `--no-codex-setup`,
54
+ `--select-model`, `--no-prompt`, `--rate-limit <seconds>`, `--wait`.
55
+
56
+ After startup the banner prints a **Usage Viewer** link of the form
57
+ `https://betahi.github.io/copilot-api?endpoint=http://127.0.0.1:4142/usage`,
58
+ which renders the Copilot quota snapshot (chat / completions / premium
59
+ interactions) read from `GET /usage`.
60
+
61
+ The bridge exposes both adapter-style endpoints (`/v1/responses`,
62
+ `/v1/messages`) and the raw OpenAI-compatible surface
63
+ (`/v1/chat/completions`, `/v1/embeddings`, `/v1/models`) so tools like
64
+ LiteLLM, Continue, Cline and Aider work out of the box. CORS is enabled
65
+ globally for browser-based clients.
66
+
67
+ `--rate-limit N` enforces a minimum of N seconds between upstream
68
+ requests (anti–abuse-detection throttle). Add `--wait` to block instead
69
+ of returning HTTP 429 when the window has not elapsed.
70
+
71
+ ## Configure Codex CLI
72
+
73
+ `start` writes a managed block into **`~/.codex/config.toml`**. You don't edit
74
+ this block; the bridge regenerates it on every start.
75
+
76
+ ```toml
77
+ # >>> copilot-bridge managed block — auto-generated, do not edit between markers >>>
78
+ model_provider = "bridge"
79
+
80
+ [model_providers.bridge]
81
+ name = "Copilot Bridge"
82
+ base_url = "http://127.0.0.1:4142/v1"
83
+ wire_api = "responses"
84
+ prefer_websockets = false
85
+ requires_openai_auth = false
86
+ # <<< copilot-bridge managed block — edits outside this block are preserved <<<
87
+ ```
88
+
89
+ To pin the **default model** for `codex` (without passing `-m` every time),
90
+ edit the **top of the same file** — outside the markers. The bridge preserves
91
+ these keys across rewrites:
92
+
93
+ ```toml
94
+ model = "gpt-5.3-codex"
95
+ model_reasoning_effort = "high"
96
+ ```
97
+
98
+ That's it — `codex exec '...'` will now route through the bridge to Copilot.
99
+
100
+ Use `--no-codex-setup` to skip the managed-block writer entirely (e.g. if you
101
+ manage `~/.codex/config.toml` yourself).
102
+
103
+ ### Codex warning: "Model metadata ... not found"
104
+
105
+ This is a Codex client-side metadata warning, not a bridge routing failure.
106
+ Requests can still complete through the bridge.
107
+
108
+ For `claude-opus-4.6-1m`, upstream still enforces a 1,000,000-token prompt
109
+ limit (about 900k succeeds; around 1,000,046 is rejected as too long).
110
+
111
+ ## Configure Claude Code
112
+
113
+ Bridge endpoints: `POST /v1/messages`, `POST /v1/messages/count_tokens`.
114
+
115
+ Point Claude Code at the bridge through **`~/.claude/settings.json`**:
116
+
117
+ ```json
118
+ {
119
+ "env": {
120
+ "ANTHROPIC_BASE_URL": "http://127.0.0.1:4142",
121
+ "ANTHROPIC_AUTH_TOKEN": "dummy",
122
+ "ANTHROPIC_MODEL": "claude-opus-4.7",
123
+ "ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-sonnet-4.6",
124
+ "ANTHROPIC_SMALL_FAST_MODEL": "claude-haiku-4.5",
125
+ "COPILOT_REASONING_EFFORT": "medium"
126
+ },
127
+ "model": "claude-opus-4.7"
128
+ }
129
+ ```
130
+
131
+ `COPILOT_REASONING_EFFORT` is also read from the project-local
132
+ `.claude/settings.json` and `.claude/settings.local.json` and applied to the
133
+ upstream call when the model supports reasoning.
134
+
135
+ ## Environment overrides
136
+
137
+ | Variable | Purpose |
138
+ | -------------------------- | ---------------------------------------------------- |
139
+ | `COPILOT_TOKEN` | Pre-issued Copilot bearer token (skip device login). |
140
+ | `COPILOT_ACCOUNT_TYPE` | `individual` \| `business` \| `enterprise`. |
141
+ | `COPILOT_BASE_URL` | Override the upstream Copilot base URL. |
142
+ | `COPILOT_VSCODE_VERSION` | Override the VS Code version sent upstream. |
143
+ | `COPILOT_REASONING_EFFORT` | Default reasoning effort when the model supports it. |
144
+
145
+ ## Supported models
146
+
147
+ The bridge resolves aliases and clamps reasoning effort to what each model
148
+ accepts upstream.
149
+
150
+ ### GPT-5 family — native Responses passthrough
151
+
152
+ | Model | Reasoning efforts |
153
+ | --------------- | ---------------------------------------- |
154
+ | `gpt-5.5` | `none`, `low`, `medium`, `high`, `xhigh` |
155
+ | `gpt-5.4` | `low`, `medium`, `high`, `xhigh` |
156
+ | `gpt-5.4-mini` | `none`, `low`, `medium` |
157
+ | `gpt-5.3-codex` | `low`, `medium`, `high`, `xhigh` |
158
+ | `gpt-5.2` | `low`, `medium`, `high`, `xhigh` |
159
+ | `gpt-5.2-codex` | `low`, `medium`, `high`, `xhigh` |
160
+ | `gpt-5-mini` | `low`, `medium`, `high` |
161
+
162
+ ### Claude family — translated to chat completions
163
+
164
+ | Model | Reasoning efforts | Notes |
165
+ | -------------------- | --------------------------------------- | -------------------------------------- |
166
+ | `claude-opus-4.7` | `low`, `medium`, `high`, `xhigh`, `max` | Effort sent as `output_config.effort`. |
167
+ | `claude-opus-4.6` | `low`, `medium`, `high` | |
168
+ | `claude-opus-4.6-1m` | `low`, `medium`, `high` | 1M-token context window. |
169
+ | `claude-sonnet-4.6` | `low`, `medium`, `high` | |
170
+ | `claude-opus-4.5` | — | Reasoning not accepted upstream. |
171
+ | `claude-sonnet-4.5` | — | Reasoning not accepted upstream. |
172
+ | `claude-sonnet-4` | — | Reasoning not accepted upstream. |
173
+ | `claude-haiku-4.5` | — | Reasoning not accepted upstream. |
174
+
175
+ ### Gemini family — translated to chat completions
176
+
177
+ | Model | Aliases |
178
+ | ------------------------ | ---------------- |
179
+ | `gemini-3.1-pro-preview` | `gemini-3.1-pro` |
180
+ | `gemini-3-flash-preview` | `gemini-3-flash` |
181
+ | `gemini-2.5-pro` | — |
182
+
183
+ ### Legacy
184
+
185
+ `gpt-4.1`, `gpt-4o` — chat-only upstream, no reasoning parameter.
186
+
187
+ ### Reasoning effort
188
+
189
+ If an unsupported value is sent for a reasoning-capable model, the bridge
190
+ falls back to the model's default (`medium` for the GPT-5 / Claude Opus 4.7
191
+ families) instead of forwarding an invalid request upstream. Set the default
192
+ globally via `COPILOT_REASONING_EFFORT` (env, or `env` in
193
+ `~/.claude/settings.json`); per-request `reasoning_effort` (or Anthropic
194
+ `thinking.budget_tokens`) takes precedence.
195
+
196
+ ## Development
197
+
198
+ Requires [Bun](https://bun.sh) ≥ 1.2.
199
+
200
+ ```sh
201
+ bun install
202
+ bun run dev # watch mode against src/main.ts
203
+ bun test # run all tests (bun test runner)
204
+ bun run typecheck # tsc --noEmit
205
+ bun run build # produce dist/main.js with tsdown
206
+
207
+ # run directly from source (no build, no npx); --port specifies the port
208
+ bun run ./src/main.ts start --host 127.0.0.1 --port 4141 --no-prompt
209
+ ```
210
+
211
+ Adding another CLI: drop a new translator under `src/bridges/<client>/`,
212
+ reuse `src/services/copilot/` for upstream calls, register routes in
213
+ `src/server.ts`, and add tests under `tests/`.