Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Astra9// File : Packages/AstraProviders/Sources/XAIProvider.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/// xAI through its Responses API (`POST https://api.x.ai/v1/responses`). xAI documents Chat Completions as a legacy endpoint.92public struct XAIProvider: ChatProvider {93 public let kind: ProviderKind = .xAI94 public static let defaultBaseURL = URL(string: "https://api.x.ai/v1")!95 /// `usage.cost_in_usd_ticks` is in units of 1e-10 US dollars.96 static let costTicksPerUSD = 10_000_000_000.097 /// Price fields of the models API are in units of 1e-4 US dollars per million tokens (20000 = $2.00 / 1M).98 static let priceUnitsPerUSDPerMTok = 10_000.099100 private let http: ProviderHTTP101 private let apiKey: KeyProvider102 private let baseURL: URL103104 public init(transport: any HTTPTransport, apiKey: @escaping KeyProvider, diagnostics: (any DiagnosticsSink)? = nil, baseURL: URL = XAIProvider.defaultBaseURL) {105 self.http = ProviderHTTP(kind: .xAI, transport: transport, diagnostics: diagnostics)106 self.apiKey = apiKey107 self.baseURL = baseURL108 }109110 private func headers(_ key: String) -> [String: String] { ["Authorization": "Bearer \(key)", "Accept": "application/json"] }111112 /// xAI answers a bad key with HTTP 400 and names the team id in "model not found" messages.113 static func normalize(_ failure: ProviderHTTP.Failure) -> ProviderHTTP.Failure {114 var f = failure115 let message = (f.body.message ?? "").lowercased()116 if f.status == 400, message.contains("incorrect api key") || message.contains("invalid api key") || message.contains("no api key") { f.status = 401 }117 if let text = f.body.message {118 f.body.message = text.replacingOccurrences(of: #"\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b"#, with: "•••", options: .regularExpression)119 }120 return f121 }122123 public func listModels() async throws -> [ModelInfo] {124 let key = try resolveKey(apiKey, kind: kind, required: true) ?? ""125 // `/language-models` carries modalities, prices and reasoning efforts; `/models` carries the context length.126 let (detailed, _) = try await http.json(ProviderHTTP.request(baseURL.appendingPathComponent("language-models"), headers: headers(key)), secrets: [key], normalize: Self.normalize)127 var contexts: [String: Int] = [:]128 if let (plain, _) = try? await http.json(ProviderHTTP.request(baseURL.appendingPathComponent("models"), headers: headers(key)), secrets: [key], normalize: Self.normalize) {129 for entry in plain["data"]?.arrayValue ?? [] { if let id = entry["id"]?.stringValue, let n = entry["context_length"]?.intValue { contexts[id] = n } }130 }131 guard let models = detailed["models"]?.arrayValue ?? detailed["data"]?.arrayValue else { throw AstraError.decoding("the model list has no models array") }132 let fetchedAt = Date()133 return models.compactMap { entry -> ModelInfo? in134 guard let id = entry["id"]?.stringValue else { return nil }135 let outputs = entry["output_modalities"]?.arrayValue?.compactMap(\.stringValue) ?? ["text"]136 guard outputs.contains("text") else { return nil }137 let inputs = entry["input_modalities"]?.arrayValue?.compactMap(\.stringValue) ?? []138 var capabilities: Set<Capability> = [.text]139 if inputs.contains("image") { capabilities.insert(.vision) }140 if inputs.contains("audio") { capabilities.insert(.audioInput) }141 if let efforts = entry["capabilities"]?["reasoning_effort"]?.arrayValue, !efforts.isEmpty { capabilities.insert(.reasoning) }142 var pricing: Pricing?143 if let input = entry["prompt_text_token_price"]?.doubleValue, let output = entry["completion_text_token_price"]?.doubleValue {144 let cached = entry["cached_prompt_text_token_price"]?.doubleValue.map { $0 / Self.priceUnitsPerUSDPerMTok }145 if cached != nil { capabilities.insert(.promptCaching) }146 pricing = Pricing(inputPerMTok: input / Self.priceUnitsPerUSDPerMTok, outputPerMTok: output / Self.priceUnitsPerUSDPerMTok, cachedInputPerMTok: cached,147 asOf: fetchedAt.isoDay, sourceURL: "https://api.x.ai/v1/language-models")148 }149 return ModelInfo(ref: ModelRef(provider: .xAI, modelID: id), displayName: id, contextLength: contexts[id] ?? entry["context_length"]?.intValue,150 capabilities: capabilities, source: .providerAPI(fetchedAt: fetchedAt), pricing: pricing, vendor: entry["owned_by"]?.stringValue ?? "xai",151 createdAt: entry["created"]?.doubleValue.map { Date(timeIntervalSince1970: $0) })152 }153 .sorted { ($0.createdAt ?? .distantPast) > ($1.createdAt ?? .distantPast) }154 }155156 public func verify() async throws -> ConnectionStatus {157 let models = try await listModels()158 return ConnectionStatus(ok: true, detail: "Key accepted. \(models.count) language models available.", modelCount: models.count)159 }160161 /// The model list says which models reason, but a generation does not fetch it: unknown fields are sent and dropped on a 400.162 static func plan(for modelID: String) -> ResponsesModelPlan {163 let nonReasoning = modelID.contains("non-reasoning")164 return ResponsesModelPlan(acceptsSampling: nil, reasoningEfforts: nonReasoning ? [] : nil, requestSummaryByDefault: !nonReasoning)165 }166167 public func stream(_ request: GenerationRequest) -> AsyncThrowingStream<StreamEvent, Error> {168 let http = http, apiKey = apiKey, url = baseURL.appendingPathComponent("responses")169 let secretBox = SecretBox()170 return makeEventStream(secrets: { secretBox.values }) { continuation in171 let key = try resolveKey(apiKey, kind: .xAI, required: true) ?? ""172 secretBox.add(key)173 try await runResponsesStream(http: http, request: request, plan: Self.plan(for: request.model.modelID), secrets: [key],174 costTicksPerUSD: Self.costTicksPerUSD, normalize: Self.normalize, makeRequest: { body in175 ProviderHTTP.request(url, method: "POST", headers: ["Authorization": "Bearer \(key)", "Accept": "text/event-stream"], body: body)176 }, continuation: continuation)177 }178 }179}180