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.
This commit is contained in:
wizardchen
2026-06-03 22:49:00 +08:00
committed by lyingbug
parent 876fd0276f
commit b9dde2abe8
+11
View File
@@ -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"]++