From 0b4aa96fd09ec3544eada44f7094564a0d9ca154 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 14:33:46 +0800 Subject: [PATCH] gateway: a relay whose models are typed in by hand is offered as a searcher (axelburks on Discord) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit axelburks added a custom provider on OpenAI's Responses API and ticked "Searches the web by itself", but Settings › Web search's searcher picker didn't offer it. Searchers() and namedSearcher took a provider's models from Available() alone. A relay with no /models list has its models typed in by hand (Exposed) and none available, so it was left out of the picker, while the same row named it among the relays to pick there. searcherModels gives what the provider lists, then the hand-typed ones it doesn't. A relay that lists its models was offered before this already. Tests: - TestSearcherRelayWithoutModelList (gateway): a Responses relay with no /models and a typed model is a searcher and is resolved by name. - TestSearcherPickerOffersResponsesRelay (gui): the relay saved the way the editor saves it is in SearchChoices with its model. Both were red on the old code. Run (mote-run), at this commit alone: go vet of gateway and gui, and go test -run 'Searcher' of gateway and gui: ok. The full checks ran with the next commit; its body has them. Not tried: the reporter's own relay (we don't know whether it lists its models; the reply asks). --- docs/subsystems/gateway-routing.md | 7 +++ internal/gateway/search.go | 30 ++++++++++-- internal/gateway/search_unlisted_test.go | 58 ++++++++++++++++++++++++ internal/gui/searcher_relay_test.go | 43 ++++++++++++++++++ 4 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 internal/gateway/search_unlisted_test.go create mode 100644 internal/gui/searcher_relay_test.go diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index 9d8c75754..e7e238a7d 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -912,6 +912,13 @@ did (01huadalang, 10-20, of Trae's search model). A client's own of whose models searches by itself, such as a plugin that marks none and whose `config` hook doesn't set `searches`, is not a searcher: its models are given magpie's `web_search`, and the row's `searchLeftOut` names it. +The models looked at are `searcherModels`: what the provider lists +(`Available()`) and the ones typed in by hand (`Exposed`). A custom +Responses relay with no model list, marked as searching by itself, has +only the hand-typed ones. Before, it was left out of the picker while +the page listed it among the relays to pick by hand (axelburks on +Discord). Covered by `TestSearcherRelayWithoutModelList` and +`TestSearcherPickerOffersResponsesRelay`. Covered by `TestCommandCodeSearchesForOtherModels`, `TestPluginSearchModelIsASearcher` and `TestSearcherChosen`. diff --git a/internal/gateway/search.go b/internal/gateway/search.go index f1c8df00f..bd3af6d92 100644 --- a/internal/gateway/search.go +++ b/internal/gateway/search.go @@ -399,7 +399,12 @@ const ( // its small model, as when none is named. Nil when none is named or // provider search is explicitly off. func chosenSearcher() (*provider.Provider, string, string) { - v := strings.TrimSpace(settings.Load().Searcher) + return namedSearcher(settings.Load().Searcher) +} + +// namedSearcher is chosenSearcher of v, a searcher as Settings writes it. +func namedSearcher(v string) (*provider.Provider, string, string) { + v = strings.TrimSpace(v) if v == "" || v == "off" { return nil, "", "" } @@ -419,7 +424,7 @@ func chosenSearcher() (*provider.Provider, string, string) { // it searches with no model return &p, "", "" } - if model != "" && slices.ContainsFunc(p.Available(), func(m catalog.Model) bool { return m.ID == model }) && searchableModel(p, model) { + if model != "" && slices.ContainsFunc(searcherModels(p), func(m catalog.Model) bool { return m.ID == model }) && searchableModel(p, model) { return &p, model, "" } if m := searcherModel(p); m != "" { @@ -446,7 +451,7 @@ func Searchers() []SearcherChoice { continue } var ms []catalog.Model - for _, m := range p.Available() { + for _, m := range searcherModels(p) { if !provider.IsGroupRef(m.ID) && searchableModel(p, m.ID) { ms = append(ms, m) } @@ -460,6 +465,25 @@ func Searchers() []SearcherChoice { return out } +// searcherModels are the models a searcher may be named with: those its +// list gives, then those typed in by hand that it doesn't list. A relay +// with no model list (its models typed in by hand) has none listed, and +// was left out of the picker while the row named it a relay to pick +// (axelburks on Discord). +func searcherModels(p provider.Provider) []catalog.Model { + avail := p.Available() + out := avail + for _, m := range p.ExposedOf(avail) { + if !slices.ContainsFunc(avail, func(a catalog.Model) bool { return a.ID == m.ID }) { + if len(out) == len(avail) { + out = slices.Clone(avail) + } + out = append(out, m) + } + } + return out +} + // SearcherChoice is a provider Settings' Searcher may name. type SearcherChoice struct { Provider provider.Provider diff --git a/internal/gateway/search_unlisted_test.go b/internal/gateway/search_unlisted_test.go new file mode 100644 index 000000000..4cff5d13c --- /dev/null +++ b/internal/gateway/search_unlisted_test.go @@ -0,0 +1,58 @@ +package gateway + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/settings" +) + +// A relay on OpenAI's Responses API said to search the web by itself, +// whose models are typed in by hand because it has no model list, can be +// named as the searcher with those models: it was left out of the picker +// while Settings named it among the relays to pick (axelburks on Discord). +func TestSearcherRelayWithoutModelList(t *testing.T) { + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + setHome(t, t.TempDir()) + catalog.Reset() + t.Cleanup(catalog.Reset) + srv := httptest.NewServer(http.NotFoundHandler()) // no /models + t.Cleanup(srv.Close) + p := provider.Provider{ID: "relay", Name: "Relay", Key: "k", Searches: true, Responses: srv.URL + "/v1", Models: []string{"gpt-5.5", "gpt-5-mini"}} + if err := provider.Save(p); err != nil { + t.Fatal(err) + } + if _, err := p.Fetch(context.Background()); err == nil { + t.Fatal("the relay listed models") + } + cs := Searchers() + if len(cs) != 1 || cs[0].Provider.ID != "relay" || !cs[0].ManualOnly { + t.Fatalf("searchers = %+v", cs) + } + var ids []string + for _, m := range cs[0].Models { + ids = append(ids, m.ID) + } + if len(ids) != 2 || ids[0] != "gpt-5.5" || ids[1] != "gpt-5-mini" { + t.Errorf("models = %v", ids) + } + if cs[0].Small == "" { + t.Error("no model to search with when none is named") + } + st := settings.Load() + st.Searcher = "relay/gpt-5.5" + if err := settings.Save(st); err != nil { + t.Fatal(err) + } + if got, m, ok := searcher(); !ok || got.ID != "relay" || m != "gpt-5.5" { + t.Errorf("searcher = %s %s %v", got.ID, m, ok) + } + if why := SearcherUnused(); why != "" { + t.Errorf("unused: %q", why) + } +} diff --git a/internal/gui/searcher_relay_test.go b/internal/gui/searcher_relay_test.go new file mode 100644 index 000000000..64a0517c3 --- /dev/null +++ b/internal/gui/searcher_relay_test.go @@ -0,0 +1,43 @@ +package gui + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +// A custom provider on OpenAI's Responses API, saved by the editor with +// "Searches the web by itself" ticked and its models typed in because it +// has no model list, is offered in Settings › Web search's searcher +// picker with those models (axelburks on Discord): it was named among the +// relays to pick there, with nothing to pick. +func TestSearcherPickerOffersResponsesRelay(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + srv := httptest.NewServer(http.NotFoundHandler()) // no /models + t.Cleanup(srv.Close) + mux := http.NewServeMux() + providerRoutes(mux, nil) + w := httptest.NewRecorder() + mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/save", strings.NewReader( + `{"new":true,"name":"Relay","responses":"`+srv.URL+`/v1","baseAPI":"responses","key":"k","searches":true,"icon":"generic","models":["gpt-5.5"]}`))) + if w.Code != 200 { + t.Fatalf("save: %d %s", w.Code, w.Body) + } + var s settingsJSON + searchState(&s) + if len(s.SearchChoices) != 1 || s.SearchChoices[0].Name != "Relay" { + t.Fatalf("choices = %+v", s.SearchChoices) + } + c := s.SearchChoices[0] + if len(c.Models) != 1 || c.Models[0].ID != c.ID+"/gpt-5.5" || c.Small != "gpt-5.5" { + t.Errorf("choice = %+v", c) + } + if len(s.SearchRelays) != 1 || len(s.SearchLeftOut) != 0 { + t.Errorf("relays %v, left out %v", s.SearchRelays, s.SearchLeftOut) + } +}