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:
+27
-12
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user