Files
kangkang/康康/Features/Me/ModelSelfTestView.swift
link2026 30f75dc2cd ```
feat(DiaryQuickSheet): 添加AI追问问答功能和底部协作入口

- 新增currentAnswer状态管理追问输入,添加answerFocused状态独立处理键盘避让
- 移除键盘工具条的关心条,将AI协作入口固定到底部按钮
- 添加完整的问答式追问卡片组件,支持自由回答输入和加入日记功能
- 修改prompt阶段行为,不再在正文区显示邀请横幅
- 更新recordCurrent为answerCurrent,实现问题+答案一同加入日记的逻辑
- 调整底部操作栏布局,间距和内边距优化

refactor(InferenceSettingsView): 性能自检改为内联展开模式

- 将性能自检视图从导航链接改为当前页就地展开
- 添加showSelfTest状态控制展开收起动画
- 支持ModelSelfTestView内联嵌入模式,去除外层导航和背景

chore(Localizable): 同步更新本地化字符串资源

- 添加新的UI文本:加入日记、在这儿写下你的回答、康康帮你一起填等
- 修复部分字符串位置调整和翻译映射问题
- 同步更新多语言版本的翻译内容

style(RootView): 优化记一笔标签页视觉设计

- 为记一笔标签添加语音识别角标标识
- 使用麦克风图标配合加号突出长按语音直达功能
```
2026-06-17 10:05:32 +08:00

213 lines
7.5 KiB
Swift

import SwiftUI
/// : prompt,(MNN·SME2 / MNN·NEON / MLX·GPU)
/// prefill / decode , (§12 2/6)
struct ModelSelfTestView: View {
@State private var output = ""
@State private var phase: Phase = .idle
@State private var rate: Double = 0
@State private var lastResult: BenchmarkResult?
@State private var history: [String: BenchmarkResult] = [:]
private enum Phase: Equatable {
case idle, loading, running, done, failed(String)
var label: String {
switch self {
case .idle: return String(appLoc: "未开始")
case .loading: return String(appLoc: "加载模型…")
case .running: return String(appLoc: "推理中…")
case .done: return String(appLoc: "完成 ✓")
case .failed(let m): return String(appLoc: "失败:\(m)")
}
}
}
private var isBusy: Bool { phase == .loading || phase == .running }
private var statusColor: Color {
switch phase {
case .failed: return Tj.Palette.brick
case .done: return Tj.Palette.leaf
default: return Tj.Palette.text2
}
}
/// :, ScrollView / /
var embedded = false
var body: some View {
if embedded {
content
} else {
ScrollView {
content.padding(16)
}
.background(Tj.Palette.sand.ignoresSafeArea())
.navigationTitle("性能自检")
.navigationBarTitleDisplayMode(.inline)
}
}
private var content: some View {
VStack(alignment: .leading, spacing: 16) {
promptCard
HStack {
Text(phase.label)
.font(.tjScaled( 13, weight: .medium))
.foregroundStyle(statusColor)
.lineLimit(1)
Spacer()
if rate > 0 {
Text(String(format: "%.1f tok/s", rate))
.font(.tjScaled( 12, design: .monospaced))
.foregroundStyle(Tj.Palette.text3)
}
}
Button {
Task { await run() }
} label: {
Text(isBusy ? "运行中…" : "运行性能自检").frame(maxWidth: .infinity)
}
.buttonStyle(TjPrimaryButton())
.disabled(isBusy)
if isBusy { AIFlowBar() }
if let r = lastResult { statsCard(r) }
outputCard
if !history.isEmpty { historyCard }
}
.onAppear { history = BenchmarkService.load() }
}
private var promptCard: some View {
VStack(alignment: .leading, spacing: 6) {
Text("测试 PROMPT")
.font(.tjScaled( 11, weight: .semibold))
.tracking(0.5)
.foregroundStyle(Tj.Palette.text3)
Text(BenchmarkService.fixedPrompt)
.font(.tjScaled( 14))
.foregroundStyle(Tj.Palette.text)
}
.padding(14)
.frame(maxWidth: .infinity, alignment: .leading)
.tjCard()
}
private func statsCard(_ r: BenchmarkResult) -> some View {
VStack(alignment: .leading, spacing: 10) {
HStack {
Text("本次结果")
.font(.tjScaled( 12, weight: .semibold))
.foregroundStyle(Tj.Palette.text2)
Spacer()
TjBadge(text: r.backendLabel, style: .leaf)
}
HStack(spacing: 0) {
metric(String(appLoc: "读入"), r.prefillTokensPerSecond > 0
? String(format: "%.0f tok/s", r.prefillTokensPerSecond) : "")
metric(String(appLoc: "生成"), String(format: "%.1f tok/s", r.decodeTokensPerSecond))
metric(String(appLoc: "总耗时"), String(format: "%.1fs", r.totalSeconds))
}
Text(String(appLoc: "prompt \(r.promptTokens) tok · 生成 \(r.genTokens) tok · 100% 本地"))
.font(.tjScaled( 10, design: .monospaced))
.foregroundStyle(Tj.Palette.text3)
}
.padding(14)
.frame(maxWidth: .infinity, alignment: .leading)
.tjCard()
}
private func metric(_ label: String, _ value: String) -> some View {
VStack(spacing: 3) {
Text(value)
.font(.tjScaled( 15, weight: .semibold, design: .monospaced))
.foregroundStyle(Tj.Palette.text)
Text(label)
.font(.tjScaled( 10))
.foregroundStyle(Tj.Palette.text3)
}
.frame(maxWidth: .infinity)
}
private var outputCard: some View {
ScrollView {
Text(output.isEmpty ? "(暂无输出)" : output)
.font(.system(.footnote, design: .monospaced))
.foregroundStyle(Tj.Palette.text)
.frame(maxWidth: .infinity, alignment: .leading)
.textSelection(.enabled)
.padding(12)
}
.frame(maxHeight: 220)
.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)
)
}
private var historyCard: some View {
VStack(alignment: .leading, spacing: 10) {
Text("各引擎实测对比")
.font(.tjScaled( 12, weight: .semibold))
.foregroundStyle(Tj.Palette.text2)
ForEach(history.keys.sorted(), id: \.self) { key in
if let r = history[key] {
HStack {
Text(key)
.font(.tjScaled( 12, weight: .medium))
.foregroundStyle(Tj.Palette.text)
Spacer()
Text(String(format: String(appLoc: "生成 %.1f tok/s"), r.decodeTokensPerSecond))
.font(.tjScaled( 12, design: .monospaced))
.foregroundStyle(Tj.Palette.leaf)
Text(r.date.formatted(.dateTime.month().day()))
.font(.tjScaled( 10))
.foregroundStyle(Tj.Palette.text3)
}
}
}
Text("在「我的 · 推理引擎」切换引擎后再跑一次,即可对比 SME2 与 GPU。")
.font(.tjScaled( 10))
.foregroundStyle(Tj.Palette.text3)
}
.padding(14)
.frame(maxWidth: .infinity, alignment: .leading)
.tjCard()
}
@MainActor
private func run() async {
output = ""
rate = 0
lastResult = nil
phase = .loading
do {
let result = try await BenchmarkService.shared.run { piece, r in
output += piece
if r > 0 { rate = r }
if phase == .loading { phase = .running }
}
lastResult = result
history = BenchmarkService.load()
phase = .done
} catch {
phase = .failed(error.localizedDescription)
}
}
}
#Preview {
NavigationStack { ModelSelfTestView() }
}