From 4ad93b383bdd40aabe4bca8ac734e21724be28fc Mon Sep 17 00:00:00 2001 From: Mark Gregory Date: Tue, 17 Nov 2026 09:40:46 +0000 Subject: [PATCH] Keep installed models, chat selection, and web enablement in sync --- Sources/AIStudioApp.swift | 7 +++++++ Sources/MCP.swift | 10 +++++----- Sources/ModelDownload.swift | 30 +++++++++++++++--------------- Sources/StudioModel.swift | 24 ++++++++++++++++++++++-- Tests/StudioTests.swift | 3 +++ 5 files changed, 52 insertions(+), 22 deletions(-) diff --git a/Sources/AIStudioApp.swift b/Sources/AIStudioApp.swift index 3277a1f..0572ccb 100644 --- a/Sources/AIStudioApp.swift +++ b/Sources/AIStudioApp.swift @@ -191,6 +191,13 @@ struct ChatView: View { } } } + .onChange(of: studio.modelSelectionRevision) { + let defaults = UserDefaults.standard + mlxPath = defaults.string(forKey: "mlxPath") ?? "" + model = defaults.string(forKey: "ollamaModel") ?? "" + mlxOnly = defaults.bool(forKey: "mlxOnly") + compatibleEnabled = defaults.bool(forKey: "compatibleEnabled") + } .onChange(of: studio.selected) { old, _ in useDocuments = false if old != nil { diff --git a/Sources/MCP.swift b/Sources/MCP.swift index 970b60b..14ee1f3 100644 --- a/Sources/MCP.swift +++ b/Sources/MCP.swift @@ -223,6 +223,7 @@ struct MCPSettings: View { @AppStorage("mcpTransport") private var transport = "http" @AppStorage("mcpExecutable") private var executable = "" @AppStorage("mcpArguments") private var launchArguments = "" + @AppStorage("builtInWebEnabled") private var builtInWebEnabled = true @AppStorage("webSearchEndpoint") private var webSearchEndpoint = "" @AppStorage("mcpConfigPath") private var configPath = "" @State private var token = "" @@ -243,11 +244,10 @@ struct MCPSettings: View { HSplitView { Form { Section("Built-In Tools") { - LabeledContent { - Text("Runs on demand").foregroundStyle(.secondary) - } label: { - Label(BuiltInWebTools.name, systemImage: "globe") - } + Toggle("Web Search and Page Retrieval", isOn: $builtInWebEnabled) + .onChange(of: builtInWebEnabled) { _, _ in studio.restoreToolsets() } + Text(builtInWebEnabled ? "Ready for web requests in chat." : "Automatic web lookup is off.") + .font(.caption).foregroundStyle(.secondary) } Section("Custom MCP Servers") { Menu("Manage Servers", systemImage: "ellipsis.circle") { diff --git a/Sources/ModelDownload.swift b/Sources/ModelDownload.swift index bd9f502..929d3c3 100644 --- a/Sources/ModelDownload.swift +++ b/Sources/ModelDownload.swift @@ -156,7 +156,8 @@ struct ModelCatalogView: View { private var installedModels: [CatalogModel] { - (try? JSONDecoder().decode([CatalogModel].self, from: managed)) ?? [] + let saved = (try? JSONDecoder().decode([CatalogModel].self, from: managed)) ?? [] + return saved + CatalogModel.all.filter { $0.installed(at: $0.localFolder) } } @@ -179,6 +180,11 @@ struct ModelCatalogView: View { private var mlxLibrary: some View { Form { + if !installedModels.isEmpty { + Section("Installed MLX Models") { + ForEach(installedModels.filter { $0.installed(at: $0.localFolder) }) { model in modelRow(model) } + } + } Section("Local MLX Models") { HStack { Button("Register Model Folder…") { scanRequested = false; pickingLibrary = true } @@ -207,11 +213,6 @@ struct ModelCatalogView: View { } if !libraryStatus.isEmpty { Text(libraryStatus).foregroundStyle(.secondary) } } - if !installedModels.isEmpty { - Section("Downloaded Models") { - ForEach(installedModels) { model in modelRow(model) } - } - } Section("Hugging Face") { HStack { TextField("Search MLX models", text: $query, prompt: Text("e.g. Qwen, Gemma")) @@ -220,10 +221,12 @@ struct ModelCatalogView: View { if searching { ProgressView().controlSize(.small) } } if !searchStatus.isEmpty { Text(searchStatus).foregroundStyle(.secondary) } - ForEach(matches) { model in modelRow(model) } + ForEach(matches.filter { model in !installedModels.contains { $0.id == model.id && $0.installed(at: $0.localFolder) } }) { model in + modelRow(model) + } } Section("Recommended Models") { - ForEach(CatalogModel.all) { model in modelRow(model) } + ForEach(CatalogModel.all.filter { !$0.installed(at: $0.localFolder) }) { model in modelRow(model) } } if !status.isEmpty { Section { @@ -285,9 +288,7 @@ struct ModelCatalogView: View { } let active = ollamaModel == model.name && !mlxOnly && !compatibleEnabled Button(active ? "Active" : "Use Model") { - ollamaModel = model.name - compatibleEnabled = false - mlxOnly = false + studio.selectOllamaModel(model.name) } .disabled(active || studio.running) } @@ -405,8 +406,7 @@ struct ModelCatalogView: View { defer { loadingPath = nil } do { try await studio.loadMLX(path) - mlxPath = path - mlxOnly = true + studio.selectMLXModel(path) status = "Loaded \(name)." } catch { status = error.localizedDescription } } @@ -424,7 +424,7 @@ struct ModelCatalogView: View { await MainActor.run { status = message; progress = fraction } } if !CatalogModel.all.contains(where: { $0.id == model.id }) { - managed = try JSONEncoder().encode(installedModels.filter { $0.id != model.id } + [model]) + managed = try JSONEncoder().encode(installedModels.filter { $0.id != model.id && !CatalogModel.all.contains($0) } + [model]) } status = "Downloaded \(model.title)." } catch { @@ -441,7 +441,7 @@ struct ModelCatalogView: View { mlxPath = "" studio.unloadMLX() } - managed = try JSONEncoder().encode(installedModels.filter { $0.id != model.id }) + managed = try JSONEncoder().encode(installedModels.filter { $0.id != model.id && !CatalogModel.all.contains($0) }) status = "Removed \(model.title)." } catch { status = error.localizedDescription } } diff --git a/Sources/StudioModel.swift b/Sources/StudioModel.swift index 08bc19a..a4ad985 100644 --- a/Sources/StudioModel.swift +++ b/Sources/StudioModel.swift @@ -19,6 +19,7 @@ final class StudioModel: ObservableObject { @Published private(set) var modelStatus = "" @Published private(set) var managing = false @Published private(set) var running = false + @Published private(set) var modelSelectionRevision = 0 @Published private(set) var webStatus = "" @Published private(set) var webDiagnostics: [String] = [] @Published private(set) var webSearchLink: URL? @@ -181,7 +182,7 @@ final class StudioModel: ObservableObject { \(toolAttachment.output) """) } - let lookup = ChatWebLookup(message: text) + let lookup = UserDefaults.standard.object(forKey: "builtInWebEnabled") as? Bool == false ? nil : ChatWebLookup(message: text) let wantsRepositories = GiteaRepositoryLookup.matches(text) let repositoryClient = wantsRepositories ? activeToolsets.first(where: { entry in @@ -467,6 +468,21 @@ final class StudioModel: ObservableObject { } func cancelManagement() { management?.cancel() } + func selectMLXModel(_ path: String) { + let defaults = UserDefaults.standard + defaults.set(path, forKey: "mlxPath") + defaults.set(true, forKey: "mlxOnly") + modelSelectionRevision += 1 + } + + func selectOllamaModel(_ name: String) { + let defaults = UserDefaults.standard + defaults.set(name, forKey: "ollamaModel") + defaults.set(false, forKey: "compatibleEnabled") + defaults.set(false, forKey: "mlxOnly") + modelSelectionRevision += 1 + } + func loadMLX(_ path: String) async throws { await configureMLXRuntime() try await mlx.load(path) @@ -587,7 +603,11 @@ final class StudioModel: ObservableObject { extension StudioModel { func restoreToolsets(defaults: UserDefaults = .standard) { let endpoint = defaults.string(forKey: "webSearchEndpoint") ?? "" - connectMCP(.web, name: BuiltInWebTools.name) { BuiltInWebTools(searchEndpoint: endpoint) } + if defaults.object(forKey: "builtInWebEnabled") as? Bool != false { + connectMCP(.web, name: BuiltInWebTools.name) { BuiltInWebTools(searchEndpoint: endpoint) } + } else { + disconnectMCP(.web) + } guard let path = defaults.string(forKey: "mcpConfigPath"), !path.isEmpty else { return } let enabled = Set(enabledMCPProfiles(path: path, defaults: defaults)) for name in enabled { diff --git a/Tests/StudioTests.swift b/Tests/StudioTests.swift index 4d2d925..fca9fb4 100644 --- a/Tests/StudioTests.swift +++ b/Tests/StudioTests.swift @@ -775,6 +775,9 @@ final class MultipleToolsetsTests: XCTestCase { afterDisable.restoreToolsets(defaults: defaults) try await waitUntil { afterDisable.activeToolsets.map(\.id) == [.web] } XCTAssertNil(afterDisable.mcpConnections[.profile("enabled")]) + defaults.set(false, forKey: "builtInWebEnabled") + afterDisable.restoreToolsets(defaults: defaults) + XCTAssertNil(afterDisable.mcpConnections[.web]) } @MainActor