@hsupu/copilot-api 0.8.0 → 0.8.1-beta.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/README.md CHANGED
@@ -4,111 +4,180 @@
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's API as standard OpenAI and Anthropic compatible endpoints. Works with Claude Code, Cursor, and other tools that speak these 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
- - **Queue-based rate limiting**: Requests are queued and processed sequentially with configurable delays, instead of being rejected when rate limited
17
- - **`/v1/event_logging/batch` endpoint**: Compatibility endpoint for Anthropic SDK's event logging (returns OK without processing)
18
- - **`logout` command**: Remove stored GitHub token with `copilot-api logout`
19
- - **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.
20
- - **Request History UI**: Optional built-in Web UI (`--history`) to view, search, filter, and export all API requests/responses. Access at `/history` when enabled.
11
+ ## Quick Start
21
12
 
22
- ### Bug Fixes
13
+ ### Install from npm (Recommended)
23
14
 
24
- - **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.
25
- - **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
26
- - **Fixed `bunx` symlink issue**: Changed pre-commit hook to use `bun x` instead of `bunx` for better compatibility
15
+ ```sh
16
+ npx -y @hsupu/copilot-api start
17
+ ```
27
18
 
28
- ### Documentation
19
+ ### Run from Source
29
20
 
30
- - Added [CLAUDE.md](./CLAUDE.md) with project architecture documentation
21
+ ```sh
22
+ git clone https://github.com/puxu-msft/copilot-api-js.git
23
+ cd copilot-api-js
24
+ bun install
25
+ bun run dev # Development mode with hot reload
26
+ bun run start # Production mode
27
+ bun run build # Build for distribution
28
+
29
+ # Testing
30
+ bun test # Backend unit tests
31
+ bun run test:all # All backend tests
32
+ bun run test:ui # Frontend (History UI) tests
33
+ bun run typecheck # TypeScript type checking
34
+ ```
31
35
 
32
- ## Quick Start
36
+ ## Using with Claude Code
33
37
 
34
- ### Install from npm (Recommended)
38
+ Run the interactive setup command:
35
39
 
36
40
  ```sh
37
- # Run directly with npx
38
- npx @hsupu/copilot-api start
41
+ copilot-api setup-claude-code
42
+ ```
39
43
 
40
- # Or install globally
41
- npm install -g @hsupu/copilot-api
42
- copilot-api start
44
+ Or manually create `~/.claude/settings.json`:
45
+
46
+ ```json
47
+ {
48
+ "env": {
49
+ "ANTHROPIC_BASE_URL": "http://localhost:4141",
50
+ "ANTHROPIC_AUTH_TOKEN": "dummy",
51
+ "ANTHROPIC_MODEL": "opus",
52
+ "ANTHROPIC_SMALL_FAST_MODEL": "haiku",
53
+ "DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1",
54
+ "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1"
55
+ }
56
+ }
43
57
  ```
44
58
 
45
- ### Install from GitHub
59
+ ## Features
46
60
 
47
- You can also install directly from GitHub (requires build step):
61
+ ### Dual API Compatibility
48
62
 
49
- ```sh
50
- npm install -g github:puxu-msft/copilot-api-js
51
- copilot-api start
52
- ```
63
+ Exposes both OpenAI and Anthropic compatible endpoints through a single proxy:
53
64
 
54
- ### Running from Source
65
+ - **Direct Anthropic path** — Uses Copilot API's native Anthropic endpoint for Claude models
66
+ - **OpenAI-compatible path** — Forwards OpenAI Chat Completions, Responses, Embeddings, and Models requests to Copilot's OpenAI endpoints
55
67
 
56
- ```sh
57
- # Clone the repository
58
- git clone https://github.com/puxu-msft/copilot-api-js.git
59
- cd copilot-api-js
68
+ ### Auto-Truncate
60
69
 
61
- # Install dependencies
62
- bun install
70
+ Automatically handles context length limits (enabled by default):
63
71
 
64
- # Development mode (with hot reload)
65
- bun run dev
72
+ - **Reactive** Retries failed requests with a truncated payload when hitting token or byte limits
73
+ - **Proactive** — Pre-checks requests against known model limits before sending
74
+ - **Dynamic limit learning** — Adjusts limits based on actual API error responses
75
+ - **Tool result compression** — Compresses old `tool_result` content before truncating messages
66
76
 
67
- # Production mode
68
- bun run start
77
+ ### Message Sanitization
69
78
 
70
- # Build for distribution
71
- bun run build
72
- ```
79
+ Cleans up messages before forwarding to the API:
73
80
 
74
- ### After Building
81
+ - Filters orphaned `tool_use` / `tool_result` blocks (unpaired due to interrupted tool calls or truncation)
82
+ - Fixes tool name casing mismatches
83
+ - Removes empty text content blocks
84
+ - Strips `<system-reminder>` tags from message content
85
+ - **[Optional]** Deduplicates repeated tool calls (`config.yaml: anthropic.dedup_tool_calls`)
86
+ - **[Optional]** Strips system-reminder tags from Read tool results (`config.yaml: anthropic.truncate_read_tool_result`)
75
87
 
76
- ```sh
77
- # Run the built version locally
78
- npx .
88
+ ### Model Name Translation
79
89
 
80
- # Or link globally
81
- bun link
82
- copilot-api start
83
- ```
90
+ Translates client-sent model names to matching Copilot models:
91
+
92
+ | Input | Resolved To |
93
+ |-------|-------------|
94
+ | `opus`, `sonnet`, `haiku` | Best available model in that family |
95
+ | `claude-opus-4-6` | `claude-opus-4.6` |
96
+ | `claude-sonnet-4-6-20250514` | `claude-sonnet-4.6` |
97
+ | `claude-opus-4-6-fast`, `opus[1m]` | `claude-opus-4.6-fast`, `claude-opus-4.6-1m` |
98
+ | `claude-sonnet-4`, `gpt-4` | Passed through directly |
99
+
100
+ User-configured `model_overrides` (via config.yaml) can redirect any model name to another, with chained resolution and family-level overrides.
101
+
102
+ ### Server-Side Tools
103
+
104
+ Supports Anthropic server-side tools (`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 (`--no-rewrite-anthropic-tools`).
105
+
106
+ ### Request History UI
107
+
108
+ Built-in web interface for inspecting API requests and responses. Access at `http://localhost:4141/history/v3/`.
84
109
 
85
- ## Command Reference
110
+ - Real-time updates via WebSocket
111
+ - Filter by model, endpoint, status, and time range
112
+ - Session tracking and statistics
113
+
114
+ ### Additional Features
115
+
116
+ - **Model overrides** — Configure arbitrary model name redirections via config.yaml
117
+ - **Adaptive rate limiting** — Intelligent rate limiting with exponential backoff (3 modes: Normal, Rate-limited, Recovering)
118
+ - **Tool name truncation** — Truncates tool names exceeding 64 characters (OpenAI limit) with hash suffixes
119
+ - **Health checks** — Container-ready endpoint at `/health`
120
+ - **Graceful shutdown** — Connection draining on shutdown signals
121
+ - **Proxy support** — HTTP/HTTPS proxy via environment variables
122
+
123
+ ## Commands
86
124
 
87
125
  | Command | Description |
88
126
  |---------|-------------|
89
- | `start` | Start the API server (handles auth if needed) |
127
+ | `start` | Start the API server (authenticates automatically if needed) |
90
128
  | `auth` | Run GitHub authentication flow only |
91
129
  | `logout` | Remove stored GitHub token |
92
- | `check-usage` | Show Copilot usage and quota |
93
- | `debug` | Display diagnostic information |
94
-
95
- ### Start Command Options
96
-
97
- | Option | Description | Default |
98
- |--------|-------------|---------|
99
- | `--port`, `-p` | Port to listen on | 4141 |
100
- | `--host` | Host/interface to bind to | (all interfaces) |
101
- | `--verbose`, `-v` | Enable verbose logging | false |
102
- | `--account-type`, `-a` | Account type (individual, business, enterprise) | individual |
103
- | `--rate-limit`, `-r` | Seconds between requests (uses queue) | none |
104
- | `--wait`, `-w` | Wait in queue instead of rejecting | false |
105
- | `--github-token`, `-g` | Provide GitHub token directly | none |
106
- | `--claude-code`, `-c` | Generate Claude Code launch command | false |
107
- | `--show-token` | Show tokens on fetch/refresh | false |
108
- | `--manual` | Manual request approval mode | false |
109
- | `--proxy-env` | Use proxy from environment | false |
110
- | `--history` | Enable request history UI at `/history` | false |
111
- | `--history-limit` | Max history entries in memory | 1000 |
130
+ | `check-usage` | Show Copilot usage and quota information |
131
+ | `debug info` | Display diagnostic information |
132
+ | `debug models` | Fetch and display raw model data from Copilot API |
133
+ | `list-claude-code` | List all locally installed Claude Code versions |
134
+ | `setup-claude-code` | Interactively configure Claude Code to use this proxy |
135
+
136
+ ### `start` Options
137
+
138
+ **General:**
139
+
140
+ | Option | Default | Description |
141
+ |--------|---------|-------------|
142
+ | `--port`, `-p` | 4141 | Port to listen on |
143
+ | `--host`, `-H` | (all interfaces) | Host/interface to bind to |
144
+ | `--verbose`, `-v` | false | Enable verbose logging |
145
+ | `--account-type`, `-a` | individual | Account type: `individual`, `business`, or `enterprise` |
146
+ | `--github-token`, `-g` | | Provide GitHub token directly |
147
+ | `--no-http-proxy-from-env` | enabled | Disable HTTP proxy from environment variables |
148
+
149
+ **Auto-Truncate:**
150
+
151
+ | Option | Default | Description |
152
+ |--------|---------|-------------|
153
+ | `--no-auto-truncate` | enabled | Disable auto-truncation on context limit errors |
154
+
155
+ **Anthropic-Specific (via config.yaml):**
156
+
157
+ These options are configured in `config.yaml` under the `anthropic:` section. See [`config.example.yaml`](config.example.yaml).
158
+
159
+ | Config Key | Default | Description |
160
+ |------------|---------|-------------|
161
+ | `anthropic.rewrite_tools` | true | Rewrite server-side tools to custom format |
162
+ | `stream_idle_timeout` | 300 | Max seconds between SSE events (0 = no timeout) |
163
+
164
+ **Sanitization:**
165
+
166
+ | Option | Default | Description |
167
+ |--------|---------|-------------|
168
+ | `--collect-system-prompts` | false | Collect system prompts to file |
169
+
170
+ **Rate Limiting:**
171
+
172
+ | Option | Default | Description |
173
+ |--------|---------|-------------|
174
+ | `--no-rate-limit` | enabled | Disable adaptive rate limiting |
175
+
176
+ Rate limiter sub-parameters are configured in `config.yaml` under `rate_limiter:`. See [`config.example.yaml`](config.example.yaml).
177
+
178
+ ## Configuration
179
+
180
+ Create a `config.yaml` in the working directory. See [`config.example.yaml`](config.example.yaml) for all available options.
112
181
 
113
182
  ## API Endpoints
114
183
 
@@ -117,52 +186,45 @@ copilot-api start
117
186
  | Endpoint | Method | Description |
118
187
  |----------|--------|-------------|
119
188
  | `/v1/chat/completions` | POST | Chat completions |
189
+ | `/v1/responses` | POST | Responses API |
120
190
  | `/v1/models` | GET | List available models |
191
+ | `/v1/models/:model` | GET | Get specific model details |
121
192
  | `/v1/embeddings` | POST | Text embeddings |
122
193
 
194
+ All endpoints also work without the `/v1` prefix.
195
+
123
196
  ### Anthropic Compatible
124
197
 
125
198
  | Endpoint | Method | Description |
126
199
  |----------|--------|-------------|
127
200
  | `/v1/messages` | POST | Messages API |
128
201
  | `/v1/messages/count_tokens` | POST | Token counting |
129
- | `/v1/event_logging/batch` | POST | Event logging (no-op) |
130
202
 
131
203
  ### Utility
132
204
 
133
205
  | Endpoint | Method | Description |
134
206
  |----------|--------|-------------|
135
- | `/usage` | GET | Copilot usage stats |
136
- | `/token` | GET | Current Copilot token |
137
- | `/history` | GET | Request history Web UI (requires `--history`) |
138
- | `/history/api/*` | GET/DELETE | History API endpoints |
139
-
140
- ## Using with Claude Code
141
-
142
- Create `.claude/settings.json` in your project:
143
-
144
- ```json
145
- {
146
- "env": {
147
- "ANTHROPIC_BASE_URL": "http://localhost:4141",
148
- "ANTHROPIC_AUTH_TOKEN": "dummy",
149
- "ANTHROPIC_MODEL": "gpt-4.1",
150
- "ANTHROPIC_SMALL_FAST_MODEL": "gpt-4.1",
151
- "DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1",
152
- "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1"
153
- },
154
- "permissions": {
155
- "deny": ["WebSearch"]
156
- }
157
- }
158
- ```
159
-
160
- Or use the interactive setup:
161
-
162
- ```sh
163
- bun run start --claude-code
164
- ```
165
-
166
- ## Upstream Project
167
-
168
- For the original project documentation, features, and updates, see: [ericc-ch/copilot-api](https://github.com/ericc-ch/copilot-api)
207
+ | `/health` | GET | Health check (200 healthy, 503 unhealthy) |
208
+ | `/usage` | GET | Copilot usage and quota statistics |
209
+ | `/token` | GET | Current Copilot token information |
210
+ | `/history/v3/` | GET | History web UI |
211
+ | `/history/ws` | WebSocket | Real-time history updates |
212
+ | `/history/api/entries` | GET | Query history entries |
213
+ | `/history/api/entries/:id` | GET | Get single entry |
214
+ | `/history/api/summaries` | GET | Entry summaries |
215
+ | `/history/api/stats` | GET | Usage statistics |
216
+ | `/history/api/sessions` | GET | List sessions |
217
+
218
+ ## Account Types
219
+
220
+ The account type determines the Copilot API base URL:
221
+
222
+ | Type | API Base URL |
223
+ |------|-------------|
224
+ | `individual` | `api.githubcopilot.com` |
225
+ | `business` | `api.business.githubcopilot.com` |
226
+ | `enterprise` | `api.enterprise.githubcopilot.com` |
227
+
228
+ ## License
229
+
230
+ [MIT](LICENSE)
package/README.zh.md ADDED
@@ -0,0 +1,39 @@
1
+
2
+ ## 项目概述
3
+
4
+ GitHub Copilot API 的逆向代理,将其暴露为 OpenAI 和 Anthropic 兼容端点。使得 Claude Code 等工具可以使用 GitHub Copilot 作为后端。
5
+
6
+ ## 常用命令
7
+
8
+ ```sh
9
+ bun install # 安装依赖
10
+ bun run dev # 开发模式(热重载)
11
+ bun run start # 生产模式
12
+ bun run build # 构建发布版(tsdown)
13
+ bun run typecheck # 类型检查
14
+ bun run lint # Lint 暂存文件
15
+ bun run lint:all # Lint 所有文件
16
+ bun run knip # 查找未使用的导出/依赖
17
+ bun test # 运行所有测试
18
+ bun test tests/foo.test.ts # 运行单个测试文件
19
+ ```
20
+
21
+ ## API 端点
22
+
23
+ | 端点 | 用途 |
24
+ |------|------|
25
+ | `/v1/chat/completions` | OpenAI 兼容 chat |
26
+ | `/v1/messages` | Anthropic 兼容 messages |
27
+ | `/v1/messages/count_tokens` | Anthropic 兼容 token 计数 |
28
+ | `/v1/models` | 列出可用模型 |
29
+ | `/v1/embeddings` | 文本嵌入 |
30
+ | `/api/event_logging/batch` | Event logging(空操作) |
31
+ | `/usage` | Copilot 配额/用量统计 |
32
+ | `/health` | 健康检查 |
33
+ | `/token` | 当前 Copilot token 信息 |
34
+ | `/history` | 请求历史 Web UI(v1 和 v2) |
35
+ | `/history/ws` | WebSocket 实时历史更新 |
36
+ | `/history/api/entries` | 历史查询 API |
37
+ | `/history/api/sessions` | 会话列表 API |
38
+ | `/history/api/stats` | 统计 API |
39
+ | `/history/api/export` | 导出历史(JSON/CSV) |