dsh-unified-router 0.7.4 → 0.7.5
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/lib/apikey-providers.js +12 -1
- package/lib/apikey-quota.js +43 -5
- package/package.json +1 -1
package/lib/apikey-providers.js
CHANGED
|
@@ -913,9 +913,20 @@ const bailian = {
|
|
|
913
913
|
rate_limit_exceeded: 'rpm',
|
|
914
914
|
Throttling: 'rpm',
|
|
915
915
|
'Throttling.RateQuota': 'rpm',
|
|
916
|
+
'Throttling.ServiceOverloaded': 'rpm',
|
|
916
917
|
'Throttling.AllocationQuota': 'tpm',
|
|
918
|
+
'Throttling.ResourceExhausted': 'tpm',
|
|
919
|
+
// 百炼把每分钟 Token 限流也标成 `insufficient_quota`(429 实测样本见
|
|
920
|
+
// throttlePattern),按 TPM 限流处理;真正的额度用尽由 exhaustedPattern
|
|
921
|
+
// 与 exhaustedCodes 拦住。
|
|
922
|
+
insufficient_quota: 'tpm',
|
|
917
923
|
},
|
|
918
|
-
|
|
924
|
+
// 429 实测原文:`Allocated quota exceeded, please increase your quota limit.
|
|
925
|
+
// For details, see: …/error-code#token-limit` —— message 与锚点都指向
|
|
926
|
+
// **每分钟 Token 限流**,不是账户额度耗尽。
|
|
927
|
+
throttlePattern: /token-limit|allocated[\s_-]+quota[\s_-]+exceeded|increase[\s_-]+your[\s_-]+quota[\s_-]+limit|quota[\s_-]+per[\s_-]+minute|\brate[\s_-]+limit|too[\s_-]+many[\s_-]+requests/i,
|
|
928
|
+
exhaustedCodes: ['AllocationQuota.FreeTierOnly'],
|
|
929
|
+
exhaustedPattern: /free[\s_-]+tier|free[\s_-]+quota|欠费|arrears|balance[\s_-]+(?:exhausted|depleted)|out[\s_-]+of[\s_-]+(?:credits?|budget)/i,
|
|
919
930
|
},
|
|
920
931
|
}
|
|
921
932
|
|
package/lib/apikey-quota.js
CHANGED
|
@@ -93,13 +93,39 @@ async function peekApiKeyError(response) {
|
|
|
93
93
|
} catch { return null } finally { clearTimeout(timer); void reader.cancel().catch(() => {}) }
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
/**
|
|
97
|
+
* 通用限流/耗尽判据(默认值,渠道规格可用 `throttlePattern` / `exhaustedPattern` 覆盖)。
|
|
98
|
+
*
|
|
99
|
+
* 为什么用文本判据而不是错误码表:上游对「每分钟 Token 超限」与「账户额度用尽」
|
|
100
|
+
* 用同一个错误码(百炼 429 实测两者都是 `insufficient_quota`),只查码必然误判;
|
|
101
|
+
* 而各家的码表既不稳定也无法穷举。同类实现(magpie 网关)同样以正文措辞为主判据,
|
|
102
|
+
* 只在少数明确码上走快路径 —— 这里沿用同一口径。
|
|
103
|
+
*
|
|
104
|
+
* 判定顺序:**先认「明确指向用尽」且不指向限流的**(真耗尽不能换号/不能当可重试),
|
|
105
|
+
* 其余落到限流或未知。文本取自错误正文的 message。
|
|
106
|
+
*/
|
|
107
|
+
const DEFAULT_THROTTLE_PATTERN = new RegExp([
|
|
108
|
+
'rate[\\s_.-]?limit', 'frequency[\\s_.-]?limit', 'too many requests',
|
|
109
|
+
'per[\\s_.-]?(?:second|sec|minute|min)\\b', '\\b[rt]p[ms]\\b',
|
|
110
|
+
'(?:token|call|request)[\\s_.-]+rate[\\s_.-]+limit',
|
|
111
|
+
'每分钟', '每秒', '频率', '太频繁', '过于频繁', '速率',
|
|
112
|
+
'token[\\s_.-]?limit',
|
|
113
|
+
].join('|'), 'i')
|
|
114
|
+
const DEFAULT_EXHAUSTED_PATTERN = new RegExp([
|
|
115
|
+
'insufficient[\\s_.-]?(?:balance|credit|fund)', '\\bcredits? (?:are )?exhausted\\b',
|
|
116
|
+
'credit[\\s_.-]?balance[\\s_.-]?exhausted', 'out of (?:credits?|budget)',
|
|
117
|
+
'budget.{0,32}(?:exceeded|exhausted)', 'free[\\s_.-]?tier', 'free[\\s_.-]?quota',
|
|
118
|
+
'余额不足', '欠费', '请充值', 'arrears',
|
|
119
|
+
].join('|'), 'i')
|
|
120
|
+
|
|
96
121
|
/**
|
|
97
122
|
* 识别限流信号。
|
|
98
123
|
*
|
|
99
|
-
*
|
|
124
|
+
* 规格用四个字段表达差异:
|
|
100
125
|
* - `scanOnOk`:有的上游用 2xx + 业务码回限流(云知声 `100501`/`100502`),
|
|
101
126
|
* 这类渠道连成功响应也要读 body;其余渠道只在 429 时读。
|
|
102
127
|
* - `codes`:业务码 → `rpm` / `tpm` 的映射。
|
|
128
|
+
* - `throttlePattern` / `exhaustedPattern`:可选覆盖默认文本判据。
|
|
103
129
|
*/
|
|
104
130
|
export async function detectApiKeyRateLimit(spec, response, now = Date.now()) {
|
|
105
131
|
const recipe = spec?.rateLimit
|
|
@@ -107,16 +133,28 @@ export async function detectApiKeyRateLimit(spec, response, now = Date.now()) {
|
|
|
107
133
|
// 只在 429,或规格声明「2xx 也可能带限流」时才读 body。
|
|
108
134
|
if (response.status !== 429 && !(recipe.scanOnOk === true && response.ok)) return null
|
|
109
135
|
const body = await peekApiKeyError(response)
|
|
110
|
-
|
|
111
|
-
|
|
136
|
+
// 错误信封有两种形状:OpenAI 兼容(`{error:{code,type,message}}`)与阿里云风格
|
|
137
|
+
// (顶层 `{code,type,message}`)。只查 `error.*` 会把后者读成空串,进而把限流
|
|
138
|
+
// 误判为「unknown」。两种都要取。
|
|
139
|
+
const code = String(body?.base_resp?.status_code ?? body?.error?.code ?? body?.code ?? body?.error?.type ?? body?.type ?? '')
|
|
140
|
+
const type = String(body?.error?.type ?? body?.type ?? '')
|
|
141
|
+
const message = String(body?.error?.message ?? body?.message ?? body?.msg ?? '')
|
|
112
142
|
// 额度耗尽不是限流:这类信号交给额度面板,不参与换号。
|
|
113
143
|
const exhausted = new Set([
|
|
114
144
|
'100104', '220107', '220108', '220109',
|
|
115
145
|
'insufficient_quota', 'insufficient_balance', 'billing_hard_limit_reached',
|
|
116
146
|
...(recipe.exhaustedCodes ?? []),
|
|
117
147
|
])
|
|
118
|
-
|
|
119
|
-
|
|
148
|
+
// ⚠️ 上游会把**每分钟 Token 限流**也标成 `insufficient_quota`(百炼 429 实测:
|
|
149
|
+
// `{"code":"insufficient_quota","message":"Allocated quota exceeded, please
|
|
150
|
+
// increase your quota limit. …#token-limit"}`),只按错误码一刀切会把可恢复的
|
|
151
|
+
// TPM 限流当成账户额度用尽。故用正文二次判定:明确指向限流的不算耗尽,
|
|
152
|
+
// 明确指向额度/余额用尽的仍然排除。
|
|
153
|
+
const looksThrottled = (recipe.throttlePattern ?? DEFAULT_THROTTLE_PATTERN).test(message)
|
|
154
|
+
const looksExhausted = (recipe.exhaustedPattern ?? DEFAULT_EXHAUSTED_PATTERN).test(message)
|
|
155
|
+
if (looksExhausted && !looksThrottled) return null
|
|
156
|
+
if ((exhausted.has(code) || exhausted.has(type)) && !looksThrottled) return null
|
|
157
|
+
const reason = recipe.codes?.[code] ?? recipe.codes?.[type] ?? (looksThrottled ? 'tpm' : 'unknown')
|
|
120
158
|
if (reason === 'unknown' && response.status !== 429) return null
|
|
121
159
|
const header = response.headers.get('retry-after')?.trim()
|
|
122
160
|
const delay = header && /^\d+(?:\.\d+)?$/.test(header) ? Number(header) * 1000
|