# File based from: https://github.com/microsoft/autogen/blob/main/autogen/coding/base.py# Credit to original authorsfrom__future__importannotationsfromabcimportABC,abstractmethodfromdataclassesimportdataclassfromtypesimportTracebackTypefromtypingimportList,Optional,TypefrompydanticimportBaseModelfromtyping_extensionsimportSelffrom.._cancellation_tokenimportCancellationTokenfrom.._component_configimportComponentBase
[docs]@dataclassclassCodeBlock:"""A code block extracted fromm an agent message."""code:strlanguage:str
[docs]@dataclassclassCodeResult:"""Result of a code execution."""exit_code:intoutput:str
[docs]classCodeExecutor(ABC,ComponentBase[BaseModel]):"""Executes code blocks and returns the result. This is an abstract base class for code executors. It defines the interface for executing code blocks and returning the result. A concrete implementation of this class should be provided to execute code blocks in a specific environment. For example, :class:`~autogen_ext.code_executors.docker.DockerCommandLineCodeExecutor` executes code blocks in a command line environment in a Docker container. It is recommended for subclass to be used as a context manager to ensure that resources are cleaned up properly. To do this, implement the :meth:`~autogen_core.code_executor.CodeExecutor.start` and :meth:`~autogen_core.code_executor.CodeExecutor.stop` methods that will be called when entering and exiting the context manager. """component_type="code_executor"
[docs]@abstractmethodasyncdefexecute_code_blocks(self,code_blocks:List[CodeBlock],cancellation_token:CancellationToken)->CodeResult:"""Execute code blocks and return the result. This method should be implemented by the code executor. Args: code_blocks (List[CodeBlock]): The code blocks to execute. Returns: CodeResult: The result of the code execution. Raises: ValueError: Errors in user inputs asyncio.TimeoutError: Code execution timeouts asyncio.CancelledError: CancellationToken evoked during execution """...
[docs]@abstractmethodasyncdefstart(self)->None:"""Start the code executor."""...
[docs]@abstractmethodasyncdefstop(self)->None:"""Stop the code executor and release any resources."""...
[docs]@abstractmethodasyncdefrestart(self)->None:"""Restart the code executor. This method should be implemented by the code executor. This method is called when the agent is reset. """...