[PR #25320] feat: Enable streaming output for Workflow-as-Tool nodes #31009

Closed
opened 2026-02-21 20:48:39 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/25320

State: closed
Merged: No


Fixes #25206

📝 Summary

This PR resolves the issue where Workflows used as Tools lose their streaming output capability. It introduces a "Stream Mode" toggle for these tool nodes, allowing them to provide real-time, chunk-by-chunk output just like a standard LLM node. This significantly improves the user experience and performance for nested workflows or chatflows that rely on other workflows as tools.


Backend Changes

  • New API Endpoint: Added POST /apps/<app_id>/workflows/draft/nodes/<node_id>/stream-toggle to enable or disable streaming mode for a tool node. The endpoint includes permission checks to ensure only authorized users can make changes.
  • New Service Logic: Implemented the update_draft_workflow_node function in WorkflowService to handle the modification of the workflow graph in the database.
  • Core Tool Enhancement:
    • Added a new ToolProviderType: STREAM_WORKFLOW.
    • Updated ToolManager to recognize the new provider type and correctly set a streaming=True flag on the tool runtime.
    • Modified the WorkflowTool._invoke method to handle generator-based results, iterating through them and yielding StreamEvent.TEXT_CHUNK events for real-time output.

Frontend Changes

  • New UI Component: A toggle <Button> has been added to the Tool node's settings panel (panel.tsx), allowing users to switch between "Stream Mode" and "Workflow Mode".
  • New State Management Logic: Created a dedicated React hook, use-tool-stream-toggle.ts, to encapsulate all related frontend logic. This hook handles:
    • API requests to the new endpoint.
    • Updating the node's state in the UI upon success.
    • Syncing the changes with the backend draft.
    • Error handling for the API call.
  • Internationalization (i18n): Added new UI text for the toggle button in both English (en-US) and Chinese (zh-Hans).

📸 Screenshots / Demo

This feature introduces a "Stream Mode" toggle for Workflow-as-a-Tool nodes.

Before (Normal Output Mode)

This is the default behavior, compatible with legacy workflows. The tool node waits for the entire sub-workflow to finish before outputting all at once.

before

After (Stream Mode Enabled)

After enabling "Stream Mode", the tool node provides real-time, chunk-by-chunk streaming output.

after


Checklist

  • This change requires a documentation update.
  • I understand that this PR may be closed if there was no previous discussion or issue.
  • I've added tests for the changes that I've introduced.
  • I ran dev/reformat (backend) and cd web && npx lint-staged (frontend) to appease the lint gods.
**Original Pull Request:** https://github.com/langgenius/dify/pull/25320 **State:** closed **Merged:** No --- Fixes #25206 ## 📝 Summary This PR resolves the issue where **Workflows used as Tools** lose their streaming output capability. It introduces a "Stream Mode" toggle for these tool nodes, allowing them to provide real-time, chunk-by-chunk output just like a standard LLM node. This significantly improves the user experience and performance for nested workflows or chatflows that rely on other workflows as tools. --- ## ✨ Backend Changes - **New API Endpoint**: Added `POST /apps/<app_id>/workflows/draft/nodes/<node_id>/stream-toggle` to enable or disable streaming mode for a tool node. The endpoint includes permission checks to ensure only authorized users can make changes. - **New Service Logic**: Implemented the `update_draft_workflow_node` function in `WorkflowService` to handle the modification of the workflow graph in the database. - **Core Tool Enhancement**: - Added a new `ToolProviderType`: `STREAM_WORKFLOW`. - Updated `ToolManager` to recognize the new provider type and correctly set a `streaming=True` flag on the tool runtime. - Modified the `WorkflowTool._invoke` method to handle generator-based results, iterating through them and yielding `StreamEvent.TEXT_CHUNK` events for real-time output. --- ## ✨ Frontend Changes - **New UI Component**: A toggle `<Button>` has been added to the Tool node's settings panel (`panel.tsx`), allowing users to switch between "Stream Mode" and "Workflow Mode". - **New State Management Logic**: Created a dedicated React hook, `use-tool-stream-toggle.ts`, to encapsulate all related frontend logic. This hook handles: - API requests to the new endpoint. - Updating the node's state in the UI upon success. - Syncing the changes with the backend draft. - Error handling for the API call. - **Internationalization (i18n)**: Added new UI text for the toggle button in both English (`en-US`) and Chinese (`zh-Hans`). --- ## 📸 Screenshots / Demo This feature introduces a "Stream Mode" toggle for Workflow-as-a-Tool nodes. ### Before (Normal Output Mode) This is the default behavior, compatible with legacy workflows. The tool node waits for the entire sub-workflow to finish before outputting all at once. ![before](https://github.com/user-attachments/assets/756f26fb-942c-4b96-9b97-7e7810fa517c) ### After (Stream Mode Enabled) After enabling "Stream Mode", the tool node provides real-time, chunk-by-chunk streaming output. ![after](https://github.com/user-attachments/assets/1b46f841-c55f-433d-a2b8-c88504d620c5) --- ## ✅ Checklist - [ ] This change requires a documentation update. - [X] I understand that this PR may be closed if there was no previous discussion or issue. - [X] I've added tests for the changes that I've introduced. - [X] I ran `dev/reformat` (backend) and `cd web && npx lint-staged` (frontend) to appease the lint gods.
yindo added the pull-request label 2026-02-21 20:48:39 -05:00
yindo closed this issue 2026-02-21 20:48:39 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#31009