/** * Millisa AI Client for iOS (Swift) * ───────────────────────────────────────────────────────────────────────────── * Compatible with iOS 15.0+, macOS 12.0+, Swift 5.5+ (Modern Async/Await & Concurrency) */ import Foundation public final class MillisaClient { private let apiKey: String private let baseURL: URL public init(apiKey: String, baseURL: String = "https://millisa.codly.in/api/v1") { self.apiKey = apiKey self.baseURL = URL(string: baseURL)! } /** * Send a single prompt query to Millisa AI Agent */ public func queryAgent(prompt: String, model: String = "millisa-auto") async throws -> String { let endpoint = baseURL.appendingPathComponent("agent/query") var request = URLRequest(url: endpoint) request.httpMethod = "POST" request.addValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization") request.addValue("application/json", forHTTPHeaderField: "Content-Type") let body: [String: Any] = [ "prompt": prompt, "model": model ] request.httpBody = try JSONSerialization.data(withJSONObject: body) let (data, response) = try await URLSession.shared.data(for: request) guard let httpResponse = response as? HTTPURLResponse else { throw URLError(.badServerResponse) } guard httpResponse.statusCode == 200 else { let errorJson = (try? JSONSerialization.jsonObject(with: data) as? [String: Any])?["error"] as? [String: Any] let message = errorJson?["message"] as? String ?? "HTTP Error \(httpResponse.statusCode)" throw NSError(domain: "MillisaError", code: httpResponse.statusCode, userInfo: [NSLocalizedDescriptionKey: message]) } guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any], let reply = json["response"] as? String else { throw URLError(.cannotParseResponse) } return reply } /** * Multi-turn chat completion (OpenAI-compatible) */ public func chatCompletion( messages: [[String: String]], // [["role": "user", "content": "..."]] model: String = "millisa-auto" ) async throws -> String { let endpoint = baseURL.appendingPathComponent("chat/completions") var request = URLRequest(url: endpoint) request.httpMethod = "POST" request.addValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization") request.addValue("application/json", forHTTPHeaderField: "Content-Type") let body: [String: Any] = [ "model": model, "messages": messages ] request.httpBody = try JSONSerialization.data(withJSONObject: body) let (data, response) = try await URLSession.shared.data(for: request) guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else { throw URLError(.badServerResponse) } guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any], let choices = json["choices"] as? [[String: Any]], let message = choices.first?["message"] as? [String: Any], let content = message["content"] as? String else { throw URLError(.cannotParseResponse) } return content } } // ───────────────────────────────────────────────────────────────────────────── // Usage Example in SwiftUI / iOS: // ───────────────────────────────────────────────────────────────────────────── /* let millisa = MillisaClient(apiKey: "mls_live_YOUR_MILLISA_API_KEY") Task { do { let reply = try await millisa.queryAgent(prompt: "Hello Millisa, recommend best architecture for iOS apps.") print("Millisa replied:\n\(reply)") } catch { print("Error: \(error.localizedDescription)") } } */