Skip to content

Commit

Permalink
fixing bug to multiple crews on yaml format in the same project
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomdmoura committed Jun 18, 2024
1 parent 095015d commit e6445af
Showing 1 changed file with 45 additions and 24 deletions.
69 changes: 45 additions & 24 deletions src/crewai/project/annotations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
tasks_order = []


def memoize(func):
cache = {}

Expand All @@ -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):
Expand All @@ -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
Expand Down

0 comments on commit e6445af

Please sign in to comment.