Files
magpie/internal/gui/modelprice.go
T
yetone 667ac8bd6f settings: a look at one setting takes the shared settings, not a copy
settings.Load() copies the whole Settings (clone + deepCopy of every map
and slice) for each caller. 133 places read one field of it and threw
the rest away: the proxy for every outgoing request (netproxy), the
GitHub token, the China mirror switch, a model's output cap, wire or
price on a gateway request. With a real account in the tray, Load's
clone was 26.6 MB of the 318 MB the process allocated in its first
seven minutes, most of it through netproxy.For on each request.

Each of them now reads settings.Shared(), the read-only settings every
caller is given for the same settings.json. Only reads were changed:
a scalar field (string, bool, int, float) or a map field's index
read. A caller that keeps a whole map or slice, or calls a method on
the settings, still takes Load's copy.
2026-11-14 14:02:49 +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.Shared().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))
})
}