@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 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-engineered proxy of GitHub Copilot API. It is not supported by GitHub, and may break unexpectedly. Use at your own risk.
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
- ## Fork Improvements
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
- This fork includes the following enhancements over the upstream project:
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
- ### Bug Fixes
13
+ ### Install from npm (Recommended)
26
14
 
27
- - **Fixed missing `model` field in streaming**: The first streaming chunk from Copilot API sometimes has an empty `choices` array but contains the model name. We now store this for use in subsequent events.
28
- - **Auto-fix message sequence errors**: When tool calls are interrupted (e.g., by user cancel), the API now automatically adds placeholder `tool_result` blocks to maintain valid message sequences
29
- - **Fixed `bunx` symlink issue**: Changed pre-commit hook to use `bun x` instead of `bunx` for better compatibility
15
+ ```sh
16
+ # Run directly
17
+ npx -y @hsupu/copilot-api start
18
+ ```
30
19
 
31
- ### Documentation
20
+ ### Run from Source
32
21
 
33
- - Added [CLAUDE.md](./CLAUDE.md) with project architecture documentation
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
- ## Quick Start
31
+ ## Using with Claude Code
36
32
 
37
- ### Install from npm (Recommended)
33
+ Run the interactive setup command:
38
34
 
39
35
  ```sh
40
- # Run directly with npx
41
- npx @hsupu/copilot-api start
36
+ copilot-api setup-claude-code
37
+ ```
38
+
39
+ Or manually create `~/.claude/settings.json`:
42
40
 
43
- # Or install globally
44
- npm install -g @hsupu/copilot-api
45
- copilot-api start
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
- ### Install from GitHub
54
+ ## Features
49
55
 
50
- You can also install directly from GitHub (requires build step):
56
+ ### Dual API Compatibility
51
57
 
52
- ```sh
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
- ### Running from Source
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
- ```sh
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
- # Install dependencies
65
- bun install
65
+ Intelligent rate limiting with exponential backoff, replacing the upstream queue-based approach. Operates in three modes:
66
66
 
67
- # Development mode (with hot reload)
68
- bun run dev
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
- # Production mode
71
- bun run start
71
+ Learns from Copilot API's `Retry-After` headers for optimal retry timing.
72
72
 
73
- # Build for distribution
74
- bun run build
75
- ```
73
+ ### Auto-Truncate
76
74
 
77
- ### After Building
75
+ Automatically handles context length limits (enabled by default):
78
76
 
79
- ```sh
80
- # Run the built version locally
81
- npx .
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
- # Or link globally
84
- bun link
85
- copilot-api start
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
- ## Command Reference
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 (handles auth if needed) |
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
- | `patch-claude` | Patch Claude Code's context window limit |
98
-
99
- ### Start Command Options
100
-
101
- | Option | Description | Default |
102
- |--------|-------------|---------|
103
- | `--port`, `-p` | Port to listen on | 4141 |
104
- | `--host`, `-H` | Host/interface to bind to | (all interfaces) |
105
- | `--verbose`, `-v` | Enable verbose logging | false |
106
- | `--account-type`, `-a` | Account type (individual, business, enterprise) | individual |
107
- | `--manual` | Manual request approval mode | false |
108
- | `--no-rate-limit` | Disable adaptive rate limiting | false |
109
- | `--retry-interval` | Seconds to wait before retrying after rate limit | 10 |
110
- | `--request-interval` | Seconds between requests in rate-limited mode | 10 |
111
- | `--recovery-timeout` | Minutes before attempting recovery | 10 |
112
- | `--consecutive-successes` | Successes needed to exit rate-limited mode | 5 |
113
- | `--github-token`, `-g` | Provide GitHub token directly | none |
114
- | `--claude-code`, `-c` | Generate Claude Code launch command | false |
115
- | `--show-token` | Show tokens on fetch/refresh | false |
116
- | `--proxy-env` | Use proxy from environment | false |
117
- | `--no-history` | Disable request history UI at `/history` | false |
118
- | `--history-limit` | Max history entries in memory | 1000 |
119
- | `--no-auto-truncate` | Disable auto-truncate when exceeding limits | false |
120
- | `--compress-tool-results` | Compress old tool results before truncating | false |
121
- | `--redirect-anthropic` | Force Anthropic through OpenAI translation | false |
122
- | `--no-rewrite-anthropic-tools` | Don't rewrite server-side tools | false |
123
-
124
- ### Patch-Claude Command Options
125
-
126
- | Option | Description | Default |
127
- |--------|-------------|---------|
128
- | `--limit`, `-l` | Context window limit in tokens | 128000 |
129
- | `--restore`, `-r` | Restore original 200k limit | false |
130
- | `--path`, `-p` | Path to Claude Code cli.js | auto-detect |
131
- | `--status`, `-s` | Show current patch status | false |
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
- | `/v1/event_logging/batch` | POST | Event logging (no-op) |
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
- | `/` | GET | Server status |
156
- | `/usage` | GET | Copilot usage stats |
157
- | `/token` | GET | Current Copilot token |
158
- | `/health` | GET | Health check |
159
- | `/history` | GET | Request history Web UI (enabled by default) |
160
- | `/history/api/*` | GET/DELETE | History API endpoints |
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
- ## Using with Claude Code
219
+ ## Account Types
163
220
 
164
- Create `.claude/settings.json` in your project:
221
+ The account type determines the Copilot API base URL:
165
222
 
166
- ```json
167
- {
168
- "env": {
169
- "ANTHROPIC_BASE_URL": "http://localhost:4141",
170
- "ANTHROPIC_AUTH_TOKEN": "dummy",
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
- ## Upstream Project
229
+ ## License
189
230
 
190
- For the original project documentation, features, and updates, see: [ericc-ch/copilot-api](https://github.com/ericc-ch/copilot-api)
231
+ [MIT](LICENSE)