Repository Public
1//2// SP Chat iOS3// File: SPChat-iOS/Core/Models/Conversation.swift4// Description: Conversations, their loaded detail (messages, branches, files, artefacts) and projects.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//910import Foundation1112struct Conversation: Identifiable, Hashable, Sendable {13 var id: String14 var projectId: String?15 var title: String16 var modelId: String?17 var routingMode: String?18 var toolsEnabled: Bool?19 var pinned: Bool20 var favorite: Bool21 var archivedAt: Date?22 var parentId: String?23 /// Name given to this branch when the conversation was forked from another one.24 var branchLabel: String?25 var mode: String26 var messageCount: Int27 var totalCostUsd: Double28 var lastMessageAt: Date?29 var createdAt: Date30 var updatedAt: Date31 var projectName: String?32 var projectColor: String?33 var hasDraft: Bool34 var streaming: Bool3536 var isArchived: Bool { archivedAt != nil }37 var sortDate: Date { lastMessageAt ?? createdAt }38 static let untitled = "Nouvelle conversation"39}4041extension Conversation: Codable {42 private enum K: String, CodingKey {43 case id, project_id, title, model_id, settings, pinned, favorite, archived_at, parent_id, branch_label, mode, message_count, total_cost_usd, last_message_at, created_at, updated_at, project_name, project_color, has_draft, streaming44 }45 init(from decoder: Decoder) throws {46 let c = try decoder.container(keyedBy: K.self)47 id = try c.decode(String.self, forKey: .id)48 projectId = c.flexString(.project_id)49 title = c.flexString(.title) ?? Conversation.untitled50 modelId = c.flexString(.model_id)51 let settings = try? c.decodeIfPresent(JSONValue.self, forKey: .settings)52 routingMode = settings?["routingMode"]?.stringValue53 toolsEnabled = settings?["toolsEnabled"]?.boolValue54 pinned = c.flexBool(.pinned) ?? false55 favorite = c.flexBool(.favorite) ?? false56 archivedAt = c.flexDate(.archived_at)57 parentId = c.flexString(.parent_id)58 branchLabel = c.flexString(.branch_label)59 mode = c.flexString(.mode) ?? "chat"60 messageCount = c.flexInt(.message_count) ?? 061 totalCostUsd = c.flexDouble(.total_cost_usd) ?? 062 lastMessageAt = c.flexDate(.last_message_at)63 createdAt = c.flexDate(.created_at) ?? Date()64 updatedAt = c.flexDate(.updated_at) ?? createdAt65 projectName = c.flexString(.project_name)66 projectColor = c.flexString(.project_color)67 hasDraft = c.flexBool(.has_draft) ?? false68 streaming = c.flexBool(.streaming) ?? false69 }70 func encode(to encoder: Encoder) throws {71 var c = encoder.container(keyedBy: K.self)72 try c.encode(id, forKey: .id); try c.encodeIfPresent(projectId, forKey: .project_id); try c.encode(title, forKey: .title); try c.encodeIfPresent(modelId, forKey: .model_id)73 var settings: [String: JSONValue] = [:]74 if let routingMode { settings["routingMode"] = .string(routingMode) }75 if let toolsEnabled { settings["toolsEnabled"] = .bool(toolsEnabled) }76 try c.encode(JSONValue.object(settings), forKey: .settings)77 try c.encode(pinned, forKey: .pinned); try c.encode(favorite, forKey: .favorite)78 try c.encodeIfPresent(archivedAt.map(ISO8601.string), forKey: .archived_at); try c.encodeIfPresent(parentId, forKey: .parent_id); try c.encodeIfPresent(branchLabel, forKey: .branch_label)79 try c.encode(mode, forKey: .mode); try c.encode(messageCount, forKey: .message_count); try c.encode(totalCostUsd, forKey: .total_cost_usd)80 try c.encodeIfPresent(lastMessageAt.map(ISO8601.string), forKey: .last_message_at)81 try c.encode(ISO8601.string(createdAt), forKey: .created_at); try c.encode(ISO8601.string(updatedAt), forKey: .updated_at)82 try c.encodeIfPresent(projectName, forKey: .project_name); try c.encodeIfPresent(projectColor, forKey: .project_color)83 try c.encode(hasDraft, forKey: .has_draft); try c.encode(streaming, forKey: .streaming)84 }85}8687/// Number of sibling versions under one parent (branch navigation "2 / 3").88struct SiblingCount: Codable, Hashable, Sendable {89 var parentId: String?90 var role: String91 var n: Int92 private enum K: String, CodingKey { case parent_id, role, n }93 init(from decoder: Decoder) throws {94 let c = try decoder.container(keyedBy: K.self)95 parentId = c.flexString(.parent_id); role = c.flexString(.role) ?? "assistant"; n = c.flexInt(.n) ?? 196 }97 func encode(to encoder: Encoder) throws {98 var c = encoder.container(keyedBy: K.self)99 try c.encodeIfPresent(parentId, forKey: .parent_id); try c.encode(role, forKey: .role); try c.encode(n, forKey: .n)100 }101}102103struct Attachment: Identifiable, Hashable, Sendable {104 var id: String105 var filename: String106 var mime: String107 var sizeBytes: Int?108 var pageCount: Int?109 var messageId: String?110 var conversationId: String?111 var projectId: String?112 var conversationTitle: String?113 var indexStatus: String?114 var indexError: String?115 var chunkCount: Int?116 var createdAt: Date?117118 var isImage: Bool { mime.hasPrefix("image/") }119 var asPart: MessagePart {120 isImage ? .image(attachmentId: id, mime: mime, name: filename) : .file(attachmentId: id, mime: mime, name: filename, size: sizeBytes, pages: pageCount)121 }122}123124extension Attachment: Codable {125 private enum K: String, CodingKey { case id, filename, mime, size_bytes, size, page_count, pages, message_id, conversation_id, project_id, conversation_title, index_status, index_error, chunk_count, created_at }126 init(from decoder: Decoder) throws {127 let c = try decoder.container(keyedBy: K.self)128 id = try c.decode(String.self, forKey: .id)129 filename = c.flexString(.filename) ?? "fichier"130 mime = c.flexString(.mime) ?? "application/octet-stream"131 sizeBytes = c.flexInt(.size_bytes) ?? c.flexInt(.size)132 pageCount = c.flexInt(.page_count) ?? c.flexInt(.pages)133 messageId = c.flexString(.message_id); conversationId = c.flexString(.conversation_id); projectId = c.flexString(.project_id)134 conversationTitle = c.flexString(.conversation_title)135 indexStatus = c.flexString(.index_status); indexError = c.flexString(.index_error); chunkCount = c.flexInt(.chunk_count)136 createdAt = c.flexDate(.created_at)137 }138 func encode(to encoder: Encoder) throws {139 var c = encoder.container(keyedBy: K.self)140 try c.encode(id, forKey: .id); try c.encode(filename, forKey: .filename); try c.encode(mime, forKey: .mime)141 try c.encodeIfPresent(sizeBytes, forKey: .size_bytes); try c.encodeIfPresent(pageCount, forKey: .page_count)142 try c.encodeIfPresent(messageId, forKey: .message_id); try c.encodeIfPresent(conversationId, forKey: .conversation_id); try c.encodeIfPresent(projectId, forKey: .project_id)143 try c.encodeIfPresent(conversationTitle, forKey: .conversation_title); try c.encodeIfPresent(indexStatus, forKey: .index_status)144 try c.encodeIfPresent(indexError, forKey: .index_error); try c.encodeIfPresent(chunkCount, forKey: .chunk_count)145 try c.encodeIfPresent(createdAt.map(ISO8601.string), forKey: .created_at)146 }147}148149struct Artifact: Identifiable, Hashable, Sendable {150 var id: String151 var rootId: String152 var version: Int153 var type: String154 var language: String?155 var title: String156 var saved: Bool157 var messageId: String?158 var content: String?159160 var fileExtension: String {161 switch type {162 case "html": "html"; case "svg": "svg"; case "mermaid": "mmd"; case "react": language == "jsx" ? "jsx" : "tsx"163 case "python": "py"; case "typescript": "ts"; case "markdown": "md"; case "table": "csv"; case "chart": "json"164 default: "txt"165 }166 }167}168169extension Artifact: Codable {170 private enum K: String, CodingKey { case id, root_id, version, type, language, title, saved, message_id, content }171 init(from decoder: Decoder) throws {172 let c = try decoder.container(keyedBy: K.self)173 id = try c.decode(String.self, forKey: .id)174 rootId = c.flexString(.root_id) ?? id; version = c.flexInt(.version) ?? 1; type = c.flexString(.type) ?? "text"175 language = c.flexString(.language); title = c.flexString(.title) ?? "Artefact"; saved = c.flexBool(.saved) ?? false176 messageId = c.flexString(.message_id); content = c.flexString(.content)177 }178 func encode(to encoder: Encoder) throws {179 var c = encoder.container(keyedBy: K.self)180 try c.encode(id, forKey: .id); try c.encode(rootId, forKey: .root_id); try c.encode(version, forKey: .version); try c.encode(type, forKey: .type)181 try c.encodeIfPresent(language, forKey: .language); try c.encode(title, forKey: .title); try c.encode(saved, forKey: .saved)182 try c.encodeIfPresent(messageId, forKey: .message_id); try c.encodeIfPresent(content, forKey: .content)183 }184}185186/// `GET /api/conversations/:id?all=1` — everything needed to display a conversation, also the unit cached for offline reading.187struct ConversationDetail: Codable, Hashable, Sendable {188 var conversation: Conversation189 var messages: [ChatMessage]190 var siblings: [SiblingCount]191 var attachments: [Attachment]192 var artifacts: [Artifact]193 var draft: String?194 var liveRun: String?195196 private enum K: String, CodingKey { case conversation, messages, siblings, attachments, artifacts, draft, liveRun }197 init(conversation: Conversation, messages: [ChatMessage], siblings: [SiblingCount] = [], attachments: [Attachment] = [], artifacts: [Artifact] = [], draft: String? = nil, liveRun: String? = nil) {198 self.conversation = conversation; self.messages = messages; self.siblings = siblings; self.attachments = attachments; self.artifacts = artifacts; self.draft = draft; self.liveRun = liveRun199 }200 init(from decoder: Decoder) throws {201 let c = try decoder.container(keyedBy: K.self)202 conversation = try c.decode(Conversation.self, forKey: .conversation)203 messages = (try c.decodeIfPresent([ChatMessage].self, forKey: .messages)) ?? []204 siblings = (try? c.decodeIfPresent([SiblingCount].self, forKey: .siblings)) ?? []205 attachments = (try? c.decodeIfPresent([Attachment].self, forKey: .attachments)) ?? []206 artifacts = (try? c.decodeIfPresent([Artifact].self, forKey: .artifacts)) ?? []207 draft = c.flexString(.draft)208 liveRun = c.flexString(.liveRun)209 }210211 /// The thread on screen: active messages in chronological order.212 var activeMessages: [ChatMessage] { messages.filter(\.active) }213}214215struct Project: Identifiable, Hashable, Sendable, Codable {216 var id: String217 var name: String218 var description: String?219 var instructions: String?220 var defaultModel: String?221 var color: String?222 var conversationCount: Int223 var fileCount: Int224 private enum K: String, CodingKey { case id, name, description, instructions, default_model, color, conversation_count, file_count }225 init(from decoder: Decoder) throws {226 let c = try decoder.container(keyedBy: K.self)227 id = try c.decode(String.self, forKey: .id); name = c.flexString(.name) ?? "Projet"228 description = c.flexString(.description); instructions = c.flexString(.instructions); defaultModel = c.flexString(.default_model); color = c.flexString(.color)229 conversationCount = c.flexInt(.conversation_count) ?? 0; fileCount = c.flexInt(.file_count) ?? 0230 }231 func encode(to encoder: Encoder) throws {232 var c = encoder.container(keyedBy: K.self)233 try c.encode(id, forKey: .id); try c.encode(name, forKey: .name); try c.encodeIfPresent(description, forKey: .description); try c.encodeIfPresent(instructions, forKey: .instructions)234 try c.encodeIfPresent(defaultModel, forKey: .default_model); try c.encodeIfPresent(color, forKey: .color)235 try c.encode(conversationCount, forKey: .conversation_count); try c.encode(fileCount, forKey: .file_count)236 }237}238239struct SearchHit: Identifiable, Hashable, Sendable, Decodable {240 var id: String241 var conversationId: String242 var role: String243 var modelId: String?244 var createdAt: Date245 var title: String246 var snippet: String247 private enum K: String, CodingKey { case id, conversation_id, role, model_id, created_at, title, snippet }248 init(from decoder: Decoder) throws {249 let c = try decoder.container(keyedBy: K.self)250 id = try c.decode(String.self, forKey: .id); conversationId = c.flexString(.conversation_id) ?? ""251 role = c.flexString(.role) ?? "assistant"; modelId = c.flexString(.model_id); createdAt = c.flexDate(.created_at) ?? Date()252 title = c.flexString(.title) ?? Conversation.untitled; snippet = c.flexString(.snippet) ?? ""253 }254}255