293474ac3c
- 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>
188 lines
6.1 KiB
Dart
188 lines
6.1 KiB
Dart
/// The harness account's subscription quota, as carried by a usage frame's
|
||
/// `usage.account` (see docs/cli.md). Only reported values are kept: a window
|
||
/// without a usable percentage is dropped and nothing is estimated.
|
||
library;
|
||
|
||
/// One rolling quota window, e.g. "5-hour" or "Weekly".
|
||
class ChatQuotaWindow {
|
||
const ChatQuotaWindow({
|
||
required this.id,
|
||
required this.label,
|
||
required this.usedPercent,
|
||
required this.resetsAt,
|
||
});
|
||
|
||
final String id;
|
||
final String label;
|
||
|
||
/// 0–100; a harness may report more than 100 past a cap.
|
||
final double usedPercent;
|
||
|
||
/// Epoch milliseconds, 0 when the harness did not say.
|
||
final int resetsAt;
|
||
}
|
||
|
||
class ChatAccountUsage {
|
||
const ChatAccountUsage({
|
||
required this.harness,
|
||
required this.plan,
|
||
required this.asOf,
|
||
required this.windows,
|
||
required this.unsupported,
|
||
this.switchedAt = 0,
|
||
this.extraUsage,
|
||
});
|
||
|
||
/// Display name of the harness ("Claude Code", "Codex", "OpenCode").
|
||
final String harness;
|
||
final String plan;
|
||
|
||
/// Epoch milliseconds when the server received the report, 0 if unknown.
|
||
final int asOf;
|
||
final List<ChatQuotaWindow> windows;
|
||
|
||
/// The harness exposes no quota at all.
|
||
final bool unsupported;
|
||
|
||
/// Epoch milliseconds when the operator switched the chat's Claude account
|
||
/// to this one, 0 if never. The report then stands for the new account even
|
||
/// before any window is known, and an earlier usage-limit refusal in the
|
||
/// transcript no longer applies.
|
||
final int switchedAt;
|
||
|
||
/// The account's extra usage (paid usage credits past the limits) as the
|
||
/// harness last reported it; null when it never said.
|
||
final ChatExtraUsage? extraUsage;
|
||
}
|
||
|
||
/// Claude Code's extra usage ("overage") state, `usage.account.extra_usage`.
|
||
class ChatExtraUsage {
|
||
const ChatExtraUsage({
|
||
required this.status,
|
||
required this.inUse,
|
||
required this.disabledReason,
|
||
});
|
||
|
||
/// "allowed", "allowed_warning" or "rejected"; empty when unknown.
|
||
final String status;
|
||
|
||
/// Requests are drawing on extra usage now.
|
||
final bool inUse;
|
||
|
||
/// Why extra usage is off, e.g. "out_of_credits"; empty when on.
|
||
final String disabledReason;
|
||
|
||
/// Extra usage would take over when a limit is reached.
|
||
bool get available => status == 'allowed' || status == 'allowed_warning';
|
||
}
|
||
|
||
ChatExtraUsage? _extraUsage(Object? value) {
|
||
if (value is! Map) return null;
|
||
final status = '${value['status'] ?? ''}'.trim();
|
||
final reason = '${value['disabled_reason'] ?? ''}'.trim();
|
||
final inUse = value['in_use'] == true;
|
||
if (status.isEmpty && reason.isEmpty && !inUse) return null;
|
||
return ChatExtraUsage(status: status, inUse: inUse, disabledReason: reason);
|
||
}
|
||
|
||
/// A discreet note on [usage]'s extra usage: "Using extra usage" or "Extra
|
||
/// usage available"; null when it is off or unknown.
|
||
String? chatExtraUsageNote(ChatAccountUsage? usage) {
|
||
final extra = usage?.extraUsage;
|
||
if (extra == null) return null;
|
||
if (extra.inUse) return 'Using extra usage';
|
||
if (extra.available) return 'Extra usage available';
|
||
return null;
|
||
}
|
||
|
||
int _millis(Object? value) =>
|
||
value is num && value.isFinite && value > 0 ? value.toInt() : 0;
|
||
|
||
/// Parses `usage.account`, or null when it reports nothing usable.
|
||
ChatAccountUsage? chatAccountUsage(Object? value) {
|
||
if (value is! Map) return null;
|
||
final harness = '${value['harness'] ?? ''}'.trim();
|
||
if (value['unsupported'] == true) {
|
||
return ChatAccountUsage(
|
||
harness: harness.isEmpty ? 'the harness' : harness,
|
||
plan: '',
|
||
asOf: 0,
|
||
windows: const [],
|
||
unsupported: true,
|
||
);
|
||
}
|
||
final windows = <ChatQuotaWindow>[];
|
||
final raw = value['windows'];
|
||
if (raw is List) {
|
||
for (final entry in raw) {
|
||
if (entry is! Map) continue;
|
||
final percent = entry['used_percent'];
|
||
final label = '${entry['label'] ?? ''}'.trim();
|
||
if (percent is! num || !percent.isFinite || percent < 0) continue;
|
||
if (label.isEmpty) continue;
|
||
windows.add(
|
||
ChatQuotaWindow(
|
||
id: '${entry['id'] ?? label}',
|
||
label: label,
|
||
usedPercent: percent.toDouble(),
|
||
resetsAt: _millis(entry['resets_at']),
|
||
),
|
||
);
|
||
}
|
||
}
|
||
final switchedAt = _millis(value['switched_at']);
|
||
// A switched account replaces the previous report even with no windows
|
||
// yet; otherwise an empty report keeps the last one.
|
||
if (windows.isEmpty && switchedAt == 0) return null;
|
||
return ChatAccountUsage(
|
||
harness: harness,
|
||
plan: '${value['plan'] ?? ''}'.trim(),
|
||
asOf: _millis(value['as_of']),
|
||
windows: List.unmodifiable(windows),
|
||
unsupported: false,
|
||
switchedAt: switchedAt,
|
||
extraUsage: _extraUsage(value['extra_usage']),
|
||
);
|
||
}
|
||
|
||
/// "42% used · resets in 2h 14m". The percentage is rounded, never clamped.
|
||
String chatQuotaSummary(ChatQuotaWindow window, DateTime now) {
|
||
final used = '${window.usedPercent.round()}% used';
|
||
if (window.resetsAt <= 0) return used;
|
||
final left = DateTime.fromMillisecondsSinceEpoch(
|
||
window.resetsAt,
|
||
).difference(now);
|
||
if (left.isNegative) return '$used · reset since this report';
|
||
return '$used · resets in ${_countdown(left)}';
|
||
}
|
||
|
||
String _countdown(Duration left) {
|
||
if (left.inMinutes < 1) return 'under a minute';
|
||
final days = left.inDays;
|
||
final hours = left.inHours % 24;
|
||
final minutes = left.inMinutes % 60;
|
||
if (days > 0) return hours > 0 ? '${days}d ${hours}h' : '${days}d';
|
||
if (left.inHours > 0) {
|
||
return minutes > 0 ? '${left.inHours}h ${minutes}m' : '${left.inHours}h';
|
||
}
|
||
return '${minutes}m';
|
||
}
|
||
|
||
/// "as of 5 min ago"; empty when the report time is unknown.
|
||
String chatQuotaAsOf(int asOf, DateTime now) {
|
||
if (asOf <= 0) return '';
|
||
final age = now.difference(DateTime.fromMillisecondsSinceEpoch(asOf));
|
||
if (age.inMinutes < 1) return 'as of just now';
|
||
if (age.inHours < 1) return 'as of ${age.inMinutes} min ago';
|
||
if (age.inDays < 1) return 'as of ${age.inHours} h ago';
|
||
return 'as of ${age.inDays} d ago';
|
||
}
|
||
|
||
/// A native plan id as a label: "max" -> "Max", "self_serve_business" ->
|
||
/// "Self serve business".
|
||
String chatPlanLabel(String plan) {
|
||
final words = plan.replaceAll('_', ' ').trim();
|
||
if (words.isEmpty) return '';
|
||
return words[0].toUpperCase() + words.substring(1);
|
||
}
|