Chat info: account quota per harness (Claude rate_limit_event, Codex account/rateLimits, OpenCode unsupported)
Usage frames carry optional usage.account {harness, source, plan, as_of,
windows[{id,label,used_percent,resets_at}], unsupported}. Claude Code quota
comes from its rate_limit_event stream lines and the initialize account
(plan), shared across chats on the same account. Codex reads
account/rateLimits/read once per connection (re-read when a chat opens after
5 min) and merges account/rateLimits/updated. OpenCode exposes no quota API
and says so. The app's chat info sheet shows an Account usage section with a
progress bar and reset countdown per window, Material and Cupertino.
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../domain/chat_account_usage.dart';
|
||||
|
||||
/// The info sheet's "Account usage" section: one progress bar per quota
|
||||
/// window the harness reported, the plan and the report's age. It draws
|
||||
/// nothing for an unknown quota, and one muted line when the harness has no
|
||||
/// quota API. Reset countdowns tick while the sheet is open.
|
||||
class ChatAccountUsageSection extends StatefulWidget {
|
||||
const ChatAccountUsageSection({
|
||||
super.key,
|
||||
required this.usage,
|
||||
required this.cupertino,
|
||||
this.now = DateTime.now,
|
||||
});
|
||||
|
||||
final ChatAccountUsage? usage;
|
||||
final bool cupertino;
|
||||
final DateTime Function() now;
|
||||
|
||||
@override
|
||||
State<ChatAccountUsageSection> createState() =>
|
||||
_ChatAccountUsageSectionState();
|
||||
}
|
||||
|
||||
class _ChatAccountUsageSectionState extends State<ChatAccountUsageSection> {
|
||||
Timer? _tick;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_tick = Timer.periodic(const Duration(seconds: 30), (_) {
|
||||
if (mounted) setState(() {});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_tick?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final usage = widget.usage;
|
||||
if (usage == null) return const SizedBox.shrink();
|
||||
final cupertino = widget.cupertino;
|
||||
final now = widget.now();
|
||||
final secondary = cupertino
|
||||
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.onSurfaceVariant;
|
||||
final body = cupertino
|
||||
? CupertinoTheme.of(context).textTheme.textStyle
|
||||
: Theme.of(context).textTheme.bodyMedium;
|
||||
final title = cupertino
|
||||
? CupertinoTheme.of(
|
||||
context,
|
||||
).textTheme.textStyle.copyWith(fontWeight: FontWeight.w600)
|
||||
: Theme.of(context).textTheme.titleSmall;
|
||||
final muted = body?.copyWith(color: secondary);
|
||||
final asOf = chatQuotaAsOf(usage.asOf, now);
|
||||
final plan = chatPlanLabel(usage.plan);
|
||||
return Padding(
|
||||
key: const Key('chat-account-usage'),
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(24, 12, 24, 6),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text('Account usage', style: title),
|
||||
if (usage.unsupported)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 4),
|
||||
child: Text(
|
||||
'Not reported by ${usage.harness}',
|
||||
key: const Key('chat-account-usage-unsupported'),
|
||||
style: muted,
|
||||
),
|
||||
)
|
||||
else ...[
|
||||
if (plan.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 4),
|
||||
child: Text(
|
||||
usage.harness.isEmpty
|
||||
? '$plan plan'
|
||||
: '${usage.harness} · $plan plan',
|
||||
key: const Key('chat-account-usage-plan'),
|
||||
style: muted,
|
||||
),
|
||||
),
|
||||
for (final window in usage.windows)
|
||||
_QuotaWindowRow(
|
||||
window: window,
|
||||
now: now,
|
||||
cupertino: cupertino,
|
||||
body: body,
|
||||
muted: muted,
|
||||
),
|
||||
if (asOf.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 6),
|
||||
child: Text(
|
||||
asOf,
|
||||
key: const Key('chat-account-usage-as-of'),
|
||||
style: muted,
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _QuotaWindowRow extends StatelessWidget {
|
||||
const _QuotaWindowRow({
|
||||
required this.window,
|
||||
required this.now,
|
||||
required this.cupertino,
|
||||
required this.body,
|
||||
required this.muted,
|
||||
});
|
||||
|
||||
final ChatQuotaWindow window;
|
||||
final DateTime now;
|
||||
final bool cupertino;
|
||||
final TextStyle? body;
|
||||
final TextStyle? muted;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final summary = chatQuotaSummary(window, now);
|
||||
final fraction = (window.usedPercent / 100).clamp(0.0, 1.0);
|
||||
final high = window.usedPercent >= 90;
|
||||
// Cupertino has no linear progress control; the standard indicator is
|
||||
// drawn with the system fill and tint colors, like UIProgressView.
|
||||
final bar = cupertino
|
||||
? LinearProgressIndicator(
|
||||
value: fraction,
|
||||
backgroundColor: CupertinoColors.systemFill.resolveFrom(context),
|
||||
color:
|
||||
(high ? CupertinoColors.systemRed : CupertinoColors.activeBlue)
|
||||
.resolveFrom(context),
|
||||
borderRadius: BorderRadius.circular(2),
|
||||
semanticsLabel: '${window.label} quota',
|
||||
)
|
||||
: LinearProgressIndicator(
|
||||
value: fraction,
|
||||
color: high ? Theme.of(context).colorScheme.error : null,
|
||||
borderRadius: BorderRadius.circular(2),
|
||||
semanticsLabel: '${window.label} quota',
|
||||
);
|
||||
return Padding(
|
||||
key: Key('chat-account-window-${window.id}'),
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(window.label, style: body),
|
||||
Padding(padding: const EdgeInsets.symmetric(vertical: 4), child: bar),
|
||||
Text(
|
||||
summary,
|
||||
key: Key('chat-account-window-summary-${window.id}'),
|
||||
style: muted,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../data/chat_repository.dart';
|
||||
import '../domain/chat_account_usage.dart';
|
||||
import '../domain/chat_sessions.dart';
|
||||
import '../domain/chat_transcript.dart';
|
||||
import 'chat_server_queue.dart';
|
||||
@@ -73,6 +74,7 @@ class ChatThreadController extends ChangeNotifier {
|
||||
String? _liveAssistantMessageId;
|
||||
String? _awaitingResponseOperation;
|
||||
JsonMap? _usage;
|
||||
ChatAccountUsage? _accountUsage;
|
||||
String? _error;
|
||||
|
||||
bool _subscribed = false;
|
||||
@@ -147,6 +149,10 @@ class ChatThreadController extends ChangeNotifier {
|
||||
/// Unknown fields stay null, so a viewer can distinguish them from zero.
|
||||
JsonMap? get usage => _usage;
|
||||
|
||||
/// The harness account's latest reported quota, or null while unknown. It
|
||||
/// is account state: a frame without one keeps the last known value.
|
||||
ChatAccountUsage? get accountUsage => _accountUsage;
|
||||
|
||||
String? get error => _error;
|
||||
bool get subscribed => _subscribed;
|
||||
String? get subscriptionError => _subscriptionError;
|
||||
@@ -348,8 +354,14 @@ class ChatThreadController extends ChangeNotifier {
|
||||
// Usage replaces the last snapshot and never adds a message, runs a turn
|
||||
// or admits a queued prompt. An unusable snapshot keeps the previous
|
||||
// recorded values instead of replacing them with fabricated zeros.
|
||||
final usage = chatUsageSnapshot(body['usage']);
|
||||
if (usage == null || !_reportedUsage(usage)) return;
|
||||
final raw = body['usage'];
|
||||
final account = chatAccountUsage(raw is Map ? raw['account'] : null);
|
||||
if (account != null) _accountUsage = account;
|
||||
final usage = chatUsageSnapshot(raw);
|
||||
if (usage == null || !_reportedUsage(usage)) {
|
||||
if (account != null) _notify(transcript: false);
|
||||
return;
|
||||
}
|
||||
_usage = usage;
|
||||
_notify(transcript: false);
|
||||
return;
|
||||
|
||||
@@ -17,6 +17,7 @@ import '../ui/sessions/session_actions.dart';
|
||||
import '../ui/transcript/chat_markdown_view.dart';
|
||||
import '../ui/transcript/chat_message_view.dart';
|
||||
import '../ui/transcript/chat_injected_rows.dart';
|
||||
import 'chat_account_usage_section.dart';
|
||||
import 'chat_background_tasks.dart';
|
||||
import 'chat_command_actions.dart';
|
||||
import 'chat_composer_bar.dart';
|
||||
@@ -1530,6 +1531,10 @@ class _ChatInfoSheet extends StatelessWidget {
|
||||
),
|
||||
for (final metric in _chatInfoMetrics(controller.usage))
|
||||
_ChatInfoRow(metric: metric, cupertino: cupertino),
|
||||
ChatAccountUsageSection(
|
||||
usage: controller.accountUsage,
|
||||
cupertino: cupertino,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(24, 8, 24, 16),
|
||||
child: Text(
|
||||
|
||||
Reference in New Issue
Block a user