/// 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, }); final String configDir; /// The current login, saved or not; null when signed out. final ChatClaudeAccount? active; /// The saved accounts. final List accounts; /// After a switch, the new account's quota as the server knows it. final ChatAccountUsage? usage; /// Saved accounts other than the active one: the switch targets. List 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']), ); } } /// The confirmation shown after a switch. String chatClaudeSwitchedMessage(ChatClaudeAccount? account) => 'Switched to ${account?.label ?? 'the saved account'} — send a message ' 'to continue';