Skip to content

Commit

Permalink
fix tasks and agents ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomdmoura committed Oct 18, 2024
1 parent d72ebb9 commit 6fa2b89
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
26 changes: 5 additions & 21 deletions src/crewai/project/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,13 @@ def wrapper(self, *args, **kwargs) -> Crew:
instantiated_agents = []
agent_roles = set()

# Collect methods from crew instance (not class)
all_functions = [
(name, getattr(self, name))
for name in dir(self)
if callable(getattr(self, name)) and not name.startswith("__")
]

# Filter tasks and agents
tasks = [
(name, method)
for name, method in all_functions
if hasattr(method, "is_task") and method.is_task
]

agents = [
(name, method)
for name, method in all_functions
if hasattr(method, "is_agent") and method.is_agent
]
# Use the preserved task and agent information
tasks = self._original_tasks.items()
agents = self._original_agents.items()

# Instantiate tasks in order
for task_name, task_method in tasks:
task_instance = task_method()
task_instance = task_method(self)
instantiated_tasks.append(task_instance)
agent_instance = getattr(task_instance, "agent", None)
if agent_instance and agent_instance.role not in agent_roles:
Expand All @@ -107,7 +91,7 @@ def wrapper(self, *args, **kwargs) -> Crew:

# Instantiate agents not included by tasks
for agent_name, agent_method in agents:
agent_instance = agent_method()
agent_instance = agent_method(self)
if agent_instance.role not in agent_roles:
instantiated_agents.append(agent_instance)
agent_roles.add(agent_instance.role)
Expand Down
12 changes: 12 additions & 0 deletions src/crewai/project/crew_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ def __init__(self, *args, **kwargs):
self.map_all_agent_variables()
self.map_all_task_variables()

# Preserve task and agent information
self._original_tasks = {
name: method
for name, method in cls.__dict__.items()
if hasattr(method, "is_task") and method.is_task
}
self._original_agents = {
name: method
for name, method in cls.__dict__.items()
if hasattr(method, "is_agent") and method.is_agent
}

@staticmethod
def load_yaml(config_path: Path):
try:
Expand Down

0 comments on commit 6fa2b89

Please sign in to comment.