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)
+ }
}