diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 96b4ac161..b3dae7296 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -17186,8 +17186,11 @@ function planTerm(q) { return t(q.renew === "auto" ? "Renews {date}" : q.renew === "off" ? "Expires {date}" : "Until {date}", { date }); } // planName: a plan id as the vendor's own app names it (visamzh on X: -// ChatGPT's self_serve_business_prolite is its Business) -const PLAN_NAMES = { self_serve_business_prolite: "Business", self_serve_business_usage_based: "Business", free_workspace: "Free", ent26: "Enterprise", prolite: "Pro Lite", promax: "Pro Max", edu_plus: "Edu Plus", edu_pro: "Edu Pro" }; +// ChatGPT's self_serve_business_prolite is its Business). ChatGPT's app +// names its Pro plans by price: prolite Pro 100, promax Pro 500 +// (shangyuan0818 on X: "Pro Lite" is no plan it sells). Its plain pro +// stays Pro, as Claude's and Grok's pro share the id +const PLAN_NAMES = { self_serve_business_prolite: "Business", self_serve_business_usage_based: "Business", free_workspace: "Free", ent26: "Enterprise", prolite: "Pro 100", promax: "Pro 500", edu_plus: "Edu Plus", edu_pro: "Edu Pro" }; const planName = (p) => p && (PLAN_NAMES[p] || (/^enterprise_cbp_/.test(p) ? "Enterprise" : p)); const planChip = (s) => Object.assign(el("span", "plan", s), { title: s }); function planSpan(q) { diff --git a/internal/gui/tests/plan-name-fits.test.cjs b/internal/gui/tests/plan-name-fits.test.cjs index c682f573f..969c22125 100644 --- a/internal/gui/tests/plan-name-fits.test.cjs +++ b/internal/gui/tests/plan-name-fits.test.cjs @@ -24,7 +24,9 @@ const quotas = [ windows: [{ name: "7 days", used: 20, resetsAt: later }], resets: { count: 2, until: later } }, { provider: "codex", name: "Codex", icon: "codex-color", user: "second.seat@example.com", plan: "self_serve_business_prolite", windows: [{ name: "7 days", used: 7, resetsAt: later }], resets: { count: 1, until: later } }, - { provider: "codex", name: "Codex", icon: "codex-color", user: "plus.account@example.com", plan: "plus", + // shangyuan0818 on X: ChatGPT's $100 Pro, plan_type prolite, read "Pro + // Lite", a plan ChatGPT never sold; its app calls it Pro 100 + { provider: "codex", name: "Codex", icon: "codex-color", user: "pro100.account@example.com", plan: "prolite", windows: [{ name: "5 hours", used: 7, resetsAt: later }, { name: "7 days", used: 64, resetsAt: later }], resets: { count: 1, until: later } }, // a plan id no app has named yet: shown as it comes, and cut { provider: "codex", name: "Codex", icon: "codex-color", user: "next.seat@example.com", plan: UNNAMED, @@ -111,7 +113,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", rows.forEach(({ user, plan, kids }, i) => { assert.ok(user.w >= 60, `${at} row ${i}: the account's name is read (${user.w}px of ${user.text})`); for (const k of kids) assert.ok(k.inside, `${at} row ${i}: ${k.cls} stays inside the card`); - assert.equal(plan.text, ["Business", "Business", "plus", UNNAMED][i], `${at} row ${i}: the plan as ChatGPT names it`); + assert.equal(plan.text, ["Business", "Business", "Pro 100", UNNAMED][i], `${at} row ${i}: the plan as ChatGPT names it`); assert.equal(plan.title, plan.text, `${at} row ${i}: the plan's tooltip`); }); // "All accounts": its windows by plan, named as the rows name them diff --git a/internal/provider/account.go b/internal/provider/account.go index 56fd9a6b9..924b34751 100644 --- a/internal/provider/account.go +++ b/internal/provider/account.go @@ -438,13 +438,46 @@ type claudeAuth struct { // and tells the two apart only by the rate-limit tier. func (a claudeAuth) plan() string { return claudePlan(a.SubscriptionType, a.RateLimitTier) } -func claudePlan(sub, tier string) string { - if sub == "max" && strings.Contains(tier, "max_20x") { +func claudePlan(sub, tier string) string { return claudePlanShown(sub, tier, "") } + +// claudePlanShown is claudePlan with the name Claude itself shows the +// plan by: /api/oauth/profile's organization.plan_display_name, which +// Claude Code keeps as oauthAccount.planDisplayName and shows as +// "Claude " ("Ultra" for a Max 20x account, none for Max 5x). A +// known tier decides; the name is read only when the tier doesn't say, +// so a profile left from another account can't rename this one. +func claudePlanShown(sub, tier, shown string) string { + if sub != "max" { + return sub + } + switch { + case strings.Contains(tier, "max_20x"): + return "ultra" + case strings.Contains(tier, "max_5x"): + return "max" + case strings.EqualFold(strings.TrimSpace(shown), "ultra"): return "ultra" } return sub } +// claudeShownPlan is the plan name in a Claude Code oauthAccount profile. +func claudeShownPlan(acct map[string]any) string { + s, _ := acct["planDisplayName"].(string) + return s +} + +// claudeLivePlan is the plan of Claude Code's own sign-in c: the +// credentials', or `claude auth status`'s when it said one, named by the +// tier (claudePlanShown); acct is Claude Code's profile, nil when unread. +func claudeLivePlan(c claudeCredentials, statusPlan string, acct map[string]any) string { + sub := c.OAuth.SubscriptionType + if statusPlan != "" { + sub = statusPlan + } + return claudePlanShown(sub, c.OAuth.RateLimitTier, claudeShownPlan(acct)) +} + // claudeCredentials keeps the whole credential blob in raw, so refreshing a // token writes back everything else — MCP OAuth state included — untouched. type claudeCredentials struct { @@ -836,14 +869,11 @@ func claudeAccount() (Provider, bool) { if signedOut { return claudeStandInAccount() } - plan := c.OAuth.plan() - if statusPlan != "" { - plan = claudePlan(statusPlan, c.OAuth.RateLimitTier) - } // named as its saved login is (liveLogin): it is then the account Logins // flags Active, not served a second time beside itself, and its // allowances and limits are found under its name (#177) user, acct := claudeSignedInUser(c.OAuth.SubscriptionType, statusPlan, user) + plan := claudeLivePlan(c, statusPlan, acct) if b, err := c.marshal(); err == nil && user != "" { // by its sign-in when its profile is another's (claudeSignInOwner) l := savedLogin{Agent: "claude", User: user, Auth: b} diff --git a/internal/provider/claude_plan_test.go b/internal/provider/claude_plan_test.go index d9544f79b..0a842ce31 100644 --- a/internal/provider/claude_plan_test.go +++ b/internal/provider/claude_plan_test.go @@ -1,8 +1,14 @@ package provider import ( + "context" "encoding/json" + "path/filepath" + "sync/atomic" "testing" + "time" + + "github.com/yetone/magpie/internal/testenv" ) // Claude Max 20x is sold as Claude Ultra now, but Claude Code still says @@ -61,3 +67,72 @@ func TestClaudeMax20xIsUltra(t *testing.T) { t.Fatalf("a held Ultra account renewed under another profile isn't found: %v %v", h.User, ok) } } + +// The Usage card of Claude Code's own sign-in, the only Claude account, +// named its plan by `claude auth status`, which says "max" for Max 20x +// too: an Ultra account read "Max" (shangyuan0818 on X). It is named as +// the account is. Credentials and profile are shaped as Claude Code 2.1 +// writes them; plan_display_name is what /api/oauth/profile answers +// (organization.plan_display_name "Ultra" for a 20x account, none for 5x). +func TestClaudeUsageCardPlanIsUltra(t *testing.T) { + for _, c := range []struct { + name, tier, shown, want string + }{ + {"20x", "default_claude_max_20x", "Ultra", "ultra"}, + {"20x, profile not yet read", "default_claude_max_20x", "", "ultra"}, + {"no tier, Claude names it Ultra", "", "Ultra", "ultra"}, + {"5x", "default_claude_max_5x", "", "max"}, + // a profile left from another account doesn't rename a known tier + {"5x under a 20x profile", "default_claude_max_5x", "Ultra", "max"}, + } { + t.Run(c.name, func(t *testing.T) { + home := claudeHome(t) + writeFile(t, filepath.Join(home, ".claude", ".credentials.json"), map[string]any{ + "claudeAiOauth": map[string]any{ + "accessToken": "sk-ant-oat01-a", "refreshToken": "sk-ant-ort01-a", + "expiresAt": time.Now().Add(time.Hour).UnixMilli(), "subscriptionType": "max", + "rateLimitTier": c.tier, "scopes": []string{"user:inference", "user:profile"}, + }, + }) + acct := map[string]any{"emailAddress": "a@example.com", "billingType": "stripe_subscription"} + if c.shown != "" { + acct["planDisplayName"] = c.shown + } + writeFile(t, filepath.Join(home, ".claude.json"), map[string]any{"oauthAccount": acct}) + dir := t.TempDir() + exe := filepath.Join(dir, "claude") + testenv.Program(t, exe, "#!/bin/sh\necho '{\"loggedIn\":true,\"authMethod\":\"claude.ai\",\"apiProvider\":\"firstParty\",\"email\":\"a@example.com\",\"subscriptionType\":\"max\"}'\n") + old := claudeExecutable + claudeExecutable = func() string { return exe } + t.Cleanup(func() { claudeExecutable = old; forgetClaudeStatus() }) + forgetClaudeStatus() + var out atomic.Value + out.Store("Current session: 40% used") + fakeClaudeUsage(t, &out, nil) + + if q := claudeSubscriptionUsage(context.Background(), "a@example.com"); q.Plan != c.want { + t.Errorf("Usage card plan %q, want %q", q.Plan, c.want) + } + if p, ok := claudeAccount(); !ok || p.Account.Plan != c.want { + t.Errorf("account plan %v %q, want %q", ok, p.Account.Plan, c.want) + } + }) + } +} + +// A sign-in or saved account whose credentials carry no tier is named by +// the plan name Claude gave its profile. +func TestClaudeSavedPlanFromProfileName(t *testing.T) { + c, _ := parseClaudeCredentials([]byte(`{"claudeAiOauth":{"accessToken":"a","refreshToken":"r","subscriptionType":"max"}}`)) + l, err := claudeLogin(c, map[string]any{"emailAddress": "u@example.com", "planDisplayName": "Ultra"}) + if err != nil || l.Plan != "ultra" { + t.Fatalf("sign-in plan %q, %v; want ultra", l.Plan, err) + } + b, _ := json.Marshal([]savedLogin{{Agent: "claude", User: "u@example.com", Plan: "max", + Auth: json.RawMessage(`{"claudeAiOauth":{"accessToken":"a","refreshToken":"r","subscriptionType":"max"}}`), + Profile: json.RawMessage(`{"emailAddress":"u@example.com","planDisplayName":"Ultra"}`)}}) + ls, err := parseLogins(b) + if err != nil || len(ls) != 1 || ls[0].Plan != "ultra" { + t.Fatalf("saved plan %+v %v; want ultra", ls, err) + } +} diff --git a/internal/provider/logins.go b/internal/provider/logins.go index ff5a0d2f8..19b883ed7 100644 --- a/internal/provider/logins.go +++ b/internal/provider/logins.go @@ -226,7 +226,9 @@ func parseLogins(b []byte) ([]savedLogin, error) { // a Max 20x saved before it was named Ultra if out[i].Agent == "claude" && out[i].Plan == "max" { if c, ok := parseClaudeCredentials(out[i].Auth); ok { - out[i].Plan = claudePlan(out[i].Plan, c.OAuth.RateLimitTier) + var acct map[string]any + _ = json.Unmarshal(out[i].Profile, &acct) + out[i].Plan = claudePlanShown(out[i].Plan, c.OAuth.RateLimitTier, claudeShownPlan(acct)) } } } @@ -869,12 +871,13 @@ func liveLogin(agent string) (savedLogin, bool) { if signedOut { return savedLogin{}, false } - l := savedLogin{Agent: agent, Plan: c.OAuth.plan(), Auth: b} + l := savedLogin{Agent: agent, Auth: b} var acct map[string]any if l.User, acct = claudeSignedInUser(c.OAuth.SubscriptionType, plan, user); acct != nil { l.Profile, _ = json.Marshal(acct) l.HeldAs = claudeProfileKey(l.Profile) } + l.Plan = claudeLivePlan(c, "", acct) if l.User == "" { return savedLogin{}, false } diff --git a/internal/provider/signin.go b/internal/provider/signin.go index 9874d4c3c..c999c7ce8 100644 --- a/internal/provider/signin.go +++ b/internal/provider/signin.go @@ -838,7 +838,7 @@ func claudeLogin(c claudeCredentials, acct map[string]any) (savedLogin, error) { return savedLogin{}, err } profile, _ := json.Marshal(acct) - return savedLogin{Agent: "claude", User: claudeUser(email, c.OAuth.SubscriptionType, acct), Plan: c.OAuth.plan(), Auth: auth, Profile: profile}, nil + return savedLogin{Agent: "claude", User: claudeUser(email, c.OAuth.SubscriptionType, acct), Plan: claudeLivePlan(c, "", acct), Auth: auth, Profile: profile}, nil } func codexExchange(ctx context.Context, code, verifier, redirect string) (savedLogin, error) { diff --git a/internal/provider/subscription_usage.go b/internal/provider/subscription_usage.go index 278a2c4f7..874f31a63 100644 --- a/internal/provider/subscription_usage.go +++ b/internal/provider/subscription_usage.go @@ -730,12 +730,16 @@ func (e *accountStatusError) Error() string { return http.StatusText(e.status) } // a Team seat isn't the email alone `claude auth status` gives. func claudeSubscriptionUsage(ctx context.Context, user string) SubscriptionQuota { q := SubscriptionQuota{Provider: "claude", Name: "Claude Code", Icon: "claude-color", Windows: []QuotaWindow{}} - if _, _, ok := claudeCredential(); !ok { + c, _, ok := claudeCredential() + if !ok { q.Error = "Claude Code is signed out; run claude auth login" return q } - _, plan, _ := claudeIdentity() - q.Plan = plan + // named as the account is (claudeAccount): `claude auth status` says + // "max" for Max 20x too, which Claude sells as Ultra (shangyuan0818 on X) + _, statusPlan, _ := claudeIdentity() + acct, _ := claudeProfileAccount() + q.Plan = claudeLivePlan(c, statusPlan, acct) var err error q.Windows, err = claudeWindows(ctx, user, true) if err != nil {