import Foundation /// Extracts a short, non-user-facing message from an Ollama error body. /// /// The body is `{"error":"…"}`; plain text is accepted as a fallback. The result is capped at /// 500 characters and is never shown raw to the user or logged in clear. enum OllamaErrorBody { static func message(from data: Data) -> String? { if let envelope = try? JSONDecoder().decode(ErrorEnvelope.self, from: data), !envelope.error.isEmpty { return truncate(envelope.error) } guard let text = String(data: data, encoding: .utf8)? .trimmingCharacters(in: .whitespacesAndNewlines), !text.isEmpty else { return nil } return truncate(text) } private static func truncate(_ value: String) -> String { value.count > 500 ? String(value.prefix(500)) : value } private struct ErrorEnvelope: Decodable { let error: String } }