From 74c726e2d56710897ec5733c3b98d1a746cb3656 Mon Sep 17 00:00:00 2001 From: Mark Gregory Date: Mon, 16 Nov 2026 16:57:00 +0000 Subject: [PATCH] Add editable themes and make web diagnostics visible --- Sources/AIStudioApp.swift | 171 ++++++++++++++++++++++++++++++++++---- Sources/Library.swift | 15 ++-- Tests/StudioTests.swift | 10 +++ 3 files changed, 175 insertions(+), 21 deletions(-) diff --git a/Sources/AIStudioApp.swift b/Sources/AIStudioApp.swift index 381cc39..fa7848c 100644 --- a/Sources/AIStudioApp.swift +++ b/Sources/AIStudioApp.swift @@ -6,6 +6,8 @@ import UniformTypeIdentifiers @main struct AIStudioApp: App { @StateObject private var studio = StudioModel() + @AppStorage("studioCustomThemes") private var customThemesData = Data() + @AppStorage("studioCustomThemeID") private var customThemeID = "" @AppStorage("studioTheme") private var theme = StudioTheme.system @AppStorage("studioAccent") private var accent = StudioAccent.storm @AppStorage("studioChatSize") private var chatSize = 14 @@ -22,7 +24,7 @@ struct AIStudioApp: App { } .frame(minWidth: 740, minHeight: 500) .preferredColorScheme(theme.colorScheme) - .tint(accent.color) + .tint(StudioCustomTheme.selected(customThemeID, in: customThemesData)?.accentColor ?? accent.color) .task { await Task.yield() studio.open() @@ -41,7 +43,7 @@ struct AIStudioApp: App { Settings { StudioSettingsView(studio: studio) .preferredColorScheme(theme.colorScheme) - .tint(accent.color) + .tint(StudioCustomTheme.selected(customThemeID, in: customThemesData)?.accentColor ?? accent.color) } } @@ -74,6 +76,8 @@ struct ChatView: View { @AppStorage("composerSpellCheck") private var spellCheck = true @AppStorage("composerAutoCorrect") private var autoCorrect = false @AppStorage("studioChatSize") private var chatSize = 14 + @AppStorage("studioCustomThemes") private var customThemesData = Data() + @AppStorage("studioCustomThemeID") private var customThemeID = "" @AppStorage("studioPalette") private var palette = StudioPalette.system @Environment(\.colorScheme) private var colorScheme @AppStorage("studioChatFont") private var chatFont = StudioFont.system @@ -130,7 +134,7 @@ struct ChatView: View { Divider() composer } - .background(palette.surface(dark: colorScheme == .dark)) + .background(StudioCustomTheme.selected(customThemeID, in: customThemesData)?.surface(dark: colorScheme == .dark) ?? palette.surface(dark: colorScheme == .dark)) .navigationTitle("AI Studio") .toolbar { Button("Documents", systemImage: "doc.on.doc") { showingLibrary = true } @@ -237,9 +241,12 @@ struct ChatView: View { Text(activity.joined(separator: " ยท ")) .font(.caption).foregroundStyle(.secondary).lineLimit(2).textSelection(.enabled) } - if showDebugOutput, !studio.webDiagnostics.isEmpty { + if showDebugOutput { + let diagnostics = studio.webDiagnostics.isEmpty + ? [studio.webStatus.isEmpty ? "No web lookup for this message. Paste a URL or ask to search the web." : studio.webStatus] + : studio.webDiagnostics ScrollView { - Text(studio.webDiagnostics.joined(separator: "\n")) + Text(diagnostics.joined(separator: "\n")) .font(.system(.caption, design: .monospaced)) .textSelection(.enabled) .frame(maxWidth: .infinity, alignment: .leading) @@ -335,6 +342,8 @@ struct ChatView: View { struct MessageView: View { let message: Message + @AppStorage("studioCustomThemes") private var customThemesData = Data() + @AppStorage("studioCustomThemeID") private var customThemeID = "" @AppStorage("studioPalette") private var palette = StudioPalette.system @Environment(\.colorScheme) private var colorScheme var streaming = false @@ -373,7 +382,7 @@ struct MessageView: View { } .frame(maxWidth: .infinity, alignment: .leading) .padding(14) - .background(palette.message(dark: colorScheme == .dark, user: message.role == .user), + .background(StudioCustomTheme.selected(customThemeID, in: customThemesData)?.message(dark: colorScheme == .dark, user: message.role == .user) ?? palette.message(dark: colorScheme == .dark, user: message.role == .user), in: RoundedRectangle(cornerRadius: 12)) } } @@ -1610,6 +1619,8 @@ struct StudioSettingsView: View { struct StudioAppearanceView: View { @AppStorage("studioTheme") private var theme = StudioTheme.system @AppStorage("studioAccent") private var accent = StudioAccent.storm + @AppStorage("studioCustomThemes") private var customThemesData = Data() + @AppStorage("studioCustomThemeID") private var customThemeID = "" @AppStorage("studioPalette") private var palette = StudioPalette.system @AppStorage("studioMode") private var mode = StudioMode.normal @AppStorage("studioChatFont") private var font = StudioFont.system @@ -1618,6 +1629,12 @@ struct StudioAppearanceView: View { @AppStorage("composerSpellCheck") private var spell = true @AppStorage("composerAutoCorrect") private var correct = false @State private var query = "" + @State private var draft: StudioCustomTheme? + + private var customThemes: [StudioCustomTheme] { + StudioCustomTheme.decode(customThemesData) + } + var body: some View { Form { @@ -1628,14 +1645,6 @@ struct StudioAppearanceView: View { Picker("Workspace mode", selection: $mode) { ForEach(StudioMode.allCases) { Text($0.rawValue).tag($0) } }.pickerStyle(.segmented) - Picker("Theme palette", selection: $palette) { - ForEach(StudioPalette.allCases) { entry in Text(entry.rawValue).tag(entry) } - } - Picker("Accent", selection: $accent) { - ForEach(StudioAccent.allCases) { entry in - Label(entry.rawValue, systemImage: "circle.fill").foregroundStyle(entry.color).tag(entry) - } - } Picker("Chat font", selection: $font) { ForEach(StudioFont.allCases) { Text($0.rawValue).tag($0) } } @@ -1644,6 +1653,37 @@ struct StudioAppearanceView: View { ForEach(StudioTheme.allCases) { Text($0.rawValue).tag($0) } } } + Section("Theme Library") { + ForEach(StudioPalette.allCases) { entry in + Button { + palette = entry + customThemeID = "" + } label: { + themeRow(entry.rawValue, light: entry.surface(dark: false), dark: entry.surface(dark: true), + selected: customThemeID.isEmpty && palette == entry) + } + .buttonStyle(.plain) + } + ForEach(customThemes) { entry in + HStack { + Button { + customThemeID = entry.id + } label: { + themeRow(entry.name, light: entry.surface(dark: false), dark: entry.surface(dark: true), + selected: customThemeID == entry.id) + } + .buttonStyle(.plain) + Button("Edit") { draft = entry } + } + } + Button("New Theme", systemImage: "plus") { draft = StudioCustomTheme() } + Picker("Built-in accent", selection: $accent) { + ForEach(StudioAccent.allCases) { entry in + Label(entry.rawValue, systemImage: "circle.fill").foregroundStyle(entry.color).tag(entry) + } + } + .disabled(!customThemeID.isEmpty) + } Section("Composer") { Toggle("Check spelling as you type", isOn: $spell) Toggle("Correct spelling automatically", isOn: $correct) @@ -1656,6 +1696,33 @@ struct StudioAppearanceView: View { } } .formStyle(.grouped) + .sheet(item: $draft) { entry in + StudioThemeEditor(theme: entry, canDelete: customThemes.contains { $0.id == entry.id }) { edited in + var entries = customThemes.filter { $0.id != entry.id } + if let edited { + let index = customThemes.firstIndex { $0.id == entry.id } ?? entries.endIndex + entries.insert(edited, at: index) + customThemeID = edited.id + } else if customThemeID == entry.id { + customThemeID = "" + } + if let data = try? JSONEncoder().encode(entries) { customThemesData = data } + draft = nil + } + } + } + + private func themeRow(_ name: String, light: Color, dark: Color, selected: Bool) -> some View { + HStack { + RoundedRectangle(cornerRadius: 4).fill(light).frame(width: 18, height: 18) + .overlay(RoundedRectangle(cornerRadius: 4).strokeBorder(.secondary.opacity(0.4))) + RoundedRectangle(cornerRadius: 4).fill(dark).frame(width: 18, height: 18) + .overlay(RoundedRectangle(cornerRadius: 4).strokeBorder(.secondary.opacity(0.4))) + Text(name) + Spacer() + if selected { Image(systemName: "checkmark").accessibilityLabel("Selected") } + } + .contentShape(Rectangle()) } private var shortcuts: [(String, String)] { @@ -1869,3 +1936,79 @@ enum StudioPalette: String, CaseIterable, Identifiable { brightness: dark ? (user ? 0.24 : 0.19) : (user ? 0.93 : 0.96)) } } + +private struct StudioCustomTheme: Codable, Identifiable { + var id = UUID().uuidString + var name = "" + var light = "F5F6FA" + var dark = "171B26" + var accent = "597CE0" + + static func decode(_ data: Data) -> [Self] { + (try? JSONDecoder().decode([Self].self, from: data)) ?? [] + } + + static func selected(_ id: String, in data: Data) -> Self? { + guard !id.isEmpty else { return nil } + return decode(data).first { $0.id == id } + } + + static func color(_ text: String) -> Color? { + let hex = text.trimmingCharacters(in: .whitespacesAndNewlines) + let digits = hex.hasPrefix("#") ? String(hex.dropFirst()) : hex + guard digits.count == 6, digits.allSatisfy({ $0.isASCII && $0.isHexDigit }), + let rgb = Int(digits, radix: 16) else { return nil } + return Color(red: Double((rgb >> 16) & 255) / 255, + green: Double((rgb >> 8) & 255) / 255, + blue: Double(rgb & 255) / 255) + } + + var accentColor: Color { Self.color(accent) ?? .accentColor } + + func surface(dark isDark: Bool) -> Color { + Self.color(isDark ? dark : light) ?? Color(nsColor: .windowBackgroundColor) + } + + func message(dark: Bool, user: Bool) -> Color { + user ? accentColor.opacity(0.16) : (dark ? Color.white : Color.black).opacity(0.06) + } +} + +private struct StudioThemeEditor: View { + @State var theme: StudioCustomTheme + let canDelete: Bool + let save: (StudioCustomTheme?) -> Void + @Environment(\.dismiss) private var dismiss + + var body: some View { + Form { + TextField("Theme name", text: $theme.name) + colorField("Light background", text: $theme.light) + colorField("Dark background", text: $theme.dark) + colorField("Accent", text: $theme.accent) + Text("Enter six-digit hex colors, such as #597CE0.").foregroundStyle(.secondary) + HStack { + if canDelete { Button("Delete Theme", role: .destructive) { save(nil) } } + Spacer() + Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction) + Button("Save Theme") { + theme.name = theme.name.trimmingCharacters(in: .whitespacesAndNewlines) + save(theme) + } + .keyboardShortcut(.defaultAction) + .disabled(theme.name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || + [theme.light, theme.dark, theme.accent].contains { StudioCustomTheme.color($0) == nil }) + } + } + .padding(24) + .frame(width: 460) + } + + private func colorField(_ label: String, text: Binding) -> some View { + LabeledContent(label) { + RoundedRectangle(cornerRadius: 4).fill(StudioCustomTheme.color(text.wrappedValue) ?? .clear) + .frame(width: 20, height: 20) + TextField(label, text: text).labelsHidden().frame(width: 115) + } + } +} diff --git a/Sources/Library.swift b/Sources/Library.swift index bdc61b4..d5ed201 100644 --- a/Sources/Library.swift +++ b/Sources/Library.swift @@ -279,7 +279,8 @@ enum ChatWebLookup { let text = message.trimmingCharacters(in: .whitespacesAndNewlines) let prefixes = ["/search ", "can you search the web for ", "please search the web for ", "search the web for ", "search online for ", "look up online ", "web search for ", - "find documentation for ", "find online ", "browse the web for "] + "find documentation for ", "find online ", "browse the web for ", + "can you look up ", "please look up ", "look up ", "search for "] if let prefix = prefixes.first(where: { text.lowercased().hasPrefix($0) }) { let query = String(text.dropFirst(prefix.count)).trimmingCharacters(in: .whitespacesAndNewlines) if !query.isEmpty { self = .search(query); return } @@ -319,15 +320,15 @@ enum ChatWebLookup { failures.append(error.localizedDescription) choices = [] } - if choices.isEmpty { - if failures.isEmpty { failures.append("No usable search results.") } - var browser = URLComponents(string: "https://duckduckgo.com/") - browser?.queryItems = [.init(name: "q", value: query)] - browserFallback = browser?.url - } + if choices.isEmpty && failures.isEmpty { failures.append("No usable search results.") } } let primary = try await Self.fetch(choices) failures += primary.failures + if primary.pages.isEmpty, case .search(let query) = self { + var browser = URLComponents(string: "https://duckduckgo.com/") + browser?.queryItems = [.init(name: "q", value: query)] + browserFallback = browser?.url + } var seen = Set(choices + primary.pages.map(\.url)) let terms = Set(LibraryImport.matchQuery(topic).components(separatedBy: " OR ") .map { $0.trimmingCharacters(in: CharacterSet(charactersIn: "\"")) }.filter { !$0.isEmpty }) diff --git a/Tests/StudioTests.swift b/Tests/StudioTests.swift index 3e9e50e..7157812 100644 --- a/Tests/StudioTests.swift +++ b/Tests/StudioTests.swift @@ -636,6 +636,16 @@ final class ToolboxRepairTests: XCTestCase { XCTAssertTrue(followed.reference.contains("/follow-detail")) } + func testLiveDefaultSearchRetrievesPages() async throws { + guard let query = ProcessInfo.processInfo.environment["AISTUDIO_TEST_REAL_WEB_QUERY"] else { + throw XCTSkip("Set AISTUDIO_TEST_REAL_WEB_QUERY to check live search and retrieval.") + } + let lookup = try XCTUnwrap(ChatWebLookup(message: "Can you look up " + query)) + let result = try await lookup.resolve(searchEndpoint: "") + XCTAssertTrue(result.reference.contains("Source: https://"), result.status) + XCTAssertTrue(result.diagnostics.contains { $0.hasPrefix("Fetched ") }, result.status) + } + func testLiveLaravelRetrieval() async throws { guard let address = ProcessInfo.processInfo.environment["AISTUDIO_TEST_REAL_WEB_PAGE"] else { throw XCTSkip("Set AISTUDIO_TEST_REAL_WEB_PAGE to check a real page.")