Xcode 26 默认开启 Swift 6 严格并发检查。AIRuntime(actor) 调用 ModelStore.shared.isReady(...) 跨 actor 边界,因 ModelStore 非 Sendable 而编译报错"Expression is 'async' but is not marked with 'await'; this is an error in the Swift 6 language mode"。 两个类的内部状态只读(rootURL: let),方法只做线程安全的 filesystem I/O,符合 Sendable 语义,标 @unchecked Sendable 即可,不必加锁或重构。 修复目标错误: - AIRuntime.swift:48 - guard ModelStore.shared.isReady(.llm) ... - 后续 CaptureService 调 FileVault.shared.writeJPEG 同样路径 不影响: - HomeView/B5ResultView 里 Text "+" 的 macOS 26.0 deprecation 是 warning,不阻塞 build,留待 UI polish 周清理 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
86 lines
2.9 KiB
Swift
86 lines
2.9 KiB
Swift
import Foundation
|
|
|
|
enum ModelKind: String, CaseIterable {
|
|
/// 与 HuggingFace mlx-community 仓库名一一对应,也是沙盒 Models/ 下的子目录名。
|
|
case llm = "Qwen3-1.7B-4bit"
|
|
case vl = "Qwen2.5-VL-3B-Instruct-4bit"
|
|
|
|
var displayName: String {
|
|
switch self {
|
|
case .llm: return "Qwen3-1.7B"
|
|
case .vl: return "Qwen2.5-VL-3B"
|
|
}
|
|
}
|
|
|
|
/// HuggingFace 仓库 ID(org/name),用于下载
|
|
var huggingFaceRepo: String { "mlx-community/\(rawValue)" }
|
|
|
|
/// 用于判定该模型是否已就绪的最小标志文件
|
|
var sentinelFilename: String { "config.json" }
|
|
}
|
|
|
|
/// `@unchecked Sendable`:rootURL 是 let,方法只读 filesystem(线程安全),
|
|
/// 可被任意 actor / Task 跨边界访问。
|
|
final class ModelStore: @unchecked Sendable {
|
|
static let shared: ModelStore = {
|
|
do {
|
|
let appSupport = try FileManager.default.url(
|
|
for: .applicationSupportDirectory,
|
|
in: .userDomainMask,
|
|
appropriateFor: nil,
|
|
create: true
|
|
)
|
|
let root = appSupport.appendingPathComponent("Models", isDirectory: true)
|
|
return try ModelStore(rootURL: root)
|
|
} catch {
|
|
fatalError("ModelStore.shared init failed: \(error)")
|
|
}
|
|
}()
|
|
|
|
let rootURL: URL
|
|
|
|
init(rootURL: URL) throws {
|
|
self.rootURL = rootURL
|
|
try FileManager.default.createDirectory(at: rootURL, withIntermediateDirectories: true)
|
|
}
|
|
|
|
func localURL(for kind: ModelKind) -> URL {
|
|
rootURL.appendingPathComponent(kind.rawValue, isDirectory: true)
|
|
}
|
|
|
|
func isReady(_ kind: ModelKind) -> Bool {
|
|
let sentinel = localURL(for: kind).appendingPathComponent(kind.sentinelFilename)
|
|
return FileManager.default.fileExists(atPath: sentinel.path)
|
|
}
|
|
|
|
func totalBytes(for kind: ModelKind) -> Int {
|
|
let folder = localURL(for: kind)
|
|
guard let enumerator = FileManager.default.enumerator(
|
|
at: folder,
|
|
includingPropertiesForKeys: [.fileSizeKey]
|
|
) else { return 0 }
|
|
var sum = 0
|
|
for case let url as URL in enumerator {
|
|
if let size = try? url.resourceValues(forKeys: [.fileSizeKey]).fileSize {
|
|
sum += size
|
|
}
|
|
}
|
|
return sum
|
|
}
|
|
|
|
/// Demo 现场旁路:从 Bundle 拷贝预装模型(W6 才真正使用,本周占位)
|
|
func seedFromBundle(_ kind: ModelKind) throws {
|
|
guard let bundleURL = Bundle.main.url(forResource: kind.rawValue, withExtension: nil) else {
|
|
#if DEBUG
|
|
assertionFailure("Bundle 缺少 \(kind.rawValue),检查资源是否加入 target")
|
|
#endif
|
|
return
|
|
}
|
|
let target = localURL(for: kind)
|
|
if FileManager.default.fileExists(atPath: target.path) {
|
|
try FileManager.default.removeItem(at: target)
|
|
}
|
|
try FileManager.default.copyItem(at: bundleURL, to: target)
|
|
}
|
|
}
|