fix tasks and agents ordering

This commit is contained in:
João Moura
2024-10-18 08:06:38 -03:00
parent c00e714460
commit 7948e62fd5
2 changed files with 17 additions and 21 deletions

View File

@@ -76,29 +76,13 @@ def crew(func) -> Callable[..., Crew]:
instantiated_agents = [] instantiated_agents = []
agent_roles = set() agent_roles = set()
# Collect methods from crew instance (not class) # Use the preserved task and agent information
all_functions = [ tasks = self._original_tasks.items()
(name, getattr(self, name)) agents = self._original_agents.items()
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
]
# Instantiate tasks in order # Instantiate tasks in order
for task_name, task_method in tasks: for task_name, task_method in tasks:
task_instance = task_method() task_instance = task_method(self)
instantiated_tasks.append(task_instance) instantiated_tasks.append(task_instance)
agent_instance = getattr(task_instance, "agent", None) agent_instance = getattr(task_instance, "agent", None)
if agent_instance and agent_instance.role not in agent_roles: if agent_instance and agent_instance.role not in agent_roles:
@@ -107,7 +91,7 @@ def crew(func) -> Callable[..., Crew]:
# Instantiate agents not included by tasks # Instantiate agents not included by tasks
for agent_name, agent_method in agents: for agent_name, agent_method in agents:
agent_instance = agent_method() agent_instance = agent_method(self)
if agent_instance.role not in agent_roles: if agent_instance.role not in agent_roles:
instantiated_agents.append(agent_instance) instantiated_agents.append(agent_instance)
agent_roles.add(agent_instance.role) agent_roles.add(agent_instance.role)

View File

@@ -34,6 +34,18 @@ def CrewBase(cls: T) -> T:
self.map_all_agent_variables() self.map_all_agent_variables()
self.map_all_task_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 @staticmethod
def load_yaml(config_path: Path): def load_yaml(config_path: Path):
try: try: