Skip to content

Cursx patch 1 fix workflow tool 1#8

Open
Cursx wants to merge 43 commits intomainfrom
Cursx-patch-1-fix-workflow-tool-1
Open

Cursx patch 1 fix workflow tool 1#8
Cursx wants to merge 43 commits intomainfrom
Cursx-patch-1-fix-workflow-tool-1

Conversation

@Cursx
Copy link
Copy Markdown
Owner

@Cursx Cursx commented Nov 21, 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

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 enhances the agent's tool invocation mechanism by introducing a 'direct return' strategy. This allows specific tools to explicitly indicate that their output should be considered the definitive final answer, thereby concluding the agent's reasoning process immediately. This change streamlines agent workflows, improves efficiency by preventing unnecessary subsequent LLM calls, and provides greater control over how tool outputs are integrated into the overall response.

Highlights

  • Direct Return Mechanism for Tools: Introduced a new capability for workflow tools to signal a 'direct return'. When this flag is set, the agent immediately uses the tool's output as the final answer, bypassing further reasoning iterations and improving efficiency.
  • Enhanced ToolInvokeMeta: The ToolInvokeMeta class now includes an extra dictionary field to carry additional metadata, specifically used to propagate the return_direct flag from the tool engine to the agent runners.
  • Agent Runner Logic Updates: Modified both cot_agent_runner.py and fc_agent_runner.py to detect and act upon the return_direct flag. This involves short-circuiting the agent's iteration loop and directly returning the tool's result as the final answer.
  • Tool Engine Integration: Updated tool_engine.py to identify the return_direct signal from ToolInvokeMessage.MessageType.VARIABLE within tool responses and correctly store it in the ToolInvokeMeta.extra field. It also filters these internal variable messages from plain text conversion.
  • Refactored Final Answer Handling: Extracted the common logic for publishing the final message event and yielding the LLMResultChunk into a new private method _yield_final_answer in fc_agent_runner.py, promoting cleaner code and supporting early returns.
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 'direct return' feature for tools, allowing a tool to signal that its result should be returned as the final answer, bypassing further agent iterations. The changes span across the tool engine and both CoT and Function-Calling agent runners. A new extra field is added to ToolInvokeMeta to carry the return_direct flag. The agent runners are updated to respect this flag and alter their execution flow accordingly.

The implementation is mostly correct, but I've found a critical bug in the function-calling agent runner (fc_agent_runner.py) where an undefined variable is used, which would lead to a runtime error. My review includes a fix for this issue.

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>
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 provide a final answer and terminate the agent's execution loop early. This is implemented by passing a return_direct flag through the ToolInvokeMeta. The changes span across the tool engine and both the CoT and Function Calling agent runners to support this new workflow.

The implementation is solid and the new feature is well-integrated. I've identified a minor inconsistency in fc_agent_runner.py regarding dictionary access, which should be addressed for better code robustness and consistency. Overall, this is a good enhancement to the agent's tool-handling capabilities.

Comment thread api/core/agent/fc_agent_runner.py
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 Nov 21, 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, which allows a tool to signal that its output should be treated as the final answer, short-circuiting the agent's iterative process. The implementation is well-integrated across the tool engine and both CoT and Function Call agent runners. The use of the extra field in ToolInvokeMeta is a flexible approach. I've also noted the good refactoring in fc_agent_runner.py which created the _yield_final_answer method, improving code reuse. The overall changes look solid. I have one minor suggestion for improving type safety.

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 Nov 22, 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 a tool's output to be treated as the final answer, thereby terminating the agent's execution loop. This is implemented by adding an extra field to ToolInvokeMeta to carry a return_direct flag. The changes are consistently applied across both CotAgentRunner and FunctionCallAgentRunner, and the logic appears sound. I have one suggestion to improve code conciseness in tool_engine.py for better maintainability.

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

Cursx commented Nov 24, 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' mechanism for tools, enabling them to provide a final answer and terminate the agent's execution flow prematurely. This is achieved by introducing a return_direct flag, which is propagated from the workflow tool, through the tool engine, and to the agent runners. The implementation is logical and covers both CoT and Function Calling agent runners. My review includes a suggestion to refactor a large code block in the fc_agent_runner for better maintainability, and a point about improving the robustness of the flag parsing logic in the workflow_as_tool.

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 Nov 25, 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' feature for workflow tools, allowing them to short-circuit the agent loop and return a final answer directly. The implementation spans across agent runners, the tool engine, and tool entities. While the feature is implemented consistently, I've found a couple of high-severity bugs related to duplicated agent thoughts being saved, and a medium-severity issue regarding code quality. My review includes suggestions to fix these issues.

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 3 commits November 25, 2025 17:00
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
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, allowing a tool's output to be returned immediately as the final answer, bypassing further agent iterations. The changes are implemented across the tool entity, tool engine, workflow tool, and both CoT and Function Calling agent runners. The implementation is mostly solid, but I've found a potential issue in the Function Calling agent runner regarding logging when multiple tools are called. My review includes a suggestion to address this to ensure complete and accurate tracing of agent behavior.

Comment thread api/core/agent/fc_agent_runner.py
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 a tool to terminate an agent's execution loop and return its output as the final answer. This is primarily achieved by introducing a return_direct flag that can be set by tools (specifically workflow tools in this PR). The flag is propagated through a new variable message type and the ToolInvokeMeta.extra field. Both CoT and Function Calling agent runners have been updated to handle this flag, stopping their iteration and yielding the tool's response directly.

The implementation is logical and covers the necessary changes across the tool engine and agent runners. I've found one issue in the new _handle_direct_return function within the function-calling agent runner, where the agent's thought is being duplicated. My review includes a suggestion to fix this.

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>
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 a tool to short-circuit the agent's execution loop and return its result directly. This is primarily aimed at workflow tools. The implementation involves adding a return_direct flag in the tool's output, which is then propagated through the ToolInvokeMeta to the agent runners. Both CotAgentRunner and FunctionCallAgentRunner are updated to handle this flag and terminate the execution loop accordingly. The changes are logical and well-integrated across multiple files. I have one minor suggestion to improve code clarity by removing an unused parameter.

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

Cursx commented Nov 25, 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 a tool to signal that its output should be the final answer, short-circuiting further agent iterations. The changes are well-implemented across the agent runners (cot_agent_runner, fc_agent_runner) and the tool engine. The mechanism uses a special variable message to propagate the return_direct flag, which is a clean approach. The refactoring in fc_agent_runner to centralize final answer handling is also a good improvement. I have one minor suggestion for code clarity.

Comment thread api/core/agent/fc_agent_runner.py
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 in both CoT and Function Calling agents, allowing a tool to provide a final answer and terminate the agent's iteration loop. The changes span across agent runners, the tool engine, and tool definitions to support this new signaling mechanism. The implementation is solid, with new helper methods to handle the direct return logic cleanly. I've provided a couple of suggestions to improve code clarity and maintainability by removing a redundant check and refactoring duplicated code.

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

Cursx commented Nov 26, 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 terminate the agent loop and return a final answer directly. The implementation is well-thought-out, covering both CoT and Function Calling agent runners.

Key changes include:

  • Modifying cot_agent_runner.py and fc_agent_runner.py to handle the return_direct flag from tool invocations.
  • In fc_agent_runner.py, the logic for handling multiple tool calls has been significantly refactored and improved, fixing potential bugs with response aggregation and state management.
  • New helper methods like _handle_direct_return and _yield_final_answer in fc_agent_runner.py make the code more modular and readable.
  • The ToolInvokeMeta entity has been extended to carry the return_direct signal.
  • tool_engine.py is updated to detect the signal from tool messages and propagate it.
  • workflow_as_tool/tool.py is updated to allow workflows to signal a direct return.

The changes are clean, and the refactoring in fc_agent_runner.py is a significant improvement. The new feature is implemented robustly. I have not found any issues of medium or higher severity. Great work!

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 stop the agent's iteration loop and return a final answer immediately. The changes are implemented across agent runners, the tool engine, and tool entities. A workflow can now also signal a direct return. The implementation is mostly solid, but I found a small logic issue in the workflow tool implementation where a piece of code is unreachable.

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

Cursx commented Nov 26, 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, allowing tools to bypass further agent iterations and return a final answer immediately. The implementation is comprehensive, touching upon tool definitions, the tool engine, and both CoT and Function-Calling agent runners to ensure consistent behavior. The code is well-structured, and new logic is encapsulated in helper methods, which is great for maintainability. I have one minor suggestion to further improve the code structure by refactoring a nested function into a static method. Overall, this is a solid contribution that enhances the agent's flexibility.

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

Cursx commented Nov 27, 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 provide a final answer and terminate the agent loop early. This is implemented for both CoT and Function Calling agents, as well as for workflow-based tools. The changes are well-structured, introducing new helper methods to handle the new logic. My main feedback is to refactor some duplicated code in the CoT agent runner to improve maintainability, similar to what has been done in the function calling agent runner.

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

Cursx commented Nov 27, 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 feature for tools, allowing them to return a final answer and bypass further agent iterations. The changes are well-structured across agent runners, the tool engine, and tool implementations. I've identified a couple of areas for improvement regarding type consistency and potential information loss in the new logic. My review includes suggestions to enhance robustness and maintainability.

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

Cursx commented Nov 27, 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 provide a final answer and terminate the agent's execution loop. The changes are implemented consistently across both the Chain-of-Thought (CoT) and Function Calling agent runners. The signaling mechanism is well-designed, propagating a return_direct flag from the tool implementation up to the agent runner.

My review focuses on improving code structure and ensuring deterministic behavior. I've suggested refactoring a nested helper function into a static method for better readability, ensuring deterministic output from dictionary iteration, and removing a redundant variable assignment to simplify the code. Overall, the changes are well-structured and the new feature is implemented correctly.

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 and others added 2 commits November 27, 2025 18:50
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 Nov 27, 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 provide a final answer and bypass further agent reasoning steps. This is primarily implemented for workflow-based tools. The changes are well-structured across agent runners, the tool engine, and tool entity definitions. The agent runners (cot_agent_runner and fc_agent_runner) have been refactored to centralize final answer generation, which improves code clarity. I've identified a potential null pointer issue and a minor opportunity for code optimization.

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

Cursx commented Nov 27, 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 feature for tools, which allows a tool's output to be immediately returned as the final answer, skipping subsequent agent steps. The implementation is well-executed across both CotAgentRunner and FcAgentRunner.

Key changes include:

  • A mechanism for workflow tools to signal a direct return by including return_direct: true in their output.
  • The ToolEngine now detects this signal and passes it along in the ToolInvokeMeta.
  • Agent runners have been updated to handle this flag, yielding the tool's response as the final answer and terminating the execution loop when appropriate.
  • Code has been refactored to extract duplicated logic into helper methods like _yield_final_answer, which improves maintainability.

The changes are logical and the refactoring is a good improvement. I have one minor suggestion to improve the readability of a new logic block.

Comment on lines +121 to +130
direct_text = None
if return_direct_flag:
string_values = [v for k, v in sorted(outputs.items()) if isinstance(v, str)]
if string_values:
direct_text = "\n".join(string_values)

if direct_text is not None:
yield self.create_text_message(direct_text)
else:
yield self.create_text_message(json.dumps(outputs, ensure_ascii=False))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The logic for determining the text output can be simplified to improve readability and reduce redundancy. You can determine the text_output first and then yield it in a single statement.

        text_output = json.dumps(outputs, ensure_ascii=False)
        if return_direct_flag:
            string_values = [v for _, v in sorted(outputs.items()) if isinstance(v, str)]
            if string_values:
                text_output = "\n".join(string_values)

        yield self.create_text_message(text_output)

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