sandbox_config.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import os
  2. from dataclasses import dataclass, field, fields
  3. from openhands.core.config.config_utils import get_field_info
  4. @dataclass
  5. class SandboxConfig:
  6. """Configuration for the sandbox.
  7. Attributes:
  8. remote_runtime_api_url: The hostname for the Remote Runtime API.
  9. local_runtime_url: The default hostname for the local runtime. You may want to change to http://host.docker.internal for DIND environments
  10. base_container_image: The base container image from which to build the runtime image.
  11. runtime_container_image: The runtime container image to use.
  12. user_id: The user ID for the sandbox.
  13. timeout: The timeout for the default sandbox action execution.
  14. remote_runtime_init_timeout: The timeout for the remote runtime to start.
  15. enable_auto_lint: Whether to enable auto-lint.
  16. use_host_network: Whether to use the host network.
  17. initialize_plugins: Whether to initialize plugins.
  18. force_rebuild_runtime: Whether to force rebuild the runtime image.
  19. runtime_extra_deps: The extra dependencies to install in the runtime image (typically used for evaluation).
  20. This will be rendered into the end of the Dockerfile that builds the runtime image.
  21. It can contain any valid shell commands (e.g., pip install numpy).
  22. The path to the interpreter is available as $OH_INTERPRETER_PATH,
  23. which can be used to install dependencies for the OH-specific Python interpreter.
  24. runtime_startup_env_vars: The environment variables to set at the launch of the runtime.
  25. This is a dictionary of key-value pairs.
  26. This is useful for setting environment variables that are needed by the runtime.
  27. For example, for specifying the base url of website for browsergym evaluation.
  28. browsergym_eval_env: The BrowserGym environment to use for evaluation.
  29. Default is None for general purpose browsing. Check evaluation/miniwob and evaluation/webarena for examples.
  30. platform: The platform on which the image should be built. Default is None.
  31. """
  32. remote_runtime_api_url: str = 'http://localhost:8000'
  33. local_runtime_url: str = 'http://localhost'
  34. keep_runtime_alive: bool = False
  35. rm_all_containers: bool = False
  36. api_key: str | None = None
  37. base_container_image: str = 'nikolaik/python-nodejs:python3.12-nodejs22' # default to nikolaik/python-nodejs:python3.12-nodejs22 for eventstream runtime
  38. runtime_container_image: str | None = None
  39. user_id: int = os.getuid() if hasattr(os, 'getuid') else 1000
  40. timeout: int = 120
  41. remote_runtime_init_timeout: int = 180
  42. enable_auto_lint: bool = (
  43. False # once enabled, OpenHands would lint files after editing
  44. )
  45. use_host_network: bool = False
  46. runtime_extra_build_args: list[str] | None = None
  47. initialize_plugins: bool = True
  48. force_rebuild_runtime: bool = False
  49. runtime_extra_deps: str | None = None
  50. runtime_startup_env_vars: dict[str, str] = field(default_factory=dict)
  51. browsergym_eval_env: str | None = None
  52. platform: str | None = None
  53. close_delay: int = 15
  54. def defaults_to_dict(self) -> dict:
  55. """Serialize fields to a dict for the frontend, including type hints, defaults, and whether it's optional."""
  56. dict = {}
  57. for f in fields(self):
  58. dict[f.name] = get_field_info(f)
  59. return dict
  60. def __str__(self):
  61. attr_str = []
  62. for f in fields(self):
  63. attr_name = f.name
  64. attr_value = getattr(self, f.name)
  65. attr_str.append(f'{attr_name}={repr(attr_value)}')
  66. return f"SandboxConfig({', '.join(attr_str)})"
  67. def __repr__(self):
  68. return self.__str__()