gateway: a Chat reply's leading <thinking> block is read as thinking

A Chat reply may open its reasoning with <thinking>…</thinking> in the
text. thoughtTags held only <thought> and <think>, 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 <thinking in the answer's text, 44 of
them at offset 0 (neighbads).

<thinking> is added beside the two, closed by </thinking>. It shares no
whole prefix with <think> (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 <thinking> 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 <think>, so a <thinking>- or
<thought>-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 <thinking>
cases of TestChatThoughtTags FAIL and TestAfterThoughtCloseInCode FAILs
on the <thinking> and <thought> 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 <ggbdpq@gmail.com>
This commit is contained in:
yetone
2026-11-06 10:04:22 +08:00
co-authored by neighbads ggbdpq
parent cbd379e1b7
commit e1d4dca967
3 changed files with 30 additions and 11 deletions
@@ -103,6 +103,10 @@ func TestChatThoughtTags(t *testing.T) {
think, text string
}{
{"whole", []string{"<thought>Greeting.</thought>Hello"}, "Greeting.", "Hello"},
{"<thinking> whole", []string{"<thinking>Let me think</thinking>Hi"}, "Let me think", "Hi"},
{"<thinking> split tags", []string{"<thin", "king>Let me", " think</thin", "king>Hi"}, "Let me think", "Hi"},
{"<thinking> the open tag a chunk alone", []string{"<thinking>", "Let me think", "</thinking>", "Hi"}, "Let me think", "Hi"},
{"<thinking> never closed", []string{"<thinking>half", " a thought</thi"}, "half a thought</thi", ""},
{"split tags", []string{"<tho", "ught>Let me", " think</tho", "ught>\n\nHello", " there"}, "Let me think", "Hello there"},
{"one byte a chunk", strings.Split("<thought>ab</thought>cd", ""), "ab", "cd"},
{"leading newline", []string{"\n", "<thought>x</thought>", "y"}, "x", "y"},
+17 -11
View File
@@ -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{"<thought>": "</thought>", "<think>": "</think>"}
var thoughtTags = map[string]string{"<thought>": "</thought>", "<thinking>": "</thinking>", "<think>": "</think>"}
// text sends a piece of the reply's text, a leading <thought> or <think>
// block of it as thinking.
// text sends a piece of the reply's text, a leading <thought>, <thinking> or
// <think> 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 <think>…</think> 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), "<think>")
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, "</think>", true); i >= 0 {
return strings.TrimSpace(rest[i+len("</think>"):])
}
return t
}
+9
View File
@@ -212,4 +212,13 @@ func TestAfterThoughtCloseInCode(t *testing.T) {
if got := afterThought("\r\n<think>a</think>\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{"<thinking>a</thinking>\nb", "<thought>a</thought>b", " <thinking>\na\n</thinking>\n\nb"} {
if got := afterThought(c); got != "b" {
t.Errorf("afterThought(%q) = %q, want b", c, got)
}
}
if got := afterThought("<thinking>never closed"); got != "<thinking>never closed" {
t.Errorf("an unclosed <thinking> = %q, want it as it was", got)
}
}