diff --git a/docs/subsystems/provider-plugins.md b/docs/subsystems/provider-plugins.md index 9e48115a0..5cf53129c 100644 --- a/docs/subsystems/provider-plugins.md +++ b/docs/subsystems/provider-plugins.md @@ -70,7 +70,7 @@ A plugin in front of Anthropic's or OpenAI's own API (a CPA or sub2api relay, sa 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. -A request translated to a plugin's Responses API that turned tools off with no tools declared (Chat's `tool_choice: "none"`, Anthropic's `{"type":"none"}`, Gemini's `functionCallingConfig.mode` `NONE`) reaches the plugin's `fetch` with `tool_choice: "none"` and no `tools` (#3110). Before, it went without a `tool_choice`, the same as a request with no preference, so a plugin that adds a server-side tool of its own (Grok's `x_search`, magpie-community/plugins#263) couldn't respect it. [`buildResponses`](../../internal/gateway/responses.go) keeps the `"none"`, and [`forwardTranslated`](../../internal/gateway/gateway.go) leaves a lone `tool_choice` out for every upstream that isn't a plugin, and for xAI and Copilot (`refusesLoneToolChoice`, #378), so what a vendor is sent is unchanged. The plugin knows whether its vendor takes it. Grok's Responses backend doesn't: the Grok plugin's bodies are shaped by `grokPluginBody`, which keeps the lone `"none"` that `grokBody` drops for the built-in, and the plugin leaves it out before its request goes to Grok (0.1.18, the mover's `min`). Chat, Anthropic and Gemini requests still carry no tool choice beside no tools, which OpenAI's Chat refuses ("'tool_choice' is only allowed when 'tools' are specified"). `TestToolsOffWithoutToolsReachesPlugin`, `TestToolsOffWithoutToolsNotSentToVendor`, `TestToolsOffWithoutToolsLeftOutForXAI` and `TestToolsOffWithoutToolsPerBuilder` in [`tool_choice_none_test.go`](../../internal/gateway/tool_choice_none_test.go), and `TestGrokPluginBodyKeepsLoneNone`, cover it. +A request translated to the Grok plugin's Responses API that turned tools off with no tools declared (Chat's `tool_choice: "none"`, Anthropic's `{"type":"none"}`, Gemini's `functionCallingConfig.mode` `NONE`) reaches the plugin's `fetch` with `tool_choice: "none"` and no `tools` (#3110). Before, it went without a `tool_choice`, the same as a request with no preference, so a plugin that adds a server-side tool of its own (Grok's `x_search`, magpie-community/plugins#263) couldn't respect it. [`buildResponses`](../../internal/gateway/responses.go) keeps the `"none"`, and [`forwardTranslated`](../../internal/gateway/gateway.go) leaves a lone `tool_choice` out for every other upstream (`keepsLoneNone`): every vendor, xAI and Copilot (`refusesLoneToolChoice`, #378), and every other plugin, whose vendors haven't been tried with a lone `"none"`. So what a vendor is sent is unchanged. Grok's Responses backend doesn't take it either: the Grok plugin's bodies are shaped by `grokPluginBody`, which keeps the lone `"none"` that `grokBody` drops for the built-in, and the plugin leaves it out before its request goes to Grok (0.1.18, the mover's `min`). Chat, Anthropic and Gemini requests still carry no tool choice beside no tools, which OpenAI's Chat refuses ("'tool_choice' is only allowed when 'tools' are specified"). `TestToolsOffWithoutToolsReachesPlugin`, `TestToolsOffWithoutToolsNotSentToOtherPlugins`, `TestToolsOffWithoutToolsNotSentToVendor`, `TestToolsOffWithoutToolsLeftOutForXAI` and `TestToolsOffWithoutToolsPerBuilder` in [`tool_choice_none_test.go`](../../internal/gateway/tool_choice_none_test.go), and `TestGrokPluginBodyKeepsLoneNone`, cover it. ## A plugin's decision API diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 67445569e..e56468446 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -5176,12 +5176,13 @@ func (s *Server) forwardTranslated(ctx context.Context, p provider.Provider, to if to == provider.Chat && (p.IsBedrock() || p.IsAzure()) { body = asCompletionTokens(body) } - if to == provider.Responses && (!p.IsPlugin() || refusesLoneToolChoice(p)) { + if to == provider.Responses && !keepsLoneNone(p) { // the "none" buildResponses keeps for a request that turned - // tools off with none declared goes to a plugin alone, which - // reads it before adding a tool of its own (#3110), and knows - // whether its vendor takes it. A vendor was never sent it, and - // xAI turns it away (#378) + // tools off with none declared goes to the Grok plugin alone, + // which reads it before adding x_search and drops it before + // Grok (0.1.18, #3110). Any other upstream was never sent it: + // xAI turns it away (#378), and no other plugin's vendor has + // been tried with it body = withoutLoneToolChoice(body) } if to == provider.Chat && req.WebSearch && p.IsRemoteMagpie() { @@ -6741,6 +6742,13 @@ func withoutFields(body []byte, fields ...string) []byte { return out } +// keepsLoneNone is whether p is sent the lone tool_choice "none" a +// request that turned tools off with none declared keeps (#3110): only the +// Grok plugin, which reads it and leaves it out before Grok itself. +func keepsLoneNone(p provider.Provider) bool { + return p.PluginProvider() == "grok" && !refusesLoneToolChoice(p) +} + // refusesLoneToolChoice is whether p's Responses API turns away a // tool_choice with no tools beside it: xAI's, and Copilot's /responses in // front of it for Grok (#378). diff --git a/internal/gateway/tool_choice_none_test.go b/internal/gateway/tool_choice_none_test.go index 75a1e8d4f..80ab1c027 100644 --- a/internal/gateway/tool_choice_none_test.go +++ b/internal/gateway/tool_choice_none_test.go @@ -30,7 +30,7 @@ var noneReply = sse( // #3110: a Chat, Anthropic or Gemini request that turned tools off, with // no tools, reaches a provider plugin on Responses still saying "none", // so a plugin adding a server-side tool (Grok's x_search) can tell it -// from no choice. The plugin knows whether its vendor takes it. +// from no choice. The plugin drops it before Grok (0.1.18). func TestToolsOffWithoutToolsReachesPlugin(t *testing.T) { var mu sync.Mutex var sent []map[string]any @@ -71,6 +71,37 @@ func TestToolsOffWithoutToolsReachesPlugin(t *testing.T) { } } +// Any other provider plugin on Responses is sent no lone tool_choice: its +// vendor hasn't been tried with one, and a 400 there would be a regression. +func TestToolsOffWithoutToolsNotSentToOtherPlugins(t *testing.T) { + var mu sync.Mutex + var sent [][]byte + pid := besideFake(t, "factory", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + mu.Lock() + sent = append(sent, b) + mu.Unlock() + w.Header().Set("Content-Type", "text/event-stream") + io.WriteString(w, noneReply) + })) + for _, c := range toolsOffNoTools { + t.Run(c.name, func(t *testing.T) { + mu.Lock() + sent = nil + mu.Unlock() + path := strings.Replace(c.path, "fake/m1", pid+"/fake-resp", 1) + body := strings.Replace(c.body, "fake/m1", pid+"/fake-resp", 1) + rec := httptest.NewRecorder() + New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body))) + mu.Lock() + defer mu.Unlock() + if rec.Code != 200 || len(sent) != 1 || strings.Contains(string(sent[0]), `"tool_choice"`) { + t.Errorf("%d %s\nplugin got %q", rec.Code, rec.Body, sent) + } + }) + } +} + // A vendor's Responses API is sent no tool_choice beside no tools, as // before #3110: it has no use for one, and some turn it away. func TestToolsOffWithoutToolsNotSentToVendor(t *testing.T) {