fix(agent): support returnDirect in default ReactAgent#4481
Open
gaozexun wants to merge 9 commits intoalibaba:mainfrom
Open
fix(agent): support returnDirect in default ReactAgent#4481gaozexun wants to merge 9 commits intoalibaba:mainfrom
gaozexun wants to merge 9 commits intoalibaba:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes the default ReactAgent ReAct loop so that tools annotated with returnDirect=true can terminate execution immediately (skipping the extra, unnecessary second LLM call), matching the intended Spring AI returnDirect semantics.
Changes:
- Detect
returnDirecton the lastToolResponseMessageand routetools -> ENDinstead of continuing to the model. - When a
returnDirectToolResponseMessageis the last message, convert it into the finalAssistantMessage. - Introduce
ReturnDirectMessageSupporthelper forreturnDirectdetection and tool-response-to-assistant conversion.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/ReactAgent.java |
Adds default-agent handling for returnDirect to end the graph early and return the tool output as the final assistant message. |
spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/hook/returndirect/ReturnDirectMessageSupport.java |
New shared helper for detecting returnDirect metadata and turning a ToolResponseMessage into an AssistantMessage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
...main/java/com/alibaba/cloud/ai/graph/agent/hook/returndirect/ReturnDirectMessageSupport.java
Outdated
Show resolved
Hide resolved
...ng-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/ReactAgent.java
Show resolved
Hide resolved
Contributor
Author
|
@yuluo-yx 我已经解决了 code review 评论,辛苦审核下 |
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.
Describe what this PR does / why we need it
This PR fixes issue #4462 in the default ReactAgent execution flow.
AgentToolNode already writes the returnDirect completion signal into ToolResponseMessage.metadata, but the default ReactAgent does not consume that signal. As a result, after a returnDirect=true tool finishes, the agent may still route back to the model for an unnecessary second model call instead of returning the tool result directly.
This PR fixes that gap in ReactAgent while keeping ReturnDirectModelHook unchanged.
Does this pull request fix one issue?
Yes. Fixes #4462.
Describe how you did it
The patch is intentionally kept small and limited to the default ReactAgent path.
In ReactAgent.makeToolsToModelEdge(...), check whether the last ToolResponseMessage is marked as returnDirect.
If it is returnDirect, route directly to END instead of continuing the ReAct loop.
In ReactAgent.extractAssistantMessage(...), if the last message is a returnDirect ToolResponseMessage, convert it into the final AssistantMessage.
Introduce a small shared helper, ReturnDirectMessageSupport, to centralize:
detecting returnDirect from tool-response metadata
converting a ToolResponseMessage into the final assistant response
ReturnDirectModelHook is intentionally left unchanged, because explicit hook-based behavior already works as expected.
Describe how to verify it
Set a breakpoint at buildChatClientRequestSpec. Before the fix, it is hit twice, which means the LLM is called twice. After the fix, it is hit only once.