feat(Capture): 归档后后台预生成大白话摘要,详情页秒开 + 兜底重试

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
link2026
2026-06-10 07:12:48 +08:00
parent 0a824610cf
commit 43cdde9bab
5 changed files with 184 additions and 8 deletions

View File

@@ -0,0 +1,44 @@
import Foundation
/// prompt: +
/// 线:;,(:)
nonisolated enum InsightPrompts {
/// (, Report.summary)
static func reportPlainSummary(title: String, typeLabel: String, indicatorLines: String) -> String {
"""
你是健康档案助手。下面是一份报告的指标列表,请用大白话给本人(称「你」)写 2~3 句整体解读:
- 第 1 句:总体情况(共几项、几项异常)。
- 之后:点名最值得留意的异常项,用生活化语言说明偏高/偏低意味着什么方向。
- 不诊断疾病、不推荐药物或剂量;异常较多时建议「带上报告咨询医生」。
- 只输出正文文字,不要标题、列表、JSON、markdown。
示例:
输入:血常规(化验单),指标:白细胞 5.2 (3.5-9.5) normal;血红蛋白 118 (130-175) low;血小板 210 (125-350) normal
输出:这份血常规共 3 项,2 项正常,血红蛋白略低于参考范围。血红蛋白偏低通常与贫血方向有关,平时可以多补充含铁食物;如果还伴随乏力头晕,建议带上报告咨询医生。
现在的报告:\(title)(\(typeLabel))
指标:
\(indicatorLines)
只输出 2~3 句正文。/no_think
"""
}
/// (TrendDetailView,)
static func trendInsight(title: String, unit: String, rangeText: String, dataLines: String) -> String {
"""
你是健康档案助手。下面是「\(title)」的历史记录(单位 \(unit)\(rangeText)),请用大白话给本人(称「你」)写 1~2 句趋势解读:
- 说清整体走向(上升/下降/平稳/波动)和当前值与参考范围的关系。
- 不诊断疾病、不推荐药物;持续异常时温和建议「复查或咨询医生」。
- 只输出正文文字,不要标题、列表、JSON。
示例:
输入:体重,单位 kg,记录:2026-04-01 72.5 / 2026-04-15 71.8 / 2026-05-01 71.2
输出:近一个月你的体重稳步下降了约 1.3kg,节奏平缓,继续保持现在的习惯就好。
现在的记录:
\(dataLines)
只输出 1~2 句正文。/no_think
"""
}
}

View File

@@ -311,6 +311,9 @@ struct UnifiedCaptureFlow: View {
} }
try? ctx.save() try? ctx.save()
// :,
// AI (/) token
Task { await ReportInsightService.shared.pregenerateIfNeeded(report: report, in: ctx) }
onClose() onClose()
} }
} }

View File

@@ -257,14 +257,7 @@ struct TimelineEntryDetailView: View {
} }
} }
if let sum = r.summary, !sum.isEmpty { ReportSummaryCard(report: r)
card {
Text(String(appLoc: "摘要"))
.font(.tjScaled( 12, weight: .semibold)).foregroundStyle(Tj.Palette.text2)
Text(sum).font(.tjScaled( 14)).foregroundStyle(Tj.Palette.text)
.fixedSize(horizontal: false, vertical: true)
}
}
if !r.indicators.isEmpty { if !r.indicators.isEmpty {
card { card {
@@ -559,3 +552,52 @@ private struct EvidenceHighlightOverlay: View {
) )
} }
} }
// MARK: - ()
/// ;(,),
/// 线, SwiftData
private struct ReportSummaryCard: View {
@Environment(\.modelContext) private var ctx
let report: Report
@State private var generating = false
var body: some View {
Group {
if let sum = report.summary, !sum.isEmpty {
container {
Text(String(appLoc: "摘要"))
.font(.tjScaled( 12, weight: .semibold)).foregroundStyle(Tj.Palette.text2)
Text(sum).font(.tjScaled( 14)).foregroundStyle(Tj.Palette.text)
.fixedSize(horizontal: false, vertical: true)
}
} else if generating {
container {
Text("本地 AI 正在解读这份报告…")
.font(.tjScaled( 12)).foregroundStyle(Tj.Palette.text3)
AIFlowBar()
}
}
}
.task {
guard (report.summary ?? "").isEmpty, !report.indicators.isEmpty else { return }
generating = true
await ReportInsightService.shared.pregenerateIfNeeded(report: report, in: ctx)
generating = false
}
}
private func container<C: View>(@ViewBuilder _ body: () -> C) -> some View {
VStack(alignment: .leading, spacing: 10) { body() }
.padding(14)
.frame(maxWidth: .infinity, alignment: .leading)
.background(
RoundedRectangle(cornerRadius: Tj.Radius.md, style: .continuous)
.fill(Tj.Palette.paper)
)
.overlay(
RoundedRectangle(cornerRadius: Tj.Radius.md, style: .continuous)
.strokeBorder(Tj.Palette.lineSoft, lineWidth: 1)
)
}
}

View File

@@ -0,0 +1,61 @@
import Foundation
import SwiftData
/// (§3.1: AIRuntime,UI )
/// :();
/// : summary VL
@MainActor
final class ReportInsightService {
static let shared = ReportInsightService()
private init() {}
/// ID,
private var inFlight: Set<String> = []
func pregenerateIfNeeded(report: Report, in ctx: ModelContext) async {
guard (report.summary ?? "").isEmpty, !report.indicators.isEmpty else { return }
let key = String(describing: report.persistentModelID)
guard !inFlight.contains(key) else { return }
inFlight.insert(key)
defer { inFlight.remove(key) }
do {
try await AIRuntime.shared.prepare()
} catch {
return // :,
}
let prompt = InsightPrompts.reportPlainSummary(
title: report.title,
typeLabel: report.type.label,
indicatorLines: Self.indicatorLines(for: report.indicators)
)
var collected = ""
do {
let stream = await AIRuntime.shared.generate(
prompt: prompt, maxTokens: 200, priority: .background)
for try await chunk in stream { collected += chunk.text }
} catch {
return // (CancellationError):,
}
let text = HealthExportService.stripThinkBlocks(collected)
.trimmingCharacters(in: .whitespacesAndNewlines)
guard !text.isEmpty, (report.summary ?? "").isEmpty else { return }
report.summary = text
try? ctx.save()
}
/// ( range)status;, 15 prompt
static func indicatorLines(for indicators: [Indicator]) -> String {
let sorted = indicators.sorted {
($0.status == .normal ? 1 : 0) < ($1.status == .normal ? 1 : 0)
}
return sorted.prefix(15).map { i in
var line = "\(i.name) \(i.value)"
if !i.unit.isEmpty { line += " \(i.unit)" }
if !i.range.isEmpty { line += "(参考 \(i.range))" }
line += " \(i.status.rawValue)"
return line
}.joined(separator: "\n")
}
}

View File

@@ -0,0 +1,26 @@
import Testing
@testable import
struct InsightPromptsTests {
@Test func reportSummaryPromptCarriesDataAndGuards() {
let p = InsightPrompts.reportPlainSummary(
title: "春季体检", typeLabel: "体检报告",
indicatorLines: "血红蛋白 118 g/L(参考 130-175)low")
#expect(p.contains("春季体检"))
#expect(p.contains("血红蛋白 118"))
#expect(p.contains("/no_think"))
#expect(p.contains("不诊断"))
#expect(!p.contains("患者"))
}
@Test func trendPromptCarriesDataAndGuards() {
let p = InsightPrompts.trendInsight(
title: "空腹血糖", unit: "mmol/L", rangeText: ",参考 3.9-6.1",
dataLines: "2026-05-01 5.2 / 2026-06-01 5.8")
#expect(p.contains("空腹血糖"))
#expect(p.contains("2026-06-01 5.8"))
#expect(p.contains("/no_think"))
#expect(!p.contains("患者"))
}
}