SPB Git

Repository Public MIT

OpenAICompatibleProvider.swift 8.4 KB 173 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Astra9// File             : Packages/AstraProviders/Sources/OpenAICompatibleProvider.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/// A user-configured endpoint that speaks the OpenAI Chat Completions dialect (`{base}/chat/completions`, `{base}/models`).92/// The profile is validated on every request: plain HTTP is refused unless the user allowed it for a host on the local network.93public struct OpenAICompatibleProvider: ChatProvider {94    public let kind: ProviderKind = .custom95    public let profile: EndpointProfile96    static let dialect = ChatCompletionsDialect(reasoning: .effortField, requestsUsage: true, includesRouting: false)9798    private let http: ProviderHTTP99    private let apiKey: KeyProvider100101    /// `apiKey` may return nil: many local servers need no key.102    public init(profile: EndpointProfile, transport: any HTTPTransport, apiKey: @escaping KeyProvider = { nil }, diagnostics: (any DiagnosticsSink)? = nil) {103        self.profile = profile104        self.http = ProviderHTTP(kind: .custom, transport: transport, diagnostics: diagnostics)105        self.apiKey = apiKey106    }107108    private struct Target { var base: URL; var headers: [String: String]; var key: String? }109110    private func target() throws -> Target {111        let base = try profile.validated()112        let key = try resolveKey(apiKey, kind: .custom, required: false)113        var headers = profile.extraHeaders114        if let key { headers["Authorization"] = "Bearer \(key)" }115        return Target(base: base, headers: headers, key: key)116    }117118    private func ref(_ modelID: String) -> ModelRef { ModelRef(provider: .custom, modelID: modelID, endpointID: profile.id) }119120    public func listModels() async throws -> [ModelInfo] {121        let t = try target()122        let json: JSONValue123        do {124            (json, _) = try await http.json(ProviderHTTP.request(t.base.appendingPathComponent("models"), headers: t.headers), secrets: [t.key].compactMap { $0 })125        } catch let error as AstraError {126            // A server without a model list is still usable with the model id the user typed.127            if case .invalidRequest = error, let id = profile.defaultModelID, !id.isEmpty {128                return [ModelInfo(ref: ref(id), displayName: id, capabilities: [.text], source: .user)]129            }130            throw error131        }132        let entries = json["data"]?.arrayValue ?? json["models"]?.arrayValue ?? json.arrayValue ?? []133        let fetchedAt = Date()134        var models = entries.compactMap { entry -> ModelInfo? in135            guard let id = entry["id"]?.stringValue ?? entry["name"]?.stringValue ?? entry.stringValue, !id.isEmpty else { return nil }136            // Only what this endpoint reports about itself; nothing is inferred from the model's name.137            var capabilities: Set<Capability> = [.text]138            let reported = entry["capabilities"]139            if reported?["tools"]?.boolValue == true { capabilities.insert(.tools) }140            if reported?["vision"]?.boolValue == true { capabilities.insert(.vision) }141            if reported?["thinking"]?.boolValue == true || reported?["reasoning"]?.boolValue == true { capabilities.insert(.reasoning) }142            let context = entry["context_window"]?.intValue ?? entry["context_length"]?.intValue ?? entry["max_model_len"]?.intValue143            return ModelInfo(ref: ref(id), displayName: entry["display_name"]?.stringValue ?? id, contextLength: context,144                             maxOutputTokens: entry["max_output_tokens"]?.intValue, capabilities: capabilities, source: .providerAPI(fetchedAt: fetchedAt),145                             vendor: entry["owned_by"]?.stringValue, createdAt: entry["created"]?.doubleValue.map { Date(timeIntervalSince1970: $0) })146        }147        if let id = profile.defaultModelID, !id.isEmpty, !models.contains(where: { $0.ref.modelID == id }) {148            models.insert(ModelInfo(ref: ref(id), displayName: id, capabilities: [.text], source: .user), at: 0)149        }150        return models151    }152153    public func verify() async throws -> ConnectionStatus {154        let models = try await listModels()155        return ConnectionStatus(ok: true, detail: "Endpoint reachable. \(models.count) models listed.", modelCount: models.count)156    }157158    public func stream(_ request: GenerationRequest) -> AsyncThrowingStream<StreamEvent, Error> {159        let http = http160        let secretBox = SecretBox()161        return makeEventStream(secrets: { secretBox.values }) { continuation in162            let t = try target()163            secretBox.add(t.key)164            var headers = t.headers165            headers["Accept"] = "text/event-stream"166            let url = t.base.appendingPathComponent("chat/completions")167            try await runChatCompletionsStream(http: http, request: request, dialect: Self.dialect, secrets: [t.key].compactMap { $0 }, makeRequest: { body in168                ProviderHTTP.request(url, method: "POST", headers: headers, body: body)169            }, continuation: continuation)170        }171    }172}173
Files main
Menu

Theme