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:
wizardchen
2026-06-03 22:14:35 +08:00
committed by lyingbug
parent 710d759b8e
commit 876fd0276f
6 changed files with 457 additions and 33 deletions
+27 -12
View File
@@ -132,18 +132,33 @@ func (e *AgentEngine) analyzeResponse(
"answer_len": len(response.Content),
})
// Emit answer as final answer event (thinking events were already streamed)
answerID := generateEventID("answer")
if response.Content != "" {
e.eventBus.Emit(ctx, event.Event{
ID: answerID,
Type: event.EventAgentFinalAnswer,
SessionID: sessionID,
Data: event.AgentFinalAnswerData{
Content: response.Content,
Done: false,
},
})
// Emit the final answer. The answer text reaches the UI by one of two
// paths:
// (a) Already streamed live during the think phase — the common case
// now that plain assistant content is routed straight to
// EventAgentFinalAnswer (response.AnswerStreamed). Re-emitting the
// full content here would render it twice and produce the
// end-of-stream "jump from Thinking to Answer" the user reported,
// so we only close the existing stream with a Done marker on the
// same event ID.
// (b) Not streamed live (e.g. the content only surfaced in the
// accumulated result) — emit the full content, then Done.
var answerID string
if response.AnswerStreamed && response.AnswerEventID != "" {
answerID = response.AnswerEventID
} else {
answerID = generateEventID("answer")
if response.Content != "" {
e.eventBus.Emit(ctx, event.Event{
ID: answerID,
Type: event.EventAgentFinalAnswer,
SessionID: sessionID,
Data: event.AgentFinalAnswerData{
Content: response.Content,
Done: false,
},
})
}
}
e.eventBus.Emit(ctx, event.Event{
ID: answerID,