dsh-sub2api 0.2.4 → 0.2.6
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 +21 -0
- package/README.md +18 -0
- package/lib/adapter.js +66 -1
- package/lib/config.js +8 -0
- package/lib/index.js +1 -0
- package/lib/routes.js +4 -1
- package/lib/schema.js +1 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.6
|
|
4
|
+
|
|
5
|
+
- Retry a dropped connection once before the first chunk. The transport reports
|
|
6
|
+
these as `TRANSPORT` and the host's retry budget is shared with every other
|
|
7
|
+
failure class, so one reset could exhaust it and surface as a terminal error.
|
|
8
|
+
A retry is safe there: nothing was shown and nothing was billed.
|
|
9
|
+
- Name the path that actually failed. The transport reports the connection's
|
|
10
|
+
base URL, so the message read "https://host/v1 could not be reached" while the
|
|
11
|
+
request went to `/v1/chat/completions`; it now says `/chat/completions`, how
|
|
12
|
+
many times it retried, and points at `streamIdleTimeoutMs` for long contexts.
|
|
13
|
+
- `transportRetries` (0-5, default 1) and a README troubleshooting section for
|
|
14
|
+
this error.
|
|
15
|
+
|
|
16
|
+
## 0.2.5
|
|
17
|
+
|
|
18
|
+
- 0.2.4's failure back-off also swallowed the retry a top-up was supposed to
|
|
19
|
+
trigger: the balance rejection recorded seconds earlier counted as "recently
|
|
20
|
+
failed", so the catalog stayed empty until the two minutes elapsed. The
|
|
21
|
+
back-off now excludes `INSUFFICIENT_BALANCE`, because that answer changes the
|
|
22
|
+
moment money lands.
|
|
23
|
+
|
|
3
24
|
## 0.2.4
|
|
4
25
|
|
|
5
26
|
- Sync the catalog by itself once the account is funded. A top-up normally
|
package/README.md
CHANGED
|
@@ -227,6 +227,24 @@ Cannot switch to sub2api/...: no context capacity; configure contextWindow on th
|
|
|
227
227
|
|
|
228
228
|
---
|
|
229
229
|
|
|
230
|
+
## 常见报错
|
|
231
|
+
|
|
232
|
+
### `https://sub.pscly.cc/v1 could not be reached or the connection dropped: fetch failed`
|
|
233
|
+
|
|
234
|
+
这是**传输层**的失败(不是网关返回的业务错误):TCP/TLS 连接没建立起来,或者建立后被对端断开。
|
|
235
|
+
|
|
236
|
+
排查顺序:
|
|
237
|
+
|
|
238
|
+
1. **先看是不是偶发**。插件现在会在**首个字节之前**自动重试一次(`transportRetries`,默认 1),偶发的 socket 重置会被这一步吃掉,用户看不到。
|
|
239
|
+
2. **看上下文规模**。如果你在推 100 万 token 级的长上下文(本站实测 1M 上下文单次首字 30–40 秒),等待首字节的时间会很长;Node 内建 fetch 的响应头超时是 300 秒,超过就会以 `fetch failed` 形式失败。
|
|
240
|
+
3. **看网关日志**。`docker logs sub2api | grep -iE "reset|canceled|EOF"`;如果网关侧完全没有这条请求的记录,问题在链路上(DNS/CDN/代理),不在网关。
|
|
241
|
+
|
|
242
|
+
> 报错里的 URL 显示成 `/v1` 是传输层只上报 base 的结果;插件会把它改写成真正失败的那条路径 `/chat/completions`,并带上重试次数。
|
|
243
|
+
|
|
244
|
+
### `Cannot switch to sub2api/...: no context capacity`
|
|
245
|
+
|
|
246
|
+
见上文[上下文窗口](#上下文窗口),在面板里给这个模型手填容量即可。
|
|
247
|
+
|
|
230
248
|
## 开发
|
|
231
249
|
|
|
232
250
|
```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
|
+
isTransportFailure(error);
|
|
203
|
+
if (!canRetry) throw describeStreamFailure(error, emitted, attempt);
|
|
204
|
+
attempt += 1;
|
|
205
|
+
this.deps.logger?.warn?.(
|
|
206
|
+
`[sub2api] 连接中断,第 ${attempt} 次重试:${String(error?.message ?? error).slice(0, 120)}`,
|
|
207
|
+
);
|
|
208
|
+
await delay(400 * attempt);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
183
211
|
}
|
|
184
212
|
}
|
|
185
213
|
|
|
@@ -205,6 +233,43 @@ export function mergeSampling(routeSampling, modelSampling, options) {
|
|
|
205
233
|
return merged;
|
|
206
234
|
}
|
|
207
235
|
|
|
236
|
+
/** Whether a failure came from the transport rather than the endpoint's answer. */
|
|
237
|
+
export function isTransportFailure(error) {
|
|
238
|
+
const code = String(error?.code ?? "");
|
|
239
|
+
if (code === "TRANSPORT" || code === "TIMEOUT") return true;
|
|
240
|
+
const message = String(error?.message ?? "");
|
|
241
|
+
return /fetch failed|could not be reached|connection dropped|socket hang up|ECONNRESET|ETIMEDOUT|EPIPE/i.test(
|
|
242
|
+
message,
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Name the endpoint that actually failed.
|
|
248
|
+
*
|
|
249
|
+
* The transport reports the connection's base URL, so the thrown message reads
|
|
250
|
+
* "https://host/v1 could not be reached" even though the request went to
|
|
251
|
+
* /v1/chat/completions. That sends people looking at the wrong thing.
|
|
252
|
+
*/
|
|
253
|
+
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
|
+
const retryNote = attempts > 0 ? `,已重试 ${attempts} 次` : "";
|
|
259
|
+
const midStream = emitted ? ",回复中途断开" : "";
|
|
260
|
+
const hint =
|
|
261
|
+
";如果这条会话很长,可能是等待首字超时,可在插件配置里调大 streamIdleTimeoutMs";
|
|
262
|
+
return new Sub2ApiError(
|
|
263
|
+
`与中转站的连接中断(/chat/completions${retryNote}${midStream})${hint}。原始信息:${message}`,
|
|
264
|
+
{ code: error?.code ?? "TRANSPORT", cause: error },
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Sleep between retries. */
|
|
269
|
+
function delay(ms) {
|
|
270
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
271
|
+
}
|
|
272
|
+
|
|
208
273
|
/**
|
|
209
274
|
* The level a request selected, matched by id against the model's declared
|
|
210
275
|
* levels. An unknown id yields undefined rather than guessing.
|
package/lib/config.js
CHANGED
|
@@ -78,6 +78,7 @@ 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
|
+
transportRetries: toCount(field(config, "transportRetries", 1), 1, 5),
|
|
81
82
|
reasoningModels: toStringArray(field(config, "reasoningModels", [])),
|
|
82
83
|
defaultReasoningEffort: String(field(config, "defaultReasoningEffort", "")).trim() || undefined,
|
|
83
84
|
autoProvision: field(config, "autoProvision", true) !== false,
|
|
@@ -85,6 +86,13 @@ export function resolveConfig(config) {
|
|
|
85
86
|
};
|
|
86
87
|
}
|
|
87
88
|
|
|
89
|
+
/** Coerce to a bounded non-negative integer. */
|
|
90
|
+
function toCount(value, fallback, max) {
|
|
91
|
+
const n = typeof value === "number" ? value : Number(value);
|
|
92
|
+
if (!Number.isFinite(n) || n < 0) return fallback;
|
|
93
|
+
return Math.min(Math.trunc(n), max);
|
|
94
|
+
}
|
|
95
|
+
|
|
88
96
|
/** Coerce to a positive multiplier, falling back to the documented default. */
|
|
89
97
|
function toMultiplier(value) {
|
|
90
98
|
const n = typeof value === "number" ? value : Number(value);
|
package/lib/index.js
CHANGED
|
@@ -119,6 +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().transportRetries,
|
|
122
123
|
logger,
|
|
123
124
|
});
|
|
124
125
|
|
package/lib/routes.js
CHANGED
|
@@ -62,8 +62,11 @@ export function makeRoutes(deps) {
|
|
|
62
62
|
|
|
63
63
|
// Back off after a failure: a group with no models would otherwise be
|
|
64
64
|
// retried on every poll, and the answer will not change within seconds.
|
|
65
|
+
// A balance rejection is excluded — funding the account changes the answer
|
|
66
|
+
// immediately, and that is exactly the case this path exists for.
|
|
67
|
+
const blockedByBalance = catalog.lastError?.code === "INSUFFICIENT_BALANCE";
|
|
65
68
|
const failedAt = catalog.lastError?.at;
|
|
66
|
-
if (typeof failedAt === "number" && now - failedAt < 120000) return;
|
|
69
|
+
if (!blockedByBalance && typeof failedAt === "number" && now - failedAt < 120000) return;
|
|
67
70
|
|
|
68
71
|
const empty = catalog.visible().length === 0;
|
|
69
72
|
const funded = Number.isFinite(balance) && balance > 0;
|
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
|
+
transportRetries: count("首字之前连接中断时的重试次数(0-5,默认 1)"),
|
|
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.6",
|
|
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",
|