Address PR feedback: Add validation, refactor code duplication, enhance tests

- Add regex pattern validation for execution_image parameter
- Refactor get_code_execution_tools() to eliminate code duplication using tool_kwargs
- Add comprehensive tests for invalid Docker image format validation
- Add tests for various valid Docker image formats
- Update documentation with best practices section for custom Docker images
- Fix lint issues: remove unused imports and variables

Addresses feedback from joaomdmoura in PR #2934

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2025-06-02 18:23:11 +00:00
parent 229bbd9bbe
commit 45404537fd
4 changed files with 69 additions and 9 deletions

View File

@@ -130,6 +130,7 @@ class Agent(BaseAgent):
execution_image: Optional[str] = Field(
default=None,
description="Custom Docker image to use for code execution. If not specified, uses the default image.",
pattern=r"^[a-zA-Z0-9._/-]+(?::[a-zA-Z0-9._-]+)?$",
)
reasoning: bool = Field(
default=False,
@@ -569,10 +570,10 @@ class Agent(BaseAgent):
# Set the unsafe_mode based on the code_execution_mode attribute
unsafe_mode = self.code_execution_mode == "unsafe"
tool_kwargs = {"unsafe_mode": unsafe_mode}
if self.execution_image:
return [CodeInterpreterTool(unsafe_mode=unsafe_mode, default_image_tag=self.execution_image)]
else:
return [CodeInterpreterTool(unsafe_mode=unsafe_mode)]
tool_kwargs["default_image_tag"] = self.execution_image
return [CodeInterpreterTool(**tool_kwargs)]
except ModuleNotFoundError:
self._logger.log(
"info", "Coding tools not available. Install crewai_tools. "