SPB Git

Repository Public MIT

ProviderRegistry.swift 6.4 KB 127 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Astra9// File             : Packages/AstraProviders/Sources/ProviderRegistry.swift10// Module           : Packages11// Repository       : https://git.spboucher.ai/orix-astra12// Created          : 2026-11-2113// Last Modified    : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import AstraCore89import Foundation9091/// Builds the adapter for a model. Keys are read from the credential store at request time, never captured.92public enum ProviderRegistry {93    /// Cloud providers this module implements.94    public static let supportedKinds: [ProviderKind] = [.openAI, .anthropic, .google, .xAI, .openRouter, .custom]9596    public static func makeProvider(for ref: ModelRef, credentials: any CredentialStore, endpoints: [EndpointProfile], transport: any HTTPTransport,97                                    diagnostics: (any DiagnosticsSink)? = nil) throws -> any ChatProvider {98        try makeProvider(kind: ref.provider, endpointID: ref.endpointID, credentials: credentials, endpoints: endpoints, transport: transport, diagnostics: diagnostics)99    }100101    /// The adapter for a provider as a whole (model lists, key verification); `endpointID` selects the profile for `.custom`.102    public static func makeProvider(kind: ProviderKind, endpointID: String? = nil, credentials: any CredentialStore, endpoints: [EndpointProfile] = [],103                                    transport: any HTTPTransport, diagnostics: (any DiagnosticsSink)? = nil) throws -> any ChatProvider {104        let key: KeyProvider = { try credentials.secret(for: CredentialAccount.provider(kind)) }105        switch kind {106        case .openAI: return OpenAIProvider(transport: transport, apiKey: key, diagnostics: diagnostics)107        case .anthropic: return AnthropicProvider(transport: transport, apiKey: key, diagnostics: diagnostics)108        case .google: return GeminiProvider(transport: transport, apiKey: key, diagnostics: diagnostics)109        case .xAI: return XAIProvider(transport: transport, apiKey: key, diagnostics: diagnostics)110        case .openRouter: return OpenRouterProvider(transport: transport, apiKey: key, diagnostics: diagnostics)111        case .custom:112            guard let endpointID, let profile = endpoints.first(where: { $0.id == endpointID }) else {113                throw AstraError.invalidRequest("This custom endpoint is no longer configured. Add it again in Models.")114            }115            return OpenAICompatibleProvider(profile: profile, transport: transport, apiKey: { try credentials.secret(for: CredentialAccount.endpoint(profile.id)) }, diagnostics: diagnostics)116        case .mlx, .appleFoundation:117            throw AstraError.unsupported("\(kind.displayName) models run on this device and are not served by a cloud adapter.")118        }119    }120}121122/// Convenience spelling of `ProviderRegistry.makeProvider(for:…)`.123public func makeProvider(for ref: ModelRef, credentials: any CredentialStore, endpoints: [EndpointProfile], transport: any HTTPTransport,124                         diagnostics: (any DiagnosticsSink)?) throws -> any ChatProvider {125    try ProviderRegistry.makeProvider(for: ref, credentials: credentials, endpoints: endpoints, transport: transport, diagnostics: diagnostics)126}127
Files main
Menu

Theme