Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatCore/Sources/Services.swift3// Description: Contracts between modules that must not depend on each other: inference service, HTTP route handlers, synchronization records.4import Foundation56/// What the Local API server, the arena and the agents see of the engine.7public protocol InferenceService: TextGenerating {8 func listModels() async -> [ModelDescriptor]9 func embed(modelID: String, texts: [String]) async throws -> [[Float]]10}1112// MARK: HTTP (shared by the Local API server and the modules that add routes to it)1314public struct HTTPRequestMessage: Sendable {15 public var method: String // uppercased16 public var path: String // without the query string17 public var query: [String: String]18 public var headers: [String: String] // keys lowercased19 public var body: Data20 public var remoteAddress: String?21 public init(method: String, path: String, query: [String: String] = [:], headers: [String: String] = [:], body: Data = Data(), remoteAddress: String? = nil) {22 self.method = method; self.path = path; self.query = query; self.headers = headers; self.body = body; self.remoteAddress = remoteAddress23 }24}2526public struct HTTPResponseMessage: Sendable {27 public var status: Int28 public var headers: [String: String]29 public var body: Data30 public init(status: Int = 200, headers: [String: String] = [:], body: Data = Data()) { self.status = status; self.headers = headers; self.body = body }31 public static func json<T: Encodable>(_ value: T, status: Int = 200) -> HTTPResponseMessage {32 let encoder = JSONEncoder(); encoder.dateEncodingStrategy = .iso860133 return HTTPResponseMessage(status: status, headers: ["Content-Type": "application/json"], body: (try? encoder.encode(value)) ?? Data("{}".utf8))34 }35 public static func error(_ status: Int, _ message: String, type: String = "invalid_request_error") -> HTTPResponseMessage {36 json(["error": ["message": message, "type": type]], status: status)37 }38}3940/// Extra authenticated routes served by the Local API server (e.g. the sync relay under /lc/v1/sync/…). Return nil for paths you do not own.41public protocol HTTPRouteHandler: Sendable {42 func handle(_ request: HTTPRequestMessage) async -> HTTPResponseMessage?43}4445// MARK: Synchronization4647/// One entity version. `payload` is the JSON encoding of the Core type named by `kind` (ISO-8601 dates).48public struct SyncRecord: Codable, Sendable, Hashable {49 public enum Kind: String, Codable, Sendable, CaseIterable {50 case conversation, message, folder, template, workspace, agent, collection, preferences51 }52 public var kind: Kind53 public var id: String // entity UUID string ("preferences" for the singleton)54 public var updatedAt: Date55 public var isDeleted: Bool56 public var payload: Data57 public init(kind: Kind, id: String, updatedAt: Date, isDeleted: Bool = false, payload: Data) {58 self.kind = kind; self.id = id; self.updatedAt = updatedAt; self.isDeleted = isDeleted; self.payload = payload59 }60}6162/// Implemented by the conversation store. Conflict rule: last writer wins on `updatedAt`; deletions are tombstones.63public protocol SyncableStore: Sendable {64 func changes(since: Date?) async throws -> [SyncRecord]65 /// Applies remote records that are newer than the local version. Returns how many were applied.66 func apply(_ records: [SyncRecord]) async throws -> Int67}6869public enum CoreCoding {70 public static var encoder: JSONEncoder { let e = JSONEncoder(); e.dateEncodingStrategy = .iso8601; e.outputFormatting = [.sortedKeys]; return e }71 public static var decoder: JSONDecoder { let d = JSONDecoder(); d.dateDecodingStrategy = .iso8601; return d }72}7374// MARK: Preferences (synchronized)7576public struct AppPreferences: Codable, Sendable, Hashable {77 public var defaultModelID: String?78 public var defaultPreset: InferencePreset79 public var defaultSystemPrompt: String80 public var routing: RoutingPreferences81 public var autoTitle: Bool82 public var sendWithReturn: Bool83 public var showMetricsInChat: Bool84 public var appLockEnabled: Bool85 public var hapticsEnabled: Bool86 public var keepModelLoadedMinutes: Int // 0 = unload right after the answer (useful on iPhone), -1 = keep until quit87 public var appearance: String // "system", "light", "dark"88 public var accent: String89 public var updatedAt: Date90 public init(defaultModelID: String? = nil, defaultPreset: InferencePreset = .balanced, defaultSystemPrompt: String = "",91 routing: RoutingPreferences = .init(), autoTitle: Bool = true, sendWithReturn: Bool = true, showMetricsInChat: Bool = true,92 appLockEnabled: Bool = false, hapticsEnabled: Bool = true, keepModelLoadedMinutes: Int = -1, appearance: String = "system",93 accent: String = "indigo", updatedAt: Date = Date()) {94 self.defaultModelID = defaultModelID; self.defaultPreset = defaultPreset; self.defaultSystemPrompt = defaultSystemPrompt95 self.routing = routing; self.autoTitle = autoTitle; self.sendWithReturn = sendWithReturn; self.showMetricsInChat = showMetricsInChat96 self.appLockEnabled = appLockEnabled; self.hapticsEnabled = hapticsEnabled; self.keepModelLoadedMinutes = keepModelLoadedMinutes97 self.appearance = appearance; self.accent = accent; self.updatedAt = updatedAt98 }99}100