| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  | import time | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  | from typing import AsyncGenerator, Callable, Optional, Union | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from autogen_agentchat.base import TaskResult | 
					
						
							| 
									
										
										
										
											2024-12-18 14:09:19 -08:00
										 |  |  | from autogen_agentchat.messages import AgentEvent, ChatMessage | 
					
						
							| 
									
										
										
										
											2024-12-03 17:00:44 -08:00
										 |  |  | from autogen_core import CancellationToken | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  | from .database import Component, ComponentFactory | 
					
						
							|  |  |  | from .datamodel import ComponentConfigInput, TeamResult | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class TeamManager: | 
					
						
							|  |  |  |     def __init__(self) -> None: | 
					
						
							|  |  |  |         self.component_factory = ComponentFactory() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  |     async def _create_team(self, team_config: ComponentConfigInput, input_func: Optional[Callable] = None) -> Component: | 
					
						
							| 
									
										
										
										
											2024-11-15 14:51:57 -08:00
										 |  |  |         """Create team instance with common setup logic""" | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  |         return await self.component_factory.load(team_config, input_func=input_func) | 
					
						
							| 
									
										
										
										
											2024-11-15 14:51:57 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _create_result(self, task_result: TaskResult, start_time: float) -> TeamResult: | 
					
						
							|  |  |  |         """Create TeamResult with timing info""" | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  |         return TeamResult(task_result=task_result, usage="", duration=time.time() - start_time) | 
					
						
							| 
									
										
										
										
											2024-11-15 14:51:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  |     async def run_stream( | 
					
						
							|  |  |  |         self, | 
					
						
							|  |  |  |         task: str, | 
					
						
							|  |  |  |         team_config: ComponentConfigInput, | 
					
						
							| 
									
										
										
										
											2024-11-15 14:51:57 -08:00
										 |  |  |         input_func: Optional[Callable] = None, | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  |         cancellation_token: Optional[CancellationToken] = None, | 
					
						
							| 
									
										
										
										
											2024-12-18 14:09:19 -08:00
										 |  |  |     ) -> AsyncGenerator[Union[AgentEvent | ChatMessage, ChatMessage, TaskResult], None]: | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  |         """Stream the team's execution results""" | 
					
						
							|  |  |  |         start_time = time.time() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-11-15 14:51:57 -08:00
										 |  |  |             team = await self._create_team(team_config, input_func) | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  |             stream = team.run_stream(task=task, cancellation_token=cancellation_token) | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             async for message in stream: | 
					
						
							|  |  |  |                 if cancellation_token and cancellation_token.is_cancelled(): | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if isinstance(message, TaskResult): | 
					
						
							| 
									
										
										
										
											2024-11-15 14:51:57 -08:00
										 |  |  |                     yield self._create_result(message, start_time) | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  |                 else: | 
					
						
							|  |  |  |                     yield message | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-16 18:51:52 -08:00
										 |  |  |             # close agent resources | 
					
						
							|  |  |  |             for agent in team._participants: | 
					
						
							|  |  |  |                 if hasattr(agent, "close"): | 
					
						
							|  |  |  |                     await agent.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  |         except Exception as e: | 
					
						
							|  |  |  |             raise e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async def run( | 
					
						
							|  |  |  |         self, | 
					
						
							|  |  |  |         task: str, | 
					
						
							|  |  |  |         team_config: ComponentConfigInput, | 
					
						
							| 
									
										
										
										
											2024-11-15 14:51:57 -08:00
										 |  |  |         input_func: Optional[Callable] = None, | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  |         cancellation_token: Optional[CancellationToken] = None, | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  |     ) -> TeamResult: | 
					
						
							|  |  |  |         """Original non-streaming run method with optional cancellation""" | 
					
						
							|  |  |  |         start_time = time.time() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 14:51:57 -08:00
										 |  |  |         team = await self._create_team(team_config, input_func) | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  |         result = await team.run(task=task, cancellation_token=cancellation_token) | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-16 18:51:52 -08:00
										 |  |  |         # close agent resources | 
					
						
							|  |  |  |         for agent in team._participants: | 
					
						
							|  |  |  |             if hasattr(agent, "close"): | 
					
						
							|  |  |  |                 await agent.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 14:51:57 -08:00
										 |  |  |         return self._create_result(result, start_time) |