18e0e565ca
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>
107 lines
3.2 KiB
Dart
107 lines
3.2 KiB
Dart
/// The saved Claude accounts of the Claude home a chat's directory selects
|
|
/// (its CLAUDE_CONFIG_DIR), as the server's `claude_accounts` reply reports
|
|
/// them. Replies describe accounts only; they never carry a token.
|
|
library;
|
|
|
|
import 'chat_account_usage.dart';
|
|
|
|
/// One Claude login.
|
|
class ChatClaudeAccount {
|
|
const ChatClaudeAccount({
|
|
required this.name,
|
|
required this.email,
|
|
required this.organization,
|
|
required this.plan,
|
|
required this.active,
|
|
required this.saved,
|
|
});
|
|
|
|
final String name;
|
|
final String email;
|
|
final String organization;
|
|
final String plan;
|
|
|
|
/// The Claude home is signed in with this account.
|
|
final bool active;
|
|
|
|
/// False for an active login with no saved slot yet.
|
|
final bool saved;
|
|
|
|
/// What the operator calls the account: its email, else its name.
|
|
String get label => email.isNotEmpty ? email : name;
|
|
|
|
static ChatClaudeAccount? fromJson(Object? value) {
|
|
if (value is! Map) return null;
|
|
final name = '${value['name'] ?? ''}'.trim();
|
|
final email = '${value['email'] ?? ''}'.trim();
|
|
if (name.isEmpty && email.isEmpty) return null;
|
|
return ChatClaudeAccount(
|
|
name: name.isEmpty ? email : name,
|
|
email: email,
|
|
organization: '${value['organization'] ?? ''}'.trim(),
|
|
plan: '${value['plan'] ?? ''}'.trim(),
|
|
active: value['active'] == true,
|
|
saved: value['saved'] == true,
|
|
);
|
|
}
|
|
}
|
|
|
|
/// A `claude_accounts` reply.
|
|
class ChatClaudeAccounts {
|
|
const ChatClaudeAccounts({
|
|
required this.configDir,
|
|
required this.active,
|
|
required this.accounts,
|
|
this.usage,
|
|
this.autoSwitch,
|
|
});
|
|
|
|
final String configDir;
|
|
|
|
/// The current login, saved or not; null when signed out.
|
|
final ChatClaudeAccount? active;
|
|
|
|
/// The saved accounts.
|
|
final List<ChatClaudeAccount> accounts;
|
|
|
|
/// 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)
|
|
if (!account.active) account,
|
|
];
|
|
|
|
/// Whether the active login can be saved (it is not saved yet).
|
|
bool get canSaveActive => active != null && !active!.saved;
|
|
|
|
static ChatClaudeAccounts? fromJson(Object? value) {
|
|
if (value is! Map) return null;
|
|
final raw = value['accounts'];
|
|
return ChatClaudeAccounts(
|
|
configDir: '${value['config_dir'] ?? ''}',
|
|
active: ChatClaudeAccount.fromJson(value['active']),
|
|
accounts: [
|
|
if (raw is List)
|
|
for (final entry in raw) ?ChatClaudeAccount.fromJson(entry),
|
|
],
|
|
usage: chatAccountUsage(value['usage']),
|
|
autoSwitch: switch (value['auto_switch']) {
|
|
final bool on => on,
|
|
_ => null,
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
/// The confirmation shown after a switch.
|
|
String chatClaudeSwitchedMessage(ChatClaudeAccount? account) =>
|
|
'Switched to ${account?.label ?? 'the saved account'} — send a message '
|
|
'to continue';
|