From 9973011be5b025f6f59251b429bc0336e0dd9163 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Wed, 21 May 2025 03:30:20 +0000 Subject: [PATCH] feat: Add date_format parameter and error handling to inject_date feature Co-Authored-By: Joe Moura --- docs/usage_examples/inject_date_example.py | 18 +++++- src/crewai/agent.py | 14 ++++- tests/test_agent_inject_date.py | 67 ++++++++++++++++++++-- 3 files changed, 91 insertions(+), 8 deletions(-) diff --git a/docs/usage_examples/inject_date_example.py b/docs/usage_examples/inject_date_example.py index 50c73711d..16ec6f963 100644 --- a/docs/usage_examples/inject_date_example.py +++ b/docs/usage_examples/inject_date_example.py @@ -7,15 +7,29 @@ agent = Agent( inject_date=True, # Enable automatic date injection ) +agent_custom_format = Agent( + role="financial_analyst", + goal="Provide financial insights with proper date context", + backstory="You are a financial analyst who needs precise date formatting.", + inject_date=True, + date_format="%B %d, %Y", # Format as "May 21, 2025" +) + task = Task( description="Research market trends and provide analysis", expected_output="A comprehensive report on current market trends", agent=agent, ) +task_custom = Task( + description="Analyze financial data and provide insights", + expected_output="A detailed financial analysis report", + agent=agent_custom_format, +) + crew = Crew( - agents=[agent], - tasks=[task], + agents=[agent, agent_custom_format], + tasks=[task, task_custom], ) result = crew.kickoff() diff --git a/src/crewai/agent.py b/src/crewai/agent.py index f3d5a829f..8cb47706c 100644 --- a/src/crewai/agent.py +++ b/src/crewai/agent.py @@ -119,6 +119,10 @@ class Agent(BaseAgent): default=False, description="Whether to automatically inject the current date into tasks.", ) + date_format: str = Field( + default="%Y-%m-%d", + description="Format string for date when inject_date is enabled.", + ) code_execution_mode: Literal["safe", "unsafe"] = Field( default="safe", description="Mode for code execution: 'safe' (using Docker) or 'unsafe' (direct execution).", @@ -254,8 +258,14 @@ class Agent(BaseAgent): if self.inject_date: from datetime import datetime - current_date = datetime.now().strftime("%Y-%m-%d") - task.description += f"\n\nCurrent Date: {current_date}" + try: + current_date: str = datetime.now().strftime(self.date_format) + task.description += f"\n\nCurrent Date: {current_date}" + except Exception as e: + if hasattr(self, '_logger'): + self._logger.log("warning", f"Failed to inject date: {str(e)}") + else: + print(f"Warning: Failed to inject date: {str(e)}") if self.tools_handler: self.tools_handler.last_used_tool = {} # type: ignore # Incompatible types in assignment (expression has type "dict[Never, Never]", variable has type "ToolCalling") diff --git a/tests/test_agent_inject_date.py b/tests/test_agent_inject_date.py index f8e9d32ea..fd067efb5 100644 --- a/tests/test_agent_inject_date.py +++ b/tests/test_agent_inject_date.py @@ -1,13 +1,15 @@ -import re from datetime import datetime -from unittest.mock import patch, MagicMock +from unittest.mock import patch from crewai.agent import Agent from crewai.task import Task def test_agent_inject_date(): - """Test that the inject_date flag injects the current date into the task.""" + """Test that the inject_date flag injects the current date into the task. + + Tests that when inject_date=True, the current date is added to the task description. + """ agent = Agent( role="test_agent", goal="test_goal", @@ -31,7 +33,10 @@ def test_agent_inject_date(): def test_agent_without_inject_date(): - """Test that without inject_date flag, no date is injected.""" + """Test that without inject_date flag, no date is injected. + + Tests that when inject_date=False (default), no date is added to the task description. + """ agent = Agent( role="test_agent", goal="test_goal", @@ -53,3 +58,57 @@ def test_agent_without_inject_date(): assert "Current Date:" not in called_task.description assert called_task.description == original_description + + +def test_agent_inject_date_custom_format(): + """Test that the inject_date flag with custom date_format works correctly. + + Tests that when inject_date=True with a custom date_format, the date is formatted correctly. + """ + agent = Agent( + role="test_agent", + goal="test_goal", + backstory="test_backstory", + inject_date=True, + date_format="%d/%m/%Y", + ) + + task = Task( + description="Test task", + expected_output="Test output", + agent=agent, + ) + + with patch.object(Agent, 'execute_task', return_value="Task executed") as mock_execute: + agent.execute_task(task) + + called_task = mock_execute.call_args[0][0] + + current_date = datetime.now().strftime("%d/%m/%Y") + assert f"Current Date: {current_date}" in called_task.description + + +def test_agent_inject_date_invalid_format(): + """Test error handling with invalid date format. + + Tests that when an invalid date_format is provided, the task description remains unchanged. + """ + agent = Agent( + role="test_agent", + goal="test_goal", + backstory="test_backstory", + inject_date=True, + date_format="invalid", + ) + + task = Task( + description="Test task", + expected_output="Test output", + agent=agent, + ) + + original_description = task.description + + agent.execute_task(task) + + assert task.description == original_description