Files
pipelines/app/lib/features/chat/domain/chat_usage_ring.dart
T
alan 0dc805ae0e Chat: show the active Claude account in the send tooltip, menu and limit banner
usage.account now carries the email and organization of the account
Claude Code is signed in to (from its initialize account, or the saved
account just switched to), sent at process start and after a switch.
The app shows it as the send tooltip's usage prefix, the Claude account
menu subtitle, the usage-limit banner's status line and the info sheet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 16:33:46 +00:00

129 lines
4.3 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// What the composer's send-button usage ring shows: the harness account's
/// 5-hour window (the most constrained short window harnesses report), or the
/// most used window when there is no 5-hour one, and how loudly to draw it.
/// Only reported windows count; one whose reset has passed is stale and left
/// out, and nothing is estimated.
library;
import 'chat_account_usage.dart';
/// The percentages at which the ring turns from quiet to a warning, then
/// urgent.
const int chatQuotaWarningLevel = 80;
const int chatQuotaUrgentLevel = 95;
/// How the ring is drawn: barely visible, a warning from the first warning
/// level (80%), urgent from [chatQuotaUrgentLevel] (95%).
enum ChatUsageRingTone { quiet, warning, urgent }
class ChatUsageRing {
const ChatUsageRing({
required this.shown,
required this.windows,
this.extraUsage,
});
/// The window the ring's arc measures.
final ChatQuotaWindow shown;
/// Every live window, in the harness's order, for the tooltip.
final List<ChatQuotaWindow> windows;
/// "Extra usage available" or "Using extra usage" ([chatExtraUsageNote]).
final String? extraUsage;
/// The arc's share of the circle, 0–1 (past a cap stays full).
double get fraction => (shown.usedPercent / 100).clamp(0.0, 1.0);
ChatUsageRingTone get tone {
final percent = shown.usedPercent;
if (percent >= chatQuotaUrgentLevel) return ChatUsageRingTone.urgent;
if (percent >= chatQuotaWarningLevel) return ChatUsageRingTone.warning;
return ChatUsageRingTone.quiet;
}
}
bool _fiveHour(ChatQuotaWindow window) =>
window.id == 'five_hour' || window.label.toLowerCase() == '5-hour';
/// The ring for [usage] at [now], or null when the harness reports no quota
/// (unknown, unsupported, or every window already reset).
ChatUsageRing? chatUsageRing(ChatAccountUsage? usage, DateTime now) {
if (usage == null || usage.unsupported) return null;
final at = now.millisecondsSinceEpoch;
final live = [
for (final window in usage.windows)
if (window.resetsAt <= 0 || window.resetsAt > at) window,
];
if (live.isEmpty) return null;
var shown = live.firstWhere(_fiveHour, orElse: () => live.first);
if (!_fiveHour(shown)) {
for (final window in live) {
if (window.usedPercent > shown.usedPercent) shown = window;
}
}
return ChatUsageRing(
shown: shown,
windows: List.unmodifiable(live),
extraUsage: chatExtraUsageNote(usage),
);
}
const List<String> _weekdays = [
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat',
'Sun',
];
String _two(int value) => value.toString().padLeft(2, '0');
/// A reset time in local 24-hour time: "17:20" today, "Thu 09:00" within the
/// coming week, "28 Nov 09:00" further out.
String chatUsageRingResetTime(DateTime at, DateTime now) {
final local = at.toLocal();
final today = now.toLocal();
final time = '${_two(local.hour)}:${_two(local.minute)}';
final day = DateTime(local.year, local.month, local.day);
final days = day.difference(DateTime(today.year, today.month, today.day));
if (days.inHours.abs() < 12) return time;
if (!days.isNegative && days.inDays < 7) {
return '${_weekdays[local.weekday - 1]} $time';
}
const months = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', //
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
];
return '${local.day} ${months[local.month - 1]} $time';
}
/// The send button's tooltip: [label] ("Send"), then the account the chat
/// runs on and its usage: "sam@example.com · 5-hour 42% · resets 17:20 · …".
/// Either part may be missing; with neither the tooltip is the label alone.
String chatSendTooltip(
String label,
ChatAccountUsage? usage,
ChatUsageRing? ring,
DateTime now,
) {
final line = [
if (usage?.email case final email? when email.isNotEmpty) email,
if (ring != null) chatUsageRingSummary(ring, now),
].join(' · ');
return line.isEmpty ? label : '$label\n$line';
}
/// "5-hour 42% · resets 17:20 · Weekly 61% · resets Thu 09:00", then any
/// extra usage note.
String chatUsageRingSummary(ChatUsageRing ring, DateTime now) => [
for (final window in ring.windows) ...[
'${window.label} ${window.usedPercent.round()}%',
if (window.resetsAt > 0)
'resets ${chatUsageRingResetTime(DateTime.fromMillisecondsSinceEpoch(window.resetsAt), now)}',
],
?ring.extraUsage,
].join(' · ');