Files
magpie/internal/gui/caller_keys.go
T
yetone 2b938b170d gateway keys: the key editor offers decision models (Jev, Clef) beside the groups
Kautism on Discord: a gateway key held to a few routing groups couldn't
use Clef or Jev, and they showed nowhere for it. A decision model is a
model: /v1/systemone and the decision list another magpie asks for
(DecidersHeader) hold a key to it as to any model, which stays so (the
restriction covers both routes). But GET /api/caller-keys/models listed
only provider.Catalog, which has no decision models, so the only way to
let a held key ask one was All models.

- caller_keys.go: the picker's list now has OfferedDeciders, each after
  its provider's chat models, marked decides.
- keys.js: such a model's note says it is a decision model that a
  routing group asks whatever the key may use, and that picking it lets
  the key ask it itself; en, zh, zh-TW, ja, de.
- A group the key names still asks its own Jev for every turn with no key
  check (askJev); TestGatewayKeyHeldToAGroupKeepsItsDecider holds that,
  and that the key is refused Jev at /v1/systemone and not listed it
  until it names it, then allowed and listed.
- docs/reference.md and gateway-routing.md say so.

Run on mote at the pre-rebase head; at the pushed head (rebased on
066ece93b, which touches no gui, gateway or access file) TestCallerKey*
and TestGatewayKey* in internal/gui and internal/gateway ran again: ok.
- go test ./internal/gui/: ok except TestMainWindowCorners/macOS_26_SDK,
  the AppKit test red on macOS 27 (#3949), unrelated.
- go test -run 'Key|Decide|Jev|SystemOne|Decider' ./internal/gateway/: ok.
- go vet darwin, GOOS=linux -tags nogui, GOOS=windows for internal/gui
  and internal/gateway: ok. gofmt -l clean.
- GUI: gateway-key-*.test.cjs and i18n*.test.cjs, chromium and webkit:
  136 of 138 ok; the 2 reds are gateway-key-models "en ... wider than
  560" (#4081), red 2 of 3 webkit runs on untouched origin/main
  d24be3c25 too. The new gateway-key-deciders.test.cjs: chromium and
  webkit, 1000px and 400px, all five languages, ok.

Broken by hand: OfferedDeciders(nil)[:0] in the handler, and
TestCallerKeyModelsOfferDeciders went red ("ts/jev-latest not offered");
the key check in serveSystemOne turned off, and
TestGatewayKeyHeldToAGroupKeepsItsDecider went red (200 for Jev).

Not tried: a real Cloudflare Clef or TypeSafe Jev account, another
magpie connected with a held key in a real app, and the whole GUI suite
(mote was at load ~150 with other sessions' suites).
2026-11-02 13:53:19 +08:00

139 lines
5.1 KiB
Go

package gui
import (
"cmp"
"encoding/json"
"net/http"
"slices"
"strings"
"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
// Decides marks a decision model (Jev, Clef…), which no agent lists:
// a key held to some models reaches it at /v1/systemone, and another
// magpie lists it, only when it is picked (Kautism on Discord)
Decides bool `json:"decides,omitempty"`
}
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)})
}
// the decision models the routing groups' pickers offer, each
// after its provider's chat models: the gateway holds a key to
// them as to any model (serveSystemOne, and another magpie's list
// of them), so a key held to some models had no way to be let use
// one but All models (Kautism on Discord: a key held to two
// groups, and Clef and Jev listed nowhere)
for _, e := range provider.OfferedDeciders(nil) {
id := e.Provider.ID + "/" + e.Model
if slices.ContainsFunc(out, func(m keyModelJSON) bool { return strings.EqualFold(m.ID, id) }) {
continue
}
m := keyModelJSON{ID: id, Name: cmp.Or(e.Name, e.Model), Provider: e.Provider.ID, ProviderName: cmp.Or(e.Provider.Name, e.Provider.ID), Decides: true}
at := len(out)
for i := len(out) - 1; i >= 0; i-- {
if out[i].Provider == m.Provider {
at = i + 1
break
}
}
out = slices.Insert(out, at, m)
}
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})
})
}