thinkingOffUnlessAsked turns thinking off on a Messages request for a non-Claude model that doesn't mention it. Kimi Code (China)'s K3.1 (k3.1-pro, k3.1-pro-highspeed) refuses that with 400, "K3.1 Pro does not support disabled thinking mode. Please enable thinking and try again, or choose another model.", in a wording neither alwaysThinks, mandatoryReasoning nor thinkingOffRefused matched, so the 400 went back to the client on every request that carried no thinking — ZCode's, which asks for a level as output_config.effort and sends no thinking — and the session could not start. withoutThinkingOff would have handled it; only the match missed, as in #1141. alwaysThinks now matches Kimi's literal sentence. Both retry sites use it: the relayed Messages path (withoutThinkingOff, gateway.go) and the translated Chat/Responses path (thinkingBackOn). Tests: TestAnthropicPassthroughKimiThinkingOffRefused (the reporter's error body: omitted thinking succeeds after one retry without thinking; adaptive goes once, as sent; Kimi's own max_tokens 400 and a near wording "tool use is not supported by this model" are not retried), and TestTranslatedThinkingOffRefused now runs Kimi's wording. With the regex change reverted both fail (400 handed back). Review and run record (landed by the maintainer's agent; gua2100 brought both fixes from Discord, rebased onto main): - External code, read line by line: no network, dependency, workflow or credential change; Codex base_instructions/identity untouched. - Base origin/maind99e213f5. Full checks on mote (macOS, Go 1.26.8, sandbox HOME) at 31f025634, whose tree is the one pushed (only messages reworded since): gofmt, go vet darwin, linux, windows, builds for all three: rc=0. go test -tags nogui -count=1 -timeout 45m ./...: 79 packages ok, internal/gateway ok (855s); two reds in untouched packages, both filed before: internal/plugin TestGatewayHookMixedPiPackage (#2865, "starting plugins: context deadline exceeded") and internal/update TestRelaunchOpenTriedAgain (#3881). Run alone at the same tree both pass (plugin 97s; update -count=3). - Broken by hand: the #3739 regex reverted, so both Kimi tests fail (400 handed back). The #2532 product files reverted (six files), so all three tests fail. The mergeTurns role guard removed fails (5 input messages, want 7). - Real binary on mote, sandbox HOME, a fake upstream that records requests: atd99e213f5, wrke1990's literal #2532 body sends Plan in instructions and Default as a user <system-reminder>; at HEAD both developer items keep their role and place. #3739's curl body (and zfsdream96's ZCode shape, output_config effort max, no thinking) is a 400 at d99e213f5; at HEAD it is a 200 after one retry without thinking, and the next request goes without disabled. - Rebased ontofa427216a(responses.go stream deltas, landed meanwhile; no conflict). At the rebased pushed tree: gofmt, go vet and go test -count=1 ./internal/gateway ok (580s) on mote. The full suite was not rerun after that rebase. - Not covered: the search reroute's re-parse when a fallback changes whether the target is Responses (breaking it leaves the tests green; no route found that reaches it). Not tried: the real Kimi Code endpoint and its kimi-code-cn preset (a custom Anthropic provider with Kimi's literal 400 was used), a real Codex session against a paid upstream, Windows/Linux runtime. #3739's second symptom (k3 losing thinking when output_config.effort is asked) is not addressed. Generated-by: GLM-5.3-Flash (ZCode)
155 lines
7.0 KiB
Go
155 lines
7.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
var anthropicHi = sse(
|
|
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"msg_1","model":"m1","usage":{"input_tokens":7}}}`,
|
|
`data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
|
|
`data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"hi"}}`,
|
|
`data: {"type":"content_block_stop","index":0}`,
|
|
`data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}`,
|
|
`data: {"type":"message_stop"}`)
|
|
|
|
var chatHi = sse(
|
|
`data: {"id":"c1","model":"m1","choices":[{"delta":{"role":"assistant","content":"hi"}}]}`,
|
|
`data: {"id":"c1","choices":[{"delta":{},"finish_reason":"stop"}]}`,
|
|
`data: [DONE]`)
|
|
|
|
// responsesHi is the same reply from a Responses API, which reads its own
|
|
// events only: a Chat stream from it is no reply (#1933).
|
|
var responsesHi = sse(
|
|
`data: {"type":"response.output_text.delta","item_id":"m1","output_index":0,"content_index":0,"delta":"hi"}`,
|
|
`data: {"type":"response.completed","response":{"id":"r1","status":"completed","output":[],"usage":{"input_tokens":7,"output_tokens":1}}}`)
|
|
|
|
// A user-added provider whose model's levels no one lists (#1636,
|
|
// jswysnemc): a client asking reasoning off or minimal on another API
|
|
// than the provider's went at another level, while the request log and
|
|
// Routing said the one asked. Reasoning off went to an Anthropic API as
|
|
// thinking at a 4096-token budget; it goes as thinking off now. Where the
|
|
// level asked can't go as it is — none and minimal read as low for a
|
|
// model not known to stop (effortOf) — the route keeps what was asked
|
|
// and the try and the log what went.
|
|
func TestTranslatedEffortLoggedAsSent(t *testing.T) {
|
|
for _, c := range []struct {
|
|
up provider.Protocol
|
|
path, body string
|
|
asked, went, thing string // thing: what the upstream body asks, as "field=value"
|
|
}{
|
|
{provider.Anthropic, "/v1/responses", `{"model":"m1","stream":true,"input":"hi","reasoning":{"effort":"none"}}`, "none", "none", "thinking=disabled"},
|
|
{provider.Anthropic, "/v1/chat/completions", `{"model":"m1","stream":true,"messages":[{"role":"user","content":"hi"}],"reasoning_effort":"none"}`, "none", "none", "thinking=disabled"},
|
|
{provider.Anthropic, "/v1/responses", `{"model":"m1","stream":true,"input":"hi","reasoning":{"effort":"high"}}`, "high", "high", "thinking=enabled"},
|
|
{provider.Chat, "/v1/responses", `{"model":"m1","stream":true,"input":"hi","reasoning":{"effort":"minimal"}}`, "minimal", "low", "reasoning_effort=low"},
|
|
{provider.Chat, "/v1/responses", `{"model":"m1","stream":true,"input":"hi","reasoning":{"effort":"none"}}`, "none", "low", "reasoning_effort=low"},
|
|
{provider.Chat, "/v1/responses", `{"model":"m1","stream":true,"input":"hi","reasoning":{"effort":"xhigh"}}`, "xhigh", "xhigh", "reasoning_effort=xhigh"},
|
|
{provider.Responses, "/v1/chat/completions", `{"model":"m1","stream":true,"messages":[{"role":"user","content":"hi"}],"reasoning_effort":"none"}`, "none", "low", "reasoning.effort=low"},
|
|
// the same API: it goes as it came, and the log says so
|
|
{provider.Chat, "/v1/chat/completions", `{"model":"m1","stream":true,"messages":[{"role":"user","content":"hi"}],"reasoning_effort":"none"}`, "none", "none", "reasoning_effort=none"},
|
|
} {
|
|
f := &fake{t: t, reply: chatHi}
|
|
switch c.up {
|
|
case provider.Anthropic:
|
|
f.reply = anthropicHi
|
|
case provider.Responses:
|
|
f.reply = responsesHi
|
|
}
|
|
setup(t, c.up, f)
|
|
s := New()
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", c.path, strings.NewReader(c.body)))
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%s: %d %s", c.body, rec.Code, rec.Body)
|
|
}
|
|
var got map[string]any
|
|
json.Unmarshal(f.got, &got)
|
|
field, want, _ := strings.Cut(c.thing, "=")
|
|
var v any
|
|
switch field {
|
|
case "thinking":
|
|
m, _ := got["thinking"].(map[string]any)
|
|
v = m["type"]
|
|
case "reasoning.effort":
|
|
m, _ := got["reasoning"].(map[string]any)
|
|
v = m["effort"]
|
|
default:
|
|
v = got[field]
|
|
}
|
|
if v != want {
|
|
t.Errorf("%s to %s: sent %s", c.body, c.up, f.got)
|
|
}
|
|
r := lastRoute(s)
|
|
if r.Effort != c.asked || len(r.Tries) != 1 || r.Tries[0].Effort != c.went {
|
|
t.Errorf("%s to %s: route at %q, tries %+v; want asked %q, went %q", c.body, c.up, r.Effort, r.Tries, c.asked, c.went)
|
|
}
|
|
recs := usage.Load(time.Time{})
|
|
if len(recs) == 0 || recs[len(recs)-1].Effort != c.went {
|
|
t.Errorf("%s to %s: logged %+v, want %q", c.body, c.up, recs, c.went)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A vendor's model on its Anthropic API that won't stop thinking (Z.ai's
|
|
// GLM-5.3, #699) refuses the thinking off a Codex asking none is now sent
|
|
// (#1636): asked again thinking at its least, as before, and from then on
|
|
// asked so at once.
|
|
func TestTranslatedThinkingOffRefused(t *testing.T) {
|
|
for i, refusal := range []string{
|
|
`GLM-5.3 always engages in thinking and cannot be disabled`,
|
|
// MiniMax's and Volcengine Ark's wordings (#1141)
|
|
`invalid params, model \"MiniMax-M3.1-Flash-Preview\" requires adaptive thinking; thinking.type=\"disabled\" (including reasoning.effort=none) is not allowed (2013)`,
|
|
"thinking.type `disabled` is not supported by this model Request id: 0217",
|
|
// Kimi Code's K3.1 (ZCode sends no thinking at all, #3739)
|
|
`Kimi Code (China): K3.1 Pro does not support disabled thinking mode. Please enable thinking and try again, or choose another model.`,
|
|
// Sub2API's gpt-6.1-sol reads disabled as reasoning effort none (#1465)
|
|
`gpt-6.1-sol does not support reasoning effort \"none\"; use low, medium, high, xhigh or max`,
|
|
// #699's Chinese, escaped as \uXXXX the way a relay's
|
|
// json.dumps sends it (#1669)
|
|
`\u8be5\u6a21\u578b\u59cb\u7ec8\u652f\u6301\u601d\u8003\uff0c\u4e0d\u53ef\u5173\u95ed`,
|
|
} {
|
|
t.Run(fmt.Sprint(i), func(t *testing.T) { translatedThinkingOffRefused(t, refusal) })
|
|
}
|
|
}
|
|
|
|
func translatedThinkingOffRefused(t *testing.T, refusal string) {
|
|
var sent []string
|
|
f := &fake{t: t, reply: anthropicHi}
|
|
f.refuse = func(b []byte) (int, string) {
|
|
var v struct {
|
|
Thinking struct {
|
|
Type string `json:"type"`
|
|
} `json:"thinking"`
|
|
}
|
|
json.Unmarshal(b, &v)
|
|
sent = append(sent, v.Thinking.Type)
|
|
if v.Thinking.Type == "disabled" {
|
|
return 400, `{"type":"error","error":{"type":"invalid_request_error","message":"` + refusal + `"}}`
|
|
}
|
|
return 0, ""
|
|
}
|
|
setup(t, provider.Anthropic, f)
|
|
s := New()
|
|
for turn, want := range []string{"disabled,enabled", "enabled"} {
|
|
sent = nil
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(`{"model":"m1","stream":true,"input":"hi","reasoning":{"effort":"none"}}`)))
|
|
if rec.Code != 200 {
|
|
t.Fatalf("turn %d: %d %s", turn+1, rec.Code, rec.Body)
|
|
}
|
|
if strings.Join(sent, ",") != want {
|
|
t.Errorf("turn %d: thinking sent %v, want %s", turn+1, sent, want)
|
|
}
|
|
if r := lastRoute(s); len(r.Tries) != 1 || r.Tries[0].Effort != "low" {
|
|
t.Errorf("turn %d: tries %+v, want it logged at low", turn+1, r.Tries)
|
|
}
|
|
}
|
|
}
|