[Chore/Refactor] Unify blob chunk merging logic between plugin tool and agent implementations #16458

Closed
opened 2026-02-21 19:26:11 -05:00 by yindo · 0 comments
Owner

Originally created by @Yeuoly on GitHub (Aug 26, 2025).

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for refactoring, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • Please do not modify this template :) and fill in all the required fields.

Description

The plugin agent implementation lacks blob chunk merging logic that is already implemented in plugin tools. This creates inconsistent behavior between tool and agent responses when handling blob data. The refactor extracts the common chunk merging functionality into a reusable utility and applies it to both implementations.

Motivation

  1. Performance optimization: Agent responses currently return individual blob chunks instead of complete files, forcing consumers to handle chunk assembly with additional network and processing overhead.

  2. Code duplication elimination: The chunk merging logic (55+ lines) exists in core/plugin/impl/tool.py but is missing from core/plugin/impl/agent.py, creating maintenance burden.

  3. Consistent behavior: Both tools and agents should handle blob data identically - tools already merge chunks into complete blob messages, agents should do the same.

  4. Better error handling: The existing tool implementation includes file size validation (30MB limit) and chunk size validation (8KB limit) that agents currently lack.

Additional Context

  • Current state: Tools return complete BLOB messages, agents return fragmented BLOB_CHUNK messages
  • Performance impact: Consumers of agent responses must implement their own chunk merging, leading to duplicated logic across the codebase
  • Solution: Extract merge_blob_chunks() utility function and apply to both tool.py and agent.py
  • Related PR: #24575 implements this refactor with comprehensive test coverage (16 test cases)

Code comparison:

  • Agent (current): return response (raw chunks)
  • Tool (current): 55+ lines of chunk merging logic
  • Unified (proposed): return merge_blob_chunks(response) (same for both)
Originally created by @Yeuoly on GitHub (Aug 26, 2025). ## Self Checks - [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542). - [x] This is only for refactoring, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues, including closed ones. - [x] I confirm that I am using English to submit this report, otherwise it will be closed. - [x] Please do not modify this template :) and fill in all the required fields. ## Description The plugin agent implementation lacks blob chunk merging logic that is already implemented in plugin tools. This creates inconsistent behavior between tool and agent responses when handling blob data. The refactor extracts the common chunk merging functionality into a reusable utility and applies it to both implementations. ## Motivation 1. **Performance optimization**: Agent responses currently return individual blob chunks instead of complete files, forcing consumers to handle chunk assembly with additional network and processing overhead. 2. **Code duplication elimination**: The chunk merging logic (55+ lines) exists in `core/plugin/impl/tool.py` but is missing from `core/plugin/impl/agent.py`, creating maintenance burden. 3. **Consistent behavior**: Both tools and agents should handle blob data identically - tools already merge chunks into complete blob messages, agents should do the same. 4. **Better error handling**: The existing tool implementation includes file size validation (30MB limit) and chunk size validation (8KB limit) that agents currently lack. ## Additional Context - **Current state**: Tools return complete `BLOB` messages, agents return fragmented `BLOB_CHUNK` messages - **Performance impact**: Consumers of agent responses must implement their own chunk merging, leading to duplicated logic across the codebase - **Solution**: Extract `merge_blob_chunks()` utility function and apply to both `tool.py` and `agent.py` - **Related PR**: #24575 implements this refactor with comprehensive test coverage (16 test cases) **Code comparison:** - **Agent (current)**: `return response` (raw chunks) - **Tool (current)**: 55+ lines of chunk merging logic - **Unified (proposed)**: `return merge_blob_chunks(response)` (same for both)
yindo closed this issue 2026-02-21 19:26:11 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#16458