2026-01-14 01:08:15 +00:00
import type { AssistantMessage } from "@mariozechner/pi-ai" ;
2026-01-30 03:15:10 +01:00
import type { OpenClawConfig } from "../../config/config.js" ;
2026-01-14 01:08:15 +00:00
import type { FailoverReason } from "./types.js" ;
2026-02-01 10:03:47 +09:00
import { formatSandboxToolPolicyBlockedMessage } from "../sandbox.js" ;
2026-01-14 01:08:15 +00:00
2026-02-05 17:58:43 -04:00
export const BILLING_ERROR_USER_MESSAGE =
"⚠️ API provider returned a billing error — your API key has run out of credits or has an insufficient balance. Check your provider's billing dashboard and top up or switch to a different API key." ;
2026-01-14 01:08:15 +00:00
export function isContextOverflowError ( errorMessage? : string ) : boolean {
2026-01-31 16:19:20 +09:00
if ( ! errorMessage ) {
return false ;
}
2026-01-14 01:08:15 +00:00
const lower = errorMessage . toLowerCase ( ) ;
2026-01-24 19:09:24 -03:00
const hasRequestSizeExceeds = lower . includes ( "request size exceeds" ) ;
const hasContextWindow =
lower . includes ( "context window" ) ||
lower . includes ( "context length" ) ||
lower . includes ( "maximum context length" ) ;
2026-01-14 01:08:15 +00:00
return (
lower . includes ( "request_too_large" ) ||
lower . includes ( "request exceeds the maximum size" ) ||
lower . includes ( "context length exceeded" ) ||
lower . includes ( "maximum context length" ) ||
lower . includes ( "prompt is too long" ) ||
2026-01-24 19:09:24 -03:00
lower . includes ( "exceeds model context window" ) ||
( hasRequestSizeExceeds && hasContextWindow ) ||
2026-02-09 00:22:57 -07:00
lower . includes ( "context overflow:" ) ||
2026-01-14 01:08:15 +00:00
( lower . includes ( "413" ) && lower . includes ( "too large" ) )
) ;
}
2026-01-20 10:06:47 +00:00
const CONTEXT_WINDOW_TOO_SMALL_RE = /context window.*(too small|minimum is)/i ;
const CONTEXT_OVERFLOW_HINT_RE =
/context.*overflow|context window.*(too (?:large|long)|exceed|over|limit|max(?:imum)?|requested|sent|tokens)|(?:prompt|request|input).*(too (?:large|long)|exceed|over|limit|max(?:imum)?)/i ;
export function isLikelyContextOverflowError ( errorMessage? : string ) : boolean {
2026-01-31 16:19:20 +09:00
if ( ! errorMessage ) {
return false ;
}
if ( CONTEXT_WINDOW_TOO_SMALL_RE . test ( errorMessage ) ) {
return false ;
}
if ( isContextOverflowError ( errorMessage ) ) {
return true ;
}
2026-01-20 10:06:47 +00:00
return CONTEXT_OVERFLOW_HINT_RE . test ( errorMessage ) ;
}
2026-01-14 01:08:15 +00:00
export function isCompactionFailureError ( errorMessage? : string ) : boolean {
2026-01-31 16:19:20 +09:00
if ( ! errorMessage ) {
return false ;
}
if ( ! isContextOverflowError ( errorMessage ) ) {
return false ;
}
2026-01-14 01:08:15 +00:00
const lower = errorMessage . toLowerCase ( ) ;
return (
lower . includes ( "summarization failed" ) ||
lower . includes ( "auto-compaction" ) ||
lower . includes ( "compaction failed" ) ||
lower . includes ( "compaction" )
) ;
}
2026-01-15 01:34:19 +00:00
const ERROR_PAYLOAD_PREFIX_RE =
/^(?:error|api\s*error|apierror|openai\s*error|anthropic\s*error|gateway\s*error)[:\s-]+/i ;
2026-01-16 03:00:40 +00:00
const FINAL_TAG_RE = /<\s*\/?\s*final\s*>/gi ;
const ERROR_PREFIX_RE =
/^(?:error|api\s*error|openai\s*error|anthropic\s*error|gateway\s*error|request failed|failed|exception)[:\s-]+/i ;
2026-02-07 17:07:12 -08:00
const CONTEXT_OVERFLOW_ERROR_HEAD_RE =
/^(?:context overflow:|request_too_large\b|request size exceeds\b|request exceeds the maximum size\b|context length exceeded\b|maximum context length\b|prompt is too long\b|exceeds model context window\b)/i ;
2026-01-16 03:00:40 +00:00
const HTTP_STATUS_PREFIX_RE = /^(?:http\s*)?(\d{3})\s+(.+)$/i ;
const HTTP_ERROR_HINTS = [
"error" ,
"bad request" ,
"not found" ,
"unauthorized" ,
"forbidden" ,
"internal server" ,
"service unavailable" ,
"gateway" ,
"rate limit" ,
"overloaded" ,
"timeout" ,
"timed out" ,
"invalid" ,
"too many requests" ,
"permission" ,
] ;
function stripFinalTagsFromText ( text : string ) : string {
2026-01-31 16:19:20 +09:00
if ( ! text ) {
return text ;
}
2026-01-16 03:00:40 +00:00
return text . replace ( FINAL_TAG_RE , "" ) ;
}
2026-01-25 10:22:47 +00:00
function collapseConsecutiveDuplicateBlocks ( text : string ) : string {
const trimmed = text . trim ( ) ;
2026-01-31 16:19:20 +09:00
if ( ! trimmed ) {
return text ;
}
2026-01-25 10:22:47 +00:00
const blocks = trimmed . split ( /\n{2,}/ ) ;
2026-01-31 16:19:20 +09:00
if ( blocks . length < 2 ) {
return text ;
}
2026-01-25 10:22:47 +00:00
const normalizeBlock = ( value : string ) = > value . trim ( ) . replace ( /\s+/g , " " ) ;
const result : string [ ] = [ ] ;
let lastNormalized : string | null = null ;
for ( const block of blocks ) {
const normalized = normalizeBlock ( block ) ;
if ( lastNormalized && normalized === lastNormalized ) {
continue ;
}
result . push ( block . trim ( ) ) ;
lastNormalized = normalized ;
}
2026-01-31 16:19:20 +09:00
if ( result . length === blocks . length ) {
return text ;
}
2026-01-25 10:22:47 +00:00
return result . join ( "\n\n" ) ;
}
2026-01-16 03:00:40 +00:00
function isLikelyHttpErrorText ( raw : string ) : boolean {
const match = raw . match ( HTTP_STATUS_PREFIX_RE ) ;
2026-01-31 16:19:20 +09:00
if ( ! match ) {
return false ;
}
2026-01-16 03:00:40 +00:00
const code = Number ( match [ 1 ] ) ;
2026-01-31 16:19:20 +09:00
if ( ! Number . isFinite ( code ) || code < 400 ) {
return false ;
}
2026-01-16 03:00:40 +00:00
const message = match [ 2 ] . toLowerCase ( ) ;
return HTTP_ERROR_HINTS . some ( ( hint ) = > message . includes ( hint ) ) ;
}
2026-01-15 01:34:19 +00:00
2026-02-07 17:07:12 -08:00
function shouldRewriteContextOverflowText ( raw : string ) : boolean {
if ( ! isContextOverflowError ( raw ) ) {
return false ;
}
return (
isRawApiErrorPayload ( raw ) ||
isLikelyHttpErrorText ( raw ) ||
ERROR_PREFIX_RE . test ( raw ) ||
CONTEXT_OVERFLOW_ERROR_HEAD_RE . test ( raw )
) ;
}
2026-01-15 01:34:19 +00:00
type ErrorPayload = Record < string , unknown > ;
function isErrorPayloadObject ( payload : unknown ) : payload is ErrorPayload {
2026-01-31 16:19:20 +09:00
if ( ! payload || typeof payload !== "object" || Array . isArray ( payload ) ) {
return false ;
}
2026-01-15 01:34:19 +00:00
const record = payload as ErrorPayload ;
2026-01-31 16:19:20 +09:00
if ( record . type === "error" ) {
return true ;
}
if ( typeof record . request_id === "string" || typeof record . requestId === "string" ) {
return true ;
}
2026-01-15 01:34:19 +00:00
if ( "error" in record ) {
const err = record . error ;
if ( err && typeof err === "object" && ! Array . isArray ( err ) ) {
const errRecord = err as ErrorPayload ;
if (
typeof errRecord . message === "string" ||
typeof errRecord . type === "string" ||
typeof errRecord . code === "string"
) {
return true ;
}
}
}
return false ;
}
function parseApiErrorPayload ( raw : string ) : ErrorPayload | null {
2026-01-31 16:19:20 +09:00
if ( ! raw ) {
return null ;
}
2026-01-15 01:34:19 +00:00
const trimmed = raw . trim ( ) ;
2026-01-31 16:19:20 +09:00
if ( ! trimmed ) {
return null ;
}
2026-01-15 01:34:19 +00:00
const candidates = [ trimmed ] ;
if ( ERROR_PAYLOAD_PREFIX_RE . test ( trimmed ) ) {
candidates . push ( trimmed . replace ( ERROR_PAYLOAD_PREFIX_RE , "" ) . trim ( ) ) ;
}
for ( const candidate of candidates ) {
2026-01-31 16:19:20 +09:00
if ( ! candidate . startsWith ( "{" ) || ! candidate . endsWith ( "}" ) ) {
continue ;
}
2026-01-15 01:34:19 +00:00
try {
const parsed = JSON . parse ( candidate ) as unknown ;
2026-01-31 16:19:20 +09:00
if ( isErrorPayloadObject ( parsed ) ) {
return parsed ;
}
2026-01-15 01:34:19 +00:00
} catch {
// ignore parse errors
}
}
return null ;
}
function stableStringify ( value : unknown ) : string {
if ( ! value || typeof value !== "object" ) {
return JSON . stringify ( value ) ? ? "null" ;
}
if ( Array . isArray ( value ) ) {
return ` [ ${ value . map ( ( entry ) = > stableStringify ( entry ) ) . join ( "," ) } ] ` ;
}
const record = value as Record < string , unknown > ;
2026-01-31 16:03:28 +09:00
const keys = Object . keys ( record ) . toSorted ( ) ;
2026-01-15 01:34:19 +00:00
const entries = keys . map ( ( key ) = > ` ${ JSON . stringify ( key ) } : ${ stableStringify ( record [ key ] ) } ` ) ;
return ` { ${ entries . join ( "," ) } } ` ;
}
export function getApiErrorPayloadFingerprint ( raw? : string ) : string | null {
2026-01-31 16:19:20 +09:00
if ( ! raw ) {
return null ;
}
2026-01-15 01:34:19 +00:00
const payload = parseApiErrorPayload ( raw ) ;
2026-01-31 16:19:20 +09:00
if ( ! payload ) {
return null ;
}
2026-01-15 01:34:19 +00:00
return stableStringify ( payload ) ;
}
export function isRawApiErrorPayload ( raw? : string ) : boolean {
return getApiErrorPayloadFingerprint ( raw ) !== null ;
}
2026-01-15 08:16:44 +00:00
export type ApiErrorInfo = {
httpCode? : string ;
type ? : string ;
message? : string ;
requestId? : string ;
} ;
export function parseApiErrorInfo ( raw? : string ) : ApiErrorInfo | null {
2026-01-31 16:19:20 +09:00
if ( ! raw ) {
return null ;
}
2026-01-15 08:16:44 +00:00
const trimmed = raw . trim ( ) ;
2026-01-31 16:19:20 +09:00
if ( ! trimmed ) {
return null ;
}
2026-01-15 08:16:44 +00:00
let httpCode : string | undefined ;
let candidate = trimmed ;
const httpPrefixMatch = candidate . match ( / ^ ( \ d { 3 } ) \ s + ( . + ) $ / s ) ;
if ( httpPrefixMatch ) {
httpCode = httpPrefixMatch [ 1 ] ;
candidate = httpPrefixMatch [ 2 ] . trim ( ) ;
}
const payload = parseApiErrorPayload ( candidate ) ;
2026-01-31 16:19:20 +09:00
if ( ! payload ) {
return null ;
}
2026-01-15 08:16:44 +00:00
const requestId =
typeof payload . request_id === "string"
? payload . request_id
: typeof payload . requestId === "string"
? payload . requestId
: undefined ;
const topType = typeof payload . type === "string" ? payload.type : undefined ;
const topMessage = typeof payload . message === "string" ? payload.message : undefined ;
let errType : string | undefined ;
let errMessage : string | undefined ;
if ( payload . error && typeof payload . error === "object" && ! Array . isArray ( payload . error ) ) {
const err = payload . error as Record < string , unknown > ;
2026-01-31 16:19:20 +09:00
if ( typeof err . type === "string" ) {
errType = err . type ;
}
if ( typeof err . code === "string" && ! errType ) {
errType = err . code ;
}
if ( typeof err . message === "string" ) {
errMessage = err . message ;
}
2026-01-15 08:16:44 +00:00
}
return {
httpCode ,
type : errType ? ? topType ,
message : errMessage ? ? topMessage ,
requestId ,
} ;
}
export function formatRawAssistantErrorForUi ( raw? : string ) : string {
const trimmed = ( raw ? ? "" ) . trim ( ) ;
2026-01-31 16:19:20 +09:00
if ( ! trimmed ) {
return "LLM request failed with an unknown error." ;
}
2026-01-15 08:16:44 +00:00
2026-01-22 22:24:25 +00:00
const httpMatch = trimmed . match ( HTTP_STATUS_PREFIX_RE ) ;
if ( httpMatch ) {
const rest = httpMatch [ 2 ] . trim ( ) ;
if ( ! rest . startsWith ( "{" ) ) {
return ` HTTP ${ httpMatch [ 1 ] } : ${ rest } ` ;
}
}
2026-01-15 08:16:44 +00:00
const info = parseApiErrorInfo ( trimmed ) ;
if ( info ? . message ) {
const prefix = info . httpCode ? ` HTTP ${ info . httpCode } ` : "LLM error" ;
const type = info . type ? ` ${ info . type } ` : "" ;
const requestId = info . requestId ? ` (request_id: ${ info . requestId } ) ` : "" ;
return ` ${ prefix } ${ type } : ${ info . message } ${ requestId } ` ;
}
return trimmed . length > 600 ? ` ${ trimmed . slice ( 0 , 600 ) } … ` : trimmed ;
}
2026-01-14 01:08:15 +00:00
export function formatAssistantErrorText (
msg : AssistantMessage ,
2026-01-30 03:15:10 +01:00
opts ? : { cfg? : OpenClawConfig ; sessionKey? : string } ,
2026-01-14 01:08:15 +00:00
) : string | undefined {
2026-01-16 03:00:40 +00:00
// Also format errors if errorMessage is present, even if stopReason isn't "error"
2026-01-14 01:08:15 +00:00
const raw = ( msg . errorMessage ? ? "" ) . trim ( ) ;
2026-01-31 16:19:20 +09:00
if ( msg . stopReason !== "error" && ! raw ) {
return undefined ;
}
if ( ! raw ) {
return "LLM request failed with an unknown error." ;
}
2026-01-14 01:08:15 +00:00
const unknownTool =
raw . match ( /unknown tool[:\s]+["']?([a-z0-9_-]+)["']?/i ) ? ?
2026-01-14 14:31:43 +00:00
raw . match ( /tool\s+["']?([a-z0-9_-]+)["']?\s+(?:not found|is not available)/i ) ;
2026-01-14 01:08:15 +00:00
if ( unknownTool ? . [ 1 ] ) {
const rewritten = formatSandboxToolPolicyBlockedMessage ( {
cfg : opts?.cfg ,
sessionKey : opts?.sessionKey ,
toolName : unknownTool [ 1 ] ,
} ) ;
2026-01-31 16:19:20 +09:00
if ( rewritten ) {
return rewritten ;
}
2026-01-14 01:08:15 +00:00
}
if ( isContextOverflowError ( raw ) ) {
return (
"Context overflow: prompt too large for the model. " +
"Try again with less input or a larger-context model."
) ;
}
2026-01-16 03:00:40 +00:00
// Catch role ordering errors - including JSON-wrapped and "400" prefix variants
2026-01-16 03:30:56 +00:00
if (
/incorrect role information|roles must alternate|400.*role|"message".*role.*information/i . test (
raw ,
)
) {
2026-01-14 01:08:15 +00:00
return (
"Message ordering conflict - please try again. " +
"If this persists, use /new to start a fresh session."
) ;
}
2026-02-03 05:17:42 +08:00
if ( isMissingToolCallInputError ( raw ) ) {
return (
"Session history looks corrupted (tool call input missing). " +
"Use /new to start a fresh session. " +
"If this keeps happening, reset the session or delete the corrupted session transcript."
) ;
}
2026-01-14 14:31:43 +00:00
const invalidRequest = raw . match ( /"type":"invalid_request_error".*?"message":"([^"]+)"/ ) ;
2026-01-14 01:08:15 +00:00
if ( invalidRequest ? . [ 1 ] ) {
return ` LLM request rejected: ${ invalidRequest [ 1 ] } ` ;
}
if ( isOverloadedErrorMessage ( raw ) ) {
return "The AI service is temporarily overloaded. Please try again in a moment." ;
}
2026-02-05 17:58:43 -04:00
if ( isBillingErrorMessage ( raw ) ) {
return BILLING_ERROR_USER_MESSAGE ;
}
2026-01-22 22:24:25 +00:00
if ( isLikelyHttpErrorText ( raw ) || isRawApiErrorPayload ( raw ) ) {
return formatRawAssistantErrorForUi ( raw ) ;
2026-01-15 01:34:19 +00:00
}
2026-01-16 03:00:40 +00:00
// Never return raw unhandled errors - log for debugging but return safe message
if ( raw . length > 600 ) {
console . warn ( "[formatAssistantErrorText] Long error truncated:" , raw . slice ( 0 , 200 ) ) ;
}
2026-01-14 01:08:15 +00:00
return raw . length > 600 ? ` ${ raw . slice ( 0 , 600 ) } … ` : raw ;
}
2026-01-16 03:00:40 +00:00
export function sanitizeUserFacingText ( text : string ) : string {
2026-01-31 16:19:20 +09:00
if ( ! text ) {
return text ;
}
2026-01-16 03:00:40 +00:00
const stripped = stripFinalTagsFromText ( text ) ;
const trimmed = stripped . trim ( ) ;
2026-01-31 16:19:20 +09:00
if ( ! trimmed ) {
return stripped ;
}
2026-01-16 03:00:40 +00:00
if ( /incorrect role information|roles must alternate/i . test ( trimmed ) ) {
return (
"Message ordering conflict - please try again. " +
"If this persists, use /new to start a fresh session."
) ;
}
2026-02-07 17:07:12 -08:00
if ( shouldRewriteContextOverflowText ( trimmed ) ) {
2026-01-16 03:00:40 +00:00
return (
"Context overflow: prompt too large for the model. " +
"Try again with less input or a larger-context model."
) ;
}
2026-02-05 17:58:43 -04:00
if ( isBillingErrorMessage ( trimmed ) ) {
return BILLING_ERROR_USER_MESSAGE ;
}
2026-01-16 03:00:40 +00:00
if ( isRawApiErrorPayload ( trimmed ) || isLikelyHttpErrorText ( trimmed ) ) {
2026-01-22 22:24:25 +00:00
return formatRawAssistantErrorForUi ( trimmed ) ;
2026-01-16 03:00:40 +00:00
}
if ( ERROR_PREFIX_RE . test ( trimmed ) ) {
if ( isOverloadedErrorMessage ( trimmed ) || isRateLimitErrorMessage ( trimmed ) ) {
return "The AI service is temporarily overloaded. Please try again in a moment." ;
}
if ( isTimeoutErrorMessage ( trimmed ) ) {
return "LLM request timed out." ;
}
2026-01-22 22:24:25 +00:00
return formatRawAssistantErrorForUi ( trimmed ) ;
2026-01-16 03:00:40 +00:00
}
2026-01-25 10:22:47 +00:00
return collapseConsecutiveDuplicateBlocks ( stripped ) ;
2026-01-16 03:00:40 +00:00
}
2026-01-14 14:31:43 +00:00
export function isRateLimitAssistantError ( msg : AssistantMessage | undefined ) : boolean {
2026-01-31 16:19:20 +09:00
if ( ! msg || msg . stopReason !== "error" ) {
return false ;
}
2026-01-14 01:08:15 +00:00
return isRateLimitErrorMessage ( msg . errorMessage ? ? "" ) ;
}
type ErrorPattern = RegExp | string ;
const ERROR_PATTERNS = {
rateLimit : [
/rate[_ ]limit|too many requests|429/ ,
"exceeded your current quota" ,
"resource has been exhausted" ,
"quota exceeded" ,
"resource_exhausted" ,
"usage limit" ,
] ,
2026-01-14 14:31:43 +00:00
overloaded : [ /overloaded_error|"type"\s*:\s*"overloaded_error"/i , "overloaded" ] ,
timeout : [ "timeout" , "timed out" , "deadline exceeded" , "context deadline exceeded" ] ,
2026-01-14 01:08:15 +00:00
billing : [
/\b402\b/ ,
"payment required" ,
"insufficient credits" ,
"credit balance" ,
"plans & billing" ,
] ,
auth : [
/invalid[_ ]?api[_ ]?key/ ,
"incorrect api key" ,
"invalid token" ,
"authentication" ,
2026-01-20 09:31:24 +00:00
"re-authenticate" ,
"oauth token refresh failed" ,
2026-01-14 01:08:15 +00:00
"unauthorized" ,
"forbidden" ,
"access denied" ,
"expired" ,
"token has expired" ,
/\b401\b/ ,
/\b403\b/ ,
"no credentials found" ,
"no api key found" ,
] ,
format : [
"string should match pattern" ,
"tool_use.id" ,
"tool_use_id" ,
"messages.1.content.1.tool_use.id" ,
"invalid request format" ,
] ,
} as const ;
2026-02-03 05:17:42 +08:00
const TOOL_CALL_INPUT_MISSING_RE =
/tool_(?:use|call)\.(?:input|arguments).*?(?:field required|required)/i ;
const TOOL_CALL_INPUT_PATH_RE =
/messages\.\d+\.content\.\d+\.tool_(?:use|call)\.(?:input|arguments)/i ;
2026-01-18 15:19:25 +00:00
const IMAGE_DIMENSION_ERROR_RE =
/image dimensions exceed max allowed size for many-image requests:\s*(\d+)\s*pixels/i ;
const IMAGE_DIMENSION_PATH_RE = /messages\.(\d+)\.content\.(\d+)\.image/i ;
2026-01-27 15:59:11 -06:00
const IMAGE_SIZE_ERROR_RE = /image exceeds\s*(\d+(?:\.\d+)?)\s*mb/i ;
2026-01-18 15:19:25 +00:00
2026-01-14 14:31:43 +00:00
function matchesErrorPatterns ( raw : string , patterns : readonly ErrorPattern [ ] ) : boolean {
2026-01-31 16:19:20 +09:00
if ( ! raw ) {
return false ;
}
2026-01-14 01:08:15 +00:00
const value = raw . toLowerCase ( ) ;
return patterns . some ( ( pattern ) = >
pattern instanceof RegExp ? pattern . test ( value ) : value . includes ( pattern ) ,
) ;
}
export function isRateLimitErrorMessage ( raw : string ) : boolean {
return matchesErrorPatterns ( raw , ERROR_PATTERNS . rateLimit ) ;
}
export function isTimeoutErrorMessage ( raw : string ) : boolean {
return matchesErrorPatterns ( raw , ERROR_PATTERNS . timeout ) ;
}
export function isBillingErrorMessage ( raw : string ) : boolean {
const value = raw . toLowerCase ( ) ;
2026-01-31 16:19:20 +09:00
if ( ! value ) {
return false ;
}
if ( matchesErrorPatterns ( value , ERROR_PATTERNS . billing ) ) {
return true ;
}
2026-01-14 01:08:15 +00:00
return (
value . includes ( "billing" ) &&
( value . includes ( "upgrade" ) ||
value . includes ( "credits" ) ||
value . includes ( "payment" ) ||
value . includes ( "plan" ) )
) ;
}
2026-02-03 05:17:42 +08:00
export function isMissingToolCallInputError ( raw : string ) : boolean {
if ( ! raw ) {
return false ;
}
return TOOL_CALL_INPUT_MISSING_RE . test ( raw ) || TOOL_CALL_INPUT_PATH_RE . test ( raw ) ;
}
2026-01-14 14:31:43 +00:00
export function isBillingAssistantError ( msg : AssistantMessage | undefined ) : boolean {
2026-01-31 16:19:20 +09:00
if ( ! msg || msg . stopReason !== "error" ) {
return false ;
}
2026-01-14 01:08:15 +00:00
return isBillingErrorMessage ( msg . errorMessage ? ? "" ) ;
}
export function isAuthErrorMessage ( raw : string ) : boolean {
return matchesErrorPatterns ( raw , ERROR_PATTERNS . auth ) ;
}
export function isOverloadedErrorMessage ( raw : string ) : boolean {
return matchesErrorPatterns ( raw , ERROR_PATTERNS . overloaded ) ;
}
2026-01-18 15:19:25 +00:00
export function parseImageDimensionError ( raw : string ) : {
maxDimensionPx? : number ;
messageIndex? : number ;
contentIndex? : number ;
raw : string ;
} | null {
2026-01-31 16:19:20 +09:00
if ( ! raw ) {
return null ;
}
2026-01-18 15:19:25 +00:00
const lower = raw . toLowerCase ( ) ;
2026-01-31 16:19:20 +09:00
if ( ! lower . includes ( "image dimensions exceed max allowed size" ) ) {
return null ;
}
2026-01-18 15:19:25 +00:00
const limitMatch = raw . match ( IMAGE_DIMENSION_ERROR_RE ) ;
const pathMatch = raw . match ( IMAGE_DIMENSION_PATH_RE ) ;
return {
maxDimensionPx : limitMatch?. [ 1 ] ? Number . parseInt ( limitMatch [ 1 ] , 10 ) : undefined ,
messageIndex : pathMatch?. [ 1 ] ? Number . parseInt ( pathMatch [ 1 ] , 10 ) : undefined ,
contentIndex : pathMatch?. [ 2 ] ? Number . parseInt ( pathMatch [ 2 ] , 10 ) : undefined ,
raw ,
} ;
}
export function isImageDimensionErrorMessage ( raw : string ) : boolean {
return Boolean ( parseImageDimensionError ( raw ) ) ;
}
2026-01-27 15:59:11 -06:00
export function parseImageSizeError ( raw : string ) : {
maxMb? : number ;
raw : string ;
} | null {
2026-01-31 16:19:20 +09:00
if ( ! raw ) {
return null ;
}
2026-01-27 15:59:11 -06:00
const lower = raw . toLowerCase ( ) ;
2026-01-31 16:19:20 +09:00
if ( ! lower . includes ( "image exceeds" ) || ! lower . includes ( "mb" ) ) {
return null ;
}
2026-01-27 15:59:11 -06:00
const match = raw . match ( IMAGE_SIZE_ERROR_RE ) ;
return {
maxMb : match?. [ 1 ] ? Number . parseFloat ( match [ 1 ] ) : undefined ,
raw ,
} ;
}
2026-01-27 22:21:51 +05:30
export function isImageSizeError ( errorMessage? : string ) : boolean {
2026-01-31 16:19:20 +09:00
if ( ! errorMessage ) {
return false ;
}
2026-01-27 15:59:11 -06:00
return Boolean ( parseImageSizeError ( errorMessage ) ) ;
2026-01-27 22:21:51 +05:30
}
2026-01-14 01:08:15 +00:00
export function isCloudCodeAssistFormatError ( raw : string ) : boolean {
2026-01-18 15:19:25 +00:00
return ! isImageDimensionErrorMessage ( raw ) && matchesErrorPatterns ( raw , ERROR_PATTERNS . format ) ;
2026-01-14 01:08:15 +00:00
}
2026-01-14 14:31:43 +00:00
export function isAuthAssistantError ( msg : AssistantMessage | undefined ) : boolean {
2026-01-31 16:19:20 +09:00
if ( ! msg || msg . stopReason !== "error" ) {
return false ;
}
2026-01-14 01:08:15 +00:00
return isAuthErrorMessage ( msg . errorMessage ? ? "" ) ;
}
export function classifyFailoverReason ( raw : string ) : FailoverReason | null {
2026-01-31 16:19:20 +09:00
if ( isImageDimensionErrorMessage ( raw ) ) {
return null ;
}
if ( isImageSizeError ( raw ) ) {
return null ;
}
if ( isRateLimitErrorMessage ( raw ) ) {
return "rate_limit" ;
}
if ( isOverloadedErrorMessage ( raw ) ) {
return "rate_limit" ;
}
if ( isCloudCodeAssistFormatError ( raw ) ) {
return "format" ;
}
if ( isBillingErrorMessage ( raw ) ) {
return "billing" ;
}
if ( isTimeoutErrorMessage ( raw ) ) {
return "timeout" ;
}
if ( isAuthErrorMessage ( raw ) ) {
return "auth" ;
}
2026-01-14 01:08:15 +00:00
return null ;
}
export function isFailoverErrorMessage ( raw : string ) : boolean {
return classifyFailoverReason ( raw ) !== null ;
}
2026-01-14 14:31:43 +00:00
export function isFailoverAssistantError ( msg : AssistantMessage | undefined ) : boolean {
2026-01-31 16:19:20 +09:00
if ( ! msg || msg . stopReason !== "error" ) {
return false ;
}
2026-01-14 01:08:15 +00:00
return isFailoverErrorMessage ( msg . errorMessage ? ? "" ) ;
}