Files
magpie/internal/gui/catalog_models.go
T
yetone eb56de0b59 gui: a model's Same as can be picked from models.dev's list, filtered (timfang4162 on X)
In Model settings the Same as box, which binds a provider's model to
another model's metadata (window, reply limit, levels, images, list
price), was a bare text field. A button beside it now opens the app's own
menu (not a <select>) with the models of the models.dev catalog magpie has
already synced: id, name, window and price on each line, a Filter… box over
id, name and maker, the current one ticked. A pick fills the box, so it is
staged and saved with the editor's Save like a typed id, and a value typed
afterwards still wins; nothing about how Same as is read changed.

GET /api/catalog/models lists the cache with one entry per model
(catalog.SameModel), taken from the provider that lists its window and
price. It fetches nothing. The picker lives in modellimits.js, loaded with
the model rows, so the core script budget is not raised (five comments in
renderModels cut to pay for the strings, 613 bytes of room before).

Run on mote at this tree:
- TestCatalogModelsListsTheSyncedCatalogOnce; broken by hand (last
  provider wins instead of the one with window and price) it fails with
  "kimi-k3 = {ID: ... }; want the provider that lists its window and
  price".
- model-same-pick.test.cjs, 22 cases (chromium and webkit, en zh zh-TW ja
  de, 400 and 900px): filter by maker, name and id, Enter picks, the
  current one ticked, one catalog read, no scroll, no <select>, saved as
  modelPrefs same; a failed read says so. With the filter argument taken
  out of openProtoMenu it fails. provider-same-as.test.cjs still passes.
- gofmt, go vet (darwin, linux nogui, windows) on internal/gui,
  TestCoreScriptBudget.
Full GUI suite at this tree (rebased on 5c05ac41b): the only reds are not this change's, each also red or flaky at origin/main 1f99f6f0b run alone, and filed: account-arrange #6010, account-eye #5052, account-refresh-holds #5814, accounts-own-signin's Antigravity test #6038 (a webkit access-control flake that moves between languages).
Not tried: the real app's WebKitGTK; the full suite is started after the
push.
2026-11-11 10:56:41 +08:00

78 lines
2.0 KiB
Go

package gui
import (
"net/http"
"slices"
"sort"
"strings"
"github.com/yetone/magpie/internal/catalog"
)
// catalogModel is one model of the synced models.dev catalog, as the model
// editor's "Same as" picker lists it.
type catalogModel struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
Maker string `json:"maker,omitempty"`
Context int `json:"context,omitempty"`
Output int `json:"output,omitempty"`
Input float64 `json:"input,omitempty"`
Cost float64 `json:"cost,omitempty"`
Images bool `json:"images,omitempty"`
}
// catalogModels lists the models of the catalog magpie already synced
// (nothing is fetched), one a model: the providers that serve the same
// model (catalog.SameModel) are one entry, taken from the one that lists
// its window and price. Sorted by name.
func catalogModels() []catalogModel {
best := map[string]catalog.Model{}
score := func(m catalog.Model) int {
n := 0
if m.Price != nil {
n += 2
}
if m.Context > 0 {
n++
}
return n
}
for _, pid := range catalog.Providers() {
for _, m := range catalog.Provider(pid) {
k := catalog.SameModel(m.ID)
if k == "" {
continue
}
if old, ok := best[k]; !ok || score(m) > score(old) {
best[k] = m
}
}
}
out := make([]catalogModel, 0, len(best))
for _, m := range best {
c := catalogModel{ID: m.ID, Name: m.Name, Maker: catalog.ProviderName(m.Provider), Context: m.Context, Output: m.Output, Images: m.Images}
if m.Price != nil {
c.Input, c.Cost = m.Price.Input, m.Price.Output
}
if c.Name == c.ID {
c.Name = ""
}
out = append(out, c)
}
sort.Slice(out, func(i, j int) bool {
a, b := strings.ToLower(out[i].ID), strings.ToLower(out[j].ID)
if a != b {
return a < b
}
return out[i].ID < out[j].ID
})
return slices.Clip(out)
}
func catalogModelsRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/catalog/models", func(rw http.ResponseWriter, r *http.Request) {
writeJSON(rw, catalogModels())
})
}