from typing import Optional from agentpress.tool import ToolResult, openapi_schema, xml_schema from sandbox.sandbox import SandboxToolsBase, Sandbox from agentpress.thread_manager import ThreadManager 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) @openapi_schema({ "type": "function", "function": { "name": "expose_port", "description": "Expose a port from the agent's sandbox environment to the public internet and get its preview URL. This is essential for making services running in the sandbox accessible to users, such as web applications, APIs, or other network services. The exposed URL can be shared with users to allow them to interact with the sandbox environment.", "parameters": { "type": "object", "properties": { "port": { "type": "integer", "description": "The port number to expose. Must be a valid port number between 1 and 65535.", "minimum": 1, "maximum": 65535 } }, "required": ["port"] } } }) @xml_schema( tag_name="expose-port", mappings=[ {"param_name": "port", "node_type": "content", "path": "."} ], example=''' 8000 3000 5173 8081 ''' ) async def expose_port(self, port: int) -> ToolResult: try: # Ensure sandbox is initialized await self._ensure_sandbox() # Convert port to integer if it's a string port = int(port) # Validate port number if not 1 <= port <= 65535: return self.fail_response(f"Invalid port number: {port}. Must be between 1 and 65535.") # Get the preview link for the specified port preview_link = 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)}")