@hsupu/copilot-api 0.7.15 → 0.7.17
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/README.md +168 -127
- package/dist/{main.js → main.mjs} +4184 -3628
- package/dist/main.mjs.map +1 -0
- package/package.json +18 -14
- package/dist/main.js.map +0 -1
package/README.md
CHANGED
|
@@ -4,131 +4,182 @@
|
|
|
4
4
|
> This is a fork of [ericc-ch/copilot-api](https://github.com/ericc-ch/copilot-api) with additional improvements and bug fixes.
|
|
5
5
|
|
|
6
6
|
> [!WARNING]
|
|
7
|
-
> This is a reverse
|
|
7
|
+
> This is a reverse proxy for the GitHub Copilot API. It is not officially supported by GitHub and may break at any time. Use at your own risk.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
A reverse proxy that exposes GitHub Copilot API as OpenAI and Anthropic compatible API endpoints. Works with Claude Code and other tools that speak OpenAI or Anthropic protocols.
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
### New Features
|
|
14
|
-
|
|
15
|
-
- **`--host` option**: Bind the server to a specific network interface (e.g., `--host 0.0.0.0` for all interfaces, `--host 127.0.0.1` for localhost only)
|
|
16
|
-
- **Adaptive rate limiting**: Smart rate limiting with exponential backoff, auto-recovery, and Retry-After support (replaces queue-based limiting)
|
|
17
|
-
- **Direct Anthropic API**: Claude models use Copilot's native Anthropic endpoint without translation overhead
|
|
18
|
-
- **Smart auto-truncate**: Automatically truncates conversation history when exceeding context limits, with optional tool result compression
|
|
19
|
-
- **`/v1/event_logging/batch` endpoint**: Compatibility endpoint for Anthropic SDK's event logging (returns OK without processing)
|
|
20
|
-
- **`logout` command**: Remove stored GitHub token with `copilot-api logout`
|
|
21
|
-
- **`patch-claude` command**: Patch Claude Code's context window limit to match Copilot's limits
|
|
22
|
-
- **Tool name length handling**: Automatically truncates long tool names (>64 chars) to comply with OpenAI's limit, with hash-based suffix to avoid collisions. Original names are restored in responses.
|
|
23
|
-
- **Request History UI**: Built-in Web UI (enabled by default) to view, search, filter, and export all API requests/responses. Access at `/history`.
|
|
11
|
+
## Quick Start
|
|
24
12
|
|
|
25
|
-
###
|
|
13
|
+
### Install from npm (Recommended)
|
|
26
14
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
-
|
|
15
|
+
```sh
|
|
16
|
+
# Run directly
|
|
17
|
+
npx -y @hsupu/copilot-api start
|
|
18
|
+
```
|
|
30
19
|
|
|
31
|
-
###
|
|
20
|
+
### Run from Source
|
|
32
21
|
|
|
33
|
-
|
|
22
|
+
```sh
|
|
23
|
+
git clone https://github.com/puxu-msft/copilot-api-js.git
|
|
24
|
+
cd copilot-api-js
|
|
25
|
+
bun install
|
|
26
|
+
bun run dev # Development mode with hot reload
|
|
27
|
+
bun run start # Production mode
|
|
28
|
+
bun run build # Build for distribution
|
|
29
|
+
```
|
|
34
30
|
|
|
35
|
-
##
|
|
31
|
+
## Using with Claude Code
|
|
36
32
|
|
|
37
|
-
|
|
33
|
+
Run the interactive setup command:
|
|
38
34
|
|
|
39
35
|
```sh
|
|
40
|
-
|
|
41
|
-
|
|
36
|
+
copilot-api setup-claude-code
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Or manually create `~/.claude/settings.json`:
|
|
42
40
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
41
|
+
```json
|
|
42
|
+
{
|
|
43
|
+
"env": {
|
|
44
|
+
"ANTHROPIC_BASE_URL": "http://localhost:4141",
|
|
45
|
+
"ANTHROPIC_AUTH_TOKEN": "dummy",
|
|
46
|
+
"ANTHROPIC_MODEL": "opus",
|
|
47
|
+
"ANTHROPIC_SMALL_FAST_MODEL": "haiku",
|
|
48
|
+
"DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1",
|
|
49
|
+
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
46
52
|
```
|
|
47
53
|
|
|
48
|
-
|
|
54
|
+
## Features
|
|
49
55
|
|
|
50
|
-
|
|
56
|
+
### Dual API Compatibility
|
|
51
57
|
|
|
52
|
-
|
|
53
|
-
npm install -g github:puxu-msft/copilot-api-js
|
|
54
|
-
copilot-api start
|
|
55
|
-
```
|
|
58
|
+
Exposes both OpenAI and Anthropic compatible endpoints through a single proxy:
|
|
56
59
|
|
|
57
|
-
|
|
60
|
+
- **Direct Anthropic path** — Uses Copilot API's anthropic endpoint
|
|
61
|
+
- **Translated path** — Translates to OpenAI format and uses Copilot's OpenAI-compatible endpoint
|
|
58
62
|
|
|
59
|
-
|
|
60
|
-
# Clone the repository
|
|
61
|
-
git clone https://github.com/puxu-msft/copilot-api-js.git
|
|
62
|
-
cd copilot-api-js
|
|
63
|
+
### Adaptive Rate Limiting
|
|
63
64
|
|
|
64
|
-
|
|
65
|
-
bun install
|
|
65
|
+
Intelligent rate limiting with exponential backoff, replacing the upstream queue-based approach. Operates in three modes:
|
|
66
66
|
|
|
67
|
-
|
|
68
|
-
|
|
67
|
+
- **Normal** — Requests pass through freely
|
|
68
|
+
- **Rate-limited** — Queues requests with configurable intervals after hitting limits
|
|
69
|
+
- **Recovering** — Gradually resumes normal operation after consecutive successes
|
|
69
70
|
|
|
70
|
-
|
|
71
|
-
bun run start
|
|
71
|
+
Learns from Copilot API's `Retry-After` headers for optimal retry timing.
|
|
72
72
|
|
|
73
|
-
|
|
74
|
-
bun run build
|
|
75
|
-
```
|
|
73
|
+
### Auto-Truncate
|
|
76
74
|
|
|
77
|
-
|
|
75
|
+
Automatically handles context length limits (enabled by default):
|
|
78
76
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
77
|
+
- **Reactive** — Retries failed requests with a truncated payload when hitting token or byte limits
|
|
78
|
+
- **Proactive** — Pre-checks requests against known model limits before sending
|
|
79
|
+
- **Dynamic limit learning** — Adjusts limits based on actual API error responses
|
|
80
|
+
- **Tool result compression** — Compresses old `tool_result` content before truncating messages, preserving more conversation context
|
|
81
|
+
- Up to 5 retry attempts per request with 2% safety margin
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
83
|
+
### Message Sanitization
|
|
84
|
+
|
|
85
|
+
Cleans up messages before forwarding to the API:
|
|
86
|
+
|
|
87
|
+
- Filters orphaned `tool_use` / `tool_result` blocks (unpaired due to interrupted tool calls or truncation)
|
|
88
|
+
- Handles server-side tools (`server_tool_use` / `*_tool_result`) that appear inline in assistant messages
|
|
89
|
+
- Fixes double-serialized tool inputs from stream accumulation
|
|
90
|
+
- Removes corrupted blocks from older history data
|
|
91
|
+
- Fixes tool name casing mismatches
|
|
92
|
+
- Removes empty text content blocks
|
|
93
|
+
- Strips `<system-reminder>` tags from message content
|
|
94
|
+
|
|
95
|
+
### Model Name Translation
|
|
96
|
+
|
|
97
|
+
Translates client-sent model names to matching Copilot models:
|
|
98
|
+
|
|
99
|
+
| Input | Resolved To |
|
|
100
|
+
|-------|-------------|
|
|
101
|
+
| `opus`, `sonnet`, `haiku` | Best available model in that family |
|
|
102
|
+
| `claude-opus-4-6` | `claude-opus-4.6` |
|
|
103
|
+
| `claude-sonnet-4-5-20250514` | `claude-sonnet-4.5` |
|
|
104
|
+
| `claude-sonnet-4`, `gpt-4` | Passed through directly |
|
|
105
|
+
|
|
106
|
+
Each model family has a priority list. Short aliases resolve to the first available model.
|
|
87
107
|
|
|
88
|
-
|
|
108
|
+
### Server-Side Tools
|
|
109
|
+
|
|
110
|
+
Supports Anthropic server-side tools (e.g., `web_search`, `tool_search`). These tools are executed by the API backend, with both `server_tool_use` and result blocks appearing inline in assistant messages. Tool definitions can optionally be rewritten to a custom format (configurable via `--no-rewrite-anthropic-tools`).
|
|
111
|
+
|
|
112
|
+
### Request History UI
|
|
113
|
+
|
|
114
|
+
Built-in web interface for inspecting API requests and responses. Access at `http://localhost:4141/history`.
|
|
115
|
+
|
|
116
|
+
- Real-time updates via WebSocket
|
|
117
|
+
- Filter by model, endpoint, status, and time range
|
|
118
|
+
- Full-text search across request/response content
|
|
119
|
+
- Export as JSON or CSV
|
|
120
|
+
- Session tracking and statistics
|
|
121
|
+
|
|
122
|
+
### Additional Features
|
|
123
|
+
|
|
124
|
+
- **Sonnet → Opus redirection** — Optionally redirect sonnet model requests to the best available opus model
|
|
125
|
+
- **Security research mode** — Passphrase-protected mode for authorized penetration testing, CTF competitions, and security education
|
|
126
|
+
- **Tool name truncation** — Automatically truncates tool names exceeding 64 characters (OpenAI limit) with hash suffixes, restoring original names in responses
|
|
127
|
+
- **Health checks** — Container-ready health endpoint at `/health`
|
|
128
|
+
- **Graceful shutdown** — Connection draining on shutdown signals
|
|
129
|
+
- **Proxy support** — HTTP/HTTPS proxy via environment variables
|
|
130
|
+
|
|
131
|
+
## Commands
|
|
89
132
|
|
|
90
133
|
| Command | Description |
|
|
91
134
|
|---------|-------------|
|
|
92
|
-
| `start` | Start the API server (
|
|
135
|
+
| `start` | Start the API server (authenticates automatically if needed) |
|
|
93
136
|
| `auth` | Run GitHub authentication flow only |
|
|
94
137
|
| `logout` | Remove stored GitHub token |
|
|
95
|
-
| `check-usage` | Show Copilot usage and quota |
|
|
96
|
-
| `debug` | Display diagnostic information |
|
|
97
|
-
| `
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
|
104
|
-
|
|
105
|
-
| `--
|
|
106
|
-
| `--
|
|
107
|
-
| `--
|
|
108
|
-
| `--
|
|
109
|
-
| `--
|
|
110
|
-
| `--
|
|
111
|
-
| `--
|
|
112
|
-
| `--
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
|
117
|
-
|
|
118
|
-
| `--
|
|
119
|
-
| `--
|
|
120
|
-
| `--
|
|
121
|
-
| `--
|
|
122
|
-
| `--
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
| Option |
|
|
127
|
-
|
|
128
|
-
| `--
|
|
129
|
-
| `--
|
|
130
|
-
|
|
131
|
-
|
|
138
|
+
| `check-usage` | Show Copilot usage and quota information |
|
|
139
|
+
| `debug info` | Display diagnostic information |
|
|
140
|
+
| `debug models` | Fetch and display raw model data from Copilot API |
|
|
141
|
+
| `list-claude-code` | List all locally installed Claude Code versions |
|
|
142
|
+
| `setup-claude-code` | Interactively configure Claude Code to use this proxy |
|
|
143
|
+
|
|
144
|
+
### `start` Options
|
|
145
|
+
|
|
146
|
+
| Option | Default | Description |
|
|
147
|
+
|--------|---------|-------------|
|
|
148
|
+
| `--port`, `-p` | 4141 | Port to listen on |
|
|
149
|
+
| `--host`, `-H` | (all interfaces) | Host/interface to bind to |
|
|
150
|
+
| `--verbose`, `-v` | false | Enable verbose logging |
|
|
151
|
+
| `--account-type`, `-a` | individual | Account type: `individual`, `business`, or `enterprise` |
|
|
152
|
+
| `--manual` | false | Manual request approval mode |
|
|
153
|
+
| `--github-token`, `-g` | | Provide GitHub token directly |
|
|
154
|
+
| `--proxy-env` | false | Use proxy from environment variables |
|
|
155
|
+
| `--history-limit` | 200 | Max history entries in memory (0 = unlimited) |
|
|
156
|
+
|
|
157
|
+
**Rate Limiting:**
|
|
158
|
+
|
|
159
|
+
| Option | Default | Description |
|
|
160
|
+
|--------|---------|-------------|
|
|
161
|
+
| `--no-rate-limit` | false | Disable adaptive rate limiting |
|
|
162
|
+
| `--retry-interval` | 10 | Seconds to wait before retrying after rate limit |
|
|
163
|
+
| `--request-interval` | 10 | Seconds between requests in rate-limited mode |
|
|
164
|
+
| `--recovery-timeout` | 10 | Minutes before attempting recovery |
|
|
165
|
+
| `--consecutive-successes` | 5 | Consecutive successes needed to exit rate-limited mode |
|
|
166
|
+
|
|
167
|
+
**Auto-Truncate:**
|
|
168
|
+
|
|
169
|
+
| Option | Default | Description |
|
|
170
|
+
|--------|---------|-------------|
|
|
171
|
+
| `--no-auto-truncate` | false | Disable auto-truncation on context limit errors |
|
|
172
|
+
| `--no-compress-tool-results` | false | Disable tool result compression during truncation |
|
|
173
|
+
|
|
174
|
+
**Anthropic-Specific:**
|
|
175
|
+
|
|
176
|
+
| Option | Default | Description |
|
|
177
|
+
|--------|---------|-------------|
|
|
178
|
+
| `--redirect-anthropic` | false | Force Anthropic requests through OpenAI translation |
|
|
179
|
+
| `--no-rewrite-anthropic-tools` | false | Don't rewrite server-side tools to custom format |
|
|
180
|
+
| `--redirect-count-tokens` | false | Route count_tokens through OpenAI translation |
|
|
181
|
+
| `--redirect-sonnet-to-opus` | false | Redirect sonnet requests to best available opus |
|
|
182
|
+
| `--security-research-mode` | | Enable security research mode with passphrase |
|
|
132
183
|
|
|
133
184
|
## API Endpoints
|
|
134
185
|
|
|
@@ -138,53 +189,43 @@ copilot-api start
|
|
|
138
189
|
|----------|--------|-------------|
|
|
139
190
|
| `/v1/chat/completions` | POST | Chat completions |
|
|
140
191
|
| `/v1/models` | GET | List available models |
|
|
192
|
+
| `/v1/models/:model` | GET | Get specific model details |
|
|
141
193
|
| `/v1/embeddings` | POST | Text embeddings |
|
|
142
194
|
|
|
195
|
+
All endpoints also work without the `/v1` prefix.
|
|
196
|
+
|
|
143
197
|
### Anthropic Compatible
|
|
144
198
|
|
|
145
199
|
| Endpoint | Method | Description |
|
|
146
200
|
|----------|--------|-------------|
|
|
147
201
|
| `/v1/messages` | POST | Messages API |
|
|
148
202
|
| `/v1/messages/count_tokens` | POST | Token counting |
|
|
149
|
-
| `/
|
|
203
|
+
| `/api/event_logging/batch` | POST | Event logging (no-op, returns OK) |
|
|
150
204
|
|
|
151
205
|
### Utility
|
|
152
206
|
|
|
153
207
|
| Endpoint | Method | Description |
|
|
154
208
|
|----------|--------|-------------|
|
|
155
|
-
|
|
|
156
|
-
| `/usage` | GET | Copilot usage
|
|
157
|
-
| `/token` | GET | Current Copilot token |
|
|
158
|
-
| `/
|
|
159
|
-
| `/history` |
|
|
160
|
-
| `/history/api
|
|
209
|
+
| `/health` | GET | Health check (200 healthy, 503 unhealthy) |
|
|
210
|
+
| `/usage` | GET | Copilot usage and quota statistics |
|
|
211
|
+
| `/token` | GET | Current Copilot token information |
|
|
212
|
+
| `/history` | GET | Request history web UI |
|
|
213
|
+
| `/history/ws` | WebSocket | Real-time history updates |
|
|
214
|
+
| `/history/api/entries` | GET | Query history entries |
|
|
215
|
+
| `/history/api/stats` | GET | Usage statistics |
|
|
216
|
+
| `/history/api/export` | GET | Export history (JSON/CSV) |
|
|
217
|
+
| `/history/api/sessions` | GET | List sessions |
|
|
161
218
|
|
|
162
|
-
##
|
|
219
|
+
## Account Types
|
|
163
220
|
|
|
164
|
-
|
|
221
|
+
The account type determines the Copilot API base URL:
|
|
165
222
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
"ANTHROPIC_MODEL": "gpt-4.1",
|
|
172
|
-
"ANTHROPIC_SMALL_FAST_MODEL": "gpt-4.1",
|
|
173
|
-
"DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1",
|
|
174
|
-
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1"
|
|
175
|
-
},
|
|
176
|
-
"permissions": {
|
|
177
|
-
"deny": ["WebSearch"]
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
Or use the interactive setup:
|
|
183
|
-
|
|
184
|
-
```sh
|
|
185
|
-
bun run start --claude-code
|
|
186
|
-
```
|
|
223
|
+
| Type | API Base URL |
|
|
224
|
+
|------|-------------|
|
|
225
|
+
| `individual` | `api.githubcopilot.com` |
|
|
226
|
+
| `business` | `api.business.githubcopilot.com` |
|
|
227
|
+
| `enterprise` | `api.enterprise.githubcopilot.com` |
|
|
187
228
|
|
|
188
|
-
##
|
|
229
|
+
## License
|
|
189
230
|
|
|
190
|
-
|
|
231
|
+
[MIT](LICENSE)
|