[PR #24575] refactor: unify blob chunk merging logic for plugin tool and agent #30701

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

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

State: closed
Merged: Yes


Summary

This PR unifies the blob chunk merging logic between plugin tool and agent implementations by extracting the common functionality into a reusable utility.

Fixes #24576

Changes

  • Extracted common utility: Created core/plugin/utils/chunk_merger.py with merge_blob_chunks() function
  • Replaced duplicate code: Updated both tool.py and agent.py to use the unified implementation
  • Enhanced functionality: Function renamed from merge_chunks to merge_blob_chunks for better clarity
  • Comprehensive testing: Added 16 test cases covering various scenarios:
    • Data integrity verification (merged chunks == original data)
    • Edge cases (empty chunks, single chunks, exact buffer sizes)
    • Concurrent file handling
    • Large file simulation (1MB split into 128 chunks)
    • Error handling (file/chunk size limits)
    • Type safety for both ToolInvokeMessage and AgentInvokeMessage

Test plan

  • All 16 unit tests pass
  • Type checking with mypy passes
  • Code formatting with ruff passes
  • Existing functionality preserved (tool and agent chunk merging works identically)

Technical Details

  • Supports generic message types using TypeVar
  • Maintains configurable size limits (30MB file, 8KB chunk by default)
  • Proper buffer management and cleanup
  • Type-safe implementation with proper assertions

🤖 Generated with Claude Code

**Original Pull Request:** https://github.com/langgenius/dify/pull/24575 **State:** closed **Merged:** Yes --- ## Summary This PR unifies the blob chunk merging logic between plugin tool and agent implementations by extracting the common functionality into a reusable utility. Fixes #24576 ## Changes - **Extracted common utility**: Created `core/plugin/utils/chunk_merger.py` with `merge_blob_chunks()` function - **Replaced duplicate code**: Updated both `tool.py` and `agent.py` to use the unified implementation - **Enhanced functionality**: Function renamed from `merge_chunks` to `merge_blob_chunks` for better clarity - **Comprehensive testing**: Added 16 test cases covering various scenarios: - Data integrity verification (merged chunks == original data) - Edge cases (empty chunks, single chunks, exact buffer sizes) - Concurrent file handling - Large file simulation (1MB split into 128 chunks) - Error handling (file/chunk size limits) - Type safety for both ToolInvokeMessage and AgentInvokeMessage ## Test plan - [x] All 16 unit tests pass - [x] Type checking with mypy passes - [x] Code formatting with ruff passes - [x] Existing functionality preserved (tool and agent chunk merging works identically) ## Technical Details - Supports generic message types using TypeVar - Maintains configurable size limits (30MB file, 8KB chunk by default) - Proper buffer management and cleanup - Type-safe implementation with proper assertions 🤖 Generated with [Claude Code](https://claude.ai/code)
yindo added the pull-request label 2026-02-21 20:48:03 -05:00
yindo closed this issue 2026-02-21 20:48:03 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#30701