SPB Git

Repository Public

AccountSettings.swift 3.7 KB 86 lines swift

1//2//  SP Chat iOS3//  File: SPChat-iOS/Features/Settings/AccountSettings.swift4//  Description: Settings stored on the server and shared by the three apps: tools the server offers, Smart Follow-Ups5//               switch, the owner's standing instructions.6//7//  Author: Simon-Pierre Boucher8//  Contact: contact@spboucher.ai9//1011import Foundation12import Observation1314struct ServerTool: Identifiable, Hashable, Sendable, Codable {15    var name: String16    var description: String?17    var category: String?18    var permission: String?19    var id: String { name }2021    var label: String {22        switch name {23        case "web_search": String(localized: "Recherche web"); case "fetch_page": String(localized: "Lecture de pages"); case "python": String(localized: "Python")24        case "search_documents": String(localized: "Documents"); case "generate_image": String(localized: "Images"); case "save_memory": String(localized: "Mémoire")25        default: name26        }27    }28    var symbol: String {29        switch name { case "web_search": "globe"; case "fetch_page": "doc.plaintext"; case "python": "terminal"; case "search_documents": "doc.text.magnifyingglass"; case "generate_image": "photo"; case "save_memory": "brain.head.profile"; default: "wrench.and.screwdriver" }30    }31}3233@MainActor34@Observable35final class AccountSettings {36    private(set) var tools: [ServerTool] = []37    private(set) var followUpsEnabled = true38    private(set) var instructionsText = ""39    private(set) var instructionsEnabled = false40    /// Which integrations have a key on the server (openai, anthropic, xai, openrouter, tavily).41    private(set) var integrations: [String: Bool] = [:]42    private(set) var hasLoaded = false4344    @ObservationIgnored private let api: ChatAPI45    @ObservationIgnored private let store: LocalStore4647    init(api: ChatAPI, store: LocalStore) { self.api = api; self.store = store }4849    var webSearchAvailable: Bool { tools.contains { $0.name == "web_search" } && integrations["tavily"] != false }5051    func loadCached() async {52        guard !hasLoaded, let j: JSONValue = await store.value(for: "settings") else { return }53        apply(j)54    }5556    func refresh() async {57        guard let j = try? await api.settings() else { return }58        apply(j); hasLoaded = true59        await store.setValue(j, for: "settings")60    }6162    private func apply(_ j: JSONValue) {63        tools = j.list("tools").compactMap { $0.decoded(ServerTool.self) }64        let s = j["settings"]65        followUpsEnabled = s?["assistant"]?.bool("followUps") ?? true66        instructionsText = s?["instructions"]?.str("text") ?? ""67        instructionsEnabled = s?["instructions"]?.bool("enabled") ?? false68        integrations = (j["integrations"]?.objectValue ?? [:]).compactMapValues(\.boolValue)69    }7071    /// Applied locally only after the server confirmed it.72    func setFollowUps(_ on: Bool) async -> String? {73        do { try await api.updateSetting(key: "assistant", value: ["followUps": .bool(on)]); followUpsEnabled = on; return nil }74        catch { return (error as? APIError)?.message ?? String(localized: "Enregistrement impossible.") }75    }7677    func saveInstructions(text: String, enabled: Bool) async -> String? {78        do {79            try await api.updateSetting(key: "instructions", value: ["text": .string(String(text.prefix(20_000))), "enabled": .bool(enabled)])80            instructionsText = text; instructionsEnabled = enabled; return nil81        } catch { return (error as? APIError)?.message ?? String(localized: "Enregistrement impossible.") }82    }8384    func reset() { tools = []; followUpsEnabled = true; instructionsText = ""; instructionsEnabled = false; integrations = [:]; hasLoaded = false }85}86
Files main
Menu

Theme