// src/sessions/errors.ts
var RATE_LIMIT_PATTERNS = [
  "rate_limit",
  "rate limit",
  "429",
  "too many requests",
  "model_rate_limited"
];
var OVERLOADED_PATTERNS = [
  "overloaded",
  "capacity",
  "503",
  "service unavailable",
  "model_overloaded"
];
var BILLING_PATTERNS = [
  "credit balance",
  "billing",
  "insufficient",
  "quota",
  "payment",
  "billing_error",
  "purchase credits"
];
var REQUEST_FAILED_PATTERNS = [
  "invalid_request",
  "authentication_failed",
  "invalid_api_key",
  "unauthorized",
  "403",
  "401"
];
function classifyError(message) {
  const lower = message.toLowerCase();
  for (const p of RATE_LIMIT_PATTERNS) {
    if (lower.includes(p)) {
      return { type: "model_rate_limited_error", retryable: true, message };
    }
  }
  for (const p of OVERLOADED_PATTERNS) {
    if (lower.includes(p)) {
      return { type: "model_overloaded_error", retryable: true, message };
    }
  }
  for (const p of BILLING_PATTERNS) {
    if (lower.includes(p)) {
      return { type: "billing_error", retryable: false, message };
    }
  }
  for (const p of REQUEST_FAILED_PATTERNS) {
    if (lower.includes(p)) {
      return { type: "model_request_failed_error", retryable: false, message };
    }
  }
  return { type: "server_error", retryable: false, message };
}
function buildErrorPayload(classified, retryStatus) {
  return {
    error: {
      type: classified.type,
      message: classified.message,
      retry_status: retryStatus
    }
  };
}

export {
  classifyError,
  buildErrorPayload
};
