Files
pipelines/app/tool/perf/chat_claude_accounts_fixture.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

107 lines
2.9 KiB
Dart

// `?claudeAccounts=N` (with `?chats=1&harness=claude`): the chat's Claude
// home has N saved accounts (alex@caplens.example, sam@caplens.example,
// ops@caplens.example), the first one active, and `claude_account_use`
// switches between them. `?claudeUsage=limit` pushes the active account's
// quota at 100% on subscribe (the usage-limit banner); `?claudeUsage=warn`
// pushes it at 88% (the usage warning banner).
import 'package:pipelines_app/core/api_gateway.dart';
Map<String, String> _params = const {};
int _active = 0;
const _emails = [
'alex@caplens.example',
'sam@caplens.example',
'ops@caplens.example',
];
void configureChatClaudeAccountsFixture(Map<String, String> params) {
_params = params;
}
int get _count =>
(int.tryParse(_params['claudeAccounts'] ?? '') ?? 0).clamp(0, 3);
JsonMap _account(int i) => {
'name': _emails[i],
'email': _emails[i],
'organization': 'Caplens',
'plan': i == 2 ? 'pro' : 'max',
'active': i == _active,
'saved': true,
};
JsonMap _list({JsonMap? usage}) => {
'config_dir': '/home/alex/caplens/.claude-profile',
'active': _account(_active),
'accounts': [for (var i = 0; i < _count; i++) _account(i)],
'usage': ?usage,
};
/// Replies to the claude_account* requests, or null for any other request.
JsonMap? chatClaudeAccountsReply(JsonMap request) {
final type = '${request['type']}';
if (_count == 0 || !type.startsWith('claude_account')) return null;
final body = request['claude_account'];
final name = body is Map ? '${body['name'] ?? ''}' : '';
JsonMap? usage;
if (type == 'claude_account_use') {
final index = _emails.indexOf(name);
if (index < 0 || index >= _count) {
return {
'id': request['id'],
'type': 'error',
'error': 'no saved Claude account "$name"',
};
}
_active = index;
final now = DateTime.now().millisecondsSinceEpoch;
usage = {
'harness': 'Claude Code',
'plan': 'max',
'as_of': now,
'switched_at': now,
};
}
return {
'id': request['id'],
'type': type,
'claude_accounts': _list(usage: usage),
};
}
/// The active account's quota frame for `?claudeUsage=`, or null.
JsonMap? chatClaudeUsageFrame(String sessionId) {
final percent = switch (_params['claudeUsage']) {
'limit' => 100.0,
'warn' => 88.0,
_ => null,
};
if (percent == null) return null;
final now = DateTime.now();
return {
'type': 'chat_event',
'chat_event': {
'session_id': sessionId,
'kind': 'usage',
'usage': {
'account': {
'harness': 'Claude Code',
'plan': 'max',
'as_of': now.millisecondsSinceEpoch,
'windows': [
{
'id': 'five_hour',
'label': '5-hour',
'used_percent': percent,
'resets_at': now
.add(const Duration(hours: 2, minutes: 10))
.millisecondsSinceEpoch,
},
],
},
},
},
};
}