@YIXIE233 has several sub2api relays. Each reports a key's group rate at GET /v1/sub2api/billing, and each sells its dollars at its own price (¥1 for $1 at one, ¥7 for $100 at another). Until now every key's rate was typed by hand and typed again whenever a relay changed its prices. Now a provider of API keys has a "Top-up rate" under Advanced, next to Price rate (TopUpRate: above 0 up to 1000, at most four decimals). With one set, the gateway's KeepKeyRatesRead asks each key's relay for its rate in the background. It reads at start, every 6h, 30 min after a failed read, and at once on a save that sets a new top-up rate. It is never on a request's path. The relay asked is the origin of its sub2api key query, else of its first base; loopback and vendor hosts are never asked. Readings go to key-rates.json beside providers.json. A key then bills at group rate x top-up rate, and that rate reaches both places a price is used: PriceAt (Cheapest/By price routing) and BaseKeyPriceIn (the ledger, Usage, spend caps). A group rate of 0 prices at 0 (zeroRate), not at the list price. - A key's own KeyPriceRates still wins. - A key never read, or whose reads all failed, is unknown and bills at the provider's PriceRate, never 1x. - A failed reread keeps the last rate it read. The Usage caches key on a hash of the rates read, so a reread that finds the same rates keeps them. /api/providers lists keyGroupRates, which the editor names under the box. An empty per-key line's placeholder shows group rate x top-up rate. `magpie provider key-rate` lists both. i18n: zh, zh-TW, ja, de. Not counted yet: sub2api's peak-hour multiplier (#7286). Run (mote, via mote-run): - At7dc85f26e, before the rebase: go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok. gofmt -l: nothing. - go test -count=1 ./internal/provider ./internal/cli ./internal/usage: ok. - go test ./internal/gui: two reds, TestBuiltinsTranslated (a Routing string in zh/zh-TW/ja/de) and TestSettingsSaveKeepsWhatItDoesNotSend (RedactModelDir). This change doesn't touch either; checked against origin/main below. - The box's code lives in the lazy keyrates.js (loaded for a provider of keys of its own too, keyRateField's new `on`). The top-up rate rides keyPriceRates(ed, body), and keyPriceRates gives undefined when no per-key lines were drawn. With that, app.js stays under TestCoreScriptBudget with no raise (a first cut was 261 bytes over). - go test -run 'CoreScriptBudget|TopUpRate|KeyPriceRate|I18n|Translated' ./internal/gui at the rebased head (c2924c36d): ok except TestBuiltinsTranslated (#7257). That test and TestSettingsSaveKeepsWhatItDoesNotSend (#7240) fail the same on an archive of origin/mainc2924c36d. - node --test top-up-rate, key-price-rate, provider-price-rate and old-webkit .test.cjs (Chromium and WebKit, en/zh/zh-TW/ja/de): 62 pass, 2 fail. Both fails are old-webkit (guideback.js imported by navpages.js, the class of #6570, and Safari 15 styles, #7235), and they fail the same on origin/mainc2924c36d. Broken by hand: with readKeyPriceRate turned off and zeroRate dropped from tariffOf, TestKeyGroupRateTimesTopUpRate, ...FailedReadIsUnknown and ...Zero go red ($1.8 where $0.14 was wanted); green again once restored. Not tried: a real sub2api relay. The fixtures are sub2api's own reply shapes from backend/internal/handler/gateway_key_billing.go (schema 1), served by httptest.
78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
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)
|
||
g, read := p.KeyGroupRate(k.ID)
|
||
switch {
|
||
case own:
|
||
fmt.Println(name, "· ×"+rateText(r))
|
||
case read && p.TopUpRate > 0:
|
||
fmt.Println(name, muted.Render("· ×"+rateText(r)+", its group's ×"+rateText(g)+" read from the relay, at the top-up rate ×"+rateText(p.TopUpRate)))
|
||
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) }
|