refactor(agui): redesign event dispatching with strategy pattern and deferred queue#1341
Open
jujn wants to merge 6 commits into
Open
refactor(agui): redesign event dispatching with strategy pattern and deferred queue#1341jujn wants to merge 6 commits into
jujn wants to merge 6 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the AG-UI adapter’s streaming event conversion to use a strategy-based dispatch model with a dedicated StreamContext that supports deferred “end” events, and extends the core message model with a CustomBlock to enable emitting AG-UI CUSTOM events.
Changes:
- Refactors
AguiAgentAdapterto dispatchContentBlockconversion viaBlockEventConverterstrategies and manage lifecycle/end-event ordering viaStreamContext. - Adds block converters for text, thinking/reasoning, tool use/results, and custom blocks under
adapter.strategy. - Introduces
CustomBlockas a newContentBlocksubtype and wires it into Jackson polymorphic serialization; updates AG-UI adapter tests for revised lifecycle behavior.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| agentscope-extensions/agentscope-extensions-agui/src/main/java/io/agentscope/core/agui/adapter/AguiAgentAdapter.java | Refactors the adapter to strategy-based block conversion and deferred end-event flushing. |
| agentscope-extensions/agentscope-extensions-agui/src/main/java/io/agentscope/core/agui/adapter/StreamContext.java | Adds a context object to track active lifecycles and queue/flush deferred end events. |
| agentscope-extensions/agentscope-extensions-agui/src/main/java/io/agentscope/core/agui/adapter/strategy/BlockEventConverter.java | Introduces the strategy interface for block→event conversion. |
| agentscope-extensions/agentscope-extensions-agui/src/main/java/io/agentscope/core/agui/adapter/strategy/TextBlockConverter.java | Implements text block conversion with lifecycle tracking and deferred end events. |
| agentscope-extensions/agentscope-extensions-agui/src/main/java/io/agentscope/core/agui/adapter/strategy/ThinkingBlockConverter.java | Implements reasoning conversion (when enabled) with lifecycle tracking and deferred end events. |
| agentscope-extensions/agentscope-extensions-agui/src/main/java/io/agentscope/core/agui/adapter/strategy/ToolUseBlockConverter.java | Implements tool call start/args conversion and interrupts active text/reasoning lifecycles. |
| agentscope-extensions/agentscope-extensions-agui/src/main/java/io/agentscope/core/agui/adapter/strategy/ToolResultBlockConverter.java | Implements tool result conversion and ensures tool call closure semantics. |
| agentscope-extensions/agentscope-extensions-agui/src/main/java/io/agentscope/core/agui/adapter/strategy/CustomBlockConverter.java | Implements CustomBlock→AG-UI CUSTOM event emission. |
| agentscope-extensions/agentscope-extensions-agui/src/test/java/io/agentscope/core/agui/adapter/AguiAgentAdapterTest.java | Updates test expectations for the new lifecycle/event ordering behavior. |
| agentscope-core/src/main/java/io/agentscope/core/message/CustomBlock.java | Adds a new ContentBlock subtype for custom extension payloads. |
| agentscope-core/src/main/java/io/agentscope/core/message/ContentBlock.java | Registers CustomBlock in the sealed hierarchy and Jackson subtype mapping. |
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description:
Closes #1347, #1222 and #1382
This PR implements the architectural refactoring proposed in Issue 1347, bringing a strategy-based dispatcher to the AG-UI adapter and natively supporting AG-UI custom events.
Code Changes & Architecture
AguiAgentAdapter: Replaced the massiveif-elseblock with aBlockEventConverterstrategy interface. The adapter now acts as a clean router (O(1)dispatching based on Block class).StreamContext: Replaced the heavySet-based state tracking with a lightweight context. It uses a Deferred Queue mechanism (delayingEndevents until termination or interruption), completely resolving lifecycle mismatch bugs and tail-end data loss.BlockEventConverterImplementations: Logic forTextBlock,ThinkingBlock,ToolUseBlock, andToolResultBlockare now beautifully isolated in their own classes.Introducing
CustomBlockTo fully support the AG-UI
CUSTOMevent protocol without hacky workarounds, this PR introducesCustomBlockinto theagentscope-core\messagepackage.CustomBlockin thecore-messagepackage introduces a slight coupling with frontend concepts, AG-UI has effectively become one of the three foundational interaction protocols for modern Agents. A genericCustomBlock(containing justnameandvalue) is agnostic enough to serve as a universal side-channel for Agent-to-UI communications (e.g., real-time tool execution progress, workflow status changes). Strongly recommend this inclusion for developer experience.CustomBlockConverter: TranslatesCustomBlockinto stateless AG-UICustomevents dynamically.Resolves
CustomBlock.of("tool_progress", "50%")from long-running tools, perfectly resolving the real-time feedback issue without polluting theReasoningblocks).Testing
testRunWithCustomEventForToolProgressinAguiAgentAdapterTestto verify that stateless custom events do not interfere with the lifecycle of main streaming blocks (Text/Tool).Checklist
Please check the following items before code is ready to be reviewed.
mvn spotless:applymvn test)