| 24 | return super().execute_code_blocks(tool_call_code_blocks) |
| 25 | |
| 26 | class DCLCE(DockerCommandLineCodeExecutor): |
| 27 | def __init__( |
| 28 | self, |
| 29 | image: str = "python:3-slim", |
| 30 | container_name: Optional[str] = None, |
| 31 | timeout: int = 60, |
| 32 | work_dir: Union[Path, str] = Path("."), |
| 33 | bind_dir: Optional[Union[Path, str]] = None, |
| 34 | auto_remove: bool = True, |
| 35 | stop_container: bool = True, |
| 36 | execution_policies: Optional[Dict[str, bool]] = None, |
| 37 | ): |
| 38 | if timeout < 1: |
| 39 | raise ValueError("Timeout must be greater than or equal to 1.") |
| 40 | |
| 41 | if isinstance(work_dir, str): |
| 42 | work_dir = Path(work_dir) |
| 43 | work_dir.mkdir(exist_ok=True) |
| 44 | |
| 45 | if bind_dir is None: |
| 46 | bind_dir = work_dir |
| 47 | elif isinstance(bind_dir, str): |
| 48 | bind_dir = Path(bind_dir) |
| 49 | |
| 50 | client = docker.from_env() |
| 51 | # Check if the image exists |
| 52 | try: |
| 53 | client.images.get(image) |
| 54 | except ImageNotFound: |
| 55 | logging.info(f"Pulling image {image}...") |
| 56 | # Let the docker exception escape if this fails. |
| 57 | client.images.pull(image) |
| 58 | |
| 59 | if container_name is None: |
| 60 | container_name = f"autogen-code-exec-{uuid.uuid4()}" |
| 61 | |
| 62 | # Start a container from the image, read to exec commands later |
| 63 | self._container = client.containers.create( |
| 64 | image, |
| 65 | name=container_name, |
| 66 | entrypoint="/bin/sh", |
| 67 | tty=True, |
| 68 | auto_remove=auto_remove, |
| 69 | volumes={str(bind_dir.resolve()): {"bind": "/workspace/repository", "mode": "rw"}, str(work_dir.resolve()): {"bind": "/workspace", "mode": "rw"}}, |
| 70 | working_dir="/workspace", |
| 71 | ) |
| 72 | self._container.start() |
| 73 | |
| 74 | _wait_for_ready(self._container) |
| 75 | |
| 76 | def cleanup() -> None: |
| 77 | try: |
| 78 | container = client.containers.get(container_name) |
| 79 | container.stop() |
| 80 | except docker.errors.NotFound: |
| 81 | pass |
| 82 | atexit.unregister(cleanup) |
| 83 |
no outgoing calls
no test coverage detected