feat(AI): 统一多模态模型架构,整合文本和视觉推理路径 - 将文本生成和VL(图→文)功能合并到单一的Qwen3.5-4B多模态MNN模型 - 移除独立的Qwen3-VL-4B模型依赖,MLX VL改为使用.llm的多模态模型 - 更新ModelKind枚举,新增userFacing集合用于面向用户展示 - MNN后端现在同时支持文本和视觉任务,模拟器回退到MLX refactor(models): 模型管理和界面调整以适应新的多模态架构 - 更新模型管理界面,只显示统一的Qwen3.5-4B(MNN)模型给用户 - 修改就绪状态检查逻辑,使用ModelKind.userFacing替代allCases - 更新模型文件清单,从Qwen3.5-2B升级到Qwen3.5-4B-4bit - 调整模型管理页面UI,突出MNN+SME2端侧加速功能 feat(camera): 添加拍照识别引擎切换功能 - 实现双路径拍照识别:Apple Vision OCR + 文本模型 和 Qwen3-VL直接识别 - 添加预处理逻辑,优化Qwen3-VL对窄长区域图片的识别效果 - 在模型管理页面添加拍照识别引擎选择组件 - 提供用户界面选项,在两种识别方式间切换 style(ui): 优化输入框样式和颜色主题一致性 - 为指标快速表单添加浅色主题偏好 - 统一所有文本输入框的颜色样式(theme) - 创建EntryInputField组件,替换原有的单行输入+按钮模式 - 实现聊天框风格的条目输入,支持多行自适应和圆形发送按钮 fix(build): 修正Xcode项目配置中的重复框架搜索路径 - 清理project.pbxproj中重复的FRAMEWORK_SEARCH_PATHS配置 - 重新排列Swift桥接头文件配置确保正确引用 - 修复因路径配置重复导致的编译警告问题 test: 增加区域图片预处理和模型清单测试覆盖 - 添加RegionImageCropper.prepareForQwenVL的单元测试 - 验证宽而矮图片的放大和填充逻辑 - 更新ModelManifestTests中的字节数预期值以匹配新模型 - 修正OCRService中VNRecognizedTextObservation类型的处理 ```
149 lines
5.8 KiB
Swift
149 lines
5.8 KiB
Swift
import Foundation
|
|
import Observation
|
|
|
|
/// 模型下载编排:遍历 ModelManifest 逐文件串行下载,聚合进度,支持暂停/重试/旁路导入。
|
|
/// UI 只观察 `states`,不直接碰 URLSession(§3.1 模块边界)。
|
|
/// 核心下载/校验逻辑在 `FileDownloader`,文件路径/就绪判定在 `ModelStore`。
|
|
@MainActor
|
|
@Observable
|
|
final class ModelDownloadService {
|
|
static let shared = ModelDownloadService()
|
|
|
|
private(set) var states: [ModelKind: DownloadState] = [:]
|
|
|
|
private let store: ModelStore
|
|
private var tasks: [ModelKind: Task<Void, Never>] = [:]
|
|
private var lastSampleTime: [ModelKind: Date] = [:]
|
|
private var lastSampleBytes: [ModelKind: Int] = [:]
|
|
|
|
init(store: ModelStore = .shared) {
|
|
self.store = store
|
|
refreshStates()
|
|
}
|
|
|
|
/// 根据沙盒现状刷新每个模型的状态(已完整→ready,否则 idle)。
|
|
func refreshStates() {
|
|
for kind in ModelKind.allCases {
|
|
let total = ModelManifest.totalBytes(for: kind)
|
|
if store.isComplete(for: kind) {
|
|
states[kind] = DownloadState(phase: .ready, receivedBytes: total,
|
|
totalBytes: total, bytesPerSecond: 0)
|
|
} else if states[kind]?.phase == .downloading {
|
|
continue // 不打断进行中的下载
|
|
} else {
|
|
states[kind] = DownloadState(phase: .idle, receivedBytes: completedBytes(for: kind),
|
|
totalBytes: total, bytesPerSecond: 0)
|
|
}
|
|
}
|
|
}
|
|
|
|
var isAnyDownloading: Bool {
|
|
states.values.contains { $0.phase == .downloading }
|
|
}
|
|
|
|
/// 下载某个模型。幂等:已在下载或已就绪则忽略。
|
|
func download(_ kind: ModelKind) {
|
|
guard tasks[kind] == nil, states[kind]?.phase != .ready else { return }
|
|
let total = ModelManifest.totalBytes(for: kind)
|
|
states[kind] = DownloadState(phase: .downloading, receivedBytes: completedBytes(for: kind),
|
|
totalBytes: total, bytesPerSecond: 0)
|
|
lastSampleTime[kind] = Date()
|
|
lastSampleBytes[kind] = completedBytes(for: kind)
|
|
|
|
let task = Task { [weak self] in
|
|
guard let self else { return }
|
|
await self.run(kind)
|
|
}
|
|
tasks[kind] = task
|
|
}
|
|
|
|
func downloadAll() {
|
|
for kind in ModelKind.userFacing { download(kind) }
|
|
}
|
|
|
|
/// 暂停下载。已下载的 .part 保留,下次从断点续传。
|
|
func cancel(_ kind: ModelKind) {
|
|
tasks[kind]?.cancel()
|
|
tasks[kind] = nil
|
|
let total = ModelManifest.totalBytes(for: kind)
|
|
states[kind] = DownloadState(phase: .idle, receivedBytes: completedBytes(for: kind),
|
|
totalBytes: total, bytesPerSecond: 0)
|
|
}
|
|
|
|
/// 旁路导入:从用户选择的文件夹拷入模型(现场重装兜底)。
|
|
func importModel(_ kind: ModelKind, from folder: URL) throws {
|
|
try store.importModel(kind, from: folder)
|
|
refreshStates()
|
|
}
|
|
|
|
// MARK: - 内部
|
|
|
|
private func run(_ kind: ModelKind) async {
|
|
let files = ModelManifest.files(for: kind)
|
|
let downloader = FileDownloader()
|
|
var completedBefore = 0
|
|
|
|
do {
|
|
for file in files {
|
|
if Task.isCancelled { return }
|
|
let destination = store.fileURL(for: kind, relativePath: file.path)
|
|
let base = completedBefore
|
|
try await downloader.download(
|
|
from: ModelManifest.fileURL(for: kind, file: file),
|
|
to: destination,
|
|
expectedBytes: file.bytes,
|
|
onProgress: { [weak self] received in
|
|
guard let self else { return }
|
|
Task { @MainActor in
|
|
self.applyProgress(kind, currentTotal: base + received)
|
|
}
|
|
}
|
|
)
|
|
completedBefore += file.bytes
|
|
}
|
|
finish(kind, success: true, message: nil)
|
|
} catch {
|
|
if Task.isCancelled {
|
|
// cancel() 已设置 idle 状态
|
|
} else {
|
|
finish(kind, success: false, message: error.localizedDescription)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func applyProgress(_ kind: ModelKind, currentTotal: Int) {
|
|
guard var state = states[kind], state.phase == .downloading else { return }
|
|
let now = Date()
|
|
if let lastTime = lastSampleTime[kind], let lastBytes = lastSampleBytes[kind] {
|
|
let dt = now.timeIntervalSince(lastTime)
|
|
if dt >= 0.5 {
|
|
state.bytesPerSecond = Double(currentTotal - lastBytes) / dt
|
|
lastSampleTime[kind] = now
|
|
lastSampleBytes[kind] = currentTotal
|
|
}
|
|
}
|
|
state.receivedBytes = currentTotal
|
|
states[kind] = state
|
|
}
|
|
|
|
private func finish(_ kind: ModelKind, success: Bool, message: String?) {
|
|
tasks[kind] = nil
|
|
let total = ModelManifest.totalBytes(for: kind)
|
|
if success {
|
|
states[kind] = DownloadState(phase: .ready, receivedBytes: total,
|
|
totalBytes: total, bytesPerSecond: 0)
|
|
} else {
|
|
states[kind] = DownloadState(phase: .failed(message ?? String(appLoc: "下载失败")),
|
|
receivedBytes: completedBytes(for: kind),
|
|
totalBytes: total, bytesPerSecond: 0)
|
|
}
|
|
}
|
|
|
|
/// 已完整下载的文件字节之和(用于续传时的起始进度)。
|
|
private func completedBytes(for kind: ModelKind) -> Int {
|
|
ModelManifest.files(for: kind).reduce(0) { sum, file in
|
|
store.localBytes(for: kind, relativePath: file.path) == file.bytes ? sum + file.bytes : sum
|
|
}
|
|
}
|
|
}
|