Files
magpie/internal/gateway/custom_tool.go
T
yetone 5b12407e8e gateway: a custom tool a Responses upstream turns away is translated, not given back as a 400 (#2656)
DeepSeek's Responses API answers Codex's exec, a custom tool, with 400
"Unsupported custom tool: 'exec'. Only 'apply_patch' is supported."
The relay (attempt -> passthrough) sent the tools as they came, so the
translation that offers a custom tool as a function and gives its call
back as a custom_tool_call never ran, and the 400 matched no word list,
so a group's next member wasn't asked either.

- passthrough: a 400/422 matching customToolRefusal, on a Responses
  body that offers a custom tool (tools, a namespace's, an input item's
  tools), marks the model (customToolRefused) and returns not done.
- attempt: then, and on a marked model's later requests with a custom
  tool, the request is translated on the client's own API (ownAPI), so
  exec keeps its name and comes back as custom_tool_call. A Codex
  compaction (keepsCompaction) isn't translated and gets the refusal.
- shapeRefused also takes customToolRefusal, so where the refusal does
  reach a group, the next member is asked and nobody rests.

Not scoped to DeepSeek: any Responses relay (key, plugin) that says its
custom tools are unsupported is handled the same; an upstream that never
refused them still gets them as sent (tested).

Tests: custom_tool_test.go, fixture from the reporter's literal error
text and request. TestCustomToolTranslatedWhereRefused (stream and
not), TestCustomToolRefusalInGroup (reporter's ordered group, next
member asked 0 times), TestCustomToolRefusalWordings. Broken by hand:
passthrough branch off -> both request tests 400 with the reporter's
exact message; attempt pre-skip off -> next turn asked 4 times, not 3;
shapeRefused addition off -> Wordings red.

Run:
- gofmt -l clean; go vet ./... (darwin), GOOS=linux and GOOS=windows
  go vet -tags nogui ./... clean, at the pushed head.
- Full go test -tags nogui -timeout 45m ./... on e085d6e92 + this
  change: every package ok except internal/agent, timed out at 2706s
  (TestPortMovesEveryAgent running 30s at the deadline; load ~100-150;
  same as #2544, commented there). internal/agent alone -timeout 60m:
  ok 3031s.
- After rebasing onto cb05dc2d1 (3 commits, none in this path):
  internal/gateway -timeout 60m run twice. First: FAIL at 2559s; I
  piped it through tail and lost which test, so it is not named or
  filed here. Second, full log kept: ok 2509s (load ~150).
- Rebased again onto 9f9bfc160 (3 more commits: agent, provider's
  Codex window; none in this path): TestCustomTool*, TestCodexCustom*,
  TestToolStrict*, *Shape* ok; gofmt and vet (darwin, linux, windows)
  clean. Then onto 85eef5d87 (GUI quota refresh only): build and
  TestCustomTool* ok. The full suite was not rerun at this head.
- Not tried against the real DeepSeek API; the fake upstream refuses as
  the reporter's logs show.
2026-10-17 17:44:23 +08:00

67 lines
2.3 KiB
Go

package gateway
import (
"bytes"
"encoding/json"
"regexp"
)
// Codex offers custom tools on Responses (exec in code mode, apply_patch):
// a tool of type custom whose call carries free-form input. A vendor's
// Responses API may take none of them, or only some: DeepSeek's answers
// 400 "Unsupported custom tool: 'exec'. Only 'apply_patch' is supported."
// to a request that only offers exec (#2656). A relay sends the tools as
// they came, so such a request is translated instead, on the same API:
// the translation offers each custom tool as a function taking its input
// (customSchema) and gives a call to one back as the custom_tool_call
// Codex runs. Once the model has turned them away it is translated from
// the start.
// customToolRefusal is an upstream turning a custom tool away: DeepSeek's
// "Unsupported custom tool: 'exec'", or the type named unsupported. A
// refusal of anything else about a tool doesn't match.
var customToolRefusal = regexp.MustCompile(`(?i)unsupported custom tool|custom tools? (?:is |are )?not (?:supported|allowed)|(?:unsupported|unknown|invalid) tool type[^\n{}]{0,20}?['"\x60]custom['"\x60]|tool type ['"\x60]custom['"\x60] (?:is )?(?:not supported|unsupported)`)
// customToolRefused is how unfit remembers model turning custom tools
// away.
func customToolRefused(model string) string { return "custom tools\x00" + model }
// hasCustomTool reports whether a Responses body offers a custom tool:
// among its tools, in a namespace there, or in an input item that carries
// tools (Responses Lite's additional_tools, a tool search's output).
func hasCustomTool(body []byte) bool {
if !bytes.Contains(body, []byte(`"custom"`)) {
return false
}
var m map[string]any
if json.Unmarshal(body, &m) != nil {
return false
}
if customIn(m["tools"]) {
return true
}
items, _ := m["input"].([]any)
for _, it := range items {
if im, _ := it.(map[string]any); im != nil && customIn(im["tools"]) {
return true
}
}
return false
}
// customIn reports whether a list of tools has a custom one, a namespace's
// included.
func customIn(v any) bool {
tools, _ := v.([]any)
for _, t := range tools {
tm, _ := t.(map[string]any)
if tm == nil {
continue
}
if tm["type"] == "custom" || tm["type"] == "namespace" && customIn(tm["tools"]) {
return true
}
}
return false
}