Chat: show the active Claude account in the send tooltip, menu and limit banner
usage.account now carries the email and organization of the account Claude Code is signed in to (from its initialize account, or the saved account just switched to), sent at process start and after a switch. The app shows it as the send tooltip's usage prefix, the Claude account menu subtitle, the usage-limit banner's status line and the info sheet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -77,7 +77,8 @@ func (c *Chat) ClaudeAccounts(ctx context.Context, sessionID, directory, operati
|
||||
}
|
||||
email, org, plan, ok := store.Identity()
|
||||
if ok {
|
||||
list.Usage = c.switchAccount(ctx, configDir, accountKeyFor(email, org), plan)
|
||||
account := accountIdentity{key: accountKeyFor(email, org), plan: plan, email: strings.TrimSpace(email), organization: strings.TrimSpace(org)}
|
||||
list.Usage = c.switchAccount(ctx, configDir, account)
|
||||
log.Printf("claude account: %s switched to a saved account", configDir)
|
||||
}
|
||||
return list, nil
|
||||
@@ -85,15 +86,17 @@ func (c *Chat) ClaudeAccounts(ctx context.Context, sessionID, directory, operati
|
||||
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 {
|
||||
// switchAccount records that configDir now signs in as account: 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 account and
|
||||
// republishes its usage, which names it.
|
||||
func (c *Chat) switchAccount(ctx context.Context, configDir string, account accountIdentity) *protocol.ChatAccountUsage {
|
||||
key := account.key
|
||||
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 = &protocol.ChatAccountUsage{Harness: quotaHarness, Source: "account_switch", Plan: account.plan, AsOf: now}
|
||||
}
|
||||
usage.SwitchedAt = now
|
||||
if c.quotas == nil {
|
||||
@@ -116,9 +119,29 @@ func (c *Chat) switchAccount(ctx context.Context, configDir, key, plan string) *
|
||||
continue
|
||||
}
|
||||
s.mu.Lock()
|
||||
s.account = accountIdentity{key: key, plan: plan}
|
||||
s.account = account
|
||||
s.mu.Unlock()
|
||||
s.republishUsage()
|
||||
}
|
||||
return usage.Clone()
|
||||
return c.accountUsage(account)
|
||||
}
|
||||
|
||||
// accountUsage returns a copy of the latest quota for an account, naming the
|
||||
// account. With no quota known yet it names the account alone; it is nil when
|
||||
// neither is known (an API key login reports no email).
|
||||
func (c *Chat) accountUsage(account accountIdentity) *protocol.ChatAccountUsage {
|
||||
var usage *protocol.ChatAccountUsage
|
||||
if account.key != "" {
|
||||
c.quotaMu.Lock()
|
||||
usage = c.quotas[account.key].Clone()
|
||||
c.quotaMu.Unlock()
|
||||
}
|
||||
if account.email == "" {
|
||||
return usage
|
||||
}
|
||||
if usage == nil {
|
||||
usage = &protocol.ChatAccountUsage{Harness: quotaHarness, Source: "initialize", Plan: account.plan}
|
||||
}
|
||||
usage.Email, usage.Organization = account.email, account.organization
|
||||
return usage
|
||||
}
|
||||
|
||||
@@ -71,6 +71,10 @@ func TestUseAccountMovesTheHomesChatsToTheNewAccountQuota(t *testing.T) {
|
||||
if len(pushed.Windows) != 0 {
|
||||
t.Fatalf("the new account inherited the old quota: %+v", pushed)
|
||||
}
|
||||
// usage.account names the account the home is signed in to now.
|
||||
if list.Usage.Email != "a@example.invalid" || pushed.Email != "a@example.invalid" || pushed.Organization != "Org" {
|
||||
t.Fatalf("switched usage names %q / %q (%q)", list.Usage.Email, pushed.Email, pushed.Organization)
|
||||
}
|
||||
data, _ := os.ReadFile(filepath.Join(profile, ".credentials.json"))
|
||||
if !strings.Contains(string(data), "refresh-a1") {
|
||||
t.Fatal("credentials not switched")
|
||||
|
||||
@@ -427,7 +427,7 @@ func (s *session) emit(frame protocol.ChatEventFrame) {
|
||||
s.mu.Lock()
|
||||
if frame.Kind == protocol.ChatEventUsage && frame.Usage != nil {
|
||||
usage := *frame.Usage
|
||||
usage.Account = s.chat.accountUsage(s.account.key)
|
||||
usage.Account = s.chat.accountUsage(s.account)
|
||||
frame.Usage = &usage
|
||||
s.usage = &frame
|
||||
}
|
||||
@@ -463,10 +463,10 @@ func (s *session) subscribe() chan protocol.ChatEventFrame {
|
||||
// Account quota may have moved since (another chat on the account).
|
||||
frame := *s.usage
|
||||
usage := *frame.Usage
|
||||
usage.Account = s.chat.accountUsage(s.account.key)
|
||||
usage.Account = s.chat.accountUsage(s.account)
|
||||
frame.Usage = &usage
|
||||
ch <- frame
|
||||
} else if account := s.chat.accountUsage(s.account.key); account != nil {
|
||||
} else if account := s.chat.accountUsage(s.account); account != nil {
|
||||
ch <- protocol.ChatEventFrame{SessionID: s.id, Kind: protocol.ChatEventUsage, Usage: &protocol.ChatUsage{Account: account}}
|
||||
}
|
||||
if s.compaction != nil {
|
||||
|
||||
@@ -133,7 +133,10 @@ func TestTurnStreamsTextWithoutDuplicatingAndReportsUsage(t *testing.T) {
|
||||
ended = true
|
||||
}
|
||||
case protocol.ChatEventUsage:
|
||||
usage = frame.Usage
|
||||
// The account-only report at start carries no tokens.
|
||||
if frame.Usage != nil && frame.Usage.TotalTokens != nil {
|
||||
usage = frame.Usage
|
||||
}
|
||||
}
|
||||
case <-deadline:
|
||||
t.Fatalf("turn did not settle (ended=%v usage=%v text=%q)", ended, usage != nil, text)
|
||||
|
||||
@@ -181,6 +181,10 @@ func (s *session) spawnOnce(ctx context.Context) (*process, error) {
|
||||
s.chat.rememberCatalog(models)
|
||||
s.lastErr = ""
|
||||
s.mu.Unlock()
|
||||
if account.email != "" {
|
||||
// Watchers learn which account this chat now runs on.
|
||||
s.republishUsage()
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
|
||||
+12
-14
@@ -25,10 +25,13 @@ const quotaHarness = "Claude Code"
|
||||
const quotaSource = "rate_limit_event"
|
||||
|
||||
// accountIdentity is the signed-in account a process reported at initialize.
|
||||
// key is never sent to clients; it only shares quota between sessions.
|
||||
// key is never sent to clients; it only shares quota between sessions. email
|
||||
// and organization name the account to the operator (usage.account).
|
||||
type accountIdentity struct {
|
||||
key string
|
||||
plan string
|
||||
key string
|
||||
plan string
|
||||
email string
|
||||
organization string
|
||||
}
|
||||
|
||||
func decodeAccountIdentity(raw json.RawMessage) accountIdentity {
|
||||
@@ -45,7 +48,12 @@ func decodeAccountIdentity(raw json.RawMessage) accountIdentity {
|
||||
if strings.Trim(key, "\x00") == "" {
|
||||
key = ""
|
||||
}
|
||||
return accountIdentity{key: key, plan: strings.TrimSpace(account.SubscriptionType)}
|
||||
return accountIdentity{
|
||||
key: key,
|
||||
plan: strings.TrimSpace(account.SubscriptionType),
|
||||
email: strings.TrimSpace(account.Email),
|
||||
organization: strings.TrimSpace(account.Organization),
|
||||
}
|
||||
}
|
||||
|
||||
type rateLimitWindowInfo struct {
|
||||
@@ -175,16 +183,6 @@ func mergeRateLimit(previous *protocol.ChatAccountUsage, raw json.RawMessage, pl
|
||||
return next
|
||||
}
|
||||
|
||||
// accountUsage returns a copy of the latest quota for an account key.
|
||||
func (c *Chat) accountUsage(key string) *protocol.ChatAccountUsage {
|
||||
if key == "" {
|
||||
return nil
|
||||
}
|
||||
c.quotaMu.Lock()
|
||||
defer c.quotaMu.Unlock()
|
||||
return c.quotas[key].Clone()
|
||||
}
|
||||
|
||||
// handleRateLimit records a rate_limit_event for the session's account and
|
||||
// republishes the latest usage snapshot, with the new quota, to every session
|
||||
// of that account.
|
||||
|
||||
@@ -87,9 +87,14 @@ func TestMergeRateLimitCarriesExtraUsage(t *testing.T) {
|
||||
|
||||
func TestDecodeAccountIdentity(t *testing.T) {
|
||||
got := decodeAccountIdentity(json.RawMessage(`{"email":"a@example.invalid","organization":"Org","subscriptionType":"max","apiProvider":"firstParty"}`))
|
||||
if got.key == "" || got.plan != "max" {
|
||||
if got.key == "" || got.plan != "max" || got.email != "a@example.invalid" || got.organization != "Org" {
|
||||
t.Fatalf("identity = %+v", got)
|
||||
}
|
||||
// An API key login names no account, so nothing is shown for it.
|
||||
chat := &Chat{}
|
||||
if usage := chat.accountUsage(decodeAccountIdentity(json.RawMessage(`{"apiProvider":"firstParty","apiKeySource":"ANTHROPIC_API_KEY"}`))); usage != nil {
|
||||
t.Fatalf("API key usage = %+v", usage)
|
||||
}
|
||||
if empty := decodeAccountIdentity(json.RawMessage(`{}`)); empty.key != "" || empty.plan != "" {
|
||||
t.Fatalf("empty account = %+v", empty)
|
||||
}
|
||||
@@ -103,10 +108,17 @@ func TestRateLimitEventReachesEverySessionOfTheAccount(t *testing.T) {
|
||||
if err := chat.Prompt(context.Background(), first.ID, "hello"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
quota := waitFrame(t, frames, "account usage", func(frame protocol.ChatEventFrame) bool {
|
||||
// The process's start names the account before any quota is known.
|
||||
named := waitFrame(t, frames, "account identity", func(frame protocol.ChatEventFrame) bool {
|
||||
return frame.Kind == protocol.ChatEventUsage && frame.Usage != nil && frame.Usage.Account != nil
|
||||
}).Usage.Account
|
||||
if quota.Plan != "max" || len(quota.Windows) != 2 || quota.Windows[0].Label != "5-hour" || quota.Windows[0].UsedPercent != 42 ||
|
||||
if named.Email != "fixture@example.invalid" || named.Organization != "Fixture Org" || len(named.Windows) != 0 {
|
||||
t.Fatalf("account identity = %+v", named)
|
||||
}
|
||||
quota := waitFrame(t, frames, "account usage", func(frame protocol.ChatEventFrame) bool {
|
||||
return frame.Kind == protocol.ChatEventUsage && frame.Usage != nil && frame.Usage.Account != nil && len(frame.Usage.Account.Windows) > 0
|
||||
}).Usage.Account
|
||||
if quota.Plan != "max" || quota.Email != "fixture@example.invalid" || len(quota.Windows) != 2 || quota.Windows[0].Label != "5-hour" || quota.Windows[0].UsedPercent != 42 ||
|
||||
quota.Windows[1].Label != "Weekly" || quota.Windows[1].UsedPercent != 73.1 || quota.Windows[1].ResetsAt != 1_900_400_000_000 {
|
||||
t.Fatalf("account usage = %+v", quota)
|
||||
}
|
||||
|
||||
@@ -808,6 +808,11 @@ type ChatAccountUsage struct {
|
||||
Source string `json:"source,omitempty"`
|
||||
// Plan is the harness's plan identifier when it reports one ("max", "plus").
|
||||
Plan string `json:"plan,omitempty"`
|
||||
// Email and Organization name the account the harness is signed in to now
|
||||
// (the operator's own login, never a credential); empty when it did not
|
||||
// say (an API key, or a harness without accounts).
|
||||
Email string `json:"email,omitempty"`
|
||||
Organization string `json:"organization,omitempty"`
|
||||
// AsOf is when the server received the report (epoch milliseconds).
|
||||
AsOf int64 `json:"as_of,omitempty"`
|
||||
Windows []ChatQuotaWindow `json:"windows,omitempty"`
|
||||
|
||||
Reference in New Issue
Block a user