Add branded startup, Ollama and MLX inference, persistent chat and document retrieval, confirmed MCP tools, and reproducible build and test scripts.
386 lines
17 KiB
Swift
386 lines
17 KiB
Swift
import SwiftUI
|
|
import AppKit
|
|
import UniformTypeIdentifiers
|
|
|
|
@main
|
|
struct AIStudioApp: App {
|
|
@StateObject private var studio = StudioModel()
|
|
|
|
var body: some Scene {
|
|
Window("AI Studio", id: "studio") {
|
|
Group {
|
|
if studio.launching {
|
|
StudioWelcome(loading: true)
|
|
} else {
|
|
ChatView(studio: studio)
|
|
}
|
|
}
|
|
.frame(minWidth: 740, minHeight: 500)
|
|
.task {
|
|
await Task.yield()
|
|
studio.open()
|
|
}
|
|
}
|
|
.defaultSize(width: 1000, height: 700)
|
|
Settings {
|
|
TabView {
|
|
BackendSettings(studio: studio)
|
|
.tabItem { Label("Models", systemImage: "cpu") }
|
|
ModelCatalogView(studio: studio)
|
|
.tabItem { Label("MLX Catalog", systemImage: "arrow.down.circle") }
|
|
MCPSettings(studio: studio)
|
|
.tabItem { Label("MCP Tools", systemImage: "wrench.and.screwdriver") }
|
|
}
|
|
.frame(width: 630, height: 600)
|
|
}
|
|
}
|
|
}
|
|
|
|
struct ChatView: View {
|
|
@ObservedObject var studio: StudioModel
|
|
@AppStorage("endpoint") private var endpoint = "http://127.0.0.1:11434"
|
|
@AppStorage("ollamaModel") private var model = ""
|
|
@AppStorage("mlxPath") private var mlxPath = ""
|
|
@AppStorage("thinking") private var thinking = Thinking.automatic
|
|
@AppStorage("failover") private var failover = true
|
|
@AppStorage("mlxOnly") private var mlxOnly = false
|
|
@AppStorage("recall") private var recall = true
|
|
@State private var confirmDelete = false
|
|
@State private var showingLibrary = false
|
|
@State private var useDocuments = false
|
|
|
|
var body: some View {
|
|
NavigationSplitView {
|
|
List(studio.conversations, selection: $studio.selected) { chat in
|
|
Label(chat.title, systemImage: "bubble.left").tag(chat.id)
|
|
}
|
|
.disabled(studio.running)
|
|
.navigationSplitViewColumnWidth(min: 180, ideal: 235)
|
|
.toolbar {
|
|
Button("New Chat", systemImage: "square.and.pencil") { studio.newChat() }
|
|
.disabled(studio.running)
|
|
Button("Delete Chat", systemImage: "trash") { confirmDelete = true }
|
|
.disabled(studio.running || studio.selected == nil)
|
|
}
|
|
} detail: {
|
|
VStack(spacing: 0) {
|
|
if studio.messages.isEmpty && studio.partial == nil {
|
|
StudioWelcome(loading: false)
|
|
} else {
|
|
transcript
|
|
}
|
|
Divider()
|
|
composer
|
|
}
|
|
.navigationTitle("AI Studio")
|
|
.toolbar {
|
|
Button("Documents", systemImage: "doc.on.doc") { showingLibrary = true }
|
|
.disabled(studio.selected == nil)
|
|
SettingsLink { Label("Settings", systemImage: "gear") }
|
|
}
|
|
}
|
|
.sheet(isPresented: $showingLibrary) { DocumentLibraryView(studio: studio) }
|
|
.onChange(of: studio.selected) { _, _ in useDocuments = false }
|
|
.confirmationDialog("Delete this conversation?", isPresented: $confirmDelete, titleVisibility: .visible) {
|
|
Button("Delete Conversation", role: .destructive) { studio.deleteChat() }
|
|
} message: {
|
|
Text("This permanently removes the conversation and its recalled messages.")
|
|
}
|
|
}
|
|
|
|
private var transcript: some View {
|
|
ScrollViewReader { proxy in
|
|
ScrollView {
|
|
LazyVStack(alignment: .leading, spacing: 18) {
|
|
ForEach(studio.messages) { MessageView(message: $0) }
|
|
if let partial = studio.partial { MessageView(message: partial) }
|
|
Color.clear.frame(height: 1).id("end")
|
|
}
|
|
.frame(maxWidth: 800)
|
|
.padding(24)
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
.onChange(of: studio.messages.count, initial: true) { _, _ in proxy.scrollTo("end", anchor: .bottom) }
|
|
.onChange(of: studio.partial) { _, _ in proxy.scrollTo("end", anchor: .bottom) }
|
|
}
|
|
}
|
|
|
|
private var composer: some View {
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
HStack {
|
|
Label(mlxOnly ? "MLX on this Mac" : "Ollama · \(model.isEmpty ? "Choose a model" : model)",
|
|
systemImage: mlxOnly ? "cpu" : "server.rack")
|
|
.font(.caption)
|
|
if !mlxOnly {
|
|
Text(endpoint).font(.caption).foregroundStyle(.secondary).lineLimit(1)
|
|
}
|
|
Spacer()
|
|
if !mlxOnly || MLXThinking.supports(mlxPath) {
|
|
let modes: [Thinking] = mlxOnly ? [.automatic, .off, .on] : Thinking.allCases
|
|
Picker("Thinking", selection: Binding(
|
|
get: { modes.contains(thinking) ? thinking : .automatic },
|
|
set: { thinking = $0 }
|
|
)) {
|
|
ForEach(modes) { Text($0.rawValue.capitalized).tag($0) }
|
|
}
|
|
.fixedSize()
|
|
.disabled(studio.running)
|
|
}
|
|
}
|
|
if !studio.documents.isEmpty {
|
|
Toggle(mlxOnly ? "Include matching document excerpts" :
|
|
"Send matching document excerpts to Ollama", isOn: $useDocuments)
|
|
.font(.caption)
|
|
.disabled(studio.running)
|
|
}
|
|
if let attachment = studio.toolAttachment {
|
|
HStack {
|
|
Label("Attach \(attachment.name) result to next message", systemImage: "paperclip")
|
|
if !mlxOnly { Text("Sent to Ollama").foregroundStyle(.secondary) }
|
|
Spacer()
|
|
Button("Remove", systemImage: "xmark.circle") { studio.removeToolAttachment() }
|
|
}
|
|
.font(.caption)
|
|
}
|
|
TextEditor(text: $studio.prompt)
|
|
.font(.body)
|
|
.frame(height: 62)
|
|
.padding(5)
|
|
.background(.background, in: RoundedRectangle(cornerRadius: 8))
|
|
.overlay(RoundedRectangle(cornerRadius: 8).strokeBorder(.quaternary))
|
|
.accessibilityLabel("Message")
|
|
HStack {
|
|
Text(studio.status).font(.caption).foregroundStyle(.secondary).textSelection(.enabled)
|
|
Spacer()
|
|
if studio.running {
|
|
Button("Stop", systemImage: "stop.fill") { studio.stop() }
|
|
} else {
|
|
Button("Send", systemImage: "arrow.up") {
|
|
studio.send(endpoint: endpoint, model: model, mlxPath: mlxPath, thinking: thinking,
|
|
failover: failover, mlxOnly: mlxOnly, recall: recall, useDocuments: useDocuments)
|
|
}
|
|
.buttonStyle(.borderedProminent)
|
|
.keyboardShortcut(.return, modifiers: .command)
|
|
.disabled(studio.prompt.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
|
|
}
|
|
}
|
|
}
|
|
.padding(16)
|
|
}
|
|
}
|
|
|
|
struct MessageView: View {
|
|
let message: Message
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
HStack {
|
|
Label(message.role == .user ? "You" : "Assistant",
|
|
systemImage: message.role == .user ? "person.crop.circle" : "sparkle")
|
|
.font(.headline)
|
|
if !message.backend.isEmpty {
|
|
Text(message.backend).font(.caption).foregroundStyle(.secondary)
|
|
}
|
|
}
|
|
if !message.thinking.isEmpty {
|
|
DisclosureGroup("Thinking") {
|
|
Text(message.thinking).foregroundStyle(.secondary).textSelection(.enabled)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
}
|
|
}
|
|
if !message.content.isEmpty { Text(message.content).textSelection(.enabled) }
|
|
if message.content.isEmpty && message.thinking.isEmpty { ProgressView().controlSize(.small) }
|
|
}
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
.padding(14)
|
|
.background(message.role == .user ? Color.accentColor.opacity(0.08) : Color.secondary.opacity(0.06),
|
|
in: RoundedRectangle(cornerRadius: 12))
|
|
}
|
|
}
|
|
|
|
struct BackendSettings: View {
|
|
@ObservedObject var studio: StudioModel
|
|
@AppStorage("endpoint") private var endpoint = "http://127.0.0.1:11434"
|
|
@AppStorage("ollamaModel") private var model = ""
|
|
@AppStorage("mlxPath") private var mlxPath = ""
|
|
@AppStorage("failover") private var failover = true
|
|
@AppStorage("mlxOnly") private var mlxOnly = false
|
|
@AppStorage("recall") private var recall = true
|
|
@State private var tag = ""
|
|
@State private var pickingFolder = false
|
|
@State private var confirmRemoval = false
|
|
@State private var folderError = ""
|
|
@State private var modelsEndpoint = ""
|
|
|
|
var body: some View {
|
|
Form {
|
|
Section("Inference") {
|
|
Picker("Backend", selection: $mlxOnly) {
|
|
Text("Prefer Ollama").tag(false)
|
|
Text("MLX only").tag(true)
|
|
}
|
|
Toggle("Fall back to MLX when Ollama fails before output", isOn: $failover)
|
|
.disabled(mlxOnly)
|
|
Toggle("Recall older matching messages in this conversation", isOn: $recall)
|
|
}
|
|
Section("Ollama") {
|
|
TextField("Server URL", text: $endpoint)
|
|
.disabled(studio.managing)
|
|
SecureField("Ollama bearer token (session only)", text: Binding(
|
|
get: { studio.ollamaToken },
|
|
set: { studio.setOllamaToken($0, endpoint: endpoint) }
|
|
))
|
|
.disabled(studio.managing)
|
|
.onChange(of: endpoint) { _, newEndpoint in
|
|
studio.setOllamaToken("", endpoint: newEndpoint)
|
|
}
|
|
TextField("Chat model", text: $model, prompt: Text("model:tag"))
|
|
HStack {
|
|
Button("Refresh Installed Models") {
|
|
modelsEndpoint = endpoint
|
|
studio.refresh(endpoint: endpoint)
|
|
}
|
|
.disabled(studio.managing)
|
|
if studio.managing { Button("Cancel") { studio.cancelManagement() } }
|
|
}
|
|
if modelsEndpoint == endpoint, !studio.models.isEmpty {
|
|
Menu("Installed Models") {
|
|
ForEach(studio.models) { entry in
|
|
Button(entry.name) { model = entry.name }
|
|
}
|
|
}
|
|
}
|
|
HStack {
|
|
Button("Inspect Model") { studio.inspect(model, endpoint: endpoint) }
|
|
Button("Delete Model…", role: .destructive) { confirmRemoval = true }
|
|
}
|
|
.disabled(studio.managing || model.isEmpty)
|
|
HStack {
|
|
TextField("Download model", text: $tag, prompt: Text("model:tag"))
|
|
Button("Pull") {
|
|
modelsEndpoint = endpoint
|
|
studio.pull(tag, endpoint: endpoint)
|
|
}
|
|
.disabled(studio.managing || tag.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
|
|
}
|
|
Text(studio.modelStatus).foregroundStyle(.secondary).textSelection(.enabled)
|
|
}
|
|
Section("MLX") {
|
|
LabeledContent("Model folder", value: mlxPath.isEmpty ? "None selected" : mlxPath)
|
|
HStack {
|
|
Button("Choose Folder…") { pickingFolder = true }
|
|
Button("Unload Model") { studio.unloadMLX() }
|
|
Button("Clear") { mlxPath = ""; studio.unloadMLX() }.disabled(mlxPath.isEmpty)
|
|
}
|
|
.disabled(studio.running)
|
|
if !folderError.isEmpty { Text(folderError).foregroundStyle(.red) }
|
|
}
|
|
}
|
|
.formStyle(.grouped)
|
|
.frame(width: 590, height: 550)
|
|
.fileImporter(isPresented: $pickingFolder, allowedContentTypes: [.folder]) { result in
|
|
do {
|
|
let url = try result.get()
|
|
guard FileManager.default.fileExists(atPath: url.appending(path: "config.json").path) else {
|
|
throw StudioError("This folder does not contain an MLX config.json.")
|
|
}
|
|
mlxPath = url.path
|
|
folderError = ""
|
|
studio.unloadMLX()
|
|
} catch { folderError = error.localizedDescription }
|
|
}
|
|
.confirmationDialog("Delete \(model) from this Ollama server?", isPresented: $confirmRemoval, titleVisibility: .visible) {
|
|
Button("Delete Model", role: .destructive) { studio.remove(model, endpoint: endpoint) }
|
|
}
|
|
}
|
|
}
|
|
|
|
struct StudioWelcome: View {
|
|
let loading: Bool
|
|
|
|
var body: some View {
|
|
VStack(spacing: 12) {
|
|
Image(nsImage: NSApp.applicationIconImage)
|
|
.resizable()
|
|
.interpolation(.high)
|
|
.frame(width: 116, height: 116)
|
|
.accessibilityHidden(true)
|
|
Text("AI Studio")
|
|
.font(.system(size: 34, weight: .semibold, design: .rounded))
|
|
if loading {
|
|
ProgressView("Opening workspace…")
|
|
.controlSize(.small)
|
|
} else {
|
|
Text("Choose a model in Settings or start with a message below.")
|
|
.foregroundStyle(.secondary)
|
|
.multilineTextAlignment(.center)
|
|
SettingsLink { Label("Model Settings", systemImage: "slider.horizontal.3") }
|
|
.buttonStyle(.bordered)
|
|
.padding(.top, 4)
|
|
}
|
|
}
|
|
.padding(32)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
.background {
|
|
LinearGradient(colors: [.accentColor.opacity(0.12), .clear],
|
|
startPoint: .top, endPoint: .bottom)
|
|
}
|
|
}
|
|
}
|
|
|
|
struct DocumentLibraryView: View {
|
|
@Environment(\.dismiss) private var dismiss
|
|
@ObservedObject var studio: StudioModel
|
|
@State private var importing = false
|
|
@State private var fileError = ""
|
|
@State private var removing: LibraryDocument?
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 14) {
|
|
Text("Conversation Documents").font(.title2.bold())
|
|
Text("Import PDF, text, Markdown, or CSV. Enable excerpts in the composer to use matching passages.")
|
|
.foregroundStyle(.secondary)
|
|
List(studio.documents) { document in
|
|
HStack {
|
|
Label(document.title, systemImage: "doc.text")
|
|
Spacer()
|
|
Button("Remove…", systemImage: "trash") { removing = document }
|
|
.labelStyle(.iconOnly)
|
|
.disabled(studio.running || studio.importingDocument)
|
|
}
|
|
}
|
|
.overlay {
|
|
if studio.documents.isEmpty { Text("No documents imported").foregroundStyle(.secondary) }
|
|
}
|
|
if !fileError.isEmpty || !studio.libraryStatus.isEmpty {
|
|
Text(fileError.isEmpty ? studio.libraryStatus : fileError).foregroundStyle(.red)
|
|
}
|
|
HStack {
|
|
Button("Import Document…", systemImage: "plus") { importing = true }
|
|
.disabled(studio.running || studio.importingDocument)
|
|
if studio.importingDocument { ProgressView().controlSize(.small) }
|
|
Spacer()
|
|
Button("Done") { dismiss() }.keyboardShortcut(.defaultAction)
|
|
}
|
|
}
|
|
.padding(22)
|
|
.frame(width: 470, height: 360)
|
|
.fileImporter(isPresented: $importing, allowedContentTypes: [
|
|
.pdf, .plainText, .commaSeparatedText, UTType(filenameExtension: "md") ?? .plainText
|
|
]) { result in
|
|
do {
|
|
let url = try result.get()
|
|
fileError = ""
|
|
Task { await studio.importDocument(url) }
|
|
} catch { fileError = error.localizedDescription }
|
|
}
|
|
.confirmationDialog("Remove this document?", isPresented: Binding(
|
|
get: { removing != nil }, set: { if !$0 { removing = nil } }
|
|
), titleVisibility: .visible, presenting: removing) { document in
|
|
Button("Remove", role: .destructive) { studio.deleteDocument(document) }
|
|
} message: { document in
|
|
Text(document.title)
|
|
}
|
|
}
|
|
}
|