gateway: an allowed_tools choice reaches the Grok plugin's fetch, end to end (#3153)
Follow-up to ggbdpq's grokShaped change: its test reads grokPluginBody alone. skygunner's TestAllowedToolsReachesGrokPlugin (852182f6 on their fork, from the #3153 thread) posts a /v1/responses request with an allowed_tools choice through the gateway to the fake Grok plugin and checks its fetch gets that object as sent, which is where the plugin decides on x_search. Taken as written; provider-plugins.md names it. Broken by hand: with internal/provider/grok.go atd307aa648(before the fix) it is red ("sent" with no tool_choice), green with it. The plugin side was read in magpie-community/plugins at 26864ec (grok 0.2.3, the mover's min): rewrite decides x_search on the choice as it came (null or "auto" only) and deletes an object whose type isn't in TOOLS before Grok, so no min raise is needed. Run at the pushed head on mote: go test ./... -timeout 45m on mote with this branch on41b5c0f19: 83 packages ok. internal/gui is red on TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestMainWindowCorners (#3949), both red alone at41b5c0f19too, and on TestColdTurnsTaken (#4510). internal/update is red on TestRelaunchOpenTriedAgain (#3881). The last two pass alone at41b5c0f19and here. Rebased onf856d381a: agent, gateway, plugin, provider and sessions ok, gui red on #4514 and #3949 only, gofmt -l clean, go vet ok for linux (nogui), windows and darwin. Rebased onbb478f0e5: agent and gateway ok, gui the same. The last rebase, onto573b21f4b, brings in only internal/gui/providers*.go and internal/provider/models.go, which this branch doesn't touch. Co-authored-by: vincent <webmaster@palmit.com.au> Co-authored-by: ggbdpq <ggbdpq@gmail.com>
This commit is contained in:
@@ -96,7 +96,7 @@ A plugin can instead mark single models of its list with `searches: true` (on th
|
||||
|
||||
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 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`). A request the plugin serves in its own Responses protocol goes to its `fetch` as the client wrote it, and the client's own `tool_choice` object of a type Grok's backend doesn't take (an `allowed_tools` naming the client's tools) was dropped the same way (`grokShaped`), so the plugin couldn't tell it from no choice and added `x_search` over it: a tool outside the client's allowed list went out. `grokPluginBody` keeps such an object for the plugin to read now, and the plugin leaves it out before Grok as it does the lone `"none"` (#3153); the built-in, which is Grok's last stop, still drops it. 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), `TestGrokPluginBodyKeepsLoneNone` and `TestGrokPluginBodyKeepsUnsupportedToolChoice`, 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`). A request the plugin serves in its own Responses protocol goes to its `fetch` as the client wrote it, and the client's own `tool_choice` object of a type Grok's backend doesn't take (an `allowed_tools` naming the client's tools) was dropped the same way (`grokShaped`), so the plugin couldn't tell it from no choice and added `x_search` over it: a tool outside the client's allowed list went out. `grokPluginBody` keeps such an object for the plugin to read now, and the plugin leaves it out before Grok as it does the lone `"none"` (#3153); the built-in, which is Grok's last stop, still drops it. 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), `TestGrokPluginBodyKeepsLoneNone`, `TestGrokPluginBodyKeepsUnsupportedToolChoice` and the gateway-to-plugin `TestAllowedToolsReachesGrokPlugin`, cover it.
|
||||
|
||||
## A plugin model's sampling
|
||||
|
||||
|
||||
@@ -190,3 +190,30 @@ func TestToolsOffWithoutToolsPerBuilder(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// #3153: the Grok plugin must see the client's allowed list before it
|
||||
// decides whether to add its own server-side search tool.
|
||||
func TestAllowedToolsReachesGrokPlugin(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var sent []byte
|
||||
pid := besideFake(t, "grok", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
mu.Lock()
|
||||
sent = b
|
||||
mu.Unlock()
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
io.WriteString(w, noneReply)
|
||||
}))
|
||||
const choice = `{"type":"allowed_tools","mode":"auto","tools":[{"type":"function","name":"read_file"}]}`
|
||||
code, body := postTo(t, New(), "/v1/responses", `{"model":"`+pid+`/fake-resp","stream":true,"input":"hi","tools":[{"type":"function","name":"read_file","parameters":{"type":"object","properties":{}}}],"tool_choice":`+choice+`}`)
|
||||
if code != 200 {
|
||||
t.Fatalf("%d %s", code, body)
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
var got map[string]json.RawMessage
|
||||
if err := json.Unmarshal(sent, &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sameJSON(t, got["tool_choice"], choice)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user