
    Xi^                    V    d Z ddlmZ ddlZddlmZ ddlmZ erddlm	Z	 dZ
	 dddZdS )z>Automatic agent selection using LLM to pick best participants.    )annotationsN)TYPE_CHECKING)AgentConfig)	LLMClient      topicstrcontext
all_agentsdict[str, AgentConfig]clientr   
max_agentsintreturn	list[str]c                   d                     d                                 D                       }d| d| d}d|  }|r|d| z  }d|d	g}d
}	t          t                    D ]R}
|	r|                    dd|	 dd	           |                    ||          }	 |                                }|                    d          rUd|v r|                    dd          d         n|}|	                    d          r
|dd         }|                                }t          j        |          }|d         }fd|D             }|s,dd                                                                }	|d|         c S # t          j        t          t          f$ r}t!          |          }	Y d}~Ld}~ww xY wt#          dt           d|	           )a  PM selects the best agents for a meeting topic using LLM.

    Args:
        topic: The meeting discussion topic.
        context: Background context for the meeting.
        all_agents: All available agents to choose from.
        client: LLM client for making the selection.
        max_agents: Maximum number of agents to select.

    Returns:
        List of selected agent names.

    Raises:
        RuntimeError: When all retry attempts are exhausted.
    
c              3  6   K   | ]\  }}d | d|j          V  dS )z- z: N)role).0nameconfigs      9/root/projects/multi-agents-meeting/src/agent_selector.py	<genexpr>z%auto_select_agents.<locals>.<genexpr>&   sJ       # #'3tV"T""V[""# # # # # #    zcYou are a meeting facilitator selecting the best participants for a discussion.

Available Agents:
z

Select up to a   agents most relevant to the topic.
Output ONLY a JSON object with these fields:
- "selected_agents": list of agent names (must be from the available agents)
- "reasoning": brief explanation of why these agents were chosen

Output ONLY the JSON object, no other text.zTopic: z

Context: user)r   content z$Your previous response was invalid: z . Please output ONLY valid JSON.)systemmessagesz```   Nselected_agentsc                    g | ]}|v |	S  r&   )r   r   r   s     r   
<listcomp>z&auto_select_agents.<locals>.<listcomp>U   s#    LLLT9K9KD9K9K9Kr   z+No valid agents in selection. Choose from: z, z*Failed to get valid agent selection after z attempts. Last error: )joinitemsrange_MAX_RETRIESappendchatstrip
startswithsplitendswithjsonloadskeysJSONDecodeErrorKeyError	TypeErrorr
   RuntimeError)r	   r   r   r   r   agent_descriptionssystem_promptuser_contentr!   
last_errorattemptraw_responsecleaneddataselectedvalid_agentsexcs     `              r   auto_select_agentsrD      s   ,  # #7A7G7G7I7I# # #  
/ 
/ 
/
 
/ 
/ 
/M %U$$L 21111/5,&O&O%PHJ&& & & 	OO5: 5 5 5     {{-({KK	"((**G!!%(( *7;w'--a0033G##E** +%crclG!--//:g&&D-.H MLLLXLLLL C$(IIjoo.?.?$@$@C C    ,,,,$h	: 	 	 	SJHHHH	 	$\ 	$ 	$!	$ 	$  s    CF6	FF8F33F8)r   )r	   r
   r   r
   r   r   r   r   r   r   r   r   )__doc__
__future__r   r2   typingr   
src.modelsr   src.llm_clientr   r+   rD   r&   r   r   <module>rJ      s    D D " " " " " "              " " " " " " )(((((( X X X X X X Xr   