§
    ÚŽ’j(>  ã                   óÔ   — d Z ddlZddlmZ ddlZddlmZ ddlmZ ddl	m
Z
 dd	lmZmZmZmZmZmZmZmZmZ d
eez  deeef         fd„Z G d„ de¦  «        Z G d„ de¦  «        ZdS )z)The Moonshot AI formatter for agentscope.é    N)ÚAny)ÚFieldé   )Ú_OpenAIFormatterBaseé   )Úlogger)	ÚMsgÚ	URLSourceÚBase64SourceÚ	TextBlockÚ	DataBlockÚThinkingBlockÚ	HintBlockÚToolCallBlockÚToolResultBlockÚsourceÚreturnc                 óî  — t          | t          ¦  «        rd| j        › d| j        › �}�nFt          | t          ¦  «        �rt          | j        ¦  «        }|                     d¦  «        r„|                     d¦  «        }t          |d¦  «        5 }t          j        |                     ¦   «         ¦  «                             d¦  «        }ddd¦  «         n# 1 swxY w Y   d| j        › d|› �}nƒt          j        |d¬¦  «        }|                     ¦   «          t          j        |j        ¦  «                             d¦  «        }d| j        › d|› �}nt%          d	t'          | ¦  «        › �¦  «        ‚d
d|idœS )uh  Convert an image source to Moonshot ``image_url`` format.

    Moonshot's vision API only accepts base64 data URIs or file IDs â€” raw
    remote URLs are rejected. This helper downloads remote ``http(s)://``
    URLs and converts them to base64 data URIs, while ``file://`` URLs and
    ``Base64Source`` go through the same conversion as the OpenAI base.
    zdata:z;base64,zfile://Úrbzutf-8Né   )ÚtimeoutzUnsupported image source type: Ú	image_urlÚurl)Útyper   )Ú
isinstancer   Ú
media_typeÚdatar
   Ústrr   Ú
startswithÚremoveprefixÚopenÚbase64Ú	b64encodeÚreadÚdecodeÚrequestsÚgetÚraise_for_statusÚcontentÚ
ValueErrorr   )r   r   Úurl_strÚ
local_pathÚfÚencodedÚresponses          úVC:\Projects\DSCons\.venv\Lib\site-packages\agentscope\formatter\_moonshot_formatter.pyÚ_moonshot_format_image_sourcer1      sÉ  € õ �&�,Ñ'Ô'ð KØ>�fÔ'Ð>Ð>°´Ð>Ð>ˆ‰å	�F�IÑ	&Ô	&ñ KÝ�f”j‘/”/ˆØ×Ò˜iÑ(Ô(ð 		?Ø ×-Ò-¨iÑ8Ô8ˆJÝ�j $Ñ'Ô'ð E¨1Ý Ô*¨1¯6ª6©8¬8Ñ4Ô4×;Ò;¸GÑDÔD�ðEð Eð Eñ Eô Eð Eð Eð Eð Eð Eð Eøøøð Eð Eð Eð Eà>˜&Ô+Ð>Ð>°WÐ>Ð>ˆCˆCå”| G°RÐ8Ñ8Ô8ˆHØ×%Ò%Ñ'Ô'Ð'ÝÔ& xÔ'7Ñ8Ô8×?Ò?ÀÑHÔHˆGØ>˜&Ô+Ð>Ð>°WÐ>Ð>ˆCˆCõ ÐI½4À¹<¼<ÐIÐIÑJÔJÐJð Ø˜S�\ðð ð s   Â:CÃCÃCc                   ó¤   — e Zd ZU dZ ed„ d¬¦  «        Zee         ed<   de	e
z  deeef         fd„Zd	ee         deeeef                  fd
„ZdS )ÚMoonshotChatFormattera¨  The Moonshot AI formatter for chatbot scenario.

    Moonshot's API is OpenAI-compatible, but thinking models (``kimi-k2.6``,
    ``kimi-k2-thinking``) return a ``reasoning_content`` field alongside
    ``content`` in assistant messages.  This formatter preserves that field
    when re-sending assistant messages back to the API so that the
    *Preserved Thinking* feature works correctly in multi-turn conversations.
    c                  ó
   — g d¢S ©N)z
text/plainzimage/*zaudio/*© r6   ó    r0   ú<lambda>zMoonshotChatFormatter.<lambda>F   ó   € Ð DÐ DÐ D€ r7   úPThe supported input types. Defaults to ``["text/plain", "image/*", "audio/*"]``.©Údefault_factoryÚdescriptionÚinput_typesr   r   c                 ó    — t          |¦  «        S ©N©r1   ©Úselfr   s     r0   Ú_format_image_sourcez*MoonshotChatFormatter._format_image_sourceM   ó   € õ -¨VÑ4Ô4Ð4r7   Úmsgsc              ƒ   ó"	  K  — |                       |¦  «         g }d}|t          |¦  «        k     �r_||         }g }g }g }|                     ¦   «         D �]®}t          |t          ¦  «        r|                     |j        ¦  «         Œ3t          |t          ¦  «        r|                     d|j        dœ¦  «         Œft          |t          ¦  «        r-|  
                    |¦  «        }	|	�|                     |	¦  «         Œ¨t          |t          ¦  «        �r:|s|s|r[|j        |j        |pddœ}
|j        dk    r|rd                     |¦  «        nd|
d	<   |r||
d
<   |                     |
¦  «         g }g }g }t          |j        t           ¦  «        r#|                     dd|j        dœgdœ¦  «         �Œ\g }|j        D ]v}t          |t          ¦  «        r|                     d|j        dœ¦  «         Œ5t          |t          ¦  «        r,|  
                    |¦  «        }|�|                     |¦  «         Œw|r|                     d|dœ¦  «         �Œøt          |t"          ¦  «        r-|                     |j        d|j        |j        dœdœ¦  «         �Œ:t          |t(          ¦  «        �r<|s|s|r[|j        |j        |pddœ}|j        dk    r|rd                     |¦  «        nd|d	<   |r||d
<   |                     |¦  «         g }g }g }|                      |j        ¦  «        \  }}|                     d|j        ||j        dœ¦  «         |r–g }|D ]v}t          |t          ¦  «        r|                     d|j        dœ¦  «         Œ5t          |t          ¦  «        r,|  
                    |¦  «        }|�|                     |¦  «         Œw|r|                     dd|dœ¦  «         �ŒŒt/          j        dt3          |¦  «        ¦  «         �Œ°|j        |j        |pddœ}
|j        dk    r|rd                     |¦  «        nd|
d	<   |r||
d
<   |
d         s|
                     d
¦  «        s|r|                     |
¦  «         |dz  }|t          |¦  «        k     �°_|S )a  Format messages into the Moonshot / OpenAI-compatible API format.

        Behaves identically to :class:`OpenAIChatFormatter` except that
        :class:`ThinkingBlock` content is placed into the ``reasoning_content``
        field of the assistant message dict (required for Preserved Thinking).

        Args:
            msgs (`list[Msg]`):
                The list of message objects to format.

        Returns:
            `list[dict[str, Any]]`:
                The formatted messages as a list of dictionaries.
        r   Útext©r   rH   N)ÚroleÚnamer)   Ú	assistantú
Ú Úreasoning_contentÚ
tool_callsÚuser©rJ   r)   Úfunction)rK   Ú	arguments)Úidr   rS   Útool)rJ   Útool_call_idr)   rK   zsystem-reminderz2Unsupported block type %s in the message, skipped.r)   r   )Úassert_list_of_msgsÚlenÚget_content_blocksr   r   ÚappendÚthinkingr   rH   r   Ú_format_openai_data_blockr   rJ   rK   ÚjoinÚhintr   r   rU   Úinputr   Úconvert_tool_result_to_stringÚoutputr   Úwarningr   r'   )rC   rF   ÚmessagesÚiÚmsgÚcontent_blocksÚreasoning_partsrP   ÚblockÚ	formattedÚmsg_moonshotÚ
hint_partsÚsubÚformatted_subÚ	msg_flushÚtextual_outputÚmultimodal_dataÚpromo_contentÚitemÚfmt_items                       r0   ÚformatzMoonshotChatFormatter.formatT   s  è è € ð$ 	× Ò  Ñ&Ô&Ð&à!ˆØˆØ•#�d‘)”)Šm‰mØ�q”'ˆCØ)+ˆNØ)+ˆOØ%'ˆJà×/Ò/Ñ1Ô1ð Eñ E�Ý˜e¥]Ñ3Ô3ð Dð $×*Ò*¨5¬>Ñ:Ô:Ð:Ð:å ¥yÑ1Ô1ð Ø"×)Ò)°6À5Ä:Ð*NÐ*NÑOÔOÐOÐOå ¥yÑ1Ô1ð |Ø $× >Ò >¸uÑ EÔ E�IØ Ð,Ø&×-Ò-¨iÑ8Ô8Ð8øå ¥yÑ1Ô1ñ wØ%ð (¨ð (°ð (à$'¤HØ$'¤HØ'5Ð'=¸ð(ð (˜ð
 œ8 {Ò2Ð2ð $3ð!( §	¢	¨/Ñ :Ô :Ð :à%'ð )Ð)<Ñ=ð
 &ð DØ9C˜L¨Ñ6Ø Ÿš¨Ñ5Ô5Ð5Ø)+˜Ø*,˜Ø%'˜
å! %¤*­cÑ2Ô2ð Ø Ÿšà(.à-3¸U¼ZÐ$HÐ$Hð,"ðð ñô ð ñ ð 24˜
Ø#(¤:ð 
Eð 
E˜CÝ)¨#­yÑ9Ô9ð 	EØ *× 1Ò 1Ø-3¸S¼XÐ$FÐ$Fñ!"ô !"ð !"ð !"õ ",¨CµÑ!;Ô!;ð EØ04×0NÒ0NØ$'ñ1"ô 1" ð $1Ð#<Ø$.×$5Ò$5°mÑ$DÔ$DÐ$DøØ%ð Ø$ŸOšOØ)/¸JÐ GÐ Gñô ð ùõ   ¥}Ñ5Ô5ð HØ×%Ò%à"'¤(Ø$.à(-¬
Ø-2¬[ð)ð )ðð ñ	ô 	ð 	ñ 	õ   ¥Ñ7Ô7ñ <Ø%ð (¨ð (°ð (à$'¤HØ$'¤HØ'5Ð'=¸ð%ð %˜	ð
 œ8 {Ò2Ð2ð $3ð!( §	¢	¨/Ñ :Ô :Ð :à%'ð &Ð&9Ñ:ð
 &ð AØ6@˜I lÑ3Ø Ÿš¨	Ñ2Ô2Ð2Ø)+˜Ø*,˜Ø%'˜
ð
 ×:Ò:¸5¼<ÑHÔHñØ&Ø'ð —O’Oà$*Ø,1¬HØ'5Ø$)¤Jð	ð ñô ð ð 'ð Ø(*˜Ø$3ð 
Cð 
C˜DÝ)¨$µ	Ñ:Ô:ð 	CØ -× 4Ò 4Ø-3¸T¼YÐ$GÐ$Gñ!"ô !"ð !"ð !"õ ",¨Dµ)Ñ!<Ô!<ð CØ+/×+IÒ+IØ$(ñ,"ô ," ð $,Ð#7Ø$1×$8Ò$8¸Ñ$BÔ$BÐ$BøØ(ð Ø$ŸOšOà,2Ø,=Ø/<ð!"ð !"ñô ð ùõ ”NØLÝ˜U™œñô ð ñ ð œØœØ)Ð1¨Tðð ˆLð Œx˜;Ò&Ð&à2AÐI�D—I’I˜oÑ.Ô.Ð.Àrð Ð0Ñ1ð ð 8Ø-7�˜\Ñ*ð ˜YÔ'ð.à×#Ò# LÑ1Ô1ð.ð #ð.ð
 —’ Ñ-Ô-Ð-à�‰FˆAðM •#�d‘)”)Šm‰mðP ˆr7   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r>   Úlistr   Ú__annotations__r
   r   Údictr   rD   r	   ru   r6   r7   r0   r3   r3   ;   sË   € € € € € € ðð ð #˜UØDÐDðDðñ ô €K��c”ð ð ñ ð5à˜LÑ(ð5ð 
ˆc�3ˆhŒð5ð 5ð 5ð 5ð~à�3Œið~ð 
ˆd�3˜�8ŒnÔ	ð~ð ~ð ~ð ~ð ~ð ~r7   r3   c            
       óp  — e Zd ZU dZ edd¬¦  «        Zeed<    ed„ d¬¦  «        Ze	e         ed	<   d
e
ez  deeef         fd„Zde	e         de	eeef                  fd„Zde	e         de	eeef                  fd„Z	 dde	e         dede	eeef                  fd„Zededeeef         fd„¦   «         ZdS )ÚMoonshotMultiAgentFormattera5  Formatter for the Moonshot AI API in multi-agent conversations.

    Moonshot's API is OpenAI-compatible, so the multi-agent history collapsing
    strategy is the same as :class:`OpenAIMultiAgentFormatter`.  Tool
    sequences are delegated to :class:`MoonshotChatFormatter` so that
    ``reasoning_content`` is preserved correctly for multi-turn
    *Preserved Thinking* conversations.

    .. note:: Telling the assistant's name in the system prompt is important
        in multi-agent conversations so that the model knows which role it
        is playing.
    zg# Conversation History
The content between <history></history> tags contains your conversation history
z7The prompt to use for the conversation history section.)Údefaultr=   Úconversation_history_promptc                  ó
   — g d¢S r5   r6   r6   r7   r0   r8   z$MoonshotMultiAgentFormatter.<lambda>-  r9   r7   r:   r;   r>   r   r   c                 ó    — t          |¦  «        S r@   rA   rB   s     r0   rD   z0MoonshotMultiAgentFormatter._format_image_source4  rE   r7   rF   c              ƒ   ó"  K  — |                       |¦  «         g }d}|rG|d         j        dk    r6|                     |                      |d         ¦  «        ƒ d{V —†¦  «         d}d}|                      ||d…         ¦  «        2 3 d{V —†\  }}|dk    rB|                     t          | j        ¬¦  «                             |¦  «        ƒ d{V —†¦  «         ŒR|dk    r1|                     |  	                    ||¦  «        ƒ d{V —†¦  «         d	}ŒŠ6 |S )
a  Format input messages into the Moonshot AI API format for
        multi-agent conversations.

        Non-tool messages from all agents are collapsed into a single user
        message with ``<history></history>`` tags.  Tool call / result
        sequences are delegated to :class:`MoonshotChatFormatter`.

        Args:
            msgs (`list[Msg]`):
                The list of message objects to format.

        Returns:
            `list[dict[str, Any]]`:
                The formatted messages as a list of dictionaries.
        r   ÚsystemNr   TÚtool_sequence©r>   Úagent_messageF)
rX   rJ   r[   Ú_format_system_messageÚ_group_messagesÚextendr3   r>   ru   Ú_format_agent_message)rC   rF   Úformatted_msgsÚstart_indexÚis_first_agent_messageÚtypÚgroups          r0   ru   z"MoonshotMultiAgentFormatter.format:  s©  è è € ð  	× Ò  Ñ&Ô&Ð&à%'ˆØˆØð 	�D˜”G”L HÒ,Ð,Ø×!Ò!Ø×1Ò1°$°q´'Ñ:Ô:Ð:Ð:Ð:Ð:Ð:Ð:ñô ð ð ˆKà!%ÐØ $× 4Ò 4°T¸+¸,¸,Ô5GÑ HÔ Hð 	/ð 	/ð 	/ð 	/ð 	/ð 	/ð 	/‘*�#�uØ�oÒ%Ð%Ø×%Ò%Ý/Ø$(Ô$4ðñ ô ç’f˜U‘m”mð$ð $ð $ð $ð $ð $ñô ð ð ð
 ˜Ò'Ð'Ø×%Ò%Ø×4Ò4ØØ.ñô ð ð ð ð ð ð ñô ð ð */Ð&øð !Ið  Ðs   ÂDc              ƒ   ób   K  — t          | j        ¬¦  «                             |¦  «        ƒ d{V —†S )zOFormat a sequence of tool-related messages using
        MoonshotChatFormatter.r†   N)r3   r>   ru   )rC   rF   s     r0   Ú_format_tool_sequencez1MoonshotMultiAgentFormatter._format_tool_sequenceg  sN   è è € õ +ØÔ(ð
ñ 
ô 
ç
Š&�‰,Œ,ðð ð ð ð ð ð 	r7   TÚis_firstc              ƒ   ó  K  — |r| j         }nd}g }g }|D ]”}|                     ¦   «         D ]}}t          |t          ¦  «        r%|                     |j        › d|j        › �¦  «         Œ<t          |t          ¦  «        r,|                      |¦  «        }|�|                     |¦  «         Œ~Œ•|s|sg S d 	                    |¦  «        }	|	r|dz   |	z   dz   }	g }
|	r|
                     d|	dœ¦  «         |
 
                    |¦  «         d	|
d
œgS )z:Collapse agent messages into a ``<history>`` user message.rN   z: NrM   z
<history>
z
</history>rH   rI   rQ   rR   )r€   rZ   r   r   r[   rK   rH   r   r]   r^   rŠ   )rC   rF   r“   r€   Úaccumulated_textÚmedia_blocksrf   ri   rj   Úhistory_textÚcontent_lists              r0   r‹   z1MoonshotMultiAgentFormatter._format_agent_messageq  sz  è è € ð ð 	-Ø*.Ô*JÐ'Ð'à*,Ð'à&(ÐØ#%ˆàð 	7ð 	7ˆCØ×/Ò/Ñ1Ô1ð 7ð 7�Ý˜e¥YÑ/Ô/ð 7Ø$×+Ò+¨s¬xÐ,GÐ,G¸5¼:Ð,GÐ,GÑHÔHÐHÐHÝ ¥yÑ1Ô1ð 7Ø $× >Ò >¸uÑ EÔ E�IØ Ð,Ø$×+Ò+¨IÑ6Ô6Ð6øð7ð  ð 	¨ð 	ØˆIà—y’yÐ!1Ñ2Ô2ˆØð 	à+Øñ àñð !ñ!ð ð .0ˆØð 	HØ×Ò¨¸Ð FÐ FÑGÔGÐGØ×Ò˜LÑ)Ô)Ð)à¨LÐ9Ð9Ð:Ð:r7   rf   c              ƒ   ó4   K  — d|                       ¦   «         dœS )z0Format a system message for the Moonshot AI API.r„   rR   )Úget_text_content)rf   s    r0   rˆ   z2MoonshotMultiAgentFormatter._format_system_message›  s)   è è € ð Ø×+Ò+Ñ-Ô-ð
ð 
ð 	
r7   N)T)rv   rw   rx   ry   r   r€   r   r{   r>   rz   r
   r   r|   r   rD   r	   ru   r’   Úboolr‹   Ústaticmethodrˆ   r6   r7   r0   r~   r~     s®  € € € € € € ðð ð (- uð*ð Nð(ñ (ô (Ð ð ð ñ ð #˜UØDÐDðDðñ ô €K��c”ð ð ñ ð5à˜LÑ(ð5ð 
ˆc�3ˆhŒð5ð 5ð 5ð 5ð+  c¤ð +¨t°D¸¸c¸´NÔ/Cð +ð +ð +ð +ðZà�3Œiðð 
ˆd�3˜�8ŒnÔ	ðð ð ð ð ð(;ð (;à�3Œið(;ð ð(;ð 
ˆd�3˜�8ŒnÔ	ð	(;ð (;ð (;ð (;ðT ð
¨#ð 
°$°s¸C°x´.ð 
ð 
ð 
ñ „\ð
ð 
ð 
r7   r~   )ry   r"   Útypingr   r&   Úpydanticr   Ú_openai_formatterr   Ú_loggingr   Úmessager	   r
   r   r   r   r   r   r   r   r|   r   r1   r3   r~   r6   r7   r0   ú<module>r¢      sx  ðà /Ð /Ø €€€Ø Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð à 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø Ð Ð Ð Ð Ð ð
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð Ø˜Ñ$ð à	ˆ#ˆsˆ(„^ð ð  ð  ð  ðFWð Wð Wð Wð WÐ0ñ Wô Wð WðtL
ð L
ð L
ð L
ð L
Ð"6ñ L
ô L
ð L
ð L
ð L
r7   