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>
This commit is contained in:
@@ -253,6 +253,9 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
|
||||
case protocol.ChatCommands:
|
||||
handleChatCommands(ctx, request, broker, write)
|
||||
return true
|
||||
case protocol.ClaudeAccounts, protocol.ClaudeAccountSave, protocol.ClaudeAccountUse, protocol.ClaudeAccountRemove:
|
||||
handleClaudeAccounts(ctx, request, broker, write)
|
||||
return true
|
||||
case protocol.ChatModelSelect:
|
||||
handleChatModelSwitch(ctx, request, broker, write)
|
||||
return true
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
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})
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// accountChat is a Claude harness with saved accounts.
|
||||
type accountChat struct {
|
||||
*fakeChat
|
||||
calls []string
|
||||
}
|
||||
|
||||
func (c *accountChat) ClaudeAccounts(_ context.Context, sessionID, directory, operation, name string) (protocol.ClaudeAccountList, error) {
|
||||
c.calls = append(c.calls, strings.Join([]string{sessionID, directory, operation, name}, "|"))
|
||||
return protocol.ClaudeAccountList{
|
||||
ConfigDir: "/home/op/caplens/.claude-profile",
|
||||
Active: &protocol.ClaudeAccount{Name: "b@example.invalid", Email: "b@example.invalid", Active: true, Saved: true},
|
||||
Accounts: []protocol.ClaudeAccount{
|
||||
{Name: "a@example.invalid", Email: "a@example.invalid", Plan: "max", Saved: true},
|
||||
{Name: "b@example.invalid", Email: "b@example.invalid", Active: true, Saved: true},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func TestClaudeAccountRequestsRouteToTheChatsClaudeHome(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "chat.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
claudeID := "01900000-0000-7000-8000-000000000021"
|
||||
codexID := "01900000-0000-7000-8000-000000000022"
|
||||
for id, backend := range map[string]string{claudeID: "claude", codexID: "codex"} {
|
||||
if err := store.RecordOperatorSession(ctx, id, "/home/op/caplens", "chat", protocol.ChatSessionTagOperator); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.SetOperatorSessionBackend(ctx, id, backend); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
claudeChat := &accountChat{fakeChat: scriptedChat()}
|
||||
manager := &chatManager{claude: claudeChat, codex: scriptedChat(), registry: store, cache: map[string]string{}}
|
||||
var reply protocol.Message
|
||||
capture := func(msg protocol.Message) error { reply = msg; return nil }
|
||||
|
||||
for _, kind := range []string{protocol.ClaudeAccounts, protocol.ClaudeAccountSave, protocol.ClaudeAccountUse, protocol.ClaudeAccountRemove} {
|
||||
handled := handleChat(ctx, protocol.Message{Type: kind, ID: "r-" + kind, ClaudeAccount: &protocol.ClaudeAccountRequest{SessionID: claudeID, Name: "a@example.invalid"}}, manager, nil, store, "client", capture)
|
||||
if !handled || reply.Type != kind || reply.ID != "r-"+kind || reply.ClaudeAccounts == nil || len(reply.ClaudeAccounts.Accounts) != 2 {
|
||||
t.Fatalf("%s reply = %+v", kind, reply)
|
||||
}
|
||||
}
|
||||
want := []string{
|
||||
claudeID + "|/home/op/caplens|claude_accounts|a@example.invalid",
|
||||
claudeID + "|/home/op/caplens|claude_account_save|a@example.invalid",
|
||||
claudeID + "|/home/op/caplens|claude_account_use|a@example.invalid",
|
||||
claudeID + "|/home/op/caplens|claude_account_remove|a@example.invalid",
|
||||
}
|
||||
if strings.Join(claudeChat.calls, "\n") != strings.Join(want, "\n") {
|
||||
t.Fatalf("calls = %v", claudeChat.calls)
|
||||
}
|
||||
wire, _ := json.Marshal(reply)
|
||||
if !strings.Contains(string(wire), `"claude_accounts":{"config_dir":"/home/op/caplens/.claude-profile","active":{"name":"b@example.invalid"`) {
|
||||
t.Fatalf("wire = %s", wire)
|
||||
}
|
||||
|
||||
// Other harnesses, a missing name for use/remove, and a bad id are refused.
|
||||
for _, request := range []protocol.Message{
|
||||
{Type: protocol.ClaudeAccounts, ID: "x1", ClaudeAccount: &protocol.ClaudeAccountRequest{SessionID: codexID}},
|
||||
{Type: protocol.ClaudeAccountUse, ID: "x2", ClaudeAccount: &protocol.ClaudeAccountRequest{SessionID: claudeID}},
|
||||
{Type: protocol.ClaudeAccountRemove, ID: "x3", ClaudeAccount: &protocol.ClaudeAccountRequest{SessionID: claudeID}},
|
||||
{Type: protocol.ClaudeAccounts, ID: "x4", ClaudeAccount: &protocol.ClaudeAccountRequest{SessionID: "../etc"}},
|
||||
{Type: protocol.ClaudeAccounts, ID: "x5"},
|
||||
} {
|
||||
handleChat(ctx, request, manager, nil, store, "client", capture)
|
||||
if reply.Type != protocol.Error || reply.ID != request.ID {
|
||||
t.Fatalf("%s accepted: %+v", request.ID, reply)
|
||||
}
|
||||
}
|
||||
if len(claudeChat.calls) != 4 {
|
||||
t.Fatalf("a refused request reached the harness: %v", claudeChat.calls)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user