aiandcloud (2.98B tokens, 12,654 requests a day, server mode behind a
proxy) saw Gateway keys draw a Cloudflare 524 page as raw HTML.
Slow list: every poll of /api/caller-keys read up to a month of ledger
rows once per window per key. budget.AllOf now reads each distinct window
once for all keys, and keeps the settled blocks' sums with a cursor
(usage.VisitFrom), so a poll only reads what was appended since; the
unfinished tail block is always read again. A rewritten or shorter log
resets the cursor. All(k) is AllOf of one key.
Numbers (BenchmarkAll vs BenchmarkReadEach, 8 keys x day/week/month,
31 days x 12000 rows/day = 372k rows, warm): 211-228 ms before, 23.5 ms
after.
Raw HTML: fail() now turns an error string holding a web page (a remote
magpie's or a vendor's 524/502 page inside the JSON error) into a short
said/what pair, "A proxy in front of magpie answered, not magpie ({what})"
with the page's title, translated in all 5 languages (key already in
i18n.js). The browser already handled non-JSON bodies (notJSON, #2032).
Run (Windows box, mote ssh was down earlier): go test budget, usage, gui
(AllOf, Budget, FailSays, I18n, Keys); gateway-keys-proxy-page and
gateway-key-limit GUI files in chromium and webkit, 5 languages: 24/24;
gofmt; vet windows, linux nogui, darwin (mote, only a clang deprecation
warning). Broken by hand: with the old fail() TestFailSaysAProxyPageByItsTitle
is red; with VisitFrom's reset() removed TestAllOfIsWhatEachWindowRead is red.
Not tried: a user-scale cold ledger, a server behind a real Cloudflare,
the full suite before the tag (started after).
273 lines
9.7 KiB
Go
273 lines
9.7 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/catalog"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/library"
|
|
"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"`
|
|
}
|
|
|
|
// keyMCPJSON is an MCP server a gateway key may be held to, with its
|
|
// tools, as its picker lists it.
|
|
type keyMCPJSON struct {
|
|
Name string `json:"name"`
|
|
Title string `json:"title,omitempty"`
|
|
Tools []string `json:"tools"`
|
|
Own bool `json:"own,omitempty"` // one of magpie's own
|
|
}
|
|
|
|
func withLimits(keys []access.Key) []keyRow {
|
|
now := time.Now()
|
|
out := make([]keyRow, 0, len(keys))
|
|
all := budget.AllOf(keys, now)
|
|
for i, k := range keys {
|
|
row := keyRow{Key: k, UsedAll: all[i]}
|
|
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(), "accountGroups": accountGroupsJSON()})
|
|
})
|
|
// 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 models a routing group listing alone keeps out of the lists,
|
|
// each after its provider's: a key not given the group is listed
|
|
// those it may use (the gateway's keyListed), so the picker offers
|
|
// them to pick one at a time too (luckymanjc on Discord: Codex
|
|
// offered GPT-6-Luna alone)
|
|
for _, s := range provider.GroupStandIns() {
|
|
e := s.Entry
|
|
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)}
|
|
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)
|
|
}
|
|
// 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)
|
|
}
|
|
// the image and video models, which Catalog leaves out as agents
|
|
// don't chat with them: /v1/images and /v1/videos hold a key to its
|
|
// models as chat does (keyHolds), so a key held to some models had
|
|
// no way to be let draw but All models (aipha2025 on X)
|
|
for _, p := range provider.All() {
|
|
if !p.On() {
|
|
continue
|
|
}
|
|
var media []catalog.Model
|
|
media = append(media, gateway.Drawers(p)...)
|
|
media = append(media, gateway.Videomakers(p)...)
|
|
for _, e := range media {
|
|
id := p.ID + "/" + e.ID
|
|
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.ID), Provider: p.ID, ProviderName: cmp.Or(p.Name, p.ID)}
|
|
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})
|
|
}
|
|
}
|
|
// the account groups a key's accounts may name, "@<id>" or "!@<id>"
|
|
// (LeiWang_2000 on X), each with its accounts
|
|
writeJSON(w, map[string]any{"models": out, "accounts": accounts, "groups": accountGroupsJSON()})
|
|
})
|
|
// the MCP servers and tools a key may be held to (wakaka on Discord):
|
|
// magpie's own, then the Library's, each with its tools by name
|
|
mux.HandleFunc("GET /api/caller-keys/mcp", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
out := []keyMCPJSON{}
|
|
for _, o := range gateway.OwnMCPTools(r.Context()) {
|
|
m := keyMCPJSON{Name: o.Name, Title: o.Title, Tools: o.Tools, Own: true}
|
|
if m.Tools == nil {
|
|
m.Tools = []string{}
|
|
}
|
|
out = append(out, m)
|
|
}
|
|
for _, s := range library.KeyMCPServers() {
|
|
m := keyMCPJSON{Name: s.Name, Title: s.Title, Tools: []string{}}
|
|
for _, t := range s.Tools {
|
|
m.Tools = append(m.Tools, t.Name)
|
|
}
|
|
out = append(out, m)
|
|
}
|
|
writeJSON(w, map[string]any{"servers": out})
|
|
})
|
|
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(), "accountGroups": accountGroupsJSON(), "secret": secret})
|
|
})
|
|
// the account groups a key's accounts may name, "@<id>" or "!@<id>"
|
|
// (LeiWang_2000 on X): save adds one (no id) or changes it, remove
|
|
// takes one off unless a key names it
|
|
mux.HandleFunc("GET /api/account-groups", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
gs, err := access.AccountGroups()
|
|
if err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, map[string]any{"groups": gs})
|
|
})
|
|
mux.HandleFunc("POST /api/account-groups/{action}", func(w http.ResponseWriter, r *http.Request) {
|
|
var in access.AccountGroup
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
var err error
|
|
switch r.PathValue("action") {
|
|
case "save":
|
|
_, err = access.SaveAccountGroup(in)
|
|
case "remove":
|
|
err = access.RemoveAccountGroup(in.ID)
|
|
default:
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
gs, err := access.AccountGroups()
|
|
if err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
writeJSON(w, map[string]any{"groups": gs})
|
|
})
|
|
}
|
|
|
|
// accountGroupsJSON are the account groups for the keys' rows, none when
|
|
// they can't be read (a key naming one still holds: identityOf).
|
|
func accountGroupsJSON() []access.AccountGroup {
|
|
gs, err := access.AccountGroups()
|
|
if err != nil || gs == nil {
|
|
return []access.AccountGroup{}
|
|
}
|
|
return gs
|
|
}
|