Files
magpie/internal/provider/chatgpt_same_plan.go
T
yetone f4a73672ef routing: a ChatGPT API account is weighed by its Codex twin's reading, and its story lines wear its own logo
tmr11235 on X (v0.1.3924): a routing group of two ChatGPT accounts, each
added both as Codex and as ChatGPT API (chatgpt-api). The Codex rows read
their week and reset; the story below said "剩余额度未知,排在已知的之后"
next to the Codex logo, so it read as the Codex rows being unknown.

Two causes, both still on main:

- The story's lines were about the ChatGPT API accounts, but a line
  named only an account's name, and logoed() put the logo of the first
  member of that name in the order: the Codex twin. asides() now says
  which member each line is about (by its place, as two lines may say
  the same words), and renderSteps hands it to logoed(), as it did for
  a skipped member's line only.
- OpenAI tells a ChatGPT API account's plan usage nowhere magpie can
  ask, so Smart weighed it as unknown and put it after every known
  account, though it spends the same plan as its Codex twin.
  Allowances("chatgpt-api") now gives such an account the Codex
  account's allowance (samePlanAllowances), matched as
  SamePlanAccounts matches them: both tokens' chatgpt_account_id and
  chatgpt_user_id, never the name, so two Team seats of one email stay
  apart. One with no Codex twin stays unknown. The Routing card reads
  the same figure, as it reads the gateway's.

What was run (GUI on the Linux box through wsl-run, mote over its load
then; Go on mote):
- go test ./internal/provider -run 'TestChatGPTAPIAllowanceIsTheSameAccountsCodexReading|TestRenewedAccountForgetsItsAllowance|SamePlan': ok
- node --test routing-twin-logo (new; chromium and webkit; en, zh,
  zh-TW, ja, de; 1100 and 420): 20 of 20 ok.
- Every GUI file that opens the Routing page (199) plus i18n-keys and
  i18n-en-no-cjk: 3533 ok, 61 red in 13 files (click-scroll,
  ctx-head-narrow, ctx-route-align, ctx-session-line,
  inner-scroll-contain, mobile-web, routing-ctx-sessions,
  routing-many-keys, routing-pin-away, routing-pulse-composited,
  routing-row-swap, routing-sky-under, routing-story-shrinks). The same
  13 files at origin/main daaa11cb5 on the same Linux box: 57 red in
  the same tests; on mote the 13 files give 23 red at both
  origin/main and this change, test for test (ctx-session-line,
  mobile-web, routing-pin-away, routing-row-swap,
  routing-story-shrinks).
- go vet ./internal/provider ./internal/gateway (darwin), GOOS=linux
  and GOOS=windows go vet ./internal/provider: ok; gofmt -l: none.

Broken by hand:
- samePlanAllowances not applied: the new provider test is red ("the
  chatgpt-api account reads [] (known false)").
- asides' member dropped (about[i] -> undefined): routing-twin-logo is
  red in all 20 cases, the unknown lines lead with codex-color.

Not tried: a real ChatGPT API + Codex pair in a sandbox HOME (the
match is SamePlanAccounts', run for real since 2df5d40ee); the full
suite before the push (started after it).
2026-11-13 17:36:40 +08:00

136 lines
3.8 KiB
Go

package provider
import (
"cmp"
"encoding/json"
"maps"
"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, and routing weighs the chatgpt-api account by the
// Codex account's reading (samePlanAllowances): unknown, it went after
// every known account though it spends the same plan (tmr11235 on X).
// 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 ""
}
// samePlanAllowances is own, the ChatGPT API accounts' allowances by user,
// with each one that is the same ChatGPT account as a Codex one given that
// Codex account's: one plan, one five hours and one week, which only the
// Codex sign-in can read. One read of its own stands. own is not changed.
func samePlanAllowances(own map[string]Allowance) map[string]Allowance {
same := SamePlanAccounts()
if len(same) == 0 {
return own
}
var codex, out map[string]Allowance
for _, l := range sharedLogins() {
if l.Agent != ChatGPTAPIID {
continue
}
if _, has := own[l.User]; has {
continue
}
to, ok := same[ChatGPTAPIID+"\x00"+strings.ToLower(l.User)]
if !ok {
continue
}
if codex == nil {
codex = Allowances("codex")
}
for u, a := range codex {
if "codex\x00"+strings.ToLower(u) != to {
continue
}
if out == nil {
out = maps.Clone(own)
if out == nil {
out = map[string]Allowance{}
}
}
out[l.User] = a
break
}
}
if out == nil {
return own
}
return out
}