SPB Git

Repository Public

Providers.swift 3.0 KB 86 lines swift

1import Foundation23/// The wire protocol family spoken by a configured assistant provider.4public enum AIProviderKind: String, Sendable, Codable, CaseIterable, Hashable {5    /// Anthropic Messages API (`POST {base}/v1/messages`).6    case anthropic7    /// OpenAI chat completions (`POST {base}/v1/chat/completions`).8    case openAI9    /// Any server speaking the OpenAI chat-completions dialect (Ollama, LM Studio, ...).10    /// The API key is optional for this kind.11    case openAICompatible1213    /// Human-readable name for settings screens.14    public var displayName: String {15        switch self {16        case .anthropic: "Anthropic"17        case .openAI: "OpenAI"18        case .openAICompatible: "OpenAI-compatible (local)"19        }20    }2122    /// Base URL used when the user has not typed one.23    public var defaultBaseURL: URL {24        switch self {25        case .anthropic: URL(string: "https://api.anthropic.com")!26        case .openAI: URL(string: "https://api.openai.com")!27        case .openAICompatible: URL(string: "http://localhost:11434")!28        }29    }3031    /// Model ids offered as suggestions only. The model is always a free,32    /// user-editable string and is never validated against this list.33    public var suggestedModels: [String] {34        switch self {35        case .anthropic: ["claude-fable-5-2", "claude-opus-5-5", "claude-sonnet-5-5", "claude-haiku-5-5"]36        case .openAI, .openAICompatible: []37        }38    }3940    /// Model pre-filled for a new configuration (empty when there is no sensible default).41    public var defaultModel: String {42        switch self {43        case .anthropic: "claude-sonnet-5-5"44        case .openAI, .openAICompatible: ""45        }46    }4748    /// Whether a request is refused locally when no key is stored for the provider.49    public var requiresAPIKey: Bool {50        switch self {51        case .anthropic, .openAI: true52        case .openAICompatible: false53        }54    }55}5657/// A user-configured provider. It is safe to persist: it never contains the API key,58/// which lives in a ``SecretStore`` under the account name ``id``.59public struct AIProviderConfig: Sendable, Codable, Hashable, Identifiable {60    /// Stable identifier; also the account name used in the ``SecretStore``.61    public var id: String62    /// Wire protocol family.63    public var kind: AIProviderKind64    /// Label shown to the user (and in ``ContextPreview/providerLabel``).65    public var label: String66    /// Base URL of the API, without the `/v1/...` path.67    public var baseURL: URL68    /// Free-form model identifier, sent as typed.69    public var model: String7071    /// Creates a configuration. `baseURL` and `model` default to the kind's defaults.72    public init(73        id: String = UUID().uuidString,74        kind: AIProviderKind,75        label: String? = nil,76        baseURL: URL? = nil,77        model: String? = nil78    ) {79        self.id = id80        self.kind = kind81        self.label = label ?? kind.displayName82        self.baseURL = baseURL ?? kind.defaultBaseURL83        self.model = model ?? kind.defaultModel84    }85}86
Files main
Menu

Theme