Download core/tools/sb_expose_tool.py from Kyrlos11/nniback: direct link, hf CLI and curl.
- Browser
- Download file 5.26 kB
-
https://huggingface.co/spaces/Kyrlos11/nniback/resolve/main/core/tools/sb_expose_tool.py
- Command line
-
hf download hf://spaces/Kyrlos11/nniback/core/tools/sb_expose_tool.py
-
curl -L -o sb_expose_tool.py https://huggingface.co/spaces/Kyrlos11/nniback/resolve/main/core/tools/sb_expose_tool.py
5.26 kB
| from core.agentpress.tool import ToolResult, openapi_schema, tool_metadata | |
| from core.sandbox.tool_base import SandboxToolsBase | |
| from core.agentpress.thread_manager import ThreadManager | |
| import asyncio | |
| import time | |
| class SandboxExposeTool(SandboxToolsBase): | |
| """Tool for exposing and retrieving preview URLs for sandbox ports.""" | |
| def __init__(self, project_id: str, thread_manager: ThreadManager): | |
| super().__init__(project_id, thread_manager) | |
| async def expose_port(self, port: int) -> ToolResult: | |
| try: | |
| await self._ensure_sandbox() | |
| port = int(port) | |
| if not 1 <= port <= 65535: | |
| return self.fail_response(f"Invalid port number: {port}. Must be between 1 and 65535.") | |
| if port == 8080: | |
| return self.fail_response( | |
| "Port 8080 is already auto-exposed! You don't need this tool for port 8080. " | |
| "When you create HTML files with create_file or full_file_rewrite, they automatically get preview URLs. " | |
| "Just create your HTML file and use the URL from the tool response." | |
| ) | |
| if port not in [6080, 8003]: # Skip check for known sandbox ports | |
| try: | |
| port_check = await self.sandbox.process.exec(f"netstat -tlnp | grep :{port}", timeout=5) | |
| if port_check.exit_code != 0: | |
| return self.fail_response(f"No service is currently listening on port {port}. Please start a service on this port first.") | |
| except Exception: | |
| # If we can't check, proceed anyway - the user might be starting a service | |
| pass | |
| # Get the preview link for the specified port | |
| preview_link = await self.sandbox.get_preview_link(port) | |
| # Extract the actual URL from the preview link object | |
| url = preview_link.url if hasattr(preview_link, 'url') else str(preview_link) | |
| return self.success_response({ | |
| "url": url, | |
| "port": port, | |
| "message": f"Successfully exposed port {port} to the public. Users can now access this service at: {url}" | |
| }) | |
| except ValueError: | |
| return self.fail_response(f"Invalid port number: {port}. Must be a valid integer between 1 and 65535.") | |
| except Exception as e: | |
| return self.fail_response(f"Error exposing port {port}: {str(e)}") | |