feat(agent): enhance event routing for reasoning and answers in streaming
- Introduced new tests to ensure correct routing of reasoning and answer content during streaming, addressing UX issues where answers appeared under the Thinking phase before transitioning. - Implemented a ThinkStreamSplitter to separate inline reasoning from answer text, allowing for real-time updates to the UI without duplication. - Updated the event emission logic to prevent re-emission of answers that were already streamed live, ensuring a seamless user experience. - Enhanced the ChatResponse struct to include flags for streamed answers, improving the handling of final answer events. - Added comprehensive unit tests for the ThinkStreamSplitter to validate its functionality across various scenarios.
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package tools
|
||||
|
||||
import "strings"
|
||||
|
||||
// thinkOpenTag / thinkCloseTag are the inline reasoning markers some models
|
||||
// (DeepSeek, Qwen, …) embed directly in their `content` field instead of the
|
||||
// separate `reasoning_content` channel.
|
||||
const (
|
||||
thinkOpenTag = "<think>"
|
||||
thinkCloseTag = "</think>"
|
||||
)
|
||||
|
||||
// ThinkStreamSplitter incrementally separates inline <think>…</think> reasoning
|
||||
// from user-facing answer text as content arrives chunk-by-chunk. It is the
|
||||
// streaming counterpart to StripThinkBlocks: where StripThinkBlocks operates on
|
||||
// a fully-accumulated string, this splitter routes each chunk live so the
|
||||
// thinking portion can stream into the "thought" UI area and the answer portion
|
||||
// into the "final answer" area without waiting for the whole response.
|
||||
//
|
||||
// Tag boundaries that straddle two chunks (e.g. "<thi" at the end of one chunk
|
||||
// and "nk>" at the start of the next) are handled by buffering the trailing
|
||||
// bytes that could still become a tag prefix until the next Feed call. Call
|
||||
// Flush at end-of-stream to drain any buffered remainder.
|
||||
//
|
||||
// The splitter is NOT safe for concurrent use; create one per stream.
|
||||
type ThinkStreamSplitter struct {
|
||||
inThink bool
|
||||
// pending holds trailing bytes from a previous Feed that may be the start
|
||||
// of a (possibly split) <think>/</think> tag and therefore cannot yet be
|
||||
// classified as think or answer text.
|
||||
pending string
|
||||
}
|
||||
|
||||
// NewThinkStreamSplitter returns a splitter ready to receive content chunks.
|
||||
func NewThinkStreamSplitter() *ThinkStreamSplitter {
|
||||
return &ThinkStreamSplitter{}
|
||||
}
|
||||
|
||||
// Feed consumes one content chunk and returns the portions that are now
|
||||
// unambiguously thinking text and answer text respectively. Either return value
|
||||
// may be empty. Bytes that could still be part of a tag spanning into the next
|
||||
// chunk are buffered internally and surface on a later Feed or on Flush.
|
||||
func (sp *ThinkStreamSplitter) Feed(s string) (thinkOut, answerOut string) {
|
||||
if s == "" {
|
||||
return "", ""
|
||||
}
|
||||
sp.pending += s
|
||||
|
||||
var think, answer strings.Builder
|
||||
for {
|
||||
if sp.inThink {
|
||||
if idx := strings.Index(sp.pending, thinkCloseTag); idx >= 0 {
|
||||
think.WriteString(sp.pending[:idx])
|
||||
sp.pending = sp.pending[idx+len(thinkCloseTag):]
|
||||
sp.inThink = false
|
||||
continue
|
||||
}
|
||||
safe, hold := holdBackPartialTag(sp.pending, thinkCloseTag)
|
||||
think.WriteString(safe)
|
||||
sp.pending = hold
|
||||
return think.String(), answer.String()
|
||||
}
|
||||
|
||||
if idx := strings.Index(sp.pending, thinkOpenTag); idx >= 0 {
|
||||
answer.WriteString(sp.pending[:idx])
|
||||
sp.pending = sp.pending[idx+len(thinkOpenTag):]
|
||||
sp.inThink = true
|
||||
continue
|
||||
}
|
||||
safe, hold := holdBackPartialTag(sp.pending, thinkOpenTag)
|
||||
answer.WriteString(safe)
|
||||
sp.pending = hold
|
||||
return think.String(), answer.String()
|
||||
}
|
||||
}
|
||||
|
||||
// Flush drains any buffered remainder at end-of-stream. An unterminated <think>
|
||||
// block is treated as thinking text; anything else is answer text.
|
||||
func (sp *ThinkStreamSplitter) Flush() (thinkOut, answerOut string) {
|
||||
rest := sp.pending
|
||||
sp.pending = ""
|
||||
if rest == "" {
|
||||
return "", ""
|
||||
}
|
||||
if sp.inThink {
|
||||
return rest, ""
|
||||
}
|
||||
return "", rest
|
||||
}
|
||||
|
||||
// holdBackPartialTag splits s into the part that is safe to emit now and a
|
||||
// trailing suffix that is a proper prefix of tag (and so might complete into a
|
||||
// real tag on the next chunk). When s ends with no such prefix, the whole
|
||||
// string is safe and hold is empty.
|
||||
func holdBackPartialTag(s, tag string) (safe, hold string) {
|
||||
maxK := len(tag) - 1
|
||||
if maxK > len(s) {
|
||||
maxK = len(s)
|
||||
}
|
||||
for k := maxK; k >= 1; k-- {
|
||||
if strings.HasSuffix(s, tag[:k]) {
|
||||
return s[:len(s)-k], s[len(s)-k:]
|
||||
}
|
||||
}
|
||||
return s, ""
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package tools
|
||||
|
||||
import "testing"
|
||||
|
||||
// feedAll runs the splitter over a sequence of chunks and concatenates the
|
||||
// think / answer outputs (including the final Flush), mirroring how the agent
|
||||
// stream router consumes it.
|
||||
func feedAll(sp *ThinkStreamSplitter, chunks []string) (think, answer string) {
|
||||
for _, c := range chunks {
|
||||
tk, ans := sp.Feed(c)
|
||||
think += tk
|
||||
answer += ans
|
||||
}
|
||||
tk, ans := sp.Flush()
|
||||
think += tk
|
||||
answer += ans
|
||||
return think, answer
|
||||
}
|
||||
|
||||
func TestThinkStreamSplitter(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
chunks []string
|
||||
wantThink string
|
||||
wantAnswer string
|
||||
}{
|
||||
{
|
||||
name: "no think tags - all answer",
|
||||
chunks: []string{"Hello ", "world"},
|
||||
wantThink: "",
|
||||
wantAnswer: "Hello world",
|
||||
},
|
||||
{
|
||||
name: "single think block then answer",
|
||||
chunks: []string{"<think>reasoning</think>The answer is 42."},
|
||||
wantThink: "reasoning",
|
||||
wantAnswer: "The answer is 42.",
|
||||
},
|
||||
{
|
||||
name: "open tag split across chunks",
|
||||
chunks: []string{"<thi", "nk>secret</think>visible"},
|
||||
wantThink: "secret",
|
||||
wantAnswer: "visible",
|
||||
},
|
||||
{
|
||||
name: "close tag split across chunks",
|
||||
chunks: []string{"<think>think part</thi", "nk>answer part"},
|
||||
wantThink: "think part",
|
||||
wantAnswer: "answer part",
|
||||
},
|
||||
{
|
||||
name: "think content streamed across multiple chunks",
|
||||
chunks: []string{"<think>a", "b", "c</think>", "done"},
|
||||
wantThink: "abc",
|
||||
wantAnswer: "done",
|
||||
},
|
||||
{
|
||||
name: "answer before think block",
|
||||
chunks: []string{"prefix <think>mid</think> suffix"},
|
||||
wantThink: "mid",
|
||||
wantAnswer: "prefix suffix",
|
||||
},
|
||||
{
|
||||
name: "two think blocks",
|
||||
chunks: []string{"<think>one</think>A<think>two</think>B"},
|
||||
wantThink: "onetwo",
|
||||
wantAnswer: "AB",
|
||||
},
|
||||
{
|
||||
name: "unterminated think treated as think on flush",
|
||||
chunks: []string{"<think>still thinking"},
|
||||
wantThink: "still thinking",
|
||||
wantAnswer: "",
|
||||
},
|
||||
{
|
||||
name: "literal less-than in answer is preserved",
|
||||
chunks: []string{"if a < ", "b then"},
|
||||
wantThink: "",
|
||||
wantAnswer: "if a < b then",
|
||||
},
|
||||
{
|
||||
name: "empty chunks are no-ops",
|
||||
chunks: []string{"", "answer", ""},
|
||||
wantThink: "",
|
||||
wantAnswer: "answer",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
sp := NewThinkStreamSplitter()
|
||||
think, answer := feedAll(sp, tt.chunks)
|
||||
if think != tt.wantThink {
|
||||
t.Errorf("think = %q, want %q", think, tt.wantThink)
|
||||
}
|
||||
if answer != tt.wantAnswer {
|
||||
t.Errorf("answer = %q, want %q", answer, tt.wantAnswer)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user