@jer-y/copilot-proxy 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) 2025 jer-y
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,359 @@
1
+ # Copilot API Proxy
2
+
3
+ > [!WARNING]
4
+ > This is a reverse-engineered proxy of GitHub Copilot API. It is not supported by GitHub, and may break unexpectedly. Use at your own risk.
5
+
6
+ > [!WARNING]
7
+ > **GitHub Security Notice:**
8
+ > Excessive automated or scripted use of Copilot (including rapid or bulk requests, such as via automated tools) may trigger GitHub's abuse-detection systems.
9
+ > You may receive a warning from GitHub Security, and further anomalous activity could result in temporary suspension of your Copilot access.
10
+ >
11
+ > GitHub prohibits use of their servers for excessive automated bulk activity or any activity that places undue burden on their infrastructure.
12
+ >
13
+ > Please review:
14
+ >
15
+ > - [GitHub Acceptable Use Policies](https://docs.github.com/site-policy/acceptable-use-policies/github-acceptable-use-policies#4-spam-and-inauthentic-activity-on-github)
16
+ > - [GitHub Copilot Terms](https://docs.github.com/site-policy/github-terms/github-terms-for-additional-products-and-features#github-copilot)
17
+ >
18
+ > Use this proxy responsibly to avoid account restrictions.
19
+
20
+ ---
21
+
22
+ **Note:** If you are using [opencode](https://github.com/sst/opencode), you do not need this project. Opencode supports GitHub Copilot provider out of the box.
23
+
24
+ ---
25
+
26
+ ## Project Overview
27
+
28
+ A reverse-engineered proxy for the GitHub Copilot API that exposes it as an OpenAI and Anthropic compatible service. This allows you to use GitHub Copilot with any tool that supports the OpenAI Chat Completions API or the Anthropic Messages API, including to power [Claude Code](https://docs.anthropic.com/en/docs/claude-code/overview).
29
+
30
+ ## Features
31
+
32
+ - **OpenAI & Anthropic Compatibility**: Exposes GitHub Copilot as an OpenAI-compatible (`/v1/chat/completions`, `/v1/models`, `/v1/embeddings`) and Anthropic-compatible (`/v1/messages`) API.
33
+ - **Responses API Support**: Supports the OpenAI Responses API (`/v1/responses`) for thinking-mode models like `gpt-5`, `gpt-5.1-codex`, `gpt-5.2-codex`, `o3-mini`, and `o4-mini`.
34
+ - **Model-Aware Translation**: Automatically applies model-specific optimizations — prompt caching (`copilot_cache_control`) for Claude models, `reasoning_effort` mapping from Anthropic's `thinking.budget_tokens`, and intelligent model name normalization (e.g., `claude-sonnet-4-5-20250929` → `claude-sonnet-4.5`).
35
+ - **Claude Code Integration**: Easily configure and launch [Claude Code](https://docs.anthropic.com/en/docs/claude-code/overview) to use Copilot as its backend with a simple command-line flag (`--claude-code`).
36
+ - **Usage Dashboard**: A web-based dashboard to monitor your Copilot API usage, view quotas, and see detailed statistics.
37
+ - **Rate Limit Control**: Manage API usage with rate-limiting options (`--rate-limit`) and a waiting mechanism (`--wait`) to prevent errors from rapid requests.
38
+ - **Manual Request Approval**: Manually approve or deny each API request for fine-grained control over usage (`--manual`).
39
+ - **Token Visibility**: Option to display GitHub and Copilot tokens during authentication and refresh for debugging (`--show-token`).
40
+ - **Flexible Authentication**: Authenticate interactively or provide a GitHub token directly, suitable for CI/CD environments.
41
+ - **Support for Different Account Types**: Works with individual, business, and enterprise GitHub Copilot plans.
42
+
43
+ ## Prerequisites
44
+
45
+ - Bun (>= 1.2.x)
46
+ - GitHub account with Copilot subscription (individual, business, or enterprise)
47
+
48
+ ## Installation
49
+
50
+ To install dependencies, run:
51
+
52
+ ```sh
53
+ bun install
54
+ ```
55
+
56
+ ## Using with Docker
57
+
58
+ Build image
59
+
60
+ ```sh
61
+ docker build -t copilot-proxy .
62
+ ```
63
+
64
+ Run the container
65
+
66
+ ```sh
67
+ # Create a directory on your host to persist the GitHub token and related data
68
+ mkdir -p ./copilot-data
69
+
70
+ # Run the container with a bind mount to persist the token
71
+ # This ensures your authentication survives container restarts
72
+
73
+ docker run -p 4141:4141 -v $(pwd)/copilot-data:/root/.local/share/copilot-proxy copilot-proxy
74
+ ```
75
+
76
+ > **Note:**
77
+ > The GitHub token and related data will be stored in `copilot-data` on your host. This is mapped to `/root/.local/share/copilot-proxy` inside the container, ensuring persistence across restarts.
78
+
79
+ ### Docker with Environment Variables
80
+
81
+ You can pass the GitHub token directly to the container using environment variables:
82
+
83
+ ```sh
84
+ # Build with GitHub token
85
+ docker build --build-arg GH_TOKEN=your_github_token_here -t copilot-proxy .
86
+
87
+ # Run with GitHub token
88
+ docker run -p 4141:4141 -e GH_TOKEN=your_github_token_here copilot-proxy
89
+
90
+ # Run with additional options
91
+ docker run -p 4141:4141 -e GH_TOKEN=your_token copilot-proxy start --verbose --port 4141
92
+ ```
93
+
94
+ ### Docker Compose Example
95
+
96
+ ```yaml
97
+ version: '3.8'
98
+ services:
99
+ copilot-proxy:
100
+ build: .
101
+ ports:
102
+ - '4141:4141'
103
+ environment:
104
+ - GH_TOKEN=your_github_token_here
105
+ restart: unless-stopped
106
+ ```
107
+
108
+ The Docker image includes:
109
+
110
+ - Multi-stage build for optimized image size
111
+ - Non-root user for enhanced security
112
+ - Health check for container monitoring
113
+ - Pinned base image version for reproducible builds
114
+
115
+ ## Using with npx
116
+
117
+ You can run the project directly using npx:
118
+
119
+ ```sh
120
+ npx copilot-proxy@latest start
121
+ ```
122
+
123
+ With options:
124
+
125
+ ```sh
126
+ npx copilot-proxy@latest start --port 8080
127
+ ```
128
+
129
+ For authentication only:
130
+
131
+ ```sh
132
+ npx copilot-proxy@latest auth
133
+ ```
134
+
135
+ ## Command Structure
136
+
137
+ Copilot API now uses a subcommand structure with these main commands:
138
+
139
+ - `start`: Start the Copilot API server. This command will also handle authentication if needed.
140
+ - `auth`: Run GitHub authentication flow without starting the server. This is typically used if you need to generate a token for use with the `--github-token` option, especially in non-interactive environments.
141
+ - `check-usage`: Show your current GitHub Copilot usage and quota information directly in the terminal (no server required).
142
+ - `debug`: Display diagnostic information including version, runtime details, file paths, and authentication status. Useful for troubleshooting and support.
143
+
144
+ ## Command Line Options
145
+
146
+ ### Start Command Options
147
+
148
+ The following command line options are available for the `start` command:
149
+
150
+ | Option | Description | Default | Alias |
151
+ | -------------- | ----------------------------------------------------------------------------- | ---------- | ----- |
152
+ | --port | Port to listen on | 4141 | -p |
153
+ | --verbose | Enable verbose logging | false | -v |
154
+ | --account-type | Account type to use (individual, business, enterprise) | individual | -a |
155
+ | --manual | Enable manual request approval | false | none |
156
+ | --rate-limit | Rate limit in seconds between requests | none | -r |
157
+ | --wait | Wait instead of error when rate limit is hit | false | -w |
158
+ | --github-token | Provide GitHub token directly (must be generated using the `auth` subcommand) | none | -g |
159
+ | --claude-code | Generate a command to launch Claude Code with Copilot API config | false | -c |
160
+ | --show-token | Show GitHub and Copilot tokens on fetch and refresh | false | none |
161
+ | --proxy-env | Initialize proxy from environment variables | false | none |
162
+
163
+ ### Auth Command Options
164
+
165
+ | Option | Description | Default | Alias |
166
+ | ------------ | ------------------------- | ------- | ----- |
167
+ | --verbose | Enable verbose logging | false | -v |
168
+ | --show-token | Show GitHub token on auth | false | none |
169
+
170
+ ### Debug Command Options
171
+
172
+ | Option | Description | Default | Alias |
173
+ | ------ | ------------------------- | ------- | ----- |
174
+ | --json | Output debug info as JSON | false | none |
175
+
176
+ ## API Endpoints
177
+
178
+ The server exposes several endpoints to interact with the Copilot API. It provides OpenAI-compatible endpoints and Anthropic-compatible endpoints, allowing for greater flexibility with different tools and services. All endpoints are available with or without the `/v1/` prefix.
179
+
180
+ ### OpenAI Compatible Endpoints
181
+
182
+ These endpoints mimic the OpenAI API structure.
183
+
184
+ | Endpoint | Method | Description |
185
+ | --------------------------- | ------ | --------------------------------------------------------- |
186
+ | `POST /v1/chat/completions` | `POST` | Creates a model response for the given chat conversation. |
187
+ | `GET /v1/models` | `GET` | Lists the currently available models. |
188
+ | `POST /v1/embeddings` | `POST` | Creates an embedding vector representing the input text. |
189
+
190
+ ### OpenAI Responses API Endpoint
191
+
192
+ This endpoint supports the [OpenAI Responses API](https://platform.openai.com/docs/api-reference/responses) format, used by thinking-mode models such as `gpt-5`, `gpt-5.1-codex`, `gpt-5.2-codex`, `o3-mini`, and `o4-mini`. Requests are forwarded directly to Copilot's `/responses` endpoint.
193
+
194
+ | Endpoint | Method | Description |
195
+ | --------------------- | ------ | ------------------------------------------------------------------------ |
196
+ | `POST /v1/responses` | `POST` | Creates a model response using the Responses API (supports streaming). |
197
+
198
+ ### Anthropic Compatible Endpoints
199
+
200
+ These endpoints are designed to be compatible with the Anthropic Messages API. Incoming Anthropic-format requests are automatically translated to the OpenAI format before being sent to Copilot, and responses are translated back.
201
+
202
+ | Endpoint | Method | Description |
203
+ | -------------------------------- | ------ | ------------------------------------------------------------ |
204
+ | `POST /v1/messages` | `POST` | Creates a model response for a given conversation. |
205
+ | `POST /v1/messages/count_tokens` | `POST` | Calculates the number of tokens for a given set of messages. |
206
+
207
+ ### Usage Monitoring Endpoints
208
+
209
+ Endpoints for monitoring your Copilot usage and quotas.
210
+
211
+ | Endpoint | Method | Description |
212
+ | ------------ | ------ | ------------------------------------------------------------ |
213
+ | `GET /usage` | `GET` | Get detailed Copilot usage statistics and quota information. |
214
+ | `GET /token` | `GET` | Get the current Copilot token being used by the API. |
215
+
216
+ ## Example Usage
217
+
218
+ Using with npx:
219
+
220
+ ```sh
221
+ # Basic usage with start command
222
+ npx copilot-proxy@latest start
223
+
224
+ # Run on custom port with verbose logging
225
+ npx copilot-proxy@latest start --port 8080 --verbose
226
+
227
+ # Use with a business plan GitHub account
228
+ npx copilot-proxy@latest start --account-type business
229
+
230
+ # Use with an enterprise plan GitHub account
231
+ npx copilot-proxy@latest start --account-type enterprise
232
+
233
+ # Enable manual approval for each request
234
+ npx copilot-proxy@latest start --manual
235
+
236
+ # Set rate limit to 30 seconds between requests
237
+ npx copilot-proxy@latest start --rate-limit 30
238
+
239
+ # Wait instead of error when rate limit is hit
240
+ npx copilot-proxy@latest start --rate-limit 30 --wait
241
+
242
+ # Provide GitHub token directly
243
+ npx copilot-proxy@latest start --github-token ghp_YOUR_TOKEN_HERE
244
+
245
+ # Run only the auth flow
246
+ npx copilot-proxy@latest auth
247
+
248
+ # Run auth flow with verbose logging
249
+ npx copilot-proxy@latest auth --verbose
250
+
251
+ # Show your Copilot usage/quota in the terminal (no server needed)
252
+ npx copilot-proxy@latest check-usage
253
+
254
+ # Display debug information for troubleshooting
255
+ npx copilot-proxy@latest debug
256
+
257
+ # Display debug information in JSON format
258
+ npx copilot-proxy@latest debug --json
259
+
260
+ # Initialize proxy from environment variables (HTTP_PROXY, HTTPS_PROXY, etc.)
261
+ npx copilot-proxy@latest start --proxy-env
262
+ ```
263
+
264
+ ## Using the Usage Viewer
265
+
266
+ After starting the server, a URL to the Copilot Usage Dashboard will be displayed in your console. This dashboard is a web interface for monitoring your API usage.
267
+
268
+ 1. Start the server. For example, using npx:
269
+ ```sh
270
+ npx copilot-proxy@latest start
271
+ ```
272
+ 2. The server will output a URL to the usage viewer. Copy and paste this URL into your browser. It will look something like this:
273
+ `https://jer-y.github.io/copilot-proxy?endpoint=http://localhost:4141/usage`
274
+ - If you use the `start.bat` script on Windows, this page will open automatically.
275
+
276
+ The dashboard provides a user-friendly interface to view your Copilot usage data:
277
+
278
+ - **API Endpoint URL**: The dashboard is pre-configured to fetch data from your local server endpoint via the URL query parameter. You can change this URL to point to any other compatible API endpoint.
279
+ - **Fetch Data**: Click the "Fetch" button to load or refresh the usage data. The dashboard will automatically fetch data on load.
280
+ - **Usage Quotas**: View a summary of your usage quotas for different services like Chat and Completions, displayed with progress bars for a quick overview.
281
+ - **Detailed Information**: See the full JSON response from the API for a detailed breakdown of all available usage statistics.
282
+ - **URL-based Configuration**: You can also specify the API endpoint directly in the URL using a query parameter. This is useful for bookmarks or sharing links. For example:
283
+ `https://jer-y.github.io/copilot-proxy?endpoint=http://your-api-server/usage`
284
+
285
+ ## Using with Claude Code
286
+
287
+ This proxy can be used to power [Claude Code](https://docs.anthropic.com/en/claude-code), an experimental conversational AI assistant for developers from Anthropic.
288
+
289
+ There are two ways to configure Claude Code to use this proxy:
290
+
291
+ ### Interactive Setup with `--claude-code` flag
292
+
293
+ To get started, run the `start` command with the `--claude-code` flag:
294
+
295
+ ```sh
296
+ npx copilot-proxy@latest start --claude-code
297
+ ```
298
+
299
+ You will be prompted to select a primary model and a "small, fast" model for background tasks. After selecting the models, a command will be copied to your clipboard. This command sets the necessary environment variables for Claude Code to use the proxy.
300
+
301
+ Paste and run this command in a new terminal to launch Claude Code.
302
+
303
+ ### Manual Configuration with `settings.json`
304
+
305
+ Alternatively, you can configure Claude Code by creating a `.claude/settings.json` file in your project's root directory. This file should contain the environment variables needed by Claude Code. This way you don't need to run the interactive setup every time.
306
+
307
+ Here is an example `.claude/settings.json` file:
308
+
309
+ ```json
310
+ {
311
+ "env": {
312
+ "ANTHROPIC_BASE_URL": "http://localhost:4141",
313
+ "ANTHROPIC_AUTH_TOKEN": "dummy",
314
+ "ANTHROPIC_MODEL": "gpt-4.1",
315
+ "ANTHROPIC_DEFAULT_SONNET_MODEL": "gpt-4.1",
316
+ "ANTHROPIC_SMALL_FAST_MODEL": "gpt-4.1",
317
+ "ANTHROPIC_DEFAULT_HAIKU_MODEL": "gpt-4.1",
318
+ "DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1",
319
+ "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1"
320
+ },
321
+ "permissions": {
322
+ "deny": [
323
+ "WebSearch"
324
+ ]
325
+ }
326
+ }
327
+ ```
328
+
329
+ You can find more options here: [Claude Code settings](https://docs.anthropic.com/en/docs/claude-code/settings#environment-variables)
330
+
331
+ You can also read more about IDE integration here: [Add Claude Code to your IDE](https://docs.anthropic.com/en/docs/claude-code/ide-integrations)
332
+
333
+ ## Running from Source
334
+
335
+ The project can be run from source in several ways:
336
+
337
+ ### Development Mode
338
+
339
+ ```sh
340
+ bun run dev
341
+ ```
342
+
343
+ ### Production Mode
344
+
345
+ ```sh
346
+ bun run start
347
+ ```
348
+
349
+ ## Usage Tips
350
+
351
+ - To avoid hitting GitHub Copilot's rate limits, you can use the following flags:
352
+ - `--manual`: Enables manual approval for each request, giving you full control over when requests are sent.
353
+ - `--rate-limit <seconds>`: Enforces a minimum time interval between requests. For example, `copilot-proxy start --rate-limit 30` will ensure there's at least a 30-second gap between requests.
354
+ - `--wait`: Use this with `--rate-limit`. It makes the server wait for the cooldown period to end instead of rejecting the request with an error. This is useful for clients that don't automatically retry on rate limit errors.
355
+ - If you have a GitHub business or enterprise plan account with Copilot, use the `--account-type` flag (e.g., `--account-type business`). See the [official documentation](https://docs.github.com/en/enterprise-cloud@latest/copilot/managing-copilot/managing-github-copilot-in-your-organization/managing-access-to-github-copilot-in-your-organization/managing-github-copilot-access-to-your-organizations-network#configuring-copilot-subscription-based-network-routing-for-your-enterprise-or-organization) for more details.
356
+
357
+ ## Acknowledgments
358
+
359
+ This project is forked from [ericc-ch/copilot-api](https://github.com/ericc-ch/copilot-api). This repository was created for personal use.