Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
gateway: a request's body is looked at for web search without a copy, and not parsed for a provider that searches by itself
yetone2a27fb8da8fb
diff --git a/internal/gateway/fallback.go b/internal/gateway/fallback.goindex c34871b28..b8e991c30 100644--- a/internal/gateway/fallback.go+++ b/internal/gateway/fallback.go@@ -1354,7 +1354,7 @@ func passing(status int, header http.Header, body []byte, again int) (time.Duratcase wait > longestPause:return 0, falsecase status == 408, status == 500, status == 502, status == 503, status == 504, status == 529:- if strings.Contains(string(body), didNotStream) {+ if bytes.Contains(body, []byte(didNotStream)) {return 0, false}return wait, again < lastRetriesdiff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.goindex a0f248abf..0f7a3f52d 100644--- a/internal/gateway/gateway.go+++ b/internal/gateway/gateway.go@@ -3539,7 +3539,7 @@ func (s *Server) attempt(w http.ResponseWriter, r *http.Request, from provider.PownAPI := false// except a subagent's task its lead sealed (#1109): only the server// that sealed it reads it, and a translation would drop it- if relay && searchAsked(from, body) && (from == provider.Chat && !p.IsRemoteMagpie() || !searchesModel(p, from, model)) && !(from == provider.Responses && hasSealedAgentMessage(body)) {+ if relay && searchedForIt(p, from, model, body) && !(from == provider.Responses && hasSealedAgentMessage(body)) {relay, ownAPI = false, true}// Copilot seals an argument the tools mark "encrypted" (spawn_agent'sdiff --git a/internal/gateway/patience.go b/internal/gateway/patience.goindex b66c71281..8c5fe4394 100644--- a/internal/gateway/patience.go+++ b/internal/gateway/patience.go@@ -1,10 +1,10 @@package gatewayimport (+ "bytes""context""net/http""slices"- "strings""time""github.com/yetone/magpie/internal/provider"@@ -59,7 +59,7 @@ func busy(status int, body []byte) bool {switch status {case 408, 500, 502, 503, 504, 529:return failure(status, body) == failOther && !protectionRefused(status, body) &&- !strings.Contains(string(body), didNotStream)+ !bytes.Contains(body, []byte(didNotStream))case 429:return failure(status, body) == failRate && !creditWords.Match(wordsOf(body))}diff --git a/internal/gateway/routing.go b/internal/gateway/routing.goindex 1d81d5708..04123b3c1 100644--- a/internal/gateway/routing.go+++ b/internal/gateway/routing.go@@ -297,14 +297,14 @@ func failure(status int, body []byte) string {// Kimi's "exceeded model token limit" is the conversation's// length, not the plan'sreturn failOther- case status == 402 && usedUpWords.Match(body) && !creditWords.Match(body) && !strings.Contains(string(body), "insufficient_quota"):+ case status == 402 && usedUpWords.Match(body) && !creditWords.Match(body) && !bytes.Contains(body, []byte("insufficient_quota")):// a 402 that says a usage limit and nothing of money is out of// quota, not of credit: Factory's "You've reached your weekly// standard usage limit (resets in …). Switch to Droid Core …" is// Standard's pool, which counts some models only, and read as the// account out of credit it rested Droid Core with it (#1979)return failQuota- case status == 402, creditWords.Match(body) && (status != 429 || brokeWords.Match(body)) || strings.Contains(string(body), "insufficient_quota"):+ case status == 402, creditWords.Match(body) && (status != 429 || brokeWords.Match(body)) || bytes.Contains(body, []byte("insufficient_quota")):return failCreditcase status == 429 && resetsWhen.Match(body):// out until the time it names, in whichever language it said so:diff --git a/internal/gateway/search.go b/internal/gateway/search.goindex c5c53389e..ce5f6d256 100644--- a/internal/gateway/search.go+++ b/internal/gateway/search.go@@ -1,6 +1,7 @@package gatewayimport (+ "bytes""cmp""context""encoding/json"@@ -215,13 +216,24 @@ var searchHosts = map[provider.Protocol][]string{// searchAsked is whether a body offers the model web search, looked for// cheaply before it is parsed.func searchAsked(proto provider.Protocol, body []byte) bool {- if !strings.Contains(string(body), "web_search") && !strings.Contains(string(body), "oogle_search") && !strings.Contains(string(body), "googleSearch") {+ // bytes, not string(body): each conversion copied the whole body, three+ // times a request (a 200KB body: 640KB and 3ms)+ if !bytes.Contains(body, []byte("web_search")) && !bytes.Contains(body, []byte("oogle_search")) && !bytes.Contains(body, []byte("googleSearch")) {return false}req, err := parse(proto, body)return err == nil && req.WebSearch}+// searchedForIt is whether a relayed request's web search is done by magpie+// for the provider, which can't be relayed: a body offering it to a model+// that doesn't search by itself. What is known of the provider is asked+// first: Codex offers web_search on every turn, and each relayed turn was+// parsed whole to learn it, for a provider that searches by itself.+func searchedForIt(p provider.Provider, from provider.Protocol, model string, body []byte) bool {+ return (from == provider.Chat && !p.IsRemoteMagpie() || !searchesModel(p, from, model)) && searchAsked(from, body)+}+// searcher is the model magpie searches with: the one Settings names, while// it can (chosenSearcher), else the first of the providers that search by// themselves, with a small model of theirs, as searching needs no more.diff --git a/internal/gateway/search_test.go b/internal/gateway/search_test.goindex 9057f5412..84bf1da2d 100644--- a/internal/gateway/search_test.go+++ b/internal/gateway/search_test.go@@ -656,3 +656,49 @@ func TestPluginSaidToSearch(t *testing.T) {})}}++// A request's body is looked at for web search without a copy of it, and+// not parsed at all for a provider that searches by itself: Codex offers+// web_search on every turn, and each relayed turn was parsed whole.+func TestSearchAskedLooksCheaply(t *testing.T) {+ long := strings.Repeat("a long prompt with words in it ", 7000)+ plain := []byte(`{"model":"m","input":"` + long + `"}`)+ if n := testing.AllocsPerRun(20, func() { searchAsked(provider.Responses, plain) }); n != 0 {+ t.Errorf("a %dKB body with no search in it: %v allocations, want 0", len(plain)/1024, n)+ }+ asks := []byte(`{"model":"gpt-5","input":"` + long + `","tools":[{"type":"web_search"}]}`)+ codex := provider.Provider{ID: "codex", Responses: provider.CodexBase, Account: &provider.Account{Agent: "codex"}}+ if searchedForIt(codex, provider.Responses, "gpt-5", asks) {+ t.Error("Codex searches by itself, and magpie searched for it")+ }+ if n := testing.AllocsPerRun(20, func() { searchedForIt(codex, provider.Responses, "gpt-5", asks) }); n > 20 {+ t.Errorf("a provider that searches by itself: %v allocations, the body parsed", n)+ }+ other := provider.Provider{ID: "x", Responses: "https://example.test/v1"}+ if !searchedForIt(other, provider.Responses, "gpt-5", asks) {+ t.Error("a provider that doesn't search: magpie didn't search for it")+ }+ if searchedForIt(other, provider.Responses, "gpt-5", plain) {+ t.Error("a body offering no search: magpie searched for it")+ }+}++func BenchmarkSearchedForIt(b *testing.B) {+ long := strings.Repeat("a long prompt with words in it ", 7000)+ asks := []byte(`{"model":"gpt-5","input":"` + long + `","tools":[{"type":"web_search"}]}`)+ plain := []byte(`{"model":"gpt-5","input":"` + long + `"}`)+ codex := provider.Provider{ID: "codex", Responses: provider.CodexBase, Account: &provider.Account{Agent: "codex"}}+ other := provider.Provider{ID: "x", Responses: "https://example.test/v1"}+ b.Run("codex-asks", func(b *testing.B) {+ b.ReportAllocs()+ for b.Loop() {+ searchedForIt(codex, provider.Responses, "gpt-5", asks)+ }+ })+ b.Run("other-plain", func(b *testing.B) {+ b.ReportAllocs()+ for b.Loop() {+ searchedForIt(other, provider.Responses, "gpt-5", plain)+ }+ })+}