mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-10 08:38:30 +00:00
adding before and after crew
This commit is contained in:
@@ -34,18 +34,39 @@ def CrewBase(cls: T) -> T:
|
||||
self.map_all_agent_variables()
|
||||
self.map_all_task_variables()
|
||||
|
||||
# Preserve task and agent information
|
||||
self._original_tasks = {
|
||||
# Preserve all decorated functions
|
||||
self._original_functions = {
|
||||
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
|
||||
if any(
|
||||
hasattr(method, attr)
|
||||
for attr in [
|
||||
"is_task",
|
||||
"is_agent",
|
||||
"is_before_crew",
|
||||
"is_after_crew",
|
||||
"is_kickoff",
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
# Store specific function types
|
||||
self._original_tasks = self._filter_functions(
|
||||
self._original_functions, "is_task"
|
||||
)
|
||||
self._original_agents = self._filter_functions(
|
||||
self._original_functions, "is_agent"
|
||||
)
|
||||
self._before_crew = self._filter_functions(
|
||||
self._original_functions, "is_before_crew"
|
||||
)
|
||||
self._after_crew = self._filter_functions(
|
||||
self._original_functions, "is_after_crew"
|
||||
)
|
||||
self._kickoff = self._filter_functions(
|
||||
self._original_functions, "is_kickoff"
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def load_yaml(config_path: Path):
|
||||
try:
|
||||
@@ -192,4 +213,25 @@ def CrewBase(cls: T) -> T:
|
||||
callback_functions[callback]() for callback in callbacks
|
||||
]
|
||||
|
||||
def kickoff(self, inputs=None):
|
||||
# Execute before_crew functions and allow them to modify inputs
|
||||
for _, func in self._before_crew.items():
|
||||
modified_inputs = func(self, inputs)
|
||||
if modified_inputs is not None:
|
||||
inputs = modified_inputs
|
||||
|
||||
# Get the crew instance
|
||||
crew_instance = self.crew()
|
||||
|
||||
# Execute the crew's tasks
|
||||
result = crew_instance.kickoff(inputs=inputs)
|
||||
|
||||
# Execute after_crew functions and allow them to modify the output
|
||||
for _, func in self._after_crew.items():
|
||||
modified_result = func(self, result)
|
||||
if modified_result is not None:
|
||||
result = modified_result
|
||||
|
||||
return result
|
||||
|
||||
return cast(T, WrappedClass)
|
||||
|
||||
Reference in New Issue
Block a user