Files
magpie/internal/catalog/family.go
T
yetone e887139e7a providers: Best in family serves the newest model of each family a provider lists
Steven on Discord #feedback asked for a provider to serve only the newest
model of each family, so a newer one (Opus 5.6) takes the old one's place
without re-picking.

The provider editor's Models control gets a fourth option, beside
Vendor's list / Picked only / Usage only: Best in family. It is also on the
CLI as `magpie provider models <id> best`. With nothing picked, the provider
serves only the newest model of each family in the vendor's list, up to 24.
A family is the id with its version numbers taken out
(catalog.FamilyOf/BestInFamily in internal/catalog/family.go). A dated
snapshot counts as its alias, and sizes and suffixes (mini, flash, -fast,
-thinking) stay part of the name. Provider.Exposed applies it, and so does
Codex's native list (CodexNativePicked). A provider/model a request names
explicitly is still served, since resolve doesn't go through Exposed. A pick,
Select none or Usage only clears the flag (normalize), and a providers.json
without bestInFamily reads as before, so every existing mode is unchanged.
The editor draws the family's newest as dashed (m.best) before saving. Every
language has the wording (zh, zh-TW, ja, de). The design reference is
docs/subsystems/providers-accounts.md (Model picks).

Tests: family_test.go is a table of real ids, including the models.dev Claude
list in testdata (no network). There are also best_in_family_test.go
(provider), TestProviderModelsBest (cli), provider_save_best_test.go (gui
save keeps/clears), codex_best_in_family_test.go (gateway) and
provider-best-in-family.test.cjs (5 languages x chromium/webkit x 1100/420).

Broken by hand: with the BestInFamily filter in Exposed and in
CodexNativePicked turned off, the 4 new Go tests went red.

Budget: after the rebase app.js went over appScriptBudget. Comments were cut
and the tooltip and hint merged into one string, so app.js is 1,311,399 of
1,311,735. Neither budget was raised.

Run at this head, on origin/main 1027db408, via mote-run (macOS 27):
- gofmt -l clean; go vet clean for darwin, linux -tags nogui and windows
  (at base 05a6087e1, the same Go diff).
- go test -count=1 ./internal/catalog ./internal/provider ./internal/cli
  ./internal/gateway: ok.
- go test ./internal/gui: everything except TestMainWindowCorners/macOS_26_SDK
  (red on macOS 27, #3949; not this change).
- node --test: provider-best-in-family, provider-none-picked,
  provider-picked-only-shows-picks, i18n-keys, gui-de, gui-ja, gui-zh-tw,
  context-map and gateway-key-models, 113 tests, 108 pass. The 5 reds are
  context-map 420px "two tiles overlap" (#3488, #4304) and gateway-key-models
  webkit en (#4304). Both fail the same way on a clean origin/main 1027db408
  checkout run alone (8 of 30 red there).
- The earlier wider GUI run (38 provider/routing files, concurrency 3) had
  the same red files on origin/main deb0b8da7 as at this change's head:
  model-chip-steady, models-works-only, provider-fetch-again and the others
  in #4304. Three with no issue were filed: #4320 (recovered-file),
  #4321 (tencent-cloud), #4322 (warm-now).

Not tried: no real account or real magpie app; the editor was checked
through Playwright screenshots only. Families are read off the id alone, so
a vendor whose ids don't put the version in numbers (or orders them
oddly, e.g. grok-4.20 against grok-4.7 compared numerically) may group
or rank unexpectedly. Bedrock's -v1:0 counts as version digits.
2026-10-30 04:48:17 +08:00

178 lines
5.4 KiB
Go

package catalog
import (
"regexp"
"slices"
"strconv"
"strings"
)
// A model's family is its id with the version numbers taken out, so that
// claude-opus-4-8 and claude-opus-5-5 are claude-opus, o3 and o1 are o, and
// o4-mini is o-mini. Nothing about a vendor is compiled in: what is read
// off the id alone is
//
// - a version: a number (4, 4.5, the 4 and 5 of claude-opus-4-5), the
// digits a word ends in (qwen3, o4, k2), a v and its digits (v3,
// v1:0), or the digits a word starts with that aren't a size (gpt-4o
// is gpt-o, glm-4.5v glm-v);
// - a size, which stays in the name: 8b, 70b, 1.5b, a22b, 8x7b, 128e,
// 32k, so qwen3-32b and qwen3-8b are two families;
// - a snapshot: a date or revision after the version (20251101, 0528,
// 2507, 2024-08-06, 05-20, 001), read as the alias it was taken of;
// - a preview (preview, exp, beta): the same model before its release,
// so gemini-3-pro-preview is a gemini-pro;
// - latest, an alias of the newest, which is the alias too.
//
// Every other word stays in the name: -mini, -pro, -nano, -flash,
// -flash-lite, -codex, -thinking, -instruct, :free, and a provider prefix
// (anthropic/, us.anthropic.).
// Family is what an id's name says about it: the family it belongs to and
// where it stands in it.
type Family struct {
Name string // the id without its version, snapshot or preview
Version []int // every version number, in the order the id has them
Snapshot string // its dates or revision, "" for an alias
Preview bool
}
var (
familySep = regexp.MustCompile(`[-_/:@\[\]\s]+`)
// a number: 4, 4.5, 2.0
familyNum = regexp.MustCompile(`^\d+(?:\.\d+)*$`)
// a word that ends in its version: qwen3, qwen2.5, o4, k2, v1, llama3.1
familyTail = regexp.MustCompile(`^([a-z][a-z.]*?[a-z])(\d+(?:\.\d+)*)$|^([a-z])(\d+(?:\.\d+)*)$`)
// a word that starts with its version: 4o, 4.5v, 3n
familyHead = regexp.MustCompile(`^(\d+(?:\.\d+)*)([a-z]+)$`)
// a size, kept in the name: 8b, 1.5b, 70b, 128e, 32k, 8x7b, a22b, e4b
familySize = regexp.MustCompile(`^(?:[a-z]?\d+(?:\.\d+)?|\d+x\d+(?:\.\d+)?)[bmkte]$`)
familyYear = regexp.MustCompile(`^(?:19|20)\d\d$`)
)
var familyPreview = map[string]bool{"preview": true, "exp": true, "experimental": true, "beta": true, "alpha": true}
// FamilyOf reads id's family off its name.
func FamilyOf(id string) Family {
s := strings.ToLower(strings.TrimSpace(id))
words := familySep.Split(s, -1)
seps := familySep.FindAllString(s, -1)
// which purely numeric words are a snapshot rather than a version
snap := make([]bool, len(words))
for i, w := range words {
if !familyNum.MatchString(w) || strings.Contains(w, ".") {
continue
}
// a date or revision: 0528, 2507, 20251101, 2024, 001, 05
if len(w) >= 4 || len(w) >= 2 && w[0] == '0' {
snap[i] = true
}
}
for i, w := range words {
if snap[i] || !familyNum.MatchString(w) || len(w) != 2 {
continue
}
// the day after a month (05-20), the month before a year (12-2024)
if i > 0 && snap[i-1] || i+1 < len(words) && familyYear.MatchString(words[i+1]) {
snap[i] = true
}
}
var f Family
var name, snaps strings.Builder
keep := func(i int, w string) {
if name.Len() > 0 && i > 0 {
name.WriteString(seps[i-1])
}
name.WriteString(w)
}
addVersion := func(v string) {
for _, n := range strings.Split(v, ".") {
x, err := strconv.Atoi(n)
if err != nil {
x = 0
}
f.Version = append(f.Version, x)
}
}
for i, w := range words {
switch {
case w == "":
case snap[i]:
if snaps.Len() > 0 {
snaps.WriteString("-")
}
snaps.WriteString(w)
case familyPreview[w]:
f.Preview = true
case w == "latest":
case familyNum.MatchString(w):
addVersion(w)
case familySize.MatchString(w):
keep(i, w)
default:
if m := familyTail.FindStringSubmatch(w); m != nil {
word, v := m[1], m[2]
if word == "" {
word, v = m[3], m[4]
}
if word != "v" { // v3, v1: the version alone
keep(i, word)
}
addVersion(v)
} else if m := familyHead.FindStringSubmatch(w); m != nil {
addVersion(m[1])
keep(i, m[2])
} else {
keep(i, w)
}
}
}
f.Name, f.Snapshot = name.String(), snaps.String()
if f.Name == "" {
f.Name = s // all numbers: a family of its own
}
return f
}
// newerInFamily says a, of the same family as b, is the one to serve over
// it: the higher version; at the same version a release over a preview,
// then the alias over a dated snapshot (the alias names the vendor's
// current one), then the later release date, then the later snapshot.
func newerInFamily(a, b Model, fa, fb Family) bool {
if c := slices.Compare(fa.Version, fb.Version); c != 0 {
return c > 0
}
if fa.Preview != fb.Preview {
return fb.Preview
}
if (fa.Snapshot == "") != (fb.Snapshot == "") {
return fa.Snapshot == ""
}
if a.Released != b.Released {
return a.Released > b.Released
}
return fa.Snapshot > fb.Snapshot
}
// BestInFamily is ms with only the newest model of each family left
// (FamilyOf, newerInFamily), in the order ms has them. A family's first
// listed wins a tie.
func BestInFamily(ms []Model) []Model {
fams := make([]Family, len(ms))
best := map[string]int{}
for i, m := range ms {
fams[i] = FamilyOf(m.ID)
j, ok := best[fams[i].Name]
if !ok || newerInFamily(m, ms[j], fams[i], fams[j]) {
best[fams[i].Name] = i
}
}
out := make([]Model, 0, len(best))
for i, m := range ms {
if best[fams[i].Name] == i {
out = append(out, m)
}
}
return out
}