Files
magpie/internal/gui/caller_keys.go
T
yetone 4f6498f822 routing, usage: an account renamed in magpie reads by its new name (magpie-community/plugins#310, molvqingtai)
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/main ca4a1cde0, head tree
539c84e92. 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 (tree 068acf128, this push less the last commit). The
  two reds: gui-ja, red on main since c2636b7b5 and 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 on
  0ddcf28a1): 11713 of 11769 pass. Each of the 21 red files was then run
  alone at the stack and at origin/main 8278fd9b0. 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.
2026-10-23 01:58:01 +08:00

112 lines
4.0 KiB
Go

package gui
import (
"cmp"
"encoding/json"
"net/http"
"time"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/budget"
"github.com/yetone/magpie/internal/provider"
)
// keyRow is a gateway key as the Gateway page lists it, with what it has
// used of its limit (#585): Used of its first, UsedAll of each (#2227),
// in the order of its limits.
type keyRow struct {
access.Key
Used *budget.Status `json:"used,omitempty"`
UsedAll []budget.Status `json:"usedAll,omitempty"`
}
// keyModelJSON is a model a gateway key may be held to, as its picker
// lists it.
type keyModelJSON struct {
ID string `json:"id"` // "<provider>/<model>" or "group/<id>", what the key keeps
Name string `json:"name"`
Provider string `json:"provider,omitempty"`
ProviderName string `json:"providerName,omitempty"`
Group bool `json:"group,omitempty"` // a routing group (Magic_zero on Discord)
Account bool `json:"account,omitempty"` // a signed-in account the key may be held to (#905)
Key bool `json:"key,omitempty"` // one of a provider's keys, by its KeyID
Plan string `json:"plan,omitempty"` // the account's subscription, for the menu's note
}
func withLimits(keys []access.Key) []keyRow {
now := time.Now()
out := make([]keyRow, 0, len(keys))
for _, k := range keys {
row := keyRow{Key: k, UsedAll: budget.All(k, now)}
if len(row.UsedAll) > 0 {
row.Used = &row.UsedAll[0]
}
out = append(out, row)
}
return out
}
func callerKeyRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/caller-keys", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
access.MigrateLegacyLANKeyBestEffort()
keys, err := access.List()
if err != nil {
fail(w, err)
return
}
writeJSON(w, map[string]any{"keys": withLimits(keys), "accountNames": provider.AccountNames()})
})
// the models a key may be held to (#882), and the accounts and keys
// (#905): the routing groups, then each provider's models, then its
// accounts and keys; a group the key names is its with every member
mux.HandleFunc("GET /api/caller-keys/models", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
out := []keyModelJSON{}
accounts := []keyModelJSON{}
for _, e := range provider.Catalog() {
if e.Group != "" {
// by the id magpie keeps it by, group/<id>, as a key held to it
// before names it: a found group's auto/<model> passes for it
// (gateway's allows), and its name is what the picker shows
out = append(out, keyModelJSON{ID: provider.GroupPrefix + e.Group, Name: cmp.Or(e.Name, e.Group), Group: true})
continue
}
out = append(out, keyModelJSON{ID: e.Provider.ID + "/" + e.Model, Name: cmp.Or(e.Name, e.Model), Provider: e.Provider.ID, ProviderName: cmp.Or(e.Provider.Name, e.Provider.ID)})
}
for _, p := range provider.All() {
if !p.On() {
continue
}
name := cmp.Or(p.Name, p.ID)
// an account by its stable id, kept through renames, shown by the
// name the user gave it, else who is signed in; a key by its
// fingerprint
for _, a := range p.AccountIDs() {
accounts = append(accounts, keyModelJSON{ID: p.ID + "/" + a.ID, Name: a.Shown(p), Provider: p.ID, ProviderName: name, Account: !a.Key, Key: a.Key, Plan: a.Plan})
}
}
writeJSON(w, map[string]any{"models": out, "accounts": accounts})
})
mux.HandleFunc("POST /api/caller-keys/{action}", func(w http.ResponseWriter, r *http.Request) {
var in access.Change
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(w, err)
return
}
access.MigrateLegacyLANKeyBestEffort()
secret, err := access.Update(r.PathValue("action"), in)
if err != nil {
fail(w, err)
return
}
keys, err := access.List()
if err != nil {
fail(w, err)
return
}
w.Header().Set("Cache-Control", "no-store")
writeJSON(w, map[string]any{"keys": withLimits(keys), "accountNames": provider.AccountNames(), "secret": secret})
})
}