mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-10 00:28:31 +00:00
cleanign state agent
This commit is contained in:
@@ -23,24 +23,14 @@ class AgentState(BaseModel):
|
||||
"""
|
||||
|
||||
# Core fields
|
||||
completed: bool = Field(
|
||||
default=False,
|
||||
description="Whether the current task is finished"
|
||||
)
|
||||
|
||||
original_plan: List[str] = Field(
|
||||
default_factory=list,
|
||||
description="The initial plan from first reasoning pass. Never overwrite unless user requests complete replan"
|
||||
)
|
||||
|
||||
last_plan: List[str] = Field(
|
||||
default_factory=list,
|
||||
description="The most recent plan (original or mid-execution update)"
|
||||
)
|
||||
|
||||
acceptance_criteria: List[str] = Field(
|
||||
default_factory=list,
|
||||
description="Concrete goals to satisfy before marking completed=true"
|
||||
description="Concrete goals to satisfy for task completion"
|
||||
)
|
||||
|
||||
scratchpad: Dict[str, Any] = Field(
|
||||
@@ -74,16 +64,10 @@ class AgentState(BaseModel):
|
||||
description="Number of execution steps completed"
|
||||
)
|
||||
|
||||
def update_last_plan(self, new_plan: List[str]) -> None:
|
||||
"""Update the last plan and timestamp."""
|
||||
self.last_plan = new_plan
|
||||
self.last_updated = datetime.now()
|
||||
|
||||
def set_original_plan(self, plan: List[str]) -> None:
|
||||
"""Set the original plan (only if not already set)."""
|
||||
if not self.original_plan:
|
||||
self.original_plan = plan
|
||||
self.last_plan = plan
|
||||
self.last_updated = datetime.now()
|
||||
|
||||
def add_to_scratchpad(self, key: str, value: Any) -> None:
|
||||
@@ -132,16 +116,9 @@ class AgentState(BaseModel):
|
||||
self.steps_completed += 1
|
||||
self.last_updated = datetime.now()
|
||||
|
||||
def mark_completed(self) -> None:
|
||||
"""Mark the task as completed."""
|
||||
self.completed = True
|
||||
self.last_updated = datetime.now()
|
||||
|
||||
def reset(self, task_id: Optional[str] = None) -> None:
|
||||
"""Reset state for a new task."""
|
||||
self.completed = False
|
||||
self.original_plan = []
|
||||
self.last_plan = []
|
||||
self.acceptance_criteria = []
|
||||
self.scratchpad = {}
|
||||
self.tool_usage_history = []
|
||||
@@ -154,16 +131,15 @@ class AgentState(BaseModel):
|
||||
"""Generate a concise string representation for LLM context."""
|
||||
context = f"Current State (Step {self.steps_completed}):\n"
|
||||
context += f"- Task ID: {self.task_id}\n"
|
||||
context += f"- Completed: {self.completed}\n"
|
||||
|
||||
if self.acceptance_criteria:
|
||||
context += "- Acceptance Criteria:\n"
|
||||
for criterion in self.acceptance_criteria:
|
||||
context += f" • {criterion}\n"
|
||||
|
||||
if self.last_plan:
|
||||
context += "- Current Plan:\n"
|
||||
for i, step in enumerate(self.last_plan, 1):
|
||||
if self.original_plan:
|
||||
context += "- Plan:\n"
|
||||
for i, step in enumerate(self.original_plan, 1):
|
||||
context += f" {i}. {step}\n"
|
||||
|
||||
if self.tool_usage_history:
|
||||
|
||||
@@ -138,9 +138,6 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
|
||||
if self.ask_for_human_input:
|
||||
formatted_answer = self._handle_human_feedback(formatted_answer)
|
||||
|
||||
# Mark task as completed in agent state
|
||||
self.agent_state.mark_completed()
|
||||
|
||||
self._create_short_term_memory(formatted_answer)
|
||||
self._create_long_term_memory(formatted_answer)
|
||||
self._create_external_memory(formatted_answer)
|
||||
@@ -312,6 +309,7 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
|
||||
f"Agent State:\n"
|
||||
f"Raw: {self.agent_state.model_dump_json()}\n"
|
||||
f"[AGENT STATE] Step {self.agent_state.steps_completed}:\n"
|
||||
f"Original Plan: {getattr(self.agent_state, 'original_plan', None)}\n"
|
||||
f"Tool: {formatted_answer.tool}\n"
|
||||
f"Scratchpad: {self.agent_state.scratchpad}\n"
|
||||
f"Tool History: {len(self.agent_state.tool_usage_history)} entries\n"
|
||||
@@ -749,12 +747,8 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
|
||||
iteration_messages=self.messages,
|
||||
)
|
||||
|
||||
# Update agent state with new plan if available
|
||||
# Update acceptance criteria if they changed from the reasoning output
|
||||
if reasoning_output.plan.structured_plan:
|
||||
self.agent_state.update_last_plan(
|
||||
reasoning_output.plan.structured_plan.steps
|
||||
)
|
||||
# Update acceptance criteria if they changed
|
||||
if reasoning_output.plan.structured_plan.acceptance_criteria:
|
||||
self.agent_state.acceptance_criteria = (
|
||||
reasoning_output.plan.structured_plan.acceptance_criteria
|
||||
|
||||
Reference in New Issue
Block a user