§
    ÙŽ’j)-  ã                   ó–   — d Z ddlm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  G d	„ d
e¦  «        Z G d„ de¦  «        ZdS )zThe DeepSeek formatter module.é    )ÚAny)ÚFieldé   )ÚFormatterBaseé   )Úlogger)ÚMsgÚ	TextBlockÚ	DataBlockÚThinkingBlockÚ	HintBlockÚToolCallBlockÚToolResultBlockc                   ó~   — e Zd ZU dZ ed„ d¬¦  «        Zee         ed<   dee	         dee
eef                  fd„Zd	S )
ÚDeepSeekChatFormatterz¸The DeepSeek formatter class for chatbot scenario, where only a user
    and an agent are involved. We use the `role` field to identify different
    entities in the conversation.
    c                  ó   — dgS ©Nz
text/plain© r   ó    úVC:\Projects\DSCons\.venv\Lib\site-packages\agentscope\formatter\_deepseek_formatter.pyú<lambda>zDeepSeekChatFormatter.<lambda>   ó   €   € r   úgThe supported input types. Defaults to ``["text/plain"]`` (DeepSeek does not support multimodal input).©Údefault_factoryÚdescriptionÚinput_typesÚmsgsÚreturnc           	   ƒ   óÀ  K  — |                       |¦  «         g }|D �]À}g }g }g }|                     ¦   «         D �]}t          |t          ¦  «        r|                     d|j        dœ¦  «         Œ6t          |t          ¦  «        r|                     |j        ¦  «         Œft          |t          ¦  «        �rZ|s|s|rxd 	                    d„ |D ¦   «         ¦  «        }|j
        |p|rdnddœ}	|j
        dk    r|rd 	                    |¦  «        nd|	d	<   |r||	d
<   |                     |	¦  «         g }g }g }t          |j        t          ¦  «        r|                     d|j        dœ¦  «         �Œ3g }
|j        D ]j}t          |t          ¦  «        r|
                     |j        ¦  «         Œ2t          |t          ¦  «        r#|
                     d|j        j        › d�¦  «         Œk|
r+|                     dd 	                    |
¦  «        dœ¦  «         �ŒÖt          |t           ¦  «        r-|                     |j        d|j        |j        dœdœ¦  «         �Œt          |t(          ¦  «        rÁ|s|s|rxd 	                    d„ |D ¦   «         ¦  «        }|j
        |p|rdnddœ}|j
        dk    r|rd 	                    |¦  «        nd|d	<   |r||d
<   |                     |¦  «         g }g }g }|                      |j        ¦  «        \  }}|                     d|j        ||j        dœ¦  «         �Œît/          j        dt3          |¦  «        ¦  «         �Œd 	                    d„ |D ¦   «         ¦  «        }|j
        |p|rdnddœ}|j
        dk    r|rd 	                    |¦  «        nd|d	<   |r||d
<   |d         s|                     d
¦  «        s|r|                     |¦  «         �ŒÂ|S )a  Format message objects into DeepSeek API format.

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

        Returns:
            `list[dict[str, Any]]`:
                The formatted messages as a list of dictionaries.
        Útext)Útyper!   ú
c              3   óB   K  — | ]}|                      d d¦  «        V — ŒdS ©r!   Ú N©Úget©Ú.0Úbs     r   ú	<genexpr>z/DeepSeekChatFormatter.format.<locals>.<genexpr>E   óA   è è € ð 1ð 1Ø23˜AŸEšE &¨"Ñ-Ô-ð1ð 1ð 1ð 1ð 1ð 1r   Nr&   ©ÚroleÚcontentÚ	assistantÚreasoning_contentÚ
tool_callsÚuserú[z* attached, not supported by this provider]Úfunction)ÚnameÚ	arguments)Úidr"   r6   c              3   óB   K  — | ]}|                      d d¦  «        V — ŒdS r%   r'   r)   s     r   r,   z/DeepSeekChatFormatter.format.<locals>.<genexpr>‚   r-   r   Útool)r/   Útool_call_idr0   r7   z2Unsupported block type %s in the message, skipped.c              3   óB   K  — | ]}|                      d d¦  «        V — ŒdS r%   r'   r)   s     r   r,   z/DeepSeekChatFormatter.format.<locals>.<genexpr>©   s0   è è € Ð#NÐ#N¸! A§E¢E¨&°"Ñ$5Ô$5Ð#NÐ#NÐ#NÐ#NÐ#NÐ#Nr   r0   )Úassert_list_of_msgsÚget_content_blocksÚ
isinstancer
   Úappendr!   r   Úthinkingr   Újoinr/   ÚhintÚstrr   ÚsourceÚ
media_typer   r9   r7   Úinputr   Úconvert_tool_result_to_stringÚoutputr   Úwarningr"   r(   )Úselfr   ÚmessagesÚmsgÚcontent_blocksÚreasoning_content_blocksr3   ÚblockÚcontent_textÚmsg_flush_hintÚhint_text_partsÚsubÚ	msg_flushÚtextual_outputÚ_Úcontent_msgÚmsg_deepseeks                    r   ÚformatzDeepSeekChatFormatter.format"   s`  è è € ð 	× Ò  Ñ&Ô&Ð&à!ˆØð Q	.ñ Q	.ˆCØ#%ˆNØ-/Ð$ØˆJà×/Ò/Ñ1Ô1ð oñ o�Ý˜e¥YÑ/Ô/ð nØ"×)Ò)°6À5Ä:Ð*NÐ*NÑOÔOÐOÐOå ¥}Ñ5Ô5ð kØ,×3Ò3°E´NÑCÔCÐCÐCå ¥yÑ1Ô1ñ hà&ð(à%ð(ð 4ð(ð
 (,§y¢yð 1ð 1Ø7Eð1ñ 1ô 1ñ (ô (˜ð %(¤HØ'3ð (<Ø(2Ð :  ¸ð:ð :˜ð
 œ8 {Ò2Ð2ð $<ð!( §	¢	Ð*BÑ CÔ CÐ Cà%'ð +Ð+>Ñ?ð
 &ð FØ;E˜N¨<Ñ8Ø Ÿš¨Ñ7Ô7Ð7Ø)+˜Ø35Ð0Ø%'˜
å! %¤*­cÑ2Ô2ð Ø ŸšØ%+¸¼
ÐCÐCñô ð ñ ð 68˜Ø#(¤:ð "ð "˜CÝ)¨#­yÑ9Ô9ð "Ø /× 6Ò 6°s´xÑ @Ô @Ð @Ð @Ý!+¨CµÑ!;Ô!;ð "Ø /× 6Ò 6ð%F¨¬
Ô(=ð %Fð %Fð %Fñ!"ô !"ð !"øð +ð Ø$ŸOšOà,2Ø/3¯yªy¸Ñ/IÔ/Ið!"ð !"ñô ð ùõ   ¥}Ñ5Ô5ð 7Ø×%Ò%à"'¤(Ø$.à(-¬
Ø-2¬[ð)ð )ðð ñ	ô 	ð 	ñ 	õ   ¥Ñ7Ô7ð +à&ð(à%ð(ð 4ð(ð
 (,§y¢yð 1ð 1Ø7Eð1ñ 1ô 1ñ (ô (˜ð %(¤HØ'3ð (<Ø(2Ð :  ¸ð5ð 5˜	ð
 œ8 {Ò2Ð2ð $<ð!( §	¢	Ð*BÑ CÔ CÐ Cà%'ð &Ð&9Ñ:ð
 &ð AØ6@˜I lÑ3Ø Ÿš¨	Ñ2Ô2Ð2Ø)+˜Ø35Ð0Ø%'˜
à(,×(JÒ(JØœñ)ô )Ñ%�N Að —O’Oà$*Ø,1¬HØ'5Ø$)¤Jð	ð ñô ð ñ õ ”NØLÝ˜U™œñô ð ñ ð
 Ÿ)š)Ð#NÐ#N¸~Ð#NÑ#NÔ#NÑNÔNˆKð œØ&ÐF°:Ð+E¨4¨4À2ð,ð ,ˆLð Œx˜;Ò&Ð&ð 0ð�D—I’IÐ6Ñ7Ô7Ð7àð Ð0Ñ1ð ð 8Ø-7�˜\Ñ*ð ˜YÔ'ð.à×#Ò# LÑ1Ô1ð.ð ,ð.ð
 —’ Ñ-Ô-Ð-ùàˆr   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚlistrE   Ú__annotations__r	   Údictr   r[   r   r   r   r   r      s—   € € € € € € ðð ð
 #˜UØ.Ð.ð<ðñ ô €K��c”ð ð ñ ðdà�3Œiðdð 
ˆd�3˜�8ŒnÔ	ðdð dð dð dð dð dr   r   c            
       óJ  — 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
         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 )ÚDeepSeekMultiAgentFormatterzq
    DeepSeek formatter for multi-agent conversations, where more than
    a user and an agent are involved.
    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                  ó   — dgS r   r   r   r   r   r   z$DeepSeekMultiAgentFormatter.<lambda>Ù   r   r   r   r   r   r   r   c              ƒ   ó"  K  — |                       |¦  «         g }d}t          |¦  «        dk    rG|d         j        dk    r6|                     |                      |d         ¦  «        ƒ d{V —†¦  «         d}d}|                      ||d…         ¦  «        2 3 d{V —†\  }}|xdk    r0 |                     |                      |¦  «        ƒ d{V —†¦  «         ŒAdk    r2|                     |                      ||¦  «        ƒ d{V —†¦  «         d}ŒxŒy6 |S )	zlFormat input messages into the structure required by the DeepSeek
        API for multi-agent conversations.r   ÚsystemNr   TÚtool_sequenceÚagent_messageF)	r>   Úlenr/   rA   Ú_format_system_messageÚ_group_messagesÚextendÚ_format_tool_sequenceÚ_format_agent_message)rL   r   Úformatted_msgsÚstart_indexÚis_first_agent_messageÚtypÚgroups          r   r[   z"DeepSeekMultiAgentFormatter.formatà   s˜  è è € ð 	× Ò  Ñ&Ô&Ð&àˆØˆÝˆt‰9Œ9�qŠ=ˆ=˜T !œWœ\¨XÒ5Ð5Ø×!Ò!Ø×1Ò1°$°q´'Ñ:Ô:Ð:Ð:Ð:Ð:Ð:Ð:ñô ð ð ˆKà!%ÐØ $× 4Ò 4°T¸+¸,¸,Ô5GÑ HÔ Hð 	3ð 	3ð 	3ð 	3ð 	3ð 	3ð 	3‘*�#�uØØ$�_’_�_�_Ø"×)Ò)Ø"×8Ò8¸Ñ?Ô?Ð?Ð?Ð?Ð?Ð?Ð?ñô ð ð ð %’_�_Ø"×)Ò)Ø"×8Ò8Ø!Ø2ñô ð ð ð ð ð ð ñô ð ð .3Ð*Ð*ð %ð !Ið Ðs   ÂDc              ƒ   ób   K  — t          | j        ¬¦  «                             |¦  «        ƒ d{V —†S )zqGiven a sequence of tool call/result messages, format them into
        the required format for the DeepSeek API.)r   N)r   r   r[   )rL   r   s     r   rp   z1DeepSeekMultiAgentFormatter._format_tool_sequenceÿ   sN   è è € õ +ØÔ(ð
ñ 
ô 
ç
Š&�‰,Œ,ðð ð ð ð ð ð 	r   TÚis_firstc              ƒ   óL  K  — |r| j         }nd}g }g }|D ]R}|                     ¦   «         D ];}t          |t          ¦  «        r$|                     |j        › d|j        › �¦  «         Œ<ŒSd}|r|dz   d                     |¦  «        z   dz   }|r|                     d|dœ¦  «         |S )z{Given a sequence of messages without tool calls/results, format
        them into the required format for the DeepSeek API.r&   z: z
<history>
r#   z
</history>r4   r.   )rf   r?   r@   r
   rA   r7   r!   rC   )	rL   r   rx   rf   rr   Úaccumulated_textrN   rQ   Úconversation_blocks_texts	            r   rq   z1DeepSeekMultiAgentFormatter._format_agent_message	  s  è è € ð ð 	-Ø*.Ô*JÐ'Ð'à*,Ð'à%'ˆØÐàð 	Ið 	IˆCØ×/Ò/Ñ1Ô1ð Ið I�Ý˜e¥YÑ/Ô/ð IØ$×+Ò+¨s¬xÐ,GÐ,G¸5¼:Ð,GÐ,GÑHÔHÐHøðIð $&Ð Øð 	à+Øñ à—)’)Ð,Ñ-Ô-ñ.ð !ñ!ð %ð $ð 	Ø×!Ò!à"Ø7ðð ñô ð ð Ðr   rN   c              ƒ   ó4   K  — d|                       ¦   «         dœS )z'Format system message for DeepSeek API.ri   r.   )Úget_text_content)rN   s    r   rm   z2DeepSeekMultiAgentFormatter._format_system_message1  s)   è è € ð Ø×+Ò+Ñ-Ô-ð
ð 
ð 	
r   N)T)r\   r]   r^   r_   r   rf   rE   ra   r   r`   r	   rb   r   r[   rp   Úboolrq   Ústaticmethodrm   r   r   r   rd   rd   É   s{  € € € € € € ðð ð
 (- uð*ð Nð(ñ (ô (Ð ð ð ñ ð #˜UØ.Ð.ð<ðñ ô €K��c”ð ð ñ ð  c¤ð ¨t°D¸¸c¸´NÔ/Cð ð ð ð ð>à�3Œiðð 
ˆd�3˜�8ŒnÔ	ðð ð ð ð ð&ð &à�3Œið&ð ð&ð 
ˆd�3˜�8ŒnÔ	ð	&ð &ð &ð &ðP ð
Øð
à	ˆc�3ˆhŒð
ð 
ð 
ñ „\ð
ð 
ð 
r   rd   N)r_   Útypingr   Úpydanticr   Ú_formatter_baser   Ú_loggingr   Úmessager	   r
   r   r   r   r   r   r   rd   r   r   r   ú<module>r…      s  ðà $Ð $Ø Ð Ð Ð Ð Ð à Ð Ð Ð Ð Ð à *Ð *Ð *Ð *Ð *Ð *Ø Ð Ð Ð Ð Ð ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ðrð rð rð rð r˜Mñ rô rð rðjp
ð p
ð p
ð p
ð p
 -ñ p
ô p
ð p
ð p
ð p
r   