molvqingtai renamed a Kiro (plugin) account from "example@gmail.com (foo)" to "example@gmail.com (bar)", and the Routing page kept saying "(foo)" on every request made after. Cause: the Routing page never looked up the user's name at all. A trace's `who` is the vendor's account name (Weighed.Who), and plugins/routing/client.js showed it as it was. Usage records (ProviderAccount) keep the vendor's name the same way, and the Usage page's Accounts, the request log's Account filter, `magpie usage`, a gateway key's accounts (AccountNames, /api/caller-keys) and Routing's "Several accounts" pools (onOf) all showed it raw too. This affected built-in and plugin providers alike. Records still keep the vendor's name. It is the key the user's name is stored under (providers.json AccountNames, in lower case), so it stays the identity: Routing still groups and ranks by `who`. The name is looked up when it is shown: provider.AccountShown / AccountShownOf / AccountRef.Shown in Go, and userShown (accountNameOf over /api/providers' accountNames) on the Routing page. So requests from before the rename read by the current name too, and a cleared name goes back to the vendor's. A key's name and an unnamed account read as before. Already right before this change: quota cards and the menu bar (Alias), `magpie accounts`, and the editor's account row. Record for this push: 13 commits on origin/mainca4a1cde0, head tree539c84e92. Go 1.26.8, sandbox HOME, GOMODCACHE/GOCACHE pinned. The full Go run and vet were at tree d6393dfc8, the same Go code as this head: only app.js comments (cut under the core script budget) and one ja string changed after it. - gofmt -l: nothing. go build ./...; go vet ./... (darwin), GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean. - go test -timeout 45m ./..., through tee, one suite on the machine: 68 packages ok. The one red was internal/gui's TestCoreScriptBudget: app.js 1,335,314 > 1,335,000, since main grew app.js under the stack. The autoscroll commit's own comments were cut (to 1,334,629) rather than the budget raised, and go test -count=1 ./internal/gui is ok at this head (113s). Packages the run took from Go's test cache had the same inputs. - GUI: the 18 files this stack touches or names, both engines, 202 of 204 pass (tree068acf128, this push less the last commit). The two reds: gui-ja, red on main sincec2636b7b5and fixed by this push's last commit (gui-ja and i18n-keys 6 of 6 there), and usage-fold's chromium "several accounts" (#2163, red on main the same way). - Red on the old code: TestAccountNameOfAPluginAccount, with AccountNames put back to a.User, fails with "a gateway key's accounts read [fakeco/31****36@qq.com]". The new GUI test routing-account-name.test.cjs, with the old client.js, fails with "renamed, the account reads by its new name: Kiro example@gmail.com (foo) Pro". - routing-account-name passes, en and zh, both engines. internal/provider, internal/usageapi, internal/cli and internal/gateway ok. - The five usage files this change's first run found red: usage-agents-hide (#2276), usage-checkin (#3091), usage-fold (#2163) and usage-ledger (#3104) are red alone at the stack and on main the same way. usage-overview-fold now passes alone at both. The whole GUI suite ran at 1c1fc7183 (this stack's first 12 commits on0ddcf28a1): 11713 of 11769 pass. Each of the 21 red files was then run alone at the stack and at origin/main8278fd9b0. Eight are red the same way on main: plugin-card-update (#3079), quota-count-wrap and quota-name-translated (#2228), s3-sync (#2448), usage-agents-hide (#2276), usage-checkin (#3091), usage-fold's chromium "several accounts" (#2163), usage-ledger (#3104). The other 13 pass alone on both and were red only under that run's load (50 to 335): currency #3101, plugin-github #3218, provider-fetch-again #3292, rest-try-now #2564, routing-auto-reset #3293, routing-keys-fold #1612, routing-steady #1804, routing-wb-restricted #1905, sessions-folder-delete #2218, sessions-manage #3294, switch-only #3295, text-size #1866, usage-pack #3296. Not tried: a real Kiro account. The fixture is the reporter's case, with their names.
247 lines
7.3 KiB
Go
247 lines
7.3 KiB
Go
package provider
|
|
|
|
// An account's own models (#474): one account of several may be one the
|
|
// user keeps for some of the provider's models only — an account with a
|
|
// small allowance but a verified standing, kept for the cheap models so
|
|
// the big ones don't spend it. Its list is a subset of the provider's;
|
|
// an account without one serves every model the provider does, as before.
|
|
// The gateway never sends a model to an account whose list leaves it out
|
|
// (gateway/fallback.go).
|
|
|
|
import (
|
|
"cmp"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
// AccountServes says whether the account or key ref (a signed-in
|
|
// account's name, a key's KeyID) is let serve model: true when it has no
|
|
// list of its own, and for a model Codex calls by itself that no list can
|
|
// hold (codexOwnHidden).
|
|
func (p Provider) AccountServes(ref, model string) bool {
|
|
ms, ok := p.AccountModels[accountKey(ref)]
|
|
return !ok || len(ms) == 0 || slices.Contains(ms, model) || codexOwnHidden(p, model)
|
|
}
|
|
|
|
// codexOwnHidden says model is one a Codex account serves that its list
|
|
// of models can't name: codex-auto-review, the model Codex's auto-review
|
|
// of an approval (the guardian) asks for. It is hidden from the
|
|
// provider's models, so an account's list, picked from those, never had
|
|
// it, and a list kept for the big models turned every approval away as
|
|
// "every account barred" once Codex's turns went through magpie's
|
|
// account failover (#1959, parkavenue9639). ChatGPT serves it to every
|
|
// plan, free since 2026-10-06 (#1593).
|
|
func codexOwnHidden(p Provider, model string) bool {
|
|
return p.Account != nil && p.Account.Agent == "codex" && strings.EqualFold(strings.TrimSpace(model), "codex-auto-review")
|
|
}
|
|
|
|
// AccountRefs are the provider's accounts, or keys, as AccountModels
|
|
// knows them: a signed-in account's name, a key's KeyID.
|
|
func (p Provider) AccountRefs() []string {
|
|
var out []string
|
|
if p.Account == nil {
|
|
for _, k := range p.KeyList() {
|
|
out = append(out, k.ID)
|
|
}
|
|
return out
|
|
}
|
|
agent := p.Account.Agent
|
|
if p.IsPlugin() {
|
|
agent = p.ID
|
|
}
|
|
if p.Account.User != "" {
|
|
out = append(out, p.Account.User)
|
|
}
|
|
for _, l := range Logins(agent) {
|
|
if !slices.ContainsFunc(out, func(u string) bool { return accountKey(u) == accountKey(l.User) }) {
|
|
out = append(out, l.User)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// AccountRef is one of a provider's accounts or keys as a gateway key's
|
|
// account list knows it (#905): ID what the list keeps — an account's
|
|
// stable id, kept through renames, a key's fingerprint — User and Plan
|
|
// what it is shown as.
|
|
type AccountRef struct {
|
|
ID string
|
|
User string
|
|
Plan string
|
|
Key bool // one of the provider's keys, not a signed-in account
|
|
}
|
|
|
|
// AccountIDs are the provider's accounts and keys as a gateway key's
|
|
// account list knows them (#905): an account by its stable id, a key by
|
|
// its fingerprint, with the name and plan each is shown by.
|
|
func (p Provider) AccountIDs() []AccountRef {
|
|
var out []AccountRef
|
|
if p.Account == nil {
|
|
for _, k := range p.KeyList() {
|
|
out = append(out, AccountRef{ID: k.ID, User: cmp.Or(k.Name, k.Masked), Key: true})
|
|
}
|
|
return out
|
|
}
|
|
agent := p.Account.Agent
|
|
if p.IsPlugin() {
|
|
agent = p.ID
|
|
}
|
|
if p.Account.User != "" {
|
|
out = append(out, AccountRef{ID: LoginID(agent, p.Account.User), User: p.Account.User, Plan: p.Account.Plan})
|
|
}
|
|
for _, l := range Logins(agent) {
|
|
if !slices.ContainsFunc(out, func(r AccountRef) bool { return accountKey(r.User) == accountKey(l.User) }) {
|
|
out = append(out, AccountRef{ID: LoginID(agent, l.User), User: l.User, Plan: l.Plan})
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// AccountID is the stable id of p's account or key as a gateway key's
|
|
// account list knows it (#905): the account's, kept through renames, or
|
|
// a key's fingerprint.
|
|
func (p Provider) AccountID() string {
|
|
if a := p.Account; a != nil && a.User != "" {
|
|
agent := a.Agent
|
|
if p.IsPlugin() {
|
|
agent = p.ID
|
|
}
|
|
return LoginID(agent, a.User)
|
|
}
|
|
return KeyID(p.Key)
|
|
}
|
|
|
|
// AccountNames maps every account and key a gateway key may name (#905),
|
|
// "<provider>/<id>", to how it is shown, "<provider>/<user>", an account
|
|
// by the name the user gave it (AccountShown); what no provider has now
|
|
// maps to itself.
|
|
func AccountNames() map[string]string {
|
|
out := map[string]string{}
|
|
for _, p := range All() {
|
|
for _, a := range p.AccountIDs() {
|
|
out[p.ID+"/"+a.ID] = p.ID + "/" + a.Shown(p)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Shown is how the account or key a is shown on p: an account by the
|
|
// name the user gave it, a key by its own name.
|
|
func (a AccountRef) Shown(p Provider) string {
|
|
if a.Key {
|
|
return a.User
|
|
}
|
|
return p.AccountShown(a.User)
|
|
}
|
|
|
|
// accountRef is the account or key of p that ref names: a signed-in
|
|
// account by its name, a key by its KeyID or its name, in any case.
|
|
func (p Provider) accountRef(ref string) (string, bool) {
|
|
ref = strings.TrimSpace(ref)
|
|
for _, r := range p.AccountRefs() {
|
|
if accountKey(r) == accountKey(ref) {
|
|
return r, true
|
|
}
|
|
}
|
|
if p.Account == nil {
|
|
for _, k := range p.KeyList() {
|
|
if k.Name != "" && strings.EqualFold(k.Name, ref) {
|
|
return k.ID, true
|
|
}
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// SetAccountModels gives the account or key ref of the provider id the
|
|
// models it alone serves; none gives it every model the provider has again.
|
|
func SetAccountModels(id, ref string, models []string) error {
|
|
p, err := Find(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
r, ok := p.accountRef(ref)
|
|
if !ok {
|
|
if p.Account == nil {
|
|
return fmt.Errorf("%s has no key %q", p.Name, ref)
|
|
}
|
|
return fmt.Errorf("%s has no account %q", p.Name, ref)
|
|
}
|
|
ms := normalModels(models)
|
|
if len(ms) == 0 && p.AccountModels[accountKey(r)] == nil {
|
|
return nil
|
|
}
|
|
am := map[string][]string{}
|
|
for k, v := range p.AccountModels {
|
|
am[k] = v
|
|
}
|
|
if len(ms) == 0 {
|
|
delete(am, accountKey(r))
|
|
} else {
|
|
am[accountKey(r)] = ms
|
|
}
|
|
p.AccountModels = am
|
|
return Save(*p)
|
|
}
|
|
|
|
// AccountModelsOf is the models the account or key ref of the provider id
|
|
// alone serves, and the account or key as AccountModels knows it; no
|
|
// models when it serves all the provider's.
|
|
func AccountModelsOf(id, ref string) (string, []string, error) {
|
|
p, err := Find(id)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
r, ok := p.accountRef(ref)
|
|
if !ok {
|
|
return "", nil, fmt.Errorf("%s has no account or key %q", p.Name, ref)
|
|
}
|
|
return r, p.AccountModels[accountKey(r)], nil
|
|
}
|
|
|
|
// SetModels gives the provider id the models the user picked for its
|
|
// agents; none gives them the vendor's list again.
|
|
func SetModels(id string, models []string) error {
|
|
p, err := Find(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ms := normalModels(models)
|
|
if slices.Equal(ms, normalModels(p.Models)) && !p.ServesNone() {
|
|
return nil
|
|
}
|
|
p.Models, p.NoModels = ms, false
|
|
return Save(*p)
|
|
}
|
|
|
|
// normalModels is a list of model ids as it is kept: trimmed, each once,
|
|
// in the order given.
|
|
func normalModels(ms []string) []string {
|
|
var out []string
|
|
for _, m := range ms {
|
|
if m = strings.TrimSpace(m); m != "" && !slices.Contains(out, m) {
|
|
out = append(out, m)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// normalAccountModels is m as it is kept: keys in lower case, lists
|
|
// normalized, accounts serving all the provider's models left out; nil
|
|
// for none.
|
|
func normalAccountModels(m map[string][]string) map[string][]string {
|
|
var out map[string][]string
|
|
for u, ms := range m {
|
|
u, ms = accountKey(u), normalModels(ms)
|
|
if u == "" || len(ms) == 0 {
|
|
continue
|
|
}
|
|
if out == nil {
|
|
out = map[string][]string{}
|
|
}
|
|
out[u] = ms
|
|
}
|
|
return out
|
|
}
|