@@ -2199,23 +2199,41 @@ async def _post_event(
21992199 "Content-Type" : "application/json" ,
22002200 "Idempotency-Key" : request_id ,
22012201 }
2202- active_client = client or self ._client
2203- if active_client is None :
2202+ configured_client = client or self ._client
2203+ if configured_client is None or configured_client . closed :
22042204 return False , {}, "T3 Agent bridge is not connected"
2205- try :
2205+
2206+ async def post_event (
2207+ active_client : ClientSession ,
2208+ ) -> Tuple [Optional [Dict [str , Any ]], Optional [str ]]:
22062209 async with active_client .post (target , json = frame , headers = headers ) as response :
22072210 if not 200 <= response .status < 300 :
2208- return False , {} , f"T3 Agent bridge returned HTTP { response .status } "
2211+ return None , f"T3 Agent bridge returned HTTP { response .status } "
22092212 try :
2210- body = await response .json ()
2213+ response_body = await response .json ()
22112214 except Exception :
2212- return False , {}, "T3 Agent bridge returned invalid JSON"
2215+ return None , "T3 Agent bridge returned invalid JSON"
2216+ if not isinstance (response_body , dict ):
2217+ return None , "T3 Agent bridge returned invalid JSON"
2218+ return response_body , None
2219+
2220+ try :
2221+ client_loop = getattr (configured_client , "_loop" , None )
2222+ if client_loop is asyncio .get_running_loop ():
2223+ body , request_error = await post_event (configured_client )
2224+ else :
2225+ # Hermes cron delivery can call the connected adapter from its
2226+ # scheduler loop. aiohttp sessions are bound to the loop where
2227+ # they were created, so use a request-local session here.
2228+ timeout = ClientTimeout (total = self .timeout_seconds )
2229+ async with ClientSession (timeout = timeout ) as active_client :
2230+ body , request_error = await post_event (active_client )
22132231 except asyncio .CancelledError :
22142232 raise
22152233 except (ClientError , asyncio .TimeoutError ):
22162234 return False , {}, "T3 Agent bridge request failed"
2217- if not isinstance ( body , dict ) :
2218- return False , {}, "T3 Agent bridge returned invalid JSON "
2235+ if body is None :
2236+ return False , {}, request_error or "T3 Agent bridge request failed "
22192237 if body .get ("protocolVersion" ) != PROTOCOL_VERSION :
22202238 return False , body , "T3 Agent bridge returned a mismatched protocol version"
22212239 if body .get ("requestId" ) != request_id :
0 commit comments