Keep installed models, chat selection, and web enablement in sync
This commit is contained in:
@@ -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
|
.onChange(of: studio.selected) { old, _ in
|
||||||
useDocuments = false
|
useDocuments = false
|
||||||
if old != nil {
|
if old != nil {
|
||||||
|
|||||||
+5
-5
@@ -223,6 +223,7 @@ struct MCPSettings: View {
|
|||||||
@AppStorage("mcpTransport") private var transport = "http"
|
@AppStorage("mcpTransport") private var transport = "http"
|
||||||
@AppStorage("mcpExecutable") private var executable = ""
|
@AppStorage("mcpExecutable") private var executable = ""
|
||||||
@AppStorage("mcpArguments") private var launchArguments = ""
|
@AppStorage("mcpArguments") private var launchArguments = ""
|
||||||
|
@AppStorage("builtInWebEnabled") private var builtInWebEnabled = true
|
||||||
@AppStorage("webSearchEndpoint") private var webSearchEndpoint = ""
|
@AppStorage("webSearchEndpoint") private var webSearchEndpoint = ""
|
||||||
@AppStorage("mcpConfigPath") private var configPath = ""
|
@AppStorage("mcpConfigPath") private var configPath = ""
|
||||||
@State private var token = ""
|
@State private var token = ""
|
||||||
@@ -243,11 +244,10 @@ struct MCPSettings: View {
|
|||||||
HSplitView {
|
HSplitView {
|
||||||
Form {
|
Form {
|
||||||
Section("Built-In Tools") {
|
Section("Built-In Tools") {
|
||||||
LabeledContent {
|
Toggle("Web Search and Page Retrieval", isOn: $builtInWebEnabled)
|
||||||
Text("Runs on demand").foregroundStyle(.secondary)
|
.onChange(of: builtInWebEnabled) { _, _ in studio.restoreToolsets() }
|
||||||
} label: {
|
Text(builtInWebEnabled ? "Ready for web requests in chat." : "Automatic web lookup is off.")
|
||||||
Label(BuiltInWebTools.name, systemImage: "globe")
|
.font(.caption).foregroundStyle(.secondary)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Section("Custom MCP Servers") {
|
Section("Custom MCP Servers") {
|
||||||
Menu("Manage Servers", systemImage: "ellipsis.circle") {
|
Menu("Manage Servers", systemImage: "ellipsis.circle") {
|
||||||
|
|||||||
+15
-15
@@ -156,7 +156,8 @@ struct ModelCatalogView: View {
|
|||||||
|
|
||||||
|
|
||||||
private var installedModels: [CatalogModel] {
|
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 {
|
private var mlxLibrary: some View {
|
||||||
Form {
|
Form {
|
||||||
|
if !installedModels.isEmpty {
|
||||||
|
Section("Installed MLX Models") {
|
||||||
|
ForEach(installedModels.filter { $0.installed(at: $0.localFolder) }) { model in modelRow(model) }
|
||||||
|
}
|
||||||
|
}
|
||||||
Section("Local MLX Models") {
|
Section("Local MLX Models") {
|
||||||
HStack {
|
HStack {
|
||||||
Button("Register Model Folder…") { scanRequested = false; pickingLibrary = true }
|
Button("Register Model Folder…") { scanRequested = false; pickingLibrary = true }
|
||||||
@@ -207,11 +213,6 @@ struct ModelCatalogView: View {
|
|||||||
}
|
}
|
||||||
if !libraryStatus.isEmpty { Text(libraryStatus).foregroundStyle(.secondary) }
|
if !libraryStatus.isEmpty { Text(libraryStatus).foregroundStyle(.secondary) }
|
||||||
}
|
}
|
||||||
if !installedModels.isEmpty {
|
|
||||||
Section("Downloaded Models") {
|
|
||||||
ForEach(installedModels) { model in modelRow(model) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Section("Hugging Face") {
|
Section("Hugging Face") {
|
||||||
HStack {
|
HStack {
|
||||||
TextField("Search MLX models", text: $query, prompt: Text("e.g. Qwen, Gemma"))
|
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 searching { ProgressView().controlSize(.small) }
|
||||||
}
|
}
|
||||||
if !searchStatus.isEmpty { Text(searchStatus).foregroundStyle(.secondary) }
|
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") {
|
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 {
|
if !status.isEmpty {
|
||||||
Section {
|
Section {
|
||||||
@@ -285,9 +288,7 @@ struct ModelCatalogView: View {
|
|||||||
}
|
}
|
||||||
let active = ollamaModel == model.name && !mlxOnly && !compatibleEnabled
|
let active = ollamaModel == model.name && !mlxOnly && !compatibleEnabled
|
||||||
Button(active ? "Active" : "Use Model") {
|
Button(active ? "Active" : "Use Model") {
|
||||||
ollamaModel = model.name
|
studio.selectOllamaModel(model.name)
|
||||||
compatibleEnabled = false
|
|
||||||
mlxOnly = false
|
|
||||||
}
|
}
|
||||||
.disabled(active || studio.running)
|
.disabled(active || studio.running)
|
||||||
}
|
}
|
||||||
@@ -405,8 +406,7 @@ struct ModelCatalogView: View {
|
|||||||
defer { loadingPath = nil }
|
defer { loadingPath = nil }
|
||||||
do {
|
do {
|
||||||
try await studio.loadMLX(path)
|
try await studio.loadMLX(path)
|
||||||
mlxPath = path
|
studio.selectMLXModel(path)
|
||||||
mlxOnly = true
|
|
||||||
status = "Loaded \(name)."
|
status = "Loaded \(name)."
|
||||||
} catch { status = error.localizedDescription }
|
} catch { status = error.localizedDescription }
|
||||||
}
|
}
|
||||||
@@ -424,7 +424,7 @@ struct ModelCatalogView: View {
|
|||||||
await MainActor.run { status = message; progress = fraction }
|
await MainActor.run { status = message; progress = fraction }
|
||||||
}
|
}
|
||||||
if !CatalogModel.all.contains(where: { $0.id == model.id }) {
|
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)."
|
status = "Downloaded \(model.title)."
|
||||||
} catch {
|
} catch {
|
||||||
@@ -441,7 +441,7 @@ struct ModelCatalogView: View {
|
|||||||
mlxPath = ""
|
mlxPath = ""
|
||||||
studio.unloadMLX()
|
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)."
|
status = "Removed \(model.title)."
|
||||||
} catch { status = error.localizedDescription }
|
} catch { status = error.localizedDescription }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ final class StudioModel: ObservableObject {
|
|||||||
@Published private(set) var modelStatus = ""
|
@Published private(set) var modelStatus = ""
|
||||||
@Published private(set) var managing = false
|
@Published private(set) var managing = false
|
||||||
@Published private(set) var running = false
|
@Published private(set) var running = false
|
||||||
|
@Published private(set) var modelSelectionRevision = 0
|
||||||
@Published private(set) var webStatus = ""
|
@Published private(set) var webStatus = ""
|
||||||
@Published private(set) var webDiagnostics: [String] = []
|
@Published private(set) var webDiagnostics: [String] = []
|
||||||
@Published private(set) var webSearchLink: URL?
|
@Published private(set) var webSearchLink: URL?
|
||||||
@@ -181,7 +182,7 @@ final class StudioModel: ObservableObject {
|
|||||||
\(toolAttachment.output)
|
\(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 wantsRepositories = GiteaRepositoryLookup.matches(text)
|
||||||
let repositoryClient = wantsRepositories ?
|
let repositoryClient = wantsRepositories ?
|
||||||
activeToolsets.first(where: { entry in
|
activeToolsets.first(where: { entry in
|
||||||
@@ -467,6 +468,21 @@ final class StudioModel: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func cancelManagement() { management?.cancel() }
|
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 {
|
func loadMLX(_ path: String) async throws {
|
||||||
await configureMLXRuntime()
|
await configureMLXRuntime()
|
||||||
try await mlx.load(path)
|
try await mlx.load(path)
|
||||||
@@ -587,7 +603,11 @@ final class StudioModel: ObservableObject {
|
|||||||
extension StudioModel {
|
extension StudioModel {
|
||||||
func restoreToolsets(defaults: UserDefaults = .standard) {
|
func restoreToolsets(defaults: UserDefaults = .standard) {
|
||||||
let endpoint = defaults.string(forKey: "webSearchEndpoint") ?? ""
|
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 }
|
guard let path = defaults.string(forKey: "mcpConfigPath"), !path.isEmpty else { return }
|
||||||
let enabled = Set(enabledMCPProfiles(path: path, defaults: defaults))
|
let enabled = Set(enabledMCPProfiles(path: path, defaults: defaults))
|
||||||
for name in enabled {
|
for name in enabled {
|
||||||
|
|||||||
@@ -775,6 +775,9 @@ final class MultipleToolsetsTests: XCTestCase {
|
|||||||
afterDisable.restoreToolsets(defaults: defaults)
|
afterDisable.restoreToolsets(defaults: defaults)
|
||||||
try await waitUntil { afterDisable.activeToolsets.map(\.id) == [.web] }
|
try await waitUntil { afterDisable.activeToolsets.map(\.id) == [.web] }
|
||||||
XCTAssertNil(afterDisable.mcpConnections[.profile("enabled")])
|
XCTAssertNil(afterDisable.mcpConnections[.profile("enabled")])
|
||||||
|
defaults.set(false, forKey: "builtInWebEnabled")
|
||||||
|
afterDisable.restoreToolsets(defaults: defaults)
|
||||||
|
XCTAssertNil(afterDisable.mcpConnections[.web])
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
|
|||||||
Reference in New Issue
Block a user