App: Claude account auto-switch setting; the limit banner leaves a refusal the server is continuing alone

The Claude account dialog shows 'Auto-switch account when usage runs out'
(claude_account_auto_switch) when the server reports it. The usage-limit
banner schedules no auto-continue while the server's auto-switch continue
prompt is queued. A chat refused after another chat already switched the
Claude home gets a fresh switch stamp, so its banner clears too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 00:31:31 +00:00
parent ed9072b79a
commit 18e0e565ca
10 changed files with 386 additions and 11 deletions
@@ -33,8 +33,9 @@ void _failed(ScaffoldMessengerState? messenger, Object error, String fallback) {
}
/// The chat menu's "Claude account": the saved accounts of the chat's Claude
/// home with the active one checked (one tap switches), and "Save current
/// account…" for the active login. A dialog on a wide app, a bottom sheet on
/// home with the active one checked (one tap switches), "Save current
/// account…" for the active login and the home's "Auto-switch account when
/// usage runs out" choice. A dialog on a wide app, a bottom sheet on
/// a phone.
Future<void> showChatClaudeAccountsDialog(
BuildContext context,
@@ -68,6 +69,13 @@ Future<void> showChatClaudeAccountsDialog(
_failed(messenger, error, 'The account was not saved.');
}
},
onAutoSwitch: (on) async {
try {
await accounts.setAutoSwitch(on);
} catch (error) {
_failed(messenger, error, 'The setting was not changed.');
}
},
),
),
const SizedBox(height: 8),
@@ -81,11 +89,13 @@ class _AccountList extends StatelessWidget {
required this.accounts,
required this.onSwitch,
required this.onSave,
required this.onAutoSwitch,
});
final ChatClaudeAccountsController accounts;
final ValueChanged<ChatClaudeAccount> onSwitch;
final VoidCallback onSave;
final ValueChanged<bool> onAutoSwitch;
@override
Widget build(BuildContext context) {
@@ -130,6 +140,19 @@ class _AccountList extends StatelessWidget {
enabled: list.active != null && !accounts.busy,
onTap: onSave,
),
if (list.autoSwitch case final on?)
CheckboxListTile(
key: const Key('chat-claude-account-auto-switch'),
value: on,
onChanged: accounts.busy
? null
: (value) => onAutoSwitch(value ?? false),
title: const Text('Auto-switch account when usage runs out'),
subtitle: const Text(
'Continues on a saved account with usage left, after asking '
'the agent to check it is on track',
),
),
],
);
}
@@ -7,7 +7,8 @@ import '../domain/chat_claude_accounts.dart';
import 'chat_thread_controller.dart';
/// One Claude chat's saved accounts: loads `claude_accounts` once on demand,
/// switches with `claude_account_use` and saves with `claude_account_save`.
/// switches with `claude_account_use`, saves with `claude_account_save` and
/// sets automatic switching with `claude_account_auto_switch`.
/// A switch hands the new account's quota to [thread], so its usage-limit
/// banner reflects the new account at once (the server also pushes it to
/// every open chat on the same Claude home).
@@ -59,12 +60,25 @@ class ChatClaudeAccountsController extends ChangeNotifier {
/// Saves the active login as [name] ('' uses its email).
Future<void> save(String name) => _run('claude_account_save', name);
Future<ChatClaudeAccounts?> _run(String type, String? name) async {
/// Turns automatic switching (to another saved account when usage runs
/// out) on or off for the chat's Claude home.
Future<void> setAutoSwitch(bool on) =>
_run('claude_account_auto_switch', null, autoSwitch: on);
Future<ChatClaudeAccounts?> _run(
String type,
String? name, {
bool? autoSwitch,
}) async {
_busy = type != 'claude_accounts';
_notify();
try {
final reply = await gateway.call(type, {
'claude_account': {'session_id': sessionId, 'name': ?name},
'claude_account': {
'session_id': sessionId,
'name': ?name,
'auto_switch': ?autoSwitch,
},
});
final accounts = ChatClaudeAccounts.fromJson(reply['claude_accounts']);
if (accounts == null) {
@@ -25,7 +25,9 @@ import 'chat_thread_controller.dart';
/// already worked on it. The server holds it durably, so it is sent with the
/// app closed or the server restarted. Unchecking takes it back out of the
/// server's queue; Try again takes it out and sends now. The banner hides
/// once the reset passes.
/// once the reset passes, or once the account is switched (by the operator
/// or by the server's automatic switch, whose queued continue prompt also
/// stops auto-continue from being scheduled).
class ChatUsageLimitBanner extends StatefulWidget {
ChatUsageLimitBanner({
super.key,
@@ -156,7 +158,13 @@ class _ChatUsageLimitBannerState extends State<ChatUsageLimitBanner> {
final refused = chatRefusedTurn(thread.messages, thread.accountUsage);
final pending = _pending(now);
final auto = widget.autoContinue;
if (auto.loaded && auto.enabled && refused != null && pending.isEmpty) {
// The server already switched accounts and is continuing the chat.
final switching = chatAutoSwitchPending(thread.queuedPrompts);
if (auto.loaded &&
auto.enabled &&
refused != null &&
pending.isEmpty &&
!switching) {
_arm(refused, limit);
}
return _banner(context, limit, refused, pending, now);
@@ -53,6 +53,7 @@ class ChatClaudeAccounts {
required this.active,
required this.accounts,
this.usage,
this.autoSwitch,
});
final String configDir;
@@ -66,6 +67,11 @@ class ChatClaudeAccounts {
/// After a switch, the new account's quota as the server knows it.
final ChatAccountUsage? usage;
/// Whether the server switches this Claude home to another saved account
/// with usage left, and continues the chat, when usage runs out; null from
/// a server that does not switch automatically.
final bool? autoSwitch;
/// Saved accounts other than the active one: the switch targets.
List<ChatClaudeAccount> get others => [
for (final account in accounts)
@@ -86,6 +92,10 @@ class ChatClaudeAccounts {
for (final entry in raw) ?ChatClaudeAccount.fromJson(entry),
],
usage: chatAccountUsage(value['usage']),
autoSwitch: switch (value['auto_switch']) {
final bool on => on,
_ => null,
},
);
}
}
@@ -20,6 +20,18 @@ const String chatAutoContinueText = 'continue';
/// auto-continue can find them in the server's queue.
const String chatAutoContinuePrefix = 'auto-continue-';
/// The operation id prefix of the prompt the server queues after it switched
/// a refused chat to another Claude account (`auto-switch-<refusal id>`);
/// while one waits, the chat is already being continued.
const String chatAutoSwitchPrefix = 'auto-switch-';
/// Whether the server's queue for the chat holds its automatic-switch
/// continue prompt.
bool chatAutoSwitchPending(Iterable<JsonMap> queued) => queued.any(
(prompt) =>
'${prompt['operation_id'] ?? ''}'.startsWith(chatAutoSwitchPrefix),
);
class ChatUsageLimit {
const ChatUsageLimit({required this.resetsAt});