Files
pipelines/app/lib/features/chat/conversation/chat_usage_warning_banner.dart
T
alan c490f71042 Claude account switcher: save and switch a chat's Claude login in place
Saved accounts live under <CLAUDE_CONFIG_DIR>/pipelines-accounts/<slug>/
(0700 dir, 0600 files). Switching changes what /login changes: the
claudeAiOauth entry of .credentials.json and oauthAccount of .claude.json
(atomic temp+rename, mode and other keys kept) and the org caches /login
drops, under Claude Code's own refresh, storage and config locks, so
running CLIs continue on the new account without a restart. The active
login's rotated tokens are written back to its slot first.

Server: claude_accounts / claude_account_save / _use / _remove, chat-scoped
and Claude-only; a switch moves the home's open chats to the new account's
quota (usage.account.switched_at), clearing a reached limit.
CLI: pipelines claude-account list|save|use|remove [--dir].
App: Claude account in the chat menu, and Switch to <account> on the
usage-limit and usage-warning banners, with a confirmation snack.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 15:10:08 +00:00

199 lines
6.7 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_claude_account_switch_action.dart';
import 'chat_claude_accounts_controller.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.accounts,
this.now = DateTime.now,
}) : warnings = warnings ?? ChatUsageWarnings.shared;
final ChatThreadController thread;
final ChatUsageWarnings warnings;
/// The chat's saved Claude accounts, for "Switch to (other account)";
/// null for a chat that is not Claude Code's.
final ChatClaudeAccountsController? accounts;
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),
),
],
),
),
),
if (widget.accounts case final accounts?)
Flexible(
child: ChatClaudeAccountSwitchAction(
accounts: accounts,
foreground: foreground,
),
),
IconButton(
key: const Key('chat-usage-warning-dismiss'),
tooltip: 'Dismiss',
color: foreground,
onPressed: () => widget.warnings.dismiss(warning),
icon: const Icon(Icons.close),
),
],
),
),
),
);
}
}