Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/VQuantKit/Sync/VQEnvelope.swift3// Description: The object envelope of the store, raw and typed.4import Foundation56/// The envelope every object route returns (CONTRACT §1.1).7public struct VQObjectEnvelope: Sendable, Hashable, Codable, VQJSONDecodable, Identifiable {8 public var kind: VQObjectKind9 public var id: String10 /// 1 at creation, +1 at every accepted write. 0 only for an object that exists locally and was never accepted.11 public var rev: Int12 /// Per-owner change counter: orders the change feed.13 public var seq: Int14 public var createdAt: Date15 public var updatedAt: Date16 /// A tombstone: `data` is `{}`.17 public var deleted: Bool18 /// `"web"`, `"server"`, or a `vq_devices.id`.19 public var device: String20 public var data: VQJSON2122 public init(kind: VQObjectKind, id: String, rev: Int, seq: Int, createdAt: Date, updatedAt: Date, deleted: Bool = false, device: String = "", data: VQJSON) {23 self.kind = kind; self.id = id; self.rev = rev; self.seq = seq; self.createdAt = createdAt; self.updatedAt = updatedAt24 self.deleted = deleted; self.device = device; self.data = data25 }2627 public init(json j: VQJSON) {28 kind = VQObjectKind(j.str("kind")); id = j.str("id"); rev = j.int("rev") ?? 0; seq = j.int("seq") ?? 029 createdAt = j.date("createdAt") ?? Date(timeIntervalSince1970: 0); updatedAt = j.date("updatedAt") ?? Date(timeIntervalSince1970: 0)30 deleted = j.flag("deleted"); device = j.str("device"); data = j["data"] ?? .object([:])31 }3233 public var json: VQJSON {34 ["kind": .string(kind.rawValue), "id": .string(id), "rev": .number(Double(rev)), "seq": .number(Double(seq)),35 "createdAt": .number((createdAt.timeIntervalSince1970 * 1000).rounded()), "updatedAt": .number((updatedAt.timeIntervalSince1970 * 1000).rounded()),36 "deleted": .bool(deleted), "device": .string(device), "data": data]37 }3839 public init(from decoder: Decoder) throws { self.init(json: try VQJSON(from: decoder)) }40 public func encode(to encoder: Encoder) throws { try json.encode(to: encoder) }4142 public var key: VQObjectKey { VQObjectKey(kind: kind, id: id) }43 /// The typed `data`, when the envelope is of that kind and not a tombstone.44 public func typed<T: VQSyncData>(_ type: T.Type = T.self) -> VQObject<T>? {45 guard kind == T.kind, !deleted else { return nil }46 return VQObject(envelope: self)47 }48}4950public struct VQObjectKey: Sendable, Hashable, Codable, CustomStringConvertible {51 public var kind: VQObjectKind52 public var id: String53 public init(kind: VQObjectKind, id: String) { self.kind = kind; self.id = id }54 public var description: String { "\(kind.rawValue)/\(id)" }55}5657/// An envelope with its `data` typed.58public struct VQObject<T: VQSyncData>: Sendable, Hashable, Identifiable {59 public var id: String60 public var rev: Int61 public var seq: Int62 public var createdAt: Date63 public var updatedAt: Date64 public var device: String65 public var data: T6667 public init(envelope e: VQObjectEnvelope) {68 id = e.id; rev = e.rev; seq = e.seq; createdAt = e.createdAt; updatedAt = e.updatedAt; device = e.device69 data = T(json: e.data)70 }71 public var envelope: VQObjectEnvelope {72 VQObjectEnvelope(kind: T.kind, id: id, rev: rev, seq: seq, createdAt: createdAt, updatedAt: updatedAt, deleted: false, device: device, data: data.json)73 }74}7576/// A replaced revision listed by `GET …/versions`.77public struct VQObjectVersion: Sendable, Hashable, VQJSONDecodable {78 public var rev: Int79 public var updatedAt: Date?80 public var device: String81 public init(json j: VQJSON) { rev = j.int("rev") ?? 0; updatedAt = j.date("updatedAt"); device = j.str("device") }82}8384/// A page of `GET /api/v2/sync/changes`.85public struct VQChangesPage: Sendable, Hashable, VQJSONDecodable {86 /// Latest state of every object changed after `since`, ascending `seq`, tombstones included.87 public var changes: [VQObjectEnvelope]88 /// The owner's latest `seq` (the client's cursor once `more` is false).89 public var seq: Int90 public var more: Bool91 public init(changes: [VQObjectEnvelope], seq: Int, more: Bool) { self.changes = changes; self.seq = seq; self.more = more }92 public init(json j: VQJSON) { changes = j.models("changes"); seq = j.int("seq") ?? 0; more = j.flag("more") }93}9495/// Events of `GET /api/v2/sync/stream`.96public enum VQSyncStreamEvent: Sendable, Equatable {97 case hello(seq: Int)98 case change(VQChangeNotice)99 case hint(VQSyncHint)100 /// An event type this version does not know (kept, never an error).101 case other(name: String, payload: VQJSON)102103 public init(_ event: VQSSEEvent) {104 let j = event.json105 switch j["type"]?.stringValue ?? event.name {106 case "hello": self = .hello(seq: j.int("seq") ?? 0)107 case "change": self = .change(VQChangeNotice(json: j))108 case "hint": self = .hint(VQSyncHint(json: j))109 default: self = .other(name: event.name, payload: j)110 }111 }112}113114public struct VQChangeNotice: Sendable, Hashable, VQJSONDecodable {115 public var kind: VQObjectKind116 public var id: String117 public var rev: Int118 public var seq: Int119 public var deleted: Bool120 public var device: String121 public init(json j: VQJSON) {122 kind = VQObjectKind(j.str("kind")); id = j.str("id"); rev = j.int("rev") ?? 0; seq = j.int("seq") ?? 0123 deleted = j.flag("deleted"); device = j.str("device")124 }125}126127/// "Something that is not an object changed": conversations, analyses, quant jobs (CONTRACT §1.3). Refresh that list.128public struct VQSyncHint: Sendable, Hashable, VQJSONDecodable {129 public enum Topic: String, Sendable, Hashable, CaseIterable { case sessions, generations, quantJobs = "quant_jobs" }130 public var topic: VQOpen<Topic>131 /// Session, generation or job id when the server names one.132 public var id: String?133 public init(topic: Topic, id: String? = nil) { self.topic = .known(topic); self.id = id }134 public init(json j: VQJSON) { topic = VQOpen(j.str("topic")); id = j.optStr("id") }135}136