dsh-sub2api 0.7.0 → 0.8.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 +12 -0
- package/README.md +9 -0
- package/lib/client.js +35 -1
- package/lib/index.js +1 -0
- package/lib/routes.js +24 -2
- package/lib/sub2api/catalog.js +52 -0
- package/lib/sub2api/health.js +118 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.8.0
|
|
4
|
+
|
|
5
|
+
- **Models that cannot be routed leave the picker.** A sub2api catalogue lists
|
|
6
|
+
models the account pool no longer serves — choosing one fails only when you
|
|
7
|
+
press send. Refreshing now asks every model for a single token, and the ones
|
|
8
|
+
that answer 404/502 are hidden from the conversation picker while staying in
|
|
9
|
+
the panel with a reason, so the list can be explained rather than just shorter.
|
|
10
|
+
A sweep costs a handful of tokens and can be re-run from the panel.
|
|
11
|
+
- Timeouts are reported as "undecided", never as unavailable: image and video
|
|
12
|
+
models do not answer a chat completion at all, and hiding something usable is
|
|
13
|
+
worse than leaving it.
|
|
14
|
+
|
|
3
15
|
## 0.7.0
|
|
4
16
|
|
|
5
17
|
- **Measure context windows from the panel.** Models still on the default are
|
package/README.md
CHANGED
|
@@ -326,6 +326,15 @@ node scripts/build-context-table.mjs /tmp/ctx.json
|
|
|
326
326
|
npm test && npm publish
|
|
327
327
|
```
|
|
328
328
|
|
|
329
|
+
## 不可用的模型不会出现在选择器里
|
|
330
|
+
|
|
331
|
+
网关的目录不等于承诺:分组里列着一些**账号池已经服务不了**的模型,只有按发送时才会报错。
|
|
332
|
+
现在「刷新模型」会顺带给每个模型发一个单 token 请求,路由不到的(404 / 502)从对话的选择器里隐藏,
|
|
333
|
+
但仍在面板里列出来并说明原因——列表变短是可以解释的,不是凭空少东西。
|
|
334
|
+
|
|
335
|
+
- 一次扫描成本是几十个 token,面板上可以手动「重新检查」
|
|
336
|
+
- **超时不算不可用**:图像/视频模型本来就不响应聊天请求,隐藏一个能用的比留着更糟
|
|
337
|
+
|
|
329
338
|
## 自动重试
|
|
330
339
|
|
|
331
340
|
回答还没吐出第一个字之前,插件会自动重试这几类失败:
|
package/lib/client.js
CHANGED
|
@@ -1110,7 +1110,7 @@ window.__ModuleLoader__.load({
|
|
|
1110
1110
|
);
|
|
1111
1111
|
}
|
|
1112
1112
|
|
|
1113
|
-
function ModelsDetail({ state, pending, onRefreshModels, onSaveVisible, onSaveContext, onMeasureContext, onGoRecharge }) {
|
|
1113
|
+
function ModelsDetail({ state, pending, onRefreshModels, onSaveVisible, onSaveContext, onMeasureContext, onCheckHealth, onGoRecharge }) {
|
|
1114
1114
|
const models = state.models || { models: [], all: [], allowed: [], fetchedAt: 0, stale: false, error: null };
|
|
1115
1115
|
const shown = models.models || [];
|
|
1116
1116
|
const everything = models.all || [];
|
|
@@ -1180,7 +1180,34 @@ window.__ModuleLoader__.load({
|
|
|
1180
1180
|
);
|
|
1181
1181
|
}
|
|
1182
1182
|
|
|
1183
|
+
const broken = Array.isArray(models.unavailable) ? models.unavailable : [];
|
|
1184
|
+
|
|
1183
1185
|
return h("div", null,
|
|
1186
|
+
broken.length > 0
|
|
1187
|
+
? h("div", { style: { marginBottom: 10 } },
|
|
1188
|
+
h("div", { style: { ...T.hint, color: C.warn } },
|
|
1189
|
+
`${broken.length} 个模型网关当前路由不到,已从对话的选择器里隐藏:`,
|
|
1190
|
+
broken.map((m) => m.id).join("、"),
|
|
1191
|
+
"。"),
|
|
1192
|
+
h("div", { style: { ...T.row, marginTop: 6 } },
|
|
1193
|
+
h(Button, {
|
|
1194
|
+
size: "sm",
|
|
1195
|
+
onClick: async () => {
|
|
1196
|
+
try {
|
|
1197
|
+
const result = await onCheckHealth();
|
|
1198
|
+
toastStore.push(
|
|
1199
|
+
result?.unavailable > 0
|
|
1200
|
+
? `仍有 ${result.unavailable} 个不可用`
|
|
1201
|
+
: "全部恢复可用",
|
|
1202
|
+
result?.unavailable > 0 ? "error" : "ok",
|
|
1203
|
+
);
|
|
1204
|
+
} catch (e) {
|
|
1205
|
+
toastStore.push(e instanceof Error ? e.message : String(e), "error");
|
|
1206
|
+
}
|
|
1207
|
+
},
|
|
1208
|
+
}, "重新检查"),
|
|
1209
|
+
h("span", { style: T.hint }, "会给每个模型发一个单 token 请求,成本可以忽略。")))
|
|
1210
|
+
: null,
|
|
1184
1211
|
h("div", { style: { ...T.row, marginBottom: 10 } },
|
|
1185
1212
|
h("span", { style: { ...T.small, fontWeight: 500 } },
|
|
1186
1213
|
showingAll && !dirty ? `全部 ${everything.length} 个` : `已选 ${checked.length} / ${everything.length}`),
|
|
@@ -1428,6 +1455,12 @@ window.__ModuleLoader__.load({
|
|
|
1428
1455
|
return result;
|
|
1429
1456
|
}), [run]);
|
|
1430
1457
|
|
|
1458
|
+
const onCheckHealth = useCallback(() => run("health", async () => {
|
|
1459
|
+
const result = await call("/models/health", { method: "POST", body: {} });
|
|
1460
|
+
await statusStore.refresh();
|
|
1461
|
+
return result;
|
|
1462
|
+
}), [run]);
|
|
1463
|
+
|
|
1431
1464
|
const onDiagnose = useCallback(() => call("/diagnose"), []);
|
|
1432
1465
|
|
|
1433
1466
|
const onRecharge = useCallback(async (amountYuan) => {
|
|
@@ -1511,6 +1544,7 @@ window.__ModuleLoader__.load({
|
|
|
1511
1544
|
onSaveVisible,
|
|
1512
1545
|
onSaveContext,
|
|
1513
1546
|
onMeasureContext,
|
|
1547
|
+
onCheckHealth,
|
|
1514
1548
|
onGoRecharge: () => { setOpen((prev) => ({ ...prev, recharge: true })); },
|
|
1515
1549
|
})),
|
|
1516
1550
|
|
package/lib/index.js
CHANGED
package/lib/routes.js
CHANGED
|
@@ -268,8 +268,30 @@ export function makeRoutes(deps) {
|
|
|
268
268
|
status: failure instanceof Sub2ApiError ? failure.status : undefined,
|
|
269
269
|
};
|
|
270
270
|
}
|
|
271
|
-
|
|
272
|
-
|
|
271
|
+
let unavailable = 0;
|
|
272
|
+
if (error === undefined) {
|
|
273
|
+
// Refreshing is the moment a user expects the list to be truthful, so
|
|
274
|
+
// the availability sweep rides along. It is best-effort: a failure here
|
|
275
|
+
// leaves the previous verdicts rather than failing the refresh.
|
|
276
|
+
const apiKey = await session.apiKey();
|
|
277
|
+
unavailable = await catalog.sweepHealth(apiKey);
|
|
278
|
+
announceCatalog?.();
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
ok: error === undefined,
|
|
282
|
+
count,
|
|
283
|
+
unavailable,
|
|
284
|
+
error: error ?? null,
|
|
285
|
+
models: catalog.snapshot(),
|
|
286
|
+
};
|
|
287
|
+
}),
|
|
288
|
+
|
|
289
|
+
/** Re-check availability without re-fetching the catalogue. */
|
|
290
|
+
defineRoute("POST", `${ROUTE_PREFIX}/models/health`, async () => {
|
|
291
|
+
const apiKey = await session.apiKey();
|
|
292
|
+
const unavailable = await catalog.sweepHealth(apiKey);
|
|
293
|
+
announceCatalog?.();
|
|
294
|
+
return { unavailable, models: catalog.snapshot() };
|
|
273
295
|
}),
|
|
274
296
|
|
|
275
297
|
/** Save which models the picker offers. An empty list restores "show all". */
|
package/lib/sub2api/catalog.js
CHANGED
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
import { Sub2ApiError } from "../util.js";
|
|
22
22
|
import { BUILTIN_MODEL } from "../constants.js";
|
|
23
23
|
import { measuredContext } from "../model-context.js";
|
|
24
|
+
import { checkAvailability } from "./health.js";
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
27
|
* @typedef {object} ModelRecord
|
|
@@ -59,6 +60,13 @@ export class ModelCatalog {
|
|
|
59
60
|
* @type {{code?: string, message: string, status?: number, at: number} | undefined}
|
|
60
61
|
*/
|
|
61
62
|
this.lastError = undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Models the last availability sweep could not route, with the reason.
|
|
65
|
+
* They stay in `all` so the panel can name them, but leave the picker —
|
|
66
|
+
* offering a model that answers 502 when you press send helps nobody.
|
|
67
|
+
* @type {Map<string, {reason: string, status?: number}>}
|
|
68
|
+
*/
|
|
69
|
+
this.unavailable = new Map();
|
|
62
70
|
}
|
|
63
71
|
|
|
64
72
|
/** Read the disk cache once, so a cold start is not an empty picker. */
|
|
@@ -112,6 +120,13 @@ export class ModelCatalog {
|
|
|
112
120
|
this.models = records;
|
|
113
121
|
this.fetchedAt = Date.now();
|
|
114
122
|
this.lastError = undefined;
|
|
123
|
+
/**
|
|
124
|
+
* Models the last availability sweep could not route, with the reason.
|
|
125
|
+
* They stay in `all` so the panel can name them, but leave the picker —
|
|
126
|
+
* offering a model that answers 502 when you press send helps nobody.
|
|
127
|
+
* @type {Map<string, {reason: string, status?: number}>}
|
|
128
|
+
*/
|
|
129
|
+
this.unavailable = new Map();
|
|
115
130
|
await this.#persist();
|
|
116
131
|
this.logger.info?.(`[sub2api] 模型目录已同步:${records.length} 个`);
|
|
117
132
|
return records.length;
|
|
@@ -240,6 +255,41 @@ export class ModelCatalog {
|
|
|
240
255
|
return has ? [...this.models] : [...this.models, { ...BUILTIN_MODEL }];
|
|
241
256
|
}
|
|
242
257
|
|
|
258
|
+
/**
|
|
259
|
+
* Probe every listed model and remember which ones cannot be routed.
|
|
260
|
+
*
|
|
261
|
+
* Best-effort: a failure here must never break a catalogue refresh, because
|
|
262
|
+
* the catalogue is still useful without it.
|
|
263
|
+
* @param {string} apiKey
|
|
264
|
+
* @param {AbortSignal} [signal]
|
|
265
|
+
* @returns {Promise<number>} how many models were found unavailable
|
|
266
|
+
*/
|
|
267
|
+
async sweepHealth(apiKey, signal) {
|
|
268
|
+
if (typeof apiKey !== "string" || apiKey === "") return this.unavailable.size;
|
|
269
|
+
try {
|
|
270
|
+
const results = await checkAvailability({
|
|
271
|
+
apiBase: this.deps.apiBase?.() ?? "",
|
|
272
|
+
apiKey,
|
|
273
|
+
models: this.catalogue().map((model) => model.id),
|
|
274
|
+
signal,
|
|
275
|
+
});
|
|
276
|
+
const next = new Map();
|
|
277
|
+
for (const [id, result] of results) {
|
|
278
|
+
if (!result.ok) next.set(id, { reason: result.reason ?? "不可用", status: result.status });
|
|
279
|
+
}
|
|
280
|
+
this.unavailable = next;
|
|
281
|
+
return next.size;
|
|
282
|
+
} catch (error) {
|
|
283
|
+
this.logger.warn?.(`[sub2api] 可用性检查失败:${String(error?.message ?? error)}`);
|
|
284
|
+
return this.unavailable.size;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** @returns {{id: string, reason: string, status?: number}[]} */
|
|
289
|
+
unavailableList() {
|
|
290
|
+
return [...this.unavailable].map(([id, info]) => ({ id, ...info }));
|
|
291
|
+
}
|
|
292
|
+
|
|
243
293
|
visible() {
|
|
244
294
|
const allow = this.allowed();
|
|
245
295
|
const keep = (model) =>
|
|
@@ -247,6 +297,7 @@ export class ModelCatalog {
|
|
|
247
297
|
|
|
248
298
|
return this.catalogue()
|
|
249
299
|
.filter(keep)
|
|
300
|
+
.filter((model) => model.builtin === true || !this.unavailable.has(model.id))
|
|
250
301
|
.map((model) => ({ ...model, ...this.resolveContext(model) }));
|
|
251
302
|
}
|
|
252
303
|
|
|
@@ -292,6 +343,7 @@ export class ModelCatalog {
|
|
|
292
343
|
stale: this.stale,
|
|
293
344
|
error: this.lastError ?? null,
|
|
294
345
|
needsRecharge: this.lastError?.code === "INSUFFICIENT_BALANCE",
|
|
346
|
+
unavailable: this.unavailableList(),
|
|
295
347
|
allowed: this.allowed(),
|
|
296
348
|
all: this.catalogue().map((model) => ({
|
|
297
349
|
id: model.id,
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which models the gateway can actually serve right now.
|
|
3
|
+
*
|
|
4
|
+
* A sub2api catalogue is not a promise: the group lists models that no
|
|
5
|
+
* configured account can serve, so choosing one fails with 502 or 404 only when
|
|
6
|
+
* the user presses send. A one-token request answers the same question in about
|
|
7
|
+
* a tenth of a second and costs a handful of tokens, which is cheap enough to
|
|
8
|
+
* run whenever the catalogue refreshes.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-sub2api/sub2api/health
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { Sub2ApiError } from "../util.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Models a chat completion cannot judge: image and video generators never
|
|
17
|
+
* answer one, and probing them only wastes the sweep's time budget.
|
|
18
|
+
*/
|
|
19
|
+
const NON_CHAT = /(?:^|[-_/])(?:image|video|embed|embedding|tts|speech|whisper|rerank)(?:$|[-_/])/i;
|
|
20
|
+
|
|
21
|
+
/** A model the gateway could not route at all. */
|
|
22
|
+
const DEAD = /not supported by any configured account|isn't included|model_not_found|must be called via/i;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @typedef {object} HealthResult
|
|
26
|
+
* @property {boolean} ok
|
|
27
|
+
* @property {number} [status]
|
|
28
|
+
* @property {string} [reason] short, shown in the panel
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Ask for one token from `model`.
|
|
33
|
+
* @param {object} options
|
|
34
|
+
* @param {string} options.apiBase
|
|
35
|
+
* @param {string} options.apiKey
|
|
36
|
+
* @param {string} options.model
|
|
37
|
+
* @param {AbortSignal} [options.signal]
|
|
38
|
+
* @returns {Promise<HealthResult>}
|
|
39
|
+
*/
|
|
40
|
+
async function probeOne({ apiBase, apiKey, model, signal }) {
|
|
41
|
+
const controller = new AbortController();
|
|
42
|
+
const timer = setTimeout(() => controller.abort(), 15_000);
|
|
43
|
+
const onAbort = () => controller.abort();
|
|
44
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const response = await fetch(`${String(apiBase).replace(/\/+$/, "")}/v1/chat/completions`, {
|
|
48
|
+
method: "POST",
|
|
49
|
+
headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
|
|
50
|
+
body: JSON.stringify({
|
|
51
|
+
model,
|
|
52
|
+
messages: [{ role: "user", content: "hi" }],
|
|
53
|
+
max_tokens: 1,
|
|
54
|
+
stream: false,
|
|
55
|
+
}),
|
|
56
|
+
signal: controller.signal,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if (response.ok) {
|
|
60
|
+
await response.text();
|
|
61
|
+
return { ok: true, status: response.status };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const body = (await response.text()).slice(0, 400);
|
|
65
|
+
if (response.status === 429) return { ok: true, status: 429, reason: "限流" };
|
|
66
|
+
if (DEAD.test(body)) return { ok: false, status: response.status, reason: "网关没有可用账号" };
|
|
67
|
+
if (response.status >= 500) {
|
|
68
|
+
return { ok: false, status: response.status, reason: "上游不可用" };
|
|
69
|
+
}
|
|
70
|
+
// 400/401/403 on a one-token request is about the request, not the route.
|
|
71
|
+
return { ok: true, status: response.status, reason: "请求被拒绝但不代表模型不可用" };
|
|
72
|
+
} catch (error) {
|
|
73
|
+
// A timeout says nothing about whether the model works — an image or video
|
|
74
|
+
// model never answers a chat completion at all. Keep it in the picker and
|
|
75
|
+
// say so, rather than hiding something that may be perfectly usable.
|
|
76
|
+
const aborted = error?.name === "AbortError";
|
|
77
|
+
return { ok: true, status: 0, reason: aborted ? "探测超时,未判定" : "探测失败,未判定" };
|
|
78
|
+
} finally {
|
|
79
|
+
clearTimeout(timer);
|
|
80
|
+
signal?.removeEventListener("abort", onAbort);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Probe every model, a few at a time.
|
|
86
|
+
*
|
|
87
|
+
* @param {object} options
|
|
88
|
+
* @param {string} options.apiBase
|
|
89
|
+
* @param {string} options.apiKey
|
|
90
|
+
* @param {string[]} options.models
|
|
91
|
+
* @param {number} [options.concurrency]
|
|
92
|
+
* @param {AbortSignal} [options.signal]
|
|
93
|
+
* @returns {Promise<Map<string, HealthResult>>}
|
|
94
|
+
*/
|
|
95
|
+
export async function checkAvailability({ apiBase, apiKey, models, concurrency = 6, signal }) {
|
|
96
|
+
if (typeof apiKey !== "string" || apiKey === "") {
|
|
97
|
+
throw new Sub2ApiError("还没有密钥,无法检查可用性", { code: "no_api_key" });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const queue = [...new Set(models.map((id) => String(id ?? "")).filter(Boolean))].filter(
|
|
101
|
+
(id) => !NON_CHAT.test(id),
|
|
102
|
+
);
|
|
103
|
+
/** @type {Map<string, HealthResult>} */
|
|
104
|
+
const results = new Map();
|
|
105
|
+
|
|
106
|
+
let next = 0;
|
|
107
|
+
const workers = Array.from({ length: Math.min(concurrency, queue.length) }, async () => {
|
|
108
|
+
while (next < queue.length) {
|
|
109
|
+
if (signal?.aborted === true) return;
|
|
110
|
+
const model = queue[next];
|
|
111
|
+
next += 1;
|
|
112
|
+
results.set(model, await probeOne({ apiBase, apiKey, model, signal }));
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
await Promise.all(workers);
|
|
117
|
+
return results;
|
|
118
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-sub2api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Psapi (sub.pscly.cc) provider for DeepSeek Harness: zero-friction signup, auto-provisioned API key, upstream model sync, balance and recharge",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "GPL-3.0-only",
|