Files
pipelines/internal/protocol/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

68 lines
3.1 KiB
Go

package protocol
// Claude account switching for a chat whose directory selects its own Claude
// Code home (CLAUDE_CONFIG_DIR, for example from a direnv .envrc). Each request
// body is ClaudeAccountRequest naming the chat; the server resolves the chat's
// directory environment to that config directory and replies with
// ClaudeAccountList. Saved accounts live in <config dir>/pipelines-accounts/.
// Using one changes the same files `/login` changes, in place, so running
// Claude Code processes continue on the new account without a restart. No
// reply or log ever carries a token.
const (
// ClaudeAccounts lists the saved accounts and the active login.
ClaudeAccounts = "claude_accounts"
// ClaudeAccountSave snapshots the active login as a saved account; Name
// defaults to its email.
ClaudeAccountSave = "claude_account_save"
// ClaudeAccountUse makes the saved account Name the active login, first
// writing the current login's refreshed tokens back to its own slot.
ClaudeAccountUse = "claude_account_use"
// ClaudeAccountRemove deletes the saved account Name. The active login is
// unchanged.
ClaudeAccountRemove = "claude_account_remove"
// ClaudeAccountAutoSwitch sets whether a chat on this Claude home that
// runs out of usage is switched to another saved account with usage left
// and continued automatically (AutoSwitch; on by default).
ClaudeAccountAutoSwitch = "claude_account_auto_switch"
)
// ClaudeAccountRequest is the body of every claude_account* request.
type ClaudeAccountRequest struct {
SessionID string `json:"session_id"`
// Name is the saved account (its name, slug or email); for a save, the
// name to save under ("" uses the email).
Name string `json:"name,omitempty"`
// AutoSwitch is the new automatic-switch choice of a
// claude_account_auto_switch request.
AutoSwitch *bool `json:"auto_switch,omitempty"`
}
// ClaudeAccount describes one Claude login without its credentials.
type ClaudeAccount struct {
Name string `json:"name"`
Email string `json:"email,omitempty"`
Organization string `json:"organization,omitempty"`
// Plan is the subscription type the login reports ("max", "pro").
Plan string `json:"plan,omitempty"`
// Active is set on the account the config directory is signed in with.
Active bool `json:"active,omitempty"`
// Saved is false for an active login that has no saved slot yet.
Saved bool `json:"saved"`
}
// ClaudeAccountList is the reply to every claude_account* request.
type ClaudeAccountList struct {
// ConfigDir is the Claude Code home the chat's directory selects.
ConfigDir string `json:"config_dir"`
// Active is the current login (saved or not); nil when signed out.
Active *ClaudeAccount `json:"active,omitempty"`
// Accounts are the saved accounts, sorted by name.
Accounts []ClaudeAccount `json:"accounts"`
// Usage is the newly active account's latest known quota after a
// claude_account_use (see ChatAccountUsage.SwitchedAt).
Usage *ChatAccountUsage `json:"usage,omitempty"`
// AutoSwitch reports whether running out of usage switches the Claude
// home to another saved account and continues the chat automatically.
AutoSwitch bool `json:"auto_switch"`
}