fix(coding): recover provider context failures

This commit is contained in:
2026-08-26 14:42:01 +08:00
parent 9f05e2d7e1
commit a09826676e
8 changed files with 349 additions and 9 deletions

View File

@@ -9,7 +9,10 @@ import {
import { proxyAwareFetch } from '../../utils/proxy-fetch';
import { logger } from '../../utils/logger';
import { getAIGatewayErrorKind } from '../../../shared/ai-gateway-error-kind';
import { isAIGatewayUpstreamSaturated } from '../../../shared/ai-gateway-error-details';
import {
isAIGatewayUpstreamSaturated,
isAIGatewayUserContextMissing,
} from '../../../shared/ai-gateway-error-details';
const AI_PROXY_PREFIX = '/api/ai-proxy/v1';
const MAX_ERROR_LOG_MESSAGE_LENGTH = 600;
@@ -80,6 +83,9 @@ function isExpiredGatewayTokenResponse(status: number, bodyText: string): boolea
}
function getForwardedOneApiStatus(status: number, bodyText: string): number {
if (status === 502 && isAIGatewayUserContextMissing(bodyText)) {
return 401;
}
if (status === 429 && getAIGatewayErrorKind(bodyText) === 'quota_exhausted') {
return 402;
}
@@ -282,6 +288,9 @@ export async function handleAiProxyRoutes(
upstream = upstreamResult.response;
targetUrl = upstreamResult.targetUrl;
} else {
if (isAIGatewayUserContextMissing(bodyText)) {
markWorksSquareAIGatewayCredentialExpired();
}
logNonSuccessOneApiResponseFromBody(upstream, targetUrl, bodyText);
await writeBufferedResponse(res, upstream, bodyText, getForwardedOneApiStatus(upstream.status, bodyText));
return true;
@@ -290,6 +299,9 @@ export async function handleAiProxyRoutes(
if (!upstream.ok) {
const bodyText = await upstream.text();
if (isAIGatewayUserContextMissing(bodyText)) {
markWorksSquareAIGatewayCredentialExpired();
}
logNonSuccessOneApiResponseFromBody(upstream, targetUrl, bodyText);
await writeBufferedResponse(res, upstream, bodyText, getForwardedOneApiStatus(upstream.status, bodyText));
return true;