Some API-key providers tell no balance magpie can read (no Balance URL, no relay query found), so their keys showed none. Now the user types what the vendor's page says is left, on the key's row in the provider editor (Set balance) or with `magpie provider key-balance <id> [key [usd|off]]`. The key's card shows that less what magpie's usage priced the calls it sent on the key since it was typed (usage.KeySpent, the spend caps' count), marked as an estimate: "≈$12.50", the pill "≈$12.50 left", its title saying what was typed, when, and what was counted since. Typing it again counts from then; No balance takes it off. A balance the vendor tells always wins: a provider with a Balance URL, a plan source, Cline's or OpenCode Zen's card takes none typed, and a relay whose query is found shows its own; when the probe finds none (errNoBalance) the typed one is shown. A typed balance stays saved as the user left it: the editor's save keeps it, a key's replaced value carries it. An estimate is never kept in the balance history and tells routing nothing (the routing allowances cache is left alone). keybalance.js is loaded by keyspend.js (itself loaded when the editor draws a key) for a provider that takes one, so app.js and the core's script budget are unchanged. Words in zh, zh-TW, ja and de. Run at this tree (mote): - go vet ./... (darwin), GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./... at base 46b10af87; at this tree go vet of gui, provider, cli and usage; - go test -tags nogui -count=1 ./internal/gui ./internal/provider ./internal/cli ./internal/usage: provider and usage ok; gui and cli red only on #5644 below; TestCoreScriptBudget ok (1,533,747, as main) - the provider tests under -race -count=3; - GUI: key-own-balance (new: Chromium and WebKit, en zh zh-TW ja de, 900 and 400px, 20/20), key-spend-cap, old-webkit, i18n-keys, gui-de, gui-ja, gui-zh-tw, balance-found, key-balance-card, i18n-en-no-cjk: 104 pass, 1 fail, i18n-keys "a plugin's words are its own", red the same on origin/main7a2946caf(#5327, #5332, #5482, #5426). Broken by hand, each went red: - estimates kept in the balance history: TestTypedBalanceCountsDown; - no fallback to the typed balance on errNoBalance: TestToldBalanceWinsOverTyped; - the editor's save dropping KeyOwnBalances: TestKeyOwnBalanceRoutes. Red on main, not this change: TestSettingsSaveKeepsWhatItDoesNotSend and TestSettingsCLISetsWhatTheWindowSaves (groupSink, groupFirstToken, groupPatience from6b7ee4a98), filed as #5644. Not tried: a real relay account with no balance URL (the cost counted is magpie's own estimate, so a vendor pricing differently drifts until the balance is typed again); the full Go and GUI suites before the push (run after it, under the suite lock); the GUI in the real Windows and Linux apps; routing does not use the estimate. Reported-by: tenacious_eagle (Discord)
74 lines
2.0 KiB
Go
74 lines
2.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
stats "github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
const keyBalanceUsage = "magpie provider key-balance <id> [key [usd|off]]"
|
|
|
|
// keyBalanceCmd shows, or sets, the balance typed by hand on a provider's
|
|
// API keys whose vendor tells none (tenacious_eagle on Discord): what is
|
|
// left of it is the amount less what magpie priced the key's calls at
|
|
// since it was typed (provider/key_balance_own.go).
|
|
func keyBalanceCmd(rest []string) error {
|
|
if len(rest) < 1 || len(rest) > 3 {
|
|
return fmt.Errorf("%s", keyBalanceUsage)
|
|
}
|
|
p, err := provider.Find(rest[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if p.Account != nil {
|
|
return fmt.Errorf("%s is a subscription: a balance typed by hand is for an API key", p.Name)
|
|
}
|
|
if len(rest) > 2 {
|
|
usd, err := provider.ParseOwnBalance(rest[2])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := provider.SetKeyOwnBalance(p.ID, rest[1], usd); 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
|
|
}
|
|
if !p.TakesOwnBalance() {
|
|
fmt.Println(muted.Render(p.Name + " tells its balance itself: the Usage page shows that, not one typed"))
|
|
}
|
|
for _, k := range keys {
|
|
name := k.Masked + " " + muted.Render(k.ID)
|
|
if k.Name != "" {
|
|
name = k.Name + " " + muted.Render(k.Masked+" · "+k.ID)
|
|
}
|
|
b, ok := p.KeyOwnBalance(k.ID)
|
|
if !ok {
|
|
fmt.Println(name, muted.Render("· no balance typed"))
|
|
continue
|
|
}
|
|
spent := stats.KeySpent(k.ID, b.At)
|
|
fmt.Printf("%s · about $%.2f left · $%.2f typed %s, $%.2f spent since\n", name, b.USD-spent, b.USD, b.At.Local().Format("Jan 2 15:04"), spent)
|
|
}
|
|
return nil
|
|
}
|