Files
magpie/internal/provider/key_rate.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

121 lines
3.5 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 provider
// A key's own price rate (@tnt12231 on X): a relay sells keys in groups
// that bill at different rates against the official price, 0.5× on one
// and 1.2× on another, under one endpoint. Each key the user gave a rate
// of its own bills at it wherever a call it sent is counted (the Usage
// page, a key's spend cap and its typed balance); every other key 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.
import (
"fmt"
"strconv"
"strings"
)
// KeyPriceRate is the price rate the key keyID bills at: its own when the
// user gave it one, else the provider's (0: the price as listed). own
// says which.
func (p Provider) KeyPriceRate(keyID string) (rate float64, own bool) {
if r, ok := p.KeyPriceRates[strings.ToLower(keyID)]; ok && r > 0 {
return r, true
}
return p.PriceRate, false
}
// atKeyRate is p as the key keyID bills: its price rate the key's own when
// it has one, so pricing p prices that key's calls.
func (p Provider) atKeyRate(keyID string) Provider {
if keyID == "" {
return p
}
p.PriceRate, _ = p.KeyPriceRate(keyID)
return p
}
// KeyPriceRateOK says what is wrong with a key's own price rate, "" when
// nothing: above 0 up to 1000, at most three decimals. None is no value:
// the key bills at the provider's rate.
func KeyPriceRateOK(r float64) string {
if bad := PriceRateOK(r); bad != "" {
return bad
}
if r == 0 {
return "a key's own price rate is above 0; take it off for the provider's"
}
return ""
}
// SetKeyPriceRate gives the key ref (its id or name) of the provider id a
// price rate of its own, or with nil the provider's again.
func SetKeyPriceRate(id, ref string, rate *float64) error {
if rate != nil {
if bad := KeyPriceRateOK(*rate); bad != "" {
return fmt.Errorf("%s, not %v", bad, *rate)
}
}
p, err := Find(id)
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)
}
r, ok := p.accountRef(ref)
if !ok {
return fmt.Errorf("%s has no key %q", p.Name, ref)
}
k := strings.ToLower(r)
m := map[string]float64{}
for id, v := range p.KeyPriceRates {
m[id] = v
}
if rate == nil {
if _, had := m[k]; !had {
return nil
}
delete(m, k)
} else {
m[k] = *rate
}
p.KeyPriceRates = m
return Save(*p)
}
// ParseKeyPriceRate reads a key's price rate as the CLI takes it: a number
// ("0.8", "1.5x", "×1.2"), or default (nil) for the provider's.
func ParseKeyPriceRate(s string) (*float64, error) {
v := strings.ToLower(strings.TrimSpace(s))
switch v {
case "default", "provider", "off", "none", "-":
return nil, nil
}
v = strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(strings.TrimPrefix(v, "×"), "x"), "x"))
r, err := strconv.ParseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("a key's price rate is a number like 0.8 or 1.5, or default for the provider's, not %q", s)
}
if bad := KeyPriceRateOK(r); bad != "" {
return nil, fmt.Errorf("%s, not %v", bad, r)
}
return &r, nil
}
// normalKeyPriceRates is m as it is kept: key ids in lower case, rates
// that aren't one left out; nil for none.
func normalKeyPriceRates(m map[string]float64) map[string]float64 {
var out map[string]float64
for k, r := range m {
k = strings.ToLower(strings.TrimSpace(k))
if k == "" || KeyPriceRateOK(r) != "" {
continue
}
if out == nil {
out = map[string]float64{}
}
out[k] = r
}
return out
}