Repository Public
1//2// SP Chat iOS3// File: SPChat-iOS/Core/API/ChatAPI+Studio.swift4// Description: Endpoints of deep research, the image studio and project details.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//910import Foundation1112extension ChatAPI {13 private func looseJSON(_ request: HTTPRequest) async throws -> JSONValue { try await http.decode(JSONValue.self, request) }1415 // MARK: deep research1617 func researchRuns() async throws -> [ResearchRun] { try await looseJSON(.get("/api/research")).list("runs").compactMap { ResearchRun($0) } }1819 func research(_ id: String) async throws -> ResearchRun {20 let j = try await looseJSON(.get("/api/research/\(id)"))21 guard let run = j["run"].flatMap({ ResearchRun($0, live: j.str("live")) }) else { throw APIError.decoding("research") }22 return run23 }24 func deleteResearch(_ id: String) async throws { _ = try await http.data(HTTPRequest(method: "DELETE", path: "/api/research/\(id)")) }2526 /// Starts a research run: real web searches and model calls, detached on the server (it continues if the app27 /// closes). The stream reports `run`, `start`, `step`, `source`, `evidence`, `text`, `done` / `error`, `end`.28 func openResearch(question: String, modelId: String, projectId: String?, depth: ResearchDepth) async throws -> HTTPClient.StreamOpening {29 var o: [String: JSONValue] = ["question": .string(question), "modelId": .string(modelId), "depth": .string(depth.rawValue)]30 if let projectId { o["projectId"] = .string(projectId) }31 var r = HTTPRequest.json("POST", "/api/research", object: o); r.timeout = 12032 return try await http.openStream(r)33 }34 /// Resumes an interrupted, failed or cancelled run: phases already paid for are reused by the server.35 func resumeResearch(_ id: String) async throws -> HTTPClient.StreamOpening {36 var r = HTTPRequest.json("POST", "/api/research/\(id)/resume"); r.timeout = 12037 return try await http.openStream(r)38 }39 /// The report as a file (`md`, `json` or `docx`), produced by the server.40 func researchExport(_ id: String, format: String) async throws -> Data {41 var r = HTTPRequest(path: "/api/research/\(id)", query: [.init(name: "format", value: format)], accept: "*/*"); r.timeout = 6042 return try await http.data(r)43 }4445 // MARK: image studio4647 func generatedImages() async throws -> [GeneratedImage] { try await looseJSON(.get("/api/images")).list("images").compactMap(GeneratedImage.init) }4849 /// Generates an image (or edits `editAttachmentId`) with the provider's image model: a real, billed call.50 func generateImage(prompt: String, model: String?, size: String?, quality: String?, editAttachmentId: String? = nil) async throws -> ImageGeneration {51 var r = HTTPRequest.json("POST", "/api/images", object: ImageOptions.payload(prompt: prompt, model: model, size: size, quality: quality, editAttachmentId: editAttachmentId)); r.timeout = 30052 let j = try await looseJSON(r)53 guard let g = ImageGeneration(j) else { throw APIError(kind: .http, status: 502, code: "image", message: j.str("error") ?? String(localized: "Aucune image n’a été produite.")) }54 return g55 }5657 // MARK: projects5859 func projectDetail(_ id: String) async throws -> ProjectDetail {60 guard let d = ProjectDetail(try await looseJSON(.get("/api/projects/\(id)"))) else { throw APIError.decoding("project") }61 return d62 }63 func updateProject(_ id: String, patch: ProjectPatch) async throws -> Project {64 struct R: Decodable { var project: Project }65 return try await http.decode(R.self, .json("PATCH", "/api/projects/\(id)", object: patch.payload)).project66 }67}68