44
55import  random 
66from  collections .abc  import  Callable 
7- from  typing  import  TYPE_CHECKING , Any , Optional 
7+ from  types  import  SimpleNamespace 
8+ from  typing  import  TYPE_CHECKING , Any , List , Optional 
89
910from  pydantic  import  BaseModel , Field 
1011
@@ -423,19 +424,44 @@ def create_wrapper_agent(self, parent_agent: "ConversableAgent", index: int) ->
423424
424425
425426if  TYPE_CHECKING :
426-     from  ..reply_result  import  ReplyResult 
427- 
428-     AfterworkFn  =  Callable [[ str ,  Any ],  "ReplyResult "
427+     from  ..function_target_result  import  FunctionTargetResult 
428+      from  .. function_target_result   import   FunctionTargetMessage 
429+     AfterworkFn  =  Callable [...,  "FunctionTargetResult "
429430else :
430-     AfterworkFn  =  Callable [[str , Any ], Any ]
431- 
431+     AfterworkFn  =  Callable [..., Any ]
432+ 
433+ def  broadcast (messages : List ["FunctionTargetMessage" ] |  str , group_chat , current_agent , fn_name , target , user_agent , * args ) ->  None :
434+     """Broadcast a message to a specific agent.""" 
435+     if  isinstance (messages , str ):
436+         if  hasattr (target , "agent_name" ):
437+             next_target  =  target .agent_name 
438+             for  agent  in  group_chat .agents :
439+                 if  agent .name  ==  next_target :
440+                     messages  =  [SimpleNamespace (content = messages , msg_target = agent )]
441+                     break 
442+         elif  isinstance (target , RevertToUserTarget ):
443+             messages  =  [SimpleNamespace (content = messages , msg_target = user_agent )]
444+         elif  isinstance (target , StayTarget ):
445+             messages  =  [SimpleNamespace (content = messages , msg_target = current_agent )]
446+     for  message  in  messages :
447+         content  =  message .content 
448+         broadcast  =  {
449+             "role" : "system" ,
450+             "name" : f"{ fn_name }  ,
451+             "content" : f"[FUNCTION_HANDOFF] - Reply from function { fn_name } \n \n  { content }  
452+         }
453+         current_agent ._group_manager .send (
454+             broadcast ,
455+             message .msg_target ,   
456+             request_reply = False ,
457+             silent = False ,
458+         )
432459
433460class  FunctionTarget (TransitionTarget ):
434461    """Transition target that invokes a tool function with (prev_output, context).""" 
435462
436463    fn_name : str  =  Field (...)
437464    fn : AfterworkFn  =  Field (..., repr = False )
438-     broadcast_recipients : list [str ] |  None  =  None 
439465
440466    def  __init__ (self , incoming_fn , ** kwargs ):
441467        # If the first arg is callable, auto-populate fn and fn_name 
@@ -446,7 +472,6 @@ def __init__(self, incoming_fn, **kwargs):
446472            raise  ValueError (
447473                "FunctionTarget must be initialized with a callable function as the first argument or 'fn' keyword argument." 
448474            )
449-     # --- TransitionTarget API --- 
450475    def  can_resolve_for_speaker_selection (self ) ->  bool :
451476        return  False 
452477
@@ -455,17 +480,13 @@ def resolve(self, *args, **kwargs) -> SpeakerSelectionResult:
455480        last_message  =  group_chat .messages [- 1 ]["content" ] if  group_chat .messages  else  "" 
456481        current_agent : ConversableAgent  =  args [1 ]
457482        ctx  =  current_agent .context_variables 
458- 
459-         # Define the signature of the function that can be called (parameters and return type) 
460-         # Create a message class and object for adding a message to the conversation 
461-         function_target_result  =  self .fn (group_chat , current_agent , last_message , ctx )
462- 
463-         # resolve_next_target = function_target_result.target.resolve(group_chat, args[1], args[2]) 
464- 
465-         # if function_target_result.message: 
466-         #     broadcast(function_target_result.message.content, function_target_result.message.target_agent) 
467- 
468-         return  function_target_result .target .resolve (group_chat , args [1 ], args [2 ])
483+         user_agent  =  args [2 ]
484+         function_target_result  =  self .fn (last_message , ctx , group_chat , current_agent )
485+         if  function_target_result .context_variables :
486+             ctx .update (function_target_result .context_variables )
487+         if  function_target_result .messages :
488+             broadcast (function_target_result .messages , group_chat , current_agent , self .fn_name , function_target_result .target , user_agent )
489+         return  function_target_result .target .resolve (group_chat , current_agent , user_agent )
469490
470491    def  display_name (self ) ->  str :
471492        return  self .fn_name 
0 commit comments