Skip to content

test#12

Open
Cursx wants to merge 44 commits intomainfrom
return
Open

test#12
Cursx wants to merge 44 commits intomainfrom
return

Conversation

@Cursx
Copy link
Copy Markdown
Owner

@Cursx Cursx commented Dec 2, 2025

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

After the tool is called, Dify's Agent will inject the tool response as an Observation into LLM, which will then output the final answer; When the tool returns excessively long text/JSON (such as ECharts configuration), it will significantly increase context load and latency, for example, my workflow may involve Echart charts for 2000 sets of data
Learn from Langchain to enable agents to skip LLM when calling certain tools.

-Add a return signal (strictly Boolean True) to pass from the tool message to the Agent Runner.
-FC Runner adopts the strategy of "short circuit only when all tools return directly": when all tools in the same round are return-direct=True, after executing all tools, aggregate the text as the final answer and return directly; Otherwise, continue back to LLM. Framework similar to Langgraph.

Provided some screenshots of the tests

Screenshots

Before After
... ...

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @Cursx, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly optimizes the Dify Agent's workflow by enabling a direct return mechanism for tool outputs. This change aims to reduce unnecessary LLM context load and latency, particularly when tools produce large amounts of data like ECharts configurations, by allowing the agent to directly use tool results as the final answer without further LLM processing under specific conditions. This approach is inspired by similar strategies in frameworks like Langchain.

Highlights

  • Direct Tool Return Mechanism: Introduced a return_direct signal (Boolean True) that tools can emit, allowing the Agent Runner to bypass the LLM for certain tool outputs.
  • Short-Circuiting for Function Call Runner: The Function Call (FC) Runner now implements a 'short-circuit' strategy: if all tools invoked in a single round return return_direct=True, their aggregated text output is immediately returned as the final answer, skipping further LLM interaction. Otherwise, the process continues back to the LLM.
  • Enhanced ToolInvokeMeta: The ToolInvokeMeta entity now includes an extra field to carry additional metadata, such as the return_direct flag, from tool invocations.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable "direct return" feature for tools, enabling them to bypass subsequent LLM processing and return a final answer directly. This is a great optimization, especially for tools generating large outputs. The implementation is well-thought-out, touching the agent runners, tool engine, and entities to support the new return_direct flag. My review focuses on two points: a high-severity issue in fc_agent_runner.py where tool invocation details are lost in the agent's thought history during a direct return, and a minor issue of an unused variable in cot_agent_runner.py. After addressing these points, the new feature will be more robust and maintainable.

Comment thread api/core/agent/fc_agent_runner.py Outdated
Comment thread api/core/agent/cot_agent_runner.py Outdated
@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 2, 2025

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable "direct return" feature for tools, allowing them to bypass the LLM and return their output directly as the final answer. This is an excellent optimization for tools that produce large outputs, as it will reduce latency and context size. The implementation is well-structured across cot_agent_runner, fc_agent_runner, tool_engine, and workflow_as_tool. The logic for detecting and handling the return_direct flag is sound. The changes in fc_agent_runner to handle multiple tool calls and short-circuiting only when all tools have the direct return flag is particularly well-thought-out. The refactoring into helper methods also improves code readability. I have one minor suggestion for code consistency.

Comment thread api/core/agent/fc_agent_runner.py Outdated
Cursx and others added 3 commits December 2, 2025 19:12
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 2, 2025

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable optimization that allows tools to return responses directly, bypassing the LLM to reduce latency and context load, especially for large outputs. The implementation is well-structured across the cot_agent_runner, fc_agent_runner, and tool-related files.

My review focuses on a few areas for improvement:

  • In fc_agent_runner.py, there's some code duplication that could be refactored for better maintainability, and a minor inconsistency in handling tool_input.
  • In cot_agent_runner.py, I've identified a potential bug where the final LLM-generated answer might not be streamed to the user.
  • For workflow_as_tool, the direct return logic could be made more robust to handle outputs with multiple string values.

Overall, this is a solid contribution. Addressing these points will enhance the robustness and maintainability of the new feature.

Comment thread api/core/agent/cot_agent_runner.py Outdated
Comment thread api/core/agent/fc_agent_runner.py Outdated
Comment thread api/core/agent/fc_agent_runner.py Outdated
Comment thread api/core/tools/workflow_as_tool/tool.py Outdated
@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 2, 2025

/gemini review

1 similar comment
@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 2, 2025

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a 'return direct' mechanism for tools to bypass the LLM and return a response directly, which is a great optimization for tools with large outputs. The implementation across the agent runners and tool engine looks solid. I've identified a few areas for improvement, including removing some unused code, simplifying redundant logic, and making the direct response handling in workflow tools more robust. Overall, these are good changes that enhance the agent's efficiency.

Comment thread api/core/agent/cot_agent_runner.py Outdated
Comment thread api/core/agent/fc_agent_runner.py
Comment thread api/core/agent/fc_agent_runner.py Outdated
Comment thread api/core/tools/workflow_as_tool/tool.py Outdated
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a feature allowing tools to return a final answer directly, bypassing a subsequent LLM call, which is particularly useful for tools generating large outputs. The implementation primarily involves changes in cot_agent_runner.py and fc_agent_runner.py to handle a new return_direct flag. The overall approach is sound. My review includes suggestions for improving code quality by removing unused code and reducing duplication, which will enhance maintainability.

Comment thread api/core/agent/cot_agent_runner.py Outdated
Comment thread api/core/agent/fc_agent_runner.py
Comment thread api/core/agent/fc_agent_runner.py
@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 3, 2025

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a "direct return" feature for tools, allowing them to bypass the LLM and return a response directly. This is particularly useful for tools that produce large outputs, such as ECharts configurations, thereby reducing latency and context load. The implementation is well-executed for both CoT and Function Calling agent runners.

For the CoT agent, a direct_flag is checked after a tool is invoked. If true, the agent loop is terminated, and the tool's response is used as the final answer. For the FC agent, the logic is more complex to handle multiple tool calls, short-circuiting only if all tools in a round signal for direct return. Several new helper methods have been introduced, which improve code structure and readability.

The return_direct signal is propagated from the tool implementation up to the agent runners via the ToolInvokeMeta object. Overall, the changes are robust and effectively address the stated problem. I have a couple of suggestions for minor refactoring to further enhance code clarity and maintainability.

Comment thread api/core/agent/fc_agent_runner.py Outdated
Comment thread api/core/tools/workflow_as_tool/tool.py Outdated
Cursx and others added 2 commits December 3, 2025 15:11
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 3, 2025

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a significant enhancement by implementing a 'return direct' mechanism for tools, allowing the agent to short-circuit the LLM interaction when a tool provides a definitive final answer. This is particularly useful for tools that return large outputs like ECharts configurations, reducing context load and latency. The changes are well-structured, involving modifications to ToolInvokeMeta to carry the return_direct flag, updates to ToolEngine to detect and propagate this flag, and new logic in CotAgentRunner and FcAgentRunner to act upon it. The WorkflowTool has also been updated to support setting this flag in its outputs. Overall, the implementation effectively addresses the problem statement and improves efficiency.

Comment thread api/core/agent/fc_agent_runner.py Outdated
Comment thread api/core/tools/tool_engine.py Outdated
Comment thread api/core/tools/workflow_as_tool/tool.py Outdated
@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 3, 2025

/gemini review

@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 4, 2025

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism to allow agent tools to return directly without an additional LLM call, which is useful for tools with large outputs. The changes I can review in this PR are mainly focused on aggregating tool responses correctly when multiple tools with the same name are called.

The implementation in fc_agent_runner.py to aggregate tool_invoke_meta and observation using defaultdict(list) is a robust way to handle multiple tool calls and prevents potential data loss from overwriting responses. This is a good improvement.

Other changes are mostly stylistic, such as code reformatting and removing trailing newlines, which improve code consistency.

Overall, the visible changes are well-implemented and contribute to the feature described. I don't have any specific issues to raise based on the provided diffs.

@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 4, 2025

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable optimization to skip LLM calls when tools can return answers directly, which should improve latency for certain workflows. The implementation adds a return_direct flag and handles it consistently across both the CoT and Function Calling agent runners. The change to correctly aggregate results from multiple tool calls in the fc_agent_runner is also a great improvement that fixes a potential bug. I have one suggestion to refactor some duplicated aggregation logic to improve code maintainability.

Comment thread api/core/agent/fc_agent_runner.py Outdated
@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 4, 2025

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces an optimization to allow tools to return responses directly, which is particularly useful for tools that produce large outputs. This is achieved by introducing a return_direct flag. The implementation also refactors the tool response aggregation logic into a new _aggregate_by_tool_name helper method, which improves code clarity and reduces duplication.

My review has identified a critical syntax error that will prevent the code from running. Please address this issue.

Comment thread api/core/agent/fc_agent_runner.py Outdated
@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 4, 2025

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the agent's tool response handling in fc_agent_runner.py. A new helper method, _aggregate_by_tool_name, has been introduced to consolidate the logic for aggregating tool results. This is a great improvement as it reduces code duplication in both the main run loop and the _handle_direct_return method. More importantly, this change fixes a potential bug where results from multiple calls to the same tool within a single agent step would be overwritten. The new implementation correctly gathers all results into a list. The code is now cleaner and more robust. I have one minor suggestion to further improve the code structure.

Comment thread api/core/agent/fc_agent_runner.py Outdated
@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 4, 2025

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable optimization by allowing tools to short-circuit the agent loop, which is particularly useful for tools with large outputs. The refactoring in fc_agent_runner.py to use the _aggregate_by_tool_name helper method is a great improvement for code clarity and maintainability, as it centralizes the logic for handling multiple tool calls and fixes a potential issue where multiple calls to the same tool would overwrite each other's results. The minor stylistic fixes, like adding newlines at the end of files, are also appreciated as they align with best practices. I've added one suggestion to further improve the robustness of the new helper method.

Comment thread api/core/agent/fc_agent_runner.py Outdated
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 4, 2025

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request primarily refactors the FunctionCallAgentRunner to better handle tool responses. A new helper method, _aggregate_by_tool_name, has been introduced to centralize the logic for aggregating tool responses. This change not only improves code readability and maintainability by removing duplicated code but also crucially fixes a bug where multiple calls to the same tool would result in data being overwritten. The implementation is clean and effective. I have one suggestion to further enhance type safety for better long-term maintainability.

Comment thread api/core/agent/fc_agent_runner.py
@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 4, 2025

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable optimization for agent tool calls, enabling a "direct return" mechanism to bypass the LLM for certain tools. This is particularly useful for tools that generate large outputs, reducing latency and context load. The implementation correctly follows the described strategy of short-circuiting only when all tools in a round opt for direct return. Additionally, the code is significantly improved by refactoring the aggregation of tool responses into a new _aggregate_by_tool_name helper method. This not only enhances readability and maintainability by reducing code duplication but also fixes a subtle bug where multiple calls to the same tool could lead to data being overwritten. The changes are well-structured, and the code quality is high. I have no further recommendations.

@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 17, 2025

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable optimization by allowing tools to return responses directly, bypassing an unnecessary LLM call. This can significantly reduce latency and context load, especially for tools that return large amounts of data.

The implementation is well-structured, with the introduction of a return_direct flag and a new helper method _aggregate_by_tool_name to centralize aggregation logic.

I have one suggestion regarding a minor performance regression in _handle_direct_return where the new helper is called multiple times. Addressing this will ensure the changes are purely performance-positive. Overall, this is a solid improvement.

Comment thread api/core/agent/fc_agent_runner.py
@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 19, 2025

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable optimization by allowing tools to signal a "direct return", bypassing the LLM for certain responses. This can significantly reduce latency and cost, especially for tools that return large data payloads. The implementation is solid, and the introduction of the _aggregate_by_tool_name helper method is a great improvement. It not only refactors duplicated code but also fixes a subtle bug where multiple calls to the same tool would overwrite previous results. The code is now more robust, readable, and maintainable.

Comment thread api/core/agent/fc_agent_runner.py Outdated
Comment on lines +292 to +301
tool_invoke_meta = self._aggregate_by_tool_name(tool_responses, "meta")
observation = self._aggregate_by_tool_name(tool_responses, "tool_response")

self.save_agent_thought(
agent_thought_id=agent_thought_id,
tool_name="",
tool_input="",
thought="",
tool_invoke_meta={
tool_response["tool_call_name"]: tool_response["meta"] for tool_response in tool_responses
},
observation={
tool_response["tool_call_name"]: tool_response["tool_response"]
for tool_response in tool_responses
},
tool_invoke_meta=tool_invoke_meta,
observation=observation,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This is an excellent refactoring. Introducing the _aggregate_by_tool_name helper method and using it here, as well as in _handle_direct_return, significantly improves the code.

This change provides two key benefits:

  1. Bug Fix: The previous dictionary comprehension ({tool_response["tool_call_name"]: ... for ...}) would incorrectly handle scenarios where the same tool is called multiple times in one step, as later calls would overwrite the results of earlier ones. The new aggregation logic correctly collects all results in a list.
  2. Code Quality: It removes duplicated aggregation logic from _handle_direct_return, making the codebase DRY (Don't Repeat Yourself), more readable, and easier to maintain.

The new helper function is well-implemented with clear naming, docstrings, and safe key access using .get(). Great work!

@Cursx
Copy link
Copy Markdown
Owner Author

Cursx commented Dec 19, 2025

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable optimization for agent tool calls by allowing tools with large outputs to return directly, skipping an unnecessary LLM call. The implementation uses a return_direct flag, and the logic in FunctionCallAgentRunner correctly handles this short-circuit path when all tools in a round agree. The refactoring in fc_agent_runner.py, which introduces the _aggregate_by_tool_name helper, is a significant improvement. It enhances code clarity, removes duplication, and fixes a potential bug related to multiple calls to the same tool. The changes are well-executed and improve both performance and maintainability. I have no further suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant