§
    ÚŽ’j8   ã                   óž   — d Z ddlZddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
 ddlZddlmZmZ ddlmZmZmZmZmZ  G d	„ d
e¦  «        ZdS )zThe formatter module.é    N)Úabstractmethod)Úfnmatch)ÚAnyÚListÚAsyncGenerator)Ú	BaseModelÚFieldé   )ÚMsgÚ	DataBlockÚ	TextBlockÚ	URLSourceÚBase64Sourcec            
       óR  — e Zd ZU dZ ed„ d¬¦  «        Zee         ed<   	 e	dee         fd„¦   «         Z
eded	edeeeef                  fd
„¦   «         Zedee         ddfd„¦   «         Zdeeeez           z  deeeeez           f         fd„Zedee         defd„¦   «         ZdS )ÚFormatterBasezThe base class for formatters.c                  ó   — dgS )Nú
text/plain© r   ó    úRC:\Projects\DSCons\.venv\Lib\site-packages\agentscope\formatter\_formatter_base.pyú<lambda>zFormatterBase.<lambda>   s   €   € r   a  The supported input types, aligned with the model card's ``input_types`` field. Entries other than ``text/plain`` and ``application/x-thinking`` are treated as media-type patterns (glob-style, e.g. ``image/*``, ``audio/mp3``) that control which ``DataBlock``\s are forwarded to the API.)Údefault_factoryÚdescriptionÚinput_typesÚreturnc                 ó$   — d„ | j         D ¦   «         S )z„Derive the accepted media-type patterns from :attr:`input_types` by
        excluding ``text/plain`` and ``application/x-thinking``.c                 ó   — g | ]}|d v¯|‘Œ	S ))r   zapplication/x-thinkingr   )Ú.0Úts     r   ú
<listcomp>z=FormatterBase.supported_input_media_types.<locals>.<listcomp>*   s.   € ð 
ð 
ð 
àØÐ@Ð@Ð@ð à@Ð@Ð@r   )r   )Úselfs    r   Úsupported_input_media_typesz)FormatterBase.supported_input_media_types&   s&   € ð
ð 
àÔ%ð
ñ 
ô 
ð 	
r   ÚargsÚkwargsc              �   ó
   K  — dS )z[Format the Msg objects to a list of dictionaries that satisfy the
        API requirements.Nr   )r!   r#   r$   s      r   ÚformatzFormatterBase.format0   s
   è è € € € r   ÚmsgsNc                 óÂ   — t          | t          ¦  «        st          d¦  «        ‚| D ]7}t          |t          ¦  «        s t          dt	          |¦  «        › d�¦  «        ‚Œ8dS )zžAssert that the input is a list of Msg objects.

        Args:
            msgs (`list[Msg]`):
                A list of Msg objects to be validated.
        z$Input must be a list of Msg objects.zExpected Msg object, got z	 instead.N)Ú
isinstanceÚlistÚ	TypeErrorr   Útype)r'   Úmsgs     r   Úassert_list_of_msgsz!FormatterBase.assert_list_of_msgs5   s|   € õ ˜$¥Ñ%Ô%ð 	DÝÐBÑCÔCÐCàð 	ð 	ˆCÝ˜c¥3Ñ'Ô'ð ÝØDµ°S±	´	ÐDÐDÐDñô ð ðð	ð 	r   Úoutputc           
      óŠ  ‡	— t          |t          ¦  «        r|g fS g }g }|D �]æŠ	t          ‰	t          ¦  «        r|                     ‰	j        ¦  «         Œ3t          ‰	t
          ¦  «        �r�‰	j        j                             d¦  «        d         }t          ˆ	fd„| j
        D ¦   «         ¦  «        r\t          j        ¦   «         }|                     d|› d|› d�¦  «         |                     t          d|› d|› d	�¬
¦  «        ‰	g¦  «         Œêt          ‰	j        t          ¦  «        r(|                     d|› d‰	j        j        › d�¦  «         �Œ,t          ‰	j        t           ¦  «        r t#          j        ‰	j        j        ¦  «        }t'          j        |d¬¦  «        5 }t+          j        ‰	j        j        ¦  «        }|                     |¦  «         |                     d|› d|j        › d�¦  «         ddd¦  «         n# 1 swxY w Y   �Œè|r#t          d¬
¦  «        g|¢t          d¬
¦  «        ‘}d                     |¦  «        |fS )a  Turn the tool result list into a textual output to be compatible
        with the LLM API that doesn't support multimodal data in the tool
        result.

        For URL-based images, the URL is included in the list. For
        base64-encoded images, the local file path where the image is saved
        is included in the returned list.

        Args:
            output (`str | List[TextBlock | DataBlock]`):
                The output of the tool response, including text and multimodal
                data like images and audio.

        Returns:
            `tuple[str, list[TextBlock | DataBlock]]`:
                A tuple containing the textual representation of the tool
                result and a list of blocks to be promoted as a user message.
        ú/r   c              3   óL   •K  — | ]}t          ‰j        j        |¦  «        V — Œd S )N)r   ÚsourceÚ
media_type)r   Ú_Úblocks     €r   ú	<genexpr>z>FormatterBase.convert_tool_result_to_string.<locals>.<genexpr>j   sF   øè è € ð ð àõ ˜EœLÔ3°QÑ7Ô7ðð ð ð ð ð r   z<system-reminder>A(n) zD file is returned and will be presented to you with the identifier [z].</system-reminder>z- z (z file): )Útextz2 file is returned and can be accessed at the URL: z.</system-reminder>F)ÚsuffixÚdeletez( file is returned and saved locally at: NzQ<system-reminder>The multimodal data and their identifiers are listed as follows:z</system-reminder>ú
)r)   Ústrr   Úappendr8   r   r3   r4   ÚsplitÚanyr"   Ú	shortuuidÚuuidÚextendr   Úurlr   Ú	mimetypesÚguess_extensionÚtempfileÚNamedTemporaryFileÚbase64Ú	b64decodeÚdataÚwriteÚnameÚjoin)
r!   r/   Útextual_outputÚmultimodal_dataÚ	main_typeÚ
identifierÚ	extensionÚ	temp_fileÚdecoded_datar6   s
            @r   Úconvert_tool_result_to_stringz+FormatterBase.convert_tool_result_to_stringF   s6  ø€ õ. �f�cÑ"Ô"ð 	Ø˜2�:ÐàˆØ "ˆàð ;	ñ ;	ˆEÝ˜%¥Ñ+Ô+ð :Ø×%Ò% e¤jÑ1Ô1Ð1Ð1å˜E¥9Ñ-Ô-ñ 7Ø!œLÔ3×9Ò9¸#Ñ>Ô>¸qÔA�	åð ð ð ð à!Ô=ðñ ô ñ ô ð 4õ "+¤Ñ!1Ô!1�Jà"×)Ò)ð=°ð =ð =à&ð=ð =ð =ñô ð ð
 $×*Ò*å%Ø%K¨*Ð%KÐ%K¸	Ð%KÐ%KÐ%Kðñ ô ð "ð	ñô ð ð õ   ¤­iÑ8Ô8ð Ø"×)Ò)ð.°ð .ð .Ø;@¼<Ô;Kð.ð .ð .ñô ð ñ õ   ¤­lÑ;Ô;ð å )Ô 9ØœÔ/ñ!ô !�Iõ "Ô4Ø(Ø$ðñ ô ð 
ð #Ý'-Ô'7¸¼Ô8IÑ'JÔ'J˜Ø!Ÿš¨Ñ5Ô5Ð5Ø&×-Ò-ð2°Yð 2ð 2Ø>G¼nð2ð 2ð 2ñô ð ð
ð 
ð 
ñ 
ô 
ð 
ð 
ð 
ð 
ð 
ð 
øøøð 
ð 
ð 
ð 
ùð ð 
	åð9ðñ ô ð	ð
 !ð	õ Ø-ðñ ô ð	ˆOð �yŠy˜Ñ(Ô(¨/Ð9Ð9s   ÆAG<Ç<H 	ÈH 	c                ó
  K  — d}g }| D ]î}|€E|                      d¦  «        s|                      d¦  «        rd}nd}|                     |¦  «         ŒI|dk    rM|                     d¦  «        s|                     d¦  «        r|                     |¦  «         Œ�||fW V — |g}d}Œœ|dk    rL|                     d¦  «        s|                     d¦  «        r||fW V — |g}d}ŒÙ|                     |¦  «         Œï|r	||fW V — dS dS )z£Group messages into tool sequences and agent messages.

        Args:
            msgs (`list[Msg]`):
                A list of Msg objects to be grouped.
        NÚ	tool_callÚtool_resultÚtool_sequenceÚagent_message)Úget_content_blocksr=   Úhas_content_blocks)r'   Ú
group_typeÚgroupr-   s       r   Ú_group_messageszFormatterBase._group_messages¯   s�  è è € ð ˆ
ØˆØð 	&ð 	&ˆCØÐ!Ø×)Ò)Øñô ð 1à×+Ò+¨MÑ:Ô:ð1ð "1�J�Jà!0�JØ—’˜SÑ!Ô!Ð!Øà˜_Ò,Ð,Ø×)Ò)Øñô ð 1à×+Ò+¨MÑ:Ô:ð1ð —L’L Ñ%Ô%Ð%Ð%à$ eÐ+Ð+Ð+Ð+Ð+Ø ˜E�EØ!0�J�Jà˜Ò.Ð.Ø×)Ò)Øñô ð &à×+Ò+¨MÑ:Ô:ð&ð % eÐ+Ð+Ð+Ð+Ð+Ø ˜E�EØ!0�J�Jà—L’L Ñ%Ô%Ð%øàð 	$Ø˜eÐ#Ð#Ð#Ð#Ð#Ð#Ð#ð	$ð 	$r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r*   r<   Ú__annotations__Úpropertyr"   r   r   Údictr&   Ústaticmethodr   r.   r   r   r   ÚtuplerU   r   r_   r   r   r   r   r      sx  € € € € € € Ø(Ð(à"˜UØ.Ð.ð9ð	ñ 	ô 	€K��c”ð 	ð 	ñ 	ð%ð ð
¨T°#¬Yð 
ð 
ð 
ñ „Xð
ð ð #ð °ð ¸¸dÀ3ÈÀ8¼nÔ9Mð ð ð ñ „^ðð ð $ s¤)ð °ð ð ð ñ „\ðð g:à�d˜9 yÑ0Ô1Ñ1ðg:ð 
ˆs�D˜ YÑ.Ô/Ð/Ô	0ðg:ð g:ð g:ð g:ðR ð)$ D¨¤Ið )$°.ð )$ð )$ð )$ñ „\ð)$ð )$ð )$r   r   )rc   rH   rD   rF   Úabcr   r   Útypingr   r   r   r@   Úpydanticr   r	   Úmessager   r   r   r   r   r   r   r   r   ú<module>rm      s  ðà Ð Ø €€€Ø Ð Ð Ð Ø €€€Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,à Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %Ð %Ð %ðð ð ð ð ð ð ð ð ð ð ð ð ð ðC$ð C$ð C$ð C$ð C$�Iñ C$ô C$ð C$ð C$ð C$r   