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

95 lines
2.7 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import '../../../core/api_gateway.dart';
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`.
/// 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).
class ChatClaudeAccountsController extends ChangeNotifier {
ChatClaudeAccountsController({
required this.gateway,
required this.sessionId,
this.thread,
});
final ApiGateway gateway;
final String sessionId;
final ChatThreadController? thread;
ChatClaudeAccounts? _accounts;
String? _error;
Future<void>? _loading;
bool _busy = false;
bool _disposed = false;
/// The last reply, or null before one arrived.
ChatClaudeAccounts? get accounts => _accounts;
/// The last refusal (for example a chat directory with no Claude home).
String? get error => _error;
/// A switch or save is in flight.
bool get busy => _busy;
/// Loads the accounts once; later calls reuse the first load.
Future<void> ensureLoaded() => _loading ??= refresh();
Future<void> refresh() async {
try {
await _run('claude_accounts', null);
} catch (_) {
// Recorded in [error].
}
}
/// Switches to the saved account [name] and returns the new active account.
Future<ChatClaudeAccount?> use(String name) async {
final accounts = await _run('claude_account_use', name);
final usage = accounts?.usage;
if (usage != null) thread?.accountSwitched(usage);
return accounts?.active;
}
/// 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 {
_busy = type != 'claude_accounts';
_notify();
try {
final reply = await gateway.call(type, {
'claude_account': {'session_id': sessionId, 'name': ?name},
});
final accounts = ChatClaudeAccounts.fromJson(reply['claude_accounts']);
if (accounts == null) {
throw StateError('The server sent no Claude accounts.');
}
_accounts = accounts;
_error = null;
return accounts;
} catch (error) {
if (type == 'claude_accounts') _error = '$error';
rethrow;
} finally {
_busy = false;
_notify();
}
}
void _notify() {
if (!_disposed) notifyListeners();
}
@override
void dispose() {
_disposed = true;
super.dispose();
}
}