Repair Toolbox navigation, chat web lookup, and model discovery

This commit is contained in:
Mark Gregory
2026-11-16 15:46:35 +00:00
parent 28aa677372
commit 06e3291edb
11 changed files with 477 additions and 67 deletions
+54 -9
View File
@@ -82,6 +82,8 @@ struct ModelCatalogView: View {
@State private var active: CatalogModel?
@AppStorage("mlxRegisteredPaths") private var registered = Data()
@State private var pickingLibrary = false
@State private var scanningLibrary = false
@State private var scanRequested = false
@State private var libraryStatus = ""
private var library: [String] { (try? JSONDecoder().decode([String].self, from: registered)) ?? [] }
@@ -91,8 +93,16 @@ struct ModelCatalogView: View {
var body: some View {
Form {
Section("Model Library") {
Button("Register Model Folder…") { pickingLibrary = true }
.disabled(studio.running)
HStack {
Button("Register Model Folder…") { scanRequested = false; pickingLibrary = true }
Button("Scan Folder for Models…") { scanRequested = true; pickingLibrary = true }
if scanningLibrary { ProgressView().controlSize(.small) }
}
.disabled(studio.running || scanningLibrary)
if library.isEmpty {
Text("Register an existing MLX folder, scan a model library, or download a catalog model below.")
.foregroundStyle(.secondary)
}
ForEach(library, id: \.self) { path in
HStack {
Text(URL(fileURLWithPath: path).lastPathComponent).help(path)
@@ -155,14 +165,24 @@ struct ModelCatalogView: View {
.formStyle(.grouped)
.fileImporter(isPresented: $pickingLibrary, allowedContentTypes: [.folder]) { result in
do {
let url = try result.get().resolvingSymlinksInPath()
guard MLXLibrary.isModel(at: url) else {
throw StudioError("Choose an MLX folder with config.json and safetensors weights.")
let url = try result.get()
if scanRequested {
scanningLibrary = true
Task {
defer { scanningLibrary = false }
do {
let found = try await Task.detached { try MLXLibrary.discover(in: url) }.value
registered = try JSONEncoder().encode(Array(Set(library + found.map(\.path))).sorted())
libraryStatus = "Found \(found.count) compatible MLX model folder(s)."
} catch { libraryStatus = error.localizedDescription }
}
} else {
guard MLXLibrary.isModel(at: url) else {
throw StudioError("Choose an MLX folder with config.json and safetensors weights.")
}
registered = try JSONEncoder().encode(Array(Set(library + [url.resolvingSymlinksInPath().path])).sorted())
libraryStatus = "Registered \(url.lastPathComponent)."
}
if !library.contains(url.path) {
registered = try JSONEncoder().encode(library + [url.path])
}
libraryStatus = "Registered \(url.lastPathComponent)."
} catch { libraryStatus = error.localizedDescription }
}
.confirmationDialog("Remove downloaded model?", isPresented: Binding(
@@ -202,6 +222,31 @@ struct ModelCatalogView: View {
}
enum MLXLibrary {
static func discover(in root: URL) throws -> [URL] {
let scoped = root.startAccessingSecurityScopedResource()
defer { if scoped { root.stopAccessingSecurityScopedResource() } }
let manager = FileManager.default
var queue: [(URL, Int)] = [(root.resolvingSymlinksInPath(), 0)]
var found: [URL] = []
var offset = 0
while offset < queue.count && offset < 2000 && found.count < 100 {
try Task.checkCancellation()
let (folder, depth) = queue[offset]
offset += 1
if isModel(at: folder) { found.append(folder); continue }
guard depth < 4 else { continue }
let children = try manager.contentsOfDirectory(at: folder, includingPropertiesForKeys: [.isDirectoryKey, .isSymbolicLinkKey],
options: [.skipsHiddenFiles, .skipsPackageDescendants])
for child in children where queue.count < 2000 {
if let values = try? child.resourceValues(forKeys: [.isDirectoryKey, .isSymbolicLinkKey]),
values.isDirectory == true, values.isSymbolicLink != true {
queue.append((child, depth + 1))
}
}
}
return found.sorted { $0.path < $1.path }
}
static func isModel(at url: URL) -> Bool {
FileManager.default.fileExists(atPath: url.appending(path: "config.json").path) &&
((try? MLXRuntimeLimits.weightBytes(in: url)) ?? 0) > 0