dsh-sub2api 0.2.6 → 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 +13 -0
- package/README.md +31 -0
- package/lib/adapter.js +89 -15
- package/lib/config.js +5 -1
- package/lib/index.js +1 -1
- package/lib/schema.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
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
|
+
|
|
3
16
|
## 0.2.6
|
|
4
17
|
|
|
5
18
|
- Retry a dropped connection once before the first chunk. The transport reports
|
package/README.md
CHANGED
|
@@ -227,6 +227,27 @@ 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
|
+
|
|
230
251
|
## 常见报错
|
|
231
252
|
|
|
232
253
|
### `https://sub.pscly.cc/v1 could not be reached or the connection dropped: fetch failed`
|
|
@@ -241,6 +262,16 @@ Cannot switch to sub2api/...: no context capacity; configure contextWindow on th
|
|
|
241
262
|
|
|
242
263
|
> 报错里的 URL 显示成 `/v1` 是传输层只上报 base 的结果;插件会把它改写成真正失败的那条路径 `/chat/completions`,并带上重试次数。
|
|
243
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
|
+
|
|
244
275
|
### `Cannot switch to sub2api/...: no context capacity`
|
|
245
276
|
|
|
246
277
|
见上文[上下文窗口](#上下文窗口),在面板里给这个模型手填容量即可。
|
package/lib/adapter.js
CHANGED
|
@@ -199,13 +199,13 @@ export class Sub2ApiAdapter extends LlmAdapter {
|
|
|
199
199
|
!emitted &&
|
|
200
200
|
attempt < attempts &&
|
|
201
201
|
options.signal?.aborted !== true &&
|
|
202
|
-
|
|
202
|
+
isRetryableFailure(error);
|
|
203
203
|
if (!canRetry) throw describeStreamFailure(error, emitted, attempt);
|
|
204
204
|
attempt += 1;
|
|
205
205
|
this.deps.logger?.warn?.(
|
|
206
|
-
`[sub2api]
|
|
206
|
+
`[sub2api] 第 ${attempt} 次重试:${plainText(error?.message ?? error).slice(0, 100)}`,
|
|
207
207
|
);
|
|
208
|
-
await delay(
|
|
208
|
+
await delay(500 * attempt * attempt);
|
|
209
209
|
}
|
|
210
210
|
}
|
|
211
211
|
}
|
|
@@ -233,16 +233,75 @@ export function mergeSampling(routeSampling, modelSampling, options) {
|
|
|
233
233
|
return merged;
|
|
234
234
|
}
|
|
235
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
|
+
|
|
236
255
|
/** Whether a failure came from the transport rather than the endpoint's answer. */
|
|
237
256
|
export function isTransportFailure(error) {
|
|
238
|
-
|
|
239
|
-
if (code === "TRANSPORT" || code === "TIMEOUT") return true;
|
|
257
|
+
if (failureClass(error) === "TRANSPORT" || failureClass(error) === "TIMEOUT") return true;
|
|
240
258
|
const message = String(error?.message ?? "");
|
|
241
259
|
return /fetch failed|could not be reached|connection dropped|socket hang up|ECONNRESET|ETIMEDOUT|EPIPE/i.test(
|
|
242
260
|
message,
|
|
243
261
|
);
|
|
244
262
|
}
|
|
245
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
|
+
|
|
246
305
|
/**
|
|
247
306
|
* Name the endpoint that actually failed.
|
|
248
307
|
*
|
|
@@ -251,18 +310,33 @@ export function isTransportFailure(error) {
|
|
|
251
310
|
* /v1/chat/completions. That sends people looking at the wrong thing.
|
|
252
311
|
*/
|
|
253
312
|
export function describeStreamFailure(error, emitted, attempts) {
|
|
254
|
-
const message = String(error?.message ?? error);
|
|
255
|
-
const isTransport = isTransportFailure(error);
|
|
256
|
-
if (!isTransport) return error;
|
|
257
|
-
|
|
258
313
|
const retryNote = attempts > 0 ? `,已重试 ${attempts} 次` : "";
|
|
259
314
|
const midStream = emitted ? ",回复中途断开" : "";
|
|
260
|
-
const
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
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;
|
|
266
340
|
}
|
|
267
341
|
|
|
268
342
|
/** Sleep between retries. */
|
package/lib/config.js
CHANGED
|
@@ -78,7 +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
|
-
|
|
81
|
+
retries: toCount(
|
|
82
|
+
field(config, "retries", field(config, "transportRetries", 2)),
|
|
83
|
+
2,
|
|
84
|
+
5,
|
|
85
|
+
),
|
|
82
86
|
reasoningModels: toStringArray(field(config, "reasoningModels", [])),
|
|
83
87
|
defaultReasoningEffort: String(field(config, "defaultReasoningEffort", "")).trim() || undefined,
|
|
84
88
|
autoProvision: field(config, "autoProvision", true) !== false,
|
package/lib/index.js
CHANGED
|
@@ -119,7 +119,7 @@ export function apply(ctx, rawConfig = {}) {
|
|
|
119
119
|
apiKeyEnv: () => cfg().apiKeyEnv,
|
|
120
120
|
timeoutMs: () => cfg().requestTimeoutMs,
|
|
121
121
|
idleTimeoutMs: () => cfg().streamIdleTimeoutMs,
|
|
122
|
-
transportRetries: () => cfg().
|
|
122
|
+
transportRetries: () => cfg().retries,
|
|
123
123
|
logger,
|
|
124
124
|
});
|
|
125
125
|
|
package/lib/schema.js
CHANGED
|
@@ -33,7 +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
|
-
|
|
36
|
+
retries: count("首字之前的重试次数,覆盖断连与 5xx/限流(0-5,默认 2)"),
|
|
37
37
|
reasoningModels: z.array(z.string()).description("额外声明支持思考等级的模型 id 片段").volatile(),
|
|
38
38
|
defaultReasoningEffort: text("", "默认思考等级(minimal/low/medium/high);留空由上游决定"),
|
|
39
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",
|