gateway: image recognition and web search pick only models the user enabled (JonahFu on X)

On a provider with a long list, AutoVision and the searcher picked their
small model from everything the vendor lists (Available), not from what
the user enabled. A Trae CN user's text-only model had its image sent to
OpenRouter's anthropic/claude-haiku-5.5, which they never enabled and
which OpenRouter refused for their region (403).

smallModel, and so AutoVision, searcherModel and the searcher's
first-model fallback, now chooses from Exposed: the user's picks, else
what agents are offered. With no enabled model that sees, a request
turned away says so ("none of the models enabled in magpie can see
images ... enable one that can, or pick one in Settings → Models → Image
recognition"); Vision off says it is off; a describer magpie picked
itself that fails is named as magpie's pick.

Not done: no fallback to another describer on a refusal. With the pick
limited to enabled models, a refusal is of a model the user chose, and
the error now names it and where to change it. Image generation's
AutoDrawer still picks from image models a provider lists: image models
aren't in the chat pickers, so limiting it to picks would turn drawing
off for nearly everyone.

Run: the two new tests fail on the old code (AutoVision =
router/google/gemini-3-flash, searcherModel = google/gemini-3-flash) and
pass now; go test ./... green except internal/gui's
TestDockJoinLeadsCommandTab, which reads the live desktop's Command-Tab
order and passes run alone; gofmt; GOOS=linux and windows go vet
./internal/gateway.
This commit is contained in:
yetone
2026-10-12 00:01:28 +08:00
parent 221e24a3c7
commit d5cae267a7
4 changed files with 109 additions and 9 deletions
+4 -4
View File
@@ -1592,7 +1592,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
seen, err := s.seenBody(withDescribeFor(r.Context(), call.Agent, call.Model, sessionOf(r.Header), unknownSight), from, body, see)
if err != nil {
call.Status, call.Error = 502, "image not described"
writeError(w, from, 502, fmt.Sprintf("model %q can't see images, and %s couldn't describe the image for it: %v%s", call.Model, see, err, missingSeerNote()))
writeError(w, from, 502, fmt.Sprintf("model %q can't see images, and %s couldn't describe the image for it: %v%s%s", call.Model, see, err, missingSeerNote(), autoSeerNote(see)))
turnedAway()
return
}
@@ -1610,7 +1610,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
}
if currentImage {
call.Status, call.Error = 400, "model does not support image input"
writeError(w, from, 400, fmt.Sprintf("model %q does not support image input%s", call.Model, missingSeerNote()))
writeError(w, from, 400, fmt.Sprintf("model %q does not support image input%s", call.Model, noSeerNote()))
turnedAway()
return
}
@@ -1802,7 +1802,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
}
if len(cands) == 0 {
call.Status, call.Error = 400, "model does not support image input"
writeError(w, from, 400, fmt.Sprintf("model %q does not support image input%s", call.Model, missingSeerNote()))
writeError(w, from, 400, fmt.Sprintf("model %q does not support image input%s", call.Model, noSeerNote()))
turnedAway()
return
}
@@ -1950,7 +1950,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
}
see, _ := seeing()
call.Status = 502
failTo(w, kept, from, 502, fmt.Sprintf("model %q can't see images, and %s couldn't describe the image for it: %v%s", c.p.ID+"/"+c.model, see, err, missingSeerNote()))
failTo(w, kept, from, 502, fmt.Sprintf("model %q can't see images, and %s couldn't describe the image for it: %v%s%s", c.p.ID+"/"+c.model, see, err, missingSeerNote(), autoSeerNote(see)))
break
}
attemptBody = b
+6 -3
View File
@@ -313,7 +313,7 @@ func chosenSearcher() (*provider.Provider, string, string) {
if m := searcherModel(p); m != "" {
return &p, m, ""
}
if ms := p.Available(); len(ms) > 0 {
if ms := p.Exposed(); len(ms) > 0 {
return &p, ms[0].ID, ""
}
return &p, "", SearcherNoneOf
@@ -378,10 +378,13 @@ func RelaysSaidToSearch() []provider.Provider {
// smallModel is the provider's cheapest small model of a vendor that
// searches well, or its cheapest, or its first; only among those keep
// holds, when it is given.
// holds, when it is given. Only a model the user enabled is picked (what
// agents are offered, Exposed), never one of the rest of the vendor's
// list: OpenRouter turned away an unpicked anthropic/claude-haiku-5.5 for
// the user's region (JonahFu on X).
func smallModel(p provider.Provider, keep func(catalog.Model) bool) string {
var ms []catalog.Model
for _, m := range p.Available() {
for _, m := range p.Exposed() {
if keep != nil && !keep(m) {
continue
}
+28 -2
View File
@@ -138,8 +138,34 @@ func missingSeerNote() string {
return ""
}
// noSeerNote is what a request turned away for an image says of why no
// model described it: the picked one is missing, Image recognition is
// off, or none of the models the user enabled can see.
func noSeerNote() string {
if n := missingSeerNote(); n != "" {
return n
}
if provider.HeldSettings().Vision == "off" {
return "; Image recognition is off in magpie's Settings → Models"
}
if _, ok := seer(); !ok {
return "; none of the models enabled in magpie can see images to describe it: enable one that can, or pick one in Settings → Models → Image recognition"
}
return ""
}
// autoSeerNote is what a request whose image wasn't described says of a
// describer magpie picked itself, which the user may not know of.
func autoSeerNote(see string) string {
if provider.HeldSettings().Vision != "" {
return ""
}
return fmt.Sprintf("; magpie picked %s of the models you enabled: pick another in Settings → Models → Image recognition", see)
}
// AutoVision is the model magpie picks to describe images when the
// Settings name none: a small model that sees of the providers set up — a
// Settings name none: a small model that sees, of the models the user
// enabled on the providers switched on (smallModel) — a
// Claude account's, a Codex account's, the cheapest of an API key's,
// another account's. "" when no model sees.
func AutoVision() string {
@@ -350,7 +376,7 @@ func smallModelWhere(p provider.Provider, keep func(catalog.Model) bool) (string
if id == "" {
return "", 0
}
for _, m := range p.Available() {
for _, m := range p.Exposed() {
if m.ID == id && m.Price != nil {
return id, m.Price.Input + m.Price.Output
}
+71
View File
@@ -0,0 +1,71 @@
package gateway
import (
"net/http/httptest"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// routerUp is a router key whose vendor lists a small model that sees,
// anthropic/claude-haiku-5.5, which the user left unpicked: only "text",
// which can't see, is enabled (JonahFu on X: OpenRouter turned the unpicked
// Haiku away for their region, and they had never enabled it).
func routerUp(t *testing.T, picks ...string) (*Server, *eyeUp) {
t.Helper()
fresh(t)
u := &eyeUp{}
up := httptest.NewServer(u)
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "router", Name: "Router", Chat: up.URL + "/v1", Key: "key", Models: picks}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("router", up.URL+"/v1", []catalog.Model{
{ID: "text", ImageInput: imageInputBool(false), Price: &catalog.Price{Input: 0.1, Output: 0.1}},
{ID: "anthropic/claude-haiku-5.5", Images: true, ImageInput: imageInputBool(true), Price: &catalog.Price{Input: 1, Output: 5}},
{ID: "google/gemini-3-flash", Images: true, ImageInput: imageInputBool(true), Price: &catalog.Price{Input: 0.3, Output: 2.5}},
}); err != nil {
t.Fatal(err)
}
return New(), u
}
func TestAutoVisionPicksOnlyAnEnabledModel(t *testing.T) {
s, u := routerUp(t, "text")
if m := AutoVision(); m != "" {
t.Fatalf("AutoVision = %q, a model the user never enabled", m)
}
code, body := postAs(t, s, "", `{"model":"router/text","messages":[{"role":"user","content":[{"type":"text","text":"read"},{"type":"image_url","image_url":{"url":"data:image/png;base64,aGVsbG8="}}]}]}`)
if code != 400 || !strings.Contains(body, "none of the models enabled in magpie can see images") {
t.Fatalf("%d %s", code, body)
}
if got := u.sent("anthropic/claude-haiku-5.5"); len(got) != 0 {
t.Fatalf("the unpicked model was asked to describe: %v", got)
}
// with one that sees enabled, that one describes, not the cheaper
// small model the user left out
routerUp(t, "text", "anthropic/claude-haiku-5.5")
if m := AutoVision(); m != "router/anthropic/claude-haiku-5.5" {
t.Fatalf("AutoVision = %q", m)
}
}
// The web-search side request's own pick is the same: a model the user
// enabled.
func TestSearcherModelIsAnEnabledModel(t *testing.T) {
routerUp(t, "text")
p, err := provider.Find("router")
if err != nil {
t.Fatal(err)
}
if m := searcherModel(*p); m != "text" {
t.Fatalf("searcherModel = %q, want the only enabled model", m)
}
if picked := slices.ContainsFunc(p.Exposed(), func(m catalog.Model) bool { return m.ID == "google/gemini-3-flash" }); picked {
t.Fatal("gemini is enabled")
}
}