Spaces:
Sleeping
Sleeping
Upload sandbox_api.py
Browse files
aworld/sandbox/api/super/sandbox_api.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import os
|
| 3 |
+
from typing import Dict, List, Any, Optional
|
| 4 |
+
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
from aworld.sandbox.api.base_sandbox_api import BaseSandboxApi
|
| 8 |
+
from aworld.sandbox.models import SandboxStatus, SandboxEnvType, SandboxSuperResponse
|
| 9 |
+
from aworld.sandbox.run.mcp_servers import McpServers
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class SuperSandboxApi(BaseSandboxApi):
|
| 13 |
+
"""
|
| 14 |
+
API implementation for supercomputer sandbox operations.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
@classmethod
|
| 18 |
+
def _create_sandbox(
|
| 19 |
+
cls,
|
| 20 |
+
env_type: int,
|
| 21 |
+
env_config: Any,
|
| 22 |
+
mcp_servers: Optional[List[str]] = None,
|
| 23 |
+
mcp_config: Optional[Any] = None,
|
| 24 |
+
) -> SandboxSuperResponse:
|
| 25 |
+
"""
|
| 26 |
+
Create a supercomputer sandbox based on the reference implementation.
|
| 27 |
+
"""
|
| 28 |
+
try:
|
| 29 |
+
if not mcp_servers:
|
| 30 |
+
logging.info("_create_sandbox_by_super mcp_servers is not exist")
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
load_dotenv()
|
| 34 |
+
host = os.getenv("SUPERCOMPUTER_HOST")
|
| 35 |
+
|
| 36 |
+
if not host:
|
| 37 |
+
logging.warning("_create_sandbox_by_super SUPERCOMPUTER_HOST is null")
|
| 38 |
+
return None
|
| 39 |
+
|
| 40 |
+
metadata = {
|
| 41 |
+
"status": SandboxStatus.RUNNING,
|
| 42 |
+
"host": host,
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
response = cls._get_mcp_configs(
|
| 46 |
+
mcp_servers=mcp_servers,
|
| 47 |
+
mcp_config=mcp_config,
|
| 48 |
+
metadata=metadata,
|
| 49 |
+
env_type=SandboxEnvType.SUPERCOMPUTER
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
if not response:
|
| 53 |
+
return None
|
| 54 |
+
|
| 55 |
+
return SandboxSuperResponse(
|
| 56 |
+
status=SandboxStatus.RUNNING,
|
| 57 |
+
host=host,
|
| 58 |
+
mcp_config=mcp_config,
|
| 59 |
+
env_type=SandboxEnvType.SUPERCOMPUTER
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
logging.warning(f"Failed to create supercomputer sandbox: {e}")
|
| 64 |
+
return None
|
| 65 |
+
|
| 66 |
+
@classmethod
|
| 67 |
+
def _get_mcp_configs(
|
| 68 |
+
cls,
|
| 69 |
+
mcp_servers: Optional[List[str]] = None,
|
| 70 |
+
mcp_config: Optional[Any] = None,
|
| 71 |
+
metadata: Optional[Dict[str, str]] = None,
|
| 72 |
+
env_type: Optional[int] = None,
|
| 73 |
+
) -> Any:
|
| 74 |
+
"""
|
| 75 |
+
Get MCP configurations for the sandbox.
|
| 76 |
+
"""
|
| 77 |
+
try:
|
| 78 |
+
if not metadata or not metadata.get("host"):
|
| 79 |
+
return mcp_config
|
| 80 |
+
host = metadata.get("host")
|
| 81 |
+
|
| 82 |
+
if not mcp_servers:
|
| 83 |
+
return None
|
| 84 |
+
if not mcp_config or mcp_config.get("mcpServers") is None:
|
| 85 |
+
mcp_config = {
|
| 86 |
+
"mcpServers": {}
|
| 87 |
+
}
|
| 88 |
+
_mcp_servers = mcp_config.get("mcpServers")
|
| 89 |
+
|
| 90 |
+
for server in mcp_servers:
|
| 91 |
+
if server not in _mcp_servers:
|
| 92 |
+
_mcp_servers[server] = {
|
| 93 |
+
"type": "sse",
|
| 94 |
+
"url": f"{host}/{server}/sse"
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
return mcp_config
|
| 98 |
+
except Exception as e:
|
| 99 |
+
logging.warning(f"Failed to get_mcp_configs_from_super: {e}")
|
| 100 |
+
return None
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
@classmethod
|
| 104 |
+
async def _remove_sandbox(
|
| 105 |
+
cls,
|
| 106 |
+
sandbox_id: Optional[str] = None,
|
| 107 |
+
metadata: Optional[Dict[str, str]] = None,
|
| 108 |
+
env_type: Optional[int] = None,
|
| 109 |
+
) -> bool:
|
| 110 |
+
"""
|
| 111 |
+
Remove the supercomputer sandbox.
|
| 112 |
+
"""
|
| 113 |
+
# Supercomputer sandbox doesn't need special removal
|
| 114 |
+
return True
|