@@ -60,6 +60,7 @@ def __init__(
6060 self .integration_events : dict [str , list [Any ]] = {
6161 i .type (): i .events () for i in integrations
6262 }
63+ self ._integration_clients : dict [str , Any ] = {}
6364
6465 def _reset_cycle_tracking (self ) -> None :
6566 self .event_this_cycle = False
@@ -199,7 +200,7 @@ async def _emit_to_integrations(self, event: LoopEvent) -> None:
199200 },
200201 )
201202
202- await self .integrations [integration_type ].emit (event )
203+ await self .integrations [integration_type ].emit (event , self )
203204
204205 async def set (self , key : str , value : Any , local : bool = False ) -> None :
205206 if not local :
@@ -231,6 +232,28 @@ async def delete(self, key: str, local: bool = False) -> None:
231232 async def get_event_history (self ) -> list [dict [str , Any ]]:
232233 return await self .state_manager .get_event_history (self .loop_id )
233234
235+ def set_integration_client (self , integration_type : str , client : Any ) -> None :
236+ self ._integration_clients [integration_type ] = client
237+
238+ def get_integration_client (self , integration_type : str ) -> Any | None :
239+ return self ._integration_clients .get (integration_type )
240+
241+ async def setup_integrations (self , event : LoopEvent | None = None ) -> None :
242+ if not self .integrations :
243+ return
244+
245+ event_to_use = event or self .initial_event
246+ if not event_to_use :
247+ logger .warning (
248+ "Cannot setup integrations: no event available" ,
249+ extra = {"loop_id" : self .loop_id },
250+ )
251+ return
252+
253+ for integration in self .integrations .values ():
254+ if hasattr (integration , "setup_for_context" ):
255+ await integration .setup_for_context (self , event_to_use )
256+
234257 @property
235258 def should_stop (self ) -> bool :
236259 """Check if the loop should stop."""
0 commit comments