|
from typing import Dict, List, Any, Optional |
|
|
|
from aworld.sandbox.base import Sandbox |
|
from aworld.sandbox.common import BaseSandbox |
|
from aworld.sandbox.models import SandboxEnvType |
|
from aworld.sandbox.implementations import LocalSandbox, KubernetesSandbox, SuperSandbox |
|
|
|
|
|
|
|
DefaultSandbox = LocalSandbox |
|
|
|
|
|
|
|
def create_sandbox( |
|
env_type: Optional[int] = None, |
|
sandbox_id: Optional[str] = None, |
|
metadata: Optional[Dict[str, str]] = None, |
|
timeout: Optional[int] = None, |
|
mcp_servers: Optional[List[str]] = None, |
|
mcp_config: Optional[Any] = None, |
|
**kwargs |
|
) -> Sandbox: |
|
""" |
|
Factory function to create a sandbox instance based on the environment type. |
|
|
|
Args: |
|
env_type: The environment type. Defaults to LOCAL if None. |
|
sandbox_id: Unique identifier for the sandbox. If None, one will be generated. |
|
metadata: Additional metadata for the sandbox. |
|
timeout: Timeout for sandbox operations. |
|
mcp_servers: List of MCP servers to use. |
|
mcp_config: Configuration for MCP servers. |
|
**kwargs: Additional parameters for specific sandbox types. |
|
|
|
Returns: |
|
Sandbox: An instance of a sandbox implementation. |
|
|
|
Raises: |
|
ValueError: If an invalid environment type is provided. |
|
""" |
|
env_type = env_type or SandboxEnvType.LOCAL |
|
|
|
if env_type == SandboxEnvType.LOCAL: |
|
return LocalSandbox( |
|
sandbox_id=sandbox_id, |
|
metadata=metadata, |
|
timeout=timeout, |
|
mcp_servers=mcp_servers, |
|
mcp_config=mcp_config, |
|
**kwargs |
|
) |
|
elif env_type == SandboxEnvType.K8S: |
|
return KubernetesSandbox( |
|
sandbox_id=sandbox_id, |
|
metadata=metadata, |
|
timeout=timeout, |
|
mcp_servers=mcp_servers, |
|
mcp_config=mcp_config, |
|
**kwargs |
|
) |
|
elif env_type == SandboxEnvType.SUPERCOMPUTER: |
|
return SuperSandbox( |
|
sandbox_id=sandbox_id, |
|
metadata=metadata, |
|
timeout=timeout, |
|
mcp_servers=mcp_servers, |
|
mcp_config=mcp_config, |
|
**kwargs |
|
) |
|
else: |
|
raise ValueError(f"Invalid environment type: {env_type}") |
|
|
|
|
|
|
|
old_init = Sandbox.__init__ |
|
|
|
def _sandbox_init(self, *args, **kwargs): |
|
if type(self) is Sandbox: |
|
|
|
pass |
|
else: |
|
|
|
old_init(self, *args, **kwargs) |
|
|
|
|
|
original_new = object.__new__ |
|
|
|
|
|
def _sandbox_new(cls, *args, **kwargs): |
|
if cls is Sandbox: |
|
|
|
return create_sandbox(**kwargs) |
|
else: |
|
|
|
return original_new(cls) |
|
|
|
|
|
Sandbox.__init__ = _sandbox_init |
|
Sandbox.__new__ = _sandbox_new |
|
|
|
|
|
|
|
__all__ = [ |
|
'Sandbox', |
|
'BaseSandbox', |
|
'LocalSandbox', |
|
'KubernetesSandbox', |
|
'SuperSandbox', |
|
'DefaultSandbox', |
|
'SandboxEnvType', |
|
'create_sandbox' |
|
] |
|
|