import Foundation /// One Server-Sent Event, as the HTML standard assembles it from lines. public struct SSEEvent: Sendable, Equatable { /// The event's own `id:`, if it had one. (A glance has none: Base sends /// it live only, so the cursor stays where the last event put it.) public var id: String? /// `event:`, or "message". public var type: String /// Every `data:` line, joined with newlines. public var data: String /// `retry:`, in milliseconds. public var retry: Int? } /// Assembles lines into events (the HTML standard's "interpret an event /// stream"): a blank line dispatches; `:` starts a comment (Base's pings); /// `data` lines join with "\n"; `id`, `event` and `retry` as the standard /// says. An event with no data is dropped, but its `retry` is kept for the /// next. Lines come framed by the kit's NativeStreamLines (CR, LF, CRLF). public struct SSEParser: Sendable { private var id: String? private var type = "" private var data: [String] = [] private var retry: Int? private var sawData = false public init() {} public mutating func feed(_ line: String) -> SSEEvent? { if line.isEmpty { return dispatch() } if line.hasPrefix(":") { return nil } let name: Substring, value: Substring if let colon = line.firstIndex(of: ":") { name = line[.. SSEEvent? { defer { id = nil; type = ""; data = []; sawData = false } guard sawData else { return nil } let event = SSEEvent(id: id, type: type.isEmpty ? "message" : type, data: data.joined(separator: "\n"), retry: retry) retry = nil return event } } /// A few of the reply's words, streamed: `offset` counts the UTF-8 bytes /// before them. public struct AskDelta: Decodable, Sendable, Equatable { public var turnID: String public var offset: Int64 public var text: String public init(turnID: String, offset: Int64, text: String) { self.turnID = turnID; self.offset = offset; self.text = text } enum CodingKeys: String, CodingKey { case turnID = "turn_id", offset, text } public init(from decoder: Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) turnID = c.value(.turnID, ""); offset = c.value(.offset, -1); text = c.value(.text, "") } } /// A line about a turn in flight: Equipo's glance (said once, before the /// first words) or an activity label. public struct AskLine: Decodable, Sendable, Equatable { public var turnID: String public var text: String public init(turnID: String, text: String) { self.turnID = turnID; self.text = text } enum CodingKeys: String, CodingKey { case turnID = "turn_id", text, label } public init(from decoder: Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) turnID = c.value(.turnID, ""); text = c.optional(.text) ?? c.value(.label, "") } } /// What Base's stream says (ask_relay.go `frame`, ask.js `listen`). Each /// conversation event's data is `{"type": …, "data": …}`, the data in /// `/ask/state`'s shapes; a stream that can't start says why in one /// `state` event whose data is `{"state": …}`. public enum AskEvent: Sendable, Equatable { case message(AskMessage) case turn(AskTurn) case card(AskCard) case delta(AskDelta) case glance(AskLine) case activity(AskLine) case state(AskPhase) /// The event, or nil for one this app doesn't know or can't read. public static func decode(_ event: SSEEvent) -> AskEvent? { let raw = Data(event.data.utf8) let decoder = JSONDecoder() if event.type == "state" { struct State: Decodable { var state: String? } guard let s = try? decoder.decode(State.self, from: raw) else { return nil } return .state(AskPhase(server: s.state)) } struct Envelope: Decodable { var data: T } func data(_: T.Type) -> T? { (try? decoder.decode(Envelope.self, from: raw))?.data } switch event.type { case "message": return data(AskMessage.self).flatMap { $0.id.isEmpty ? nil : .message($0) } case "turn": return data(AskTurn.self).flatMap { $0.id.isEmpty ? nil : .turn($0) } case "card": return data(AskCard.self).flatMap { $0.id.isEmpty ? nil : .card($0) } case "delta": return data(AskDelta.self).flatMap { $0.turnID.isEmpty ? nil : .delta($0) } case "glance": return data(AskLine.self).flatMap { $0.turnID.isEmpty ? nil : .glance($0) } case "activity": return data(AskLine.self).flatMap { $0.turnID.isEmpty ? nil : .activity($0) } default: return nil } } }