Files
magpie/internal/provider/chatgpt_same_plan.go
T
yetone 2df5d40ee3 usage: a ChatGPT API account's calls count in the same ChatGPT account's Codex window estimate (jctaoo on Discord)
A ChatGPT account signed in through Sign in with ChatGPT (chatgpt-api) and
to Codex draws on one plan's five hours and week. Only the Codex card reads
those windows, and its whole-window estimate (usage.windowHolds) keyed the
ledger's calls by provider + account, so the chatgpt-api calls were never
counted and the estimate came out low.

provider.SamePlanAccounts maps each chatgpt-api account to the Codex
account that is the same ChatGPT account, matched on the tokens'
chatgpt_account_id and chatgpt_user_id, both known and both equal, never
on a name: two Team seats of one email share the name and the user id,
not the account id. A token that names neither matches nothing, as
before. windowHolds (current and HistoricalWindowHolds) counts a matched
account's calls as the Codex account's, dry runs included. chatgpt-api
alone (no Codex account) is unchanged: its card reads no windows, so
there is nothing to estimate. Routing's caps and pace go by the vendor's
own reading of the windows and don't sum the ledger, so they are
unchanged; the per-provider account breakdown in the summary stays per
provider on purpose.

Run (mote, at this tree):
- go test ./internal/usage/ ./internal/provider/: ok.
  ./internal/gui/: TestMainWindowCorners/macOS_26_SDK red, the AppKit
  test filed as #3949 (macOS 27 on mote), untouched by this change;
  the rest ok.
- go vet, GOOS=linux go vet -tags nogui, GOOS=windows go vet of
  internal/usage and internal/provider: ok. gofmt -l: nothing.
- quota-holds.test.cjs in Chromium and WebKit: 2/2 each (the GUI only
  renders the server's holds; no asset changed).
Broken by hand:
- remap removed in windowHolds: TestWindowHoldsCountsTheSameChatGPTAccount-
  ThroughTheAPI red, "whole 5 hours 2500 tokens, want 10000" and "the
  personal account holds 2500 tokens, want 10000".
- account id ignored (user id only) in SamePlanAccounts: red, "the Team
  seat holds 12500 tokens, want 5000" and "whole 5 hours 10000 tokens,
  want 2500" (token naming no account).
Not tried: a real chatgpt-api sign-in. No such account on this machine,
so whether OpenAI's Sign in with ChatGPT tokens carry chatgpt_account_id
and chatgpt_user_id (Codex's do, checked on the real Codex logins here)
is not verified; if they don't, nothing is matched and the estimate stays
as before. GUI suite in other languages not run (no strings changed).
2026-11-05 23:57:33 +08:00

89 lines
2.6 KiB
Go

package provider
import (
"cmp"
"encoding/json"
"strings"
)
// One ChatGPT plan, two ways in (jctaoo on Discord): an account signed in
// through Sign in with ChatGPT (chatgpt-api) and the same account signed in
// to Codex draw on one plan's five-hour and weekly limits, while only the
// Codex account's card reads them. A whole-window estimate (usage's
// WithWindowHolds) over the Codex card counts the chatgpt-api account's
// calls too, by this.
// SamePlanAccounts maps each ChatGPT API account to the Codex account that
// is the same ChatGPT account, both as provider + "\x00" + lowercased name,
// the way the usage ledger's calls are keyed. They are matched on what the
// tokens say, never on a name: the ChatGPT account (workspace) id and the
// user's id, both known and both equal. Two Team seats of one email share
// a name and a user, not an account id; an account whose tokens don't say
// both is matched to none.
func SamePlanAccounts() map[string]string {
ls := sharedLogins()
type who struct{ account, user string }
codex := map[who]string{}
for _, l := range ls {
if l.Agent != "codex" {
continue
}
var a codexAuth
if json.Unmarshal(l.Auth, &a) != nil {
continue
}
w := who{codexAccountID(a), chatgptUserID(a.Tokens.IDToken, a.Tokens.AccessToken)}
if w.account == "" || w.user == "" {
continue
}
if _, ok := codex[w]; !ok {
codex[w] = "codex\x00" + strings.ToLower(l.User)
}
}
out := map[string]string{}
if len(codex) == 0 {
return out
}
for _, l := range ls {
if l.Agent != ChatGPTAPIID {
continue
}
c, ok := siwcSaved(l)
if !ok {
continue
}
w := who{chatgptAccountID(c.IDToken, c.Access), chatgptUserID(c.IDToken, c.Access)}
if w.account == "" || w.user == "" {
continue
}
if to, ok := codex[w]; ok {
out[ChatGPTAPIID+"\x00"+strings.ToLower(l.User)] = to
}
}
return out
}
// chatgptAccountID is the ChatGPT account (workspace) the first of toks
// that names one is for.
func chatgptAccountID(toks ...string) string {
for _, t := range toks {
if a := claimString(jwtClaims(t), "https://api.openai.com/auth", "chatgpt_account_id"); a != "" {
return a
}
}
return ""
}
// chatgptUserID is the ChatGPT user the first of toks that names one is
// for: its chatgpt_user_id, as OpenAI's own tokens say it beside the
// account id.
func chatgptUserID(toks ...string) string {
for _, t := range toks {
auth := jwtClaims(t)
if u := cmp.Or(claimString(auth, "https://api.openai.com/auth", "chatgpt_user_id"), claimString(auth, "https://api.openai.com/auth", "user_id")); u != "" {
return u
}
}
return ""
}