From e6445afac519036a8018587c663c201232c1be86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Moura?= Date: Tue, 18 Jun 2024 02:32:24 -0300 Subject: [PATCH] fixing bug to multiple crews on yaml format in the same project --- src/crewai/project/annotations.py | 69 ++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/src/crewai/project/annotations.py b/src/crewai/project/annotations.py index e402b8cfeb..31c9df03ad 100644 --- a/src/crewai/project/annotations.py +++ b/src/crewai/project/annotations.py @@ -1,6 +1,3 @@ -tasks_order = [] - - def memoize(func): cache = {} @@ -10,14 +7,21 @@ def memoized_func(*args, **kwargs): cache[key] = func(*args, **kwargs) return cache[key] + memoized_func.__dict__.update(func.__dict__) return memoized_func def task(func): + if not hasattr(task, "registration_order"): + task.registration_order = [] + func.is_task = True - tasks_order.append(func.__name__) - func = memoize(func) - return func + wrapped_func = memoize(func) + + # Append the function name to the registration order list + task.registration_order.append(func.__name__) + + return wrapped_func def agent(func): @@ -32,26 +36,43 @@ def wrapper(self, *args, **kwargs): instantiated_agents = [] agent_roles = set() - # Iterate over tasks_order to maintain the defined order - for task_name in tasks_order: - possible_task = getattr(self, task_name) - if callable(possible_task): - task_instance = possible_task() - instantiated_tasks.append(task_instance) - if hasattr(task_instance, "agent"): - agent_instance = task_instance.agent - if agent_instance.role not in agent_roles: - instantiated_agents.append(agent_instance) - agent_roles.add(agent_instance.role) + all_functions = { + name: getattr(self, name) + for name in dir(self) + if callable(getattr(self, name)) + } + tasks = { + name: func + for name, func in all_functions.items() + if hasattr(func, "is_task") + } + agents = { + name: func + for name, func in all_functions.items() + if hasattr(func, "is_agent") + } + + # Sort tasks by their registration order + sorted_task_names = sorted( + tasks, key=lambda name: task.registration_order.index(name) + ) + + # Instantiate tasks in the order they were defined + for task_name in sorted_task_names: + task_instance = tasks[task_name]() + instantiated_tasks.append(task_instance) + if hasattr(task_instance, "agent"): + agent_instance = task_instance.agent + if agent_instance.role not in agent_roles: + instantiated_agents.append(agent_instance) + agent_roles.add(agent_instance.role) # Instantiate any additional agents not already included by tasks - for attr_name in dir(self): - possible_agent = getattr(self, attr_name) - if callable(possible_agent) and hasattr(possible_agent, "is_agent"): - temp_agent_instance = possible_agent() - if temp_agent_instance.role not in agent_roles: - instantiated_agents.append(temp_agent_instance) - agent_roles.add(temp_agent_instance.role) + for agent_name in agents: + temp_agent_instance = agents[agent_name]() + if temp_agent_instance.role not in agent_roles: + instantiated_agents.append(temp_agent_instance) + agent_roles.add(temp_agent_instance.role) self.agents = instantiated_agents self.tasks = instantiated_tasks