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) + } +}