refactor(agui): redesign event dispatching with strategy pattern and deferred queue#1341
Open
jujn wants to merge 20 commits into
Open
refactor(agui): redesign event dispatching with strategy pattern and deferred queue#1341jujn wants to merge 20 commits into
jujn wants to merge 20 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. |
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, #1382
This PR implements the architectural refactoring proposed in Issue 1347, bringing a strategy-based dispatcher to the AG-UI adapter and enabling pluggable custom converters. Additionally, it introduces support for real-time tool execution progress events (#1222) and seamlessly fixes the lifecycle mismatch bug mentioned in #1382.
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), resolving lifecycle mismatch bugs.BlockEventConverterImplementations: Logic forTextBlock,ThinkingBlock,ToolUseBlock, andToolResultBlockare now beautifully isolated in their own classes.customConverters&enableActingChunk: To support [Feature]: [功能建议] AGUI 中支持长时间运行的工具实时反馈执行情况 #1222 and future broader customization needs without modifying the core framework, we introduced new configuration mechanisms:AguiAdapterConfig): AddedcustomConvertersto allow developers to register their ownBlockEventConverterimplementations, andenableActingChunkto control the emission of intermediate execution chunks.agui-spring-boot-starter): UpdatedAguiPropertiesto includeenable-acting-chunk. ModifiedAgentscopeAguiMvcAutoConfigurationandAgentscopeAguiWebFluxAutoConfigurationby utilizingObjectProvider<BlockEventConverter<?>>. This allows the framework to automatically collect any custom converter beans defined in the Spring context (e.g., using@Component), transforming the adapter into a highly extensible microkernel architecture.ag-uiexamples project to demonstrate how to implement and correctly display real-time tool progress in the frontend.测试脚本及日志.zip