What changed: a Chat request to Kimi (isKimi: the Kimi platform's or a Kimi Code membership's host or preset) has each message's reasoning_details left out, its thinking put in reasoning_content when the message has none (reasoningDetailsAsContent). refusedInMessages also reads Kimi's own wording, "the <field> at position N", so another upstream that refuses the field that way (a relay in front of Kimi) is asked once more without it and marked; the retry and the remembered mark keep reasoning_details' thinking as reasoning_content too (withoutMessageReasoning). Why: Leo-origin's session was answered by MiMo through Command Code, whose reasoning_details entries carry a stream's index; pi sends them back, and Kimi Code answered 400 "the reasoning_details at position 2 entry 0 must not contain streaming index" on every turn. Probed against the real api.kimi.com/coding/v1 on 2026-10-12: Kimi refuses reasoning_details in every shape tried (with index: "must not contain streaming index"; reasoning.text, reasoning.summary, reasoning.encrypted, text, thinking without it: "has an invalid type"), and passes reasoning_content, reasoning and an empty list through validation. Run: TestKimiGetsReasoningDetailsAsContent and TestReasoningDetailsRefusedAtPosition (fake Kimi answering with the probed bytes) fail on the old gateway.go/chattidy.go: 400 "Kimi Code (China): the reasoning_details at position 1 entry 0 must not contain streaming index" after 1 call, the reporter's message, and "400 after 1 calls (want 2)". They pass with the change, as do TestMistral*, TestMessageReasoning* and the other *Reasoning* tests. Not verified: a real k3 reply through magpie; the owner's Kimi Code subscription has lapsed, so the real probe reached only validation (a valid request got 403 "does not have access"). A 400 magpie doesn't recognize still ends the request rather than failing over; that is unchanged.
97 lines
5.0 KiB
Go
97 lines
5.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// kimiRefuseDetails answers as api.kimi.com/coding/v1 did on 2026-10-12 a
|
|
// message with reasoning_details: an entry with a stream's index is
|
|
// turned away for it, any other entry (OpenRouter's reasoning.text,
|
|
// reasoning.summary, reasoning.encrypted, text, thinking) for its type.
|
|
// reasoning_content and an empty list are taken.
|
|
func kimiRefuseDetails(body []byte) (int, string) {
|
|
var q struct {
|
|
Messages []map[string]json.RawMessage `json:"messages"`
|
|
}
|
|
json.Unmarshal(body, &q)
|
|
for i, m := range q.Messages {
|
|
var details []map[string]any
|
|
json.Unmarshal(m["reasoning_details"], &details)
|
|
for j, d := range details {
|
|
why := "has an invalid type"
|
|
if _, ok := d["index"]; ok {
|
|
why = "must not contain streaming index"
|
|
}
|
|
return 400, fmt.Sprintf(`{"error":{"message":"the reasoning_details at position %d entry %d %s","type":"invalid_request_error"}}`, i, j, why)
|
|
}
|
|
}
|
|
return 0, ""
|
|
}
|
|
|
|
// #1765 (Leo-origin): a conversation MiMo answered first, through Command
|
|
// Code, has its thinking in reasoning_details with a stream's index, as pi
|
|
// keeps it; routed on to Kimi Code it was refused 400 on every turn. To
|
|
// Kimi it goes as reasoning_content in the first request; a turn with its
|
|
// own reasoning_content keeps that, and other messages keep their bytes.
|
|
func TestKimiGetsReasoningDetailsAsContent(t *testing.T) {
|
|
f := &fake{t: t, reply: chatWholeOK, ctype: "application/json", refuse: kimiRefuseDetails}
|
|
up := setup(t, provider.Chat, f)
|
|
if err := provider.Save(provider.Provider{ID: "kimi-code-cn", Name: "Kimi Code (China)", Key: "k", Models: []string{"k3"},
|
|
Preset: "kimi-code-cn", Chat: up.URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srv := New()
|
|
code, body := postTo(t, srv, "/v1/chat/completions", `{"model":"kimi-code-cn/k3","stream":false,"messages":[
|
|
{"role":"user","content":"Is 91 prime?"},
|
|
{"role":"assistant","content":"91 = 7 x 13.","reasoning_details":[{"type":"reasoning.text","text":"Check divisibility by 7: 7*13=91.","format":"unknown","index":0}]},
|
|
{"role":"user","content":"And 93?"},
|
|
{"role":"assistant","content":"93 = 3 x 31.","reasoning_content":"Digits sum to 12.","reasoning_details":[{"type":"reasoning.text","text":"Digits sum to 12.","format":"unknown","index":0}]},
|
|
{"role":"user","content":"And 97? One word."}]}`)
|
|
if code != 200 || f.calls != 1 {
|
|
t.Fatalf("%d after %d calls, sent %s: %s", code, f.calls, f.got, body)
|
|
}
|
|
ms := sentMessages(t, f.got)
|
|
if string(ms[0]) != `{"role":"user","content":"Is 91 prime?"}` || string(ms[4]) != `{"role":"user","content":"And 97? One word."}` {
|
|
t.Errorf("other messages changed: %s", f.got)
|
|
}
|
|
sameJSON(t, ms[1], `{"role":"assistant","content":"91 = 7 x 13.","reasoning_content":"Check divisibility by 7: 7*13=91."}`)
|
|
sameJSON(t, ms[3], `{"role":"assistant","content":"93 = 3 x 31.","reasoning_content":"Digits sum to 12."}`)
|
|
}
|
|
|
|
// An upstream that isn't known to be Kimi (a relay in front of it) and
|
|
// refuses reasoning_details the way Kimi does is asked once more with its
|
|
// thinking as reasoning_content, and from then on sent it so at once.
|
|
func TestReasoningDetailsRefusedAtPosition(t *testing.T) {
|
|
f := &fake{t: t, reply: chatWholeOK, ctype: "application/json", refuse: kimiRefuseDetails}
|
|
setup(t, provider.Chat, f)
|
|
srv := New()
|
|
req := `{"model":"m1","stream":false,"messages":[{"role":"user","content":"Read note.txt."},` +
|
|
`{"role":"assistant","content":null,"tool_calls":[{"id":"call_1","type":"function","function":{"name":"read","arguments":"{}"}}],"reasoning_details":[{"type":"reasoning.text","text":"Let me read it.","format":"unknown","index":0}]},` +
|
|
`{"role":"tool","tool_call_id":"call_1","content":"secret: magpie"}]}`
|
|
for _, want := range []int{2, 1} {
|
|
f.calls = 0
|
|
code, body := postTo(t, srv, "/v1/chat/completions", req)
|
|
if code != 200 || f.calls != want {
|
|
t.Fatalf("%d after %d calls (want %d), sent %s: %s", code, f.calls, want, f.got, body)
|
|
}
|
|
ms := sentMessages(t, f.got)
|
|
sameJSON(t, ms[1], `{"role":"assistant","content":null,"tool_calls":[{"id":"call_1","type":"function","function":{"name":"read","arguments":"{}"}}],"reasoning_content":"Let me read it."}`)
|
|
}
|
|
// a 400 that names reasoning_content otherwise (DeepSeek asking for it
|
|
// back) leaves it where it is
|
|
if got := refusedInMessages(400, []byte(`{"error":{"message":"The reasoning_content in the thinking mode must be passed back to the API.","type":"invalid_request_error"}}`), []byte(`{"messages":[{"reasoning_content":"x"}]}`)); len(got) != 0 {
|
|
t.Errorf("DeepSeek's ask read as a refusal of %v", got)
|
|
}
|
|
if got := refusedInMessages(400, []byte(`{"error":{"message":"the reasoning_details at position 2 entry 0 must not contain streaming index","type":"invalid_request_error"}}`), []byte(`{"messages":[{"role":"user"}]}`)); len(got) != 0 {
|
|
t.Errorf("a field the request doesn't carry read as refused: %v", got)
|
|
}
|
|
if !bytes.Contains(f.got, []byte(`"tool_call_id":"call_1"`)) {
|
|
t.Errorf("the tool result was lost: %s", f.got)
|
|
}
|
|
}
|