refactor: Improve feedback handling code quality

- Use _format_msg consistently for feedback messages
- Add comprehensive type hints
- Improve method documentation
- Add error handling for feedback processing

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-02-12 17:33:50 +00:00
parent 537aac0785
commit b7d8fadfad

View File

@@ -525,15 +525,29 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
return improved_answer return improved_answer
def _handle_regular_feedback( def _handle_regular_feedback(
self, current_answer: AgentFinish, initial_feedback: str self,
current_answer: AgentFinish,
initial_feedback: str
) -> AgentFinish: ) -> AgentFinish:
"""Process feedback for regular use with potential multiple iterations.""" """Process feedback for regular use with potential multiple iterations.
Args:
current_answer (AgentFinish): The current answer from the agent
initial_feedback (str): The initial feedback from the user
Returns:
AgentFinish: The final answer after processing all feedback iterations
"""
try:
feedback = initial_feedback feedback = initial_feedback
answer = current_answer answer = current_answer
while self.ask_for_human_input: while self.ask_for_human_input:
# Add feedback message with user role # Add feedback message with user role using standard formatter
self.messages.append({"role": "user", "content": f"Feedback: {feedback}"}) self.messages.append(self._format_msg(
f"Feedback: {feedback}",
role="user"
))
response = self._get_llm_feedback_response(feedback) response = self._get_llm_feedback_response(feedback)
if not self._feedback_requires_changes(response): if not self._feedback_requires_changes(response):
@@ -543,6 +557,12 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
feedback = self._ask_human_input(answer.output) feedback = self._ask_human_input(answer.output)
return answer return answer
except Exception as e:
self._printer.print(
content=f"Error processing feedback: {str(e)}",
color="red"
)
raise
def _get_llm_feedback_response(self, feedback: str) -> Optional[str]: def _get_llm_feedback_response(self, feedback: str) -> Optional[str]:
"""Get LLM classification of whether feedback requires changes.""" """Get LLM classification of whether feedback requires changes."""
@@ -566,7 +586,15 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
return response == "true" if response else False return response == "true" if response else False
def _process_feedback_iteration(self, feedback: str) -> AgentFinish: def _process_feedback_iteration(self, feedback: str) -> AgentFinish:
"""Process a single feedback iteration.""" """Process a single feedback iteration.
Args:
feedback (str): The feedback to process from the user
Returns:
AgentFinish: The processed agent response after incorporating feedback
"""
try:
# Add feedback instructions with user role # Add feedback instructions with user role
self.messages.append( self.messages.append(
self._format_msg( self._format_msg(
@@ -575,6 +603,12 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
) )
) )
return self._invoke_loop() return self._invoke_loop()
except Exception as e:
self._printer.print(
content=f"Error processing feedback iteration: {str(e)}",
color="red"
)
raise
def _log_feedback_error(self, retry_count: int, error: Exception) -> None: def _log_feedback_error(self, retry_count: int, error: Exception) -> None:
"""Log feedback processing errors.""" """Log feedback processing errors."""