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

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,167 +1,160 @@
1
1
  # Copilot API Proxy (Fork)
2
2
 
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.
5
-
6
3
  > [!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.
4
+ > 这是 GitHub Copilot API 的逆向代理。GitHub 官方不提供支持,且可能随时失效。使用风险自负。
8
5
 
9
- ## Fork Improvements
6
+ 本项目是 [ericc-ch/copilot-api](https://github.com/ericc-ch/copilot-api) 的 fork,因使用中遇到报错,故尝试魔改。
10
7
 
11
- This fork includes the following enhancements over the upstream project:
8
+ ## Fork 的考虑和改进
12
9
 
13
- ### New Features
10
+ CC + Opus/Haiku 是作者的唯一核心场景。其他任何花哨的功能要么是继承自上游,要么是 fallback 方案。改动全 AI 完成,我只提供功能需求、参考项目和使用反馈。
14
11
 
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`.
12
+ fork 相对于上游项目包含以下增强:
24
13
 
25
- ### Bug Fixes
14
+ ### 新功能
26
15
 
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
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 和安全教育提供专用模式
30
28
 
31
- ### Documentation
29
+ ### Bug 修复
32
30
 
33
- - Added [CLAUDE.md](./CLAUDE.md) with project architecture documentation
31
+ - **修复流式响应中缺少 `model` 字段**:Copilot API 的首个流式 chunk 有时 `choices` 数组为空但包含模型名。现已保存该值供后续事件使用。
32
+ - **自动修复消息序列错误**:当 tool 调用被中断(如用户取消)时,API 会自动添加占位 `tool_result` 块以维持合法的消息序列
33
+ - **修复 `bunx` 符号链接问题**:将 pre-commit hook 改为使用 `bun x` 替代 `bunx` 以提高兼容性
34
34
 
35
- ## Quick Start
35
+ ## 快速开始
36
36
 
37
- ### Install from npm (Recommended)
37
+ ### npm 安装(推荐)
38
38
 
39
39
  ```sh
40
- # Run directly with npx
40
+ # 直接用 npx 运行
41
41
  npx @hsupu/copilot-api start
42
42
 
43
- # Or install globally
43
+ # 或全局安装
44
44
  npm install -g @hsupu/copilot-api
45
45
  copilot-api start
46
46
  ```
47
47
 
48
- ### Install from GitHub
48
+ ### GitHub 安装
49
49
 
50
- You can also install directly from GitHub (requires build step):
50
+ 也可以直接从 GitHub 安装(需要构建步骤):
51
51
 
52
52
  ```sh
53
53
  npm install -g github:puxu-msft/copilot-api-js
54
54
  copilot-api start
55
55
  ```
56
56
 
57
- ### Running from Source
57
+ ### 从源码运行
58
58
 
59
59
  ```sh
60
- # Clone the repository
60
+ # 克隆仓库
61
61
  git clone https://github.com/puxu-msft/copilot-api-js.git
62
62
  cd copilot-api-js
63
63
 
64
- # Install dependencies
64
+ # 安装依赖
65
65
  bun install
66
66
 
67
- # Development mode (with hot reload)
67
+ # 开发模式(热重载)
68
68
  bun run dev
69
69
 
70
- # Production mode
70
+ # 生产模式
71
71
  bun run start
72
72
 
73
- # Build for distribution
73
+ # 构建发布版
74
74
  bun run build
75
75
  ```
76
76
 
77
- ### After Building
77
+ ### 构建后使用
78
78
 
79
79
  ```sh
80
- # Run the built version locally
80
+ # 本地运行构建版本
81
81
  npx .
82
82
 
83
- # Or link globally
83
+ # 或全局链接
84
84
  bun link
85
85
  copilot-api start
86
86
  ```
87
87
 
88
- ## Command Reference
89
-
90
- | Command | Description |
91
- |---------|-------------|
92
- | `start` | Start the API server (handles auth if needed) |
93
- | `auth` | Run GitHub authentication flow only |
94
- | `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 |
132
-
133
- ## API Endpoints
134
-
135
- ### OpenAI Compatible
136
-
137
- | Endpoint | Method | Description |
138
- |----------|--------|-------------|
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
+ |------|------|------|
139
132
  | `/v1/chat/completions` | POST | Chat completions |
140
- | `/v1/models` | GET | List available models |
141
- | `/v1/embeddings` | POST | Text embeddings |
133
+ | `/v1/models` | GET | 列出可用模型 |
134
+ | `/v1/embeddings` | POST | 文本嵌入 |
142
135
 
143
- ### Anthropic Compatible
136
+ ### Anthropic 兼容
144
137
 
145
- | Endpoint | Method | Description |
146
- |----------|--------|-------------|
138
+ | 端点 | 方法 | 说明 |
139
+ |------|------|------|
147
140
  | `/v1/messages` | POST | Messages API |
148
- | `/v1/messages/count_tokens` | POST | Token counting |
149
- | `/v1/event_logging/batch` | POST | Event logging (no-op) |
141
+ | `/v1/messages/count_tokens` | POST | Token 计数 |
142
+ | `/api/event_logging/batch` | POST | Event logging(空操作) |
150
143
 
151
- ### Utility
144
+ ### 工具端点
152
145
 
153
- | Endpoint | Method | Description |
154
- |----------|--------|-------------|
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 |
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 端点 |
161
154
 
162
- ## Using with Claude Code
155
+ ## 配合 Claude Code 使用
163
156
 
164
- Create `.claude/settings.json` in your project:
157
+ 在用户根目录或者打开的项目中创建 `.claude/settings.json`:
165
158
 
166
159
  ```json
167
160
  {
@@ -179,12 +172,12 @@ Create `.claude/settings.json` in your project:
179
172
  }
180
173
  ```
181
174
 
182
- Or use the interactive setup:
175
+ 或使用交互式设置:
183
176
 
184
177
  ```sh
185
- bun run start --claude-code
178
+ copilot-api setup-claude-code
186
179
  ```
187
180
 
188
- ## Upstream Project
181
+ ## 上游项目
189
182
 
190
- For the original project documentation, features, and updates, see: [ericc-ch/copilot-api](https://github.com/ericc-ch/copilot-api)
183
+ 原始项目的文档、功能和更新请参见:[ericc-ch/copilot-api](https://github.com/ericc-ch/copilot-api)