mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-10 16:48:30 +00:00
Added in Thiago fix
This commit is contained in:
@@ -137,6 +137,12 @@ class Flow(Generic[T], metaclass=FlowMeta):
|
||||
_routers: Dict[str, str] = {}
|
||||
initial_state: Union[Type[T], T, None] = None
|
||||
|
||||
def __class_getitem__(cls, item):
|
||||
class _FlowGeneric(cls):
|
||||
_initial_state_T = item
|
||||
|
||||
return _FlowGeneric
|
||||
|
||||
def __init__(self):
|
||||
print("[Flow.__init__] Initializing Flow")
|
||||
self._methods: Dict[str, Callable] = {}
|
||||
@@ -152,6 +158,8 @@ class Flow(Generic[T], metaclass=FlowMeta):
|
||||
|
||||
def _create_initial_state(self) -> T:
|
||||
print("[Flow._create_initial_state] Creating initial state")
|
||||
if self.initial_state is None and hasattr(self, "_initial_state_T"):
|
||||
return self._initial_state_T() # type: ignore
|
||||
if self.initial_state is None:
|
||||
return {} # type: ignore
|
||||
elif isinstance(self.initial_state, type):
|
||||
|
||||
@@ -9,8 +9,7 @@ class ExampleState(BaseModel):
|
||||
message: str = ""
|
||||
|
||||
|
||||
class StructuredExampleFlow(Flow):
|
||||
initial_state = ExampleState
|
||||
class StructuredExampleFlow(Flow[ExampleState]):
|
||||
|
||||
@start()
|
||||
async def start_method(self):
|
||||
@@ -21,8 +20,7 @@ class StructuredExampleFlow(Flow):
|
||||
return "Start result"
|
||||
|
||||
@listen(start_method)
|
||||
async def second_method(self, result):
|
||||
print(f"Second method, received: {result}")
|
||||
async def second_method(self):
|
||||
print(f"State before increment: {self.state}")
|
||||
self.state.counter += 1
|
||||
self.state.message += " - updated"
|
||||
@@ -30,8 +28,7 @@ class StructuredExampleFlow(Flow):
|
||||
return "Second result"
|
||||
|
||||
@listen(start_method)
|
||||
async def third_method(self, result):
|
||||
print(f"Third method, received: {result}")
|
||||
async def third_method(self):
|
||||
print(f"State before increment: {self.state}")
|
||||
self.state.counter += 1
|
||||
self.state.message += " - updated"
|
||||
|
||||
@@ -10,7 +10,6 @@ class ExampleState(BaseModel):
|
||||
|
||||
|
||||
class StructuredExampleFlow(Flow[ExampleState]):
|
||||
initial_state = ExampleState
|
||||
|
||||
@start()
|
||||
async def start_method(self):
|
||||
|
||||
@@ -11,15 +11,15 @@ class FlexibleExampleFlow(Flow):
|
||||
return "Start result"
|
||||
|
||||
@listen(start_method)
|
||||
def second_method(self, result):
|
||||
print(f"Second method, received: {result}")
|
||||
def second_method(self):
|
||||
print("Second method")
|
||||
self.state["counter"] += 1
|
||||
self.state["message"] = "Hello from flexible flow"
|
||||
return "Second result"
|
||||
|
||||
@listen(second_method)
|
||||
def third_method(self, result):
|
||||
print(f"Third method, received: {result}")
|
||||
def third_method(self):
|
||||
print("Third method")
|
||||
print(f"Final counter value: {self.state["counter"]}")
|
||||
print(f"Final message: {self.state["message"]}")
|
||||
return "Third result"
|
||||
|
||||
Reference in New Issue
Block a user