Files
magpie/internal/provider/account_names.go
T
yetone cd93bbec0e provider, gui: a signed-in account can be given a name of the user's own, and a GLM seat's account card keeps the key plan's name and no longer flickers back to two cards (#1515, aindijrncom)
The same GLM Coding Plan team seat was added twice: as a key provider the
user had named, and as a ZCode plugin account. The Usage page kept only the
account's card, which read "31****36@qq.com" or just "ZCode", and whenever
either read failed ("BigModel API keys: not JSON") or a window had no reset
yet, the key's card came back beside it.

Seat (the flicker): the old check returned "different" on any error. It
now returns same, different or unknown (seatMatch). A match is remembered in
seats.json beside quotas.json; while the remembered account is signed in an
unknown check keeps the key card hidden, windows that disagree forget it,
and a signed-out account brings the key card back (seatCards, used by
quotas, Allotted and CachedCards). A failed read is "unknown", never "not
the same".

Names: providers.json gains accountNames (by the account in lower case,
like accountCaps), set by provider.SetAccountName. They are magpie's own
and never written into the vendor's or a plugin's sign-in. Built-in and
plugin accounts both take one; key providers are refused. Each card carries
Alias (the user's name, else the seat) and Seat (the key provider's name,
and the key's when it has several).
- GUI: the editor's account row shows the name with the address beside
  it, or the address with "≡ <key plan>"; a click or Enter renames it in
  place (posted once to provider/accountname; Escape leaves it, empty
  clears it). The name stays selectable text: dragging across it renames
  nothing.
  The Usage page's cards and the tray panel name the account the same way.
  Hide accounts hides the name too. Strings in zh, zh-TW, ja, de.
- The tray tooltip says "ZCode · <name>".
- CLI: `magpie accounts alias <agent> <email> [<name>|--clear]`.
  `magpie accounts` and `magpie quota` show the name; quota also matches
  on it.
- /v1/magpie/quotas carries alias and seat.

Run:
- account_seats_test TestSeatKeptThroughAFailedRead fails on the old code
  ("a failed read brought the key's card back") and passes now.
  TestAccountNameOfAPluginAccount uses the fake plugin and checks that
  plugin-auth.json and logins.json are byte-for-byte unchanged.
  TestAccountsAlias checks that Codex's auth.json is unchanged.
  TestTrayNamesTheAccountByItsAlias.
- account-alias.test.cjs: Chromium and WebKit; en, zh, zh-TW, ja, de;
  900 and 360px; 20/20 pass. It covers no scroll on a click, keyboard,
  selectable text, no select, no stripe, no sideways scroll and masking.
  With the post-once guard removed it goes red. account-arrange (text
  selection) passes in both engines.
- Sandbox HOME, built binary, vendors behind a dead proxy:
  - `magpie accounts alias codex Me@Example.com 某某·GLM高级版`, then
    `magpie accounts` and `magpie quota` showed the name.
  - An unknown account was refused.
  - `magpie web` on spare ports: POST /api/provider/accountname saved
    "Team seat" (whitespace folded), and an unknown account got an error.
  - Gateway /v1/magpie/quotas and /api/usage/quotas carried the alias.
  - auth.json's sha was unchanged throughout.
- go test -tags nogui ./... (temp HOME, load ~200): all ok except
  TestSetAnswersWhileDevinAndProvidersHang (15.5s under load), which
  passes alone 3/3 at ~250ms.
- gofmt -l; GOOS=linux and GOOS=windows go vet on internal/provider,
  internal/gui and the root package.
- The whole GUI suite in Chromium and WebKit, each failed file rerun
  alone. These are red on the parent commit 472b36cec as well:
  mobile-web (9), usage-heatmap (11), old-webkit (Safari 15 :has and
  :focus-visible rules), usage-alert on WebKit (2), and routing-proxy,
  which flakes 2 of 3 runs on the base. Every other file passes alone.
Not run: a real GLM key plus a real ZCode account. The seat match against
real BigModel/ZCode answers is covered only by the fake-upstream tests.
2026-10-11 03:42:35 +08:00

125 lines
3.4 KiB
Go

package provider
// An account's name of the user's own (#1515, aindijrncom): a team's GLM
// seats are told apart by who holds each, while the vendor names a seat
// by a masked email (31****36@qq.com) or not at all. The name is kept in
// providers.json beside the account's other settings (AccountNames), by
// the account's name in lower case; the vendor's or plugin's sign-in is
// never written to. It is shown wherever the account is: the Usage page,
// the menu bar, magpie accounts, magpie quota and /v1/magpie/quotas
// (SubscriptionQuota.Alias, Quota.Alias).
import (
"cmp"
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
// MaxAccountName bounds an account's name, in characters.
const MaxAccountName = 64
// cleanAccountName is a name as it is kept: spaces trimmed and run
// together, no control characters; "" for none.
func cleanAccountName(s string) string {
s = strings.Map(func(r rune) rune {
if unicode.IsControl(r) {
return ' '
}
return r
}, s)
return strings.Join(strings.Fields(s), " ")
}
// normalAccountNames is m as it is kept: keys in lower case, names
// cleaned, accounts with none left out; nil for none.
func normalAccountNames(m map[string]string) map[string]string {
var out map[string]string
for u, v := range m {
u, v = accountKey(u), cleanAccountName(v)
if u == "" || v == "" || utf8.RuneCountInString(v) > MaxAccountName {
continue
}
if out == nil {
out = map[string]string{}
}
out[u] = v
}
return out
}
// AccountName is the name the user gave the account user of p, "" when
// none. As with AccountCap, an account made afresh beside the agent's own
// reads it from providers.json.
func (p Provider) AccountName(user string) string {
names := p.AccountNames
if names == nil {
if s, ok := storedPicks(p.ID); ok {
names = s.AccountNames
}
}
return names[accountKey(user)]
}
// AccountNameOf is the name the user gave the account user of the
// subscription id, "" when none.
func AccountNameOf(id, user string) string {
if user == "" {
return ""
}
if s, ok := storedPicks(id); ok {
return s.AccountNames[accountKey(user)]
}
return ""
}
// SetAccountName names the account ref of the subscription id; "" takes
// the name off.
func SetAccountName(id, ref, name string) error {
name = cleanAccountName(name)
if n := utf8.RuneCountInString(name); n > MaxAccountName {
return fmt.Errorf("an account's name is at most %d characters, not %d", MaxAccountName, n)
}
p, err := Find(id)
if err != nil {
return err
}
if p.Account == nil {
return fmt.Errorf("%s has keys, not signed-in accounts; a key is named in its own row", p.Name)
}
r, ok := p.accountRef(ref)
if !ok {
return fmt.Errorf("%s has no account %q", p.Name, ref)
}
k := accountKey(r)
if p.AccountNames[k] == name {
return nil
}
names := map[string]string{}
for u, v := range p.AccountNames {
names[u] = v
}
if name == "" {
delete(names, k)
} else {
names[k] = name
}
p.AccountNames = names
return Save(*p)
}
// withAccountNames is qs with each account's name of the user's own on
// its card, else the name of the key plan it stands for (Seat), as
// Alias. qs is not changed: it is a cache's.
func withAccountNames(qs []SubscriptionQuota) []SubscriptionQuota {
out := make([]SubscriptionQuota, len(qs))
for i, q := range qs {
if q.From == "" {
q.Alias = cmp.Or(AccountNameOf(q.Provider, q.User), q.Seat)
}
out[i] = q
}
return out
}