Steven's ESP32 desk display polls magpie (Docker, on his LAN) every 15s on GET /v1/magpie/quotas and /v1/magpie/usage and never sends a model request. Only a key held to nothing was told the usage summary, so the key kept in the device's flash had to be one that could spend every account. A gateway key can now be read-only (access.Key.ReadOnly): - `magpie gateway-key add <name> --read-only`, `magpie gateway-key access <id> [read-only|full]` (`gateway-key --help` lists it; `list` has an ACCESS column), and "Make read-only" / "Give full access" in the key's menu in Gateway -> Gateway keys, with a "Read only" tag on its row. - readOnlyGuard (internal/gateway/readonly.go), after callerGuard and before withCaller/keyLimited, judges the pattern ServeMux.Handler routes the request to. A read-only key is answered GET /, /v1, the quotas, their history and cards, the usage summary and its requests, its limit and the concurrency. Every other route is refused with 403 gateway_key_read_only: every model route, count_tokens, the Responses WebSocket's upgrade, every MCP server, Codex's and Gemini's backends, /v1/models and the Ollama routes, the catch-all, the trace and route, debug profiles, and every write (usage ingest, quotas refresh). It is default deny, so a route added later is refused until it is classified. - The key is held to nothing else, so heldKey leaves it alone: it reads the usage summary and every account's quotas, as a key held to nothing does. None of those routes holds a key's secret. - The sharing key can't be made read-only, since magpie gives it to its own agents (LANSecret). Update refuses it, and the GUI item is off with why. Older versions: a magpie from before this commit doesn't know `readOnly` and treats such a key as a full one, so downgrading magpie widens the key. Tests: - TestReadOnlyKeyEveryRoute (internal/gateway/readonly_test.go) takes the patterns from the router itself (routes() records each HandleFunc). It fails when a pattern is in neither readOnlyRoutes nor its refused list. It sends every refused pattern, by each method it takes, from 192.168.1.9 and from loopback, plus a WebSocket upgrade and an unclean chat path, and expects 403 gateway_key_read_only with the upstream never reached. It checks that every allowed route answers, that no body holds any key's secret or the upstream key, that the usage summary names the other key's use, and that a full key and a key made full again still reach the model. - TestReadOnlyKey (access) covers add-key and access-key on and off, Authenticate, List, and the sharing key refused. - TestGatewayKeyCLIReadOnly covers --read-only before and after the name and beside --key, access, the list column, bad values, and --help. - gateway-key-readonly.test.cjs runs Chromium and WebKit, 1100 and 420px, in en/zh/zh-TW/ja/de. It covers the tag, both menu items and what they post, the status line, the sharing key's item off with its why, no scroll on click, no sideways scroll, no select, and no left stripe. gateway-card.test.cjs now expects the menu's 4th item. - TestClientPathCoversHandler takes the patterns from routes() instead of parsing Handler's source, which moved into routes(). Broken by hand: - readOnlyGuard passing everything: TestReadOnlyKeyEveryRoute red (103 lines, /v1/models 200 onward). - A new `POST /v1/new-thing` route in routes(): red, "in neither readOnlyRoutes nor readOnlyRefused". - The GUI item not off for the sharing key: gateway-key-readonly red 10 of 10 in Chromium. Run at the pushed head (on origin/main0911a65e7), on mote: TestReadOnly*, TestHeldKey*, TestClientPathCoversHandler, TestGatewayKey*, TestCaller* in access, cli and gateway ok; TestCoreScriptBudget ok; on Linux (wsl-run, both engines) gateway-key-readonly, gateway-card, i18n-keys, gateway-caller-keys and gateway-key-quotas 68/68. Full Go suite on mote (macOS) at the same change on origin/maindca3e2d71(94630237d), -timeout 45m: 78 packages ok; internal/gateway red only on the four TestAntigravityTurned* and TestCodexSpentGrokFailingIsTold (red the same at origin/main, #4362); internal/gui red only on ten AppKit window tests that hit their 30s bar while mote's launchd throttled every app launch (#4125). Run on mote (macOS) on origin/maindca3e2d71: - go test ./internal/access ./internal/cli ./internal/gateway: access ok; cli red only on TestLibraryMCPHub's TempDir cleanup (#4140); gateway red only on the four TestAntigravityTurned* and TestCodexSpentGrokFailingIsTold, red the same at origin/main (#4362). TestReadOnly*, TestHeldKey*, TestClientPathCoversHandler, TestGatewayKeyCLIReadOnly pass. - TestCoreScriptBudget ok (no budget raised). gofmt clean; go vet darwin (access, cli, gateway), GOOS=windows ./... and GOOS=linux -tags nogui ./... clean. - GUI, earlier at807048470on mote: gateway-key-readonly, gateway-key-quotas, i18n-keys, gateway-card, api-key-theme, gateway-connect-keys and gateway-caller-keys 34/34 in both engines. At this tree mote's WebKit hung at launch for every test (#4125), so the 43 gateway-*/key-*/api-key-*/caller-*/menu-*/i18n-* files ran on Linux (wsl-run, both engines): 501 of 514. The reds in api-key-theme, gateway-key-limit, gateway-key-models, key-replace and menu-input-scroll are red the same at origin/maine8d42c02d(#4120, #3523, #4081, #4126, #3402); gateway-keys-empty's WebKit wait was red 1 in ~13 runs here and 0 in 4 at main, filed as #4383 (this change doesn't touch its path). - Full Go suite on Linux (wsl-run, at807048470with this change): reds only where Linux can't build the GUI (no gtk4/webkitgtk) and in internal/agent's install tests (#4033) and the gateway reds above, each red the same at origin/main dca3e2d71; plus TestClientPathCoversHandler, which parsed Handler's source and is now read from routes() (fixed here). Not tried: a real ESP32 or Docker magpie; a remote client against a running gateway (only httptest through lanGuard/Handler); the Windows test runs (vet only); the whole GUI suite (mote's WebKit hung, #4125).
561 lines
18 KiB
Go
561 lines
18 KiB
Go
package cli
|
||
|
||
import (
|
||
"cmp"
|
||
"fmt"
|
||
"io"
|
||
"os"
|
||
"slices"
|
||
"strconv"
|
||
"strings"
|
||
"text/tabwriter"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/access"
|
||
"github.com/yetone/magpie/internal/budget"
|
||
"github.com/yetone/magpie/internal/provider"
|
||
)
|
||
|
||
func gatewayKeys(args []string) error { return gatewayKeysTo(os.Stdout, args) }
|
||
|
||
func gatewayKeysTo(out io.Writer, args []string) error {
|
||
args = args[1:]
|
||
if len(args) == 0 {
|
||
args = []string{"list"}
|
||
}
|
||
action := args[0]
|
||
if action == "limit" {
|
||
access.MigrateLegacyLANKeyBestEffort()
|
||
return gatewayKeyLimit(out, args[1:])
|
||
}
|
||
if action == "models" {
|
||
access.MigrateLegacyLANKeyBestEffort()
|
||
return gatewayKeyModels(out, args[1:])
|
||
}
|
||
if action == "accounts" {
|
||
access.MigrateLegacyLANKeyBestEffort()
|
||
return gatewayKeyAccounts(out, args[1:])
|
||
}
|
||
if action == "quotas" {
|
||
access.MigrateLegacyLANKeyBestEffort()
|
||
return gatewayKeyQuotas(out, args[1:])
|
||
}
|
||
if action == "access" {
|
||
access.MigrateLegacyLANKeyBestEffort()
|
||
return gatewayKeyAccess(out, args[1:])
|
||
}
|
||
// add <name> --read-only: a key that reads stats and uses no model
|
||
// (Steven on Discord: a desk display)
|
||
readOnly := false
|
||
if action == "add" {
|
||
if i := slices.Index(args[1:], "--read-only"); i >= 0 {
|
||
readOnly = true
|
||
args = slices.Delete(slices.Clone(args), 1+i, 2+i)
|
||
}
|
||
}
|
||
// add <name> --key <value>: the key keeps a value its clients already
|
||
// send (love1sbug on X); "--key -" reads it from stdin, out of the
|
||
// shell's history
|
||
var own string
|
||
if action == "add" && len(args) == 4 && args[2] == "--key" {
|
||
own = args[3]
|
||
if own == "-" {
|
||
b, err := io.ReadAll(io.LimitReader(os.Stdin, 4096))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
own = strings.TrimSpace(string(b))
|
||
if own == "" {
|
||
return fmt.Errorf("no key came on stdin")
|
||
}
|
||
}
|
||
args = args[:2]
|
||
}
|
||
if (action == "list" && len(args) != 1) || (action != "list" && len(args) != 2) {
|
||
return fmt.Errorf("usage: magpie gateway-key list | add <name> [--key <value> | --key -] [--read-only] | rotate <id> | remove <id> | limit <id> [list | off | reset [<n>] | remove <n> | [add] day|week|month|<N>d|<N>h [--tokens N] [--cost USD] [--cache-reads]] | models <id> [all | <provider>/<model>|<provider>/* ...] | accounts <id> [all | <provider>/<account>|<provider>/<key id> ...] | quotas <id> [on | off] | access <id> [read-only | full]")
|
||
}
|
||
switch action {
|
||
case "list", "add", "rotate", "remove":
|
||
default:
|
||
return fmt.Errorf("unknown gateway-key command %q", action)
|
||
}
|
||
access.MigrateLegacyLANKeyBestEffort()
|
||
if action == "list" {
|
||
keys, err := access.List()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
w := tabwriter.NewWriter(out, 0, 4, 2, ' ', 0)
|
||
fmt.Fprintln(w, "ID\tNAME\tSTATE\tGATEWAY KEY\tLIMIT\tMODELS\tACCOUNTS\tACCESS")
|
||
now := time.Now()
|
||
// an account is shown by who is signed in, kept by its stable id (#905)
|
||
names := provider.AccountNames()
|
||
for _, k := range keys {
|
||
state := "enabled"
|
||
if k.Off {
|
||
state = "disabled"
|
||
}
|
||
// a key that names accounts and no model may use only its
|
||
// accounts' providers' models (access.HeldModels)
|
||
models := "all"
|
||
if ms := access.HeldModels(k.Models, k.Accounts); len(ms) > 0 {
|
||
models = strings.Join(ms, ",")
|
||
}
|
||
accounts := "all"
|
||
if len(k.Accounts) > 0 {
|
||
shown := make([]string, len(k.Accounts))
|
||
for i, a := range k.Accounts {
|
||
shown[i] = cmp.Or(names[a], a)
|
||
}
|
||
accounts = strings.Join(shown, ",")
|
||
}
|
||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", k.ID, strings.Map(func(r rune) rune {
|
||
if r < 32 || r == 127 {
|
||
return ' '
|
||
}
|
||
return r
|
||
}, k.Name), state, k.Masked, limitWords(k, now), models, accounts, accessWord(k))
|
||
}
|
||
return w.Flush()
|
||
}
|
||
in := access.Change{Key: args[1]}
|
||
if action == "add" {
|
||
in = access.Change{Name: args[1], Secret: own, ReadOnly: readOnly}
|
||
}
|
||
secret, err := access.Update(action+"-key", in)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if secret != "" {
|
||
_, err = fmt.Fprintln(out, secret)
|
||
} else {
|
||
_, err = fmt.Fprintln(out, "Gateway key removed")
|
||
}
|
||
return err
|
||
}
|
||
|
||
// gatewayKeyLimit shows or sets a key's limits (#585, several since #2227):
|
||
//
|
||
// magpie gateway-key limit <id> [list] what it has used of each limit
|
||
// magpie gateway-key limit <id> off no limit
|
||
// magpie gateway-key limit <id> day|week|month|<N>d|<N>h [--tokens N] [--cost USD] [--cache-reads]
|
||
// its only limit, or the one of several with that window
|
||
// magpie gateway-key limit <id> add <window> [--tokens N] [--cost USD] [--cache-reads]
|
||
// one more limit; a request passes only while every one has something left
|
||
// magpie gateway-key limit <id> remove <n> take off the nth limit
|
||
// magpie gateway-key limit <id> reset [<n>] count from 0 again, every limit or the nth; a new N-day or N-hour cycle from now (#1509)
|
||
func gatewayKeyLimit(out io.Writer, args []string) error {
|
||
usage := fmt.Errorf("usage: magpie gateway-key limit <id> [off | reset [<n>] | remove <n> | [add] day|week|month|<N>d|<N>h [--tokens N] [--cost USD] [--cache-reads]]")
|
||
if len(args) == 0 {
|
||
return usage
|
||
}
|
||
id := args[0]
|
||
if len(args) > 1 {
|
||
keys, err := access.List()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
i := slices.IndexFunc(keys, func(k access.Key) bool { return k.ID == id })
|
||
if i < 0 {
|
||
return fmt.Errorf("Key not found")
|
||
}
|
||
have := keys[i].Limits()
|
||
// nth reads a limit's number as the list shows it, from 1
|
||
nth := func(v string) (*int, error) {
|
||
n, err := strconv.Atoi(v)
|
||
if err != nil || n < 1 || n > len(have) {
|
||
if len(have) == 0 {
|
||
return nil, fmt.Errorf("%s has no limit", keys[i].Name)
|
||
}
|
||
return nil, fmt.Errorf("a limit's number is 1 to %d, as `magpie gateway-key limit %s` lists them: %q", len(have), id, v)
|
||
}
|
||
n--
|
||
return &n, nil
|
||
}
|
||
switch args[1] {
|
||
case "list":
|
||
if len(args) > 2 {
|
||
return usage
|
||
}
|
||
case "reset":
|
||
in := access.Change{Key: id}
|
||
if len(args) > 3 {
|
||
return usage
|
||
}
|
||
if len(args) == 3 {
|
||
if in.Index, err = nth(args[2]); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
if _, err := access.Update("reset-limit-key", in); err != nil {
|
||
return err
|
||
}
|
||
case "off":
|
||
if len(args) > 2 {
|
||
return usage
|
||
}
|
||
if _, err := access.Update("limit-key", access.Change{Key: id}); err != nil {
|
||
return err
|
||
}
|
||
case "remove":
|
||
if len(args) != 3 {
|
||
return usage
|
||
}
|
||
n, err := nth(args[2])
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if _, err := access.Update("limit-key", access.Change{Key: id, Index: n}); err != nil {
|
||
return err
|
||
}
|
||
case "add":
|
||
if len(args) < 3 {
|
||
return usage
|
||
}
|
||
lim, err := parseKeyLimit(args[2], args[3:], usage)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
n := len(have)
|
||
if _, err := access.Update("limit-key", access.Change{Key: id, Limit: lim, Index: &n}); err != nil {
|
||
return err
|
||
}
|
||
default:
|
||
lim, err := parseKeyLimit(args[1], args[2:], usage)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
in := access.Change{Key: id, Limit: lim}
|
||
// of several, the one counted in the same window is set; the
|
||
// only one is replaced, as before #2227
|
||
if len(have) > 1 {
|
||
v, _ := lim.Valid()
|
||
n := slices.IndexFunc(have, func(l *access.Limit) bool {
|
||
return v != nil && l.Period == v.Period && l.Days == v.Days && l.Hours == v.Hours
|
||
})
|
||
if n < 0 {
|
||
return fmt.Errorf("%s has %d limits and none %s: add one with `magpie gateway-key limit %s add %s ...`, or take one off with `remove <n>`", keys[i].Name, len(have), perPhrase(v), id, args[1])
|
||
}
|
||
in.Index = &n
|
||
}
|
||
if _, err := access.Update("limit-key", in); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
}
|
||
keys, err := access.List()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
i := slices.IndexFunc(keys, func(k access.Key) bool { return k.ID == id })
|
||
if i < 0 {
|
||
return fmt.Errorf("Key not found")
|
||
}
|
||
k, now := keys[i], time.Now()
|
||
all := budget.All(k, now)
|
||
if len(all) == 0 {
|
||
_, err = fmt.Fprintf(out, "%s: no limit\n", k.Name)
|
||
return err
|
||
}
|
||
w := tabwriter.NewWriter(out, 0, 4, 2, ' ', 0)
|
||
fmt.Fprintf(w, "Key\t%s (%s)\n", k.Name, k.ID)
|
||
for n, st := range all {
|
||
if len(all) > 1 {
|
||
fmt.Fprintf(w, "\t\n")
|
||
fmt.Fprintf(w, "Limit %d\t%s, all must have something left\n", n+1, perPhrase(&access.Limit{Period: st.Period, Days: st.Days, Hours: st.Hours}))
|
||
}
|
||
fmt.Fprintf(w, "Window\tthis %s, %s – %s (local time)\n", (&access.Limit{Period: st.Period, Days: st.Days, Hours: st.Hours}).Per(), st.Start.Format("2006-01-02 15:04"), st.Reset.Format("2006-01-02 15:04"))
|
||
if st.TokenLimit > 0 {
|
||
counted := "input, output and cache writes"
|
||
if st.CacheReads {
|
||
counted += ", and cache reads"
|
||
}
|
||
fmt.Fprintf(w, "Tokens\t%d used of %d, %d left (%s)\n", st.Tokens, st.TokenLimit, st.TokensLeft, counted)
|
||
} else {
|
||
fmt.Fprintf(w, "Tokens\t%d used, no cap\n", st.Tokens)
|
||
}
|
||
unpriced := ""
|
||
if st.Unpriced > 0 {
|
||
unpriced = fmt.Sprintf(", %d calls without a known price not in it", st.Unpriced)
|
||
}
|
||
if st.CostLimit > 0 {
|
||
fmt.Fprintf(w, "Cost\t$%.2f used of $%.2f, $%.2f left (estimate at Usage prices%s)\n", st.Cost, st.CostLimit, st.CostLeft, unpriced)
|
||
} else {
|
||
fmt.Fprintf(w, "Cost\t$%.2f used, no cap (estimate%s)\n", st.Cost, unpriced)
|
||
}
|
||
state := "open"
|
||
if st.Spent {
|
||
state = "spent: requests are refused until " + st.Reset.Format("2006-01-02 15:04")
|
||
}
|
||
fmt.Fprintf(w, "State\t%s\n", state)
|
||
}
|
||
return w.Flush()
|
||
}
|
||
|
||
// perPhrase is a limit's window as the CLI says it: "per week", "every 5
|
||
// hours".
|
||
func perPhrase(l *access.Limit) string {
|
||
if l == nil {
|
||
return ""
|
||
}
|
||
switch l.Period {
|
||
case "days":
|
||
return fmt.Sprintf("every %d days", l.Days)
|
||
case "hours":
|
||
return fmt.Sprintf("every %d hours", l.Hours)
|
||
}
|
||
return "per " + l.Period
|
||
}
|
||
|
||
// parseKeyLimit reads a limit as typed: its window, then --tokens,
|
||
// --cost and --cache-reads.
|
||
func parseKeyLimit(window string, rest []string, usage error) (*access.Limit, error) {
|
||
lim := &access.Limit{Period: window}
|
||
if window == "day" || window == "week" || window == "month" {
|
||
// a period by its name: month ends in an h, and is no count of hours
|
||
} else if n, ok := strings.CutSuffix(window, "d"); ok {
|
||
days, err := strconv.Atoi(n)
|
||
if err != nil || days < 1 || days > access.MaxDays {
|
||
return nil, fmt.Errorf("an N-day cycle is a whole number of days from 1 to %d, like 10d: %q", access.MaxDays, window)
|
||
}
|
||
lim = &access.Limit{Period: "days", Days: days}
|
||
} else if n, ok := strings.CutSuffix(window, "h"); ok {
|
||
// every N hours (#2102), a subscription's 5-hour window's pace
|
||
hours, err := strconv.Atoi(n)
|
||
if err != nil || hours < 1 || hours > access.MaxHours {
|
||
return nil, fmt.Errorf("an N-hour cycle is a whole number of hours from 1 to %d, like 5h: %q", access.MaxHours, window)
|
||
}
|
||
lim = &access.Limit{Period: "hours", Hours: hours}
|
||
} else if !slices.Contains(access.Periods, lim.Period) || lim.Period == "days" || lim.Period == "hours" {
|
||
return nil, fmt.Errorf("a limit's period is day, week, month, <N>d (every N days) or <N>h (every N hours), not %q", lim.Period)
|
||
}
|
||
for i := 0; i < len(rest); i++ {
|
||
flag, val, has := strings.Cut(rest[i], "=")
|
||
next := func() (string, error) {
|
||
if has {
|
||
return val, nil
|
||
}
|
||
if i+1 >= len(rest) {
|
||
return "", fmt.Errorf("%s needs a value", flag)
|
||
}
|
||
i++
|
||
return rest[i], nil
|
||
}
|
||
switch flag {
|
||
case "--tokens":
|
||
v, err := next()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
n, err := parseTokens(v)
|
||
if err != nil || n < 0 {
|
||
return nil, fmt.Errorf("--tokens is a count, like 1000000 or 2m: %q", v)
|
||
}
|
||
lim.Tokens = int64(n)
|
||
case "--cost":
|
||
v, err := next()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if lim.Cost, err = strconv.ParseFloat(strings.TrimPrefix(v, "$"), 64); err != nil {
|
||
return nil, fmt.Errorf("--cost is US dollars, like 5 or 2.50: %q", v)
|
||
}
|
||
case "--cache-reads":
|
||
lim.CacheReads = true
|
||
default:
|
||
return nil, usage
|
||
}
|
||
}
|
||
if !lim.Limited() {
|
||
return nil, fmt.Errorf("say --tokens, --cost or both, or off for no limit")
|
||
}
|
||
return lim, nil
|
||
}
|
||
|
||
// gatewayKeyModels shows or sets the models a key may use (#882):
|
||
//
|
||
// magpie gateway-key models <id> the models it may use
|
||
// magpie gateway-key models <id> all every model
|
||
// magpie gateway-key models <id> openai/gpt-5 anthropic/* only these
|
||
func gatewayKeyModels(out io.Writer, args []string) error {
|
||
if len(args) == 0 {
|
||
return fmt.Errorf("usage: magpie gateway-key models <id> [all | <provider>/<model>|<provider>/* ...]")
|
||
}
|
||
id := args[0]
|
||
if len(args) > 1 {
|
||
ms := args[1:]
|
||
if len(ms) == 1 && ms[0] == "all" {
|
||
ms = nil
|
||
}
|
||
if _, err := access.Update("models-key", access.Change{Key: id, Models: ms}); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
keys, err := access.List()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
i := slices.IndexFunc(keys, func(k access.Key) bool { return k.ID == id })
|
||
if i < 0 {
|
||
return fmt.Errorf("Key not found")
|
||
}
|
||
k := keys[i]
|
||
if len(k.Models) == 0 && len(k.Accounts) > 0 {
|
||
_, err = fmt.Fprintf(out, "%s: only its accounts' providers' models, %s\n", k.Name, strings.Join(access.HeldModels(nil, k.Accounts), ", "))
|
||
return err
|
||
}
|
||
if len(k.Models) == 0 {
|
||
_, err = fmt.Fprintf(out, "%s: every model\n", k.Name)
|
||
return err
|
||
}
|
||
_, err = fmt.Fprintf(out, "%s: only %s\n", k.Name, strings.Join(k.Models, ", "))
|
||
return err
|
||
}
|
||
|
||
// limitWords is a key's limits in the list: "-" for none, else what it
|
||
// has used of each, "1200/1000000 tokens per day; $1.00/$20.00 per week".
|
||
func limitWords(k access.Key, now time.Time) string {
|
||
all := budget.All(k, now)
|
||
if len(all) == 0 {
|
||
return "-"
|
||
}
|
||
var each []string
|
||
for _, st := range all {
|
||
var parts []string
|
||
if st.TokenLimit > 0 {
|
||
parts = append(parts, fmt.Sprintf("%d/%d tokens", st.Tokens, st.TokenLimit))
|
||
}
|
||
if st.CostLimit > 0 {
|
||
parts = append(parts, fmt.Sprintf("$%.2f/$%.2f", st.Cost, st.CostLimit))
|
||
}
|
||
s := strings.Join(parts, " ") + " " + perPhrase(&access.Limit{Period: st.Period, Days: st.Days, Hours: st.Hours})
|
||
if st.Spent {
|
||
s += " (spent until " + st.Reset.Format("01-02 15:04") + ")"
|
||
}
|
||
each = append(each, s)
|
||
}
|
||
return strings.Join(each, "; ")
|
||
}
|
||
|
||
// gatewayKeyAccounts shows or sets a key's account whitelist (#905): the
|
||
// accounts and keys it may use, "<provider>/<account>" — a signed-in
|
||
// account by its name, a key by its KeyID — every one when none are set.
|
||
func gatewayKeyAccounts(out io.Writer, args []string) error {
|
||
if len(args) == 0 {
|
||
return fmt.Errorf("usage: magpie gateway-key accounts <id> [all | <provider>/<account>|<provider>/<key id> ...]")
|
||
}
|
||
id := args[0]
|
||
if len(args) > 1 {
|
||
as := args[1:]
|
||
if len(as) == 1 && as[0] == "all" {
|
||
as = nil
|
||
}
|
||
if _, err := access.Update("accounts-key", access.Change{Key: id, Accounts: as}); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
keys, err := access.List()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
i := slices.IndexFunc(keys, func(k access.Key) bool { return k.ID == id })
|
||
if i < 0 {
|
||
return fmt.Errorf("Key not found")
|
||
}
|
||
k := keys[i]
|
||
if len(k.Accounts) == 0 {
|
||
_, err = fmt.Fprintf(out, "%s: every account\n", k.Name)
|
||
return err
|
||
}
|
||
// shown by who is signed in, kept by the account's stable id (#905)
|
||
names := provider.AccountNames()
|
||
shown := make([]string, len(k.Accounts))
|
||
for i, a := range k.Accounts {
|
||
shown[i] = cmp.Or(names[a], a)
|
||
}
|
||
_, err = fmt.Fprintf(out, "%s: only %s\n", k.Name, strings.Join(shown, ", "))
|
||
return err
|
||
}
|
||
|
||
// gatewayKeyQuotas shows or sets whether a key held to a budget, models
|
||
// or accounts is told the quotas of what it may use (#2216): the cards of
|
||
// the providers whose every model it may use, of its accounts and keys.
|
||
//
|
||
// magpie gateway-key quotas <id> whether it is
|
||
// magpie gateway-key quotas <id> on|off tell it them, or none (#1981)
|
||
func gatewayKeyQuotas(out io.Writer, args []string) error {
|
||
usage := fmt.Errorf("usage: magpie gateway-key quotas <id> [on | off]")
|
||
if len(args) == 0 || len(args) > 2 {
|
||
return usage
|
||
}
|
||
id := args[0]
|
||
if len(args) == 2 {
|
||
if args[1] != "on" && args[1] != "off" {
|
||
return usage
|
||
}
|
||
if _, err := access.Update("quotas-key", access.Change{Key: id, Quotas: args[1] == "on"}); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
keys, err := access.List()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
i := slices.IndexFunc(keys, func(k access.Key) bool { return k.ID == id })
|
||
if i < 0 {
|
||
return fmt.Errorf("Key not found")
|
||
}
|
||
k := keys[i]
|
||
switch {
|
||
case !k.Quotas:
|
||
_, err = fmt.Fprintf(out, "%s: told no quotas when held to a budget, models or accounts\n", k.Name)
|
||
default:
|
||
_, err = fmt.Fprintf(out, "%s: told the quotas of the providers and accounts it may use\n", k.Name)
|
||
}
|
||
return err
|
||
}
|
||
|
||
// accessWord is a key's access as the list says it: "read-only" or "full".
|
||
func accessWord(k access.Key) string {
|
||
if k.ReadOnly {
|
||
return "read-only"
|
||
}
|
||
return "full"
|
||
}
|
||
|
||
// gatewayKeyAccess shows or sets whether a key may use models or only
|
||
// read magpie's stats (Steven on Discord: a desk display's key):
|
||
//
|
||
// magpie gateway-key access <id> which it has
|
||
// magpie gateway-key access <id> read-only|full quotas and usage alone, or what its other limits let it
|
||
func gatewayKeyAccess(out io.Writer, args []string) error {
|
||
usage := fmt.Errorf("usage: magpie gateway-key access <id> [read-only | full]")
|
||
if len(args) == 0 || len(args) > 2 {
|
||
return usage
|
||
}
|
||
id := args[0]
|
||
if len(args) == 2 {
|
||
if args[1] != "read-only" && args[1] != "full" {
|
||
return usage
|
||
}
|
||
if _, err := access.Update("access-key", access.Change{Key: id, ReadOnly: args[1] == "read-only"}); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
keys, err := access.List()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
i := slices.IndexFunc(keys, func(k access.Key) bool { return k.ID == id })
|
||
if i < 0 {
|
||
return fmt.Errorf("Key not found")
|
||
}
|
||
k := keys[i]
|
||
if k.ReadOnly {
|
||
_, err = fmt.Fprintf(out, "%s: read-only, it reads quotas and usage and uses no model, MCP server or write\n", k.Name)
|
||
} else {
|
||
_, err = fmt.Fprintf(out, "%s: full access, models and stats as its other limits let it\n", k.Name)
|
||
}
|
||
return err
|
||
}
|