fix(agui): Fix duplicate TextMessageEnd emission (#562) #586
+284
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background: Fix Duplicate AguiEvent.TextMessageEnd Issue
Problem Description
When using AGUI protocol to request a ReActAgent with SKILL and TOOL capabilities, the
AguiAgentAdapterwas emitting duplicateAguiEvent.TextMessageEndevents, causing incorrect event closure in the AGUI event stream.Root Cause
The issue occurs in the
convertEventmethod ofAguiAgentAdapterwhen processingREASONINGevents that contain bothTextBlockandToolUseBlockcontent blocks:First emission path: When processing a
ToolUseBlock,checks if there's an active text message and attempts to end it before starting the tool call (lines 162-169).Second emission path: When processing a
TextBlockin the last event (event.isLast() == true), the code emitsTextMessageEndto close the text message (lines 152-158).The bug: If a message contains both text content and tool usage, and the text block is processed as the last event, the
TextMessageEndis emitted. However, when the subsequentToolUseBlockis processed, the code doesn't check if the message has already been ended, leading to a duplicateTextMessageEndevent.Impact
TextMessageEndevents in the AGUI event streamSolution
Added a guard check using
state.hasEndedMessage(messageId)before emittingTextMessageEndevents. This ensures that:Code Changes
The fix adds a conditional check before emitting
TextMessageEnd:This prevents duplicate emissions while maintaining the existing logic for properly closing messages when tool calls are encountered.