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>
109 lines
3.1 KiB
Dart
109 lines
3.1 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`, saves with `claude_account_save` and
|
|
/// sets automatic switching with `claude_account_auto_switch`.
|
|
/// 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);
|
|
|
|
/// Turns automatic switching (to another saved account when usage runs
|
|
/// out) on or off for the chat's Claude home.
|
|
Future<void> setAutoSwitch(bool on) =>
|
|
_run('claude_account_auto_switch', null, autoSwitch: on);
|
|
|
|
Future<ChatClaudeAccounts?> _run(
|
|
String type,
|
|
String? name, {
|
|
bool? autoSwitch,
|
|
}) async {
|
|
_busy = type != 'claude_accounts';
|
|
_notify();
|
|
try {
|
|
final reply = await gateway.call(type, {
|
|
'claude_account': {
|
|
'session_id': sessionId,
|
|
'name': ?name,
|
|
'auto_switch': ?autoSwitch,
|
|
},
|
|
});
|
|
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();
|
|
}
|
|
}
|