SPB Git

Repository Public

ConstellationModels.swift 5.3 KB 97 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: SPChat/Core/Models/ConstellationModels.swift3// Description: Conversation Constellation: the owner's conversations as a graph (nodes, typed links, projects, the server's basis texts) and its endpoint.4import Foundation56public struct Constellation: Sendable, Hashable, Decodable {7    public struct Node: Sendable, Hashable, Identifiable, Decodable {8        public var id: String9        public var title: String10        public var projectId: String?11        public var projectName: String?12        /// Hex colour of the project (`#RRGGBB`) when the owner gave it one.13        public var projectColor: String?14        public var modelId: String?15        public var messages: Int16        public var costUsd: Double?17        public var parentId: String?18        public var branchLabel: String?19        public var pinned: Bool20        public var favorite: Bool21        public var lastAt: Date?22        public var createdAt: Date?2324        enum CodingKeys: String, CodingKey { case id, title, projectId, projectName, projectColor, modelId, messages, costUsd, parentId, branchLabel, pinned, favorite, lastAt, createdAt }25        public init(from decoder: Decoder) throws {26            let c = try decoder.container(keyedBy: CodingKeys.self)27            id = try c.decode(String.self, forKey: .id)28            title = c.optional(String.self, .title) ?? ""29            projectId = c.optional(String.self, .projectId); projectName = c.optional(String.self, .projectName); projectColor = c.optional(String.self, .projectColor)30            modelId = c.optional(String.self, .modelId); messages = c.flexInt(.messages) ?? 0; costUsd = c.flexDouble(.costUsd)31            parentId = c.optional(String.self, .parentId); branchLabel = c.optional(String.self, .branchLabel)32            pinned = c.optional(Bool.self, .pinned) ?? false; favorite = c.optional(Bool.self, .favorite) ?? false33            lastAt = c.optional(Date.self, .lastAt); createdAt = c.optional(Date.self, .createdAt)34        }35    }3637    public struct Link: Sendable, Hashable, Decodable {38        public var source: String39        public var target: String40        /// `branch` · `project` · `topic` — any other value is kept as-is and drawn like a topic link.41        public var kind: String42        public var weight: Double4344        enum CodingKeys: String, CodingKey { case source, target, kind, weight }45        public init(from decoder: Decoder) throws {46            let c = try decoder.container(keyedBy: CodingKeys.self)47            source = try c.decode(String.self, forKey: .source); target = try c.decode(String.self, forKey: .target)48            kind = c.optional(String.self, .kind) ?? "topic"; weight = c.flexDouble(.weight) ?? 149        }50        public init(source: String, target: String, kind: String, weight: Double) { self.source = source; self.target = target; self.kind = kind; self.weight = weight }51    }5253    public struct ProjectGroup: Sendable, Hashable, Identifiable, Decodable {54        public var id: String55        public var name: String?56        public var color: String?57        public var count: Int58        enum CodingKeys: String, CodingKey { case id, name, color, count }59        public init(from decoder: Decoder) throws {60            let c = try decoder.container(keyedBy: CodingKeys.self)61            id = try c.decode(String.self, forKey: .id); name = c.optional(String.self, .name); color = c.optional(String.self, .color); count = c.flexInt(.count) ?? 062        }63    }6465    public var nodes: [Node]66    /// Links whose two ends are in `nodes` (a link to a conversation outside the window is dropped).67    public var links: [Link]68    public var projects: [ProjectGroup]69    /// The server's explanation of each link kind (`topic`, `branch`, `project`) — shown verbatim in the legend.70    public var basis: [String: String]7172    enum CodingKeys: String, CodingKey { case nodes, links, projects, basis }73    public init(from decoder: Decoder) throws {74        let c = try decoder.container(keyedBy: CodingKeys.self)75        var seen = Set<String>()76        nodes = (c.optional([Lossy<Node>].self, .nodes) ?? []).compactMap(\.value).filter { seen.insert($0.id).inserted }77        links = (c.optional([Lossy<Link>].self, .links) ?? []).compactMap(\.value).filter { $0.source != $0.target && seen.contains($0.source) && seen.contains($0.target) }78        projects = (c.optional([Lossy<ProjectGroup>].self, .projects) ?? []).compactMap(\.value)79        basis = (c.optional([String: JSONValue].self, .basis) ?? [:]).compactMapValues(\.stringValue)80    }8182    public func links(ofKind kind: String) -> [Link] { links.filter { $0.kind == kind } }83    /// Ids directly linked to a node.84    public func neighbours(of id: String) -> Set<String> {85        var out = Set<String>()86        for l in links { if l.source == id { out.insert(l.target) } else if l.target == id { out.insert(l.source) } }87        return out88    }89}9091public extension APIClient {92    /// `GET /api/constellation?limit=&project=` — recomputed by the server from existing rows (10…300 conversations).93    func constellation(limit: Int = 160, projectId: String? = nil) async throws -> Constellation {94        try await decode(Constellation.self, .get, "/api/constellation", query: ["limit": String(min(max(limit, 10), 300)), "project": projectId])95    }96}97
Files main
Menu

Theme