47 lines
1.3 KiB
Swift
47 lines
1.3 KiB
Swift
import Foundation
|
|
|
|
public enum AIRequestState: Equatable, Sendable {
|
|
case idle
|
|
case loading
|
|
case failed(AIProviderErrorState)
|
|
}
|
|
|
|
public enum AIProviderErrorState: Equatable, Sendable {
|
|
case offline
|
|
case missingConfiguration
|
|
case rateLimited(retryAfter: TimeInterval?)
|
|
case unavailable
|
|
case invalidResponse
|
|
case unsupportedCapability
|
|
}
|
|
|
|
public struct AIProviderErrorPresenter: Sendable {
|
|
public init() {}
|
|
|
|
public func state(for error: Error) -> AIProviderErrorState {
|
|
if let urlError = error as? URLError,
|
|
urlError.code == .notConnectedToInternet || urlError.code == .networkConnectionLost {
|
|
return .offline
|
|
}
|
|
|
|
if let clientError = error as? OpenAIClientError {
|
|
switch clientError {
|
|
case .missingAPIKey:
|
|
return .missingConfiguration
|
|
case let .rateLimited(retryAfter):
|
|
return .rateLimited(retryAfter: retryAfter)
|
|
case .serverError, .unacceptableStatusCode:
|
|
return .unavailable
|
|
case .invalidResponse:
|
|
return .invalidResponse
|
|
}
|
|
}
|
|
|
|
if error as? AIServiceCapabilityError != nil {
|
|
return .unsupportedCapability
|
|
}
|
|
|
|
return .unavailable
|
|
}
|
|
}
|