@hsupu/copilot-api 0.7.17-beta.0 → 0.7.18-beta

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
@@ -1,183 +1,229 @@
1
1
  # Copilot API Proxy (Fork)
2
2
 
3
- > [!WARNING]
4
- > 这是 GitHub Copilot API 的逆向代理。GitHub 官方不提供支持,且可能随时失效。使用风险自负。
5
-
6
- 本项目是 [ericc-ch/copilot-api](https://github.com/ericc-ch/copilot-api) 的 fork,因使用中遇到报错,故尝试魔改。
3
+ > [!NOTE]
4
+ > This is a fork of [ericc-ch/copilot-api](https://github.com/ericc-ch/copilot-api) with additional improvements and bug fixes.
7
5
 
8
- ## Fork 的考虑和改进
6
+ > [!WARNING]
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.
9
8
 
10
- CC + Opus/Haiku 是作者的唯一核心场景。其他任何花哨的功能要么是继承自上游,要么是 fallback 方案。改动全 AI 完成,我只提供功能需求、参考项目和使用反馈。
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.
11
10
 
12
- fork 相对于上游项目包含以下增强:
11
+ ## Quick Start
13
12
 
14
- ### 新功能
13
+ ### Install from npm (Recommended)
15
14
 
16
- - **`--host` 选项**:将服务器绑定到指定网络接口(如 `--host 0.0.0.0` 绑定所有接口,`--host 127.0.0.1` 仅绑定本地)
17
- - **自适应限流**:基于指数退避的智能限流,支持自动恢复和 Retry-After 头(替代原有的队列限流)
18
- - **直连 Anthropic API**:Claude 模型使用 Copilot 的原生 Anthropic 端点,无需格式转换
19
- - **智能自动截断**:超出上下文限制时自动截断对话历史,支持可选的 tool result 压缩
20
- - **`/api/event_logging/batch` 端点**:为 Anthropic SDK 的 event logging 提供兼容端点(返回 OK,不做处理)
21
- - **`logout` 命令**:通过 `copilot-api logout` 移除已存储的 GitHub token
22
- - **`list-claude-code` 命令**:列出所有本地安装的 Claude Code 版本
23
- - **工具名长度处理**:自动截断超过 64 字符的工具名以符合 OpenAI 限制,使用 hash 后缀避免冲突。响应中会还原原始名称。
24
- - **请求历史 UI**:内置 Web UI(默认启用),可查看、搜索、过滤和导出所有 API 请求/响应。访问 `/history`。
25
- - **`setup-claude-code` 命令**:交互式配置 Claude Code 以使用本代理
26
- - **Sonnet 重定向到 Opus**:可选将 sonnet 模型请求重定向到最佳可用 opus 模型
27
- - **安全研究模式**:为授权渗透测试、CTF 和安全教育提供专用模式
15
+ ```sh
16
+ npx -y @hsupu/copilot-api start
17
+ ```
28
18
 
29
- ### Bug 修复
19
+ ### Run from Source
30
20
 
31
- - **修复流式响应中缺少 `model` 字段**:Copilot API 的首个流式 chunk 有时 `choices` 数组为空但包含模型名。现已保存该值供后续事件使用。
32
- - **自动修复消息序列错误**:当 tool 调用被中断(如用户取消)时,API 会自动添加占位 `tool_result` 块以维持合法的消息序列
33
- - **修复 `bunx` 符号链接问题**:将 pre-commit hook 改为使用 `bun x` 替代 `bunx` 以提高兼容性
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
+ ```
34
35
 
35
- ## 快速开始
36
+ ## Using with Claude Code
36
37
 
37
- ### npm 安装(推荐)
38
+ Run the interactive setup command:
38
39
 
39
40
  ```sh
40
- # 直接用 npx 运行
41
- npx @hsupu/copilot-api start
41
+ copilot-api setup-claude-code
42
+ ```
43
+
44
+ Or manually create `~/.claude/settings.json`:
42
45
 
43
- # 或全局安装
44
- npm install -g @hsupu/copilot-api
45
- copilot-api start
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
+ }
46
57
  ```
47
58
 
48
- ### 从 GitHub 安装
59
+ ## Features
49
60
 
50
- 也可以直接从 GitHub 安装(需要构建步骤):
61
+ ### Dual API Compatibility
51
62
 
52
- ```sh
53
- npm install -g github:puxu-msft/copilot-api-js
54
- copilot-api start
55
- ```
63
+ Exposes both OpenAI and Anthropic compatible endpoints through a single proxy:
56
64
 
57
- ### 从源码运行
65
+ - **Direct Anthropic path** — Uses Copilot API's native Anthropic endpoint for Claude models
66
+ - **Translated path** — Translates between OpenAI and Anthropic formats for other models
58
67
 
59
- ```sh
60
- # 克隆仓库
61
- git clone https://github.com/puxu-msft/copilot-api-js.git
62
- cd copilot-api-js
68
+ ### Auto-Truncate
63
69
 
64
- # 安装依赖
65
- bun install
70
+ Automatically handles context length limits (enabled by default):
66
71
 
67
- # 开发模式(热重载)
68
- 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
69
76
 
70
- # 生产模式
71
- bun run start
77
+ ### Message Sanitization
72
78
 
73
- # 构建发布版
74
- bun run build
75
- ```
79
+ Cleans up messages before forwarding to the API:
76
80
 
77
- ### 构建后使用
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`)
78
87
 
79
- ```sh
80
- # 本地运行构建版本
81
- npx .
88
+ ### Model Name Translation
82
89
 
83
- # 或全局链接
84
- bun link
85
- copilot-api start
86
- ```
90
+ Translates client-sent model names to matching Copilot models:
87
91
 
88
- ## 命令参考
89
-
90
- | 命令 | 说明 |
91
- |------|------|
92
- | `start` | 启动 API 服务器(需要时自动进行认证) |
93
- | `auth` | 仅运行 GitHub 认证流程 |
94
- | `logout` | 移除已存储的 GitHub token |
95
- | `check-usage` | 查看 Copilot 用量和配额 |
96
- | `debug` | 显示诊断信息 |
97
- | `list-claude-code` | 列出所有本地安装的 Claude Code 版本 |
98
- | `setup-claude-code` | 交互式配置 Claude Code 连接 |
99
-
100
- ### start 命令选项
101
-
102
- | 选项 | 说明 | 默认值 |
103
- |------|------|--------|
104
- | `--port`, `-p` | 监听端口 | 4141 |
105
- | `--host`, `-H` | 绑定的主机/接口 | (所有接口) |
106
- | `--verbose`, `-v` | 启用详细日志 | false |
107
- | `--account-type`, `-a` | 账户类型(individual, business, enterprise) | individual |
108
- | `--manual` | 手动请求审批模式 | false |
109
- | `--no-rate-limit` | 禁用自适应限流 | false |
110
- | `--retry-interval` | 限流后重试等待秒数 | 10 |
111
- | `--request-interval` | 限流模式下请求间隔秒数 | 10 |
112
- | `--recovery-timeout` | 尝试恢复前等待的分钟数 | 10 |
113
- | `--consecutive-successes` | 退出限流模式所需的连续成功次数 | 5 |
114
- | `--github-token`, `-g` | 直接提供 GitHub token | 无 |
115
- | `--show-github-token` | 在日志中显示 GitHub token | false |
116
- | `--proxy-env` | 使用环境变量中的代理 | false |
117
- | `--history-limit` | 内存中最大历史记录条数(0 = 无限) | 200 |
118
- | `--no-auto-truncate` | 禁用超限自动截断 | false |
119
- | `--no-compress-tool-results` | 禁用自动截断时的 tool result 压缩 | false(默认启用压缩) |
120
- | `--redirect-anthropic` | 强制 Anthropic 请求走 OpenAI 转换 | false |
121
- | `--no-rewrite-anthropic-tools` | 不重写服务端工具 | false |
122
- | `--redirect-count-tokens` | count_tokens 走 OpenAI 转换而非原生 Anthropic | false |
123
- | `--redirect-sonnet-to-opus` | 将 sonnet 模型请求重定向到最佳可用 opus 模型 | false |
124
- | `--security-research-mode` | 启用安全研究模式(需要口令) | 无 |
125
-
126
- ## API 端点
127
-
128
- ### OpenAI 兼容
129
-
130
- | 端点 | 方法 | 说明 |
131
- |------|------|------|
132
- | `/v1/chat/completions` | POST | Chat completions |
133
- | `/v1/models` | GET | 列出可用模型 |
134
- | `/v1/embeddings` | POST | 文本嵌入 |
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-5-20250514` | `claude-sonnet-4.5` |
97
+ | `claude-sonnet-4`, `gpt-4` | Passed through directly |
135
98
 
136
- ### Anthropic 兼容
99
+ User-configured `model_overrides` (via config.yaml) can redirect any model name to another, with chained resolution and family-level overrides.
137
100
 
138
- | 端点 | 方法 | 说明 |
139
- |------|------|------|
140
- | `/v1/messages` | POST | Messages API |
141
- | `/v1/messages/count_tokens` | POST | Token 计数 |
142
- | `/api/event_logging/batch` | POST | Event logging(空操作) |
101
+ ### Server-Side Tools
143
102
 
144
- ### 工具端点
103
+ 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`).
145
104
 
146
- | 端点 | 方法 | 说明 |
147
- |------|------|------|
148
- | `/` | GET | 服务器状态 |
149
- | `/usage` | GET | Copilot 用量统计 |
150
- | `/token` | GET | 当前 Copilot token |
151
- | `/health` | GET | 健康检查 |
152
- | `/history` | GET | 请求历史 Web UI(默认启用) |
153
- | `/history/api/*` | GET/DELETE | 历史记录 API 端点 |
105
+ ### Request History UI
154
106
 
155
- ## 配合 Claude Code 使用
107
+ Built-in web interface for inspecting API requests and responses. Access at `http://localhost:4141/history/v3/`.
156
108
 
157
- 在用户根目录或者打开的项目中创建 `.claude/settings.json`:
109
+ - Real-time updates via WebSocket
110
+ - Filter by model, endpoint, status, and time range
111
+ - Session tracking and statistics
158
112
 
159
- ```json
160
- {
161
- "env": {
162
- "ANTHROPIC_BASE_URL": "http://localhost:4141",
163
- "ANTHROPIC_AUTH_TOKEN": "dummy",
164
- "ANTHROPIC_MODEL": "gpt-4.1",
165
- "ANTHROPIC_SMALL_FAST_MODEL": "gpt-4.1",
166
- "DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1",
167
- "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1"
168
- },
169
- "permissions": {
170
- "deny": ["WebSearch"]
171
- }
172
- }
173
- ```
113
+ ### Additional Features
174
114
 
175
- 或使用交互式设置:
115
+ - **Model overrides** — Configure arbitrary model name redirections via config.yaml
116
+ - **Adaptive rate limiting** — Intelligent rate limiting with exponential backoff (3 modes: Normal, Rate-limited, Recovering)
117
+ - **Tool name truncation** — Truncates tool names exceeding 64 characters (OpenAI limit) with hash suffixes
118
+ - **Health checks** — Container-ready endpoint at `/health`
119
+ - **Graceful shutdown** — Connection draining on shutdown signals
120
+ - **Proxy support** — HTTP/HTTPS proxy via environment variables
176
121
 
177
- ```sh
178
- copilot-api setup-claude-code
179
- ```
122
+ ## Commands
123
+
124
+ | Command | Description |
125
+ |---------|-------------|
126
+ | `start` | Start the API server (authenticates automatically if needed) |
127
+ | `auth` | Run GitHub authentication flow only |
128
+ | `logout` | Remove stored GitHub token |
129
+ | `check-usage` | Show Copilot usage and quota information |
130
+ | `debug info` | Display diagnostic information |
131
+ | `debug models` | Fetch and display raw model data from Copilot API |
132
+ | `list-claude-code` | List all locally installed Claude Code versions |
133
+ | `setup-claude-code` | Interactively configure Claude Code to use this proxy |
134
+
135
+ ### `start` Options
136
+
137
+ **General:**
138
+
139
+ | Option | Default | Description |
140
+ |--------|---------|-------------|
141
+ | `--port`, `-p` | 4141 | Port to listen on |
142
+ | `--host`, `-H` | (all interfaces) | Host/interface to bind to |
143
+ | `--verbose`, `-v` | false | Enable verbose logging |
144
+ | `--account-type`, `-a` | individual | Account type: `individual`, `business`, or `enterprise` |
145
+ | `--github-token`, `-g` | | Provide GitHub token directly |
146
+ | `--no-http-proxy-from-env` | enabled | Disable HTTP proxy from environment variables |
147
+
148
+ **Auto-Truncate:**
149
+
150
+ | Option | Default | Description |
151
+ |--------|---------|-------------|
152
+ | `--no-auto-truncate` | enabled | Disable auto-truncation on context limit errors |
153
+
154
+ **Anthropic-Specific (via config.yaml):**
155
+
156
+ These options are configured in `config.yaml` under the `anthropic:` section. See [`config.example.yaml`](config.example.yaml).
157
+
158
+ | Config Key | Default | Description |
159
+ |------------|---------|-------------|
160
+ | `anthropic.rewrite_tools` | true | Rewrite server-side tools to custom format |
161
+ | `stream_idle_timeout` | 300 | Max seconds between SSE events (0 = no timeout) |
162
+
163
+ **Sanitization:**
164
+
165
+ | Option | Default | Description |
166
+ |--------|---------|-------------|
167
+ | `--collect-system-prompts` | false | Collect system prompts to file |
168
+
169
+ **Rate Limiting:**
170
+
171
+ | Option | Default | Description |
172
+ |--------|---------|-------------|
173
+ | `--no-rate-limit` | enabled | Disable adaptive rate limiting |
174
+
175
+ Rate limiter sub-parameters are configured in `config.yaml` under `rate_limiter:`. See [`config.example.yaml`](config.example.yaml).
176
+
177
+ ## Configuration
178
+
179
+ Create a `config.yaml` in the working directory. See [`config.example.yaml`](config.example.yaml) for all available options.
180
+
181
+ ## API Endpoints
182
+
183
+ ### OpenAI Compatible
184
+
185
+ | Endpoint | Method | Description |
186
+ |----------|--------|-------------|
187
+ | `/v1/chat/completions` | POST | Chat completions |
188
+ | `/v1/responses` | POST | Responses API |
189
+ | `/v1/models` | GET | List available models |
190
+ | `/v1/models/:model` | GET | Get specific model details |
191
+ | `/v1/embeddings` | POST | Text embeddings |
192
+
193
+ All endpoints also work without the `/v1` prefix.
194
+
195
+ ### Anthropic Compatible
196
+
197
+ | Endpoint | Method | Description |
198
+ |----------|--------|-------------|
199
+ | `/v1/messages` | POST | Messages API |
200
+ | `/v1/messages/count_tokens` | POST | Token counting |
201
+
202
+ ### Utility
203
+
204
+ | Endpoint | Method | Description |
205
+ |----------|--------|-------------|
206
+ | `/health` | GET | Health check (200 healthy, 503 unhealthy) |
207
+ | `/usage` | GET | Copilot usage and quota statistics |
208
+ | `/token` | GET | Current Copilot token information |
209
+ | `/history/v3/` | GET | History web UI |
210
+ | `/history/ws` | WebSocket | Real-time history updates |
211
+ | `/history/api/entries` | GET | Query history entries |
212
+ | `/history/api/entries/:id` | GET | Get single entry |
213
+ | `/history/api/summaries` | GET | Entry summaries |
214
+ | `/history/api/stats` | GET | Usage statistics |
215
+ | `/history/api/sessions` | GET | List sessions |
216
+
217
+ ## Account Types
218
+
219
+ The account type determines the Copilot API base URL:
220
+
221
+ | Type | API Base URL |
222
+ |------|-------------|
223
+ | `individual` | `api.githubcopilot.com` |
224
+ | `business` | `api.business.githubcopilot.com` |
225
+ | `enterprise` | `api.enterprise.githubcopilot.com` |
180
226
 
181
- ## 上游项目
227
+ ## License
182
228
 
183
- 原始项目的文档、功能和更新请参见:[ericc-ch/copilot-api](https://github.com/ericc-ch/copilot-api)
229
+ [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) |