diff --git a/internal/gateway/fallback.go b/internal/gateway/fallback.go index 37ac4ddd3..a633ba99c 100644 --- a/internal/gateway/fallback.go +++ b/internal/gateway/fallback.go @@ -567,6 +567,9 @@ type holdWriter struct { // thinking: the reply has reasoned but said nothing yet, which a // refusal may still end: held longer (holdThinking) thinking bool + // thinkingShown: the model's vendor never refuses after reasoning + // (refusesAfterThinking), so its reasoning goes through as it comes + thinkingShown bool ended bool // the stream's last event was written: the reply is whole tail []byte // the end of the last write, for a marker split across two @@ -674,6 +677,23 @@ const holdBuffered = 4 * time.Minute // reasoning with it, as soon as it does. const holdThinking = 4 * time.Minute +// refusesAfterThinking tells whether a model's vendor may end a reply that +// has only reasoned with its safety filter's refusal: Claude's stop_reason +// refusal, OpenAI's content_filter or bio_policy, Gemini's SAFETY (#248). +// Anybody else's reasoning — GLM's, DeepSeek's, Kimi's… — is shown as it +// comes: held, GLM on a ZCode account through a group showed Claude Code +// its thinking only once the text began, all at once (悠悠哥 on Discord). +func refusesAfterThinking(model string) bool { + if modelFamily(model) != "" { + return true + } + m := strings.ToLower(model) + if i := strings.LastIndex(m, "/"); i >= 0 { + m = m[i+1:] + } + return strings.HasPrefix(m, "gemini") +} + // scan reads the held stream's events so far: an error before any content // fails it; content, or waiting too long for it, lets it through. func (h *holdWriter) scan() { @@ -691,6 +711,10 @@ func (h *holdWriter) scan() { h.buffered = true continue case eventThinking: + if h.thinkingShown { + h.flow() + return + } h.thinking = true continue case eventError: diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 994615405..59eb53f28 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -1048,6 +1048,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // the last one's failure is held too when an earlier one failed, // for its allowance running out to be told as that one's error hw := newHoldWriter(w, !last || again < lastRetries || other != nil) + hw.thinkingShown = !refusesAfterThinking(c.model) call.Provider, call.To, call.Usage = c.p.ID, "", Usage{} where = c.p.Where() providerKeyID, providerKeyName = "", "" diff --git a/internal/gateway/refusal_thinking_test.go b/internal/gateway/refusal_thinking_test.go index 873ef4231..5b5ef1756 100644 --- a/internal/gateway/refusal_thinking_test.go +++ b/internal/gateway/refusal_thinking_test.go @@ -57,20 +57,29 @@ func TestRefusalAfterThinkingFailsOver(t *testing.T) { reply string path string ask string + model string // a model whose vendor refuses after thinking }{ - {"claude to claude code", func(t *testing.T, s *scripted) { scriptedOn(t, "a", provider.Anthropic, s) }, anthropicThoughtThenRefused, "/v1/messages", claudeCodeAsk}, - {"claude to codex", func(t *testing.T, s *scripted) { scriptedOn(t, "a", provider.Anthropic, s) }, anthropicThoughtThenRefused, "/v1/responses", codexAsk}, - {"chat to claude code", func(t *testing.T, s *scripted) { scriptedOn(t, "a", provider.Chat, s) }, chatThought, "/v1/messages", claudeCodeAsk}, - {"responses to codex", func(t *testing.T, s *scripted) { responsesOn(t, "a", s) }, responsesThought, "/v1/responses", codexAsk}, - {"responses to claude code", func(t *testing.T, s *scripted) { responsesOn(t, "a", s) }, responsesThought, "/v1/messages", claudeCodeAsk}, + {"claude to claude code", func(t *testing.T, s *scripted) { scriptedOn(t, "a", provider.Anthropic, s) }, anthropicThoughtThenRefused, "/v1/messages", claudeCodeAsk, "claude-opus-5-5"}, + {"claude to codex", func(t *testing.T, s *scripted) { scriptedOn(t, "a", provider.Anthropic, s) }, anthropicThoughtThenRefused, "/v1/responses", codexAsk, "claude-opus-5-5"}, + {"chat to claude code", func(t *testing.T, s *scripted) { scriptedOn(t, "a", provider.Chat, s) }, chatThought, "/v1/messages", claudeCodeAsk, "gpt-5.2"}, + {"responses to codex", func(t *testing.T, s *scripted) { responsesOn(t, "a", s) }, responsesThought, "/v1/responses", codexAsk, "gpt-5.2"}, + {"responses to claude code", func(t *testing.T, s *scripted) { responsesOn(t, "a", s) }, responsesThought, "/v1/messages", claudeCodeAsk, "gpt-5.2"}, } { t.Run(x.name, func(t *testing.T) { fresh(t) a := &scripted{replies: []reply{{200, "text/event-stream", x.reply}}} b := &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}} x.setup(t, a) + pa, err := provider.Find("a") + if err != nil { + t.Fatal(err) + } + pa.Models = []string{x.model} + if err := provider.Save(*pa); err != nil { + t.Fatal(err) + } scriptedOn(t, "b", provider.Anthropic, b) - refusalGroup(t, "a/m", "b/m") + refusalGroup(t, "a/"+x.model, "b/m") s := New() code, body := sendTo(s, x.path, x.ask) if code != 200 || !strings.Contains(body, "from b") || strings.Contains(body, `"refusal"`) || strings.Contains(body, "Planning") || diff --git a/internal/gateway/thinking_streams_test.go b/internal/gateway/thinking_streams_test.go new file mode 100644 index 000000000..ad5940b21 --- /dev/null +++ b/internal/gateway/thinking_streams_test.go @@ -0,0 +1,140 @@ +package gateway + +import ( + "bufio" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/yetone/magpie/internal/provider" +) + +// slowThinker streams its reasoning a piece at a time, as GLM on ZCode's +// Start Plan does, and notes when it has said its last. +type slowThinker struct { + events []string + gap time.Duration + done chan time.Time +} + +func (s *slowThinker) ServeHTTP(w http.ResponseWriter, r *http.Request) { + io.ReadAll(r.Body) + w.Header().Set("Content-Type", "text/event-stream") + w.WriteHeader(200) + f := w.(http.Flusher) + for _, ev := range s.events { + io.WriteString(w, ev+"\n\n") + f.Flush() + time.Sleep(s.gap) + } + s.done <- time.Now() +} + +var glmThinksSlowly = []string{ + `event: message_start` + "\n" + `data: {"type":"message_start","message":{"id":"m1","role":"assistant","model":"glm-5.1","content":[]}}`, + `event: content_block_start` + "\n" + `data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}`, + `event: content_block_delta` + "\n" + `data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"first thought"}}`, + `event: content_block_delta` + "\n" + `data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":" second thought"}}`, + `event: content_block_delta` + "\n" + `data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":" third thought"}}`, + `event: content_block_stop` + "\n" + `data: {"type":"content_block_stop","index":0}`, + `event: content_block_start` + "\n" + `data: {"type":"content_block_start","index":1,"content_block":{"type":"text","text":""}}`, + `event: content_block_delta` + "\n" + `data: {"type":"content_block_delta","index":1,"delta":{"type":"text_delta","text":"done"}}`, + `event: content_block_stop` + "\n" + `data: {"type":"content_block_stop","index":1}`, + `event: message_delta` + "\n" + `data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":9}}`, + `event: message_stop` + "\n" + `data: {"type":"message_stop"}`, +} + +var glmChatThinksSlowly = []string{ + `data: {"id":"c1","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"first thought"}}]}`, + `data: {"id":"c1","choices":[{"index":0,"delta":{"reasoning_content":" second thought"}}]}`, + `data: {"id":"c1","choices":[{"index":0,"delta":{"reasoning_content":" third thought"}}]}`, + `data: {"id":"c1","choices":[{"index":0,"delta":{"content":"done"}}]}`, + `data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`, + `data: [DONE]`, +} + +// A model whose vendor never refuses after reasoning has its thinking +// streamed as it comes, through a group as directly: GLM on a ZCode +// account behind a route group showed Claude Code its thinking only once +// the text began (悠悠哥 on Discord), held there for a refusal (#248) that +// only Claude's, OpenAI's and Gemini's safety filters send. +func TestThinkingStreamsThroughGroup(t *testing.T) { + for _, x := range []struct { + name string + proto provider.Protocol + events []string + }{ + {"anthropic", provider.Anthropic, glmThinksSlowly}, + {"chat", provider.Chat, glmChatThinksSlowly}, + } { + t.Run(x.name, func(t *testing.T) { + fresh(t) + up := &slowThinker{events: x.events, gap: 200 * time.Millisecond, done: make(chan time.Time, 1)} + vendor := httptest.NewServer(up) + t.Cleanup(vendor.Close) + p := provider.Provider{ID: "zc", Name: "ZCode", Key: "k", Models: []string{"GLM-5.1"}} + if x.proto == provider.Anthropic { + p.Anthropic = vendor.URL + } else { + p.Chat = vendor.URL + "/v1" + } + if err := provider.Save(p); err != nil { + t.Fatal(err) + } + scriptedOn(t, "b", provider.Anthropic, &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}) + refusalGroup(t, "zc/GLM-5.1", "b/m") + gw := httptest.NewServer(New().Handler()) + t.Cleanup(gw.Close) + + res, err := http.Post(gw.URL+"/v1/messages", "application/json", strings.NewReader(claudeCodeAsk)) + if err != nil { + t.Fatal(err) + } + defer res.Body.Close() + var firstThought, firstText time.Time + rd := bufio.NewReader(res.Body) + for { + ln, err := rd.ReadString('\n') + if firstThought.IsZero() && strings.Contains(ln, `"thinking_delta"`) { + firstThought = time.Now() + } + if firstText.IsZero() && strings.Contains(ln, `"text_delta"`) { + firstText = time.Now() + } + if err != nil { + break + } + } + var ended time.Time + select { + case ended = <-up.done: + case <-time.After(5 * time.Second): + t.Fatal("the vendor never finished") + } + if firstThought.IsZero() || firstText.IsZero() { + t.Fatal("no thinking or no text reached the client") + } + // three thoughts 200ms apart come before the text + if firstText.Sub(firstThought) < 300*time.Millisecond || ended.Sub(firstThought) < time.Second { + t.Fatalf("thinking held: first thought %v before the text, %v before the vendor finished", firstText.Sub(firstThought), ended.Sub(firstThought)) + } + }) + } +} + +// Claude's reasoning is still held for a refusal after it (#248), and so +// are OpenAI's and Gemini's; anybody else's is let through. +func TestThinkingHeldFor(t *testing.T) { + for model, held := range map[string]bool{ + "claude-opus-5-5": true, "anthropic/claude-sonnet-4.5": true, "gpt-5.2-codex": true, "o3": true, + "gemini-3-pro-preview": true, "models/gemini-2.5-flash": true, + "GLM-5.1": false, "glm-4.6": false, "deepseek-v4": false, "kimi-k2.5": false, "MiniMax-M2": false, + } { + if got := refusesAfterThinking(model); got != held { + t.Errorf("%s: held %v, want %v", model, got, held) + } + } +}