Add editable themes and make web diagnostics visible

This commit is contained in:
Mark Gregory
2026-11-16 16:57:00 +00:00
parent de18d95741
commit 74c726e2d5
3 changed files with 175 additions and 21 deletions
+157 -14
View File
@@ -6,6 +6,8 @@ import UniformTypeIdentifiers
@main @main
struct AIStudioApp: App { struct AIStudioApp: App {
@StateObject private var studio = StudioModel() @StateObject private var studio = StudioModel()
@AppStorage("studioCustomThemes") private var customThemesData = Data()
@AppStorage("studioCustomThemeID") private var customThemeID = ""
@AppStorage("studioTheme") private var theme = StudioTheme.system @AppStorage("studioTheme") private var theme = StudioTheme.system
@AppStorage("studioAccent") private var accent = StudioAccent.storm @AppStorage("studioAccent") private var accent = StudioAccent.storm
@AppStorage("studioChatSize") private var chatSize = 14 @AppStorage("studioChatSize") private var chatSize = 14
@@ -22,7 +24,7 @@ struct AIStudioApp: App {
} }
.frame(minWidth: 740, minHeight: 500) .frame(minWidth: 740, minHeight: 500)
.preferredColorScheme(theme.colorScheme) .preferredColorScheme(theme.colorScheme)
.tint(accent.color) .tint(StudioCustomTheme.selected(customThemeID, in: customThemesData)?.accentColor ?? accent.color)
.task { .task {
await Task.yield() await Task.yield()
studio.open() studio.open()
@@ -41,7 +43,7 @@ struct AIStudioApp: App {
Settings { Settings {
StudioSettingsView(studio: studio) StudioSettingsView(studio: studio)
.preferredColorScheme(theme.colorScheme) .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("composerSpellCheck") private var spellCheck = true
@AppStorage("composerAutoCorrect") private var autoCorrect = false @AppStorage("composerAutoCorrect") private var autoCorrect = false
@AppStorage("studioChatSize") private var chatSize = 14 @AppStorage("studioChatSize") private var chatSize = 14
@AppStorage("studioCustomThemes") private var customThemesData = Data()
@AppStorage("studioCustomThemeID") private var customThemeID = ""
@AppStorage("studioPalette") private var palette = StudioPalette.system @AppStorage("studioPalette") private var palette = StudioPalette.system
@Environment(\.colorScheme) private var colorScheme @Environment(\.colorScheme) private var colorScheme
@AppStorage("studioChatFont") private var chatFont = StudioFont.system @AppStorage("studioChatFont") private var chatFont = StudioFont.system
@@ -130,7 +134,7 @@ struct ChatView: View {
Divider() Divider()
composer 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") .navigationTitle("AI Studio")
.toolbar { .toolbar {
Button("Documents", systemImage: "doc.on.doc") { showingLibrary = true } Button("Documents", systemImage: "doc.on.doc") { showingLibrary = true }
@@ -237,9 +241,12 @@ struct ChatView: View {
Text(activity.joined(separator: " · ")) Text(activity.joined(separator: " · "))
.font(.caption).foregroundStyle(.secondary).lineLimit(2).textSelection(.enabled) .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 { ScrollView {
Text(studio.webDiagnostics.joined(separator: "\n")) Text(diagnostics.joined(separator: "\n"))
.font(.system(.caption, design: .monospaced)) .font(.system(.caption, design: .monospaced))
.textSelection(.enabled) .textSelection(.enabled)
.frame(maxWidth: .infinity, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading)
@@ -335,6 +342,8 @@ struct ChatView: View {
struct MessageView: View { struct MessageView: View {
let message: Message let message: Message
@AppStorage("studioCustomThemes") private var customThemesData = Data()
@AppStorage("studioCustomThemeID") private var customThemeID = ""
@AppStorage("studioPalette") private var palette = StudioPalette.system @AppStorage("studioPalette") private var palette = StudioPalette.system
@Environment(\.colorScheme) private var colorScheme @Environment(\.colorScheme) private var colorScheme
var streaming = false var streaming = false
@@ -373,7 +382,7 @@ struct MessageView: View {
} }
.frame(maxWidth: .infinity, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading)
.padding(14) .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)) in: RoundedRectangle(cornerRadius: 12))
} }
} }
@@ -1610,6 +1619,8 @@ struct StudioSettingsView: View {
struct StudioAppearanceView: View { struct StudioAppearanceView: View {
@AppStorage("studioTheme") private var theme = StudioTheme.system @AppStorage("studioTheme") private var theme = StudioTheme.system
@AppStorage("studioAccent") private var accent = StudioAccent.storm @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("studioPalette") private var palette = StudioPalette.system
@AppStorage("studioMode") private var mode = StudioMode.normal @AppStorage("studioMode") private var mode = StudioMode.normal
@AppStorage("studioChatFont") private var font = StudioFont.system @AppStorage("studioChatFont") private var font = StudioFont.system
@@ -1618,6 +1629,12 @@ struct StudioAppearanceView: View {
@AppStorage("composerSpellCheck") private var spell = true @AppStorage("composerSpellCheck") private var spell = true
@AppStorage("composerAutoCorrect") private var correct = false @AppStorage("composerAutoCorrect") private var correct = false
@State private var query = "" @State private var query = ""
@State private var draft: StudioCustomTheme?
private var customThemes: [StudioCustomTheme] {
StudioCustomTheme.decode(customThemesData)
}
var body: some View { var body: some View {
Form { Form {
@@ -1628,14 +1645,6 @@ struct StudioAppearanceView: View {
Picker("Workspace mode", selection: $mode) { Picker("Workspace mode", selection: $mode) {
ForEach(StudioMode.allCases) { Text($0.rawValue).tag($0) } ForEach(StudioMode.allCases) { Text($0.rawValue).tag($0) }
}.pickerStyle(.segmented) }.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) { Picker("Chat font", selection: $font) {
ForEach(StudioFont.allCases) { Text($0.rawValue).tag($0) } ForEach(StudioFont.allCases) { Text($0.rawValue).tag($0) }
} }
@@ -1644,6 +1653,37 @@ struct StudioAppearanceView: View {
ForEach(StudioTheme.allCases) { Text($0.rawValue).tag($0) } 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") { Section("Composer") {
Toggle("Check spelling as you type", isOn: $spell) Toggle("Check spelling as you type", isOn: $spell)
Toggle("Correct spelling automatically", isOn: $correct) Toggle("Correct spelling automatically", isOn: $correct)
@@ -1656,6 +1696,33 @@ struct StudioAppearanceView: View {
} }
} }
.formStyle(.grouped) .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)] { 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)) 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<String>) -> 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)
}
}
}
+8 -7
View File
@@ -279,7 +279,8 @@ enum ChatWebLookup {
let text = message.trimmingCharacters(in: .whitespacesAndNewlines) let text = message.trimmingCharacters(in: .whitespacesAndNewlines)
let prefixes = ["/search ", "can you search the web for ", "please search the web for ", 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 ", "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) }) { if let prefix = prefixes.first(where: { text.lowercased().hasPrefix($0) }) {
let query = String(text.dropFirst(prefix.count)).trimmingCharacters(in: .whitespacesAndNewlines) let query = String(text.dropFirst(prefix.count)).trimmingCharacters(in: .whitespacesAndNewlines)
if !query.isEmpty { self = .search(query); return } if !query.isEmpty { self = .search(query); return }
@@ -319,15 +320,15 @@ enum ChatWebLookup {
failures.append(error.localizedDescription) failures.append(error.localizedDescription)
choices = [] choices = []
} }
if choices.isEmpty { if choices.isEmpty && failures.isEmpty { failures.append("No usable search results.") }
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
}
} }
let primary = try await Self.fetch(choices) let primary = try await Self.fetch(choices)
failures += primary.failures 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)) var seen = Set(choices + primary.pages.map(\.url))
let terms = Set(LibraryImport.matchQuery(topic).components(separatedBy: " OR ") let terms = Set(LibraryImport.matchQuery(topic).components(separatedBy: " OR ")
.map { $0.trimmingCharacters(in: CharacterSet(charactersIn: "\"")) }.filter { !$0.isEmpty }) .map { $0.trimmingCharacters(in: CharacterSet(charactersIn: "\"")) }.filter { !$0.isEmpty })
+10
View File
@@ -636,6 +636,16 @@ final class ToolboxRepairTests: XCTestCase {
XCTAssertTrue(followed.reference.contains("/follow-detail")) 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 { func testLiveLaravelRetrieval() async throws {
guard let address = ProcessInfo.processInfo.environment["AISTUDIO_TEST_REAL_WEB_PAGE"] else { 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.") throw XCTSkip("Set AISTUDIO_TEST_REAL_WEB_PAGE to check a real page.")