heibaiapi 0.6.1

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 Erick Christian Purwanto
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,348 @@
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
+ [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/E1E519XS7W)
21
+
22
+ ---
23
+
24
+ **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.
25
+
26
+ ---
27
+
28
+ ## Project Overview
29
+
30
+ 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).
31
+
32
+ ## Features
33
+
34
+ - **OpenAI & Anthropic Compatibility**: Exposes GitHub Copilot as an OpenAI-compatible (`/v1/chat/completions`, `/v1/models`, `/v1/embeddings`) and Anthropic-compatible (`/v1/messages`) API.
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
+ ## Demo
44
+
45
+ https://github.com/user-attachments/assets/7654b383-669d-4eb9-b23c-06d7aefee8c5
46
+
47
+ ## Prerequisites
48
+
49
+ - Bun (>= 1.2.x)
50
+ - GitHub account with Copilot subscription (individual, business, or enterprise)
51
+
52
+ ## Installation
53
+
54
+ To install dependencies, run:
55
+
56
+ ```sh
57
+ bun install
58
+ ```
59
+
60
+ ## Using with Docker
61
+
62
+ Build image
63
+
64
+ ```sh
65
+ docker build -t copilot-api .
66
+ ```
67
+
68
+ Run the container
69
+
70
+ ```sh
71
+ # Create a directory on your host to persist the GitHub token and related data
72
+ mkdir -p ./copilot-data
73
+
74
+ # Run the container with a bind mount to persist the token
75
+ # This ensures your authentication survives container restarts
76
+
77
+ docker run -p 4141:4141 -v $(pwd)/copilot-data:/root/.local/share/copilot-api copilot-api
78
+ ```
79
+
80
+ > **Note:**
81
+ > The GitHub token and related data will be stored in `copilot-data` on your host. This is mapped to `/root/.local/share/copilot-api` inside the container, ensuring persistence across restarts.
82
+
83
+ ### Docker with Environment Variables
84
+
85
+ You can pass the GitHub token directly to the container using environment variables:
86
+
87
+ ```sh
88
+ # Build with GitHub token
89
+ docker build --build-arg GH_TOKEN=your_github_token_here -t copilot-api .
90
+
91
+ # Run with GitHub token
92
+ docker run -p 4141:4141 -e GH_TOKEN=your_github_token_here copilot-api
93
+
94
+ # Run with additional options
95
+ docker run -p 4141:4141 -e GH_TOKEN=your_token copilot-api start --verbose --port 4141
96
+ ```
97
+
98
+ ### Docker Compose Example
99
+
100
+ ```yaml
101
+ version: "3.8"
102
+ services:
103
+ copilot-api:
104
+ build: .
105
+ ports:
106
+ - "4141:4141"
107
+ environment:
108
+ - GH_TOKEN=your_github_token_here
109
+ restart: unless-stopped
110
+ ```
111
+
112
+ The Docker image includes:
113
+
114
+ - Multi-stage build for optimized image size
115
+ - Non-root user for enhanced security
116
+ - Health check for container monitoring
117
+ - Pinned base image version for reproducible builds
118
+
119
+ ## Using with npx
120
+
121
+ You can run the project directly using npx:
122
+
123
+ ```sh
124
+ npx copilot-api@latest start
125
+ ```
126
+
127
+ With options:
128
+
129
+ ```sh
130
+ npx copilot-api@latest start --port 8080
131
+ ```
132
+
133
+ For authentication only:
134
+
135
+ ```sh
136
+ npx copilot-api@latest auth
137
+ ```
138
+
139
+ ## Command Structure
140
+
141
+ Copilot API now uses a subcommand structure with these main commands:
142
+
143
+ - `start`: Start the Copilot API server. This command will also handle authentication if needed.
144
+ - `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.
145
+ - `check-usage`: Show your current GitHub Copilot usage and quota information directly in the terminal (no server required).
146
+ - `debug`: Display diagnostic information including version, runtime details, file paths, and authentication status. Useful for troubleshooting and support.
147
+
148
+ ## Command Line Options
149
+
150
+ ### Start Command Options
151
+
152
+ The following command line options are available for the `start` command:
153
+
154
+ | Option | Description | Default | Alias |
155
+ | -------------- | ----------------------------------------------------------------------------- | ---------- | ----- |
156
+ | --port | Port to listen on | 4141 | -p |
157
+ | --verbose | Enable verbose logging | false | -v |
158
+ | --account-type | Account type to use (individual, business, enterprise) | individual | -a |
159
+ | --manual | Enable manual request approval | false | none |
160
+ | --rate-limit | Rate limit in seconds between requests | none | -r |
161
+ | --wait | Wait instead of error when rate limit is hit | false | -w |
162
+ | --github-token | Provide GitHub token directly (must be generated using the `auth` subcommand) | none | -g |
163
+ | --claude-code | Generate a command to launch Claude Code with Copilot API config | false | -c |
164
+ | --show-token | Show GitHub and Copilot tokens on fetch and refresh | false | none |
165
+
166
+ ### Auth Command Options
167
+
168
+ | Option | Description | Default | Alias |
169
+ | ------------ | ------------------------- | ------- | ----- |
170
+ | --verbose | Enable verbose logging | false | -v |
171
+ | --show-token | Show GitHub token on auth | false | none |
172
+
173
+ ### Debug Command Options
174
+
175
+ | Option | Description | Default | Alias |
176
+ | ------ | ------------------------- | ------- | ----- |
177
+ | --json | Output debug info as JSON | false | none |
178
+
179
+ ## API Endpoints
180
+
181
+ The server exposes several endpoints to interact with the Copilot API. It provides OpenAI-compatible endpoints and now also includes support for Anthropic-compatible endpoints, allowing for greater flexibility with different tools and services.
182
+
183
+ ### OpenAI Compatible Endpoints
184
+
185
+ These endpoints mimic the OpenAI API structure.
186
+
187
+ | Endpoint | Method | Description |
188
+ | --------------------------- | ------ | ---------------------------------------------------------------- |
189
+ | `POST /v1/responses` | `POST` | Most advanced interface for generating model responses. |
190
+ | `POST /v1/chat/completions` | `POST` | Creates a model response for the given chat conversation. |
191
+ | `GET /v1/models` | `GET` | Lists the currently available models. |
192
+ | `POST /v1/embeddings` | `POST` | Creates an embedding vector representing the input text. |
193
+
194
+ ### Anthropic Compatible Endpoints
195
+
196
+ These endpoints are designed to be compatible with the Anthropic Messages API.
197
+
198
+ | Endpoint | Method | Description |
199
+ | -------------------------------- | ------ | ------------------------------------------------------------ |
200
+ | `POST /v1/messages` | `POST` | Creates a model response for a given conversation. |
201
+ | `POST /v1/messages/count_tokens` | `POST` | Calculates the number of tokens for a given set of messages. |
202
+
203
+ ### Usage Monitoring Endpoints
204
+
205
+ New endpoints for monitoring your Copilot usage and quotas.
206
+
207
+ | Endpoint | Method | Description |
208
+ | ------------ | ------ | ------------------------------------------------------------ |
209
+ | `GET /usage` | `GET` | Get detailed Copilot usage statistics and quota information. |
210
+ | `GET /token` | `GET` | Get the current Copilot token being used by the API. |
211
+
212
+ ## Example Usage
213
+
214
+ Using with npx:
215
+
216
+ ```sh
217
+ # Basic usage with start command
218
+ npx copilot-api@latest start
219
+
220
+ # Run on custom port with verbose logging
221
+ npx copilot-api@latest start --port 8080 --verbose
222
+
223
+ # Use with a business plan GitHub account
224
+ npx copilot-api@latest start --account-type business
225
+
226
+ # Use with an enterprise plan GitHub account
227
+ npx copilot-api@latest start --account-type enterprise
228
+
229
+ # Enable manual approval for each request
230
+ npx copilot-api@latest start --manual
231
+
232
+ # Set rate limit to 30 seconds between requests
233
+ npx copilot-api@latest start --rate-limit 30
234
+
235
+ # Wait instead of error when rate limit is hit
236
+ npx copilot-api@latest start --rate-limit 30 --wait
237
+
238
+ # Provide GitHub token directly
239
+ npx copilot-api@latest start --github-token ghp_YOUR_TOKEN_HERE
240
+
241
+ # Run only the auth flow
242
+ npx copilot-api@latest auth
243
+
244
+ # Run auth flow with verbose logging
245
+ npx copilot-api@latest auth --verbose
246
+
247
+ # Show your Copilot usage/quota in the terminal (no server needed)
248
+ npx copilot-api@latest check-usage
249
+
250
+ # Display debug information for troubleshooting
251
+ npx copilot-api@latest debug
252
+
253
+ # Display debug information in JSON format
254
+ npx copilot-api@latest debug --json
255
+ ```
256
+
257
+ ## Using the Usage Viewer
258
+
259
+ 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.
260
+
261
+ 1. Start the server. For example, using npx:
262
+ ```sh
263
+ npx copilot-api@latest start
264
+ ```
265
+ 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:
266
+ `https://ericc-ch.github.io/copilot-api?endpoint=http://localhost:4141/usage`
267
+ - If you use the `start.bat` script on Windows, this page will open automatically.
268
+
269
+ The dashboard provides a user-friendly interface to view your Copilot usage data:
270
+
271
+ - **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.
272
+ - **Fetch Data**: Click the "Fetch" button to load or refresh the usage data. The dashboard will automatically fetch data on load.
273
+ - **Usage Quotas**: View a summary of your usage quotas for different services like Chat and Completions, displayed with progress bars for a quick overview.
274
+ - **Detailed Information**: See the full JSON response from the API for a detailed breakdown of all available usage statistics.
275
+ - **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:
276
+ `https://ericc-ch.github.io/copilot-api?endpoint=http://your-api-server/usage`
277
+
278
+ ## Using with Claude Code
279
+
280
+ 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.
281
+
282
+ There are two ways to configure Claude Code to use this proxy:
283
+
284
+ ### Interactive Setup with `--claude-code` flag
285
+
286
+ To get started, run the `start` command with the `--claude-code` flag:
287
+
288
+ ```sh
289
+ npx copilot-api@latest start --claude-code
290
+ ```
291
+
292
+ 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.
293
+
294
+ Paste and run this command in a new terminal to launch Claude Code.
295
+
296
+ ### Manual Configuration with `settings.json`
297
+
298
+ 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.
299
+
300
+ Here is an example `.claude/settings.json` file:
301
+
302
+ ```json
303
+ {
304
+ "env": {
305
+ "ANTHROPIC_BASE_URL": "http://localhost:4141",
306
+ "ANTHROPIC_AUTH_TOKEN": "dummy",
307
+ "ANTHROPIC_MODEL": "gpt-4.1",
308
+ "ANTHROPIC_DEFAULT_SONNET_MODEL": "gpt-4.1",
309
+ "ANTHROPIC_SMALL_FAST_MODEL": "gpt-4.1",
310
+ "ANTHROPIC_DEFAULT_HAIKU_MODEL": "gpt-4.1",
311
+ "DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1",
312
+ "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1"
313
+ },
314
+ "permissions": {
315
+ "deny": [
316
+ "WebSearch"
317
+ ]
318
+ }
319
+ }
320
+ ```
321
+
322
+ You can find more options here: [Claude Code settings](https://docs.anthropic.com/en/docs/claude-code/settings#environment-variables)
323
+
324
+ 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)
325
+
326
+ ## Running from Source
327
+
328
+ The project can be run from source in several ways:
329
+
330
+ ### Development Mode
331
+
332
+ ```sh
333
+ bun run dev
334
+ ```
335
+
336
+ ### Production Mode
337
+
338
+ ```sh
339
+ bun run start
340
+ ```
341
+
342
+ ## Usage Tips
343
+
344
+ - To avoid hitting GitHub Copilot's rate limits, you can use the following flags:
345
+ - `--manual`: Enables manual approval for each request, giving you full control over when requests are sent.
346
+ - `--rate-limit <seconds>`: Enforces a minimum time interval between requests. For example, `copilot-api start --rate-limit 30` will ensure there's at least a 30-second gap between requests.
347
+ - `--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.
348
+ - 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.