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
+71 -1
View File
@@ -5,7 +5,43 @@ import MLXLMCommon
actor MLXProvider: InferenceProvider {
private var loaded: (directory: String, container: ModelContainer)?
func unload() { loaded = nil }
private var eviction: Task<Void, Never>?
private var idle: Duration?
private var weightLimit: UInt64?
private var active = false
func configureRuntime(idle: Duration?, weightLimit: UInt64?) {
self.idle = idle
self.weightLimit = weightLimit
scheduleEviction()
}
func modelName() -> String? {
loaded.map { URL(fileURLWithPath: $0.directory).lastPathComponent }
}
func unload() {
guard !active else { return }
eviction?.cancel()
eviction = nil
loaded = nil
}
private func scheduleEviction() {
eviction?.cancel()
eviction = nil
guard !active, loaded != nil, let idle else { return }
eviction = Task { [weak self] in
do { try await Task.sleep(for: idle) }
catch { return }
await self?.evictWhenIdle()
}
}
private func evictWhenIdle() {
guard !Task.isCancelled else { return }
unload()
}
nonisolated func generate(_ request: InferenceRequest) -> AsyncThrowingStream<Delta, Error> {
streaming { continuation in
@@ -14,6 +50,15 @@ actor MLXProvider: InferenceProvider {
}
private func generate(_ request: InferenceRequest, continuation: AsyncThrowingStream<Delta, Error>.Continuation) async throws {
try Task.checkCancellation()
guard !active else { throw StudioError("MLX is already generating a reply.") }
active = true
eviction?.cancel()
eviction = nil
defer {
active = false
scheduleEviction()
}
let directory = request.model
let model: ModelContainer
if let loaded, loaded.directory == directory {
@@ -24,6 +69,9 @@ actor MLXProvider: InferenceProvider {
guard FileManager.default.fileExists(atPath: url.appending(path: "config.json").path) else {
throw StudioError("Select an MLX model folder containing config.json and model weights.")
}
if let weightLimit, try MLXRuntimeLimits.weightBytes(in: url) > weightLimit {
throw StudioError("The model’s weight files exceed the configured MLX weight limit.")
}
model = try await LLMModelFactory.shared.loadContainer(configuration: .init(directory: url))
try Task.checkCancellation()
loaded = (directory, model)
@@ -90,6 +138,28 @@ actor MLXProvider: InferenceProvider {
}
}
enum MLXRuntimeLimits {
static let idleKey = "mlxIdleMinutes"
static let weightKey = "mlxWeightLimitGiB"
static let defaultIdleMinutes = 15
static func weightBytes(in directory: URL) throws -> UInt64 {
let files = try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil)
var seen: Set<URL> = []
var total: UInt64 = 0
for file in files where file.pathExtension.lowercased() == "safetensors" {
let target = file.resolvingSymlinksInPath()
guard seen.insert(target).inserted else { continue }
let values = try target.resourceValues(forKeys: [.isRegularFileKey, .fileSizeKey])
if values.isRegularFile == true, let size = values.fileSize, size > 0 {
let (sum, overflow) = total.addingReportingOverflow(UInt64(size))
total = overflow ? UInt64.max : sum
}
}
return total
}
}
struct MLXThinking {
private static let start = "<think>"
private static let end = "</think>"