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

227 lines
6.9 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import '../../../core/error_reason.dart';
import '../../../ui/adaptive/app_popups.dart';
import '../../../ui/layout/app_sheet_header.dart';
import '../domain/chat_claude_accounts.dart';
import 'chat_claude_accounts_controller.dart';
/// Switches the chat's Claude home to the saved account [account] and says
/// so in a snack bar; a refusal is shown the same way. [messenger] is taken
/// before any sheet closes, so the confirmation survives it.
Future<void> switchChatClaudeAccount(
ScaffoldMessengerState? messenger,
ChatClaudeAccountsController accounts,
ChatClaudeAccount account,
) async {
try {
final active = await accounts.use(account.name);
messenger?.showSnackBar(
SnackBar(content: Text(chatClaudeSwitchedMessage(active ?? account))),
);
} catch (error) {
_failed(messenger, error, 'The Claude account was not switched.');
}
}
void _failed(ScaffoldMessengerState? messenger, Object error, String fallback) {
messenger?.showSnackBar(
SnackBar(content: Text(describeErrorReason(error, fallback: 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
/// a phone.
Future<void> showChatClaudeAccountsDialog(
BuildContext context,
ChatClaudeAccountsController accounts,
) {
unawaited(accounts.refresh());
final messenger = ScaffoldMessenger.maybeOf(context);
return showAppDetails<void>(
context,
maxWidth: 440,
builder: (sheetContext) => Column(
key: const Key('chat-claude-accounts'),
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const AppSheetHeader(title: 'Claude account'),
ListenableBuilder(
listenable: accounts,
builder: (context, _) => _AccountList(
accounts: accounts,
onSwitch: (account) {
Navigator.of(sheetContext).pop();
unawaited(switchChatClaudeAccount(messenger, accounts, account));
},
onSave: () async {
final name = await _askName(sheetContext, accounts.accounts);
if (name == null) return;
try {
await accounts.save(name);
} catch (error) {
_failed(messenger, error, 'The account was not saved.');
}
},
),
),
const SizedBox(height: 8),
],
),
);
}
class _AccountList extends StatelessWidget {
const _AccountList({
required this.accounts,
required this.onSwitch,
required this.onSave,
});
final ChatClaudeAccountsController accounts;
final ValueChanged<ChatClaudeAccount> onSwitch;
final VoidCallback onSave;
@override
Widget build(BuildContext context) {
final list = accounts.accounts;
final error = accounts.error;
if (list == null) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 8),
child: error == null
? const LinearProgressIndicator()
: Text(
describeErrorReason(
error,
fallback: 'Claude accounts are not available.',
),
key: const Key('chat-claude-accounts-error'),
),
);
}
final unsaved = list.canSaveActive ? list.active : null;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (list.accounts.isEmpty && unsaved == null)
const ListTile(title: Text('No saved accounts')),
for (final account in list.accounts)
_AccountTile(
account: account,
onTap: account.active || accounts.busy
? null
: () => onSwitch(account),
),
if (unsaved != null)
_AccountTile(account: unsaved, onTap: null, note: 'Not saved'),
if (accounts.busy) const LinearProgressIndicator(),
const Divider(),
ListTile(
key: const Key('chat-claude-account-save'),
leading: const Icon(Icons.bookmark_add_outlined),
title: const Text('Save current account…'),
enabled: list.active != null && !accounts.busy,
onTap: onSave,
),
],
);
}
}
class _AccountTile extends StatelessWidget {
const _AccountTile({required this.account, required this.onTap, this.note});
final ChatClaudeAccount account;
final VoidCallback? onTap;
final String? note;
@override
Widget build(BuildContext context) {
final detail = [
if (account.name != account.label) account.name,
if (account.organization.isNotEmpty) account.organization,
if (account.plan.isNotEmpty) account.plan,
?note,
].join(' · ');
return ListTile(
key: Key('chat-claude-account-${account.name}'),
leading: const Icon(Icons.account_circle_outlined),
title: Text(account.label),
subtitle: detail.isEmpty ? null : Text(detail),
trailing: account.active
? Icon(
Icons.check,
color: Theme.of(context).colorScheme.primary,
semanticLabel: 'Active',
)
: null,
selected: account.active,
// An active row is informative, not disabled-looking.
enabled: onTap != null || account.active,
onTap: onTap,
);
}
}
/// Asks for the saved account's name, prefilled with the login's email.
Future<String?> _askName(BuildContext context, ChatClaudeAccounts? accounts) =>
showDialog<String>(
context: context,
builder: (context) => _NameDialog(initial: accounts?.active?.email ?? ''),
);
class _NameDialog extends StatefulWidget {
const _NameDialog({required this.initial});
final String initial;
@override
State<_NameDialog> createState() => _NameDialogState();
}
class _NameDialogState extends State<_NameDialog> {
late final TextEditingController _field = TextEditingController(
text: widget.initial,
);
@override
void dispose() {
_field.dispose();
super.dispose();
}
void _save() => Navigator.of(context).pop(_field.text.trim());
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Save current account'),
content: TextField(
key: const Key('chat-claude-account-name'),
controller: _field,
autofocus: true,
decoration: const InputDecoration(labelText: 'Name'),
onSubmitted: (_) => _save(),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
FilledButton(
key: const Key('chat-claude-account-name-save'),
onPressed: _save,
child: const Text('Save'),
),
],
);
}
}