Build AI Studio 1.1 for macOS #1

Open
admin wants to merge 38 commits from admin/ai-studio-v1.1 AGit into main
Showing only changes of commit f16f699711 - Show all commits
+91 -34
View File
@@ -145,6 +145,9 @@ struct ModelCatalogView: View {
@State private var searchStatus = ""
@State private var progress: Double?
@State private var loadingPath: String?
@AppStorage("favoriteModelKeys") private var favoriteData = Data()
@State private var pullingModel: String?
@State private var backend = "MLX"
@AppStorage("endpoint") private var ollamaEndpoint = "http://127.0.0.1:11434"
@AppStorage("ollamaModel") private var ollamaModel = ""
@@ -161,6 +164,32 @@ struct ModelCatalogView: View {
}
private var readyModels: [CatalogModel] {
installedModels.filter { $0.installed(at: $0.localFolder) }
}
private var favorites: Set<String> {
(try? JSONDecoder().decode(Set<String>.self, from: favoriteData)) ?? []
}
private func favoriteFirst<T>(_ rows: [T], key: (T) -> String) -> [T] {
let saved = favorites
return rows.filter { saved.contains(key($0)) } + rows.filter { !saved.contains(key($0)) }
}
private func favoriteButton(_ key: String) -> some View {
let saved = favorites.contains(key)
let label = saved ? "Remove from Favorites" : "Add to Favorites"
return Button(label, systemImage: saved ? "star.fill" : "star") {
var updated = favorites
if saved { updated.remove(key) } else { updated.insert(key) }
if let data = try? JSONEncoder().encode(updated) { favoriteData = data }
}
.labelStyle(.iconOnly)
.buttonStyle(.borderless)
.help(label)
}
var body: some View {
VStack(spacing: 0) {
Picker("Model runtime", selection: $backend) {
@@ -180,25 +209,23 @@ 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") {
Section("Installed MLX Models") {
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 {
if library.isEmpty && readyModels.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
ForEach(favoriteFirst(readyModels) { "mlx:" + $0.id }) { model in modelRow(model) }
let paths = library.filter { path in !readyModels.contains { $0.localFolder.path == path } }
ForEach(favoriteFirst(paths) { "folder:" + $0 }, id: \.self) { path in
HStack {
Text(URL(fileURLWithPath: path).lastPathComponent).help(path)
favoriteButton("folder:" + path)
Spacer()
Button(mlxPath == path && mlxOnly ? "Active" : "Load Model") { load(path) }
.disabled(studio.running || loadingPath != nil || !MLXLibrary.isModel(at: URL(fileURLWithPath: path)) ||
@@ -217,24 +244,21 @@ struct ModelCatalogView: View {
HStack {
TextField("Search MLX models", text: $query, prompt: Text("e.g. Qwen, Gemma"))
.labelsHidden().textFieldStyle(.roundedBorder).onSubmit(search)
Button(query.isEmpty ? "Browse" : "Search", action: search).disabled(searching)
Button(query.isEmpty ? "Browse" : "Search", action: search).disabled(searching || active != nil)
if searching { ProgressView().controlSize(.small) }
}
if !searchStatus.isEmpty { Text(searchStatus).foregroundStyle(.secondary) }
ForEach(matches.filter { model in !installedModels.contains { $0.id == model.id && $0.installed(at: $0.localFolder) } }) { model in
let available = matches.filter { model in !readyModels.contains { $0.id == model.id } }
ForEach(favoriteFirst(available) { "mlx:" + $0.id }) { model in
modelRow(model)
}
}
Section("Recommended Models") {
ForEach(CatalogModel.all.filter { !$0.installed(at: $0.localFolder) }) { model in modelRow(model) }
ForEach(favoriteFirst(CatalogModel.all.filter { !$0.installed(at: $0.localFolder) }) { "mlx:" + $0.id }) { model in modelRow(model) }
}
if !status.isEmpty {
if !status.isEmpty && active == nil {
Section {
Text(status).textSelection(.enabled).foregroundStyle(.secondary)
if let progress, active != nil {
ProgressView(value: progress) { Text("Files downloaded") }
Button("Cancel Download") { operation?.cancel() }
}
if loadingPath != nil { ProgressView().controlSize(.small) }
}
}
@@ -278,9 +302,10 @@ struct ModelCatalogView: View {
.disabled(studio.managing)
if studio.modelListEndpoint == ollamaEndpoint {
if studio.models.isEmpty { Text("No models installed on this server.").foregroundStyle(.secondary) }
ForEach(studio.models) { model in
ForEach(favoriteFirst(studio.models) { "ollama:" + $0.name }) { model in
HStack {
Label(model.name, systemImage: "cube")
favoriteButton("ollama:" + model.name)
Spacer()
if let size = model.size {
Text(ByteCountFormatter.string(fromByteCount: size, countStyle: .file))
@@ -304,37 +329,56 @@ struct ModelCatalogView: View {
HStack {
TextField("Search Ollama models", text: $ollamaQuery, prompt: Text("e.g. Qwen, Llama"))
.labelsHidden().textFieldStyle(.roundedBorder).onSubmit(searchOllama)
Button(ollamaQuery.isEmpty ? "Browse" : "Search", action: searchOllama).disabled(ollamaSearching)
Button(ollamaQuery.isEmpty ? "Browse" : "Search", action: searchOllama).disabled(ollamaSearching || pullingModel != nil)
if ollamaSearching { ProgressView().controlSize(.small) }
}
if !ollamaCatalogStatus.isEmpty { Text(ollamaCatalogStatus).foregroundStyle(.secondary) }
ForEach(ollamaCatalog, id: \.self) { name in
HStack {
Text(name).font(.headline)
Spacer()
if let card = URL(string: "https://ollama.com/library/" + name) {
Link("Tags", destination: card)
ForEach(favoriteFirst(ollamaCatalog) { "ollama:" + $0 + ":latest" }, id: \.self) { name in
VStack(alignment: .leading, spacing: 6) {
HStack {
Text(name).font(.headline)
favoriteButton("ollama:" + name + ":latest")
Spacer()
if let card = URL(string: "https://ollama.com/library/" + name) {
Link("Tags", destination: card)
}
if studio.modelListEndpoint == ollamaEndpoint,
studio.models.contains(where: { $0.name == name || $0.name == name + ":latest" }) {
Text("Installed").foregroundStyle(.secondary)
} else {
Button("Pull Latest") {
pullingModel = name
studio.pull(name, endpoint: ollamaEndpoint)
}
.disabled(studio.managing)
}
}
if studio.modelListEndpoint == ollamaEndpoint,
studio.models.contains(where: { $0.name == name || $0.name == name + ":latest" }) {
Text("Installed").foregroundStyle(.secondary)
} else {
Button("Pull Latest") { studio.pull(name, endpoint: ollamaEndpoint) }
.disabled(studio.managing)
if studio.managing && pullingModel == name {
ProgressView(value: ollamaPullFraction).progressViewStyle(.linear)
.accessibilityLabel("Download progress")
Text(studio.modelStatus).font(.caption).foregroundStyle(.secondary)
}
}
}
}
}
.formStyle(.grouped)
.onChange(of: studio.managing) { _, busy in if !busy { pullingModel = nil } }
.onAppear {
studio.refresh(endpoint: ollamaEndpoint)
if ollamaCatalog.isEmpty && ollamaCatalogStatus.isEmpty { searchOllama() }
}
}
private var ollamaPullFraction: Double? {
guard studio.modelStatus.hasSuffix("%"),
let value = studio.modelStatus.split(separator: " ").last,
let percent = Double(value.dropLast()) else { return nil }
return percent / 100
}
private func searchOllama() {
guard !ollamaSearching else { return }
guard !ollamaSearching, pullingModel == nil else { return }
let term = ollamaQuery.trimmingCharacters(in: .whitespacesAndNewlines)
ollamaSearching = true
ollamaCatalogStatus = ""
@@ -353,7 +397,10 @@ struct ModelCatalogView: View {
let isActive = mlxPath == saved.localFolder.path && mlxOnly
let diskBytes = installed ? try? MLXRuntimeLimits.weightBytes(in: saved.localFolder) : nil
return VStack(alignment: .leading, spacing: 6) {
Text(model.title).font(.headline)
HStack {
Text(model.title).font(.headline)
favoriteButton("mlx:" + model.id)
}
HStack {
if let diskBytes {
Text(ByteCountFormatter.string(fromByteCount: Int64(clamping: diskBytes), countStyle: .file) + " weights")
@@ -372,20 +419,30 @@ struct ModelCatalogView: View {
.disabled(studio.running || loadingPath != nil)
Button("Reveal") { NSWorkspace.shared.activateFileViewerSelecting([saved.localFolder]) }
} else {
Button("Download") { download(model) }.disabled(active != nil)
if active?.id == model.id {
Button("Cancel Download") { operation?.cancel() }
} else {
Button("Download") { download(model) }.disabled(active != nil)
}
}
if FileManager.default.fileExists(atPath: saved.localFolder.path) {
Button("Remove…", role: .destructive) { removing = saved }
.disabled(studio.running || active != nil || loadingPath != nil)
}
}
if active?.id == model.id, let progress {
ProgressView(value: progress)
.progressViewStyle(.linear)
.accessibilityLabel("Download progress")
Text(status).font(.caption).foregroundStyle(.secondary)
}
}
.padding(.vertical, 4)
}
private func search() {
let term = query.trimmingCharacters(in: .whitespacesAndNewlines)
guard !searching else { return }
guard !searching, active == nil else { return }
searching = true
searchStatus = ""
Task {