dsh-sensenova-pool 0.1.3 → 0.2.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 +349 -284
- package/lib/client.js +297 -73
- package/lib/index.js +146 -31
- package/lib/routes.d.ts +16 -0
- package/lib/routes.js +26 -0
- package/lib/shim.d.ts +9 -0
- package/lib/shim.js +36 -9
- package/package.json +79 -78
package/lib/index.js
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* 好处是「屏蔽 429」这件事被关在一个我们完全掌控的小盒子里:上层看到的永远是
|
|
16
16
|
* 一个稳定的 OpenAI 兼容端点,429 在盒子内部被消化掉。
|
|
17
17
|
*/
|
|
18
|
+
import { randomBytes } from 'node:crypto';
|
|
18
19
|
import { PiAiAdapter } from '@deepseek-ai/dsh-llm-pi-ai';
|
|
19
20
|
import { resolveRetryPolicy } from '@deepseek-ai/dsh-llm';
|
|
20
21
|
import { createProvider } from '@earendil-works/pi-ai';
|
|
@@ -124,6 +125,22 @@ export function apply(ctx, config) {
|
|
|
124
125
|
rpmLimit: resolved.rpmLimit > 0 ? resolved.rpmLimit : null,
|
|
125
126
|
maxConcurrency: resolved.maxConcurrency,
|
|
126
127
|
});
|
|
128
|
+
/**
|
|
129
|
+
* 被用户隐藏的模型 id。
|
|
130
|
+
*
|
|
131
|
+
* 「下拉里展示什么」是用户自己的偏好,与 key 一样跨重启保留:勾掉的模型不进入
|
|
132
|
+
* `buildModels()`,因此不会出现在模型选择器里(上游仍然可用,只是不展示)。
|
|
133
|
+
*/
|
|
134
|
+
const disabledModels = new Set();
|
|
135
|
+
/**
|
|
136
|
+
* 对外暴露的 API Key(别的服务用它访问本地 shim)。
|
|
137
|
+
*
|
|
138
|
+
* 持久化在凭据记录里,重启后保持不变 —— 否则每次启动都换一把,外部服务就得
|
|
139
|
+
* 反复改配置。启动装载完成前它是空串,此时 shim 用的是进程内随机密钥。
|
|
140
|
+
*/
|
|
141
|
+
let externalKey = '';
|
|
142
|
+
/** 凭据库里是否**已有**本插件的记录(决定首次装载后要不要立刻补写)。 */
|
|
143
|
+
let recordExisted = false;
|
|
127
144
|
/** 最近的轮换事件,供状态页展示「刚才发生了什么」。 */
|
|
128
145
|
const recentRotations = [];
|
|
129
146
|
const shim = createShim({
|
|
@@ -148,17 +165,37 @@ export function apply(ctx, config) {
|
|
|
148
165
|
summary: () => pool.summary(),
|
|
149
166
|
catalog: () => catalog.status(),
|
|
150
167
|
rotations: () => recentRotations.slice(0, 20),
|
|
168
|
+
endpoint: () => {
|
|
169
|
+
// shim 监听失败时 baseUrl() 会抛;这里降级成空串,让界面显示「不可用」而不是崩。
|
|
170
|
+
try {
|
|
171
|
+
return { baseUrl: `${shim.baseUrl()}/v1`, port: shim.port(), apiKey: shim.token() };
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
return { baseUrl: '', port: 0, apiKey: shim.token() };
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
models: () => catalog.current().map((model) => ({
|
|
178
|
+
id: model.id,
|
|
179
|
+
name: model.name,
|
|
180
|
+
contextWindow: model.contextWindow,
|
|
181
|
+
maxTokens: model.maxTokens,
|
|
182
|
+
enabled: !disabledModels.has(model.id),
|
|
183
|
+
})),
|
|
151
184
|
providerId: PROVIDER_ID,
|
|
152
185
|
providerName: PROVIDER_NAME,
|
|
153
186
|
baseUrl: resolved.baseUrl,
|
|
154
187
|
};
|
|
155
188
|
// ---------------------------------------------------------------- 凭据装载
|
|
156
189
|
/**
|
|
157
|
-
* 从 DSH
|
|
190
|
+
* 从 DSH 凭据库读取本插件的状态。
|
|
158
191
|
*
|
|
159
|
-
* 存储方案:一个 `<scope>/keys` 记录,payload 里放 key
|
|
160
|
-
*
|
|
161
|
-
*
|
|
192
|
+
* 存储方案:一个 `<scope>/keys` 记录,payload 里放 key 数组、被隐藏的模型 id,
|
|
193
|
+
* 以及对外暴露的 API Key。用记录而不是环境变量引用,是因为用户要求「可以填多把
|
|
194
|
+
* key」——环境变量引用是单值的,无法表达一个池子。
|
|
195
|
+
*
|
|
196
|
+
* 为什么把「模型可见性」和「对外密钥」也塞进这条记录:DSH 的 settings 服务是
|
|
197
|
+
* 围绕插件 Config + profile patch 的配置编辑器,不是通用 KV;为这两个字段单开
|
|
198
|
+
* 一套持久化路径不值得。grant 的 payload 是**逐字保留**的,多几个字段完全安全。
|
|
162
199
|
*/
|
|
163
200
|
const RECORD_SCOPE = 'dsh-sensenova-pool';
|
|
164
201
|
const RECORD_ID = `${RECORD_SCOPE}/keys`;
|
|
@@ -217,17 +254,38 @@ export function apply(ctx, config) {
|
|
|
217
254
|
notifyKeysLoaded = resolve;
|
|
218
255
|
});
|
|
219
256
|
/**
|
|
220
|
-
* 装载 key
|
|
257
|
+
* 装载 key、模型可见性与对外密钥,并在**凭据快照确实可读**时宣告首次权威装载完成。
|
|
221
258
|
*
|
|
222
259
|
* 只有凭据服务可见时才宣告:否则「池子为空」只代表「还没读到」,不代表「真的没有
|
|
223
260
|
* key」,提前宣告会让 `saveKeys()` 把空池覆盖写回记录。
|
|
224
261
|
*/
|
|
225
262
|
async function loadKeysAndNotify() {
|
|
226
263
|
const added = await loadKeys();
|
|
227
|
-
if (credentialsService() !== undefined)
|
|
264
|
+
if (credentialsService() !== undefined) {
|
|
265
|
+
// 记录里还没有对外密钥时生成一把。是否立刻落盘取决于记录**原本是否存在**:
|
|
266
|
+
// - 存在(老用户 / 已用过插件)→ 补写一次,把这把密钥固化下来;
|
|
267
|
+
// - 不存在(首次安装)→ 只放在内存里,等用户第一次添加 key 时随那次写入
|
|
268
|
+
// 一起落盘,避免仅为一把尚未使用的密钥就在凭据库里建记录。
|
|
269
|
+
if (!externalKey) {
|
|
270
|
+
externalKey = newExternalKey();
|
|
271
|
+
shim.setToken(externalKey);
|
|
272
|
+
if (recordExisted) {
|
|
273
|
+
try {
|
|
274
|
+
await persistRecord();
|
|
275
|
+
}
|
|
276
|
+
catch (error) {
|
|
277
|
+
logger.warn(`对外密钥落盘失败(本次会话仍可用):${error instanceof Error ? error.message : String(error)}`);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
228
281
|
notifyKeysLoaded?.();
|
|
282
|
+
}
|
|
229
283
|
return added;
|
|
230
284
|
}
|
|
285
|
+
/** 生成一把形如商汤 key 的对外密钥(只被本地 shim 校验,不发给上游)。 */
|
|
286
|
+
function newExternalKey() {
|
|
287
|
+
return `sk-dsh-${randomBytes(24).toString('hex')}`;
|
|
288
|
+
}
|
|
231
289
|
async function loadKeys() {
|
|
232
290
|
const credentials = credentialsService();
|
|
233
291
|
if (credentials === undefined) {
|
|
@@ -242,12 +300,27 @@ export function apply(ctx, config) {
|
|
|
242
300
|
logger.warn(`读取 key 记录失败:${error instanceof Error ? error.message : String(error)}`);
|
|
243
301
|
return 0;
|
|
244
302
|
}
|
|
245
|
-
const
|
|
303
|
+
const payload = record?.payload ?? {};
|
|
304
|
+
recordExisted = record !== undefined && record !== null;
|
|
305
|
+
const keys = Array.isArray(payload.keys) ? payload.keys : [];
|
|
246
306
|
let added = 0;
|
|
247
307
|
for (const raw of keys) {
|
|
248
308
|
if (typeof raw === 'string' && pool.addKey(raw))
|
|
249
309
|
added += 1;
|
|
250
310
|
}
|
|
311
|
+
// 模型可见性:记录里没有该字段时表示「全部展示」。
|
|
312
|
+
disabledModels.clear();
|
|
313
|
+
if (Array.isArray(payload.disabledModels)) {
|
|
314
|
+
for (const id of payload.disabledModels) {
|
|
315
|
+
if (typeof id === 'string' && id.length > 0)
|
|
316
|
+
disabledModels.add(id);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
// 对外密钥:拿到持久化的那把后立刻换上,DSH 与外部服务从此用同一个。
|
|
320
|
+
if (typeof payload.externalKey === 'string' && payload.externalKey.length >= 16) {
|
|
321
|
+
externalKey = payload.externalKey;
|
|
322
|
+
shim.setToken(externalKey);
|
|
323
|
+
}
|
|
251
324
|
if (added > 0)
|
|
252
325
|
logger.info(`已装载 ${added} 把 key`);
|
|
253
326
|
return added;
|
|
@@ -264,19 +337,16 @@ export function apply(ctx, config) {
|
|
|
264
337
|
timer?.unref?.();
|
|
265
338
|
});
|
|
266
339
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
await Promise.race([firstAuthoritativeLoad, delay(SAVE_LOAD_WAIT_MS)]);
|
|
275
|
-
}
|
|
340
|
+
/**
|
|
341
|
+
* 把当前内存状态整体写回记录(**不等待**权威装载)。
|
|
342
|
+
*
|
|
343
|
+
* 只给两条路径用:启动装载完成后的补写(此时快照刚读过,权威),以及已经等过
|
|
344
|
+
* `firstAuthoritativeLoad` 的 `saveKeys()`。别在启动早期直接调它。
|
|
345
|
+
*/
|
|
346
|
+
async function persistRecord() {
|
|
276
347
|
const credentials = credentialsService();
|
|
277
348
|
if (credentials === undefined)
|
|
278
|
-
throw new Error('凭据服务不可用,无法保存
|
|
279
|
-
const keys = pool.keys();
|
|
349
|
+
throw new Error('凭据服务不可用,无法保存');
|
|
280
350
|
// 必须用 `grant`,不能用 `api-key`。
|
|
281
351
|
//
|
|
282
352
|
// credentials-local 对记录做白名单校验(`assertFields`),两种标签允许的字段
|
|
@@ -293,13 +363,34 @@ export function apply(ctx, config) {
|
|
|
293
363
|
// 的标签必须与 schema 严格对齐。
|
|
294
364
|
await credentials.modifyRecord(RECORD_ID, async () => ({
|
|
295
365
|
kind: 'grant',
|
|
296
|
-
payload: {
|
|
366
|
+
payload: {
|
|
367
|
+
keys: pool.keys(),
|
|
368
|
+
disabledModels: [...disabledModels],
|
|
369
|
+
externalKey,
|
|
370
|
+
},
|
|
297
371
|
}));
|
|
298
372
|
}
|
|
373
|
+
async function saveKeys() {
|
|
374
|
+
// 写入前必须确保首次**权威**装载已完成:写入是整体覆盖,若此时池子还是
|
|
375
|
+
// 空的(快照尚未读到),磁盘上原有的 key 会被这次写入静默抹掉。
|
|
376
|
+
//
|
|
377
|
+
// 只在凭据服务**确实存在**时才等:宿主压根没有该服务时,等它没有意义,直接让
|
|
378
|
+
// 下面给出明确错误,而不是让用户对着转圈的按钮等满超时。
|
|
379
|
+
if (credentialsRegistered()) {
|
|
380
|
+
await Promise.race([firstAuthoritativeLoad, delay(SAVE_LOAD_WAIT_MS)]);
|
|
381
|
+
}
|
|
382
|
+
if (credentialsService() === undefined)
|
|
383
|
+
throw new Error('凭据服务不可用,无法保存 key');
|
|
384
|
+
await persistRecord();
|
|
385
|
+
}
|
|
299
386
|
// ------------------------------------------------------------ provider 注册
|
|
300
387
|
function buildModels() {
|
|
301
388
|
const baseUrl = `${shim.baseUrl()}/v1`;
|
|
302
|
-
return catalog
|
|
389
|
+
return catalog
|
|
390
|
+
.current()
|
|
391
|
+
// 用户在下拉里勾掉的模型不注册,因此不会出现在模型选择器里。
|
|
392
|
+
.filter((model) => !disabledModels.has(model.id))
|
|
393
|
+
.map((model) => ({
|
|
303
394
|
id: model.id,
|
|
304
395
|
name: model.name,
|
|
305
396
|
api: 'openai-completions',
|
|
@@ -558,33 +649,57 @@ export function apply(ctx, config) {
|
|
|
558
649
|
}
|
|
559
650
|
}
|
|
560
651
|
// --------------------------------------------------------------- 可选界面
|
|
652
|
+
/**
|
|
653
|
+
* 保存 key 列表并刷新模型目录。
|
|
654
|
+
*
|
|
655
|
+
* 顺序有讲究:先落盘再 refreshCatalog —— 刷新会让模型选择器立刻重读模型,
|
|
656
|
+
* 若此时记录还没写成功,用户会看到「列表变了但重启又回去了」。
|
|
657
|
+
*/
|
|
658
|
+
const handleKeysChanged = async () => {
|
|
659
|
+
await saveKeys();
|
|
660
|
+
refreshCatalog();
|
|
661
|
+
void fetchCatalogFromUpstream();
|
|
662
|
+
};
|
|
663
|
+
/**
|
|
664
|
+
* 应用「哪些模型出现在下拉里」。
|
|
665
|
+
*
|
|
666
|
+
* 落盘失败不算失败:可见性已经作用于内存,下次写入会一并带上;这里只记一条
|
|
667
|
+
* 日志,不让界面上的勾选因为磁盘问题而回弹。
|
|
668
|
+
*/
|
|
669
|
+
const handleModelsChanged = async (nextDisabled) => {
|
|
670
|
+
disabledModels.clear();
|
|
671
|
+
for (const id of nextDisabled) {
|
|
672
|
+
if (typeof id === 'string' && id.length > 0)
|
|
673
|
+
disabledModels.add(id);
|
|
674
|
+
}
|
|
675
|
+
refreshCatalog();
|
|
676
|
+
try {
|
|
677
|
+
await saveKeys();
|
|
678
|
+
}
|
|
679
|
+
catch (error) {
|
|
680
|
+
logger.warn(`模型可见性落盘失败(本次会话已生效):${error instanceof Error ? error.message : String(error)}`);
|
|
681
|
+
}
|
|
682
|
+
};
|
|
561
683
|
// 设置页:填 key(用户明确要求只在设置页填)。
|
|
562
684
|
try {
|
|
563
685
|
registerSettingsSection(ctx, {
|
|
564
686
|
namespace: SETTINGS_NS,
|
|
565
687
|
config,
|
|
566
688
|
pool,
|
|
567
|
-
onKeysChanged:
|
|
568
|
-
await saveKeys();
|
|
569
|
-
refreshCatalog();
|
|
570
|
-
void fetchCatalogFromUpstream();
|
|
571
|
-
},
|
|
689
|
+
onKeysChanged: handleKeysChanged,
|
|
572
690
|
});
|
|
573
691
|
}
|
|
574
692
|
catch (error) {
|
|
575
693
|
logger.warn(`设置页注册失败(不影响模型可用):${error instanceof Error ? error.message : String(error)}`);
|
|
576
694
|
}
|
|
577
|
-
// Web 路由:给设置卡片读写 key /
|
|
695
|
+
// Web 路由:给设置卡片读写 key / 模型可见性 / 查看对外接口。
|
|
578
696
|
try {
|
|
579
697
|
registerKeyRoutes(ctx, {
|
|
580
698
|
status,
|
|
581
699
|
pool,
|
|
582
700
|
logger,
|
|
583
|
-
onKeysChanged:
|
|
584
|
-
|
|
585
|
-
refreshCatalog();
|
|
586
|
-
void fetchCatalogFromUpstream();
|
|
587
|
-
},
|
|
701
|
+
onKeysChanged: handleKeysChanged,
|
|
702
|
+
onModelsChanged: handleModelsChanged,
|
|
588
703
|
reload: async () => {
|
|
589
704
|
// 手动重载:走同一套「装载并宣告」逻辑,因此在启动早期点它也不会把
|
|
590
705
|
// 「还没读到」误当成「真的没有 key」而清空权威装载信号。
|
package/lib/routes.d.ts
CHANGED
|
@@ -40,6 +40,20 @@ export interface StatusProvider {
|
|
|
40
40
|
message: string;
|
|
41
41
|
at: number;
|
|
42
42
|
}>;
|
|
43
|
+
/** 本地 shim 的对外接入信息(地址、端口、API Key)。 */
|
|
44
|
+
endpoint(): {
|
|
45
|
+
baseUrl: string;
|
|
46
|
+
port: number;
|
|
47
|
+
apiKey: string;
|
|
48
|
+
};
|
|
49
|
+
/** 目录里的全部模型 + 是否在下拉中展示。 */
|
|
50
|
+
models(): Array<{
|
|
51
|
+
id: string;
|
|
52
|
+
name: string;
|
|
53
|
+
contextWindow: number;
|
|
54
|
+
maxTokens: number;
|
|
55
|
+
enabled: boolean;
|
|
56
|
+
}>;
|
|
43
57
|
providerId: string;
|
|
44
58
|
providerName: string;
|
|
45
59
|
baseUrl: string;
|
|
@@ -55,6 +69,8 @@ export interface RouteDeps {
|
|
|
55
69
|
logger: Logger;
|
|
56
70
|
/** key 列表发生变更后回调(持久化 + 刷新目录)。 */
|
|
57
71
|
onKeysChanged(): Promise<void>;
|
|
72
|
+
/** 模型可见性发生变更后回调(收到完整的「被隐藏」列表)。 */
|
|
73
|
+
onModelsChanged(disabled: string[]): Promise<void>;
|
|
58
74
|
/** 重新从凭据库装载。 */
|
|
59
75
|
reload(): Promise<void>;
|
|
60
76
|
/** 主动拉取上游模型目录。 */
|
package/lib/routes.js
CHANGED
|
@@ -153,8 +153,11 @@ export function registerKeyRoutes(ctx, deps) {
|
|
|
153
153
|
provider: deps.status.providerId,
|
|
154
154
|
displayName: deps.status.providerName,
|
|
155
155
|
baseUrl: deps.status.baseUrl,
|
|
156
|
+
// 对外接入信息:地址 / 端口 / API Key,供卡片展示并可复制到别的服务。
|
|
157
|
+
endpoint: deps.status.endpoint(),
|
|
156
158
|
summary: deps.status.summary(),
|
|
157
159
|
catalog: deps.status.catalog(),
|
|
160
|
+
models: deps.status.models(),
|
|
158
161
|
rotations: deps.status.rotations(),
|
|
159
162
|
// 卡片靠这一份数据渲染列表,并从响应里取写令牌(同源页面才拿得到)。
|
|
160
163
|
keys: deps.status.pool(),
|
|
@@ -214,6 +217,29 @@ export function registerKeyRoutes(ctx, deps) {
|
|
|
214
217
|
sendJson(res, 200, { cleared: true, total: 0, keys: [] });
|
|
215
218
|
return;
|
|
216
219
|
}
|
|
220
|
+
// ---- 模型可见性:切换单个模型是否出现在模型选择器里 ----------------
|
|
221
|
+
//
|
|
222
|
+
// 一次只改一个:界面是勾选框,逐项切换最贴合操作,也避免「整表覆盖」
|
|
223
|
+
// 在并发点击时互相抹掉。
|
|
224
|
+
if (action === 'set-model') {
|
|
225
|
+
const id = typeof body?.id === 'string' ? body.id : '';
|
|
226
|
+
if (!id) {
|
|
227
|
+
sendJson(res, 400, { error: '缺少模型 id' });
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const all = deps.status.models();
|
|
231
|
+
if (!all.some((model) => model.id === id)) {
|
|
232
|
+
sendJson(res, 404, { error: `未知模型:${id}` });
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const enabled = body?.enabled !== false;
|
|
236
|
+
const nextDisabled = all
|
|
237
|
+
.filter((model) => (model.id === id ? !enabled : !model.enabled))
|
|
238
|
+
.map((model) => model.id);
|
|
239
|
+
await deps.onModelsChanged(nextDisabled);
|
|
240
|
+
sendJson(res, 200, { id, enabled, models: deps.status.models() });
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
217
243
|
if (action === 'revive') {
|
|
218
244
|
// 手动复活被误判失效的 key。
|
|
219
245
|
const revived = deps.pool.reviveInvalid();
|
package/lib/shim.d.ts
CHANGED
|
@@ -56,7 +56,16 @@ export interface ShimOptions {
|
|
|
56
56
|
export interface Shim {
|
|
57
57
|
ready: Promise<void>;
|
|
58
58
|
baseUrl(): string;
|
|
59
|
+
port(): number;
|
|
60
|
+
/** 当前对外暴露的 API Key(DSH 自己也用它访问本服务)。 */
|
|
59
61
|
token(): string;
|
|
62
|
+
/**
|
|
63
|
+
* 换一把对外密钥。
|
|
64
|
+
*
|
|
65
|
+
* 旧密钥**继续接受**:pi-ai 可能缓存了 `resolveApiKey` 的结果,切换瞬间在途的
|
|
66
|
+
* 请求不该因此 401。持久化的密钥在启动装载完成后才拿到,正是走这条路径。
|
|
67
|
+
*/
|
|
68
|
+
setToken(next: string): void;
|
|
60
69
|
close(): Promise<void>;
|
|
61
70
|
}
|
|
62
71
|
/** 起一个只监听 127.0.0.1 的 OpenAI 兼容服务。 */
|
package/lib/shim.js
CHANGED
|
@@ -62,7 +62,11 @@ export function createShim(options) {
|
|
|
62
62
|
const { pool, catalog } = options;
|
|
63
63
|
const rotation = { ...DEFAULT_ROTATION, ...options.rotation };
|
|
64
64
|
// 每进程随机共享密钥:DSH 用 Bearer 带上它,别的本地进程猜不到。
|
|
65
|
-
|
|
65
|
+
// 启动装载完成后会被凭据库里那把持久化密钥替换(见 setToken),以便外部服务
|
|
66
|
+
// 配置一次就能长期使用。
|
|
67
|
+
let primaryToken = randomBytes(24).toString('base64url');
|
|
68
|
+
/** 仍被接受的密钥集合:切换后旧值保留,避免在途请求被 401。 */
|
|
69
|
+
const acceptedTokens = new Set([primaryToken]);
|
|
66
70
|
const server = createServer((req, res) => {
|
|
67
71
|
void handle(req, res);
|
|
68
72
|
});
|
|
@@ -71,13 +75,14 @@ export function createShim(options) {
|
|
|
71
75
|
server.once('error', reject);
|
|
72
76
|
});
|
|
73
77
|
server.listen(0, '127.0.0.1');
|
|
74
|
-
const
|
|
78
|
+
const listeningAddress = () => {
|
|
75
79
|
const address = server.address();
|
|
76
80
|
if (address === null || typeof address === 'string') {
|
|
77
81
|
throw new Error('sensenova-pool shim has no listening address');
|
|
78
82
|
}
|
|
79
|
-
return
|
|
83
|
+
return address;
|
|
80
84
|
};
|
|
85
|
+
const baseUrl = () => `http://127.0.0.1:${listeningAddress().port}`;
|
|
81
86
|
async function handle(req, res) {
|
|
82
87
|
try {
|
|
83
88
|
// 防 DNS rebinding:只接受回环 Host / Origin。
|
|
@@ -213,7 +218,23 @@ export function createShim(options) {
|
|
|
213
218
|
return {
|
|
214
219
|
ready,
|
|
215
220
|
baseUrl,
|
|
216
|
-
|
|
221
|
+
port: () => listeningAddress().port,
|
|
222
|
+
token: () => primaryToken,
|
|
223
|
+
setToken: (next) => {
|
|
224
|
+
if (typeof next !== 'string' || next.length < 8 || next === primaryToken)
|
|
225
|
+
return;
|
|
226
|
+
primaryToken = next;
|
|
227
|
+
acceptedTokens.add(next);
|
|
228
|
+
// 只留最近两把:换密钥不常发生,但别让集合无限增长。
|
|
229
|
+
if (acceptedTokens.size > 2) {
|
|
230
|
+
for (const token of acceptedTokens) {
|
|
231
|
+
if (token === primaryToken)
|
|
232
|
+
continue;
|
|
233
|
+
acceptedTokens.delete(token);
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
},
|
|
217
238
|
close: async () => {
|
|
218
239
|
await new Promise((resolve) => {
|
|
219
240
|
server.close(() => resolve());
|
|
@@ -229,11 +250,17 @@ export function createShim(options) {
|
|
|
229
250
|
const match = /^Bearer\s+(.+)$/i.exec(header.trim());
|
|
230
251
|
if (!match)
|
|
231
252
|
return false;
|
|
232
|
-
const
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
253
|
+
const presented = Buffer.from(match[1]);
|
|
254
|
+
let ok = false;
|
|
255
|
+
// 逐个常量时间比较:命中哪一把都不提前返回,避免从耗时推断密钥。
|
|
256
|
+
for (const token of acceptedTokens) {
|
|
257
|
+
const expected = Buffer.from(token);
|
|
258
|
+
if (expected.length !== presented.length)
|
|
259
|
+
continue;
|
|
260
|
+
if (timingSafeEqual(presented, expected))
|
|
261
|
+
ok = true;
|
|
262
|
+
}
|
|
263
|
+
return ok;
|
|
237
264
|
}
|
|
238
265
|
}
|
|
239
266
|
function summarizeFailure(message, attempts) {
|
package/package.json
CHANGED
|
@@ -1,78 +1,79 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "dsh-sensenova-pool",
|
|
3
|
-
"displayName": "DSH SenseNova Key Pool",
|
|
4
|
-
"description": "商汤日日新(SenseNova)多 Key 轮换池 — 填入多把 Key 即可零配置接入 DeepSeek Harness,遇 429 限流自动切换下一把 Key。",
|
|
5
|
-
"version": "0.
|
|
6
|
-
"author": "Phoeky",
|
|
7
|
-
"keywords": [
|
|
8
|
-
"dsh-plugin",
|
|
9
|
-
"deepseek-harness",
|
|
10
|
-
"deepseek",
|
|
11
|
-
"sensenova",
|
|
12
|
-
"日日新",
|
|
13
|
-
"key-rotation",
|
|
14
|
-
"key-pool",
|
|
15
|
-
"rate-limit",
|
|
16
|
-
"429",
|
|
17
|
-
"model-provider"
|
|
18
|
-
],
|
|
19
|
-
"type": "module",
|
|
20
|
-
"engines": {
|
|
21
|
-
"node": "^22.19.0 || >=24.0.0"
|
|
22
|
-
},
|
|
23
|
-
"main": "lib/index.js",
|
|
24
|
-
"types": "lib/index.d.ts",
|
|
25
|
-
"exports": {
|
|
26
|
-
".": {
|
|
27
|
-
"types": "./lib/index.d.ts",
|
|
28
|
-
"default": "./lib/index.js"
|
|
29
|
-
},
|
|
30
|
-
"./client": "./lib/client.js",
|
|
31
|
-
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
32
|
-
"./package.json": "./package.json"
|
|
33
|
-
},
|
|
34
|
-
"files": [
|
|
35
|
-
"lib",
|
|
36
|
-
"cordis.patch.yml",
|
|
37
|
-
"README.md",
|
|
38
|
-
"LICENSE"
|
|
39
|
-
],
|
|
40
|
-
"license": "MIT",
|
|
41
|
-
"repository": {
|
|
42
|
-
"type": "git",
|
|
43
|
-
"url": "git+https://github.com/Phoeky/dsh-sensenova-pool.git"
|
|
44
|
-
},
|
|
45
|
-
"homepage": "https://github.com/Phoeky/dsh-sensenova-pool#readme",
|
|
46
|
-
"bugs": "https://github.com/Phoeky/dsh-sensenova-pool/issues",
|
|
47
|
-
"scripts": {
|
|
48
|
-
"prepack": "npm run build",
|
|
49
|
-
"prepare": "npm run build",
|
|
50
|
-
"build": "tsc -p tsconfig.json && node tools/build-client.mjs",
|
|
51
|
-
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
52
|
-
"test": "node tools/test-all.mjs"
|
|
53
|
-
},
|
|
54
|
-
"dsh": {
|
|
55
|
-
"bundle": {
|
|
56
|
-
"patch": "./cordis.patch.yml"
|
|
57
|
-
},
|
|
58
|
-
"client": {
|
|
59
|
-
"inject": [
|
|
60
|
-
"@deepseek-ai/dsh-client-ui-plugin-manager",
|
|
61
|
-
"@deepseek-ai/dsh-client-ui-settings
|
|
62
|
-
"@deepseek-ai/dsh-client-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
"@deepseek-ai/
|
|
70
|
-
"@deepseek-ai/
|
|
71
|
-
"@deepseek-ai/dsh-llm
|
|
72
|
-
"@
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
"
|
|
77
|
-
|
|
78
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "dsh-sensenova-pool",
|
|
3
|
+
"displayName": "DSH SenseNova Key Pool",
|
|
4
|
+
"description": "商汤日日新(SenseNova)多 Key 轮换池 — 填入多把 Key 即可零配置接入 DeepSeek Harness,遇 429 限流自动切换下一把 Key。",
|
|
5
|
+
"version": "0.2.0",
|
|
6
|
+
"author": "Phoeky",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"dsh-plugin",
|
|
9
|
+
"deepseek-harness",
|
|
10
|
+
"deepseek",
|
|
11
|
+
"sensenova",
|
|
12
|
+
"日日新",
|
|
13
|
+
"key-rotation",
|
|
14
|
+
"key-pool",
|
|
15
|
+
"rate-limit",
|
|
16
|
+
"429",
|
|
17
|
+
"model-provider"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": "^22.19.0 || >=24.0.0"
|
|
22
|
+
},
|
|
23
|
+
"main": "lib/index.js",
|
|
24
|
+
"types": "lib/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./lib/index.d.ts",
|
|
28
|
+
"default": "./lib/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./client": "./lib/client.js",
|
|
31
|
+
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"lib",
|
|
36
|
+
"cordis.patch.yml",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/Phoeky/dsh-sensenova-pool.git"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/Phoeky/dsh-sensenova-pool#readme",
|
|
46
|
+
"bugs": "https://github.com/Phoeky/dsh-sensenova-pool/issues",
|
|
47
|
+
"scripts": {
|
|
48
|
+
"prepack": "npm run build",
|
|
49
|
+
"prepare": "npm run build",
|
|
50
|
+
"build": "tsc -p tsconfig.json && node tools/build-client.mjs",
|
|
51
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
52
|
+
"test": "node tools/test-all.mjs"
|
|
53
|
+
},
|
|
54
|
+
"dsh": {
|
|
55
|
+
"bundle": {
|
|
56
|
+
"patch": "./cordis.patch.yml"
|
|
57
|
+
},
|
|
58
|
+
"client": {
|
|
59
|
+
"inject": [
|
|
60
|
+
"@deepseek-ai/dsh-client-ui-plugin-manager",
|
|
61
|
+
"@deepseek-ai/dsh-client-ui-settings",
|
|
62
|
+
"@deepseek-ai/dsh-client-ui-settings-plugins",
|
|
63
|
+
"@deepseek-ai/dsh-client-locale"
|
|
64
|
+
],
|
|
65
|
+
"platform": "web"
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"peerDependencies": {
|
|
69
|
+
"@deepseek-ai/cordis": "^4.0.4 || >=4.0.4-0 <5.0.0-0",
|
|
70
|
+
"@deepseek-ai/schemastery": "~3.18.4 || >=3.18.4-0 <3.19.0-0",
|
|
71
|
+
"@deepseek-ai/dsh-llm": "0.2.0-rc.2 || >=0.2.0-rc.2 <0.3.0-0",
|
|
72
|
+
"@deepseek-ai/dsh-llm-pi-ai": "0.2.0-rc.2 || >=0.2.0-rc.2 <0.3.0-0",
|
|
73
|
+
"@earendil-works/pi-ai": "^0.87.1 || >=0.87.1-0 <0.88.0-0"
|
|
74
|
+
},
|
|
75
|
+
"devDependencies": {
|
|
76
|
+
"@types/node": "22.19.19",
|
|
77
|
+
"typescript": "5.9.3"
|
|
78
|
+
}
|
|
79
|
+
}
|