dsh-sub2api 0.2.5 → 0.2.7
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 +26 -0
- package/README.md +49 -0
- package/lib/adapter.js +140 -1
- package/lib/config.js +12 -0
- package/lib/index.js +1 -0
- package/lib/schema.js +1 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.7
|
|
4
|
+
|
|
5
|
+
- Retry 5xx and 429, not just dropped connections. The openresty in front of the
|
|
6
|
+
gateway answers 502 while the Go backend restarts; the same request a second
|
|
7
|
+
later succeeds.
|
|
8
|
+
- Read the real failure class. The transport's error carries it in `failure`
|
|
9
|
+
(`SERVER`, `TRANSPORT`, …) while `code` is the seam-level bucket
|
|
10
|
+
(`unavailable`), so routing on `code` treated every provider fault as one kind
|
|
11
|
+
— the 502 path was never taken.
|
|
12
|
+
- Report HTTP failures as sentences instead of raw HTML, and name the status.
|
|
13
|
+
- `retries` replaces `transportRetries` (the old key still works), default 2,
|
|
14
|
+
with quadratic backoff. Retries stop at the first streamed chunk.
|
|
15
|
+
|
|
16
|
+
## 0.2.6
|
|
17
|
+
|
|
18
|
+
- Retry a dropped connection once before the first chunk. The transport reports
|
|
19
|
+
these as `TRANSPORT` and the host's retry budget is shared with every other
|
|
20
|
+
failure class, so one reset could exhaust it and surface as a terminal error.
|
|
21
|
+
A retry is safe there: nothing was shown and nothing was billed.
|
|
22
|
+
- Name the path that actually failed. The transport reports the connection's
|
|
23
|
+
base URL, so the message read "https://host/v1 could not be reached" while the
|
|
24
|
+
request went to `/v1/chat/completions`; it now says `/chat/completions`, how
|
|
25
|
+
many times it retried, and points at `streamIdleTimeoutMs` for long contexts.
|
|
26
|
+
- `transportRetries` (0-5, default 1) and a README troubleshooting section for
|
|
27
|
+
this error.
|
|
28
|
+
|
|
3
29
|
## 0.2.5
|
|
4
30
|
|
|
5
31
|
- 0.2.4's failure back-off also swallowed the retry a top-up was supposed to
|
package/README.md
CHANGED
|
@@ -227,6 +227,55 @@ Cannot switch to sub2api/...: no context capacity; configure contextWindow on th
|
|
|
227
227
|
|
|
228
228
|
---
|
|
229
229
|
|
|
230
|
+
## 自动重试
|
|
231
|
+
|
|
232
|
+
回答还没吐出第一个字之前,插件会自动重试这几类失败:
|
|
233
|
+
|
|
234
|
+
| 类型 | 典型表现 | 默认重试 |
|
|
235
|
+
|---|---|---|
|
|
236
|
+
| 连接中断 | `fetch failed`、`could not be reached`、socket reset | ✅ |
|
|
237
|
+
| 网关 5xx | openresty 返回 `502 Bad Gateway`(后端重启/上游账号异常) | ✅ |
|
|
238
|
+
| 限流 | HTTP 429 | ✅ |
|
|
239
|
+
| 空响应 | 200 但没有任何内容 | ✅ |
|
|
240
|
+
| 业务错误 | 401/403、余额不足、超长上下文 | ❌ 立即返回 |
|
|
241
|
+
|
|
242
|
+
已经吐出内容之后**绝不重试**——否则会把半截回答拼成两遍。
|
|
243
|
+
|
|
244
|
+
```yaml
|
|
245
|
+
llm-sub2api:
|
|
246
|
+
retries: 2 # 0 关闭,最大 5
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
> 取舍:极少数情况下,网关其实已经受理并计费,只是响应在返回途中丢了,这时重试会多算一次。窗口很小,但如果你更在意账目精确,把 `retries` 设为 0。
|
|
250
|
+
|
|
251
|
+
## 常见报错
|
|
252
|
+
|
|
253
|
+
### `https://sub.pscly.cc/v1 could not be reached or the connection dropped: fetch failed`
|
|
254
|
+
|
|
255
|
+
这是**传输层**的失败(不是网关返回的业务错误):TCP/TLS 连接没建立起来,或者建立后被对端断开。
|
|
256
|
+
|
|
257
|
+
排查顺序:
|
|
258
|
+
|
|
259
|
+
1. **先看是不是偶发**。插件现在会在**首个字节之前**自动重试一次(`transportRetries`,默认 1),偶发的 socket 重置会被这一步吃掉,用户看不到。
|
|
260
|
+
2. **看上下文规模**。如果你在推 100 万 token 级的长上下文(本站实测 1M 上下文单次首字 30–40 秒),等待首字节的时间会很长;Node 内建 fetch 的响应头超时是 300 秒,超过就会以 `fetch failed` 形式失败。
|
|
261
|
+
3. **看网关日志**。`docker logs sub2api | grep -iE "reset|canceled|EOF"`;如果网关侧完全没有这条请求的记录,问题在链路上(DNS/CDN/代理),不在网关。
|
|
262
|
+
|
|
263
|
+
> 报错里的 URL 显示成 `/v1` 是传输层只上报 base 的结果;插件会把它改写成真正失败的那条路径 `/chat/completions`,并带上重试次数。
|
|
264
|
+
|
|
265
|
+
### `the endpoint answered 502 Bad Gateway: <html>...openresty</html>`
|
|
266
|
+
|
|
267
|
+
这是**网关前面的 openresty 返回的**,不是模型的问题:sub2api 的 Go 后端当时不可用(重启中),或者上游账号被判定不可用正在切换。
|
|
268
|
+
|
|
269
|
+
插件会自动重试(见上),连续失败才会抛给你。持续出现时看后端状态:
|
|
270
|
+
|
|
271
|
+
```bash
|
|
272
|
+
ssh pshk 'docker ps | grep sub2api; docker logs --tail 50 sub2api'
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### `Cannot switch to sub2api/...: no context capacity`
|
|
276
|
+
|
|
277
|
+
见上文[上下文窗口](#上下文窗口),在面板里给这个模型手填容量即可。
|
|
278
|
+
|
|
230
279
|
## 开发
|
|
231
280
|
|
|
232
281
|
```sh
|
package/lib/adapter.js
CHANGED
|
@@ -179,7 +179,35 @@ export class Sub2ApiAdapter extends LlmAdapter {
|
|
|
179
179
|
headers: safeAttributionHeaders(),
|
|
180
180
|
};
|
|
181
181
|
|
|
182
|
-
|
|
182
|
+
// A dropped connection before the first chunk is safe to retry: nothing was
|
|
183
|
+
// shown to the user and nothing was billed. The host retries some of these
|
|
184
|
+
// too, but its budget is shared with every other failure class, so a single
|
|
185
|
+
// reset can consume it and surface as a terminal error.
|
|
186
|
+
const attempts = Math.max(0, Math.min(5, this.deps.transportRetries?.() ?? 1));
|
|
187
|
+
let attempt = 0;
|
|
188
|
+
|
|
189
|
+
for (;;) {
|
|
190
|
+
let emitted = false;
|
|
191
|
+
try {
|
|
192
|
+
for await (const chunk of service.stream(call)) {
|
|
193
|
+
emitted = true;
|
|
194
|
+
yield chunk;
|
|
195
|
+
}
|
|
196
|
+
return;
|
|
197
|
+
} catch (error) {
|
|
198
|
+
const canRetry =
|
|
199
|
+
!emitted &&
|
|
200
|
+
attempt < attempts &&
|
|
201
|
+
options.signal?.aborted !== true &&
|
|
202
|
+
isRetryableFailure(error);
|
|
203
|
+
if (!canRetry) throw describeStreamFailure(error, emitted, attempt);
|
|
204
|
+
attempt += 1;
|
|
205
|
+
this.deps.logger?.warn?.(
|
|
206
|
+
`[sub2api] 第 ${attempt} 次重试:${plainText(error?.message ?? error).slice(0, 100)}`,
|
|
207
|
+
);
|
|
208
|
+
await delay(500 * attempt * attempt);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
183
211
|
}
|
|
184
212
|
}
|
|
185
213
|
|
|
@@ -205,6 +233,117 @@ export function mergeSampling(routeSampling, modelSampling, options) {
|
|
|
205
233
|
return merged;
|
|
206
234
|
}
|
|
207
235
|
|
|
236
|
+
/**
|
|
237
|
+
* The provider-level failure class.
|
|
238
|
+
*
|
|
239
|
+
* The transport's error carries the real class in `failure`; its `code` is the
|
|
240
|
+
* seam-level bucket (`unavailable`) and using that for routing silently treats
|
|
241
|
+
* every provider fault as one kind.
|
|
242
|
+
*/
|
|
243
|
+
export function failureClass(error) {
|
|
244
|
+
const failure = String(error?.failure ?? "");
|
|
245
|
+
if (failure !== "") return failure;
|
|
246
|
+
return String(error?.code ?? "");
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** HTTP status the transport recorded for this failure, when it has one. */
|
|
250
|
+
export function failureStatus(error) {
|
|
251
|
+
const status = Number(error?.facts?.status ?? error?.status);
|
|
252
|
+
return Number.isFinite(status) ? status : undefined;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Whether a failure came from the transport rather than the endpoint's answer. */
|
|
256
|
+
export function isTransportFailure(error) {
|
|
257
|
+
if (failureClass(error) === "TRANSPORT" || failureClass(error) === "TIMEOUT") return true;
|
|
258
|
+
const message = String(error?.message ?? "");
|
|
259
|
+
return /fetch failed|could not be reached|connection dropped|socket hang up|ECONNRESET|ETIMEDOUT|EPIPE/i.test(
|
|
260
|
+
message,
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Error codes a second attempt can plausibly turn into an answer. */
|
|
265
|
+
const RETRYABLE_CODES = new Set([
|
|
266
|
+
"TRANSPORT",
|
|
267
|
+
"TIMEOUT",
|
|
268
|
+
"SERVER",
|
|
269
|
+
"RATE_LIMIT",
|
|
270
|
+
"THROTTLED",
|
|
271
|
+
"EMPTY_RESPONSE",
|
|
272
|
+
]);
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Whether asking again could work.
|
|
276
|
+
*
|
|
277
|
+
* A 502 from the openresty in front of the gateway is the common one here: the
|
|
278
|
+
* Go backend was restarting or the upstream account was unusable, and the same
|
|
279
|
+
* request a second later usually succeeds. Retrying is only allowed before the
|
|
280
|
+
* first chunk, so a partially streamed answer is never repeated.
|
|
281
|
+
*/
|
|
282
|
+
export function isRetryableFailure(error) {
|
|
283
|
+
if (error === undefined || error === null) return false;
|
|
284
|
+
if (isTransportFailure(error)) return true;
|
|
285
|
+
|
|
286
|
+
if (RETRYABLE_CODES.has(failureClass(error))) return true;
|
|
287
|
+
|
|
288
|
+
const status = failureStatus(error);
|
|
289
|
+
if (status !== undefined && (status >= 500 || status === 429)) return true;
|
|
290
|
+
|
|
291
|
+
const message = String(error?.message ?? "");
|
|
292
|
+
return /answered 5\d\d|Bad Gateway|Service Unavailable|Gateway Timeout/i.test(message);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Strip the tags a gateway puts around an HTML error page. */
|
|
296
|
+
export function plainText(value) {
|
|
297
|
+
return String(value ?? "")
|
|
298
|
+
.replace(/<script[\s\S]*?<\/script>/gi, " ")
|
|
299
|
+
.replace(/<style[\s\S]*?<\/style>/gi, " ")
|
|
300
|
+
.replace(/<[^>]+>/g, " ")
|
|
301
|
+
.replace(/\s+/g, " ")
|
|
302
|
+
.trim();
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Name the endpoint that actually failed.
|
|
307
|
+
*
|
|
308
|
+
* The transport reports the connection's base URL, so the thrown message reads
|
|
309
|
+
* "https://host/v1 could not be reached" even though the request went to
|
|
310
|
+
* /v1/chat/completions. That sends people looking at the wrong thing.
|
|
311
|
+
*/
|
|
312
|
+
export function describeStreamFailure(error, emitted, attempts) {
|
|
313
|
+
const retryNote = attempts > 0 ? `,已重试 ${attempts} 次` : "";
|
|
314
|
+
const midStream = emitted ? ",回复中途断开" : "";
|
|
315
|
+
const status = failureStatus(error);
|
|
316
|
+
const said = plainText(error?.message ?? error);
|
|
317
|
+
|
|
318
|
+
if (status !== undefined && status >= 500) {
|
|
319
|
+
const what =
|
|
320
|
+
status === 502
|
|
321
|
+
? "中转站的网关返回 502,后端当时不可用(重启或上游账号异常)"
|
|
322
|
+
: status === 504
|
|
323
|
+
? "中转站的网关返回 504,上游超时"
|
|
324
|
+
: `中转站的网关返回 ${status}`;
|
|
325
|
+
return new Sub2ApiError(`${what}${retryNote}${midStream}。原始信息:${said}`, {
|
|
326
|
+
code: failureClass(error) || "SERVER",
|
|
327
|
+
status,
|
|
328
|
+
cause: error,
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (isTransportFailure(error)) {
|
|
333
|
+
return new Sub2ApiError(
|
|
334
|
+
`与中转站的连接中断(/chat/completions${retryNote}${midStream});如果这条会话很长,可能是等待首字超时,可在插件配置里调大 streamIdleTimeoutMs。原始信息:${said}`,
|
|
335
|
+
{ code: failureClass(error) || "TRANSPORT", cause: error },
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return error;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** Sleep between retries. */
|
|
343
|
+
function delay(ms) {
|
|
344
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
345
|
+
}
|
|
346
|
+
|
|
208
347
|
/**
|
|
209
348
|
* The level a request selected, matched by id against the model's declared
|
|
210
349
|
* levels. An unknown id yields undefined rather than guessing.
|
package/lib/config.js
CHANGED
|
@@ -78,6 +78,11 @@ export function resolveConfig(config) {
|
|
|
78
78
|
contextWindow: toPositiveInt(field(config, "contextWindow", undefined)),
|
|
79
79
|
defaultContextWindow: toPositiveInt(field(config, "defaultContextWindow", undefined)),
|
|
80
80
|
rechargeMultiplier: toMultiplier(field(config, "rechargeMultiplier", DEFAULT_RECHARGE_MULTIPLIER)),
|
|
81
|
+
retries: toCount(
|
|
82
|
+
field(config, "retries", field(config, "transportRetries", 2)),
|
|
83
|
+
2,
|
|
84
|
+
5,
|
|
85
|
+
),
|
|
81
86
|
reasoningModels: toStringArray(field(config, "reasoningModels", [])),
|
|
82
87
|
defaultReasoningEffort: String(field(config, "defaultReasoningEffort", "")).trim() || undefined,
|
|
83
88
|
autoProvision: field(config, "autoProvision", true) !== false,
|
|
@@ -85,6 +90,13 @@ export function resolveConfig(config) {
|
|
|
85
90
|
};
|
|
86
91
|
}
|
|
87
92
|
|
|
93
|
+
/** Coerce to a bounded non-negative integer. */
|
|
94
|
+
function toCount(value, fallback, max) {
|
|
95
|
+
const n = typeof value === "number" ? value : Number(value);
|
|
96
|
+
if (!Number.isFinite(n) || n < 0) return fallback;
|
|
97
|
+
return Math.min(Math.trunc(n), max);
|
|
98
|
+
}
|
|
99
|
+
|
|
88
100
|
/** Coerce to a positive multiplier, falling back to the documented default. */
|
|
89
101
|
function toMultiplier(value) {
|
|
90
102
|
const n = typeof value === "number" ? value : Number(value);
|
package/lib/index.js
CHANGED
package/lib/schema.js
CHANGED
|
@@ -33,6 +33,7 @@ export const Config = z.object({
|
|
|
33
33
|
contextWindow: count("强制覆盖所有模型的上下文窗口;留空则逐模型解析"),
|
|
34
34
|
defaultContextWindow: count("上游没有声明上下文窗口时使用的兜底值(默认 128000)"),
|
|
35
35
|
rechargeMultiplier: z.natural().min(1).max(100).description("每 1 元到账多少额度,用于面板预估").volatile(),
|
|
36
|
+
retries: count("首字之前的重试次数,覆盖断连与 5xx/限流(0-5,默认 2)"),
|
|
36
37
|
reasoningModels: z.array(z.string()).description("额外声明支持思考等级的模型 id 片段").volatile(),
|
|
37
38
|
defaultReasoningEffort: text("", "默认思考等级(minimal/low/medium/high);留空由上游决定"),
|
|
38
39
|
showSidebarQuota: z.boolean().default(false).description("在侧边栏显示余额卡片").volatile(),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-sub2api",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"description": "Sub2API 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",
|