fixing bug to multiple crews on yaml format in the same project

This commit is contained in:
João Moura
2024-06-18 02:32:24 -03:00
parent 9a71e66a62
commit ff80f57816

View File

@@ -1,6 +1,3 @@
tasks_order = []
def memoize(func): def memoize(func):
cache = {} cache = {}
@@ -10,14 +7,21 @@ def memoize(func):
cache[key] = func(*args, **kwargs) cache[key] = func(*args, **kwargs)
return cache[key] return cache[key]
memoized_func.__dict__.update(func.__dict__)
return memoized_func return memoized_func
def task(func): def task(func):
if not hasattr(task, "registration_order"):
task.registration_order = []
func.is_task = True func.is_task = True
tasks_order.append(func.__name__) wrapped_func = memoize(func)
func = memoize(func)
return func # Append the function name to the registration order list
task.registration_order.append(func.__name__)
return wrapped_func
def agent(func): def agent(func):
@@ -32,26 +36,43 @@ def crew(func):
instantiated_agents = [] instantiated_agents = []
agent_roles = set() agent_roles = set()
# Iterate over tasks_order to maintain the defined order all_functions = {
for task_name in tasks_order: name: getattr(self, name)
possible_task = getattr(self, task_name) for name in dir(self)
if callable(possible_task): if callable(getattr(self, name))
task_instance = possible_task() }
instantiated_tasks.append(task_instance) tasks = {
if hasattr(task_instance, "agent"): name: func
agent_instance = task_instance.agent for name, func in all_functions.items()
if agent_instance.role not in agent_roles: if hasattr(func, "is_task")
instantiated_agents.append(agent_instance) }
agent_roles.add(agent_instance.role) 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 # Instantiate any additional agents not already included by tasks
for attr_name in dir(self): for agent_name in agents:
possible_agent = getattr(self, attr_name) temp_agent_instance = agents[agent_name]()
if callable(possible_agent) and hasattr(possible_agent, "is_agent"): if temp_agent_instance.role not in agent_roles:
temp_agent_instance = possible_agent() instantiated_agents.append(temp_agent_instance)
if temp_agent_instance.role not in agent_roles: agent_roles.add(temp_agent_instance.role)
instantiated_agents.append(temp_agent_instance)
agent_roles.add(temp_agent_instance.role)
self.agents = instantiated_agents self.agents = instantiated_agents
self.tasks = instantiated_tasks self.tasks = instantiated_tasks