Add desktop file previews and MLX runtime controls

This commit is contained in:
Mark Gregory
2026-11-16 14:18:52 +00:00
parent 515f609f8d
commit d4acb0baa1
6 changed files with 240 additions and 6 deletions
+92 -1
View File
@@ -33,6 +33,8 @@ struct AIStudioApp: App {
.tabItem { Label("Generation", systemImage: "slider.horizontal.3") }
LocalToolsView(studio: studio)
.tabItem { Label("Local Tools", systemImage: "terminal") }
RuntimeSettingsView(studio: studio)
.tabItem { Label("Runtime", systemImage: "waveform.path") }
MCPSettings(studio: studio)
.tabItem { Label("MCP Tools", systemImage: "wrench.and.screwdriver") }
}
@@ -791,6 +793,8 @@ struct LocalToolsView: View {
@State private var output = ""
@State private var process: Process?
@State private var file: URL?
@State private var preview = ""
@State private var fileStatus = ""
@State private var picking = false
@State private var status = ""
@State private var confirm = false
@@ -835,14 +839,35 @@ struct LocalToolsView: View {
HStack {
Button("Open") { NSWorkspace.shared.open(file) }
Button("Reveal in Finder") { NSWorkspace.shared.activateFileViewerSelecting([file]) }
Button("Copy Path") { copyChatText(file.path) }
}
Button("Preview Text") {
do {
preview = try LocalFilePreview.read(file)
fileStatus = preview.isEmpty ? "This file is empty." : ""
} catch { preview = ""; fileStatus = error.localizedDescription }
}
if !fileStatus.isEmpty { Text(fileStatus).foregroundStyle(.secondary) }
if !preview.isEmpty {
ScrollView {
Text(preview).font(.system(.caption, design: .monospaced)).textSelection(.enabled)
.frame(maxWidth: .infinity, alignment: .leading)
}
.frame(height: 120)
Button("Attach Preview to Active Chat") {
studio.attachToolResult(name: file.lastPathComponent, output: preview)
}
.disabled(studio.selected == nil || studio.running)
}
}
}
}
.formStyle(.grouped)
.fileImporter(isPresented: $picking, allowedContentTypes: [.item, .folder]) { result in
preview = ""
fileStatus = ""
do { file = try result.get() }
catch { status = error.localizedDescription }
catch { fileStatus = error.localizedDescription }
}
.confirmationDialog("Run Docker command?", isPresented: $confirm, titleVisibility: .visible) {
Button("Run Command") { if let pending { run(pending) } }
@@ -944,3 +969,69 @@ struct CompatibleEndpointSettings: View {
} catch { status = error.localizedDescription }
}
}
enum LocalFilePreview {
static func read(_ url: URL, limit: Int = 8_000) throws -> String {
let scoped = url.startAccessingSecurityScopedResource()
defer { if scoped { url.stopAccessingSecurityScopedResource() } }
let file = url.resolvingSymlinksInPath()
guard try file.resourceValues(forKeys: [.isRegularFileKey]).isRegularFile == true else {
throw StudioError("Select a regular UTF-8 text file to preview.")
}
let handle = try FileHandle(forReadingFrom: file)
defer { try? handle.close() }
let bytes = try handle.read(upToCount: limit + 1) ?? Data()
let truncated = bytes.count > limit
var prefix = Data(bytes.prefix(limit))
guard !prefix.contains(0) else { throw StudioError("This file does not contain UTF-8 text.") }
for trim in 0...(truncated ? 3 : 0) {
if trim > 0 && !prefix.isEmpty { prefix.removeLast() }
if let text = String(data: prefix, encoding: .utf8) {
return truncated ? text + "\n[Preview truncated]" : text
}
}
throw StudioError("This file does not contain UTF-8 text.")
}
}
struct RuntimeSettingsView: View {
@ObservedObject var studio: StudioModel
@AppStorage(MLXRuntimeLimits.idleKey) private var idleMinutes = MLXRuntimeLimits.defaultIdleMinutes
@AppStorage(MLXRuntimeLimits.weightKey) private var weightGiB = 0
@State private var loaded: String?
var body: some View {
Form {
Section("MLX Model Residency") {
LabeledContent("Cached model", value: loaded ?? "None")
LabeledContent("State", value: studio.running ? "Generation in progress" : "Idle")
Button("Unload Cached Model") { studio.unloadMLX() }
.disabled(studio.running || loaded == nil)
Stepper(value: $idleMinutes, in: 0...120) {
LabeledContent("Unload after idle", value: idleMinutes == 0 ? "Never" : "\(idleMinutes) min")
}
Text("The idle timer starts after MLX finishes a generation. Changing the timeout restarts the timer.")
.foregroundStyle(.secondary)
}
Section("Loading Limit") {
LabeledContent("Physical memory", value: ByteCountFormatter.string(
fromByteCount: Int64(ProcessInfo.processInfo.physicalMemory), countStyle: .memory))
Stepper(value: $weightGiB, in: 0...512) {
LabeledContent("Maximum model weights", value: weightGiB == 0 ? "Off" : "\(weightGiB) GiB")
}
Text("The limit checks safetensors on disk before a new MLX load. Runtime memory also depends on the model and cache.")
.foregroundStyle(.secondary)
}
}
.formStyle(.grouped)
.onChange(of: idleMinutes) { _, _ in studio.reloadRuntimeSettings() }
.onChange(of: weightGiB) { _, _ in studio.reloadRuntimeSettings() }
.task {
while !Task.isCancelled {
loaded = await studio.mlxModelName()
do { try await Task.sleep(for: .milliseconds(500)) }
catch { return }
}
}
}
}