SPB Git

Repository Public

Preferences.swift 6.0 KB 101 lines swift

1//2//  SP Chat iOS3//  File: SPChat-iOS/Core/Persistence/Preferences.swift4//  Description: Device-level preferences (model choice, appearance, dictation, haptics) stored in UserDefaults.5//6//  Author: Simon-Pierre Boucher7//  Contact: contact@spboucher.ai8//910import Foundation11import Observation12import SwiftUI1314enum AppTheme: String, CaseIterable, Identifiable, Sendable {15    case system, light, dark16    var id: String { rawValue }17    var colorScheme: ColorScheme? { switch self { case .system: nil; case .light: .light; case .dark: .dark } }18}1920enum ListFilter: String, CaseIterable, Identifiable, Sendable {21    case all, favorites, pinned, archived22    var id: String { rawValue }23}2425/// Preferences of this device. Nothing here is secret; account-level settings (spending limits, memory) live on the server.26@MainActor27@Observable28final class Preferences {29    private let d: UserDefaults3031    var modelId: String? { didSet { d.set(modelId, forKey: "modelId") } }32    var routingMode: RoutingMode { didSet { d.set(routingMode.rawValue, forKey: "routingMode") } }33    var effort: Effort? { didSet { d.set(effort?.rawValue, forKey: "effort") } }34    var presetId: String? { didSet { d.set(presetId, forKey: "presetId") } }35    var toolsEnabled: Bool { didSet { d.set(toolsEnabled, forKey: "toolsEnabled") } }36    /// « Recherche web » toggle of the composer: the model must search before answering.37    var webSearch: Bool { didSet { d.set(webSearch, forKey: "webSearch") } }38    /// Tools the owner switched off in the composer's tool picker (empty = every tool of the server).39    var disabledTools: Set<String> { didSet { d.set(Array(disabledTools).sorted(), forKey: "disabledTools") } }40    var temperature: Double? { didSet { d.set(temperature, forKey: "temperature") } }41    var maxOutputTokens: Int? { didSet { d.set(maxOutputTokens, forKey: "maxOutputTokens") } }42    /// Shows the advisory « modèle suggéré » chip of the Smart Model Router while typing.43    var suggestModel: Bool { didSet { d.set(suggestModel, forKey: "suggestModel") } }44    var showReasoning: Bool { didSet { d.set(showReasoning, forKey: "showReasoning") } }45    var showMessageStats: Bool { didSet { d.set(showMessageStats, forKey: "showMessageStats") } }46    var theme: AppTheme { didSet { d.set(theme.rawValue, forKey: "theme") } }47    var hapticsEnabled: Bool { didSet { d.set(hapticsEnabled, forKey: "hapticsEnabled") } }48    /// Hardware keyboard: Return sends, Shift-Return inserts a line break (the on-screen keyboard always inserts a line break).49    var sendWithReturn: Bool { didSet { d.set(sendWithReturn, forKey: "sendWithReturn") } }50    var dictationLanguage: String { didSet { d.set(dictationLanguage, forKey: "dictationLanguage") } }51    var dictationAutoSend: Bool { didSet { d.set(dictationAutoSend, forKey: "dictationAutoSend") } }52    var ttsVoice: String { didSet { d.set(ttsVoice, forKey: "ttsVoice") } }53    var compressImages: Bool { didSet { d.set(compressImages, forKey: "compressImages") } }54    var listFilter: ListFilter { didSet { d.set(listFilter.rawValue, forKey: "listFilter") } }55    /// Messages written offline are posted automatically when the connection returns.56    var autoSendOutbox: Bool { didSet { d.set(autoSendOutbox, forKey: "autoSendOutbox") } }57    var notifyWhenAnswerReady: Bool { didSet { d.set(notifyWhenAnswerReady, forKey: "notifyWhenAnswerReady") } }5859    init(defaults: UserDefaults = .standard) {60        d = defaults61        func bool(_ k: String, _ fallback: Bool) -> Bool { defaults.object(forKey: k) == nil ? fallback : defaults.bool(forKey: k) }62        modelId = defaults.string(forKey: "modelId")63        routingMode = RoutingMode(rawValue: defaults.string(forKey: "routingMode") ?? "") ?? .manual64        effort = defaults.string(forKey: "effort").flatMap(Effort.init(rawValue:))65        presetId = defaults.string(forKey: "presetId")66        toolsEnabled = bool("toolsEnabled", true)67        webSearch = bool("webSearch", false)68        disabledTools = Set(defaults.stringArray(forKey: "disabledTools") ?? [])69        temperature = defaults.object(forKey: "temperature") as? Double70        maxOutputTokens = defaults.object(forKey: "maxOutputTokens") as? Int71        suggestModel = bool("suggestModel", true)72        showReasoning = bool("showReasoning", true)73        showMessageStats = bool("showMessageStats", true)74        theme = AppTheme(rawValue: defaults.string(forKey: "theme") ?? "") ?? .system75        hapticsEnabled = bool("hapticsEnabled", true)76        sendWithReturn = bool("sendWithReturn", true)77        dictationLanguage = defaults.string(forKey: "dictationLanguage") ?? ""78        dictationAutoSend = bool("dictationAutoSend", false)79        ttsVoice = defaults.string(forKey: "ttsVoice") ?? "alloy"80        compressImages = bool("compressImages", true)81        listFilter = ListFilter(rawValue: defaults.string(forKey: "listFilter") ?? "") ?? .all82        autoSendOutbox = bool("autoSendOutbox", true)83        notifyWhenAnswerReady = bool("notifyWhenAnswerReady", false)84    }8586    /// Options applied to the next message. `tools` = the tools the server offers (to turn the owner's exclusions87    /// into the `toolNames` subset the API expects).88    func sendOptions(tools: [ServerTool] = []) -> SendOptions {89        var o = SendOptions(modelId: routingMode == .manual ? modelId : nil, routingMode: routingMode, presetId: presetId, effort: effort, toolsEnabled: toolsEnabled)90        o.webSearch = webSearch ? true : nil91        o.temperature = temperature; o.maxOutputTokens = maxOutputTokens92        let allowed = tools.map(\.name).filter { !disabledTools.contains($0) }93        if !disabledTools.isEmpty, !tools.isEmpty, allowed.count < tools.count { o.toolNames = allowed }94        return o95    }96    var sendOptions: SendOptions { sendOptions() }9798    /// Clears what relates to the account (sign-out); appearance choices are kept.99    func resetAccountScoped() { modelId = nil; presetId = nil; routingMode = .manual; effort = nil; listFilter = .all; webSearch = false; disabledTools = []; temperature = nil; maxOutputTokens = nil }100}101
Files main
Menu

Theme