Download core/tools/composio_upload_tool.py from Kyrlos11/nniback: direct link, hf CLI and curl.
- Browser
- Download file 5.75 kB
-
https://huggingface.co/spaces/Kyrlos11/nniback/resolve/main/core/tools/composio_upload_tool.py
- Command line
-
hf download hf://spaces/Kyrlos11/nniback/core/tools/composio_upload_tool.py
-
curl -L -o composio_upload_tool.py https://huggingface.co/spaces/Kyrlos11/nniback/resolve/main/core/tools/composio_upload_tool.py
5.75 kB
| import os | |
| import asyncio | |
| import tempfile | |
| import mimetypes | |
| from pathlib import Path | |
| from core.agentpress.tool import ToolResult, openapi_schema, tool_metadata | |
| from core.sandbox.tool_base import SandboxToolsBase | |
| from core.agentpress.thread_manager import ThreadManager | |
| from core.utils.logger import logger | |
| class ComposioUploadTool(SandboxToolsBase): | |
| """Uploads files from the agent sandbox to Composio's S3 storage. | |
| This enables email attachments via Composio Gmail tools which require | |
| files to be in Composio's own S3 bucket (not external URLs). | |
| """ | |
| def __init__(self, project_id: str, thread_manager: ThreadManager): | |
| super().__init__(project_id, thread_manager) | |
| async def composio_upload( | |
| self, | |
| file_path: str, | |
| tool_slug: str = "GMAIL_SEND_EMAIL", | |
| toolkit_slug: str = "gmail", | |
| ) -> ToolResult: | |
| """Upload a sandbox file to Composio S3 for email attachment use.""" | |
| try: | |
| await self._ensure_sandbox() | |
| file_path = self.clean_path(file_path) | |
| full_path = f"{self.workspace_path}/{file_path}" | |
| # Validate file exists and check size | |
| try: | |
| file_info = await self.sandbox.fs.get_file_info(full_path) | |
| if file_info.size > 50 * 1024 * 1024: | |
| return self.fail_response( | |
| "File too large (>50MB). Please reduce file size before uploading." | |
| ) | |
| except Exception: | |
| return self.fail_response(f"File '{file_path}' not found in workspace.") | |
| # Download file content from sandbox | |
| try: | |
| file_content = await self.sandbox.fs.download_file(full_path) | |
| except Exception as e: | |
| return self.fail_response(f"Failed to read file '{file_path}': {e}") | |
| original_filename = os.path.basename(file_path) | |
| suffix = Path(original_filename).suffix | |
| # Write to temp file — FileUploadable.from_path() needs a local path | |
| tmp = tempfile.NamedTemporaryFile(suffix=suffix, delete=False) | |
| try: | |
| tmp.write(file_content) | |
| tmp.close() | |
| from composio.core.models._files import FileUploadable | |
| from core.composio_integration.client import get_composio_client | |
| composio_client = get_composio_client() | |
| # FileUploadable.from_path is synchronous (uses requests.put), | |
| # so wrap in asyncio.to_thread to avoid blocking the event loop. | |
| file_obj = await asyncio.to_thread( | |
| FileUploadable.from_path, | |
| client=composio_client, | |
| file=tmp.name, | |
| tool=tool_slug, | |
| toolkit=toolkit_slug, | |
| ) | |
| content_type = file_obj.mimetype | |
| if not content_type: | |
| content_type, _ = mimetypes.guess_type(original_filename) | |
| if not content_type: | |
| content_type = "application/octet-stream" | |
| return self.success_response( | |
| f"File '{original_filename}' uploaded to Composio storage.\n\n" | |
| f"Attachment data (use with email tool's attachment parameter):\n" | |
| f'{{"s3key": "{file_obj.s3key}", ' | |
| f'"mimetype": "{content_type}", ' | |
| f'"name": "{file_obj.name}"}}' | |
| ) | |
| finally: | |
| if os.path.exists(tmp.name): | |
| os.unlink(tmp.name) | |
| except Exception as e: | |
| logger.error(f"Composio upload failed: {e}") | |
| return self.fail_response(f"Failed to upload to Composio: {e}") | |