Files
magpie/internal/gateway/chat_tool_reasoning_test.go
T
yetone ea84c99c78 gateway: a DeepSeek model's tool call the client kept no thinking for goes with reasoning_content, so DeepSeek no longer turns the request away (#1462, Clear8Heart)
DeepSeek's Chat API in thinking mode refuses a whole request with 400
"The `reasoning_content` in the thinking mode must be passed back to the
API" when an assistant tool-call message since the user's last message
comes back without reasoning_content. OpenCode (and any client that
continues a turn another model made, or that dropped the thinking) sends
such turns, and magpie relayed them as they came.

What changed:
- chatReplaysReasoning(host, model) names the Chat upstreams that want
  reasoning back: a DeepSeek host, a model named deepseek wherever it is
  served (OpenCode Go's deepseek-v4.1-flash, OpenRouter), and Command
  Code's plugin. buildChat uses it as before.
- Passthrough (gateway.go, provider.Chat): for such an upstream, not
  Mistral (#494), withReasoningOnToolTurns gives every assistant
  tool-call message without reasoning_content one: its thinking from
  another field (reasoning, OpenRouter's) when it has some, else " ".
  The gate takes the member's model, so a group reaching a DeepSeek
  member is covered.
- Translated (buildChat, Responses/Anthropic -> Chat): a tool-call turn
  with no thinking gets " " too.
- Every tool turn, not only the last: DeepSeek also refuses an earlier
  tool turn of the current user turn missing it.
- " ", not "": DeepSeek's Chat takes "", but relays that omit empty
  fields drop it, and DeepSeek's Responses API refuses an empty
  reasoning_text. A vendor that refuses the field still gets the #494
  retry without it.

Checked against api.deepseek.com (owner's key, deepseek-flash, Chat):
last tool turn missing -> 400; "" -> 200; " " -> 200; real -> 200; an
earlier tool turn of the current user turn missing -> 400; a tool turn
before the latest user message missing -> 200; answers missing -> 200.

Not changed, found on the way: DeepSeek's Responses API refuses a
function_call with no reasoning item (and empty reasoning_text), and its
Anthropic API a tool_use with no thinking block; magpie relays those
natively as they come. OpenCode Go's deepseek-v4.1-flash could not be
tried for real: the owner's workspace answers "requires Global regions".

Run:
- New tests fail on the old code (TestChatToolTurnWithoutReasoningRelayed:
  400 "reasoning_content ... must be passed back"; TestTranslatedToolTurn
  WithoutReasoning: 400), pass now; TestChatToolTurnWithoutReasoning
  NotForMistral and the Mistral/MessageReasoning tests pass.
- gofmt -l clean; go vet -tags nogui ./internal/gateway on darwin,
  GOOS=linux, GOOS=windows.
- go test -tags nogui -count=1 ./... under a temp HOME: all ok.
- Real use: the built binary's gateway in a sandbox HOME with only the
  DeepSeek provider. Old: last tool turn missing 400, earlier one
  missing (stream) 400, plain answer 200. New: all three 200.
2026-10-10 14:14:47 +08:00

152 lines
7.5 KiB
Go

package gateway
import (
"encoding/json"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// deepseekRefuseToolTurns answers as DeepSeek's Chat API does in thinking
// mode (checked against api.deepseek.com, October 2026): a tool call made
// since the user's last message that comes back without reasoning_content
// turns the whole request away, while an empty one, a reply's, or one
// from before the user's last message missing are taken.
func deepseekRefuseToolTurns(body []byte) (int, string) {
var q struct {
Messages []map[string]json.RawMessage `json:"messages"`
}
json.Unmarshal(body, &q)
last := -1
for i, m := range q.Messages {
if string(m["role"]) == `"user"` {
last = i
}
}
for _, m := range q.Messages[last+1:] {
if string(m["role"]) == `"assistant"` && len(m["tool_calls"]) > 0 && m["reasoning_content"] == nil {
return 400, `{"error":{"message":"The ` + "`reasoning_content`" + ` in the thinking mode must be passed back to the API. (request_id: 06048513-d7c7-4cdf-9998-47bfc20484e6)","type":"invalid_request_error","param":null,"code":"invalid_request_error"}}`
}
}
return 0, ""
}
const timeCall = `{"id":"call_1","type":"function","function":{"name":"get_time","arguments":"{}"}}`
// the issue's request: OpenCode continuing a turn whose tool calls it has
// no thinking for (one another model made), and one that has its own
const toolTurnsNoReasoning = `{"model":"%s","stream":false,"tools":[{"type":"function","function":{"name":"get_time","parameters":{"type":"object","properties":{}}}}],"messages":[
{"role":"user","content":"hi"},
{"role":"assistant","content":"Hello!"},
{"role":"user","content":"what time is it? twice"},
{"role":"assistant","content":null,"tool_calls":[` + timeCall + `]},
{"role":"tool","tool_call_id":"call_1","content":"12:00"},
{"role":"assistant","content":null,"tool_calls":[{"id":"call_2","type":"function","function":{"name":"get_time","arguments":"{}"}}],"reasoning_content":"Once more."},
{"role":"tool","tool_call_id":"call_2","content":"12:01"},
{"role":"assistant","content":null,"tool_calls":[{"id":"call_3","type":"function","function":{"name":"get_time","arguments":"{}"}}],"reasoning":"OpenRouter's field."},
{"role":"tool","tool_call_id":"call_3","content":"12:02"}]}`
func chatProvider(t *testing.T, up string, p provider.Provider) {
t.Helper()
p.Name, p.Key, p.Chat = p.ID, "k", up+"/v1"
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
// A Chat request relayed as it came to a DeepSeek model, here behind
// OpenCode Go whose host doesn't say DeepSeek, gives every tool call
// without reasoning_content one, so DeepSeek takes it (#1462). The
// thinking a turn has keeps it, one sent in another field goes there too,
// and every other message goes as it was sent.
func TestChatToolTurnWithoutReasoningRelayed(t *testing.T) {
f := &fake{t: t, reply: chatWholeOK, ctype: "application/json", refuse: deepseekRefuseToolTurns}
up := setup(t, provider.Chat, f)
chatProvider(t, up.URL, provider.Provider{ID: "go", Models: []string{"deepseek-v4.1-flash", "glm-5.2"}})
srv := New()
for _, model := range []string{"go/deepseek-v4.1-flash", "group/flash"} {
if model == "group/flash" {
if err := provider.SaveGroup(provider.Group{ID: "flash", Name: "Flash", Members: []string{"go/deepseek-v4.1-flash"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
}
f.calls = 0
code, body := postTo(t, srv, "/v1/chat/completions", strings.Replace(toolTurnsNoReasoning, "%s", model, 1))
if code != 200 || f.calls != 1 {
t.Fatalf("%s: %d after %d calls: %s", model, code, f.calls, body)
}
ms := sentMessages(t, f.got)
sameJSON(t, ms[1], `{"role":"assistant","content":"Hello!"}`)
sameJSON(t, ms[3], `{"role":"assistant","content":null,"tool_calls":[`+timeCall+`],"reasoning_content":" "}`)
sameJSON(t, ms[5], `{"role":"assistant","content":null,"tool_calls":[{"id":"call_2","type":"function","function":{"name":"get_time","arguments":"{}"}}],"reasoning_content":"Once more."}`)
sameJSON(t, ms[7], `{"role":"assistant","content":null,"tool_calls":[{"id":"call_3","type":"function","function":{"name":"get_time","arguments":"{}"}}],"reasoning":"OpenRouter's field.","reasoning_content":"OpenRouter's field."}`)
sameJSON(t, ms[4], `{"role":"tool","tool_call_id":"call_1","content":"12:00"}`)
}
// another model on the same relay is sent the messages as they came
f.refuse = nil
code, body := postTo(t, srv, "/v1/chat/completions", strings.Replace(toolTurnsNoReasoning, "%s", "go/glm-5.2", 1))
if code != 200 {
t.Fatalf("glm: %d: %s", code, body)
}
if strings.Contains(string(f.got), `"reasoning_content":" "`) || strings.Count(string(f.got), "reasoning_content") != 1 {
t.Errorf("glm was given reasoning: %s", f.got)
}
}
// Mistral, which turns reasoning_content away (#494), is given none for a
// tool call without thinking.
func TestChatToolTurnWithoutReasoningNotForMistral(t *testing.T) {
f := &fake{t: t, reply: chatWholeOK, ctype: "application/json", refuse: mistralRefuseMessages}
up := setup(t, provider.Chat, f)
chatProvider(t, up.URL, provider.Provider{ID: "mistral", Preset: "mistral", Models: []string{"mistral-medium-3.5"}})
code, body := postTo(t, New(), "/v1/chat/completions", `{"model":"mistral/mistral-medium-3.5","stream":false,"messages":[
{"role":"user","content":"what time is it?"},
{"role":"assistant","content":null,"tool_calls":[`+timeCall+`]},
{"role":"tool","tool_call_id":"call_1","content":"12:00"}]}`)
if code != 200 || f.calls != 1 {
t.Fatalf("%d after %d calls: %s", code, f.calls, body)
}
sameJSON(t, sentMessages(t, f.got)[1], `{"role":"assistant","content":null,"tool_calls":[`+timeCall+`]}`)
}
// Translated to Chat (Codex on Responses, here a turn whose call came with
// no reasoning item: another model's), a DeepSeek model's tool call goes
// with reasoning_content all the same (#1462); a reply with no tool calls
// goes without.
func TestTranslatedToolTurnWithoutReasoning(t *testing.T) {
f := &fake{t: t, refuse: deepseekRefuseToolTurns, reply: `data: {"id":"c1","object":"chat.completion.chunk","model":"m1","choices":[{"index":0,"delta":{"role":"assistant","content":"It is 12:00."},"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"completion_tokens":2}}` + "\n\ndata: [DONE]\n\n"}
up := setup(t, provider.Chat, f)
chatProvider(t, up.URL, provider.Provider{ID: "ds", Models: []string{"deepseek-v4.1-flash"}})
code, body := postTo(t, New(), "/v1/responses", `{"model":"ds/deepseek-v4.1-flash","stream":false,"tools":[{"type":"function","name":"get_time","parameters":{"type":"object","properties":{}}}],"input":[
{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]},
{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Hello!"}]},
{"type":"message","role":"user","content":[{"type":"input_text","text":"what time is it?"}]},
{"type":"function_call","call_id":"call_1","name":"get_time","arguments":"{}"},
{"type":"function_call_output","call_id":"call_1","output":"12:00"}]}`)
if code != 200 || f.calls != 1 {
t.Fatalf("%d after %d calls: %s", code, f.calls, body)
}
ms := sentMessages(t, f.got)
var reply, call map[string]any
for _, raw := range ms {
var m map[string]any
json.Unmarshal(raw, &m)
if m["role"] != "assistant" {
continue
}
if m["tool_calls"] != nil {
call = m
} else {
reply = m
}
}
if call == nil || call["reasoning_content"] != " " {
t.Errorf("the tool call went without reasoning: %s", f.got)
}
if _, ok := reply["reasoning_content"]; reply == nil || ok {
t.Errorf("the reply was given reasoning: %s", f.got)
}
}