|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// One item in a conversation transcript: a prompt, a piece of agent prose, |
| 4 | +/// a tool run, a diff, an actionable request, or a lifecycle transition. |
| 5 | +/// |
| 6 | +/// `ChatMessage` is a pure value shared across platforms and across the |
| 7 | +/// wire: the Mac-side transcript service produces it, the mobile RPC layer |
| 8 | +/// transports it, and both the iOS and (future) macOS chat surfaces render |
| 9 | +/// it. Rendering decisions are keyed off ``kind``. |
| 10 | +public struct ChatMessage: Identifiable, Sendable, Equatable, Codable { |
| 11 | + /// Stable identity for the message, unique within its session. |
| 12 | + /// |
| 13 | + /// For transcript-derived messages this is the transcript entry's own |
| 14 | + /// UUID when available, otherwise a deterministic value derived from |
| 15 | + /// ``seq``. Stability matters: SwiftUI diffing and read-state tracking |
| 16 | + /// both key off it. |
| 17 | + public let id: String |
| 18 | + |
| 19 | + /// Monotonic position of the message within the session transcript. |
| 20 | + /// |
| 21 | + /// Doubles as the pagination cursor: history pages are requested as |
| 22 | + /// "messages before seq N". Derived from the transcript line index on |
| 23 | + /// the producing side. |
| 24 | + public let seq: Int |
| 25 | + |
| 26 | + /// Who authored the message. |
| 27 | + public let role: ChatRole |
| 28 | + |
| 29 | + /// When the message was produced, from the transcript when available. |
| 30 | + public let timestamp: Date |
| 31 | + |
| 32 | + /// The typed payload that decides how the message renders. |
| 33 | + public let kind: ChatMessageKind |
| 34 | + |
| 35 | + /// Creates a chat message. |
| 36 | + /// |
| 37 | + /// - Parameters: |
| 38 | + /// - id: Stable unique identity within the session. |
| 39 | + /// - seq: Monotonic transcript position, used as the paging cursor. |
| 40 | + /// - role: Who authored the message. |
| 41 | + /// - timestamp: When the message was produced. |
| 42 | + /// - kind: Typed payload deciding the rendering. |
| 43 | + public init(id: String, seq: Int, role: ChatRole, timestamp: Date, kind: ChatMessageKind) { |
| 44 | + self.id = id |
| 45 | + self.seq = seq |
| 46 | + self.role = role |
| 47 | + self.timestamp = timestamp |
| 48 | + self.kind = kind |
| 49 | + } |
| 50 | + |
| 51 | + private enum CodingKeys: String, CodingKey { |
| 52 | + case id |
| 53 | + case seq |
| 54 | + case role |
| 55 | + case timestamp |
| 56 | + case kind |
| 57 | + } |
| 58 | + |
| 59 | + public init(from decoder: any Decoder) throws { |
| 60 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 61 | + // id and seq are genuinely load-bearing (identity, paging cursor); |
| 62 | + // their absence is corruption and throws. Everything else fails |
| 63 | + // open so one evolved field can't sink a whole page. |
| 64 | + self.id = try container.decode(String.self, forKey: .id) |
| 65 | + self.seq = try container.decode(Int.self, forKey: .seq) |
| 66 | + let rawRole = try? container.decode(String.self, forKey: .role) |
| 67 | + self.role = rawRole.flatMap(ChatRole.init(rawValue:)) ?? .agent |
| 68 | + self.timestamp = (try? container.decode(Date.self, forKey: .timestamp)) |
| 69 | + ?? Date(timeIntervalSince1970: 0) |
| 70 | + self.kind = (try? container.decode(ChatMessageKind.self, forKey: .kind)) |
| 71 | + ?? .unsupported(ChatUnsupportedPayload(rawType: "undecodable")) |
| 72 | + } |
| 73 | +} |
0 commit comments