From b9dde2abe82c8a059eb51f277c58ea838207018f Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 3 Jun 2026 22:49:00 +0800 Subject: [PATCH] feat(agent): improve content filtering in streaming event handling - Added logic to suppress whitespace-only content emitted before the actual answer starts, addressing issues with spurious empty events interleaved with tool call events. - Enhanced the event emission process to ensure that only meaningful content is routed to the final answer area, improving the clarity and relevance of streamed responses. --- internal/agent/think.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/internal/agent/think.go b/internal/agent/think.go index f82446e0..a0cd114b 100644 --- a/internal/agent/think.go +++ b/internal/agent/think.go @@ -3,6 +3,7 @@ package agent import ( "context" "fmt" + "strings" "time" agenttools "github.com/Tencent/WeKnora/internal/agent/tools" @@ -177,6 +178,16 @@ func (e *AgentEngine) streamThinkingToEventBus( if content == "" { return } + // Suppress whitespace-only content emitted before the real answer has + // started. OpenAI-compatible models frequently prepend a stray newline + // (e.g. "\n\n") to the plain content channel in the same chunk where + // they request tool calls. Routing that to the final-answer area leaks + // spurious empty "answer" events interleaved with tool_call events. + // Once genuine answer text has streamed (answerStreamed), preserve all + // whitespace so the answer's own formatting stays intact. + if !answerStreamed && strings.TrimSpace(content) == "" { + return + } closeThinking() answerStreamed = true emittedEventTypes["final_answer_chunk"]++