fix: retry Codex overload error codes
This commit is contained in:
+52
-1
@@ -4403,6 +4403,47 @@ function truncateRetryableMessage(value, maxLength = 280) {
|
||||
return text.length > maxLength ? `${text.slice(0, maxLength - 3)}...` : text;
|
||||
}
|
||||
|
||||
const RETRYABLE_OVERLOAD_ERROR_CODES = new Set([
|
||||
"server_is_overloaded",
|
||||
"slow_down",
|
||||
]);
|
||||
|
||||
function findRetryableOverloadErrorCode(value, state = { seen: new Set() }, depth = 0) {
|
||||
if (!value || typeof value !== "object" || depth > 6 || state.seen.has(value)) {
|
||||
return null;
|
||||
}
|
||||
state.seen.add(value);
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
for (const item of value) {
|
||||
const match = findRetryableOverloadErrorCode(item, state, depth + 1);
|
||||
if (match) {
|
||||
return match;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const code = typeof value.code === "string" ? value.code.trim().toLowerCase() : "";
|
||||
if (RETRYABLE_OVERLOAD_ERROR_CODES.has(code)) {
|
||||
return {
|
||||
matched_code: code,
|
||||
matched_pattern: `error_code:${code}`,
|
||||
matched_message: truncateRetryableMessage(
|
||||
firstNonEmptyString(value.message, value.detail, value.description, code),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
for (const nested of Object.values(value)) {
|
||||
const match = findRetryableOverloadErrorCode(nested, state, depth + 1);
|
||||
if (match) {
|
||||
return match;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function findRetryableUpstreamErrorMatch(config, upstreamStatusCode, parsedBody, bodyText) {
|
||||
const retryableMessages = normalizePhraseList(
|
||||
config.retryable_error_messages,
|
||||
@@ -4420,10 +4461,16 @@ function findRetryableUpstreamErrorMatch(config, upstreamStatusCode, parsedBody,
|
||||
retryableStatusCodes.length === 0 ||
|
||||
retryableStatusCodes.includes(upstreamStatusCode)
|
||||
);
|
||||
if (!statusEligible && !isRetryableErrorPayloadShape(parsedBody)) {
|
||||
const failurePayload = isRetryableErrorPayloadShape(parsedBody);
|
||||
if (!statusEligible && !failurePayload) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const overloadCodeMatch = findRetryableOverloadErrorCode(parsedBody);
|
||||
if (overloadCodeMatch) {
|
||||
return overloadCodeMatch;
|
||||
}
|
||||
|
||||
return matchRetryableMessage(config, parsedBody, bodyText);
|
||||
}
|
||||
|
||||
@@ -4525,6 +4572,10 @@ function findRetryableStreamErrorMatch(config, parsedBody, bodyText, eventName =
|
||||
if (!isRetryableErrorPayloadShape(parsedBody, eventName)) {
|
||||
return null;
|
||||
}
|
||||
const overloadCodeMatch = findRetryableOverloadErrorCode(parsedBody);
|
||||
if (overloadCodeMatch) {
|
||||
return overloadCodeMatch;
|
||||
}
|
||||
return matchRetryableMessage(config, parsedBody, bodyText);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user