fix tasks and agents ordering

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

View File

@@ -76,29 +76,13 @@ def crew(func) -> Callable[..., 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:
@@ -107,7 +91,7 @@ def crew(func) -> Callable[..., 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)

View File

@@ -34,6 +34,18 @@ def CrewBase(cls: T) -> T:
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: