-
Notifications
You must be signed in to change notification settings - Fork 5.9k
feat: add binary streaming support for large file downloads #4310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lucasgomide
wants to merge
4
commits into
main
Choose a base branch
from
lg-support-binary-stream-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+342
−14
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
712ac05
fix: enforce additionalProperties=false in schemas
greysonlalonde 61d2692
fix: ensure nested items have required properties
greysonlalonde dad2668
Merge branch 'main' into gl/fix/apps-strict-tool-calls
greysonlalonde 2163324
feat: add binary streaming support for large file downloads
lucasgomide File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
lib/crewai-tools/src/crewai_tools/tools/crewai_platform_tools/file_hook.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| """File processing hook for CrewAI Platform Tools. | ||
|
|
||
| This module provides a hook that processes file markers returned by platform tools | ||
| and injects the files into the LLM context for native file handling. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| import os | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from crewai.hooks.tool_hooks import ToolCallHookContext | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| _FILE_MARKER_PREFIX = "__CREWAI_FILE__" | ||
|
|
||
| _hook_registered = False | ||
|
|
||
|
|
||
| def process_file_markers(context: ToolCallHookContext) -> str | None: | ||
| """Process file markers in tool results and inject files into context. | ||
|
|
||
| This hook detects file markers returned by platform tools (e.g., download_file) | ||
| and converts them into FileInput objects that are attached to the hook context. | ||
| The agent executor will then inject these files into the tool message for | ||
| native LLM file handling. | ||
|
|
||
| The marker format is: | ||
| __CREWAI_FILE__:filename:content_type:file_path | ||
|
|
||
| Args: | ||
| context: The tool call hook context containing the tool result. | ||
|
|
||
| Returns: | ||
| A human-readable message if a file was processed, None otherwise. | ||
| """ | ||
| result = context.tool_result | ||
|
|
||
| if not result or not result.startswith(_FILE_MARKER_PREFIX): | ||
| return None | ||
|
|
||
| try: | ||
| parts = result.split(":", 3) | ||
| if len(parts) < 4: | ||
| logger.warning(f"Invalid file marker format: {result[:100]}") | ||
| return None | ||
|
|
||
| _, filename, content_type, file_path = parts | ||
|
|
||
| if not os.path.isfile(file_path): | ||
| logger.error(f"File not found: {file_path}") | ||
| return f"Error: Downloaded file not found at {file_path}" | ||
|
|
||
| try: | ||
| from crewai_files import File | ||
| except ImportError: | ||
| logger.warning( | ||
| "crewai_files not installed. File will not be attached to LLM context." | ||
| ) | ||
| return ( | ||
| f"Downloaded file: {filename} ({content_type}). " | ||
| f"File saved at: {file_path}. " | ||
| "Note: Install crewai_files for native LLM file handling." | ||
| ) | ||
|
|
||
| file = File(source=file_path, content_type=content_type, filename=filename) | ||
|
|
||
| context.files = {filename: file} | ||
|
|
||
| file_size = os.path.getsize(file_path) | ||
| size_str = _format_file_size(file_size) | ||
|
|
||
| return f"Downloaded file: {filename} ({content_type}, {size_str}). File is attached for LLM analysis." | ||
|
|
||
| except Exception as e: | ||
| logger.exception(f"Error processing file marker: {e}") | ||
| return f"Error processing downloaded file: {e}" | ||
|
|
||
|
|
||
| def _format_file_size(size_bytes: int) -> str: | ||
| """Format file size in human-readable format. | ||
|
|
||
| Args: | ||
| size_bytes: Size in bytes. | ||
|
|
||
| Returns: | ||
| Human-readable size string. | ||
| """ | ||
| if size_bytes < 1024: | ||
| return f"{size_bytes} bytes" | ||
| elif size_bytes < 1024 * 1024: | ||
| return f"{size_bytes / 1024:.1f} KB" | ||
| elif size_bytes < 1024 * 1024 * 1024: | ||
| return f"{size_bytes / (1024 * 1024):.1f} MB" | ||
| else: | ||
| return f"{size_bytes / (1024 * 1024 * 1024):.1f} GB" | ||
|
|
||
|
|
||
| def register_file_processing_hook() -> bool: | ||
| """Register the file processing hook globally. | ||
|
|
||
| This function should be called once during application initialization | ||
| to enable automatic file injection for platform tools. | ||
|
|
||
| Returns: | ||
| True if the hook was registered, False if it was already registered | ||
| or if registration failed. | ||
| """ | ||
| global _hook_registered | ||
|
|
||
| if _hook_registered: | ||
| logger.debug("File processing hook already registered") | ||
| return False | ||
|
|
||
| try: | ||
| from crewai.hooks import register_after_tool_call_hook | ||
|
|
||
| register_after_tool_call_hook(process_file_markers) | ||
| _hook_registered = True | ||
lucasgomide marked this conversation as resolved.
Dismissed
Show dismissed
Hide dismissed
|
||
| logger.info("File processing hook registered successfully") | ||
| return True | ||
| except ImportError: | ||
| logger.warning( | ||
| "crewai.hooks not available. File processing hook not registered." | ||
| ) | ||
| return False | ||
| except Exception as e: | ||
| logger.exception(f"Failed to register file processing hook: {e}") | ||
| return False | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Increased timeout for large file downloads