{"version":3,"file":"error-utils.cjs","names":["CopilotKitLowLevelError","CopilotKitErrorCode"],"sources":["../../../src/service-adapters/shared/error-utils.ts"],"sourcesContent":["import {\n  CopilotKitLowLevelError,\n  CopilotKitErrorCode,\n} from \"@copilotkit/shared\";\n\n/**\n * Converts service adapter errors to structured CopilotKitError format using HTTP status codes.\n * This provides consistent error classification across all service adapters.\n */\nexport function convertServiceAdapterError(\n  error: any,\n  adapterName: string,\n): CopilotKitLowLevelError {\n  const errorName = error?.constructor?.name || error.name;\n  const errorMessage = error?.message || String(error);\n  const statusCode = error.status || error.statusCode || error.response?.status;\n  const responseData = error.error || error.response?.data || error.data;\n\n  // Create the base error with the constructor signature\n  const structuredError = new CopilotKitLowLevelError({\n    error: error instanceof Error ? error : new Error(errorMessage),\n    url: `${adapterName} service adapter`,\n    message: `${adapterName} API error: ${errorMessage}`,\n  });\n\n  // Add additional properties after construction\n  if (statusCode) {\n    (structuredError as any).statusCode = statusCode;\n  }\n  if (responseData) {\n    (structuredError as any).responseData = responseData;\n  }\n  if (errorName) {\n    (structuredError as any).originalErrorType = errorName;\n  }\n\n  // Classify error based on HTTP status codes (reliable and provider-agnostic)\n  let newCode: CopilotKitErrorCode;\n\n  if (statusCode === 401) {\n    // 401 = Authentication/API key issues\n    newCode = CopilotKitErrorCode.AUTHENTICATION_ERROR;\n  } else if (statusCode >= 400 && statusCode < 500) {\n    // 4xx = Client errors (bad request, invalid params, etc.) - these are configuration issues\n    newCode = CopilotKitErrorCode.CONFIGURATION_ERROR;\n  } else if (statusCode >= 500) {\n    // 5xx = Server errors - keep as NETWORK_ERROR since it's infrastructure related\n    newCode = CopilotKitErrorCode.NETWORK_ERROR;\n  } else if (statusCode) {\n    // Any other HTTP status with an error - likely configuration\n    newCode = CopilotKitErrorCode.CONFIGURATION_ERROR;\n  } else {\n    // No status code - likely a genuine network/connection error\n    newCode = CopilotKitErrorCode.NETWORK_ERROR;\n  }\n\n  // Update both the instance property and the extensions\n  (structuredError as any).code = newCode;\n  if ((structuredError as any).extensions) {\n    (structuredError as any).extensions.code = newCode;\n  }\n\n  return structuredError;\n}\n"],"mappings":";;;;;;;;;AASA,SAAgB,2BACd,OACA,aACyB;CACzB,MAAM,YAAY,OAAO,aAAa,QAAQ,MAAM;CACpD,MAAM,eAAe,OAAO,WAAW,OAAO,MAAM;CACpD,MAAM,aAAa,MAAM,UAAU,MAAM,cAAc,MAAM,UAAU;CACvE,MAAM,eAAe,MAAM,SAAS,MAAM,UAAU,QAAQ,MAAM;CAGlE,MAAM,kBAAkB,IAAIA,2CAAwB;EAClD,OAAO,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,aAAa;EAC/D,KAAK,GAAG,YAAY;EACpB,SAAS,GAAG,YAAY,cAAc;EACvC,CAAC;AAGF,KAAI,WACF,CAAC,gBAAwB,aAAa;AAExC,KAAI,aACF,CAAC,gBAAwB,eAAe;AAE1C,KAAI,UACF,CAAC,gBAAwB,oBAAoB;CAI/C,IAAI;AAEJ,KAAI,eAAe,IAEjB,WAAUC,uCAAoB;UACrB,cAAc,OAAO,aAAa,IAE3C,WAAUA,uCAAoB;UACrB,cAAc,IAEvB,WAAUA,uCAAoB;UACrB,WAET,WAAUA,uCAAoB;KAG9B,WAAUA,uCAAoB;AAIhC,CAAC,gBAAwB,OAAO;AAChC,KAAK,gBAAwB,WAC3B,CAAC,gBAAwB,WAAW,OAAO;AAG7C,QAAO"}