feat: context fill grade in chat UI

This commit is contained in:
2026-03-21 09:39:25 +01:00
parent 675183152a
commit 32bbf3f204
4 changed files with 93 additions and 1 deletions

View File

@@ -31,6 +31,10 @@ struct StatusBarView: View {
.font(.caption)
.foregroundStyle(.secondary)
if let model = modelManager.currentModel, model.contextLength > 0 {
contextFillView(totalContext: model.contextLength)
}
Spacer()
// GPU memory
@@ -78,4 +82,43 @@ struct StatusBarView: View {
.padding(.vertical, 4)
.background(.bar)
}
@ViewBuilder
private func contextFillView(totalContext: Int) -> some View {
let usedTokens = viewModel.contextUsedTokens
let ratio = viewModel.contextFillRatio
let percent = Int((ratio * 100).rounded())
HStack(spacing: 6) {
Capsule()
.fill(.quaternary)
.frame(width: 48, height: 6)
.overlay(alignment: .leading) {
Capsule()
.fill(contextFillColor(for: ratio))
.frame(width: max(4, 48 * ratio), height: 6)
}
Text("Ctx \(percent)%")
.font(.caption.monospacedDigit())
.foregroundStyle(.secondary)
}
.help("Approximate context usage: \(formatTokenCount(usedTokens)) of \(formatTokenCount(totalContext)) tokens")
}
private func contextFillColor(for ratio: Double) -> Color {
if ratio >= 0.9 { return .red }
if ratio >= 0.7 { return .orange }
return .blue
}
private func formatTokenCount(_ count: Int) -> String {
if count >= 1_000_000 {
return String(format: "%.1fM", Double(count) / 1_000_000)
}
if count >= 1_000 {
return String(format: "%.1fk", Double(count) / 1_000)
}
return "\(count)"
}
}