Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatNetworking/Sources/OpenAIConversion.swift3// Description: Conversions between OpenAI wire types and Core generation types, in both directions (server decodes, remote client encodes).4import Foundation5import LocalChatCore67public enum WireError: LocalizedError, Sendable, Equatable {8 case invalid(String)9 case unsupported(String)10 public var errorDescription: String? { switch self { case .invalid(let m), .unsupported(let m): m } }11}1213public enum DataURL {14 /// Decodes `data:<mime>;base64,<payload>`.15 public static func decode(_ url: String) -> ImageInput? {16 guard url.lowercased().hasPrefix("data:"), let comma = url.firstIndex(of: ",") else { return nil }17 let meta = url[url.index(url.startIndex, offsetBy: 5)..<comma].lowercased()18 guard meta.hasSuffix(";base64"), let data = Data(base64Encoded: String(url[url.index(after: comma)...]), options: .ignoreUnknownCharacters), !data.isEmpty else { return nil }19 let mime = meta.split(separator: ";").first.map(String.init) ?? "image/png"20 return ImageInput(data: data, mimeType: mime.isEmpty ? "image/png" : mime)21 }22 public static func encode(_ image: ImageInput) -> String { "data:\(image.mimeType);base64,\(image.data.base64EncodedString())" }23}2425public extension OpenAI.Message {26 /// Wire → Core. Remote image URLs are refused: the server never fetches a URL on behalf of a client.27 func promptMessage() throws -> PromptMessage {28 let role: PromptMessage.Role29 switch self.role.lowercased() {30 case "system", "developer": role = .system31 case "user": role = .user32 case "assistant": role = .assistant33 case "tool", "function": role = .tool34 default: throw WireError.invalid("Unknown message role “\(self.role)”.")35 }36 var images: [ImageInput] = []37 if case .parts(let parts)? = content {38 for part in parts where part.type == "image_url" || part.type == "input_image" {39 guard let url = part.imageURL?.url else { continue }40 guard let image = DataURL.decode(url) else { throw WireError.unsupported("Only base64 data: URLs are accepted for images; remote image URLs are not fetched.") }41 images.append(image)42 }43 }44 let calls: [ToolInvocation] = (toolCalls ?? []).compactMap { call in45 guard let name = call.function?.name, !name.isEmpty else { return nil }46 return ToolInvocation(id: call.id ?? UUID().uuidString, name: name, arguments: JSONValue.parse(call.function?.arguments ?? "{}") ?? .object([:]))47 }48 return PromptMessage(role: role, content: content?.plainText ?? "", images: images, toolCalls: calls, toolCallID: toolCallID, toolName: role == .tool ? name : nil)49 }5051 /// Core → wire.52 init(_ m: PromptMessage) {53 let content: OpenAI.Content = m.images.isEmpty ? .text(m.content)54 : .parts((m.content.isEmpty ? [] : [OpenAI.ContentPart(text: m.content)]) + m.images.map { OpenAI.ContentPart(imageURL: DataURL.encode($0)) })55 let calls = m.toolCalls.map { OpenAI.ToolCall(id: $0.id, function: .init(name: $0.name, arguments: $0.arguments.serialized())) }56 self.init(role: m.role.rawValue, content: m.role == .assistant && m.content.isEmpty && !calls.isEmpty ? nil : content,57 toolCalls: calls.isEmpty ? nil : calls, toolCallID: m.toolCallID, name: m.role == .tool ? m.toolName : nil)58 }59}6061public extension OpenAI.Tool {62 init(_ tool: ToolDefinition) { self.init(function: .init(name: tool.name, description: tool.summary, parameters: tool.parameters)) }63 var definition: ToolDefinition {64 ToolDefinition(name: function.name, summary: function.description ?? "", parameters: function.parameters ?? ["type": "object", "properties": [:]])65 }66}6768public extension OpenAI.ChatRequest {69 /// Core → wire (used by the remote client).70 init(model: String, request: GenerationRequest, stream: Bool) {71 self.init(model: model, messages: request.messages.map(OpenAI.Message.init))72 let s = request.settings73 temperature = s.temperature; topP = s.topP74 if s.topK > 0 { topK = s.topK }75 if s.minP > 0 { minP = s.minP }76 if s.repetitionPenalty != 1 { repetitionPenalty = s.repetitionPenalty }77 maxTokens = s.maxTokens; seed = s.seed78 if !request.stop.isEmpty { stop = OpenAI.StopList(request.stop) }79 if !request.tools.isEmpty { tools = request.tools.map(OpenAI.Tool.init) }80 if let schema = request.responseSchema { responseFormat = .init(type: "json_schema", jsonSchema: .init(schema: schema)) }81 enableThinking = s.reasoningEnabled82 self.stream = stream83 if stream { streamOptions = .init(includeUsage: true) }84 }8586 /// Wire → Core (used by the server). Fields the client omitted keep the values of `base`.87 func generationRequest(base: GenerationSettings = .default) throws -> GenerationRequest {88 guard !messages.isEmpty else { throw WireError.invalid("“messages” must not be empty.") }89 var s = base90 if let temperature { s.temperature = max(0, min(2, temperature)) }91 if let topP { s.topP = max(0, min(1, topP)) }92 if let topK { s.topK = max(0, topK) }93 if let minP { s.minP = max(0, min(1, minP)) }94 if let repetitionPenalty, repetitionPenalty > 0 { s.repetitionPenalty = repetitionPenalty }95 if let limit = maxCompletionTokens ?? maxTokens, limit > 0 { s.maxTokens = limit }96 if let seed { s.seed = seed }97 if let enableThinking { s.reasoningEnabled = enableThinking }98 else if let effort = reasoningEffort?.lowercased() { s.reasoningEnabled = !(effort == "none" || effort == "minimal") }99 var converted = try messages.map { try $0.promptMessage() }100 // chat templates accept one leading system turn: merge several into the first101 let systems = converted.filter { $0.role == .system }102 if systems.count > 1 || (systems.count == 1 && converted.first?.role != .system) {103 converted.removeAll { $0.role == .system }104 converted.insert(.system(systems.map(\.content).joined(separator: "\n\n")), at: 0)105 }106 guard converted.contains(where: { $0.role != .system }) else { throw WireError.invalid("“messages” must contain at least one non-system message.") }107 var toolList = (tools ?? []).map(\.definition)108 if toolChoice?.stringValue == "none" { toolList = [] }109 var schema: JSONValue?110 if let format = responseFormat {111 switch format.type {112 case "json_schema": schema = format.jsonSchema?.schema113 case "json_object": schema = ["type": "object"]114 default: break115 }116 }117 return GenerationRequest(messages: converted, settings: s, tools: toolList, responseSchema: schema, stop: stop?.values.filter { !$0.isEmpty } ?? [])118 }119}120121public extension OpenAI.Usage {122 init(_ m: GenerationMetrics) {123 self.init(promptTokens: m.promptTokens, completionTokens: m.completionTokens, cachedTokens: m.cachedPromptTokens, reasoningTokens: m.reasoningTokens)124 }125}126127public extension StopReason {128 var openAIFinishReason: String {129 switch self {130 case .endOfTurn, .stopSequence, .cancelled, .error: "stop"131 case .maxTokens: "length"132 case .toolCall: "tool_calls"133 }134 }135 init(openAIFinishReason reason: String?) {136 switch reason {137 case "length": self = .maxTokens138 case "tool_calls", "function_call": self = .toolCall139 default: self = .endOfTurn140 }141 }142}143144public extension GenerationMetrics {145 /// Builds metrics from what a server reported. Nothing is derived that the server (or the caller's own clock) did not measure.146 init(usage: OpenAI.Usage?, server: OpenAI.ServerMetrics?, finishReason: String?) {147 self.init(promptTokens: usage?.promptTokens, cachedPromptTokens: usage?.promptTokensDetails?.cachedTokens, completionTokens: usage?.completionTokens,148 reasoningTokens: usage?.completionTokensDetails?.reasoningTokens, timeToFirstToken: server?.timeToFirstToken, promptTime: server?.promptTime,149 generationTime: server?.generationTime, totalTime: server?.totalTime, peakMemoryBytes: server?.peakMemoryBytes,150 activeMemoryBytes: server?.activeMemoryBytes, stopReason: StopReason(openAIFinishReason: finishReason))151 }152}153154/// Reassembles tool calls streamed as fragments (`index` identifies the call; `arguments` arrives in pieces).155public struct ToolCallAccumulator: Sendable {156 private var order: [Int] = []157 private var ids: [Int: String] = [:]158 private var names: [Int: String] = [:]159 private var arguments: [Int: String] = [:]160 public init() {}161 public var isEmpty: Bool { order.isEmpty }162163 public mutating func add(_ fragments: [OpenAI.ToolCall]) {164 for (position, f) in fragments.enumerated() {165 let index = f.index ?? (order.last.map { f.id == nil && f.function?.name == nil ? $0 : $0 + 1 } ?? position)166 if !order.contains(index) { order.append(index) }167 if let id = f.id, !id.isEmpty { ids[index] = id }168 if let name = f.function?.name, !name.isEmpty, names[index] == nil { names[index] = name }169 if let piece = f.function?.arguments { arguments[index, default: ""] += piece }170 }171 }172173 public func finish() -> [ToolInvocation] {174 order.compactMap { index in175 guard let name = names[index] else { return nil }176 let raw = arguments[index]?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""177 return ToolInvocation(id: ids[index] ?? UUID().uuidString, name: name, arguments: JSONValue.parse(raw.isEmpty ? "{}" : raw) ?? .object(["_raw": .string(raw)]))178 }179 }180}181