SPB Git

Repository Public

SPChatApp.swift 4.8 KB 93 lines swift

1//2//  SP Chat iOS3//  File: SPChat-iOS/App/SPChatApp.swift4//  Description: Application entry point: environment, scenes, hardware-keyboard commands.5//6//  Author: Simon-Pierre Boucher7//  Contact: contact@spboucher.ai8//910import SwiftUI11import UIKit1213@main14struct SPChatApp: App {15    @UIApplicationDelegateAdaptor(AppDelegate.self) private var delegate16    @State private var environment: AppEnvironment1718    init() {19        let env = AppLaunch.makeEnvironment()20        _environment = State(initialValue: env)21        AppEnvironmentHolder.shared = env22    }2324    var body: some Scene {25        WindowGroup {26            Group {27                if AppLaunch.isRunningUnitTests { Color.clear } else { RootView() }28            }29            .environment(environment)30            .task { await AppLaunch.start(environment) }31        }32        .commands {33            // iPad hardware keyboard (hold ⌘ to see them) and the menu bar on iPadOS 2634            CommandGroup(replacing: .newItem) {35                Button("Nouvelle conversation") { environment.startNewConversation() }.keyboardShortcut("n", modifiers: .command)36            }37            CommandMenu("Conversation") {38                Button("Centre de commandes") { environment.router.sheet = .commandCenter }.keyboardShortcut("k", modifiers: .command)39                Button("Changer de modèle") { environment.router.pendingPanel = "models" }.keyboardShortcut("m", modifiers: [.command, .shift])40                Button("Explorateur de modèles") { environment.router.sheet = .explorer }.keyboardShortcut("e", modifiers: [.command, .shift])41                Button("Arène") { environment.router.arenaSeed = ArenaSeed(); environment.router.sheet = .arena }.keyboardShortcut("a", modifiers: [.command, .shift])42                Button("Recherche web") { environment.preferences.webSearch.toggle() }.keyboardShortcut("w", modifiers: [.command, .shift])43                Divider()44                Button("Rechercher dans les messages") { environment.router.sheet = .search }.keyboardShortcut("f", modifiers: [.command, .shift])45                Button("Écrire un message") { environment.router.focusComposerToken += 1 }.keyboardShortcut("l", modifiers: .command)46                Button("Arrêter la réponse") { environment.router.current?.stop() }.keyboardShortcut(".", modifiers: .command)47                Button("Copier la dernière réponse") {48                    if let text = environment.router.current?.messages.last(where: { $0.role == .assistant })?.text, !text.isEmpty { UIPasteboard.general.string = text; environment.haptics.soften() }49                }.keyboardShortcut("c", modifiers: [.command, .shift])50                Divider()51                Button("Actualiser") { Task { await environment.synchronize() } }.keyboardShortcut("r", modifiers: .command)52            }53            CommandGroup(replacing: .appSettings) {54                Button("Réglages…") { environment.router.sheet = .settings }.keyboardShortcut(",", modifiers: .command)55            }56        }57    }58}5960/// Launch-time configuration, including the debug-only hooks used by the automated tests.61@MainActor62enum AppLaunch {63    static var isRunningUnitTests: Bool {64        ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil && ProcessInfo.processInfo.environment["SPCHAT_UI_TESTS"] == nil65    }6667    static func makeEnvironment() -> AppEnvironment {68        #if DEBUG69        let p = ProcessInfo.processInfo70        if p.environment["SPCHAT_UI_TESTS"] != nil {71            // UI tests: isolated defaults, in-memory token store and a throw-away cache directory — never the real account data72            let suite = "ai.spboucher.spchat.uitests"73            UserDefaults().removePersistentDomain(forName: suite)74            let dir = FileManager.default.temporaryDirectory.appendingPathComponent("spchat-uitests-\(UUID().uuidString)", isDirectory: true)75            let env = AppEnvironment(configuration: .current(), tokens: MemorySessionTokenStore(p.environment["SPCHAT_DEBUG_SESSION"]), storeDirectory: dir, defaults: UserDefaults(suiteName: suite) ?? .standard)76            if let model = p.environment["SPCHAT_UI_MODEL"] { env.preferences.modelId = model }77            if let theme = p.environment["SPCHAT_UI_THEME"].flatMap(AppTheme.init(rawValue:)) { env.preferences.theme = theme }78            return env79        }80        #endif81        return AppEnvironment(configuration: .current())82    }8384    static func start(_ env: AppEnvironment) async {85        guard !isRunningUnitTests else { return }86        await env.start()87        #if DEBUG88        // UI tests and Scripts/sim.sh: open a screen directly (no system « Ouvrir dans SP Chat ? » prompt)89        if ProcessInfo.processInfo.environment["SPCHAT_UI_TESTS"] != nil, let link = ProcessInfo.processInfo.environment["SPCHAT_UI_LINK"].flatMap(URL.init(string:)) { env.handle(url: link) }90        #endif91    }92}93
Files main
Menu

Theme