b6d06219cd
A compact banner above the composer when any quota window of the chat's harness account reaches 80% (error colours from 95%), naming the most constrained window and a second one also at 80%+. Dismissing it holds per harness+window until the next level (80/90/95) or a reset, across chats and reloads; a newly crossed level is announced once with a root SnackBar. The usage-limit banner wins. Fixture: quota=warn80|warn95. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
185 lines
6.1 KiB
Dart
185 lines
6.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../domain/chat_account_usage.dart';
|
|
import '../domain/chat_usage_limit.dart';
|
|
import '../domain/chat_usage_warning.dart';
|
|
import 'chat_schedule_time.dart';
|
|
import 'chat_thread_controller.dart';
|
|
import 'chat_usage_warnings.dart';
|
|
|
|
/// Above the composer while the chat's harness account is nearly out of
|
|
/// quota: "Claude Code 5-hour limit 84% used · resets in 1 h 20 m", in the
|
|
/// error colours from 95%. It names the most constrained window and a second
|
|
/// one also at a warning level. Dismissing it holds until a window reaches the
|
|
/// next level or resets, in every chat of the account ([ChatUsageWarnings]).
|
|
/// The usage-limit banner wins: this one hides while the account is out of
|
|
/// quota. It also hands each new account report to [warnings], which
|
|
/// announces a newly crossed level once.
|
|
class ChatUsageWarningBanner extends StatefulWidget {
|
|
ChatUsageWarningBanner({
|
|
super.key,
|
|
required this.thread,
|
|
ChatUsageWarnings? warnings,
|
|
this.now = DateTime.now,
|
|
}) : warnings = warnings ?? ChatUsageWarnings.shared;
|
|
|
|
final ChatThreadController thread;
|
|
final ChatUsageWarnings warnings;
|
|
final DateTime Function() now;
|
|
|
|
@override
|
|
State<ChatUsageWarningBanner> createState() => _ChatUsageWarningBannerState();
|
|
}
|
|
|
|
class _ChatUsageWarningBannerState extends State<ChatUsageWarningBanner> {
|
|
ChatAccountUsage? _reported;
|
|
|
|
/// Refreshes the countdown and drops a window once its reset passes.
|
|
Timer? _tick;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
unawaited(widget.warnings.load());
|
|
widget.thread.addListener(_report);
|
|
_report();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(ChatUsageWarningBanner oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.thread == widget.thread) return;
|
|
oldWidget.thread.removeListener(_report);
|
|
widget.thread.addListener(_report);
|
|
_report();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
widget.thread.removeListener(_report);
|
|
_tick?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
void _report() {
|
|
final usage = widget.thread.accountUsage;
|
|
if (usage == null || identical(usage, _reported)) return;
|
|
_reported = usage;
|
|
unawaited(widget.warnings.report(usage, now: widget.now()));
|
|
}
|
|
|
|
void _ticking(bool on) {
|
|
if (on == (_tick != null)) return;
|
|
_tick?.cancel();
|
|
_tick = on
|
|
? Timer.periodic(const Duration(seconds: 30), (_) {
|
|
if (mounted) setState(() {});
|
|
})
|
|
: null;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ListenableBuilder(
|
|
listenable: Listenable.merge([widget.thread, widget.warnings]),
|
|
builder: (context, _) {
|
|
final now = widget.now();
|
|
final thread = widget.thread;
|
|
final limited =
|
|
chatThreadUsageLimit(thread.accountUsage, thread.messages, now) !=
|
|
null;
|
|
final warning = limited
|
|
? null
|
|
: chatUsageWarning(
|
|
thread.accountUsage,
|
|
now,
|
|
widget.warnings.dismissed,
|
|
);
|
|
_ticking(warning != null);
|
|
if (warning == null) return const SizedBox.shrink();
|
|
return _banner(context, warning, now);
|
|
},
|
|
);
|
|
}
|
|
|
|
String _resets(ChatQuotaWindow window, DateTime now) {
|
|
if (window.resetsAt <= 0) return '';
|
|
final relative = chatQuotaResetsIn(window, now);
|
|
if (relative != null) return ' · resets $relative';
|
|
final at = DateTime.fromMillisecondsSinceEpoch(window.resetsAt);
|
|
return ' · resets ${chatScheduleTime(context, at, now: () => now)}';
|
|
}
|
|
|
|
Widget _banner(BuildContext context, ChatUsageWarning warning, DateTime now) {
|
|
final scheme = Theme.of(context).colorScheme;
|
|
final text = Theme.of(context).textTheme;
|
|
final background = warning.urgent
|
|
? scheme.errorContainer
|
|
: scheme.secondaryContainer;
|
|
final foreground = warning.urgent
|
|
? scheme.onErrorContainer
|
|
: scheme.onSecondaryContainer;
|
|
final second = warning.second;
|
|
return Padding(
|
|
padding: const EdgeInsetsDirectional.only(bottom: 8),
|
|
child: Material(
|
|
key: const Key('chat-usage-warning'),
|
|
color: background,
|
|
borderRadius: BorderRadius.circular(16),
|
|
child: Padding(
|
|
padding: const EdgeInsetsDirectional.fromSTEB(16, 4, 4, 4),
|
|
child: Row(
|
|
children: [
|
|
Icon(
|
|
warning.urgent ? Icons.error_outline : Icons.data_usage,
|
|
color: foreground,
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 6),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
chatQuotaWarningTitle(
|
|
warning.harness,
|
|
warning.primary,
|
|
) +
|
|
_resets(warning.primary, now),
|
|
key: const Key('chat-usage-warning-status'),
|
|
style: text.bodyMedium?.copyWith(
|
|
color: foreground,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
if (second != null)
|
|
Text(
|
|
'${second.label} limit '
|
|
'${second.usedPercent.round()}% used'
|
|
'${_resets(second, now)}',
|
|
key: const Key('chat-usage-warning-second'),
|
|
style: text.bodySmall?.copyWith(color: foreground),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
IconButton(
|
|
key: const Key('chat-usage-warning-dismiss'),
|
|
tooltip: 'Dismiss',
|
|
color: foreground,
|
|
onPressed: () => widget.warnings.dismiss(warning),
|
|
icon: const Icon(Icons.close),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|