Files
kangkang/康康/Features/Timeline/TimelineEntry.swift
link2026 9d856fcfc4 ```
feat(AI): 集成MNN推理引擎替换MLX作为主AI运行时

- 引入MNN(alibaba) + Arm SME2 + CPU作为主AI运行时,支持A19/iPhone17的
  SME2和A17的NEON加速
- 添加MLX Swift作为兜底GPU推理方案,实现双后端切换机制
- 使用单一Qwen3.5-2B多模态模型(1.2GB),替代原有的LLM+VL分离架构
- 实现InferenceEngine.current引擎选择逻辑,真机默认MNN,模拟器回退MLX
- 更新AIAgent架构,通过MNNLLMBridge(ObjC++) → MNNBackend进行推理
- 修改队列机制防止并发推理导致OOM,使用信号量闸门控制显存占用
- 更新文档中的技术栈说明、模块边界和周次交付计划
```
2026-06-15 09:24:59 +08:00

237 lines
9.4 KiB
Swift

import SwiftUI
import SwiftData
import Foundation
enum TimelineKind: String, CaseIterable, Identifiable {
case diary, symptom, indicator, medication, report
var id: String { rawValue }
var label: String {
switch self {
case .indicator: return String(appLoc: "指标")
case .report: return String(appLoc: "报告")
case .symptom: return String(appLoc: "症状")
case .diary: return String(appLoc: "日记")
case .medication: return String(appLoc: "用药")
}
}
var icon: String {
switch self {
case .indicator: return "drop.fill"
case .report: return "doc.fill"
case .symptom: return "waveform.path.ecg"
case .diary: return "pencil"
case .medication: return "pills.fill"
}
}
var accent: Color {
switch self {
case .indicator: return Tj.Palette.brick
case .report: return Tj.Palette.ink2
case .symptom: return Tj.Palette.amber
case .diary: return Tj.Palette.leaf
case .medication: return Tj.Palette.ink
}
}
}
struct TimelineEntry: Identifiable, Hashable {
let id: String
let kind: TimelineKind
let date: Date
let title: String
var subtitle: String
let trailing: String?
let trailingIsAlert: Bool
let isOngoing: Bool
/// (>1 N ) 1
var aggregateCount: Int = 1
static func from(indicator i: Indicator) -> TimelineEntry {
TimelineEntry(
id: "indicator-\(i.persistentModelID)",
kind: .indicator,
date: i.capturedAt,
title: i.name,
subtitle: typeSubtitle(for: i),
trailing: indicatorValue(i),
trailingIsAlert: i.status != .normal,
isOngoing: false
)
}
/// Indicator , bp.systolic + bp.diastolic capturedAt
/// " 120/80 mmHg" timeline entry series from(indicator:)
/// :capturedAt 5 ()
static func from(indicators: [Indicator]) -> [TimelineEntry] {
var entries: [TimelineEntry] = []
var consumed = Set<PersistentIdentifier>()
// bp.systolic, bp.diastolic
for sys in indicators where sys.seriesKey == "bp.systolic" {
if consumed.contains(sys.persistentModelID) { continue }
guard let dia = indicators.first(where: {
$0.seriesKey == "bp.diastolic" &&
!consumed.contains($0.persistentModelID) &&
abs($0.capturedAt.timeIntervalSince(sys.capturedAt)) <= 5
}) else { continue }
consumed.insert(sys.persistentModelID)
consumed.insert(dia.persistentModelID)
entries.append(mergedBP(systolic: sys, diastolic: dia))
}
// indicator( systolic/diastolic series)
for i in indicators where !consumed.contains(i.persistentModelID) {
entries.append(from(indicator: i))
}
return entries
}
/// / :(),,
/// (`aggregateCount`,>1 N )
/// `IndicatorSeriesDetailView` /
/// (`IndicatorGroup`):(bp.*) seriesKey key seriesKey name+unit
static func aggregatedIndicators(_ indicators: [Indicator]) -> [TimelineEntry] {
var order: [String] = []
var groups: [String: [Indicator]] = [:]
for i in indicators {
let key = IndicatorGroup.of(i).id
if groups[key] == nil { order.append(key) }
groups[key, default: []].append(i)
}
return order.compactMap { key -> TimelineEntry? in
guard let members = groups[key] else { return nil }
// ( sys/dia),
guard var rep = from(indicators: members).max(by: { $0.date < $1.date }) else { return nil }
// :(bp.systolic ),
let count = key == IndicatorGroup.bloodPressure.id
? members.filter { $0.seriesKey == "bp.systolic" }.count
: members.count
rep.aggregateCount = count
if count > 1 {
rep.subtitle += " · " + String(appLoc: "\(count)")
}
return rep
}
}
private static func mergedBP(systolic sys: Indicator, diastolic dia: Indicator) -> TimelineEntry {
let abnormal = sys.status != .normal || dia.status != .normal
// status : /;
// ( , 85/55 )
let arrow: String
switch (sys.status, dia.status) {
case (.high, .high), (.high, .normal), (.normal, .high): arrow = ""
case (.low, .low), (.low, .normal), (.normal, .low): arrow = ""
default: arrow = ""
}
return TimelineEntry(
id: "bp-\(sys.persistentModelID)-\(dia.persistentModelID)",
kind: .indicator,
date: sys.capturedAt,
title: String(appLoc: "血压"),
subtitle: String(appLoc: "长期监测"),
trailing: "\(sys.value)/\(dia.value) mmHg" + arrow,
trailingIsAlert: abnormal,
isOngoing: false
)
}
static func from(report r: Report) -> TimelineEntry {
let highCount = r.indicators.filter { $0.status == .high }.count
let lowCount = r.indicators.filter { $0.status == .low }.count
return TimelineEntry(
id: "report-\(r.persistentModelID)",
kind: .report,
date: r.reportDate,
title: r.title,
subtitle: "\(r.type.label) · " + String(appLoc: "\(r.pageCount)"),
trailing: abnormalSummary(high: highCount, low: lowCount),
trailingIsAlert: highCount + lowCount > 0,
isOngoing: false
)
}
/// trailing N N N nil
/// N ,(demo )
static func abnormalSummary(high: Int, low: Int) -> String? {
switch (high, low) {
case (0, 0): return nil
case (let h, 0): return String(appLoc: "\(h) 项偏高")
case (0, let l): return String(appLoc: "\(l) 项偏低")
case (let h, let l): return String(appLoc: "\(h + l) 项异常")
}
}
/// tag () .medication ,
/// id "diary-" :TimelineDetail.resolve diaries
static func from(diary d: DiaryEntry) -> TimelineEntry {
let isMed = d.isMedicationLog
return TimelineEntry(
id: "diary-\(d.persistentModelID)",
kind: isMed ? .medication : .diary,
date: d.createdAt,
title: d.content.firstLine(),
subtitle: isMed ? String(appLoc: "用药记录") : String(appLoc: "文字日记"),
trailing: nil,
trailingIsAlert: false,
isOngoing: false
)
}
static func from(symptom s: Symptom) -> TimelineEntry {
let ongoing = s.isOngoing
let date = s.endedAt ?? s.startedAt
let subtitle: String
let trailing: String?
if ongoing {
subtitle = String(appLoc: "症状 · 持续中")
trailing = String(appLoc: "持续 \(formatDuration(s.duration))")
} else {
subtitle = String(appLoc: "症状 · 已结束")
trailing = String(appLoc: "持续 \(formatDuration(s.duration))")
}
return TimelineEntry(
id: "symptom-\(s.persistentModelID)",
kind: .symptom,
date: date,
title: s.name,
subtitle: subtitle,
trailing: trailing,
trailingIsAlert: ongoing,
isOngoing: ongoing
)
}
private static func typeSubtitle(for i: Indicator) -> String {
if let report = i.report {
return String(appLoc: "指标 · \(report.title)")
}
return i.source.label
}
private static func indicatorValue(_ i: Indicator) -> String {
let unit = i.unit.isEmpty ? "" : " \(i.unit)"
let arrow: String
switch i.status {
case .high: arrow = ""
case .low: arrow = ""
case .normal: arrow = ""
}
return "\(i.value)\(unit)\(arrow)"
}
}
private extension String {
func firstLine() -> String {
let trimmed = trimmingCharacters(in: .whitespacesAndNewlines)
if let line = trimmed.split(whereSeparator: \.isNewline).first {
let s = String(line)
return s.count > 40 ? String(s.prefix(40)) + "" : s
}
return trimmed.isEmpty ? String(appLoc: "(空日记)") : trimmed
}
}