Files
magpie/internal/gui/modelprice.go
T
yetone 40ca0c88de gui: a model's price is set from the Usage page (soilzhu80 on X)
A model an agent runs is often in no provider's list, so no provider's
Model settings could price it. A model's row on the Usage page (its "…"
or a right-click) and its bar in the Sessions tab's Models card (a
right-click) now offer "Set price…": a sheet with input, output, cache
read and cache write in $ / 1M tokens, the maker's list price greyed in
the empty boxes. It sets the model's price from any provider (*/model),
the key `magpie model price '*/model'` writes, through GET/POST
/api/model-price (modelprice.go). A provider's own price for the model
still comes first. Restore default takes the price away. Sessions' stats
are forgotten on a save, so past calls are counted at the new price too.
A Sessions bar prices the id after its last "/", the id sessions'
priceOf looks up.

Run (mote): go test -run ModelPrice ./internal/gui; node --test
usage-model-price (chromium + webkit, 420px wide, en/zh/zh-TW/ja/de),
usage-clear (its menu now has Set price… above Clear usage…),
i18n-keys; gofmt -l; go vet ./... for darwin, linux -tags nogui and
windows.
Broken by hand: SetModelPrice skipped, TestModelPriceFromAnyProvider red
("set: 200 {Price:<nil>}"); Set price… taken out of the row menu,
usage-model-price red.
Not tried: a real gateway call priced after setting it in the real app.
2026-11-10 17:45:42 +08:00

88 lines
2.8 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"strings"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// modelPriceJSON is a model's price from any provider (*/model), the one
// `magpie model price '*/model'` sets: what the user said it costs, if
// anything, and its maker's list price on models.dev, which is what the
// sheet shows greyed while nothing is typed.
type modelPriceJSON struct {
Model string `json:"model"`
Price *catalog.Price `json:"price"`
Maker *catalog.Price `json:"maker,omitempty"`
}
func anyModelPrice(model string) modelPriceJSON {
out := modelPriceJSON{Model: model}
if m, ok := settings.Load().ModelPrices[provider.AnyPriceKey(model)]; ok {
if p, bad := m.Price(); bad == "" {
out.Price = &p
}
}
if p, ok := provider.MakerPrice(model); ok {
out.Maker = &p
}
return out
}
// pricedModel is the model a price is asked for, or "" when it names none:
// one id, not every model ("*") nor a provider's ("*/…").
func pricedModel(s string) string {
s = strings.TrimSpace(s)
if s == "" || s == "*" || len(s) > 200 || strings.HasPrefix(s, settings.AnyProvider) {
return ""
}
return s
}
// modelPriceRoutes: the Usage page's Set price… on a model's row
// (soilzhu80 on X). A model an agent ran is often not in any provider's
// list, so the provider editor's Model settings can't price it; this sets
// what it costs from any provider not priced for it, the price a session's
// model is counted at. GET ?model= reads it; POST {"model","price"} sets
// it, a null price takes it away.
func modelPriceRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/model-price", func(rw http.ResponseWriter, r *http.Request) {
model := pricedModel(r.URL.Query().Get("model"))
if model == "" {
http.Error(rw, "name one model", http.StatusBadRequest)
return
}
writeJSON(rw, anyModelPrice(model))
})
mux.HandleFunc("POST /api/model-price", func(rw http.ResponseWriter, r *http.Request) {
var in struct {
Model string `json:"model"`
Price *catalog.Price `json:"price"`
}
if err := json.NewDecoder(http.MaxBytesReader(rw, r.Body, 16<<10)).Decode(&in); err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
model := pricedModel(in.Model)
if model == "" {
http.Error(rw, "name one model", http.StatusBadRequest)
return
}
if in.Price != nil {
// a price typed in the sheet has no tiers or peak hours
in.Price.Tiers, in.Price.Peak = nil, ""
}
if err := provider.SetModelPrice(provider.AnyPriceKey(model), in.Price); err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
// the Sessions tab's stats are priced when they are read
forgetStats()
writeJSON(rw, anyModelPrice(model))
})
}