Files
magpie/internal/gateway/chat_tool_shape_test.go
T
yetone 72d6103e28 gateway: a Chat tool in MCP's or another API's shape goes upstream as a Chat function tool (#2622)
jinyue806 (#2622): ZCode sends its tools on /v1/chat/completions in MCP's
shape, {"name","description","inputSchema"}, with no type and no function
object. magpie relayed them as they came, and SenseNova turned every request
with tools away ("field Tools[0].Type invalid, should be set"; a flat
{"type":"function","name":…} got "function cannot be empty"). A missing
type is invalid Chat Completions for every upstream, not SenseNova only, and
the translated routes were broken too: parseChat read no name from such a
tool, so Messages and Responses upstreams were offered a tool named "".

shapeChatTools puts a tool with its name at the top (no type, or type
"function" without a function object) into {"type":"function","function":
{name, description, parameters, strict}}, parameters taken from
parameters, inputSchema or input_schema; other fields stay beside it. It
runs right after liftMessageTools at the same two choke points: handle, for
every Chat body before routing (relay, schema fixers, redaction, plugins),
and parseChat, for a body that never came through handle. A tool that has
its function, a hosted tool (any other type) and one with no name are left
alone, and a request with nothing to shape is relayed byte for byte.

Verified:
- TestChatToolInAnotherShapeIsShaped sends the reporter's curl body with
  each of the four tool shapes from the report through the Chat relay,
  Messages and Responses. With shapeChatTools made a no-op by hand all 12
  subtests FAIL: relayed as {"name":"f","inputSchema":{…}} as it came, and
  translated as {"name":"",…}. TestShapeChatToolsLeavesChatShape (byte for
  byte on Chat-shaped/hosted/nameless tools; strict and cache_control kept)
  FAILs on the same break. Both pass restored.
- gofmt -l: clean. go vet -tags nogui ./... for darwin, linux, windows: ok.
- Full suite at 81313b912 + this change, sandbox HOME, pinned caches:
  set -o pipefail; go test -tags nogui -timeout 45m ./... : 59 packages ok
  (internal/gateway 1134s, internal/provider 548s). 7 packages failed only
  on "no space left on device" while the disk filled mid-run (cli
  TestModelDisableCmd, library TestAgentMCPFiles, sessions
  TestCodexTitleEvidenceBound, settings TestSaveConcurrent, and gui, tui,
  usage which printed PASS but couldn't write testlog.txt). Rerun once
  space was back (30G free): cli, gui, library, sessions, settings, tui,
  usage all ok.
- Rebased onto origin/main a69cd0707 (#2611's chat call ids, chattidy):
  gofmt clean, vet darwin/linux/windows on internal/gateway ok,
  go test -tags nogui -timeout 45m ./internal/gateway ./internal/source:
  ok (gateway 510s). No other package changed between the two bases.
- Not tried against the real SenseNova (no key here); the reporter's own
  table shows {"type":"function","function":{…}} answered 200.

The "inference request is invalid ... reason=unknown retryable=false" text
in the report is not magpie's (no such string in the repo); it is the
client's wrapper of the upstream error, so nothing changed there.
2026-10-17 04:07:25 +08:00

109 lines
4.5 KiB
Go

package gateway
import (
"strings"
"testing"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// zcodeAsking is jinyue806's reproduction of #2622 as they sent it to
// magpie, with tool standing in for its tools[0]. The tools are the
// shapes the report tried, each of which SenseNova turned away.
const zcodeAsking = `{
"model": "sensenova/kimi-k3",
"messages": [{"role":"user","content":"hi"}],
"max_tokens": 8,
"tools": [TOOL]
}`
var zcodeTools = map[string]string{
"name and parameters": `{"name":"f","parameters":{"type":"object","properties":{}}}`,
"mcp inputSchema": `{"name":"f","inputSchema":{"type":"object","properties":{}}}`,
"with description": `{"name":"f","description":"d","parameters":{"type":"object","properties":{}}}`,
"flat function": `{"type":"function","name":"f","parameters":{"type":"object","properties":{}}}`,
}
// A tool a Chat client sends in MCP's or another API's shape reaches the
// upstream as a Chat function tool when relayed, and as the named tool
// when translated (#2622).
func TestChatToolInAnotherShapeIsShaped(t *testing.T) {
srv, seen := relayOfEvery(t)
for _, route := range []struct {
name, model, path string
p provider.Provider
check func(t *testing.T, body string)
}{
{"chat relayed", "kimi-k3", "/v1/chat/completions", provider.Provider{Chat: srv.URL + "/v1"}, func(t *testing.T, body string) {
tool := gjson.Get(body, "tools.0")
if tool.Get("type").String() != "function" || tool.Get("function.name").String() != "f" ||
tool.Get("function.parameters.type").String() != "object" {
t.Errorf("tool relayed as %s", tool.Raw)
}
for _, k := range []string{"name", "parameters", "inputSchema", "description"} {
if tool.Get(k).Exists() {
t.Errorf("%s left beside the function: %s", k, tool.Raw)
}
}
}},
{"messages", "claude-opus-5-5", "/v1/messages", provider.Provider{Anthropic: srv.URL}, func(t *testing.T, body string) {
if tool := gjson.Get(body, "tools.0"); tool.Get("name").String() != "f" || tool.Get("input_schema.type").String() != "object" {
t.Errorf("tool sent as %s", tool.Raw)
}
}},
{"responses", "kimi-k3", "/v1/responses", provider.Provider{Responses: srv.URL + "/v1"}, func(t *testing.T, body string) {
if tool := gjson.Get(body, "tools.0"); tool.Get("name").String() != "f" || tool.Get("parameters.type").String() != "object" {
t.Errorf("tool sent as %s", tool.Raw)
}
}},
} {
for shape, tool := range zcodeTools {
t.Run(route.name+"/"+shape, func(t *testing.T) {
fresh(t)
p := route.p
p.ID, p.Name, p.Key, p.Models = "sensenova", "SenseNova", "k", []string{route.model}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
seen()
body := strings.Replace(strings.Replace(zcodeAsking, "TOOL", tool, 1), "kimi-k3", route.model, 1)
if code, b := post(t, "/v1/chat/completions", body); code != 200 {
t.Fatalf("%d %s", code, b)
}
paths, bodies := seen()
if len(paths) != 1 || paths[0] != route.path {
t.Fatalf("went to %v, want %s", paths, route.path)
}
route.check(t, bodies[0])
})
}
}
}
// A request whose tools are all in the Chat shape, or hosted, is relayed
// byte for byte; only a tool in another shape is rewritten, and its other
// fields stay.
func TestShapeChatToolsLeavesChatShape(t *testing.T) {
for _, body := range []string{
`{"model":"m","messages":[],"tools":[{"type":"function","function":{"name":"f","parameters":{"type":"object"}}}]}`,
`{"model":"m","messages":[],"tools":[{"type":"web_search_preview"},{"type":"function","function":{"name":"g"}}]}`,
`{"model":"m","messages":[],"tools":[{"type":"custom","name":"apply_patch"}]}`,
`{"model":"m","messages":[],"tools":[{"description":"no name"}]}`,
`{"model":"m","messages":[{"role":"user","content":"tools"}]}`,
} {
if got := string(shapeChatTools([]byte(body))); got != body {
t.Errorf("%s\nbecame %s", body, got)
}
}
got := shapeChatTools([]byte(`{"model":"m","tools":[{"type":"function","function":{"name":"a"}},{"name":"b","input_schema":{"type":"object"},"strict":true,"cache_control":{"type":"ephemeral"}}]}`))
if a := gjson.GetBytes(got, "tools.0").Raw; a != `{"type":"function","function":{"name":"a"}}` {
t.Errorf("a Chat tool changed: %s", a)
}
b := gjson.GetBytes(got, "tools.1")
if b.Get("function.name").String() != "b" || !b.Get("function.strict").Bool() ||
b.Get("function.parameters.type").String() != "object" || b.Get("cache_control.type").String() != "ephemeral" {
t.Errorf("tool shaped as %s", b.Raw)
}
}