dsh-sensenova-pool 1.0.0 → 1.1.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/CHANGELOG.md +74 -0
- package/README.md +64 -7
- package/lib/catalog.d.ts +59 -5
- package/lib/catalog.js +71 -12
- package/lib/client.js +58 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.js +145 -12
- package/lib/keypool.d.ts +20 -0
- package/lib/keypool.js +55 -0
- package/lib/routes.d.ts +30 -1
- package/lib/routes.js +43 -2
- package/lib/shim.d.ts +24 -2
- package/lib/shim.js +31 -5
- package/package.json +4 -1
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# 更新日志
|
|
2
|
+
|
|
3
|
+
本文件记录本插件的所有重要变更,格式参考 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/),
|
|
4
|
+
版本号遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
|
|
5
|
+
|
|
6
|
+
## [1.1.0] - 2026-11-19
|
|
7
|
+
|
|
8
|
+
### 新增
|
|
9
|
+
|
|
10
|
+
- **端点选择:中国站 / 国际站**。设置卡片顶部新增「上游端点」区,一键在
|
|
11
|
+
`https://token.sensenova.cn/v1`(中国站,默认)与 `https://token.sensenova.ai/v1`(国际站)之间切换,
|
|
12
|
+
无需重启 DSH。所选端点随 key 一起存进凭据库,重启后仍然记得([#5](https://github.com/Phoeky/dsh-sensenova-pool/issues/5))。
|
|
13
|
+
- **国际站模型目录**:国际站的内置基线覆盖上游 `/v1/models` 列出的全部 5 个模型,
|
|
14
|
+
包括两个生图模型(`sensenova-u1-fast`、`sensenova-u1.5-lite`)。
|
|
15
|
+
- **按端点的限流额度**:本地 RPM 预限流按站分开计算(默认中国站 2、国际站 3),
|
|
16
|
+
配置项 `rpmLimit` 新增 `-1` 表示「按端点取默认值」(即现在的默认行为)。
|
|
17
|
+
- 新增配置项 `endpoint`(`cn` / `intl`),只决定**默认**端点;卡片里的选择优先。
|
|
18
|
+
- 设置卡片显示当前实际上游地址,并提示 baseUrl 是否被配置文件固定。
|
|
19
|
+
|
|
20
|
+
### 变更
|
|
21
|
+
|
|
22
|
+
- 切换端点会连带重置模型目录、RPM 额度与 key 的冷却/失效标记。两个站的 key 互不通用,
|
|
23
|
+
带着旧站的「已失效/冷却」标记切过去,会让刚切换的几分钟内一把 key 都用不了。
|
|
24
|
+
- **模型目录不再按输出模态过滤**:上游 `/v1/models` 列什么就收什么(此前会静默剔除生图模型)。
|
|
25
|
+
不想要的模型可在设置页取消勾选,可见性会持久化。
|
|
26
|
+
- 单把 key 探活的默认模型改为取自当前端点的目录(此前写死 `deepseek-v4-flash`,
|
|
27
|
+
在国际站上必然 404)。
|
|
28
|
+
- `baseUrl` 配置项默认值改为空(表示「用所选端点的官方地址」);仍可显式填写以走自建网关,
|
|
29
|
+
此时切端点只换目录与额度,不改请求地址。
|
|
30
|
+
|
|
31
|
+
### 修复
|
|
32
|
+
|
|
33
|
+
- 端点切换后模型选择器不再残留另一个站独有的模型(例如国际站上出现 `deepseek-v4-flash`)。
|
|
34
|
+
|
|
35
|
+
### 兼容性
|
|
36
|
+
|
|
37
|
+
- 未切换过端点的老用户**行为完全不变**:默认端点仍是中国站,地址与限流额度不变。
|
|
38
|
+
- 凭据记录只新增了一个可选字段 `endpoint`,旧记录照常读取。
|
|
39
|
+
|
|
40
|
+
## [1.0.0]
|
|
41
|
+
|
|
42
|
+
商汤日日新多 Key 轮换池的稳定版:遇 429 自动切换 key、本地 RPM 预限流、
|
|
43
|
+
OpenAI 兼容本地接口、模型可见性与窗口自定义。
|
|
44
|
+
|
|
45
|
+
## [0.2.2]
|
|
46
|
+
|
|
47
|
+
- 思考档位改为**按模型维护实测词表**([#3](https://github.com/Phoeky/dsh-sensenova-pool/issues/3)):
|
|
48
|
+
`glm-5.3` 只认 `low / high / max`,单一映射表无论怎么调都会踩到某一头。
|
|
49
|
+
模型选择器里只显示该模型真实支持的档位。
|
|
50
|
+
|
|
51
|
+
## [0.2.1]
|
|
52
|
+
|
|
53
|
+
- 请求体清洗层:把「Agent 合法、商汤网关不认」的写法语义等价地规范化
|
|
54
|
+
(`developer`→`system`、`content: null`→`""`、`arguments` 对象→JSON 字符串、
|
|
55
|
+
`off`→`none`、摘掉 `logprobs` / `metadata` 等),修复 [issue #2](https://github.com/Phoeky/dsh-sensenova-pool/issues/2) 的 400。
|
|
56
|
+
- 新增 `debugRequestOn400`,400 时把实际发出的请求体写进日志。
|
|
57
|
+
|
|
58
|
+
## [0.2.0]
|
|
59
|
+
|
|
60
|
+
- 支持逐模型自定义上下文窗口与最大输出([#4](https://github.com/Phoeky/dsh-sensenova-pool/issues/4))。
|
|
61
|
+
- 用实测值覆盖上游把 `sensenova-6.8-flash` 报小 4 倍的 `context_length`。
|
|
62
|
+
|
|
63
|
+
## [0.1.3]
|
|
64
|
+
|
|
65
|
+
- 修复**启动期凭据竞态**:改为等凭据服务真正就绪后再装载,不再出现
|
|
66
|
+
「重启电脑后 Key 池为空」([#1](https://github.com/Phoeky/dsh-sensenova-pool/issues/1))。
|
|
67
|
+
|
|
68
|
+
## [0.1.1]
|
|
69
|
+
|
|
70
|
+
- 修复凭据记录写成 `api-key` + `payload` 导致整机 DSH 无法启动的问题(必须用 `grant`)。
|
|
71
|
+
|
|
72
|
+
## [0.1.0]
|
|
73
|
+
|
|
74
|
+
首个可用版本:多 Key 轮换池、429 自动切换、设置页管理 key。
|
package/README.md
CHANGED
|
@@ -47,6 +47,28 @@
|
|
|
47
47
|
|
|
48
48
|
截图里的 `2/3 成功,1 次限流` 是真实发生的记录:这把 Key 触发过 1 次 429,被池子内部消化掉,上层没有收到过任何 429。
|
|
49
49
|
|
|
50
|
+
### 🌏 可选端点:中国站 / 国际站
|
|
51
|
+
|
|
52
|
+
商汤有**两个独立的 API 站**,模型、套餐、额度、key 都各自独立:
|
|
53
|
+
|
|
54
|
+
| 端点 | 地址 | 适用 |
|
|
55
|
+
|---|---|---|
|
|
56
|
+
| 中国站(默认) | `https://token.sensenova.cn/v1` | 在商汤国内平台申请的 key |
|
|
57
|
+
| 国际站 | `https://token.sensenova.ai/v1` | 在国际站申请的 key |
|
|
58
|
+
|
|
59
|
+
在设置卡片最上方点一下就能切换,**无需重启 DSH**;所选端点会随 key 一起存进凭据库,
|
|
60
|
+
重启后仍然记得。切换会连带做四件事,缺一件状态就会自相矛盾:
|
|
61
|
+
|
|
62
|
+
1. **上游地址** —— 之后所有请求打到新站;
|
|
63
|
+
2. **模型目录** —— 换回该站的内置基线,再自动用新 key 刷新(中国站独有 `deepseek-*` / `glm-*`,
|
|
64
|
+
国际站是 5 个 `sensenova-*` 模型,含 2 个生图模型,全部列出);
|
|
65
|
+
3. **限流额度** —— 两个站的 RPM 分开算(本地预限流默认中国站 2、国际站 3);
|
|
66
|
+
4. **key 健康状态** —— 冷却/失效是针对旧站记下的,切过去时一律清掉,不带着旧标记进新站。
|
|
67
|
+
|
|
68
|
+
> ⚠️ 两个站的 key **互不通用**:拿国际站的 key 打中国站会得到 `401 Forbidden`。插件不会
|
|
69
|
+
> 自动猜你在哪个站(猜错会把本来好用的 key 标成失效),所以端点必须自己选对 ——
|
|
70
|
+
> 如果切过去后所有 key 都显示「已失效」,先检查是不是选错了站。
|
|
71
|
+
|
|
50
72
|
### 🧠 一次接入全部模型
|
|
51
73
|
|
|
52
74
|
装好插件后,模型选择器里自动出现「商汤日日新(Key 轮换池)」分组,包含以下模型:
|
|
@@ -169,11 +191,16 @@ sk-cccccccccccccccccccccccccccccc
|
|
|
169
191
|
|
|
170
192
|
点「**添加 Key**」。支持多种分隔方式,重复和无效项会被自动过滤。
|
|
171
193
|
|
|
172
|
-
### 2.
|
|
194
|
+
### 2. 先确认端点
|
|
195
|
+
|
|
196
|
+
卡片最上方的「上游端点」区显示当前打的是哪个站 —— 中国站还是国际站。点另一个按钮即可切换,
|
|
197
|
+
下方会显示切换后的实际上游地址。**key 只在自己申请的那个站有效**,选错站会看到 401。
|
|
198
|
+
|
|
199
|
+
### 3. 选择模型
|
|
173
200
|
|
|
174
201
|
新建会话,在模型选择器里选「**商汤日日新(Key 轮换池)**」下的任意模型,然后正常对话即可。
|
|
175
202
|
|
|
176
|
-
###
|
|
203
|
+
### 4. 需要时调整
|
|
177
204
|
|
|
178
205
|
| 按钮 | 作用 |
|
|
179
206
|
|---|---|
|
|
@@ -184,7 +211,7 @@ sk-cccccccccccccccccccccccccccccc
|
|
|
184
211
|
| **复制**(接口区) | 复制接口地址 / 对外 API Key |
|
|
185
212
|
| **模型勾选框** | 决定该模型是否出现在模型选择器里 |
|
|
186
213
|
|
|
187
|
-
###
|
|
214
|
+
### 5. 接入其他服务
|
|
188
215
|
|
|
189
216
|
设置页「本地接口」区给出两样东西:
|
|
190
217
|
|
|
@@ -217,20 +244,49 @@ print(client.chat.completions.create(
|
|
|
217
244
|
- id: llm-sensenova-pool
|
|
218
245
|
name: dsh-sensenova-pool
|
|
219
246
|
config:
|
|
220
|
-
|
|
247
|
+
endpoint: cn # cn = 中国站,intl = 国际站;默认 cn。也可在设置卡片里切换
|
|
248
|
+
rpmLimit: 2 # 每把 Key 的本地 RPM 预限流上限;0 = 关闭,-1(默认)= 按端点取值
|
|
221
249
|
maxAttempts: 6 # 单个请求最多尝试几把 Key
|
|
222
250
|
maxConcurrency: 1 # 每把 Key 的并发上限;1 = 串行(推荐)
|
|
223
251
|
acquireTimeoutMs: 180000
|
|
224
252
|
requestTimeoutMs: 60000
|
|
225
|
-
baseUrl: https://token.sensenova.cn/v1
|
|
253
|
+
# baseUrl: https://token.sensenova.cn/v1 # 仅当所有请求要走自建网关时才填;填了就不随端点切换
|
|
226
254
|
```
|
|
227
255
|
|
|
228
|
-
`
|
|
256
|
+
`endpoint` 只决定**默认**端点:一旦在设置卡片里切过,以卡片里的选择(存在凭据库)为准,
|
|
257
|
+
这样「重启后还记得你在哪个站」不需要改配置文件。`rpmLimit` 留默认值时按端点取
|
|
258
|
+
(中国站 2、国际站 3),显式写数字则两个端点都用这个值。
|
|
259
|
+
|
|
260
|
+
`rpmLimit` 的中国站默认值 2 基于实测(真实上限约 3 RPM)。**调大它不会提高吞吐,只会更容易被限流** —— 想提高吞吐应该增加 Key,而不是加压。
|
|
261
|
+
|
|
262
|
+
**两个站的 Key 能混着用吗?** 不能。key 只在发卡的那个站有效,池子只打「当前选中的端点」,
|
|
263
|
+
所以换站后要换用该站的 key(切站不会删除现有的 key,切回来它们还在)。
|
|
229
264
|
|
|
230
265
|
---
|
|
231
266
|
|
|
232
267
|
## 常见问题
|
|
233
268
|
|
|
269
|
+
**切到国际站后所有 key 都显示「已失效」?**
|
|
270
|
+
|
|
271
|
+
基本就是**选错站了**:国际站的 key 打中国站会返回 `401 Forbidden`,反之亦然。切回正确的站即可
|
|
272
|
+
(切站时会清掉冷却/失效标记,所以切回来那些 key 会立刻恢复可用)。如果确认站选对了仍然 401,
|
|
273
|
+
那才是 key 本身的问题。
|
|
274
|
+
|
|
275
|
+
**国际站里怎么没有 `deepseek-v4-flash` / `glm-5.2`?**
|
|
276
|
+
|
|
277
|
+
这些模型只在中国站提供。国际站目前有 5 个模型,且**全部列出、不做事先筛选**:
|
|
278
|
+
|
|
279
|
+
| 模型 | 输出 | 对话实测(2026-11-19) |
|
|
280
|
+
|---|---|---|
|
|
281
|
+
| `sensenova-6.8-flash` | 文本 | ✅ 流式 / 非流式 / 图片输入 / 七档思考强度 |
|
|
282
|
+
| `sensenova-6.8-flash-lite` | 文本 | ✅ 同上 |
|
|
283
|
+
| `sensenova-6.7-flash-lite` | 文本 | ⚠️ 本 key 返回 404 `model is not found` |
|
|
284
|
+
| `sensenova-u1-fast` | 图片 | ⚠️ 生图模型,对话接口 404 |
|
|
285
|
+
| `sensenova-u1.5-lite` | 图片 | ⚠️ 生图模型,对话接口 404 |
|
|
286
|
+
|
|
287
|
+
⚠️ 的那三个仍然展示:404 可能是套餐/灰度差异,隐藏会让有权限的人也选不到。选中后上游
|
|
288
|
+
会如实报 404 —— 不想要的话在设置页取消勾选即可,可见性会持久化。
|
|
289
|
+
|
|
234
290
|
**模型选择器里没有「商汤日日新」分组?**
|
|
235
291
|
|
|
236
292
|
确认 `dsh.profile.bundles` 里有 `dsh-sensenova-pool`,然后**重启 DSH**。如果分组在但某个模型不见了,去设置页看看是不是被自己勾掉了。
|
|
@@ -400,6 +456,7 @@ Paste one or more SenseNova API keys in one place and use every SenseNova model
|
|
|
400
456
|
- **Never leaks a 429 upward** — when every key is exhausted the plugin reports `503 + Retry-After` so DSH backs off on its own
|
|
401
457
|
- **All models, zero config** — a built-in catalogue works offline and refreshes from `/v1/models` once a valid key is present, so new models need no plugin upgrade
|
|
402
458
|
- **Keys are masked** — only `sk-example...0001` style masks are ever displayed
|
|
459
|
+
- **Two endpoints** — pick the China site (`token.sensenova.cn`) or the international site (`token.sensenova.ai`) from the settings card; switching also swaps the model list and the rate-limit budget, and the choice survives restarts
|
|
403
460
|
|
|
404
461
|
### Install
|
|
405
462
|
|
|
@@ -411,7 +468,7 @@ Add `"dsh-sensenova-pool"` to `dsh.profile.bundles` in your profile `package.jso
|
|
|
411
468
|
|
|
412
469
|
### Usage
|
|
413
470
|
|
|
414
|
-
Open **Settings → dsh-sensenova-pool** (or the plugin card on the Plugins page), paste your keys, click **Add keys**, then pick any model under the "商汤日日新(Key 轮换池)" group in the model picker. The same page shows the local endpoint (base URL, port, API key) and lets you hide models from the picker.
|
|
471
|
+
Open **Settings → dsh-sensenova-pool** (or the plugin card on the Plugins page), pick your endpoint (China site / international site — a key only works on the site that issued it), paste your keys, click **Add keys**, then pick any model under the "商汤日日新(Key 轮换池)" group in the model picker. The same page shows the local endpoint (base URL, port, API key) and lets you hide models from the picker.
|
|
415
472
|
|
|
416
473
|
### Troubleshooting
|
|
417
474
|
|
package/lib/catalog.d.ts
CHANGED
|
@@ -30,12 +30,60 @@ export interface SenseNovaModel {
|
|
|
30
30
|
* 内置基线目录(实测自 token.sensenova.cn)。
|
|
31
31
|
*
|
|
32
32
|
* contextWindow 严格取自 `context_length`,maxTokens 取自 `max_output_length`。
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
33
|
+
* 不含 `sensenova-u1-fast` / `sensenova-u1.5-lite`:**中国站的 /v1/models 本来就不列
|
|
34
|
+
* 这两个生图模型**(基线只镜像上游列出的内容;国际站的目录里它们有,见
|
|
35
|
+
* {@link INTL_BUILTIN_MODELS})。拿到 key 后刷新的目录同样不做任何筛选。
|
|
36
|
+
*
|
|
37
|
+
* 这是**中国站**的目录。国际站的目录不同,见 {@link INTL_BUILTIN_MODELS}。
|
|
36
38
|
*/
|
|
37
39
|
export declare const BUILTIN_MODELS: readonly SenseNovaModel[];
|
|
38
|
-
|
|
40
|
+
/** 端点标识:'cn' = 中国站,'intl' = 国际站。 */
|
|
41
|
+
export type EndpointId = 'cn' | 'intl';
|
|
42
|
+
export interface SenseNovaEndpoint {
|
|
43
|
+
id: EndpointId;
|
|
44
|
+
/** 显示名(中英文一致:网页端点本身没有语言)。 */
|
|
45
|
+
label: string;
|
|
46
|
+
/** OpenAI 兼容根地址(含 `/v1`)。 */
|
|
47
|
+
baseUrl: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 可选的上游端点。
|
|
51
|
+
*
|
|
52
|
+
* 商汤分两个独立的 API 站点,**key 不通用**(在哪个站注册的 key 只能打哪个站),
|
|
53
|
+
* 所以端点必须是用户自己的选择,不能靠「挨个试」自动判定 —— 拿 A 站的 key 打
|
|
54
|
+
* B 站只会得到 401,而池子会把这把本来好用的 key 标成失效。
|
|
55
|
+
*
|
|
56
|
+
* 默认 'cn' 是刻意的:老用户的 key 与配置都指向中国站,升级后行为必须不变。
|
|
57
|
+
*/
|
|
58
|
+
export declare const ENDPOINTS: readonly SenseNovaEndpoint[];
|
|
59
|
+
export declare const DEFAULT_ENDPOINT_ID: EndpointId;
|
|
60
|
+
/** 端点 id 归一;认不出来时回落到默认端点。 */
|
|
61
|
+
export declare function isEndpointId(value: unknown): value is EndpointId;
|
|
62
|
+
/** 按 id 取端点定义;未知 id 回落到默认端点。 */
|
|
63
|
+
export declare function endpointFor(id: unknown): SenseNovaEndpoint;
|
|
64
|
+
/** 默认上游根地址(中国站)。 */
|
|
65
|
+
export declare const DEFAULT_BASE_URL: string;
|
|
66
|
+
/**
|
|
67
|
+
* 国际站(token.sensenova.ai)的内置基线目录 —— 与上游 `/v1/models` 一一对应,
|
|
68
|
+
* **不做任何筛选**:上游列了什么,这里就有什么。
|
|
69
|
+
*
|
|
70
|
+
* 2026-11-19 用真实国际站 key 实测的可用性(供参考,不决定是否展示):
|
|
71
|
+
*
|
|
72
|
+
* - `sensenova-6.8-flash` / `sensenova-6.8-flash-lite` —— 对话、流式、图片输入、
|
|
73
|
+
* 七档 `reasoning_effort` 全部 200。
|
|
74
|
+
* - `sensenova-6.7-flash-lite` —— `/v1/models` 声明 `output_modalities: ["text"]`(是多模态
|
|
75
|
+
* 对话模型),但本 key 对话请求多次返回 404 `model is not found`。仍然列出:
|
|
76
|
+
* 可能是该 key 的套餐/灰度差异,隐藏它会让有权限的用户也选不到。选中后上游会
|
|
77
|
+
* 如实报 404,在设置页取消勾选即可隐藏。
|
|
78
|
+
* - `sensenova-u1-fast` / `sensenova-u1.5-lite` —— 生图模型(`output_modalities: ["image"]`),
|
|
79
|
+
* 对话接口返回 404。同样列出,理由同上。
|
|
80
|
+
*
|
|
81
|
+
* 窗口沿用上游报的 262144:国际站**没有**国内站 issue #4 那种「报小 4 倍」的实测
|
|
82
|
+
* 证据,因此不设 `contextWindowOverridesUpstream`,以拿到 key 后刷新的上游值为准。
|
|
83
|
+
*/
|
|
84
|
+
export declare const INTL_BUILTIN_MODELS: readonly SenseNovaModel[];
|
|
85
|
+
/** 该端点的内置基线目录。 */
|
|
86
|
+
export declare function builtinModelsFor(endpointId: unknown): readonly SenseNovaModel[];
|
|
39
87
|
/**
|
|
40
88
|
* 商汤 `reasoning_effort` 的合法拼写,按推理强度从弱到强排列。
|
|
41
89
|
*
|
|
@@ -158,8 +206,11 @@ export declare function fixedParamsFor(modelId: string | undefined): {
|
|
|
158
206
|
* 把 `/v1/models` 的原始响应解析成目录。
|
|
159
207
|
*
|
|
160
208
|
* 宽容解析:字段缺失就回落到基线/默认值,任何一条坏数据都不应该让整个目录失败。
|
|
209
|
+
*
|
|
210
|
+
* `baselines` 由调用方按**当前端点**给出:两个站的模型集合不同,拿中国站的基线
|
|
211
|
+
* 去解析国际站的响应,会让国际站缺失的模型也套上中国站的窗口/上限。
|
|
161
212
|
*/
|
|
162
|
-
export declare function parseModelCatalog(payload: unknown): SenseNovaModel[];
|
|
213
|
+
export declare function parseModelCatalog(payload: unknown, baselines?: readonly SenseNovaModel[]): SenseNovaModel[];
|
|
163
214
|
export type UpstreamErrorKind = 'rate_limit' | 'invalid_credential' | 'not_found' | 'server' | 'client';
|
|
164
215
|
export interface ClassifiedError {
|
|
165
216
|
kind: UpstreamErrorKind;
|
|
@@ -182,6 +233,9 @@ export declare function extractErrorMessage(body: string): string;
|
|
|
182
233
|
*
|
|
183
234
|
* 关键判定(实测商汤行为):
|
|
184
235
|
* - 429 `ModelAccountTpmRateLimitExceeded` → 限流,**换 key 重试**。
|
|
236
|
+
* - 401 `{"code":16,"message":"Forbidden"}` → 该 key 不属于**这个端点**。实测国际站
|
|
237
|
+
* key 打中国站 `token.sensenova.cn` 就是这条;两个站的 key 互不通用,因此用户
|
|
238
|
+
* 选错端点时应当直接看到鉴权失败,而不是被当成「限流」无限重试。
|
|
185
239
|
* - 401/403 `permission_denied_error` (code 7) → 该 key 对该模型无权限。
|
|
186
240
|
* 注意:这**不代表 key 失效**,只代表这个模型不在当前套餐里;因此归类为
|
|
187
241
|
* `client`(不冷却、不换 key 空转),而不是 `invalid_credential`。
|
package/lib/catalog.js
CHANGED
|
@@ -10,9 +10,11 @@
|
|
|
10
10
|
* 内置基线目录(实测自 token.sensenova.cn)。
|
|
11
11
|
*
|
|
12
12
|
* contextWindow 严格取自 `context_length`,maxTokens 取自 `max_output_length`。
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
13
|
+
* 不含 `sensenova-u1-fast` / `sensenova-u1.5-lite`:**中国站的 /v1/models 本来就不列
|
|
14
|
+
* 这两个生图模型**(基线只镜像上游列出的内容;国际站的目录里它们有,见
|
|
15
|
+
* {@link INTL_BUILTIN_MODELS})。拿到 key 后刷新的目录同样不做任何筛选。
|
|
16
|
+
*
|
|
17
|
+
* 这是**中国站**的目录。国际站的目录不同,见 {@link INTL_BUILTIN_MODELS}。
|
|
16
18
|
*/
|
|
17
19
|
export const BUILTIN_MODELS = [
|
|
18
20
|
{ id: 'deepseek-v4.1-flash', name: 'DeepSeek V4.1 Flash', contextWindow: 1048576, maxTokens: 65536, supportsImages: false, reasoning: true },
|
|
@@ -29,7 +31,61 @@ export const BUILTIN_MODELS = [
|
|
|
29
31
|
// 对照组:同样报 262144,但实测 257,013 tokens 通过、275K 档就 400 —— 这是真值。
|
|
30
32
|
{ id: 'sensenova-6.8-flash-lite', name: 'SenseNova 6.8 Flash-Lite', contextWindow: 262144, maxTokens: 65536, supportsImages: true, reasoning: true },
|
|
31
33
|
];
|
|
32
|
-
|
|
34
|
+
/**
|
|
35
|
+
* 可选的上游端点。
|
|
36
|
+
*
|
|
37
|
+
* 商汤分两个独立的 API 站点,**key 不通用**(在哪个站注册的 key 只能打哪个站),
|
|
38
|
+
* 所以端点必须是用户自己的选择,不能靠「挨个试」自动判定 —— 拿 A 站的 key 打
|
|
39
|
+
* B 站只会得到 401,而池子会把这把本来好用的 key 标成失效。
|
|
40
|
+
*
|
|
41
|
+
* 默认 'cn' 是刻意的:老用户的 key 与配置都指向中国站,升级后行为必须不变。
|
|
42
|
+
*/
|
|
43
|
+
export const ENDPOINTS = [
|
|
44
|
+
{ id: 'cn', label: '中国站', baseUrl: 'https://token.sensenova.cn/v1' },
|
|
45
|
+
{ id: 'intl', label: '国际站', baseUrl: 'https://token.sensenova.ai/v1' },
|
|
46
|
+
];
|
|
47
|
+
export const DEFAULT_ENDPOINT_ID = 'cn';
|
|
48
|
+
/** 端点 id 归一;认不出来时回落到默认端点。 */
|
|
49
|
+
export function isEndpointId(value) {
|
|
50
|
+
return value === 'cn' || value === 'intl';
|
|
51
|
+
}
|
|
52
|
+
/** 按 id 取端点定义;未知 id 回落到默认端点。 */
|
|
53
|
+
export function endpointFor(id) {
|
|
54
|
+
const found = ENDPOINTS.find((endpoint) => endpoint.id === id);
|
|
55
|
+
return found ?? ENDPOINTS.find((endpoint) => endpoint.id === DEFAULT_ENDPOINT_ID);
|
|
56
|
+
}
|
|
57
|
+
/** 默认上游根地址(中国站)。 */
|
|
58
|
+
export const DEFAULT_BASE_URL = endpointFor(DEFAULT_ENDPOINT_ID).baseUrl;
|
|
59
|
+
/**
|
|
60
|
+
* 国际站(token.sensenova.ai)的内置基线目录 —— 与上游 `/v1/models` 一一对应,
|
|
61
|
+
* **不做任何筛选**:上游列了什么,这里就有什么。
|
|
62
|
+
*
|
|
63
|
+
* 2026-11-19 用真实国际站 key 实测的可用性(供参考,不决定是否展示):
|
|
64
|
+
*
|
|
65
|
+
* - `sensenova-6.8-flash` / `sensenova-6.8-flash-lite` —— 对话、流式、图片输入、
|
|
66
|
+
* 七档 `reasoning_effort` 全部 200。
|
|
67
|
+
* - `sensenova-6.7-flash-lite` —— `/v1/models` 声明 `output_modalities: ["text"]`(是多模态
|
|
68
|
+
* 对话模型),但本 key 对话请求多次返回 404 `model is not found`。仍然列出:
|
|
69
|
+
* 可能是该 key 的套餐/灰度差异,隐藏它会让有权限的用户也选不到。选中后上游会
|
|
70
|
+
* 如实报 404,在设置页取消勾选即可隐藏。
|
|
71
|
+
* - `sensenova-u1-fast` / `sensenova-u1.5-lite` —— 生图模型(`output_modalities: ["image"]`),
|
|
72
|
+
* 对话接口返回 404。同样列出,理由同上。
|
|
73
|
+
*
|
|
74
|
+
* 窗口沿用上游报的 262144:国际站**没有**国内站 issue #4 那种「报小 4 倍」的实测
|
|
75
|
+
* 证据,因此不设 `contextWindowOverridesUpstream`,以拿到 key 后刷新的上游值为准。
|
|
76
|
+
*/
|
|
77
|
+
export const INTL_BUILTIN_MODELS = [
|
|
78
|
+
{ id: 'sensenova-6.8-flash', name: 'SenseNova 6.8 Flash', contextWindow: 262144, maxTokens: 65536, supportsImages: true, reasoning: true },
|
|
79
|
+
{ id: 'sensenova-6.8-flash-lite', name: 'SenseNova 6.8 Flash-Lite', contextWindow: 262144, maxTokens: 65536, supportsImages: true, reasoning: true },
|
|
80
|
+
{ id: 'sensenova-6.7-flash-lite', name: 'SenseNova 6.7 Flash-Lite', contextWindow: 262144, maxTokens: 65536, supportsImages: true, reasoning: true },
|
|
81
|
+
// 生图模型:上游列在 /v1/models 里,这里也照常列出(对话接口会 404)。
|
|
82
|
+
{ id: 'sensenova-u1-fast', name: 'SenseNova U1 Fast', contextWindow: 262144, maxTokens: 65536, supportsImages: false, reasoning: true },
|
|
83
|
+
{ id: 'sensenova-u1.5-lite', name: 'SenseNova U1.5 Lite', contextWindow: 262144, maxTokens: 65536, supportsImages: false, reasoning: true },
|
|
84
|
+
];
|
|
85
|
+
/** 该端点的内置基线目录。 */
|
|
86
|
+
export function builtinModelsFor(endpointId) {
|
|
87
|
+
return endpointFor(endpointId).id === 'intl' ? INTL_BUILTIN_MODELS : BUILTIN_MODELS;
|
|
88
|
+
}
|
|
33
89
|
// --------------------------------------------------------- 推理档位(issue #3)
|
|
34
90
|
/**
|
|
35
91
|
* 商汤 `reasoning_effort` 的合法拼写,按推理强度从弱到强排列。
|
|
@@ -256,8 +312,11 @@ function resolveContextWindow(raw, baseline) {
|
|
|
256
312
|
* 把 `/v1/models` 的原始响应解析成目录。
|
|
257
313
|
*
|
|
258
314
|
* 宽容解析:字段缺失就回落到基线/默认值,任何一条坏数据都不应该让整个目录失败。
|
|
315
|
+
*
|
|
316
|
+
* `baselines` 由调用方按**当前端点**给出:两个站的模型集合不同,拿中国站的基线
|
|
317
|
+
* 去解析国际站的响应,会让国际站缺失的模型也套上中国站的窗口/上限。
|
|
259
318
|
*/
|
|
260
|
-
export function parseModelCatalog(payload) {
|
|
319
|
+
export function parseModelCatalog(payload, baselines = BUILTIN_MODELS) {
|
|
261
320
|
const data = payload?.data;
|
|
262
321
|
if (!Array.isArray(data))
|
|
263
322
|
return [];
|
|
@@ -268,13 +327,10 @@ export function parseModelCatalog(payload) {
|
|
|
268
327
|
continue;
|
|
269
328
|
const modalities = Array.isArray(raw.input_modalities) ? raw.input_modalities : [];
|
|
270
329
|
const features = Array.isArray(raw.supported_features) ? raw.supported_features : [];
|
|
271
|
-
const baseline =
|
|
272
|
-
//
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
: undefined;
|
|
276
|
-
if (outputs && outputs.length > 0 && !outputs.includes('text'))
|
|
277
|
-
continue;
|
|
330
|
+
const baseline = baselines.find((model) => model.id === id);
|
|
331
|
+
// 上游 /v1/models 列什么就收什么,**不按输出模态筛掉生图模型**:用户明确要求
|
|
332
|
+
// 「模型全放出来」。生图模型在对话接口上会如实返回 404,比在列表里凭空消失
|
|
333
|
+
// 更容易理解;不想要的话在设置页取消勾选即可(可见性会持久化)。
|
|
278
334
|
out.push({
|
|
279
335
|
id,
|
|
280
336
|
name: typeof raw.name === 'string' && raw.name.trim() ? raw.name.trim() : id,
|
|
@@ -328,6 +384,9 @@ export function extractErrorMessage(body) {
|
|
|
328
384
|
*
|
|
329
385
|
* 关键判定(实测商汤行为):
|
|
330
386
|
* - 429 `ModelAccountTpmRateLimitExceeded` → 限流,**换 key 重试**。
|
|
387
|
+
* - 401 `{"code":16,"message":"Forbidden"}` → 该 key 不属于**这个端点**。实测国际站
|
|
388
|
+
* key 打中国站 `token.sensenova.cn` 就是这条;两个站的 key 互不通用,因此用户
|
|
389
|
+
* 选错端点时应当直接看到鉴权失败,而不是被当成「限流」无限重试。
|
|
331
390
|
* - 401/403 `permission_denied_error` (code 7) → 该 key 对该模型无权限。
|
|
332
391
|
* 注意:这**不代表 key 失效**,只代表这个模型不在当前套餐里;因此归类为
|
|
333
392
|
* `client`(不冷却、不换 key 空转),而不是 `invalid_credential`。
|
package/lib/client.js
CHANGED
|
@@ -247,7 +247,7 @@ window.__ModuleLoader__.load({
|
|
|
247
247
|
// ------------------------------------------------------------------ 卡片
|
|
248
248
|
|
|
249
249
|
/**
|
|
250
|
-
*
|
|
250
|
+
* 管理卡片:上游端点 + 对外接口 + Key 池 + 模型可见性。
|
|
251
251
|
*
|
|
252
252
|
* @param props.t 文案读取器(宿主 locale 绑定)。
|
|
253
253
|
* @param props.variant 'section' 时渲染页面标题(设置分区里需要),'card' 时不渲染。
|
|
@@ -262,6 +262,8 @@ window.__ModuleLoader__.load({
|
|
|
262
262
|
const [catalog, setCatalog] = React.useState(undefined);
|
|
263
263
|
const [models, setModels] = React.useState([]);
|
|
264
264
|
const [endpoint, setEndpoint] = React.useState(undefined);
|
|
265
|
+
// 上游端点(中国站 / 国际站):{ current, upstream, pinned, rpmLimit, options[] }
|
|
266
|
+
const [upstream, setUpstream] = React.useState(undefined);
|
|
265
267
|
const [adminKey, setAdminKey] = React.useState(undefined);
|
|
266
268
|
const [tested, setTested] = React.useState({});
|
|
267
269
|
const [copied, setCopied] = React.useState('');
|
|
@@ -279,6 +281,7 @@ window.__ModuleLoader__.load({
|
|
|
279
281
|
setCatalog(status.catalog);
|
|
280
282
|
if (Array.isArray(status.models)) setModels(status.models);
|
|
281
283
|
if (status.endpoint) setEndpoint(status.endpoint);
|
|
284
|
+
if (status.upstreamEndpoint) setUpstream(status.upstreamEndpoint);
|
|
282
285
|
if (typeof status.adminKey === 'string') setAdminKey(status.adminKey);
|
|
283
286
|
}
|
|
284
287
|
setError(undefined);
|
|
@@ -343,6 +346,16 @@ window.__ModuleLoader__.load({
|
|
|
343
346
|
return value.ok ? t('testOk', { ms: String(value.latencyMs) }) : t('testFail', { message: String(value.message).slice(0, 80) });
|
|
344
347
|
});
|
|
345
348
|
|
|
349
|
+
// 切换上游端点。key 在两个站之间不通用,所以这里不做任何自动探测 —— 换站后
|
|
350
|
+
// 新站上没有的 key 会如实显示「已失效」,用户据此判断是不是选错了站。
|
|
351
|
+
const selectEndpoint = (id) =>
|
|
352
|
+
run('endpoint', async () => {
|
|
353
|
+
const value = await postJson(`${BASE}/keys`, { action: 'set-endpoint', id }, adminKey);
|
|
354
|
+
if (value && value.upstreamEndpoint) setUpstream(value.upstreamEndpoint);
|
|
355
|
+
const chosen = (value?.upstreamEndpoint?.options || []).find((option) => option.active);
|
|
356
|
+
return t('endpointSwitched', { label: chosen ? chosen.label : id });
|
|
357
|
+
});
|
|
358
|
+
|
|
346
359
|
const toggleModel = (id, enabled) =>
|
|
347
360
|
run('model', async () => {
|
|
348
361
|
await postJson(`${BASE}/keys`, { action: 'set-model', id, enabled }, adminKey);
|
|
@@ -400,6 +413,40 @@ window.__ModuleLoader__.load({
|
|
|
400
413
|
variant === 'section' ? h('h2', { key: 'title', style: pageTitleStyle }, t('nav')) : null,
|
|
401
414
|
h('p', { key: 'hint', style: hintStyle }, t('hint')),
|
|
402
415
|
|
|
416
|
+
// ---- 上游端点:中国站 / 国际站 -------------------------------------
|
|
417
|
+
//
|
|
418
|
+
// 两个站的 key 互不通用,所以这是一个「你得自己选对」的开关,而不是自动
|
|
419
|
+
// 探测。放在最上面:填 key 之前就该先确认打的是哪个站。
|
|
420
|
+
upstream && Array.isArray(upstream.options) && upstream.options.length > 0
|
|
421
|
+
? h('div', { key: 'endpoint-pick', style: { ...blockStyle, paddingTop: 0, borderTop: 'none' } }, [
|
|
422
|
+
h('h3', { key: 'title', style: blockTitleStyle }, t('epTitle')),
|
|
423
|
+
h('p', { key: 'hint', style: hintStyle }, t('epHint')),
|
|
424
|
+
h(
|
|
425
|
+
'div',
|
|
426
|
+
{ key: 'options', style: { display: 'flex', flexWrap: 'wrap', gap: 8 } },
|
|
427
|
+
upstream.options.map((option) =>
|
|
428
|
+
h(
|
|
429
|
+
'button',
|
|
430
|
+
{
|
|
431
|
+
key: option.id,
|
|
432
|
+
type: 'button',
|
|
433
|
+
// 选中项用主按钮样式;其余是普通按钮。
|
|
434
|
+
style: option.active ? buttonPrimaryStyle : buttonStyle,
|
|
435
|
+
disabled: busy || option.active === true,
|
|
436
|
+
title: option.baseUrl,
|
|
437
|
+
onClick: () => selectEndpoint(option.id),
|
|
438
|
+
},
|
|
439
|
+
option.active ? `✓ ${option.label}` : option.label,
|
|
440
|
+
),
|
|
441
|
+
),
|
|
442
|
+
),
|
|
443
|
+
h('p', { key: 'addr', style: { ...codeStyle, margin: 0 } }, `${t('epUpstream')}:${upstream.upstream || '—'}`),
|
|
444
|
+
upstream.pinned
|
|
445
|
+
? h('p', { key: 'pinned', style: { ...hintStyle, color: 'var(--dsw-alias-label-warning, #bf8700)' } }, t('epPinned'))
|
|
446
|
+
: null,
|
|
447
|
+
])
|
|
448
|
+
: null,
|
|
449
|
+
|
|
403
450
|
summary
|
|
404
451
|
? h(
|
|
405
452
|
'div',
|
|
@@ -673,6 +720,11 @@ window.__ModuleLoader__.load({
|
|
|
673
720
|
copy: 'Copy',
|
|
674
721
|
copied: 'Copied',
|
|
675
722
|
copyFailed: 'Copy failed — select the text and copy manually.',
|
|
723
|
+
epTitle: 'Endpoint',
|
|
724
|
+
epHint: 'The China site and the international site are separate services: a key only works on the site that issued it. Switching swaps the upstream URL, the model list and the rate-limit budget.',
|
|
725
|
+
epUpstream: 'Upstream',
|
|
726
|
+
epPinned: 'baseUrl is fixed in the plugin config — switching only changes the model list and the rate-limit budget, not the address.',
|
|
727
|
+
endpointSwitched: 'Switched to {label}',
|
|
676
728
|
keysTitle: 'Key pool',
|
|
677
729
|
modelsTitle: 'Models in the picker',
|
|
678
730
|
modelsHint: '{count} of {total} models are shown. Context window and max output are editable per model — clear a field to restore the default.',
|
|
@@ -728,6 +780,11 @@ window.__ModuleLoader__.load({
|
|
|
728
780
|
copy: '复制',
|
|
729
781
|
copied: '已复制',
|
|
730
782
|
copyFailed: '复制失败,请手动选中文本复制。',
|
|
783
|
+
epTitle: '上游端点',
|
|
784
|
+
epHint: '中国站与国际站是两个独立的服务,key 只在发卡的那个站有效。切换会同时换掉上游地址、模型列表与限流额度。',
|
|
785
|
+
epUpstream: '上游地址',
|
|
786
|
+
epPinned: '插件配置里写死了 baseUrl —— 切换只换模型列表与限流额度,不改请求地址。',
|
|
787
|
+
endpointSwitched: '已切换到{label}',
|
|
731
788
|
keysTitle: 'Key 池',
|
|
732
789
|
modelsTitle: '下拉中的模型',
|
|
733
790
|
modelsHint: '共 {total} 个模型,当前展示 {count} 个。窗口与最大输出可逐模型修改(清空输入框即恢复默认)。',
|
package/lib/index.d.ts
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* 好处是「屏蔽 429」这件事被关在一个我们完全掌控的小盒子里:上层看到的永远是
|
|
16
16
|
* 一个稳定的 OpenAI 兼容端点,429 在盒子内部被消化掉。
|
|
17
17
|
*/
|
|
18
|
+
import { type EndpointId } from './catalog.js';
|
|
18
19
|
import { keyIdOf, maskKey } from './keypool.js';
|
|
19
20
|
/** 本插件注册的 provider 路由名(模型选择器里显示为此分组)。 */
|
|
20
21
|
declare const PROVIDER_ID = "sensenova-pool";
|
|
@@ -36,6 +37,7 @@ export declare const inject: string[];
|
|
|
36
37
|
*/
|
|
37
38
|
export declare const Config: import("@deepseek-ai/schemastery").Schema;
|
|
38
39
|
type PluginConfig = {
|
|
40
|
+
endpoint: EndpointId;
|
|
39
41
|
baseUrl: string;
|
|
40
42
|
maxAttempts: number;
|
|
41
43
|
acquireTimeoutMs: number;
|
package/lib/index.js
CHANGED
|
@@ -21,10 +21,10 @@ import { resolveRetryPolicy } from '@deepseek-ai/dsh-llm';
|
|
|
21
21
|
import { createProvider } from '@earendil-works/pi-ai';
|
|
22
22
|
import { openAICompletionsApi } from '@earendil-works/pi-ai/api/openai-completions.lazy';
|
|
23
23
|
import z from '@deepseek-ai/schemastery';
|
|
24
|
-
import {
|
|
24
|
+
import { DEFAULT_ENDPOINT_ID, ENDPOINTS, builtinModelsFor, endpointFor, isEndpointId, thinkingLevelMapFor, } from './catalog.js';
|
|
25
25
|
import { KeyPool, keyIdOf, maskKey } from './keypool.js';
|
|
26
26
|
import { ModelCatalog, createShim } from './shim.js';
|
|
27
|
-
import { registerKeyRoutes, registerSettingsSection } from './routes.js';
|
|
27
|
+
import { registerKeyRoutes, registerSettingsSection, } from './routes.js';
|
|
28
28
|
/** 本插件注册的 provider 路由名(模型选择器里显示为此分组)。 */
|
|
29
29
|
const PROVIDER_ID = 'sensenova-pool';
|
|
30
30
|
const PROVIDER_NAME = '商汤日日新(Key 轮换池)';
|
|
@@ -48,6 +48,22 @@ const STREAM_IDLE_TIMEOUT_MS = 300_000;
|
|
|
48
48
|
* 插件配置里调大;设为 0 即关闭本地预限流。
|
|
49
49
|
*/
|
|
50
50
|
const DEFAULT_RPM_LIMIT = 2;
|
|
51
|
+
/**
|
|
52
|
+
* 每个端点的默认 RPM 预限流值。
|
|
53
|
+
*
|
|
54
|
+
* 两个站的额度互不相干(账号与套餐都在各自的站上),所以额度也分开算:
|
|
55
|
+
* - 中国站沿用实测值 2(真实上限约 3 RPM,见上);
|
|
56
|
+
* - 国际站取保守值 3:2026-11-19 用真实国际站 key 实测连续逐档位打点观察时长
|
|
57
|
+
* 一直在秒级,没有撞到限流。仍取低于「假设上限」的值 —— 本地预限流宁可保守,
|
|
58
|
+
* 调大它不会提高吞吐,只会更容易被限流。
|
|
59
|
+
*
|
|
60
|
+
* 用户在 config 里显式写了 `rpmLimit` 时,该值对**两个端点**都生效(见 apply)。
|
|
61
|
+
*/
|
|
62
|
+
const ENDPOINT_RPM_LIMIT = { cn: DEFAULT_RPM_LIMIT, intl: 3 };
|
|
63
|
+
/** 该端点的默认 RPM 上限。 */
|
|
64
|
+
function defaultRpmLimitFor(endpointId) {
|
|
65
|
+
return ENDPOINT_RPM_LIMIT[endpointFor(endpointId).id];
|
|
66
|
+
}
|
|
51
67
|
/** 与 pi-ai 约定的本路由图片预算;与本插件无关但 profile 形状需要它。 */
|
|
52
68
|
const REQUEST_IMAGE_BUDGETS = {
|
|
53
69
|
maxRequestImageBytes: 20_971_520,
|
|
@@ -65,14 +81,23 @@ export const inject = ['llm'];
|
|
|
65
81
|
* 这类非机密参数。
|
|
66
82
|
*/
|
|
67
83
|
export const Config = z.object({
|
|
68
|
-
|
|
84
|
+
endpoint: z
|
|
85
|
+
.string()
|
|
86
|
+
.default(DEFAULT_ENDPOINT_ID)
|
|
87
|
+
.description('上游端点:cn = 中国站(token.sensenova.cn),intl = 国际站(token.sensenova.ai)。' +
|
|
88
|
+
'两个站的 key 互不通用;也可以在设置卡片里切换,切换结果会持久化。' +
|
|
89
|
+
'写错的值不会被拒绝,而是回落到 cn(不让一个拼写问题把插件卡在加载阶段)。'),
|
|
90
|
+
baseUrl: z
|
|
91
|
+
.string()
|
|
92
|
+
.default('')
|
|
93
|
+
.description('覆盖端点地址(填空表示用所选端点的默认地址)。仅在所有请求要走自建网关 / 代理时使用。'),
|
|
69
94
|
maxAttempts: z.number().default(6).description('单个请求最多尝试几把 key'),
|
|
70
95
|
acquireTimeoutMs: z.number().default(180_000).description('等待可用 key 的总预算(毫秒)'),
|
|
71
96
|
requestTimeoutMs: z.number().default(60_000).description('单次上游请求超时(毫秒)'),
|
|
72
97
|
rpmLimit: z
|
|
73
98
|
.number()
|
|
74
|
-
.default(
|
|
75
|
-
.description('每把 key 的本地 RPM 预限流上限;0
|
|
99
|
+
.default(-1)
|
|
100
|
+
.description('每把 key 的本地 RPM 预限流上限;0 表示不限制;-1(默认)表示按端点取默认值(中国站 2 / 国际站 3)。'),
|
|
76
101
|
maxConcurrency: z.number().default(1).description('每把 key 的并发上限;1 表示串行(推荐)'),
|
|
77
102
|
});
|
|
78
103
|
/**
|
|
@@ -107,12 +132,22 @@ const INERT_AUTH = {
|
|
|
107
132
|
},
|
|
108
133
|
};
|
|
109
134
|
export function apply(ctx, config) {
|
|
135
|
+
/**
|
|
136
|
+
* 配置文件里的端点(缺省 = 中国站,保证老用户升级后行为不变)。
|
|
137
|
+
*
|
|
138
|
+
* 运行期真正生效的端点是 `currentEndpoint`:它以凭据记录里的选择为准(用户在
|
|
139
|
+
* 设置卡片里切过),没有记录时回落到这里。
|
|
140
|
+
*/
|
|
141
|
+
const configEndpoint = endpointFor(config?.endpoint);
|
|
142
|
+
/**
|
|
143
|
+
* config 里显式写的 `rpmLimit`(≥0 才算显式);-1(默认)表示「按端点取默认值」。
|
|
144
|
+
*/
|
|
145
|
+
const explicitRpmLimit = typeof config?.rpmLimit === 'number' && config.rpmLimit >= 0 ? config.rpmLimit : null;
|
|
110
146
|
const resolved = {
|
|
111
|
-
baseUrl: config?.baseUrl
|
|
147
|
+
baseUrl: typeof config?.baseUrl === 'string' ? config.baseUrl.trim() : '',
|
|
112
148
|
maxAttempts: config?.maxAttempts ?? 6,
|
|
113
149
|
acquireTimeoutMs: config?.acquireTimeoutMs ?? 120_000,
|
|
114
150
|
requestTimeoutMs: config?.requestTimeoutMs ?? 60_000,
|
|
115
|
-
rpmLimit: config?.rpmLimit ?? 0,
|
|
116
151
|
maxConcurrency: config?.maxConcurrency ?? 1,
|
|
117
152
|
};
|
|
118
153
|
const logger = {
|
|
@@ -120,9 +155,30 @@ export function apply(ctx, config) {
|
|
|
120
155
|
warn: (message) => ctx.logger?.warn?.(`sensenova-pool: ${message}`),
|
|
121
156
|
error: (message) => ctx.logger?.error?.(`sensenova-pool: ${message}`),
|
|
122
157
|
};
|
|
158
|
+
/** 当前生效的端点。启动时取 config,装载凭据记录后可能被记录里的选择覆盖。 */
|
|
159
|
+
let currentEndpoint = configEndpoint;
|
|
160
|
+
/**
|
|
161
|
+
* 用户显式指定了 `baseUrl` 时不随端点切换(自建网关 / 代理的场景)。
|
|
162
|
+
*
|
|
163
|
+
* 这时「端点」只影响**内置基线与显示**,请求一律走用户给的地址。
|
|
164
|
+
*/
|
|
165
|
+
const baseUrlPinned = resolved.baseUrl.length > 0;
|
|
166
|
+
/** 所选端点对应的上游根地址。 */
|
|
167
|
+
function upstreamFor(endpoint) {
|
|
168
|
+
return baseUrlPinned ? resolved.baseUrl : endpoint.baseUrl;
|
|
169
|
+
}
|
|
170
|
+
/** 该端点下每把 key 的本地 RPM 上限(0 表示不限流)。 */
|
|
171
|
+
function rpmLimitFor(endpoint) {
|
|
172
|
+
return explicitRpmLimit ?? defaultRpmLimitFor(endpoint.id);
|
|
173
|
+
}
|
|
123
174
|
const catalog = new ModelCatalog();
|
|
175
|
+
// 回环服务按**端点**而不是地址区分:即使 baseUrl 被固定,切换端点也要换目录基线。
|
|
176
|
+
catalog.reset(currentEndpoint.id);
|
|
124
177
|
const pool = new KeyPool([], {
|
|
125
|
-
rpmLimit:
|
|
178
|
+
rpmLimit: (() => {
|
|
179
|
+
const limit = rpmLimitFor(currentEndpoint);
|
|
180
|
+
return limit > 0 ? limit : null;
|
|
181
|
+
})(),
|
|
126
182
|
maxConcurrency: resolved.maxConcurrency,
|
|
127
183
|
});
|
|
128
184
|
/**
|
|
@@ -169,7 +225,7 @@ export function apply(ctx, config) {
|
|
|
169
225
|
const shim = createShim({
|
|
170
226
|
pool,
|
|
171
227
|
catalog,
|
|
172
|
-
baseUrl:
|
|
228
|
+
baseUrl: upstreamFor(currentEndpoint),
|
|
173
229
|
rotation: {
|
|
174
230
|
maxAttempts: resolved.maxAttempts,
|
|
175
231
|
acquireTimeoutMs: resolved.acquireTimeoutMs,
|
|
@@ -197,6 +253,19 @@ export function apply(ctx, config) {
|
|
|
197
253
|
return { baseUrl: '', port: 0, apiKey: shim.token() };
|
|
198
254
|
}
|
|
199
255
|
},
|
|
256
|
+
upstreamEndpoint: () => ({
|
|
257
|
+
current: currentEndpoint.id,
|
|
258
|
+
upstream: shim.upstream(),
|
|
259
|
+
/** baseUrl 被 config 固定时,界面要提示「切端点只换目录/额度,不改地址」。 */
|
|
260
|
+
pinned: baseUrlPinned,
|
|
261
|
+
rpmLimit: rpmLimitFor(currentEndpoint),
|
|
262
|
+
options: ENDPOINTS.map((option) => ({
|
|
263
|
+
id: option.id,
|
|
264
|
+
label: option.label,
|
|
265
|
+
baseUrl: option.baseUrl,
|
|
266
|
+
active: option.id === currentEndpoint.id,
|
|
267
|
+
})),
|
|
268
|
+
}),
|
|
200
269
|
models: () => catalog.current().map((model) => {
|
|
201
270
|
const limits = limitsFor(model);
|
|
202
271
|
return {
|
|
@@ -213,7 +282,8 @@ export function apply(ctx, config) {
|
|
|
213
282
|
}),
|
|
214
283
|
providerId: PROVIDER_ID,
|
|
215
284
|
providerName: PROVIDER_NAME,
|
|
216
|
-
|
|
285
|
+
// 当前生效的上游根地址(不是回环地址):用于展示与单把 key 探活。
|
|
286
|
+
baseUrl: shim.upstream(),
|
|
217
287
|
};
|
|
218
288
|
// ---------------------------------------------------------------- 凭据装载
|
|
219
289
|
/**
|
|
@@ -312,6 +382,25 @@ export function apply(ctx, config) {
|
|
|
312
382
|
}
|
|
313
383
|
return added;
|
|
314
384
|
}
|
|
385
|
+
/**
|
|
386
|
+
* 应用一个"未来可能换端点"的选择(启动装载时用)。
|
|
387
|
+
*
|
|
388
|
+
* 与运行期的 {@link applyEndpoint} 同源,但这里不落盘、不刷新目录 —— 调用方
|
|
389
|
+
* (启动装载)随后会自己触发目录刷新,重复调用只会白白多打一次上游。
|
|
390
|
+
*/
|
|
391
|
+
function adoptEndpoint(id) {
|
|
392
|
+
if (id === currentEndpoint.id)
|
|
393
|
+
return false;
|
|
394
|
+
currentEndpoint = endpointFor(id);
|
|
395
|
+
catalog.reset(currentEndpoint.id);
|
|
396
|
+
shim.setUpstream(upstreamFor(currentEndpoint));
|
|
397
|
+
pool.setRpmLimit((() => {
|
|
398
|
+
const limit = rpmLimitFor(currentEndpoint);
|
|
399
|
+
return limit > 0 ? limit : null;
|
|
400
|
+
})());
|
|
401
|
+
pool.resetHealth();
|
|
402
|
+
return true;
|
|
403
|
+
}
|
|
315
404
|
/** 生成一把形如商汤 key 的对外密钥(只被本地 shim 校验,不发给上游)。 */
|
|
316
405
|
function newExternalKey() {
|
|
317
406
|
return `sk-dsh-${randomBytes(24).toString('hex')}`;
|
|
@@ -369,6 +458,10 @@ export function apply(ctx, config) {
|
|
|
369
458
|
externalKey = payload.externalKey;
|
|
370
459
|
shim.setToken(externalKey);
|
|
371
460
|
}
|
|
461
|
+
// 端点:记录里的选择优先于 config(用户在设置卡片里切过之后,重启也必须记住)。
|
|
462
|
+
// 记录里没有该字段 = 从未切过,保持 config 的端点,老用户行为不变。
|
|
463
|
+
if (isEndpointId(payload.endpoint))
|
|
464
|
+
adoptEndpoint(payload.endpoint);
|
|
372
465
|
if (added > 0)
|
|
373
466
|
logger.info(`已装载 ${added} 把 key`);
|
|
374
467
|
return added;
|
|
@@ -417,6 +510,8 @@ export function apply(ctx, config) {
|
|
|
417
510
|
// 只存用户真正改过的模型;没有覆盖的模型不出现在这里。
|
|
418
511
|
modelLimits: Object.fromEntries(modelLimits),
|
|
419
512
|
externalKey,
|
|
513
|
+
// 端点选择:只存认得出的 id,避免把脏值写进凭据库。
|
|
514
|
+
endpoint: currentEndpoint.id,
|
|
420
515
|
},
|
|
421
516
|
}));
|
|
422
517
|
}
|
|
@@ -643,7 +738,9 @@ export function apply(ctx, config) {
|
|
|
643
738
|
async function fetchCatalogFromUpstream() {
|
|
644
739
|
if (pool.size === 0)
|
|
645
740
|
return;
|
|
646
|
-
|
|
741
|
+
// 现取上游地址与基线:两次请求之间用户可能已经切换了端点。
|
|
742
|
+
const endpoint = currentEndpoint;
|
|
743
|
+
const url = `${shim.upstream().replace(/\/+$/, '')}/models`;
|
|
647
744
|
const attempted = new Set();
|
|
648
745
|
for (let index = 0; index < Math.min(pool.size + 1, 4); index += 1) {
|
|
649
746
|
let entry;
|
|
@@ -661,7 +758,10 @@ export function apply(ctx, config) {
|
|
|
661
758
|
});
|
|
662
759
|
if (response.ok) {
|
|
663
760
|
const payload = await response.json();
|
|
664
|
-
|
|
761
|
+
// 请求在途时端点被切走:这份响应对新端点没有意义,丢弃。
|
|
762
|
+
if (currentEndpoint.id !== endpoint.id)
|
|
763
|
+
return;
|
|
764
|
+
const count = catalog.apply(payload, builtinModelsFor(endpoint.id));
|
|
665
765
|
pool.reportSuccess(entry);
|
|
666
766
|
if (count > 0) {
|
|
667
767
|
logger.info(`模型目录已更新:${count} 个模型`);
|
|
@@ -740,6 +840,38 @@ export function apply(ctx, config) {
|
|
|
740
840
|
logger.warn(`模型窗口设置落盘失败(本次会话已生效):${error instanceof Error ? error.message : String(error)}`);
|
|
741
841
|
}
|
|
742
842
|
};
|
|
843
|
+
/**
|
|
844
|
+
* 切换上游端点(中国站 ⇄ 国际站)。
|
|
845
|
+
*
|
|
846
|
+
* 这是「换一个站继续用」而不是「改个地址」:两个站的 key、模型、限流额度都
|
|
847
|
+
* 不同,所以四件事必须一起做,缺一件都会让状态变得自相矛盾:
|
|
848
|
+
*
|
|
849
|
+
* 1. **上游地址** —— 之后所有请求打到新站;
|
|
850
|
+
* 2. **模型目录** —— 退回新站的内置基线(旧站的模型列表在新站上多数是 404);
|
|
851
|
+
* 3. **RPM 额度** —— 新站有自己的额度(中国站实测约 3 RPM,国际站另计);
|
|
852
|
+
* 4. **key 健康状态** —— 冷却/失效是针对旧站记下的,带过去会让切站后几分钟
|
|
853
|
+
* 内一把 key 都用不了。
|
|
854
|
+
*
|
|
855
|
+
* 落盘顺序:先写凭据记录再刷新模型选择器 —— 刷新会让 DSH 立刻重读模型,
|
|
856
|
+
* 若此时记录还没写成功,用户会看到「切换了但重启又回到原来那个站」。
|
|
857
|
+
* 落盘失败不回滚:内存里已经切过去了,会话可用性优先,下次写入会一并带上。
|
|
858
|
+
*/
|
|
859
|
+
const handleEndpointChanged = async (id) => {
|
|
860
|
+
if (!isEndpointId(id))
|
|
861
|
+
throw new Error(`未知端点:${id}`);
|
|
862
|
+
if (id === currentEndpoint.id)
|
|
863
|
+
return;
|
|
864
|
+
adoptEndpoint(id);
|
|
865
|
+
logger.info(`已切换上游端点到「${currentEndpoint.label}」(${shim.upstream()})`);
|
|
866
|
+
try {
|
|
867
|
+
await saveKeys();
|
|
868
|
+
}
|
|
869
|
+
catch (error) {
|
|
870
|
+
logger.warn(`端点选择落盘失败(本次会话已生效):${error instanceof Error ? error.message : String(error)}`);
|
|
871
|
+
}
|
|
872
|
+
refreshCatalog();
|
|
873
|
+
void fetchCatalogFromUpstream();
|
|
874
|
+
};
|
|
743
875
|
// 设置页:填 key(用户明确要求只在设置页填)。
|
|
744
876
|
try {
|
|
745
877
|
registerSettingsSection(ctx, {
|
|
@@ -761,6 +893,7 @@ export function apply(ctx, config) {
|
|
|
761
893
|
onKeysChanged: handleKeysChanged,
|
|
762
894
|
onModelsChanged: handleModelsChanged,
|
|
763
895
|
onModelLimitsChanged: handleModelLimitsChanged,
|
|
896
|
+
onEndpointChanged: handleEndpointChanged,
|
|
764
897
|
reload: async () => {
|
|
765
898
|
// 手动重载:走同一套「装载并宣告」逻辑,因此在启动早期点它也不会把
|
|
766
899
|
// 「还没读到」误当成「真的没有 key」而清空权威装载信号。
|
package/lib/keypool.d.ts
CHANGED
|
@@ -171,6 +171,26 @@ export declare class KeyPool {
|
|
|
171
171
|
invalid: number;
|
|
172
172
|
inflight: number;
|
|
173
173
|
};
|
|
174
|
+
/**
|
|
175
|
+
* 调整本地 RPM 预限流上限(运行期可改)。
|
|
176
|
+
*
|
|
177
|
+
* 为什么要它:两个端点(中国站 / 国际站)的限流额度并不相同,而插件允许在
|
|
178
|
+
* 设置卡片里切换端点。若不随之调整,换站只会换地址,额度仍压在旧值上。
|
|
179
|
+
* 传 null 表示不限制。
|
|
180
|
+
*/
|
|
181
|
+
setRpmLimit(limit: number | null): void;
|
|
182
|
+
get rpmLimitValue(): number | null;
|
|
183
|
+
/** 调整每把 key 的并发上限(运行期可改)。 */
|
|
184
|
+
setMaxConcurrency(value: number): void;
|
|
185
|
+
get maxConcurrencyValue(): number;
|
|
186
|
+
/**
|
|
187
|
+
* 清空冷却与失效标记,重新给每把 key 一次机会。
|
|
188
|
+
*
|
|
189
|
+
* 切换端点时使用:冷却/失效是针对旧端点记下的上游行为,新端点的额度与套餐
|
|
190
|
+
* 都不同,带着旧标记进新站会让刚切过去的几分钟内一把 key 都用不了。
|
|
191
|
+
* key 本身与累计统计(请求数 / 成功率)保留 —— 那些是事实,不该被抹掉。
|
|
192
|
+
*/
|
|
193
|
+
resetHealth(): number;
|
|
174
194
|
/** 当前是否有任何 key 立即可用(不等待)。 */
|
|
175
195
|
anyUsable(): boolean;
|
|
176
196
|
}
|
package/lib/keypool.js
CHANGED
|
@@ -431,6 +431,61 @@ export class KeyPool {
|
|
|
431
431
|
inflight: this.entries.reduce((sum, entry) => sum + entry.inflight, 0),
|
|
432
432
|
};
|
|
433
433
|
}
|
|
434
|
+
/**
|
|
435
|
+
* 调整本地 RPM 预限流上限(运行期可改)。
|
|
436
|
+
*
|
|
437
|
+
* 为什么要它:两个端点(中国站 / 国际站)的限流额度并不相同,而插件允许在
|
|
438
|
+
* 设置卡片里切换端点。若不随之调整,换站只会换地址,额度仍压在旧值上。
|
|
439
|
+
* 传 null 表示不限制。
|
|
440
|
+
*/
|
|
441
|
+
setRpmLimit(limit) {
|
|
442
|
+
const next = limit !== null && limit > 0 ? limit : null;
|
|
443
|
+
if (next === this.rpmLimit)
|
|
444
|
+
return;
|
|
445
|
+
this.rpmLimit = next;
|
|
446
|
+
// 窗口里的历史时间戳保持原样:它们代表真实发过的请求,放宽后立刻生效,
|
|
447
|
+
// 收紧后由 rpmBlockedUntil 自然消化。
|
|
448
|
+
this.notify();
|
|
449
|
+
}
|
|
450
|
+
get rpmLimitValue() {
|
|
451
|
+
return this.rpmLimit;
|
|
452
|
+
}
|
|
453
|
+
/** 调整每把 key 的并发上限(运行期可改)。 */
|
|
454
|
+
setMaxConcurrency(value) {
|
|
455
|
+
const next = Math.max(1, Math.trunc(Number.isFinite(value) ? value : 1));
|
|
456
|
+
if (next === this.maxConcurrency)
|
|
457
|
+
return;
|
|
458
|
+
this.maxConcurrency = next;
|
|
459
|
+
this.notify();
|
|
460
|
+
}
|
|
461
|
+
get maxConcurrencyValue() {
|
|
462
|
+
return this.maxConcurrency;
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* 清空冷却与失效标记,重新给每把 key 一次机会。
|
|
466
|
+
*
|
|
467
|
+
* 切换端点时使用:冷却/失效是针对旧端点记下的上游行为,新端点的额度与套餐
|
|
468
|
+
* 都不同,带着旧标记进新站会让刚切过去的几分钟内一把 key 都用不了。
|
|
469
|
+
* key 本身与累计统计(请求数 / 成功率)保留 —— 那些是事实,不该被抹掉。
|
|
470
|
+
*/
|
|
471
|
+
resetHealth() {
|
|
472
|
+
let reset = 0;
|
|
473
|
+
for (const entry of this.entries) {
|
|
474
|
+
if (entry.status === 'healthy' &&
|
|
475
|
+
entry.cooldownUntil === 0 &&
|
|
476
|
+
entry.consecutiveFailures === 0) {
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
entry.status = 'healthy';
|
|
480
|
+
entry.cooldownUntil = 0;
|
|
481
|
+
entry.consecutiveFailures = 0;
|
|
482
|
+
entry.lastError = '';
|
|
483
|
+
reset += 1;
|
|
484
|
+
}
|
|
485
|
+
if (reset > 0)
|
|
486
|
+
this.notify();
|
|
487
|
+
return reset;
|
|
488
|
+
}
|
|
434
489
|
/** 当前是否有任何 key 立即可用(不等待)。 */
|
|
435
490
|
anyUsable() {
|
|
436
491
|
const now = Date.now();
|
package/lib/routes.d.ts
CHANGED
|
@@ -40,12 +40,14 @@ export interface StatusProvider {
|
|
|
40
40
|
message: string;
|
|
41
41
|
at: number;
|
|
42
42
|
}>;
|
|
43
|
-
/** 本地 shim
|
|
43
|
+
/** 本地 shim 的对外接入信息(回环地址、端口、对外 API Key)。 */
|
|
44
44
|
endpoint(): {
|
|
45
45
|
baseUrl: string;
|
|
46
46
|
port: number;
|
|
47
47
|
apiKey: string;
|
|
48
48
|
};
|
|
49
|
+
/** 上游端点的选择状态(中国站 / 国际站)。 */
|
|
50
|
+
upstreamEndpoint(): EndpointStatus;
|
|
49
51
|
/** 目录里的全部模型 + 是否在下拉中展示 + 生效的窗口/输出上限。 */
|
|
50
52
|
models(): Array<{
|
|
51
53
|
id: string;
|
|
@@ -64,8 +66,28 @@ export interface StatusProvider {
|
|
|
64
66
|
}>;
|
|
65
67
|
providerId: string;
|
|
66
68
|
providerName: string;
|
|
69
|
+
/** 当前生效的上游根地址(会随端点切换而变;单把 key 探活打到这里)。 */
|
|
67
70
|
baseUrl: string;
|
|
68
71
|
}
|
|
72
|
+
export interface EndpointOption {
|
|
73
|
+
id: string;
|
|
74
|
+
/** 展示名(中国站 / 国际站)。 */
|
|
75
|
+
label: string;
|
|
76
|
+
/** 该端点的官方根地址。 */
|
|
77
|
+
baseUrl: string;
|
|
78
|
+
active: boolean;
|
|
79
|
+
}
|
|
80
|
+
export interface EndpointStatus {
|
|
81
|
+
/** 当前选中的端点 id。 */
|
|
82
|
+
current: string;
|
|
83
|
+
/** 当前真实的上游地址(config 固定了 baseUrl 时与所选端点的官方地址不同)。 */
|
|
84
|
+
upstream: string;
|
|
85
|
+
/** config 里写死了 baseUrl —— 切端点只换目录与额度,不改地址。 */
|
|
86
|
+
pinned: boolean;
|
|
87
|
+
/** 当前端点下每把 key 的本地 RPM 上限(0 = 不限流)。 */
|
|
88
|
+
rpmLimit: number;
|
|
89
|
+
options: EndpointOption[];
|
|
90
|
+
}
|
|
69
91
|
interface Logger {
|
|
70
92
|
info(message: string): void;
|
|
71
93
|
warn(message: string): void;
|
|
@@ -84,6 +106,13 @@ export interface RouteDeps {
|
|
|
84
106
|
contextWindow?: number;
|
|
85
107
|
maxTokens?: number;
|
|
86
108
|
}): Promise<void>;
|
|
109
|
+
/**
|
|
110
|
+
* 上游端点发生变更('cn' | 'intl')。
|
|
111
|
+
*
|
|
112
|
+
* 实现方负责落盘、切换上游地址、换模型目录与重置 RPM 额度;未知 id 应抛错
|
|
113
|
+
* (路由层据此回 400)。
|
|
114
|
+
*/
|
|
115
|
+
onEndpointChanged(id: string): Promise<void>;
|
|
87
116
|
/** 重新从凭据库装载。 */
|
|
88
117
|
reload(): Promise<void>;
|
|
89
118
|
/** 主动拉取上游模型目录。 */
|
package/lib/routes.js
CHANGED
|
@@ -186,6 +186,8 @@ export function registerKeyRoutes(ctx, deps) {
|
|
|
186
186
|
baseUrl: deps.status.baseUrl,
|
|
187
187
|
// 对外接入信息:地址 / 端口 / API Key,供卡片展示并可复制到别的服务。
|
|
188
188
|
endpoint: deps.status.endpoint(),
|
|
189
|
+
// 上游端点选择:卡片据此渲染「中国站 / 国际站」切换。
|
|
190
|
+
upstreamEndpoint: deps.status.upstreamEndpoint(),
|
|
189
191
|
summary: deps.status.summary(),
|
|
190
192
|
catalog: deps.status.catalog(),
|
|
191
193
|
models: deps.status.models(),
|
|
@@ -317,6 +319,33 @@ export function registerKeyRoutes(ctx, deps) {
|
|
|
317
319
|
sendJson(res, 200, { revived, keys: deps.status.pool() });
|
|
318
320
|
return;
|
|
319
321
|
}
|
|
322
|
+
// ---- 上游端点:中国站 ⇄ 国际站 -----------------------------------
|
|
323
|
+
//
|
|
324
|
+
// 两个站的 key 互不通用,选错端点只会得到 401;因此这里是**用户显式选择**,
|
|
325
|
+
// 不做「自动探测」。切换的连带效果(上游地址、模型目录、RPM 额度、key 冷却
|
|
326
|
+
// 状态)全部由 onEndpointChanged 负责,路由只做参数校验。
|
|
327
|
+
if (action === 'set-endpoint') {
|
|
328
|
+
const id = typeof body?.id === 'string' ? body.id : '';
|
|
329
|
+
const known = deps.status.upstreamEndpoint().options.map((option) => option.id);
|
|
330
|
+
if (!known.includes(id)) {
|
|
331
|
+
sendJson(res, 400, { error: `未知端点:${id || '(空)'},可选:${known.join(' / ')}` });
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
try {
|
|
335
|
+
await deps.onEndpointChanged(id);
|
|
336
|
+
}
|
|
337
|
+
catch (error) {
|
|
338
|
+
sendJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
sendJson(res, 200, {
|
|
342
|
+
upstreamEndpoint: deps.status.upstreamEndpoint(),
|
|
343
|
+
// 换站后模型目录整批换掉,一并回传给卡片。
|
|
344
|
+
models: deps.status.models(),
|
|
345
|
+
catalog: deps.status.catalog(),
|
|
346
|
+
});
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
320
349
|
if (action === 'refresh-catalog') {
|
|
321
350
|
await deps.fetchCatalog();
|
|
322
351
|
sendJson(res, 200, { catalog: deps.status.catalog() });
|
|
@@ -348,6 +377,7 @@ export function registerKeyRoutes(ctx, deps) {
|
|
|
348
377
|
sendJson(res, 404, { error: '找不到这把 key(可能已被删除)' });
|
|
349
378
|
return;
|
|
350
379
|
}
|
|
380
|
+
// 现取上游地址:用户可能刚切换过端点。
|
|
351
381
|
const base = deps.status.baseUrl.replace(/\/+$/, '');
|
|
352
382
|
const started = Date.now();
|
|
353
383
|
try {
|
|
@@ -355,7 +385,11 @@ export function registerKeyRoutes(ctx, deps) {
|
|
|
355
385
|
method: 'POST',
|
|
356
386
|
headers: { 'content-type': 'application/json', authorization: `Bearer ${candidate.key}` },
|
|
357
387
|
body: JSON.stringify({
|
|
358
|
-
|
|
388
|
+
// 默认探活模型取自**当前端点**的目录:国际站没有 `deepseek-v4-flash`,
|
|
389
|
+
// 写死它会让每一次国际站探活都变成 404「模型不存在」,明明是 key 好用的。
|
|
390
|
+
model: typeof body?.model === 'string' && body.model
|
|
391
|
+
? body.model
|
|
392
|
+
: deps.status.models().find((model) => model.enabled)?.id ?? 'deepseek-v4-flash',
|
|
359
393
|
messages: [{ role: 'user', content: 'ping' }],
|
|
360
394
|
max_tokens: 1,
|
|
361
395
|
}),
|
|
@@ -418,9 +452,16 @@ export function registerSettingsSection(ctx, deps) {
|
|
|
418
452
|
default: [],
|
|
419
453
|
description: '商汤日日新 API Key(可多把,遇 429 自动轮换)',
|
|
420
454
|
},
|
|
455
|
+
// 端点也放进来:卡片里切换后会写回凭据记录,但配置文件里显式写的那一份
|
|
456
|
+
// (config.endpoint)应当能在设置里一眼看到,避免两处说法不一致。
|
|
457
|
+
endpoint: {
|
|
458
|
+
type: 'string',
|
|
459
|
+
default: 'cn',
|
|
460
|
+
description: '上游端点:cn = 中国站(token.sensenova.cn),intl = 国际站(token.sensenova.ai)',
|
|
461
|
+
},
|
|
421
462
|
};
|
|
422
463
|
try {
|
|
423
|
-
settings.installSection(ctx, deps.namespace, schema, { keys: [] }, {
|
|
464
|
+
settings.installSection(ctx, deps.namespace, schema, { keys: [], endpoint: 'cn' }, {
|
|
424
465
|
onChange: () => {
|
|
425
466
|
void deps.onKeysChanged();
|
|
426
467
|
},
|
package/lib/shim.d.ts
CHANGED
|
@@ -36,13 +36,26 @@ export declare class ModelCatalog {
|
|
|
36
36
|
fetchedAt: number;
|
|
37
37
|
error: string;
|
|
38
38
|
};
|
|
39
|
-
/**
|
|
40
|
-
|
|
39
|
+
/**
|
|
40
|
+
* 用一次成功的 `/v1/models` 响应替换目录;空结果视为失败(不覆盖基线)。
|
|
41
|
+
*
|
|
42
|
+
* `baselines` 必须与**发起这次请求的端点**一致(见 {@link builtinModelsFor})。
|
|
43
|
+
*/
|
|
44
|
+
apply(payload: unknown, baselines?: readonly SenseNovaModel[]): number;
|
|
41
45
|
fail(message: string): void;
|
|
46
|
+
/**
|
|
47
|
+
* 丢开已拉取的目录,退回某个端点的内置基线。
|
|
48
|
+
*
|
|
49
|
+
* 切换端点时必须调用:上一个站拉回来的模型列表对新站毫无意义(国际站没有
|
|
50
|
+
* `glm-5.2`、`deepseek-v4-flash` 这些模型),继续展示只会让用户选到必然 404
|
|
51
|
+
* 的模型。退回基线后,新站的有效 key 会立刻触发一次刷新。
|
|
52
|
+
*/
|
|
53
|
+
reset(endpointId: string): void;
|
|
42
54
|
}
|
|
43
55
|
export interface ShimOptions {
|
|
44
56
|
pool: KeyPool;
|
|
45
57
|
catalog: ModelCatalog;
|
|
58
|
+
/** 上游 OpenAI 兼容根地址(含 `/v1`)。 */
|
|
46
59
|
baseUrl: string;
|
|
47
60
|
rotation?: Partial<RotationOptions>;
|
|
48
61
|
logger?: {
|
|
@@ -74,6 +87,15 @@ export interface Shim {
|
|
|
74
87
|
* 请求不该因此 401。持久化的密钥在启动装载完成后才拿到,正是走这条路径。
|
|
75
88
|
*/
|
|
76
89
|
setToken(next: string): void;
|
|
90
|
+
/** 当前上游根地址(切换端点后即为新值)。 */
|
|
91
|
+
upstream(): string;
|
|
92
|
+
/**
|
|
93
|
+
* 切换上游端点(中国站 ⇄ 国际站)。
|
|
94
|
+
*
|
|
95
|
+
* 只换「请求发往哪里」:回环端口、对外密钥、key 池都不动。切换后是否重置目录
|
|
96
|
+
* 由调用方决定(它才知道新端点该退回哪套基线)。
|
|
97
|
+
*/
|
|
98
|
+
setUpstream(next: string): void;
|
|
77
99
|
close(): Promise<void>;
|
|
78
100
|
}
|
|
79
101
|
/** 起一个只监听 127.0.0.1 的 OpenAI 兼容服务。 */
|
package/lib/shim.js
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
|
20
20
|
import { createServer } from 'node:http';
|
|
21
21
|
import { Readable } from 'node:stream';
|
|
22
|
-
import { BUILTIN_MODELS, DEFAULT_BASE_URL, parseModelCatalog } from './catalog.js';
|
|
22
|
+
import { BUILTIN_MODELS, DEFAULT_BASE_URL, builtinModelsFor, parseModelCatalog, } from './catalog.js';
|
|
23
23
|
import { DEFAULT_ROTATION, requestWithRotation } from './rotate.js';
|
|
24
24
|
import { sanitizeChatRequest } from './sanitize.js';
|
|
25
25
|
// --------------------------------------------------------------------- 目录
|
|
@@ -44,9 +44,13 @@ export class ModelCatalog {
|
|
|
44
44
|
error: this.lastError,
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
|
-
/**
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
/**
|
|
48
|
+
* 用一次成功的 `/v1/models` 响应替换目录;空结果视为失败(不覆盖基线)。
|
|
49
|
+
*
|
|
50
|
+
* `baselines` 必须与**发起这次请求的端点**一致(见 {@link builtinModelsFor})。
|
|
51
|
+
*/
|
|
52
|
+
apply(payload, baselines = BUILTIN_MODELS) {
|
|
53
|
+
const parsed = parseModelCatalog(payload, baselines);
|
|
50
54
|
if (parsed.length === 0)
|
|
51
55
|
return 0;
|
|
52
56
|
this.models = parsed;
|
|
@@ -57,11 +61,25 @@ export class ModelCatalog {
|
|
|
57
61
|
fail(message) {
|
|
58
62
|
this.lastError = message;
|
|
59
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* 丢开已拉取的目录,退回某个端点的内置基线。
|
|
66
|
+
*
|
|
67
|
+
* 切换端点时必须调用:上一个站拉回来的模型列表对新站毫无意义(国际站没有
|
|
68
|
+
* `glm-5.2`、`deepseek-v4-flash` 这些模型),继续展示只会让用户选到必然 404
|
|
69
|
+
* 的模型。退回基线后,新站的有效 key 会立刻触发一次刷新。
|
|
70
|
+
*/
|
|
71
|
+
reset(endpointId) {
|
|
72
|
+
this.models = [...builtinModelsFor(endpointId)];
|
|
73
|
+
this.lastFetchedAt = 0;
|
|
74
|
+
this.lastError = '';
|
|
75
|
+
}
|
|
60
76
|
}
|
|
61
77
|
/** 起一个只监听 127.0.0.1 的 OpenAI 兼容服务。 */
|
|
62
78
|
export function createShim(options) {
|
|
63
79
|
const { pool, catalog } = options;
|
|
64
80
|
const rotation = { ...DEFAULT_ROTATION, ...options.rotation };
|
|
81
|
+
// 上游地址可变:用户在设置卡片里切换端点后立刻生效,无需重启 DSH。
|
|
82
|
+
let upstreamBaseUrl = options.baseUrl;
|
|
65
83
|
// 每进程随机共享密钥:DSH 用 Bearer 带上它,别的本地进程猜不到。
|
|
66
84
|
// 启动装载完成后会被凭据库里那把持久化密钥替换(见 setToken),以便外部服务
|
|
67
85
|
// 配置一次就能长期使用。
|
|
@@ -151,7 +169,8 @@ export function createShim(options) {
|
|
|
151
169
|
options.logger?.info(`sensenova-pool: 已规范化请求体字段 ${changes.join(', ')}`);
|
|
152
170
|
}
|
|
153
171
|
const wantsStream = body.stream === true;
|
|
154
|
-
|
|
172
|
+
// 每次请求现算:端点可能在两次请求之间被切换。
|
|
173
|
+
const upstreamUrl = `${upstreamBaseUrl.replace(/\/+$/, '')}/chat/completions`;
|
|
155
174
|
// 关键:轮换发生在「拿到首个响应」之前,因此对上层完全透明。
|
|
156
175
|
const controller = new AbortController();
|
|
157
176
|
req.on('close', () => controller.abort());
|
|
@@ -236,6 +255,13 @@ export function createShim(options) {
|
|
|
236
255
|
baseUrl,
|
|
237
256
|
port: () => listeningAddress().port,
|
|
238
257
|
token: () => primaryToken,
|
|
258
|
+
upstream: () => upstreamBaseUrl,
|
|
259
|
+
setUpstream: (next) => {
|
|
260
|
+
if (typeof next !== 'string' || next.length === 0 || next === upstreamBaseUrl)
|
|
261
|
+
return;
|
|
262
|
+
upstreamBaseUrl = next;
|
|
263
|
+
options.logger?.info(`sensenova-pool: 上游端点已切换为 ${next}`);
|
|
264
|
+
},
|
|
239
265
|
setToken: (next) => {
|
|
240
266
|
if (typeof next !== 'string' || next.length < 8 || next === primaryToken)
|
|
241
267
|
return;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "dsh-sensenova-pool",
|
|
3
3
|
"displayName": "DSH SenseNova Key Pool",
|
|
4
4
|
"description": "商汤日日新(SenseNova)多 Key 轮换池 — 填入多把 Key 即可零配置接入 DeepSeek Harness,遇 429 限流自动切换下一把 Key。",
|
|
5
|
-
"version": "1.
|
|
5
|
+
"version": "1.1.0",
|
|
6
6
|
"author": "Phoeky",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"dsh-plugin",
|
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
"deepseek",
|
|
11
11
|
"sensenova",
|
|
12
12
|
"日日新",
|
|
13
|
+
"sensenova.ai",
|
|
14
|
+
"国际站",
|
|
13
15
|
"key-rotation",
|
|
14
16
|
"key-pool",
|
|
15
17
|
"rate-limit",
|
|
@@ -35,6 +37,7 @@
|
|
|
35
37
|
"lib",
|
|
36
38
|
"cordis.patch.yml",
|
|
37
39
|
"README.md",
|
|
40
|
+
"CHANGELOG.md",
|
|
38
41
|
"LICENSE"
|
|
39
42
|
],
|
|
40
43
|
"license": "MIT",
|