A key may now hold up to five limits, each in its own window (5 hours
and a week, say). A request needs every one: budget.Reserve checks
each, holds one reservation per window and settles each, and when some
are spent refuses with the spent limit that resets last, so
X-Magpie-Limit-Reset and Retry-After say when the key is really back.
Two limits in one window are refused (change that one instead).
Saved format: the first limit stays in "limit", which older magpie
reads, the rest go in "moreLimits". A file without moreLimits reads as
before.
- access: Change.Index adds (index = len), replaces (keeps its Since)
or removes (nil) one limit; reset-limit-key with an index resets one,
without resets all; the plain limit-key replaces all, as before.
- /v1/magpie/limit: "limit" is the binding one (what older clients
read), "limits" is each. A remote key card shows a window per cap of
each limit and falls back to "limit" from an older remote.
- CLI: magpie gateway-key limit <id> list | add <period> ... |
remove <n> | reset [<n>]; the single-limit form still replaces all.
- GUI: the key's badge names the limit holding it most and "+N limit(s)",
a line per limit under the row, and the editor has a tab per limit
with Add a limit / Remove this limit. Strings in en, zh, zh-TW, ja, de.
- docs/reference.md and docs/subsystems/gateway-routing.md say so.
Run (worktree on e98440ae6, sandbox HOME):
- go test ./internal/access/ ./internal/budget/ ./internal/gateway/
./internal/cli/ ./internal/provider/ ./internal/gui/: all ok.
- gofmt -l clean; go vet darwin, GOOS=linux -tags=nogui, GOOS=windows
on those packages: clean.
- GUI (Playwright 1.62.0) chromium and webkit: the new
gateway-key-limits test (5 languages x 1100/420), gateway-key*,
gateway-caller-keys, gateway-keys-off, gateway-own-key,
gateway-connect-keys: 94/94; i18n-keys ok. quota-name-translated
webkit en is red with HEAD's assets too, filed as #2228.
- Broken by hand, each went red: Reserve checking the first limit only;
the refusal naming the first spent limit, not the latest reset; the
endpoint without "limits"; every limit saved in moreLimits; duplicate
windows allowed; the remote card ignoring "limits"; the CLI plain form
adding instead of replacing; the GUI save without its index; the GUI
badge taking the first limit.
Not tried: against a real remote magpie or real accounts. Known gaps:
two N-day cycle limits are both named "this cycle" on a remote card;
an older magpie that rewrites the keys file keeps only the first limit.
108 lines
3.7 KiB
Go
108 lines
3.7 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 != "" {
|
|
out = append(out, keyModelJSON{ID: e.ID, 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 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.User, 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})
|
|
})
|
|
}
|