@@ -318,7 +318,7 @@ def send_broadcast_to_agents(payload_obj, urls_to_send, status_callback=None, ui
318318 return all_responses
319319
320320
321- def process_agent_responses (root , all_responses , floor_manager , update_conversation_history_callback , invited_agents = None , update_agent_textboxes_callback = None , extract_url_callback = None , manifest_cache = None , show_incoming_events : bool = False , directed_addressee = None , display_name_resolver = None ):
321+ def process_agent_responses (root , all_responses , floor_manager , update_conversation_history_callback , invited_agents = None , update_agent_textboxes_callback = None , extract_url_callback = None , manifest_cache = None , show_incoming_events : bool = False , directed_addressee = None , display_name_resolver = None , sync_conversant_callback = None ):
322322 """Phase 2: Process all responses and update conversation history.
323323
324324 Args:
@@ -360,6 +360,17 @@ def _url_from_speaker_uri(speaker_uri):
360360 normalized = _normalize_agent_id (key )
361361 if normalized :
362362 manifest_cache [normalized ] = assistantConversationalName
363+
364+ canonical_speaker_uri = manifest_speaker_uri or assistant_uri
365+ if sync_conversant_callback is not None :
366+ try :
367+ sync_conversant_callback (
368+ agent_url = manifest_service_url or target_url ,
369+ speaker_uri = canonical_speaker_uri ,
370+ conversational_name = assistantConversationalName ,
371+ )
372+ except Exception :
373+ pass
363374
364375 # Update agent info with conversational name
365376 if invited_agents is not None and extract_url_callback is not None :
@@ -376,14 +387,14 @@ def _url_from_speaker_uri(speaker_uri):
376387 break
377388
378389 # Add agent to floor manager if active
379- if floor_manager is not None and assistant_uri :
390+ if floor_manager is not None and canonical_speaker_uri :
380391 try :
381392 floor_manager .add_conversant (
382- speaker_uri = assistant_uri ,
393+ speaker_uri = canonical_speaker_uri ,
383394 service_url = manifest_service_url ,
384395 conversational_name = assistantConversationalName
385396 )
386- print (f"Added { assistantConversationalName or assistant_uri } to floor manager" )
397+ print (f"Added { assistantConversationalName or canonical_speaker_uri } to floor manager" )
387398 except Exception as e :
388399 print (f"Failed to add agent to floor manager: { e } " )
389400 else :
@@ -399,6 +410,15 @@ def _url_from_speaker_uri(speaker_uri):
399410 # Extract speaker info for conversation history
400411 speaker_uri = dialog_event .get ("speakerUri" , "Unknown" )
401412 print (f"[DEBUG] Processing utterance - speakerUri from dialogEvent: { speaker_uri } " )
413+
414+ if sync_conversant_callback is not None :
415+ try :
416+ sync_conversant_callback (
417+ agent_url = target_url ,
418+ speaker_uri = speaker_uri if speaker_uri != "Unknown" else None ,
419+ )
420+ except Exception :
421+ pass
402422
403423 # Get the conversational name for the actual speaker (from speakerUri in dialogEvent)
404424 speaker_conversational_name = None
@@ -484,7 +504,7 @@ def _url_from_speaker_uri(speaker_uri):
484504 )
485505
486506
487- def forward_responses_to_agents (all_responses , urls_to_send , global_conversation , update_conversation_history_callback , status_callback = None , ui_pump_callback = None , directed_addressee = None , display_name_resolver = None ):
507+ def forward_responses_to_agents (all_responses , urls_to_send , global_conversation , update_conversation_history_callback , status_callback = None , ui_pump_callback = None , directed_addressee = None , display_name_resolver = None , build_conversation_callback = None ):
488508 """Phase 3: Forward all responses to all other agents (after processing all initial responses).
489509
490510 Args:
@@ -502,7 +522,28 @@ def _url_from_speaker_uri(speaker_uri):
502522 return speaker_uri
503523 return None
504524
525+ def _current_conversation_state ():
526+ if build_conversation_callback is not None :
527+ try :
528+ return build_conversation_callback ()
529+ except Exception :
530+ pass
531+ return global_conversation
532+
533+ def _serialize_conversants (conversation_obj ):
534+ return [
535+ {
536+ "identification" : {
537+ "speakerUri" : c .identification .speakerUri ,
538+ "serviceUrl" : c .identification .serviceUrl ,
539+ "conversationalName" : c .identification .conversationalName
540+ }
541+ }
542+ for c in getattr (conversation_obj , "conversants" , []) or []
543+ ]
544+
505545 for target_url , response_data , original_sender , incoming_events in all_responses :
546+ current_conversation = _current_conversation_state ()
506547 print (f"\n === FORWARDING CHECK ===" )
507548 print (f"incoming_events count: { len (incoming_events )} " )
508549 print (f"urls_to_send: { urls_to_send } " )
@@ -533,17 +574,8 @@ def _url_from_speaker_uri(speaker_uri):
533574 forward_payload = {
534575 "openFloor" : {
535576 "conversation" : {
536- "id" : global_conversation .id ,
537- "conversants" : [
538- {
539- "identification" : {
540- "speakerUri" : c .identification .speakerUri ,
541- "serviceUrl" : c .identification .serviceUrl ,
542- "conversationalName" : c .identification .conversationalName
543- }
544- }
545- for c in global_conversation .conversants
546- ]
577+ "id" : current_conversation .id ,
578+ "conversants" : _serialize_conversants (current_conversation )
547579 },
548580 "sender" : original_sender , # Preserve original sender, not client
549581 "events" : broadcast_events # Forward events as broadcasts
@@ -559,7 +591,7 @@ def _url_from_speaker_uri(speaker_uri):
559591 except Exception :
560592 pass
561593 print (f"\n === FORWARDING TO { other_agent_url } ===" )
562- print (f"Conversation ID: { global_conversation .id } " )
594+ print (f"Conversation ID: { current_conversation .id } " )
563595 print (f"Number of broadcast events: { len (broadcast_events )} " )
564596 print (f"Broadcast events: { json .dumps (broadcast_events , indent = 2 )} " )
565597 forward_response = _post_with_optional_ui_pump (
@@ -610,15 +642,15 @@ def _url_from_speaker_uri(speaker_uri):
610642 # Find speaker name by matching serviceUrl
611643 speaker_name = None
612644 speaker_service_url = _url_from_speaker_uri (speaker_uri )
613- for c in global_conversation . conversants :
645+ for c in getattr ( current_conversation , " conversants" , []) or [] :
614646 if c .identification .speakerUri == speaker_uri :
615647 speaker_name = c .identification .conversationalName
616648 speaker_service_url = c .identification .serviceUrl or speaker_service_url
617649 break
618650
619651 # If not found by speakerUri, try by serviceUrl
620652 if not speaker_name :
621- for c in global_conversation . conversants :
653+ for c in getattr ( current_conversation , " conversants" , []) or [] :
622654 if c .identification .serviceUrl == other_agent_url :
623655 speaker_name = c .identification .conversationalName
624656 speaker_service_url = c .identification .serviceUrl or speaker_service_url
@@ -673,20 +705,12 @@ def _url_from_speaker_uri(speaker_uri):
673705 response_broadcast_events .append (evt_copy )
674706
675707 # Create payload for recursive forwarding
708+ recursive_conversation = _current_conversation_state ()
676709 recursive_payload = {
677710 "openFloor" : {
678711 "conversation" : {
679- "id" : global_conversation .id ,
680- "conversants" : [
681- {
682- "identification" : {
683- "speakerUri" : c .identification .speakerUri ,
684- "serviceUrl" : c .identification .serviceUrl ,
685- "conversationalName" : c .identification .conversationalName
686- }
687- }
688- for c in global_conversation .conversants
689- ]
712+ "id" : recursive_conversation .id ,
713+ "conversants" : _serialize_conversants (recursive_conversation )
690714 },
691715 "sender" : responding_agent_sender ,
692716 "events" : response_broadcast_events
0 commit comments