c490f71042
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>
125 lines
4.1 KiB
Go
125 lines
4.1 KiB
Go
package claude
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/claudeaccounts"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/workenv"
|
|
)
|
|
|
|
// Account switching (see internal/claudeaccounts) acts on the Claude Code home
|
|
// a chat's directory selects: the CLAUDE_CONFIG_DIR its processes run with.
|
|
|
|
// ErrNoConfigDir is returned for a chat whose directory selects no Claude
|
|
// home of its own.
|
|
var ErrNoConfigDir = errors.New("this chat's directory does not set CLAUDE_CONFIG_DIR, so it has no Claude home of its own to switch")
|
|
|
|
// AccountConfigDir is the CLAUDE_CONFIG_DIR a process started in directory
|
|
// runs with: the directory's own environment (direnv, mise) over base.
|
|
func AccountConfigDir(ctx context.Context, resolver *workenv.Resolver, directory string, base []string) (string, error) {
|
|
env := base
|
|
if resolver != nil && strings.TrimSpace(directory) != "" {
|
|
env = resolver.Resolve(ctx, directory, base).Apply(base)
|
|
}
|
|
dir, ok := envValue(env, "CLAUDE_CONFIG_DIR")
|
|
dir = strings.TrimSpace(dir)
|
|
if !ok || dir == "" || !filepath.IsAbs(dir) {
|
|
return "", ErrNoConfigDir
|
|
}
|
|
return filepath.Clean(dir), nil
|
|
}
|
|
|
|
func (c *Chat) accountConfigDir(ctx context.Context, directory string) (string, error) {
|
|
return AccountConfigDir(ctx, c.dirEnv, directory, os.Environ())
|
|
}
|
|
|
|
// accountKeyFor is the quota key decodeAccountIdentity derives from an
|
|
// initialize response for a subscription login.
|
|
func accountKeyFor(email, organization string) string {
|
|
return strings.TrimSpace(email) + "\x00" + strings.TrimSpace(organization) + "\x00" + "firstParty"
|
|
}
|
|
|
|
// ClaudeAccounts runs one claude_account* operation for the chat sessionID in
|
|
// directory ("" uses the open session's directory). After a switch every
|
|
// open chat on the same Claude home is moved to the new account's quota and
|
|
// told so, which clears a usage limit the old account had reached.
|
|
func (c *Chat) ClaudeAccounts(ctx context.Context, sessionID, directory, operation, name string) (protocol.ClaudeAccountList, error) {
|
|
if strings.TrimSpace(directory) == "" {
|
|
if s := c.session(sessionID); s != nil {
|
|
s.mu.Lock()
|
|
directory = s.directory
|
|
s.mu.Unlock()
|
|
}
|
|
}
|
|
configDir, err := c.accountConfigDir(ctx, directory)
|
|
if err != nil {
|
|
return protocol.ClaudeAccountList{}, err
|
|
}
|
|
store := claudeaccounts.Store{ConfigDir: configDir}
|
|
switch operation {
|
|
case protocol.ClaudeAccounts:
|
|
return store.List()
|
|
case protocol.ClaudeAccountSave:
|
|
return store.Save(name)
|
|
case protocol.ClaudeAccountRemove:
|
|
return store.Remove(name)
|
|
case protocol.ClaudeAccountUse:
|
|
list, err := store.Use(name)
|
|
if err != nil {
|
|
return list, err
|
|
}
|
|
email, org, plan, ok := store.Identity()
|
|
if ok {
|
|
list.Usage = c.switchAccount(ctx, configDir, accountKeyFor(email, org), plan)
|
|
log.Printf("claude account: %s switched to a saved account", configDir)
|
|
}
|
|
return list, nil
|
|
}
|
|
return protocol.ClaudeAccountList{}, errors.New("unknown Claude account operation " + operation)
|
|
}
|
|
|
|
// switchAccount records that configDir now signs in as key: its quota report
|
|
// is marked switched (an empty one when nothing is known yet), and every open
|
|
// session on that Claude home takes the new key and republishes its usage.
|
|
func (c *Chat) switchAccount(ctx context.Context, configDir, key, plan string) *protocol.ChatAccountUsage {
|
|
now := time.Now().UnixMilli()
|
|
c.quotaMu.Lock()
|
|
usage := c.quotas[key].Clone()
|
|
if usage == nil {
|
|
usage = &protocol.ChatAccountUsage{Harness: quotaHarness, Source: "account_switch", Plan: plan, AsOf: now}
|
|
}
|
|
usage.SwitchedAt = now
|
|
if c.quotas == nil {
|
|
c.quotas = map[string]*protocol.ChatAccountUsage{}
|
|
}
|
|
c.quotas[key] = usage
|
|
c.quotaMu.Unlock()
|
|
|
|
c.mu.Lock()
|
|
sessions := make([]*session, 0, len(c.sessions))
|
|
for _, s := range c.sessions {
|
|
sessions = append(sessions, s)
|
|
}
|
|
c.mu.Unlock()
|
|
for _, s := range sessions {
|
|
s.mu.Lock()
|
|
directory := s.directory
|
|
s.mu.Unlock()
|
|
if dir, err := c.accountConfigDir(ctx, directory); err != nil || dir != configDir {
|
|
continue
|
|
}
|
|
s.mu.Lock()
|
|
s.account = accountIdentity{key: key, plan: plan}
|
|
s.mu.Unlock()
|
|
s.republishUsage()
|
|
}
|
|
return usage.Clone()
|
|
}
|