| 1 | """Environment implementations for mini-SWE-agent."""
|
| 2 |
|
| 3 | import copy
|
| 4 | import importlib
|
| 5 |
|
| 6 | from minisweagent import Environment
|
| 7 |
|
| 8 | _ENVIRONMENT_MAPPING = {
|
| 9 | "docker": "minisweagent.environments.docker.DockerEnvironment",
|
| 10 | "singularity": "minisweagent.environments.singularity.SingularityEnvironment",
|
| 11 | "local": "minisweagent.environments.local.LocalEnvironment",
|
| 12 | "swerex_docker": "minisweagent.environments.extra.swerex_docker.SwerexDockerEnvironment",
|
| 13 | "swerex_modal": "minisweagent.environments.extra.swerex_modal.SwerexModalEnvironment",
|
| 14 | "bubblewrap": "minisweagent.environments.extra.bubblewrap.BubblewrapEnvironment",
|
| 15 | }
|
| 16 |
|
| 17 |
|
| 18 | def get_environment_class(spec: str) -> type[Environment]:
|
| 19 | full_path = _ENVIRONMENT_MAPPING.get(spec, spec)
|
| 20 | try:
|
| 21 | module_name, class_name = full_path.rsplit(".", 1)
|
| 22 | module = importlib.import_module(module_name)
|
| 23 | return getattr(module, class_name)
|
| 24 | except (ValueError, ImportError, AttributeError):
|
| 25 | msg = f"Unknown environment type: {spec} (resolved to {full_path}, available: {_ENVIRONMENT_MAPPING})"
|
| 26 | raise ValueError(msg)
|
| 27 |
|
| 28 |
|
| 29 | def get_environment(config: dict, *, default_type: str = "") -> Environment:
|
| 30 | config = copy.deepcopy(config)
|
| 31 | environment_class = config.pop("environment_class", default_type)
|
| 32 | return get_environment_class(environment_class)(**config)
|
| 33 |
|