Files
magpie/internal/gateway/unknown_model_test.go
T
yetone e50a958014 gateway: an unknown model's 404 names the nearest ids, display names first, and how a model is named (MOMO on Discord)
MOMO asked for "Zen-Space-Bunny", the model's name as a picker shows
it, and the 404 said only "magpie knows no model" and then every id. It
named neither the id meant (opencode-zen-free/space-bunny-free, "Space
Bunny Free") nor the forms a model id can take.

What changed:
- provider/closest.go: Closest(asked, n) splits asked into lowercase
  letter and digit words and scores every catalog entry: a word in the
  display name (the user's or the vendor's) counts 4, in the model id 3,
  the provider's id or name 1 and only alongside one of the others; the
  whole of asked found in the name or id adds 4.
- gateway/unknown_model.go: unknownModel puts "did you mean …?" (up to
  3), then the forms (provider/model, a routing group's id or name, a
  bare model id and where an ambiguous one goes), then the ids as
  before. Used by the chat 404 and the embeddings 404. Switched-off,
  disabled-group and empty-group 404s are unchanged. Draw and video keep
  their own 404: their models are a different list, and the chat
  catalog's nearest ids would point at models that can't draw.
- docs: gateway-routing.md model resolution row.

Tests: unknown_model_test.go: "Zen-Space-Bunny" against
opencode-zen-free serving space-bunny-free named "Space Bunny Free" is
told that id first and the forms; a provider's name alone suggests
nothing; a display name ranks before an id with the same words. The
404 test fails with the gateway hooks reverted.

Run: gofmt -l; go vet -tags nogui on darwin, linux, windows for provider
and gateway; go test -tags nogui ./internal/provider ./internal/gateway
and the root package in a sandbox HOME.
2026-10-10 05:46:48 +08:00

52 lines
1.8 KiB
Go

package gateway
import (
"net/http"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// MOMO on Discord asked for "Zen-Space-Bunny", the name a picker showed,
// and got a 404 that named neither the id meant nor how to name one.
func TestUnknownModelSaysWhatToAsk(t *testing.T) {
fresh(t)
serveOn(t, "opencode-zen-free", "k", []string{"big-pickle", "space-bunny-free", "exo-free"}, &keyed{})
serveOn(t, "opencode-go", "kg", []string{"glm-5.3", "kimi-k3"}, &keyed{})
if err := provider.SetModelName("opencode-zen-free/space-bunny-free", "Space Bunny Free"); err != nil {
t.Fatal(err)
}
s := New()
code, body := postAs(t, s, "", `{"model":"Zen-Space-Bunny","messages":[{"role":"user","content":"hi"}]}`)
if code != http.StatusNotFound {
t.Fatalf("%d %s", code, body)
}
if !strings.Contains(body, "did you mean opencode-zen-free/space-bunny-free") {
t.Fatalf("the id meant isn't named first: %s", body)
}
for _, form := range []string{"provider/model", "routing group's id or name", "bare model id", "Providers order"} {
if !strings.Contains(body, form) {
t.Fatalf("doesn't say %q: %s", form, body)
}
}
// the provider's name alone matches nothing
if _, body := postAs(t, s, "", `{"model":"zen-nothing","messages":[{"role":"user","content":"hi"}]}`); strings.Contains(body, "did you mean") {
t.Fatalf("a provider's name alone was a match: %s", body)
}
}
// A model's display name counts before an id that spells the same words.
func TestClosestByNameFirst(t *testing.T) {
fresh(t)
serveOn(t, "a", "ka", []string{"m1"}, &keyed{})
serveOn(t, "b", "kb", []string{"space-bunny-old"}, &keyed{})
if err := provider.SetModelName("a/m1", "Space Bunny"); err != nil {
t.Fatal(err)
}
got := provider.Closest("space bunny", 3)
if len(got) != 2 || got[0] != "a/m1" || got[1] != "b/space-bunny-old" {
t.Fatalf("Closest = %v", got)
}
}