fix(coding): recover provider context failures
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user