Files
pipelines/app/lib/features/chat/domain/chat_usage_ring.dart
T
alan 293474ac3c Chat usage limit: Claude Code-style banner with auto-continue; drop the approaching-limit banner
- The usage-limit banner now reads 'Out of usage credits' (or the refusal's
  own 'You've hit your 5-hour limit'), 'Auto-resuming at <time>', an
  'Auto-continue when limits reset' checkbox and Try again, keeping the
  account switcher's 'Switch to <account>'.
- Auto-continue (a shared, persisted preference, on by default) hands the
  server a durable scheduled prompt (send_at = reset + 1 min) with an
  idempotent auto-continue-<refusal id> operation id: the refused operator
  message again, or 'continue' when the refused turn had already worked.
  Unchecking unqueues it; Try again unqueues it and sends now.
- Remove the approaching-limit banner, its SnackBar announcements and their
  marks; the send-button usage ring replaces them.
- Claude Code's rate_limit_event overage fields (overageStatus,
  isUsingOverage/overageInUse, overageDisabledReason, overageResetsAt) reach
  clients as usage.account.extra_usage; the ring tooltip and the banner say
  'Extra usage available' or 'Using extra usage'.

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

113 lines
3.7 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';
}
/// "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(' · ');