Files
pipelines/internal/server/chat_claude_accounts.go
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

70 lines
2.7 KiB
Go

package server
import (
"context"
"errors"
"git.a-wan.net/alan/pipelines/internal/claude"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// chatAccountSwitcher is Claude Code's saved-account surface
// (claude.Chat.ClaudeAccounts): list, save, use and remove the logins of the
// Claude home a chat's directory selects.
type chatAccountSwitcher interface {
ClaudeAccounts(ctx context.Context, sessionID, directory, operation, name string) (protocol.ClaudeAccountList, error)
}
var errNotClaudeChat = errors.New("Claude accounts apply to Claude Code chats only")
// ClaudeAccounts routes a claude_account* request to the Claude adapter with
// the chat's recorded directory. Other harnesses are refused.
func (m *chatManager) ClaudeAccounts(ctx context.Context, sessionID, operation, name string) (protocol.ClaudeAccountList, error) {
if m.backendFor(ctx, sessionID) != claude.Backend {
return protocol.ClaudeAccountList{}, errNotClaudeChat
}
adapter, err := m.brokerFor(ctx, sessionID)
if err != nil {
return protocol.ClaudeAccountList{}, err
}
switcher, ok := adapter.(chatAccountSwitcher)
if !ok {
return protocol.ClaudeAccountList{}, errNotClaudeChat
}
directory := ""
if m.registry != nil {
if row, found, err := m.registry.OperatorSession(ctx, sessionID); err == nil && found {
directory = row.Directory
}
}
return switcher.ClaudeAccounts(ctx, sessionID, directory, operation, name)
}
// handleClaudeAccounts answers claude_accounts, claude_account_save,
// claude_account_use and claude_account_remove. Replies describe accounts
// only; no token ever leaves the server.
func handleClaudeAccounts(ctx context.Context, request protocol.Message, broker chatBroker, write func(protocol.Message) error) {
body := request.ClaudeAccount
if body == nil || !validChatSessionID(body.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid chat session id is required"})
return
}
if (request.Type == protocol.ClaudeAccountUse || request.Type == protocol.ClaudeAccountRemove) && body.Name == "" {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a saved account name is required"})
return
}
switcher, ok := broker.(interface {
ClaudeAccounts(ctx context.Context, sessionID, operation, name string) (protocol.ClaudeAccountList, error)
})
if !ok {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: errNotClaudeChat.Error()})
return
}
list, err := switcher.ClaudeAccounts(ctx, body.SessionID, request.Type, body.Name)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(protocol.Message{Type: request.Type, ID: request.ID, ClaudeAccounts: &list})
}