Files
pipelines/internal/server/chat_claude_accounts.go
T
alan ed9072b79a Claude accounts: switch a chat refused for usage to another saved account and continue it
When Claude Code refuses an operator chat's turn for a usage limit, the
server marks the refused account until its reset and, if the chat's Claude
home allows it (claude_account_auto_switch / `pipelines claude-account auto
on|off`, on by default, kept in pipelines-accounts/settings.json), switches
the home to the saved account with the most usage left through the
claude_account_use path. Switches of one home are serialized, so chats
refused together switch once. Each refused chat gets one queued prompt,
marked as sent automatically, that asks the agent to check it is on track
before continuing; the app's auto-continue-at-reset prompt for the refusal is
removed (and refused if it arrives later), and the operator gets a
low-priority notification.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 00:23:43 +00:00

82 lines
3.1 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, claude_account_remove and claude_account_auto_switch
// (whose choice reaches the adapter as the name "on" or "off"). 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
}
name := body.Name
if request.Type == protocol.ClaudeAccountAutoSwitch {
if body.AutoSwitch == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "auto_switch (true or false) is required"})
return
}
name = "off"
if *body.AutoSwitch {
name = "on"
}
}
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, 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})
}