billion-context 0.1.23 → 0.1.24
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 +95 -175
- package/README.zh-CN.md +70 -125
- package/dist/index.js +311 -137
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.zh-CN.md
CHANGED
|
@@ -42,134 +42,85 @@ npm install -g billion-context
|
|
|
42
42
|
|
|
43
43
|
## 快速上手
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
两种方式 —— 任选其一:
|
|
46
|
+
|
|
47
|
+
- **零配置(最简单):** 在客户端 baseURL 前面加上代理地址 + `/bili/`。无需配置文件 —— context 窗口自动从 [models.dev](https://models.dev) registry 查询。`/bili/` 前缀还是个自检测信号:billion-context 的客户端扩展(billion-context-pi / opencode-acp)能在自己的 baseUrl 里认出它并自禁用,避免双层压缩。
|
|
48
|
+
- **显式 context 窗口覆盖:** 在配置文件(或网页)里按 URL 声明 context 窗口,用于 registry 不认识的端点,或想钉死一个精确值的场景。两种方式路由都是同一个 `/bili/` 前缀 —— 配置只改变代理用哪个 context 窗口。
|
|
49
|
+
|
|
46
50
|
压缩是自动注入的 —— 你只需配置路由,无需配置压缩本身。
|
|
47
51
|
|
|
48
|
-
###
|
|
52
|
+
### 方式 A —— 零配置(`/bili/` 前缀)
|
|
53
|
+
|
|
54
|
+
启动代理:
|
|
49
55
|
|
|
50
56
|
```bash
|
|
51
57
|
bili
|
|
52
58
|
```
|
|
53
59
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
点击[http://localhost:8787/__acp/](http://localhost:8787/__acp/) 添加你的模型
|
|
57
|
-
<img width="2908" height="1787" alt="image" src="https://github.com/user-attachments/assets/cacf4b64-e5c6-41f2-b270-fd2be02eab0c" />
|
|
58
|
-
|
|
59
|
-
### 第 2 步 —— 编辑配置文件
|
|
60
|
-
|
|
61
|
-
在网页上添加你的 provider,复制以下内容到配置:
|
|
62
|
-
<img width="2931" height="1519" alt="image" src="https://github.com/user-attachments/assets/c02278be-bc7a-4f14-8f58-0f2d83784d54" />
|
|
63
|
-
|
|
64
|
-
> 不想用网页?也可以直接手编 JSON 文件,见下文[手动配置文件](#手动配置文件)。
|
|
65
|
-
|
|
66
|
-
### 第 3 步 —— 把客户端指向代理
|
|
67
|
-
|
|
68
|
-
编辑客户端自己的配置文件,让它把请求发到
|
|
69
|
-
`http://localhost:8787/<provider>/...`(第 2 步声明的 provider 名作为路径
|
|
70
|
-
第一段)。把你的**真实** API key 也填进客户端配置 —— 代理原样透传。
|
|
71
|
-
|
|
72
|
-
#### Pi(billion-context-pi)
|
|
60
|
+
然后把客户端现有的 baseURL 前面加上 `http://localhost:8787/bili/` 就行。完整上游 URL 嵌在路径里,proxy 无需任何配置就知道转发到哪:
|
|
73
61
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
// 改之前:
|
|
78
|
-
"baseUrl": "https://open.bigmodel.cn/api/coding/paas/v4",
|
|
79
|
-
// 改之后(把 host 换成代理 + 你起的 provider 名):
|
|
80
|
-
"baseUrl": "http://localhost:8787/zhipu/api/coding/paas/v4",
|
|
62
|
+
```
|
|
63
|
+
客户端 baseURL 之前: https://api.openai.com/v1
|
|
64
|
+
客户端 baseURL 之后: http://localhost:8787/bili/https://api.openai.com/v1
|
|
81
65
|
```
|
|
82
66
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
| `api` 值 | `baseUrl` 应指向 |
|
|
86
|
-
|---|---|
|
|
87
|
-
| `openai-completions` | OpenAI 兼容端点(GLM/DeepSeek/OpenAI)→ `…/zhipu/...` |
|
|
88
|
-
| `anthropic-messages` | Anthropic 兼容端点 → `…/anthropic` |
|
|
89
|
-
|
|
90
|
-
> 如果你装了 `billion-context-pi` 扩展,用隔离的 agent 目录跑 Pi
|
|
91
|
-
> (`PI_CODING_AGENT_DIR=…`),免得客户端扩展和 proxy 双重压缩。
|
|
92
|
-
> `bili-test-pi` 脚本帮你做好了这层隔离。
|
|
93
|
-
|
|
94
|
-
#### OpenCode
|
|
67
|
+
就这样 —— 真实 API key 照常填在客户端配置里(proxy 原样透传)。context 窗口(gpt-5.1-codex=400K、glm-5.2=1M、claude-opus-4=200K ……)自动从 models.dev 查询。
|
|
95
68
|
|
|
96
|
-
|
|
69
|
+
#### 各客户端示例
|
|
97
70
|
|
|
71
|
+
**OpenCode** —— 编辑 `~/.config/opencode/opencode.json`,改 provider 的 `baseURL`:
|
|
98
72
|
```jsonc
|
|
99
|
-
//
|
|
73
|
+
// 之前:
|
|
100
74
|
"baseURL": "https://open.bigmodel.cn/api/coding/paas/v4"
|
|
101
|
-
//
|
|
102
|
-
"baseURL": "http://localhost:8787/
|
|
75
|
+
// 之后(前面加上代理地址 + /bili/):
|
|
76
|
+
"baseURL": "http://localhost:8787/bili/https://open.bigmodel.cn/api/coding/paas/v4"
|
|
103
77
|
```
|
|
104
78
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
#### Codex
|
|
108
|
-
|
|
109
|
-
打开 `~/.codex/config.toml`,把现有 provider 的 **`base_url` 这一行**改成指向代理:
|
|
110
|
-
|
|
79
|
+
**Codex** —— 编辑 `~/.codex/config.toml`,改 provider 的 `base_url`:
|
|
111
80
|
```toml
|
|
112
|
-
#
|
|
113
|
-
base_url = "https://
|
|
114
|
-
#
|
|
115
|
-
base_url = "http://localhost:8787/
|
|
81
|
+
# 之前:
|
|
82
|
+
base_url = "https://api.openai.com/v1"
|
|
83
|
+
# 之后:
|
|
84
|
+
base_url = "http://localhost:8787/bili/https://api.openai.com/v1"
|
|
116
85
|
```
|
|
117
86
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
87
|
+
**Pi** —— 编辑 `~/.pi/agent/models.json`,改 provider 的 `baseUrl`:
|
|
88
|
+
```jsonc
|
|
89
|
+
// 之前:
|
|
90
|
+
"baseUrl": "https://api.anthropic.com"
|
|
91
|
+
// 之后:
|
|
92
|
+
"baseUrl": "http://localhost:8787/bili/https://api.anthropic.com"
|
|
93
|
+
```
|
|
125
94
|
|
|
126
|
-
|
|
127
|
-
三种协议 —— 如果你的客户端用别的协议或非标准 auth header,还用不了。
|
|
95
|
+
**其他客户端(Cursor / Aider / Continue ……)** —— 只要配置了上游 URL,前面加 `http://localhost:8787/bili/` 就行,其他都不用改。
|
|
128
96
|
|
|
129
|
-
### 手动配置文件
|
|
130
97
|
|
|
131
|
-
|
|
132
|
-
自动化部署,也可以直接手编 JSON 文件,效果完全一样。
|
|
98
|
+
### 方式 B 手动配置文件&设置上下文大小
|
|
133
99
|
|
|
134
100
|
打开 `~/.config/billion-context/billion-context.json`,编辑 `providers` 块。
|
|
135
|
-
|
|
136
|
-
|
|
101
|
+
**key 就是上游 URL** —— 客户端写在 `/bili/` 后面的那个字符串。value 为该
|
|
102
|
+
URL 声明按模型的 context 窗口:
|
|
137
103
|
|
|
138
104
|
```json
|
|
139
105
|
{
|
|
140
106
|
"providers": {
|
|
141
|
-
"
|
|
142
|
-
"
|
|
143
|
-
"models": {
|
|
144
|
-
"glm-5.2": { "context": 1000000, "output": 131072 }
|
|
145
|
-
}
|
|
107
|
+
"https://open.bigmodel.cn/api/coding/paas/v4": {
|
|
108
|
+
"models": { "glm-5.2": { "context": 1000000 } }
|
|
146
109
|
},
|
|
147
|
-
"
|
|
110
|
+
"https://api.anthropic.com": {}
|
|
148
111
|
}
|
|
149
112
|
}
|
|
150
113
|
```
|
|
151
114
|
|
|
152
|
-
-
|
|
153
|
-
-
|
|
115
|
+
- 一个 key 在客户端嵌入的 URL 等于它或以它开头时匹配(最长 key 优先)。纯 host key 覆盖该 host 上的所有路径。
|
|
116
|
+
- 空 value `{}` 表示"这个 URL 存在,无覆盖"(context 窗口来自 models.dev / 前缀表)。
|
|
117
|
+
- 删掉你不用的条目;添加其他的(按需)。
|
|
154
118
|
- API key **不**写在这里 —— key 在客户端那边,代理原样透传。
|
|
155
119
|
|
|
156
|
-
保存后**重启 `bili`**。启动行列出你的路由:
|
|
157
|
-
|
|
158
|
-
```
|
|
159
|
-
acp-proxy listening on http://127.0.0.1:8787 — routes: anthropic=https://api.anthropic.com, zhipu=https://open.bigmodel.cn
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
这证明代理读到了你的配置。(完整 schema —— 按模型的 context 窗口、可选字段 —— 见[配置](#配置)。)
|
|
163
|
-
|
|
164
|
-
### 网页配置
|
|
165
120
|
|
|
166
|
-
|
|
121
|
+
### 方式C 网页配置&设置上下文大小
|
|
167
122
|
|
|
168
|
-
|
|
169
|
-
- **生成客户端 URL** —— 选一个 provider,得到可直接复制的配置片段(Pi / OpenCode / Codex 的 `baseUrl`/`baseURL`/`base_url` 一行,已填好代理地址 + provider 名)。
|
|
170
|
-
- **查看会话** —— 实时会话表(请求数、省的 token、最后活跃时间),自动刷新。
|
|
171
|
-
|
|
172
|
-
改完 providers 需要**重启 bili** 才生效(UI 会提醒你)。
|
|
123
|
+
打开 [http://localhost:8787/__bili/](http://localhost:8787/__bili/) 进行配置。
|
|
173
124
|
|
|
174
125
|
### 验证
|
|
175
126
|
|
|
@@ -177,11 +128,11 @@ acp-proxy listening on http://127.0.0.1:8787 — routes: anthropic=https://api.a
|
|
|
177
128
|
|
|
178
129
|
```bash
|
|
179
130
|
# 健康检查(代理是否在跑 + 转发到哪)
|
|
180
|
-
curl -s http://localhost:8787/
|
|
131
|
+
curl -s http://localhost:8787/__bili/health
|
|
181
132
|
# → {"ok":true,"upstream":"https://api.anthropic.com"}
|
|
182
133
|
|
|
183
134
|
# 实时会话统计(发过真实请求后)
|
|
184
|
-
curl -s http://localhost:8787/
|
|
135
|
+
curl -s http://localhost:8787/__bili/stats
|
|
185
136
|
```
|
|
186
137
|
|
|
187
138
|
然后从助手发一条消息,观察日志(`~/.local/state/billion-context/bili.log`,
|
|
@@ -279,15 +230,13 @@ bili --no-auto-update # 本次启动禁用自动更新
|
|
|
279
230
|
"port": 8787,
|
|
280
231
|
"host": "127.0.0.1",
|
|
281
232
|
"providers": {
|
|
282
|
-
"
|
|
283
|
-
"url": "https://open.bigmodel.cn",
|
|
233
|
+
"https://open.bigmodel.cn/api/coding/paas/v4": {
|
|
284
234
|
"models": {
|
|
285
|
-
"glm-5.2": { "context": 1000000
|
|
286
|
-
"glm-5.1": { "context": 200000
|
|
235
|
+
"glm-5.2": { "context": 1000000 },
|
|
236
|
+
"glm-5.1": { "context": 200000 }
|
|
287
237
|
}
|
|
288
238
|
},
|
|
289
|
-
"
|
|
290
|
-
"deepseek": "https://api.deepseek.com"
|
|
239
|
+
"https://api.deepseek.com": {}
|
|
291
240
|
}
|
|
292
241
|
}
|
|
293
242
|
```
|
|
@@ -298,12 +247,11 @@ bili --no-auto-update # 本次启动禁用自动更新
|
|
|
298
247
|
|------|---------|-------------|
|
|
299
248
|
| `port` | `8787` | 代理监听端口 |
|
|
300
249
|
| `host` | `127.0.0.1` | 代理监听地址 |
|
|
301
|
-
| `upstream` | `https://api.anthropic.com` | 无路由匹配时的默认上游 |
|
|
302
250
|
| `sessionHeader` | `x-acp-session` | 客户端可发来标识会话的 header 名 |
|
|
303
251
|
| `log` | `true` | 启用请求日志 |
|
|
304
252
|
| `debug` | `false` | 详细日志(等同 `ACP_DEBUG=1`) |
|
|
305
253
|
| `passthrough` | `false` | 不压缩直接转发(等同 `ACP_PASSTHROUGH=1`) |
|
|
306
|
-
| `providers` | *(无)* |
|
|
254
|
+
| `providers` | *(无)* | 按 URL 的 context 覆盖 —— 见下文 |
|
|
307
255
|
| `compress` | *(见默认值)* | `{ injectTool, injectNudge }` |
|
|
308
256
|
|
|
309
257
|
> **选择 `host`**(IPv6 / 容器):默认 `127.0.0.1` 只听 IPv4 且仅
|
|
@@ -314,41 +262,38 @@ bili --no-auto-update # 本次启动禁用自动更新
|
|
|
314
262
|
> `--host 0.0.0.0`。⚠️ `0.0.0.0` / `::` 会把代理暴露到**所有**网卡;
|
|
315
263
|
> 确保你在可信网络或防火墙后面。
|
|
316
264
|
|
|
317
|
-
### Providers(URL
|
|
265
|
+
### Providers(按 URL 的 context 覆盖)
|
|
318
266
|
|
|
319
|
-
|
|
267
|
+
路由始终是 `/bili/` 前缀(见[方式 A](#方式-a-零配置bili-前缀))。
|
|
268
|
+
`providers` 块只声明**按 URL 的 context 窗口覆盖**,以上游 URL 为 key。
|
|
269
|
+
key 就是客户端写在 `/bili/` 后面的那个字符串:
|
|
320
270
|
|
|
321
|
-
**简单形式** —— provider 名 → URL:
|
|
322
|
-
```json
|
|
323
|
-
{ "deepseek": "https://api.deepseek.com" }
|
|
324
|
-
```
|
|
325
|
-
|
|
326
|
-
**完整形式** —— provider 名 → `{ url, models }`:
|
|
327
271
|
```json
|
|
328
272
|
{
|
|
329
|
-
"
|
|
330
|
-
"
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
273
|
+
"providers": {
|
|
274
|
+
"https://open.bigmodel.cn/api/coding/paas/v4": {
|
|
275
|
+
"models": {
|
|
276
|
+
"glm-5.2": { "context": 1000000 },
|
|
277
|
+
"glm-5.1": { "context": 200000 }
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
"https://api.deepseek.com": {}
|
|
335
281
|
}
|
|
336
282
|
}
|
|
337
283
|
```
|
|
338
284
|
|
|
339
|
-
|
|
285
|
+
同一个模型在不同上游后面可以有不同 context 窗口(例如 relay 把模型包成更大窗口)。`context` 是**输入 context 上限**(压缩器用它判断何时 nudge)。可选;缺失值回退到 [models.dev](https://models.dev) registry,再回退到内置前缀表。
|
|
340
286
|
|
|
341
|
-
> **为什么要声明 context?** LLM 的 `/models` API **不返回** context 窗口(已跨 OpenAI、Anthropic、智谱、comfly 验证)。它们是文档级信息。值错了(例如把 GLM-5.2 猜成 128K 而非 1M)会导致频繁误触发压缩。按
|
|
287
|
+
> **为什么要声明 context?** LLM 的 `/models` API **不返回** context 窗口(已跨 OpenAI、Anthropic、智谱、comfly 验证)。它们是文档级信息。值错了(例如把 GLM-5.2 猜成 128K 而非 1M)会导致频繁误触发压缩。按 URL + 模型声明能让代理匹配客户端自己用的注册表。
|
|
342
288
|
|
|
343
|
-
|
|
289
|
+
### URL key 匹配规则
|
|
344
290
|
|
|
345
|
-
|
|
291
|
+
- 一个请求在客户端嵌入的 URL **等于 key 或以 key 开头**时匹配(最长 key 优先)。
|
|
292
|
+
- 浅 key 如 `https://open.bigmodel.cn` 覆盖该 host 上的每条路径;深 key 如 `https://open.bigmodel.cn/api/anthropic` 只覆盖那一个端点。
|
|
293
|
+
- key 永不跨 host(边界检查要求 key 后面是 `/` 或字符串结尾),所以 `https://x.com` 不会匹配 `https://x.com.evil`。
|
|
294
|
+
- 未被任何匹配 key 覆盖的模型回退到 models.dev,再回退到前缀表,最后回退到 `modelContextLimit`。
|
|
346
295
|
|
|
347
|
-
|
|
348
|
-
- 保留字(`v1`、`chat`、`completions`、`messages`、`models`、`api`)被拒绝,以免与真实 API 路径段冲突。
|
|
349
|
-
- provider 名可出现在路径任意位置;最长匹配优先。
|
|
350
|
-
- **未声明任何 providers**(比如你清空了 `providers` 块)时,每个请求按完整
|
|
351
|
-
原始路径转发到默认 `upstream` —— 边缘场景,非正常流程。
|
|
296
|
+
**API key 永远不存进代理** —— 助手发什么 key,原样透传给上游。
|
|
352
297
|
|
|
353
298
|
## 会话机制
|
|
354
299
|
|
|
@@ -370,7 +315,7 @@ bili --no-auto-update # 本次启动禁用自动更新
|
|
|
370
315
|
|
|
371
316
|
## 状态
|
|
372
317
|
|
|
373
|
-
早期。协议处理和压缩已通过 mock 测试(
|
|
318
|
+
早期。协议处理和压缩已通过 mock 测试(146 项通过)。真实模型集成测试是下一里程碑。预期会有粗糙的地方。
|
|
374
319
|
|
|
375
320
|
pi 扩展模式(进程内、更紧密集成、参考实现)见 [billion-context-pi](https://github.com/ranxianglei/billion-context-pi)。
|
|
376
321
|
|