Files
pipelines/app/lib/features/chat/domain/chat_claude_accounts.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

97 lines
2.8 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,
});
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;
/// 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']),
);
}
}
/// The confirmation shown after a switch.
String chatClaudeSwitchedMessage(ChatClaudeAccount? account) =>
'Switched to ${account?.label ?? 'the saved account'} — send a message '
'to continue';