Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
provider: a signed-in account keeps its price rate (PaulT, Codex)
yetonea68f3ee45dc2
diff --git a/internal/gui/price_rate_test.go b/internal/gui/price_rate_test.goindex c616d0350..b2811b59e 100644--- a/internal/gui/price_rate_test.go+++ b/internal/gui/price_rate_test.go@@ -1,8 +1,15 @@package guiimport (+ "encoding/base64""encoding/json"+ "net/http"+ "net/http/httptest"+ "os"+ "path/filepath"+ "strings""testing"+ "time")// A save's priceRate (#819): left out keeps the rate, null clears it, a@@ -29,3 +36,62 @@ func TestPriceRateOf(t *testing.T) {}}}++// The Codex editor's Save, as the GUI posts it, keeps its Price rate and+// /api/providers gives it back (PaulT on Discord, v0.1.1805: Codex's 价格倍率+// couldn't be saved). A signed-in account's save kept its picks and+// limits only, so the rate was written away and the field came back empty.+func TestAccountSaveKeepsPriceRate(t *testing.T) {+ home := sandboxHome(t)+ codexHome := filepath.Join(home, ".codex")+ t.Setenv("CODEX_HOME", codexHome)+ jwt := func(c any) string {+ b, _ := json.Marshal(c)+ return "e30." + base64.RawURLEncoding.EncodeToString(b) + ".sig"+ }+ auth, _ := json.Marshal(map[string]any{"auth_mode": "chatgpt", "tokens": map[string]any{+ "id_token": jwt(map[string]any{"email": "[email protected]", "https://api.openai.com/auth": map[string]any{"chatgpt_plan_type": "plus", "chatgpt_account_id": "acc"}}),+ "access_token": jwt(map[string]any{"exp": time.Now().Add(24 * time.Hour).Unix()}), "refresh_token": "r", "account_id": "acc"}})+ if err := os.MkdirAll(codexHome, 0o700); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join(codexHome, "auth.json"), auth, 0o600); err != nil {+ t.Fatal(err)+ }+ mux := http.NewServeMux()+ providerRoutes(mux, nil)+ rate := func() any {+ t.Helper()+ w := httptest.NewRecorder()+ mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/providers", nil))+ var got struct {+ Providers []struct {+ ID string `json:"id"`+ PriceRate float64 `json:"priceRate"`+ } `json:"providers"`+ }+ if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {+ t.Fatalf("%v: %s", err, w.Body)+ }+ for _, p := range got.Providers {+ if p.ID == "codex" {+ return p.PriceRate+ }+ }+ return "no codex"+ }+ for _, c := range []struct {+ send string+ want float64+ }{{"0.8", 0.8}, {"1.5", 1.5}, {"null", 0}} {+ w := httptest.NewRecorder()+ body := `{"id":"codex","models":[],"noModels":false,"unlisted":false,"fallback":[],"contexts":{},"outputs":{},"compacts":{},"proxy":"","accountProxies":{},"maxConcurrency":null,"queueLimit":0,"queueWait":0,"maxRPM":0,"priceRate":` + c.send + `,"modelPrefs":{},"pinUpstream":false}`+ mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/save", strings.NewReader(body)))+ if w.Code != 200 {+ t.Fatalf("%s: %d %s", c.send, w.Code, w.Body)+ }+ if got := rate(); got != c.want {+ t.Fatalf("saved %s, read back %v", c.send, got)+ }+ }+}diff --git a/internal/provider/price_rate_test.go b/internal/provider/price_rate_test.goindex eda0cc2ac..71ce35bbb 100644--- a/internal/provider/price_rate_test.go+++ b/internal/provider/price_rate_test.go@@ -3,6 +3,7 @@ package providerimport ("testing"+ "github.com/yetone/magpie/internal/catalog""github.com/yetone/magpie/internal/settings")@@ -39,3 +40,43 @@ func TestPriceRateOK(t *testing.T) {}}}++// A signed-in account's price rate is kept and counted, as a key+// provider's is (PaulT on Discord: Codex's 价格倍率 wasn't saved). Save+// kept only an account's picks and limits, and withPicks read none back,+// so the editor's Save wrote nothing and the field came back empty.+func TestAccountPriceRateKept(t *testing.T) {+ signIn(t)+ catalog.Reset()+ t.Cleanup(catalog.Reset)+ priceCatalog(t)+ for _, id := range []string{"codex", "copilot"} {+ before, ok := EffectivePrice(id, "sol")+ if !ok {+ t.Fatalf("%s: sol has no price", id)+ }+ p, err := Find(id)+ if err != nil {+ t.Fatal(err)+ }+ q := *p+ q.PriceRate = 0.5+ if err := Save(q); err != nil {+ t.Fatal(err)+ }+ if p, err := Find(id); err != nil || p.PriceRate != 0.5 {+ t.Fatalf("%s: rate after Save: %+v %v", id, p, err)+ }+ if after, ok := EffectivePrice(id, "sol"); !ok || after.Input != before.Input*0.5 || after.Output != before.Output*0.5 {+ t.Fatalf("%s: %+v at the rate, %+v before", id, after, before)+ }+ // cleared, it is the official price again+ q.PriceRate = 0+ if err := Save(q); err != nil {+ t.Fatal(err)+ }+ if p, _ := Find(id); p.PriceRate != 0 {+ t.Fatalf("%s: rate left after clearing: %v", id, p.PriceRate)+ }+ }+}diff --git a/internal/provider/provider.go b/internal/provider/provider.goindex 725819bcc..0f130a586 100644--- a/internal/provider/provider.go+++ b/internal/provider/provider.go@@ -753,6 +753,9 @@ func withPicks(a, pk Provider) Provider {a.MaxConcurrency, a.PinUpstream = pk.MaxConcurrency, pk.PinUpstreama.AccountConcurrency, a.QueueLimit, a.QueueWait = pk.AccountConcurrency, pk.QueueLimit, pk.QueueWaita.MaxRPM = pk.MaxRPM+ // and what it bills against the official price (#819): an account's+ // editor sets it as a key provider's does (PaulT on Discord, Codex)+ a.PriceRate = pk.PriceRate// the extra body fields written for it, which a ChatGPT account's// requests take (WithExtraBody, magpie-releases#18)a.ExtraBody = pk.ExtraBody@@ -883,7 +886,7 @@ func Save(p Provider) error {if p.ID == "kiro" {key = p.Key}- p = Provider{ID: p.ID, Key: key, Models: p.Models, NoModels: p.NoModels, PickedFrom: p.PickedFrom, Delisted: p.Delisted, Unlisted: p.Unlisted, Off: p.Off, Fallback: p.Fallback, Routing: p.Routing, Sink: p.Sink, Affinity: p.Affinity, KeepLogin: p.KeepLogin, KeepLoginAs: p.KeepLoginAs, SwitchOnly: p.SwitchOnly, Contexts: p.Contexts, Family: p.Family, Proxy: p.Proxy, AccountProxies: p.AccountProxies, AccountModels: p.AccountModels, AccountCaps: p.AccountCaps, AccountWindowCaps: p.AccountWindowCaps, CapStop: p.CapStop, CapCareful: p.CapCareful, AccountNames: p.AccountNames, MaxConcurrency: p.MaxConcurrency, AccountConcurrency: p.AccountConcurrency, QueueLimit: p.QueueLimit, QueueWait: p.QueueWait, MaxRPM: p.MaxRPM, QuotaRest: p.QuotaRest, QuotaResetAt: p.QuotaResetAt, RestCap: p.RestCap, PinUpstream: p.PinUpstream, NativeCompaction: p.NativeCompaction, PromptAsWritten: p.PromptAsWritten, Hidden: hiddenAccount(p.ID), Quiet: quietAccount(p.ID), Tucked: tuckedAccount(p.ID)}+ p = Provider{ID: p.ID, Key: key, Models: p.Models, NoModels: p.NoModels, PickedFrom: p.PickedFrom, Delisted: p.Delisted, Unlisted: p.Unlisted, Off: p.Off, Fallback: p.Fallback, Routing: p.Routing, Sink: p.Sink, Affinity: p.Affinity, KeepLogin: p.KeepLogin, KeepLoginAs: p.KeepLoginAs, SwitchOnly: p.SwitchOnly, Contexts: p.Contexts, Family: p.Family, Proxy: p.Proxy, AccountProxies: p.AccountProxies, AccountModels: p.AccountModels, AccountCaps: p.AccountCaps, AccountWindowCaps: p.AccountWindowCaps, CapStop: p.CapStop, CapCareful: p.CapCareful, AccountNames: p.AccountNames, MaxConcurrency: p.MaxConcurrency, AccountConcurrency: p.AccountConcurrency, QueueLimit: p.QueueLimit, QueueWait: p.QueueWait, MaxRPM: p.MaxRPM, PriceRate: p.PriceRate, QuotaRest: p.QuotaRest, QuotaResetAt: p.QuotaResetAt, RestCap: p.RestCap, PinUpstream: p.PinUpstream, NativeCompaction: p.NativeCompaction, PromptAsWritten: p.PromptAsWritten, Hidden: hiddenAccount(p.ID), Quiet: quietAccount(p.ID), Tucked: tuckedAccount(p.ID)}} else {p.AccountProxies = keyProxies(p) // a provider of keys proxies each key apartif subscriptionID(p.ID) && !stored(p.ID) {