fix: preserve upstream model errors and native request parameters
This commit is contained in:
+21
-10
@@ -1,6 +1,6 @@
|
||||
import { randomUUID, createHash } from 'node:crypto';
|
||||
|
||||
export const APP_VERSION = '1.20.2';
|
||||
export const APP_VERSION = '1.20.3';
|
||||
|
||||
export function executionEnvironment() {
|
||||
const windows = process.platform === 'win32';
|
||||
@@ -22,7 +22,8 @@ export class ModelRequestError extends Error {
|
||||
}
|
||||
|
||||
export function connectionFailure(error, { endpoint, model, route, attempt, redact = String, timedOut = false, timeout = null }) {
|
||||
if (error instanceof ModelRequestError) return { endpoint, model, route, ...error.diagnostic, attempts: attempt };
|
||||
if (error instanceof ModelRequestError) return { ...error.diagnostic, endpoint: error.diagnostic.endpoint ?? endpoint,
|
||||
model: error.diagnostic.model ?? model, route: error.diagnostic.route ?? route, attempts: attempt };
|
||||
const causes = [error, error?.cause, ...(error?.cause?.errors ?? [])];
|
||||
const code = causes.map(item => item?.code).find(value => typeof value === 'string' && /^[A-Z0-9_]{2,60}$/.test(value)) ?? null;
|
||||
const kind = timedOut || causes.some(item => item?.name === 'TimeoutError') ? 'timeout'
|
||||
@@ -31,7 +32,7 @@ export function connectionFailure(error, { endpoint, model, route, attempt, reda
|
||||
const title = kind === 'timeout' ? '模型服务响应超时' : kind === 'dns' ? '模型服务地址解析失败'
|
||||
: kind === 'certificate' ? '模型服务证书校验失败' : '模型服务连接失败';
|
||||
return {
|
||||
id: randomUUID(), source: 'model_connection', stage: 'model_request', kind, code,
|
||||
id: randomUUID(), source: 'model_connection', origin: 'local', stage: 'model_request', kind, code,
|
||||
endpoint, model, route, attempts: attempt, occurredAt: new Date().toISOString(),
|
||||
retryable: kind !== 'certificate', title,
|
||||
message: title + (code ? '(' + code + ')' : '') + '。' + (timeout ? timeout.kind === 'idle'
|
||||
@@ -45,17 +46,27 @@ export function connectionFailure(error, { endpoint, model, route, attempt, reda
|
||||
};
|
||||
}
|
||||
|
||||
export function httpFailure(status, detail, { endpoint, model, route, attempt }) {
|
||||
const kind = status === 401 || status === 403 ? 'authentication' : status === 429 ? 'rate_limit' : 'service';
|
||||
export function httpFailure(status, detail, { endpoint, model, route, attempt, upstream, attemptId } = {}) {
|
||||
const codes = [upstream?.code, upstream?.type].filter(value => typeof value === 'string').map(value => value.toLowerCase());
|
||||
const quota = codes.some(value => ['insufficient_quota', 'billing_hard_limit_reached', 'billing_limit_reached', 'usage_limit_reached',
|
||||
'credit_balance_too_low', 'insufficient_balance', 'exceeded_quota'].includes(value));
|
||||
const limited = status === 429 || codes.some(value => ['rate_limit_exceeded', 'rate_limit_error', 'requests_limit_exceeded', 'tokens_limit_exceeded'].includes(value));
|
||||
const kind = quota ? 'quota' : status === 401 || status === 403 ? 'authentication' : limited ? 'rate_limit' : 'service';
|
||||
const title = kind === 'authentication' ? '模型服务拒绝了身份验证'
|
||||
: kind === 'rate_limit' ? '模型服务请求达到限额' : '模型服务暂时无法处理请求';
|
||||
: kind === 'quota' ? '模型服务额度或消费限制' : kind === 'rate_limit' ? '模型服务请求达到限额' : '模型服务暂时无法处理请求';
|
||||
return new ModelRequestError({
|
||||
id: randomUUID(), source: 'model_connection', stage: 'model_request', kind, httpStatus: status,
|
||||
id: randomUUID(), source: 'model_connection', origin: 'upstream', stage: 'model_request', kind, httpStatus: status,
|
||||
endpoint, model, route, attempts: attempt, occurredAt: new Date().toISOString(),
|
||||
retryable: [408, 429, 500, 502, 503, 504].includes(status), title,
|
||||
message: title + '(HTTP ' + status + ')。' + (detail ? ' ' + detail : ''), detail,
|
||||
retryable: !quota && !upstream?.retryAfterInvalid && (limited || [408, 500, 502, 503, 504].includes(status)),
|
||||
...(quota ? { failoverAllowed: true } : {}), title,
|
||||
...(upstream ? { upstream, upstreamRequestId: upstream.requestId, code: upstream.code, type: upstream.type,
|
||||
...(upstream.retryAfterMs !== null ? { retryAfterMs: upstream.retryAfterMs } : {}),
|
||||
limitReason: quota ? 'quota' : limited ? codes.some(value => /rate_limit|requests_limit|tokens_limit/.test(value)) ? 'rate' : 'unspecified' : null } : {}),
|
||||
...(attemptId ? { wireAttemptId: attemptId } : {}),
|
||||
message: title + (status >= 400 ? '(HTTP ' + status + ')' : '(上游返回错误内容)') + '。' + (detail ? ' ' + detail : ''), detail,
|
||||
suggestion: kind === 'authentication' ? '在模型连接中核对 API Key、模型权限和接口地址。'
|
||||
: kind === 'rate_limit' ? '等待服务限额恢复后重试;同时检查账户额度。' : '稍后重试,或检查模型服务状态。',
|
||||
: quota ? '核对上游账户余额、消费上限或用量限制。该线路不会自动重复请求;模型池可按规则选择其他可用成员。'
|
||||
: kind === 'rate_limit' ? '依据上游错误码核对限制原因;有 Retry-After 时按原等待时间处理,原因未给出时保持未确定。' : '稍后重试,或检查模型服务状态。',
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user