From e1d4dca967b81745ea08d93e2028d47fbf2a8b64 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 6 Nov 2026 09:59:15 +0800 Subject: [PATCH] gateway: a Chat reply's leading block is read as thinking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A Chat reply may open its reasoning with … in the text. thoughtTags held only and , so neither tag matched: chatDecoder.text took the block for the answer's own text and no thinking block went out (#5106). In a recorded day on one OpenAI-compatible host, 47 of 2832 turns carried a literal is added beside the two, closed by . It shares no whole prefix with (they part at the seventh byte, 'i' against '>'), so the wait in text covers the shared lead and no lead can name two opening tags. markedThought reads the same map, so a Gemini-marked chunk opening with is thought's text too. afterThought, which strips a leading block from the titles, descriptions and predictions Codex asks for (codex_titles.go), now reads the same map instead of its own , so a - or -led reply no longer leaves the block in a title (added in the port; the patch left it as a sibling). Run (mote): go test -tags nogui -count=5 -run 'TestChatThoughtTags| TestMarkedThought|ThoughtTag|TestAfterThought|Title|Description' ./internal/gateway/ ok; gofmt -l clean; go vet -tags nogui ./internal/gateway/ clean. Broken by hand: chat.go put back to origin/main, the four cases of TestChatThoughtTags FAIL and TestAfterThoughtCloseInCode FAILs on the and leads. Not tried: a live model that sends the tag (the reporter's model sends it in about 1.7% of turns, not on demand). The plugins repo's think-tags middleware keeps its own list and is not changed here. The tag and the whole/split cases are neighbads' patch on their fork; ggbdpq added the open-tag-alone and never-closed cases. Fixes #5106 Generated-by: GLM-5.3-Flash (ZCode) Co-authored-by: neighbads <16126598+neighbads@users.noreply.github.com> Co-authored-by: ggbdpq --- internal/gateway/aistudio_thinking_test.go | 4 ++++ internal/gateway/chat.go | 28 +++++++++++++--------- internal/gateway/think_code_test.go | 9 +++++++ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/internal/gateway/aistudio_thinking_test.go b/internal/gateway/aistudio_thinking_test.go index 5841158ab..58cdc892e 100644 --- a/internal/gateway/aistudio_thinking_test.go +++ b/internal/gateway/aistudio_thinking_test.go @@ -103,6 +103,10 @@ func TestChatThoughtTags(t *testing.T) { think, text string }{ {"whole", []string{"Greeting.Hello"}, "Greeting.", "Hello"}, + {" whole", []string{"Let me thinkHi"}, "Let me think", "Hi"}, + {" split tags", []string{"Let me", " thinkHi"}, "Let me think", "Hi"}, + {" the open tag a chunk alone", []string{"", "Let me think", "", "Hi"}, "Let me think", "Hi"}, + {" never closed", []string{"half", " a thoughtLet me", " think\n\nHello", " there"}, "Let me think", "Hello there"}, {"one byte a chunk", strings.Split("abcd", ""), "ab", "cd"}, {"leading newline", []string{"\n", "x", "y"}, "x", "y"}, diff --git a/internal/gateway/chat.go b/internal/gateway/chat.go index b269ff43d..ade4a32d1 100644 --- a/internal/gateway/chat.go +++ b/internal/gateway/chat.go @@ -1126,10 +1126,10 @@ const leadSpace = " \t\r\n" // thoughtTags are the tags a leading block of thinking comes between in a // Chat reply's text, by its opening tag. -var thoughtTags = map[string]string{"": "", "": ""} +var thoughtTags = map[string]string{"": "", "": "", "": ""} -// text sends a piece of the reply's text, a leading or -// block of it as thinking. +// text sends a piece of the reply's text, a leading , or +// block of it as thinking. func (d *chatDecoder) text(s string, emit func(Event)) { if !d.past && !d.thought { d.lead += s @@ -1244,17 +1244,23 @@ func (m *mdCode) find(s, close string, last bool) (at, hold int) { return -1, 0 } -// afterThought is t without a leading … block, a whole -// reply's thinking where its API leaves it in the text; t when it has none. +// afterThought is t without a leading thinking block (any of thoughtTags, +// as a streamed reply's is read), a whole reply's thinking where its API +// leaves it in the text; t when it has none. No opening tag is a prefix of +// another's whole tag, so at most one matches. func afterThought(t string) string { - rest, ok := strings.CutPrefix(strings.TrimLeft(t, leadSpace), "") - if !ok { + lead := strings.TrimLeft(t, leadSpace) + for open, end := range thoughtTags { + rest, ok := strings.CutPrefix(lead, open) + if !ok { + continue + } + var m mdCode + if i, _ := m.find(rest, end, true); i >= 0 { + return strings.TrimSpace(rest[i+len(end):]) + } return t } - var m mdCode - if i, _ := m.find(rest, "", true); i >= 0 { - return strings.TrimSpace(rest[i+len(""):]) - } return t } diff --git a/internal/gateway/think_code_test.go b/internal/gateway/think_code_test.go index 348787d73..100b63ac5 100644 --- a/internal/gateway/think_code_test.go +++ b/internal/gateway/think_code_test.go @@ -212,4 +212,13 @@ func TestAfterThoughtCloseInCode(t *testing.T) { if got := afterThought("\r\na\r\nb"); got != "b" { t.Errorf("afterThought after a line end = %q", got) } + // the other tags a streamed reply's thinking is read between (#5106) + for _, c := range []string{"a\nb", "ab", " \na\n\n\nb"} { + if got := afterThought(c); got != "b" { + t.Errorf("afterThought(%q) = %q, want b", c, got) + } + } + if got := afterThought("never closed"); got != "never closed" { + t.Errorf("an unclosed = %q, want it as it was", got) + } }