diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index e9e11d170..0cb21d653 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -380,11 +380,18 @@ tool on (`searchesItself`, `modelSearches`, `remoteSearch`). A provider that searches for some of its models only (`searchModels`: Command Code's Provider API, `partSearcher`) is offered and picked with those models alone, never under `x-cmd-zdr`; before, its searching models were left out -of both (01huadalang on Discord). A provider none of whose models searches -by itself, such as a plugin whose `config` hook doesn't set `searches` -(Trae's), is not a searcher: its models are given magpie's `web_search`, -and the row's `searchLeftOut` names it. Covered by -`TestCommandCodeSearchesForOtherModels` and `TestSearcherChosen`. +of both (01huadalang on Discord). So is a plugin's provider with models +its list marks `searches` (`pluginSearches`: a model that searches the web +by itself when asked in plain words, catalog `WebSearch` "native"): it is +asked with no search tool, and `searchWith` takes a reply that names a +link. Before, every plugin's provider was left out whatever its models +did (01huadalang, 10-20, of Trae's search model). A client's own +`web_search` to such a model still gets magpie's searcher. A provider none +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. +Covered by `TestCommandCodeSearchesForOtherModels`, +`TestPluginSearchModelIsASearcher` and `TestSearcherChosen`. ## A searcher that fails or finds nothing diff --git a/docs/subsystems/provider-plugins.md b/docs/subsystems/provider-plugins.md index 5cf53129c..9a844e7fd 100644 --- a/docs/subsystems/provider-plugins.md +++ b/docs/subsystems/provider-plugins.md @@ -66,6 +66,8 @@ The provider's editor lists them under *Plugin options* (`pluginOptionsField` in A plugin in front of Anthropic's or OpenAI's own API (a CPA or sub2api relay, say) can say its upstream answers a web search tool by itself (#2027). Its `config` hook sets `searches: true` on the provider; the host passes it through (`plugin.Provider.Searches`), and [`pluginProvider`](../../internal/provider/plugins.go) sets the provider's `Searches`, as a custom provider's "Searches the web by itself" does (#359). [`searchesItself`](../../internal/gateway/search.go) honours it for a plugin's provider only, on Anthropic's or Responses' API, and only for one the plugin has a model on: a client's `web_search` then goes to the plugin's `fetch` as sent, not through magpie's search. Another sign-in's `Searches` is never read, and Codex's and Grok's stay as they were. As with a relay, `searchRank` leaves it out, so magpie never picks it to search for other models; Settings can name it by hand (`namedSearcherRank`). +A plugin can instead mark single models of its list with `searches: true` (on the `config` hook's model or the `models` hook's list): a model that searches the web by itself when asked in plain words, such as Trae's search model. The host passes it through (`plugin.Model.Searches`), `pluginCatalog` makes it the catalog model's `WebSearch` "native", and the gateway offers the provider in Settings › Web search's picker and its own pick with those models alone (`pluginSearches`, `partSearcher`). + ## A plugin's service tiers A plugin says which service tiers its vendor serves a model at with `serviceTiers` on the model (#2514): ids such as `"priority"` and `"ultrafast"`, or objects with an `id`. The host keeps the ids (`tiersOf` in `host.js`, into `plugin.Model.ServiceTiers`), `pluginCatalog` puts them on the catalog model's `Tiers`, and [`CodexTiers`](../../internal/provider/codex_models.go) offers them to Codex, as `codexcat` names them (`priority` is Fast). In [`forwardTranslated`](../../internal/gateway/gateway.go) a request translated to the plugin's Chat or Responses keeps its `service_tier` (`Request.OwnTier`) only when it is among the model's tiers (`provider.PluginTiers`); a request relayed in its own protocol was sent as the client wrote it before, and still is. A model served at `priority` on its Chat or Responses API has fast mode ([`CanFast`](../../internal/provider/groupfast.go), via `pluginTierOn`), whichever plugin it is: it can be a group's fast member or an agent's fast pick, and it can be picked Ultrafast when it is served at `ultrafast` too (`UltraOn`). Fast mode in Claude's words (speed `"fast"`, or a fast pick from an agent on Claude's API) reaches the plugin as tier `priority`, or as `ultrafast` for an Ultrafast pick, when the model is served at that tier (Lovest on Discord). Cursor's `fast` flag and `-fast` siblings still give Cursor's plugin fast mode as before. diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index d6a3e4675..5d23360c1 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -121,7 +121,8 @@ type Model struct { // WebSearch is how another magpie (a Remote magpie) searches the web // for the model when a web search tool is offered: "native", its own // provider by itself, or "magpie", with that magpie's searcher; "" it - // doesn't, or didn't say. + // doesn't, or didn't say. A plugin's model it says searches by itself + // (m.searches) is "native" too. WebSearch string `json:",omitempty"` } diff --git a/internal/gateway/search.go b/internal/gateway/search.go index e47ea0627..744823455 100644 --- a/internal/gateway/search.go +++ b/internal/gateway/search.go @@ -206,12 +206,35 @@ func modelSearchesAny(p provider.Provider, model string) bool { return slices.ContainsFunc(p.Speaks(), func(proto provider.Protocol) bool { return modelSearches(p, proto, model) }) } +// pluginSearches is whether p's plugin says model searches the web by +// itself when asked in plain words (m.searches, as catalog.Model's +// WebSearch "native"): the gateway asks it with no search tool, and takes +// what it says with the links in it (searchWith). +func pluginSearches(p provider.Provider, model string) bool { + if !p.IsPlugin() { + return false + } + i := slices.IndexFunc(p.Available(), func(m catalog.Model) bool { return m.ID == model }) + return i >= 0 && p.Available()[i].WebSearch == searchNative +} + +// searchesAlone is whether model of p searches by itself where the rest +// of p's models don't: one of searchModels, or a plugin's it marks. +func searchesAlone(p provider.Provider, model string) bool { + return modelSearchesAny(p, model) || pluginSearches(p, model) +} + // partSearcher is a provider of searchModels that searches by itself for -// those of its models alone, on no API for every model: one magpie may -// search with, with those models only (searchableModel). Command Code's -// searched for themselves but were left out of the searcher picker and -// magpie's own pick (01huadalang on Discord). +// those of its models alone, on no API for every model, or a plugin's +// that marks some of its models as ones that search (pluginSearches): one +// magpie may search with, with those models only (searchableModel). +// Command Code's searched for themselves but were left out of the +// searcher picker and magpie's own pick (01huadalang on Discord), and so +// was every plugin's, Trae's search model among them (01huadalang, 10-20). func partSearcher(p provider.Provider) bool { + if p.IsPlugin() { + return slices.ContainsFunc(p.Available(), func(m catalog.Model) bool { return m.WebSearch == searchNative }) + } if p.Account != nil || slices.ContainsFunc([]provider.Protocol{provider.Anthropic, provider.Responses, provider.Chat}, func(proto provider.Protocol) bool { return searchesItself(p, proto) }) { return false } @@ -291,7 +314,7 @@ func searchRank(p provider.Provider) int { return 3 case searchesItself(p, provider.Chat): return 4 - case partSearcher(p) && slices.ContainsFunc(p.Available(), func(m catalog.Model) bool { return modelSearchesAny(p, m.ID) }): + case partSearcher(p) && slices.ContainsFunc(p.Available(), func(m catalog.Model) bool { return searchesAlone(p, m.ID) }): return 4 case googleAccount(p): // Gemini on a Google sign-in, by googleSearch (#757) diff --git a/internal/gateway/search_plugin_model_test.go b/internal/gateway/search_plugin_model_test.go new file mode 100644 index 000000000..67bf5a19a --- /dev/null +++ b/internal/gateway/search_plugin_model_test.go @@ -0,0 +1,72 @@ +package gateway + +import ( + "context" + "encoding/json" + "io" + "net/http" + "slices" + "strings" + "sync" + "testing" +) + +// A plugin that marks a model of its list as one that searches the web by +// itself (m.searches) is offered in Settings' searcher picker with that +// model alone, is the searcher magpie picks when nothing better is on, and +// searches when asked in plain words. Every plugin's provider was left out +// of the picker whatever its models did, so Trae's search model couldn't +// be picked (01huadalang on Discord, 10-20). +func TestPluginSearchModelIsASearcher(t *testing.T) { + t.Setenv("FAKE_SEARCHER", "1") + var mu sync.Mutex + var asked []string + pid := besideFake(t, "fakeco", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + var q struct { + Model string `json:"model"` + Tools []json.RawMessage `json:"tools"` + } + json.Unmarshal(b, &q) + mu.Lock() + asked = append(asked, r.URL.Path+" "+q.Model) + mu.Unlock() + if q.Model != "fake-search" || len(q.Tools) > 0 { + http.Error(w, `{"error":{"message":"unexpected"}}`, 400) + return + } + w.Header().Set("Content-Type", "text/event-stream") + io.WriteString(w, sse(`data: {"id":"c","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"上海今天小雨,22/17℃。来源:https://www.weather.com.cn/weather/101020100.shtml"},"finish_reason":"stop"}]}`, `data: [DONE]`)) + })) + + var offered []string + for _, c := range Searchers() { + for _, m := range c.Models { + offered = append(offered, c.Provider.ID+"/"+m.ID) + } + if c.Provider.ID == pid && (c.Small != "fake-search" || c.ManualOnly) { + t.Fatalf("the plugin searches with %q, manual only %v", c.Small, c.ManualOnly) + } + } + if !slices.Equal(offered, []string{pid + "/fake-search"}) { + t.Fatalf("the picker offers %v, want the plugin's fake-search alone", offered) + } + p, m, ok := searcher() + if !ok || p.ID != pid || m != "fake-search" { + t.Fatalf("searcher = %s %s %v", p.ID, m, ok) + } + + said, _, err := New().webSearch(context.Background(), "今天上海天气") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(said, "22/17℃") { + t.Fatalf("the search said %q", said) + } + mu.Lock() + got := slices.Clone(asked) + mu.Unlock() + if !slices.Equal(got, []string{"/v1/chat/completions fake-search"}) { + t.Fatalf("the vendor was asked %q", got) + } +} diff --git a/internal/gateway/search_remote.go b/internal/gateway/search_remote.go index 6cefced91..9f24e19eb 100644 --- a/internal/gateway/search_remote.go +++ b/internal/gateway/search_remote.go @@ -52,7 +52,8 @@ func remoteSearchesAny(p provider.Provider) bool { // searchableModel is a model of p that magpie may search with: a Gemini // on a Google sign-in, one a Remote magpie searches natively for, one of -// searchModels that searches by itself (partSearcher), any other +// searchModels or a plugin marks that searches by itself (partSearcher), +// any other // provider's. func searchableModel(p provider.Provider, model string) bool { switch { @@ -61,7 +62,7 @@ func searchableModel(p provider.Provider, model string) bool { case p.IsRemoteMagpie(): return remoteSearch(p, model) == searchNative case partSearcher(p): - return modelSearchesAny(p, model) + return searchesAlone(p, model) } return true } diff --git a/internal/plugin/host.js b/internal/plugin/host.js index 36376492b..61642a7bc 100644 --- a/internal/plugin/host.js +++ b/internal/plugin/host.js @@ -1715,6 +1715,8 @@ async function info(id, key, strict) { variants: m.variants ?? was?.variants ?? {}, // magpie's own mark: a decision model, answering System One ...(typeof m.decides === "boolean" ? { decides: m.decides } : was?.decides !== undefined ? { decides: was.decides } : {}), + // and one that searches the web by itself, asked in plain words + ...(typeof m.searches === "boolean" ? { searches: m.searches } : was?.searches !== undefined ? { searches: was.searches } : {}), } } const server = auths().get(id)?.spec @@ -1974,6 +1976,9 @@ async function providers({ proxies } = {}) { // a decision model (System One's), hidden from agents and // offered as a routing group's classifier decides: m.decides === true, + // a model that searches the web by itself, asked in plain + // words, which magpie may search with for other models + searches: m.searches === true, })), }) } diff --git a/internal/plugin/providers.go b/internal/plugin/providers.go index 72bfe1f83..5a6be67e2 100644 --- a/internal/plugin/providers.go +++ b/internal/plugin/providers.go @@ -103,6 +103,10 @@ type Model struct { // not named jev-…, Clef or a vendor's own). It counts only where the // provider says it serves a decision API (Provider.Decide). Decides bool `json:"decides,omitempty"` + // Searches is set by the plugin on a model that searches the web by + // itself when asked in plain words (m.searches: Trae's search model), + // which magpie may then search with for other models. + Searches bool `json:"searches,omitempty"` } // Provider is a provider a plugin signs in to. diff --git a/internal/plugin/testdata/fake/index.js b/internal/plugin/testdata/fake/index.js index 256a99c70..5a4933442 100644 --- a/internal/plugin/testdata/fake/index.js +++ b/internal/plugin/testdata/fake/index.js @@ -275,6 +275,10 @@ export const FakePlugin = async ({ client }) => ({ p.models["mimo-v2.6-flash-free"] = { ...p.models["fake-1"], id: "mimo-v2.6-flash-free", name: "MiMo V2.6 Flash Free", api: { ...p.models["fake-1"].api, id: "mimo-v2.6-flash-free" }, free: true, variants: {}, capabilities: {"temperature":true,"reasoning":true,"attachment":true,"toolcall":true,"input":{"text":true,"image":true,"audio":true,"video":true,"pdf":false},"output":{"text":true,"image":false,"audio":false,"video":false,"pdf":false},"interleaved":{"field":"reasoning_content"}} } } + // $FAKE_SEARCHER: one more model, that searches the web by itself + // when asked in plain words, marked on the list the hook gives, as + // Trae CN's search_agent ("Search") is listed beside its chat models + if (process.env.FAKE_SEARCHER) p.models["fake-search"] = { ...p.models["fake-1"], id: "fake-search", name: "Fake Search", api: { ...p.models["fake-1"].api, id: "fake-search" }, free: false, searches: true } // $FAKE_DECIDE: one more decision model, marked on the list the hook // gives, as a plugin that reads its vendor's list marks one if (process.env.FAKE_DECIDE) p.models["fake-judge"] = { ...p.models["fake-1"], id: "fake-judge", name: "Fake Judge", api: { ...p.models["fake-1"].api, id: "fake-judge" }, free: false, decides: true } diff --git a/internal/provider/plugins.go b/internal/provider/plugins.go index 96c6df7ec..96dc33ca8 100644 --- a/internal/provider/plugins.go +++ b/internal/provider/plugins.go @@ -234,6 +234,10 @@ func pluginCatalog(pp plugin.Provider) []catalog.Model { if c.Name == "" { c.Name = m.ID } + // a model that searches the web by itself, asked in plain words + if m.Searches { + c.WebSearch = "native" + } if m.Reasoning { c.Efforts, c.Reasoning = m.Variants, true }