Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

5.8 KB
Raw Blame
import { toRecord } from './object';

import { getErrorCodeSpec } from '../errors/specs';
import { AgentRuntimeErrorType } from '../types/error';
import { isErrorCausedByContentFilter } from './isErrorCausedByContentFilter';

const FALLBACK_STATUS_CODES = new Set([401, 403, 404, 408, 409, 423, 425, 429]);
const FALLBACK_ERROR_CODES = new Set([
  'accountdeactivated',
  'deploymentnotfound',
  'invalid_api_key',
  'invalidapikey',
  'invalidproviderapikey',
  'insufficient_quota',
  AgentRuntimeErrorType.InsufficientQuota.toLowerCase(),
  'model_not_found',
  'quota_exceeded',
  'rate_limit_exceeded',
]);
const TERMINAL_ERROR_CODES = new Set([
  'context_length_exceeded',
  'invalid_request_error',
  'invalid_schema',
  'invalid_type',
  'invalid_value',
  'json_schema_validation_error',
  'string_above_max_length',
]);

const FALLBACK_MESSAGE_PATTERNS = [
  'api key',
  'billing',
  'capacity',
  'deploymentnotfound',
  'forbidden',
  'insufficient quota',
  'invalid api key',
  'invalidapikey',
  'invalidproviderapikey',
  'model not found',
  'overloaded',
  'permission denied',
  'quota',
  'rate limit',
  'temporarily unavailable',
  'timeout',
  'timed out',
  'too many requests',
  'unauthorized',
];

const IMAGE_DECODING_MESSAGE_PATTERNS = [
  'failed to decode image data',
  'unable to process input image',
];

const TERMINAL_MESSAGE_PATTERNS = [
  'assistant message prefill',
  'conversation must end with a user message',
  'context length exceeded',
  'context_length_exceeded',
  'does not support parameter',
  'expected a string',
  ...IMAGE_DECODING_MESSAGE_PATTERNS,
  'input is too long',
  'input tokens exceed',
  'invalid input',
  'invalid request',
  'invalid schema',
  'invalid schema for response_format',
  'invalid type for',
  'maximum allowed number of input tokens',
  'maximum context length',
  'maximum input length',
  'messages with role',
  'missing required parameter',
  'prompt is too long',
  'request body too large',
  'request too large for model',
  'response_format',
  'schema validation error',
  'string_above_max_length',
  'tool_choice',
  'tool_calls',
  'too many input tokens',
  'unsupported parameter',
  'unrecognized request argument',
];

const collectErrorStrings = (
  value: unknown,
  visited = new WeakSet<object>(),
  depth = 0,
): string[] => {
  if (depth > 4 || value === undefined || value === null) return [];
  if (typeof value === 'string') return [value];
  if (typeof value === 'number' || typeof value === 'boolean') return [String(value)];

  if (value instanceof Error) {
    return [
      value.name,
      value.message,
      ...collectErrorStrings(value.cause, visited, depth + 1),
    ].filter(Boolean);
  }

  if (Array.isArray(value)) {
    if (visited.has(value)) return [];
    visited.add(value);

    return value.flatMap((item) => collectErrorStrings(item, visited, depth + 1));
  }

  const objectValue = toRecord(value);
  if (!objectValue) return [];
  if (visited.has(objectValue)) return [];
  visited.add(objectValue);

  const result: string[] = [];
  for (const [key, nestedValue] of Object.entries(objectValue)) {
    if (key === 'stack' || key === 'headers') continue;
    result.push(...collectErrorStrings(nestedValue, visited, depth + 1));
  }

  return result;
};

export const collectStatusCodes = (
  value: unknown,
  visited = new WeakSet<object>(),
  depth = 0,
): number[] => {
  if (depth > 4 || value === undefined || value === null) return [];
  if (Array.isArray(value)) {
    if (visited.has(value)) return [];
    visited.add(value);

    return value.flatMap((item) => collectStatusCodes(item, visited, depth + 1));
  }

  const objectValue = toRecord(value);
  if (!objectValue) return [];
  if (visited.has(objectValue)) return [];
  visited.add(objectValue);

  const result: number[] = [];
  for (const [key, nestedValue] of Object.entries(objectValue)) {
    const normalizedKey = key.toLowerCase();
    if (
      (normalizedKey === 'status' ||
        normalizedKey === 'statuscode' ||
        normalizedKey === 'httpstatuscode') &&
      typeof nestedValue === 'number'
    ) {
      result.push(nestedValue);
      continue;
    }

    result.push(...collectStatusCodes(nestedValue, visited, depth + 1));
  }

  return result;
};

export const isImageDecodingRequestError = (error: unknown): boolean => {
  const combined = collectErrorStrings(error)
    .map((value) => value.toLowerCase())
    .join('\n');

  return IMAGE_DECODING_MESSAGE_PATTERNS.some((pattern) => combined.includes(pattern));
};

export const shouldStopFallbackForError = (error: unknown): boolean => {
  const errorStrings = collectErrorStrings(error);
  const normalizedStrings = errorStrings.map((value) => value.toLowerCase());

  if (error && typeof error === 'object') {
    const errorType = (error as { errorType?: unknown }).errorType;
    if (typeof errorType === 'string') {
      const routeFallback = getErrorCodeSpec(errorType)?.routeFallback;
      if (routeFallback !== undefined) return !routeFallback;
    }
  }

  if (isErrorCausedByContentFilter(error)) return true;

  // Explicitly fallback-eligible HTTP statuses represent route or channel conditions.
  // They take precedence over provider body text, which can reuse terminal
  // request phrases such as "unable to process input image" for a 429 response.
  const statusCodes = collectStatusCodes(error);
  if (statusCodes.some((statusCode) => FALLBACK_STATUS_CODES.has(statusCode))) return false;

  if (normalizedStrings.some((value) => FALLBACK_ERROR_CODES.has(value))) return false;

  if (normalizedStrings.some((value) => TERMINAL_ERROR_CODES.has(value))) return true;

  const combined = normalizedStrings.join('\n');
  if (FALLBACK_MESSAGE_PATTERNS.some((pattern) => combined.includes(pattern))) return false;
  if (TERMINAL_MESSAGE_PATTERNS.some((pattern) => combined.includes(pattern))) return true;

  return false;
};