Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Services/RemoteInference/Sources/RemoteNodeRegistry.swift3// Description: The user's list of trusted remote nodes, persisted as JSON (keys stay in the Keychain), with health and model discovery.4import Foundation5import LocalChatCore6import LocalChatNetworking78public struct RemoteNodeStatus: Sendable, Identifiable {9 public var node: RemoteNode10 public var health: NodeHealth11 public var models: [ModelDescriptor]12 public var id: UUID { node.id }13 public init(node: RemoteNode, health: NodeHealth, models: [ModelDescriptor]) { self.node = node; self.health = health; self.models = models }14}1516public actor RemoteNodeRegistry {17 private let fileURL: URL?18 private let apiKey: @Sendable (RemoteNode) -> String?19 private let http: HTTPClient20 private var nodes: [RemoteNode] = []21 private var statuses: [UUID: RemoteNodeStatus] = [:]2223 /// - Parameter fileURL: where the node list is stored; nil keeps it in memory only.24 public init(fileURL: URL?, apiKey: @escaping @Sendable (RemoteNode) -> String?, http: HTTPClient = HTTPClient()) {25 self.fileURL = fileURL; self.apiKey = apiKey; self.http = http26 if let fileURL, let data = try? Data(contentsOf: fileURL), let saved = try? CoreCoding.decoder.decode([RemoteNode].self, from: data) { nodes = saved }27 }2829 public func allNodes() -> [RemoteNode] { nodes }30 public func node(id: UUID) -> RemoteNode? { nodes.first { $0.id == id } }31 public func status(id: UUID) -> RemoteNodeStatus? { statuses[id] }32 public func client(for node: RemoteNode) -> RemoteNodeClient { RemoteNodeClient(node: node, apiKey: apiKey(node), http: http) }3334 /// Adds a node after validating its URL against the transport policy.35 @discardableResult36 public func add(_ node: RemoteNode) throws -> RemoteNode {37 _ = try RemoteNodeClient(node: node, apiKey: nil, http: http).baseURL()38 nodes.removeAll { $0.id == node.id }; nodes.append(node)39 try save(); return node40 }4142 public func update(_ node: RemoteNode) throws {43 _ = try RemoteNodeClient(node: node, apiKey: nil, http: http).baseURL()44 guard let index = nodes.firstIndex(where: { $0.id == node.id }) else { nodes.append(node); try save(); return }45 nodes[index] = node; statuses[node.id] = nil46 try save()47 }4849 public func remove(id: UUID) throws {50 nodes.removeAll { $0.id == id }; statuses[id] = nil51 try save()52 }5354 /// Checks one node: health, latency and the models it serves.55 @discardableResult56 public func refresh(id: UUID) async -> RemoteNodeStatus? {57 guard let node = node(id: id) else { return nil }58 let status = await Self.probe(node, client: client(for: node))59 if self.node(id: id) != nil { statuses[id] = status }60 return status61 }6263 /// Checks every node concurrently. Disabled nodes are reported as unknown without any network request.64 @discardableResult65 public func refresh() async -> [RemoteNodeStatus] {66 let targets = nodes.map { ($0, client(for: $0)) }67 let results = await withTaskGroup(of: RemoteNodeStatus.self) { group -> [RemoteNodeStatus] in68 for (node, client) in targets { group.addTask { await Self.probe(node, client: client) } }69 var out: [RemoteNodeStatus] = []70 for await status in group { out.append(status) }71 return out72 }73 statuses = Dictionary(results.filter { r in nodes.contains { $0.id == r.id } }.map { ($0.id, $0) }, uniquingKeysWith: { $1 })74 return nodes.compactMap { statuses[$0.id] }75 }7677 /// Models of every node that answered at the last refresh.78 public func availableModels() -> [ModelDescriptor] {79 nodes.filter(\.isEnabled).flatMap { statuses[$0.id]?.health.state == .online ? statuses[$0.id]?.models ?? [] : [] }80 }8182 public func diagnostics(id: UUID) async -> [DiagnosticCheck] {83 guard let node = node(id: id) else { return [] }84 return await client(for: node).diagnostics()85 }8687 private static func probe(_ node: RemoteNode, client: RemoteNodeClient) async -> RemoteNodeStatus {88 guard node.isEnabled else { return RemoteNodeStatus(node: node, health: NodeHealth(state: .unknown, detail: "Disabled."), models: []) }89 let started = Date()90 do {91 let models = try await client.listModels(timeout: 8)92 return RemoteNodeStatus(node: node, health: NodeHealth(state: .online, latency: Date().timeIntervalSince(started), models: models.map(\.id)),93 models: models.map(client.descriptor(for:)))94 } catch {95 // the generic path tells unauthorized from offline and words the reason96 return RemoteNodeStatus(node: node, health: await client.health(), models: [])97 }98 }99100 private func save() throws {101 guard let fileURL else { return }102 try FileManager.default.createDirectory(at: fileURL.deletingLastPathComponent(), withIntermediateDirectories: true)103 try CoreCoding.encoder.encode(nodes).write(to: fileURL, options: .atomic)104 }105}106