Files
magpie/internal/cli/key_rate_cli.go
T
yetone a95a7a1cd8 providers: each API key can bill at a price rate of its own (tnt12231 on X)
A relay sells keys in groups that bill at different rates against the
official price (0.5x on one, 1.2x on another) under one endpoint, and
magpie had only the provider's price rate. Now a key can have its own:

- provider.KeyPriceRates, keyed by key id like KeySpendCaps: cloned,
  normalized on save, carried when a key is replaced. A key without one
  bills at the provider's PriceRate. A price the user set for a model
  stands as set, as it does under the provider's rate.
- Usage prices each call at its key's rate (usage.Pricer memoizes per
  key id, BaseKeyPriceIn), so the Usage page, a key's spend cap and its
  typed balance follow it. PriceAt, used to route by price, takes the
  rate of the key the provider holds.
- GUI: the provider editor's Advanced has "Price rate per key" under
  Price rate, shown only with two or more keys, folded past 8 keys like
  the per-key proxies. An empty line shows the provider's rate as its
  placeholder. Save sends keyPriceRates ({} clears, left out keeps).
  The lines, the check and the placeholder live in keyrates.js,
  imported when a provider with two or more keys opens. The core's
  budget goes up 397 for what is left in app.js (the import, the
  draft's copy, Advanced opening for it, the save's line, proxyLines
  drawing another kind of line); app.js stays under its own.
- CLI: magpie provider key-rate <id> [key [rate|default]].
- docs/subsystems/providers-accounts.md: a Key price rates row.

Run, at this head (rebased on 287a1ffa2) through mote-run:
- go test ./internal/usage ./internal/cli ./internal/gui: ok.
  ./internal/provider: ok but TestHostBuildsNoList, red the same on
  origin/main 51b98b1a8 (3 of 3), filed as #6479 (likely 16b8c300f).
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., go build: ok.
- GUI: key-price-rate.test.cjs (chromium and webkit x en, zh, zh-TW,
  ja, de: own rates saved, provider rate as placeholder, bad values
  refused, comma decimals, {} on clear, a folded key keeps its rate,
  one key shows no lines and posts none), i18n-keys and
  provider-many-keys-proxy (it shares proxyLines): ok. old-webkit:
  keyrates.js added to its on-demand list; its two reds (compat.js
  first, Safari 15.0 styles) fail the same on origin/main 51b98b1a8
  (#5919 and the on-demand list's #6467 family).
- Earlier, at 92bce5c72: the GUI files that reach the provider editor
  (134 tests). The 6 provider-many-keys reds fail the same on that base
  (#6209); proxy-login-fields' chromium zh timeout passes 12/12 alone on
  base and branch (#6374).

Broken by hand: atKeyRate leaving PriceRate as the provider's made
TestKeyPriceRatePrices and TestPricerKeyPriceRates red; the save not
sending keyPriceRates made 20 of 30 key-price-rate GUI tests red, and
keyrates.js dropping each parsed rate made them red again after the
move.

Not tried: a real relay with several keys at different rates; the real
Linux WebKitGTK app. The budget estimate and the Sessions page's costs
still use the provider's rate.
2026-11-13 16:31:24 +08:00

75 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cli
import (
"fmt"
"strconv"
"github.com/yetone/magpie/internal/provider"
)
const keyRateUsage = "magpie provider key-rate <id> [key [rate|default]]"
// keyRateCmd shows, or sets, the price rate each of a provider's API keys
// bills at (@tnt12231 on X: one relay, keys in groups at 0.5× and 1.2×):
// a key's own, or the provider's (provider/key_rate.go).
func keyRateCmd(rest []string) error {
if len(rest) < 1 || len(rest) > 3 {
return fmt.Errorf("%s", keyRateUsage)
}
p, err := provider.Find(rest[0])
if err != nil {
return err
}
if p.Account != nil {
return fmt.Errorf("%s is a subscription: a price rate of its own is for an API key (the provider's is in its editor)", p.Name)
}
if len(rest) > 2 {
r, err := provider.ParseKeyPriceRate(rest[2])
if err != nil {
return err
}
if err := provider.SetKeyPriceRate(p.ID, rest[1], r); err != nil {
return err
}
if p, err = provider.Find(p.ID); err != nil {
return err
}
}
keys := p.KeyList()
if len(rest) > 1 {
ref, ok := p.AccountRefOf(rest[1])
if !ok {
return fmt.Errorf("%s has no key %q", p.Name, rest[1])
}
for _, k := range keys {
if k.ID == ref {
keys = []provider.KeyInfo{k}
break
}
}
}
if len(keys) == 0 {
fmt.Println(muted.Render(p.Name + " has no key"))
return nil
}
for _, k := range keys {
name := k.Masked + " " + muted.Render(k.ID)
if k.Name != "" {
name = k.Name + " " + muted.Render(k.Masked+" · "+k.ID)
}
r, own := p.KeyPriceRate(k.ID)
switch {
case own:
fmt.Println(name, "· ×"+rateText(r))
case r > 0:
fmt.Println(name, muted.Render("· ×"+rateText(r)+", the provider's"))
default:
fmt.Println(name, muted.Render("· ×1, the provider's (the official price)"))
}
}
return nil
}
// rateText is a price rate as a line tells it: 0.8, 1.25, 2.
func rateText(r float64) string { return strconv.FormatFloat(r, 'f', -1, 64) }