MicrosandboxInterpreter

class MicrosandboxInterpreter(BaseInterpreter):
Microsandbox Code Interpreter implementation. This interpreter provides secure code execution using microsandbox, a self-hosted platform for secure execution of untrusted user/AI code. It supports Python code execution via PythonSandbox, JavaScript/Node.js code execution via NodeSandbox, and shell commands via the command interface. Parameters:
  • require_confirm (bool, optional): If True, prompt user before running code strings for security. (default: :obj:True)
  • server_url (str, optional): URL of the microsandbox server. If not provided, will use MSB_SERVER_URL environment variable, then fall back to http://127.0.0.1:5555. (default: :obj:None)
  • api_key (str, optional): API key for microsandbox authentication. If not provided, will use MSB_API_KEY environment variable. (default: :obj:None)
  • namespace (str, optional): Namespace for the sandbox. (default: :obj:"default")
  • sandbox_name (str, optional): Name of the sandbox instance. If not provided, a random name will be generated by the SDK. (default: :obj:None)
  • timeout (int, optional): Default timeout for code execution in seconds. (default: :obj:30) Environment Variables:
  • MSB_SERVER_URL: URL of the microsandbox server.
  • MSB_API_KEY: API key for microsandbox authentication.
Note: The SDK handles parameter priority as: user parameter > environment variable > default value.

init

def __init__(
    self,
    require_confirm: bool = True,
    server_url: Optional[str] = None,
    api_key: Optional[str] = None,
    namespace: str = 'default',
    sandbox_name: Optional[str] = None,
    timeout: int = 30
):

run

def run(self, code: str, code_type: str = 'python'):
Executes the given code in the microsandbox. Parameters:
  • code (str): The code string to execute.
  • code_type (str): The type of code to execute. Supported types: ‘python’, ‘javascript’, ‘bash’. (default: :obj:python)
Returns: str: The string representation of the output of the executed code.

_confirm_execution

def _confirm_execution(self, execution_type: str):
Prompt user for confirmation before executing code or commands. Parameters:
  • execution_type (str): Type of execution (‘code’ or ‘command’).

supported_code_types

def supported_code_types(self):
Provides supported code types by the interpreter.

update_action_space

def update_action_space(self, action_space: Dict[str, Any]):
Updates action space for interpreter. Parameters:
  • action_space: Action space dictionary (unused in microsandbox).
Note: Microsandbox doesn’t support action space updates as it runs in isolated environments for each execution.

execute_command

def execute_command(self, command: str):
Execute a shell command in the microsandbox. This method is designed for package management and system administration tasks. It executes shell commands directly using the microsandbox command interface. Parameters:
  • command (str): The shell command to execute (e.g., “pip install numpy”, “ls -la”, “apt-get update”).
Returns: Union[str, Tuple[str, str]]: The output of the command.

del

def __del__(self):
Destructor for the MicrosandboxInterpreter class. Microsandbox uses context managers for resource management, so no explicit cleanup is needed.