§
    ÙŽ’jâT  ã                   óì   — d Z ddlZddlZddlm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 ddlmZmZmZmZmZmZmZmZmZ  G d„ dee¦  «        Z G d„ de¦  «        Z G d„ de¦  «        ZdS )zThe Anthropic formatter module.é    N)ÚABC)ÚAny)ÚFieldé   )ÚFormatterBaseé   )Úlogger)Ú_json_loads_with_repair)	ÚMsgÚ	TextBlockÚThinkingBlockÚ	HintBlockÚ	DataBlockÚToolCallBlockÚToolResultBlockÚ	URLSourceÚBase64Sourcec                   ó¦   — e Zd ZdZdee         deeeef                  fd„Z	de
deeef         dz  fd„Zedeez  deeef         fd	„¦   «         ZdS )
Ú_AnthropicFormatterBasezƒMixin for formatting Anthropic formatters to avoid duplication between
    AnthropicChatFormatter and AnthropicMultiAgentFormatter.ÚmsgsÚreturnc           
   ƒ   óœ	  K  — |                       |¦  «         g }|D �]®}g }d}|                     ¦   «         D �]l}|r3|r1t          |t          ¦  «        s|                     d|dœ¦  «         g }d}t          |t
          ¦  «        r%|j        r|                     d|j        dœ¦  «         Œrt          |t          ¦  «        rst          |dd¦  «        }|�|                     d|d	œ¦  «         Œ³t          |d
d¦  «        }|r|                     d|j	        |dœ¦  «         Œåt          j        d¦  «         Œút          |t          ¦  «        �r|r'|rdn|j        }	|                     |	|dœ¦  «         g }d}t          |j        t          ¦  «        r#|                     dd|j        dœgdœ¦  «         �Œvg }
|j        D ]v}t          |t
          ¦  «        r|
                     d|j        dœ¦  «         Œ5t          |t           ¦  «        r,|                      |¦  «        }|r|
                     |¦  «         Œw|
r|                     d|
dœ¦  «         �Œt          |t           ¦  «        r.|                      |¦  «        }|r|                     |¦  «         �ŒUt          |t$          ¦  «        r:|                     d|j        |j        t+          |j        pd¦  «        dœ¦  «         �Œ¤t          |t          ¦  «        �r�|r'|s%|rdn|j        }	|                     |	|dœ¦  «         g }g }|j        }t          |t          ¦  «        r|r|                     d|dœ¦  «         nò|D ]ï}t          |t
          ¦  «        r%|j        r|                     d|j        dœ¦  «         Œ<t          |t           ¦  «        rž|                      |¦  «        }|r|                     |¦  «         Œ~|j        }|j                             d¦  «        d         }t          |t6          ¦  «        rd|› d|j        › d�}nd|› d|j        › d�}|                     d|dœ¦  «         Œð|s|                     dddœ¦  «         |                     d|j        |dœ¦  «         d}�ŒJt          j        dt=          |¦  «        ¦  «         �Œn|r#|rdn|j        }	|                     |	|dœ¦  «         �Œ°|S )áx  Format message objects into Anthropic 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.

        .. note:: Anthropic suggests always passing all previous thinking
         blocks back to the API in subsequent calls to maintain reasoning
         continuity. For more details, please refer to
         `Anthropic's documentation
         <https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking#preserving-thinking-blocks>`_.
        FÚuser©ÚroleÚcontentÚtext©Útyper   Úredacted_thinking_dataNÚredacted_thinking)r    ÚdataÚ	signatureÚthinking)r    r%   r$   zODropping ThinkingBlock without signature; Anthropic requires a valid signature.Útool_usez{})r    ÚidÚnameÚinputú/r   ú[z file returned, URL: ú]z file returned, type: z(empty tool output)Útool_result)r    Útool_use_idr   Tz2Unsupported block type %s in the message, skipped.)Úassert_list_of_msgsÚget_content_blocksÚ
isinstancer   Úappendr   r   r   Úgetattrr%   r	   Údebugr   r   ÚhintÚstrr   Ú_format_anthropic_data_blockr   r'   r(   r
   r)   ÚoutputÚsourceÚ
media_typeÚsplitr   ÚurlÚwarningr    )Úselfr   ÚmessagesÚmsgÚcontent_blocksÚhas_tool_resultÚblockÚredacted_datar$   r   Ú
hint_partsÚsubÚformatted_subÚformatted_blockÚtool_result_contentr8   Ú	out_blockÚ	fmt_blockr9   Ú	main_typeÚfallbacks                        úWC:\Projects\DSCons\.venv\Lib\site-packages\agentscope\formatter\_anthropic_formatter.pyÚ_format_messagesz(_AnthropicFormatterBase._format_messages    sÍ  è è € ð( 	× Ò  Ñ&Ô&Ð&à!ˆØð Z	ñ Z	ˆCØ#%ˆNØ#ˆOà×/Ò/Ñ1Ô1ð Kñ K�à#ð,à&ð,õ 'ØÝ'ñô ð,ð —O’OØ!'°NÐCÐCñô ð ð &(�NØ&+�Oå˜e¥YÑ/Ô/ð |ð
 ”zð Ø&×-Ò-Ø%+°U´ZÐ@Ð@ñô ð øõ   ¥}Ñ5Ô5ð rÝ$+ØØ0Øñ%ô %�Mð
 %Ð0Ø&×-Ò-à(;Ø(5ðð ñô ð ð õ %,Ø!Ø'Ø ñ%ô %˜	ð
 %ð Ø*×1Ò1à,6Ø05´Ø1:ð!"ð !"ñô ð ð õ #œLð!5ñô ð ð õ   ¥yÑ1Ô1ñ JØ%ð 0Ø)8ÐF˜v˜v¸c¼h˜Ø ŸšØ%)°nÐEÐEñô ð ð *,˜Ø*/˜å! %¤*­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à$(×$EÒ$EÀcÑ$JÔ$Jð !.ð $1ð !EØ$.×$5Ò$5°mÑ$DÔ$DÐ$DøØ%ð Ø$ŸOšOØ)/¸JÐ GÐ Gñô ð ùõ   ¥yÑ1Ô1ð fØ&*×&GÒ&GÈÑ&NÔ&N�OØ&ð ?Ø&×-Ò-¨oÑ>Ô>Ð>ùå ¥}Ñ5Ô5ð aØ"×)Ò)à$.Ø"'¤(Ø$)¤Jõ &=Ø %¤Ð 3¨tñ&ô &ðð ñô ð ñ õ    ¥Ñ7Ô7ñ Pð &ð ,¨oð ,Ø)8ÐF˜v˜v¸c¼h˜Ø ŸšØ%)°nÐEÐEñô ð ð *,˜à68Ð'Ø"œ\�FÝ! &­#Ñ.Ô.ð &&Ø!ð Ø/×6Ò6Ø)/¸Ð @Ð @ñô ð øð *0ð  &ð  &˜IÝ)¨)µYÑ?Ô?ð &ð $-¤>ð !&Ø$7×$>Ò$>à4:Ø4=´Nð)*ð )*ñ%&ô %&ð %&øõ ",¨IµyÑ!AÔ!Að &Ø,0×,MÒ,MØ$-ñ-"ô -" 	ð $-ð !&Ø$7×$>Ò$>¸yÑ$IÔ$IÐ$IÐ$Ià-6Ô-= FØ06Ô0A×0GÒ0GÈÑ0LÔ0LÈQÔ0O IÝ'1°&½)Ñ'DÔ'Dð 	%*ð-B°	ð -Bð -BØ4:´Jð-Bð -Bð -Bð )1¨ð-J°	ð -Jð -JØ5;Ô5Fð-Jð -Jð -Jð )1ð %8×$>Ò$>Ø17ÀÐ(JÐ(Jñ%&ô %&ð %&øð /ð Ø+×2Ò2Ø%+Ð5JÐKÐKñô ð ð #×)Ò)à$1Ø+0¬8Ø':ðð ñô ð ð '+�O‘Oõ ”NØLÝ˜U™œñô ð ñ ð
 ð 	ð "1Ð>�v�v°c´h�Ø—’à $Ø#1ðð ñô ð ùð ˆó    rC   Nc                 ó  ‡— |j         }|j        Št          ˆfd„| j        D ¦   «         ¦  «        st	          j        d‰¦  «         dS ‰                     d¦  «        st	          j        d‰¦  «         dS |                      |¦  «        S )a-  Format a DataBlock into Anthropic API format.

        Args:
            block (`DataBlock`):
                The data block to format.

        Returns:
            `dict[str, Any] | None`:
                The formatted data block, or None if the media type is not
                supported.
        c              3   óB   •K  — | ]}t          j         ‰|¦  «        V — Œd S )N)Úfnmatch)Ú.0Úpatternr:   s     €rN   ú	<genexpr>zG_AnthropicFormatterBase._format_anthropic_data_block.<locals>.<genexpr>(  sC   øè è € ð 
ð 
àõ ŒO˜J¨Ñ0Ô0ð
ð 
ð 
ð 
ð 
ð 
rP   z(Media type %s is not supported, skipped.Nzimage/z4Anthropic only supports image data, got %s, skipped.)r9   r:   ÚanyÚsupported_input_media_typesr	   r=   Ú
startswithÚ_format_image_source)r>   rC   r9   r:   s      @rN   r7   z4_AnthropicFormatterBase._format_anthropic_data_block  sÁ   ø€ ð ”ˆØÔ&ˆ
õ ð 
ð 
ð 
ð 
àÔ;ð
ñ 
ô 
ñ 
ô 
ð 	õ ŒNØ:Øñô ð ð �4ð ×$Ò$ XÑ.Ô.ð 	ÝŒNØFØñô ð ð �4à×(Ò(¨Ñ0Ô0Ð0rP   r9   c                 óÞ  — t          | t          ¦  «        rdd| j        | j        dœdœS t          | 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d| j        |dœdœS t          j        |d	¬
¦  «        }|                     ¦   «          t          j        |j        ¦  «                             d¦  «        }dd| j        |dœdœS t%          dt'          | ¦  «        › �¦  «        ‚)a  Format an image source into Anthropic API format.

        Args:
            source (`URLSource | Base64Source`):
                The image source to format.

        Returns:
            `dict[str, Any]`:
                The formatted image source.
        ÚimageÚbase64)r    r:   r#   )r    r9   zfile://Úrbzutf-8Né   )ÚtimeoutzUnsupported source type: )r1   r   r:   r#   r   r6   r<   rY   ÚremoveprefixÚopenr]   Ú	b64encodeÚreadÚdecodeÚrequestsÚgetÚraise_for_statusr   Ú
ValueErrorr    )r9   r<   Ú	file_pathÚfr#   Úresponses         rN   rZ   z,_AnthropicFormatterBase._format_image_source<  sÜ  € õ �f�lÑ+Ô+ð &	Iàà$Ø"(Ô"3Ø"œKðð ðð ð õ ˜¥	Ñ*Ô*ñ 	IÝ�f”j‘/”/ˆCØ�~Š~˜iÑ(Ô(ð à×,Ò,¨YÑ7Ô7�	Ý˜) TÑ*Ô*ð F¨aÝ!Ô+¨A¯FªF©H¬HÑ5Ô5×<Ò<¸WÑEÔE�DðFð Fð Fñ Fô Fð Fð Fð Fð Fð Fð Føøøð Fð Fð Fð Fð $à (Ø&,Ô&7Ø $ðð ðð ð õ $œ<¨°RÐ8Ñ8Ô8�Ø×)Ò)Ñ+Ô+Ð+ÝÔ'¨Ô(8Ñ9Ô9×@Ò@ÀÑIÔI�à#à (Ø&,Ô&7Ø $ðð ðð ð õ ÐG½¸f¹¼ÐGÐGÑHÔHÐHs   Â:CÃCÃC)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úlistr   Údictr6   r   rO   r   r7   Ústaticmethodr   r   rZ   © rP   rN   r   r      s×   € € € € € ð@ð @ðsà�3Œiðsð 
ˆd�3˜�8ŒnÔ	ðsð sð sð sðj%1àð%1ð 
ˆc�3ˆhŒ˜$Ñ	ð%1ð %1ð %1ð %1ðN ð3IØ˜LÑ(ð3Ià	ˆc�3ˆhŒð3Ið 3Ið 3Iñ „\ð3Ið 3Ið 3IrP   r   c                   ó~   — 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 )
ÚAnthropicChatFormatterz¹The Anthropic 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dgS ©Nz
text/plainzimage/*rt   rt   rP   rN   ú<lambda>zAnthropicChatFormatter.<lambda>z  ó   €  ¨yÐ 9€ rP   úEThe supported input types. Defaults to ``["text/plain", "image/*"]``.©Údefault_factoryÚdescriptionÚinput_typesr   r   c              ƒ   ó<   K  — |                       |¦  «        ƒ d{V —†S )r   N)rO   )r>   r   s     rN   ÚformatzAnthropicChatFormatter.format�  s.   è è € ð( ×*Ò*¨4Ñ0Ô0Ð0Ð0Ð0Ð0Ð0Ð0Ð0rP   N)rm   rn   ro   rp   r   r   rq   r6   Ú__annotations__r   rr   r   r�   rt   rP   rN   rv   rv   s  s�   € € € € € € ðð ð
 #˜UØ9Ð9ð9ðñ ô €K��c”ð ð ñ ð1à�3Œið1ð 
ˆd�3˜�8ŒnÔ	ð1ð 1ð 1ð 1ð 1ð 1rP   rv   c            	       ó  — 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de	eeef                  fd„Zede
deeef         fd„¦   «         ZdS )ÚAnthropicMultiAgentFormatterzmAnthropic 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dgS rx   rt   rt   rP   rN   ry   z%AnthropicMultiAgentFormatter.<lambda>§  rz   rP   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 )	zmFormat input messages into the structure required by the Anthropic
        API for multi-agent conversations.r   ÚsystemNr   TÚtool_sequenceÚagent_messageF)	r/   Úlenr   r2   Ú_format_system_messageÚ_group_messagesÚextendrO   Ú_format_agent_message)r>   r   Úformatted_msgsÚstart_indexÚis_first_agent_messageÚtypÚgroups          rN   r�   z#AnthropicMultiAgentFormatter.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ØØ$�_’_�_�_Ø"×)Ò)Ø"×3Ò3°EÑ:Ô:Ð:Ð:Ð:Ð:Ð:Ð:ñô ð ð ð %’_�_Ø"×)Ò)Ø"×8Ò8Ø!Ø2ñô ð ð ð ð ð ð ñô ð ð .3Ð*Ð*ð %ð !Ið Ðs   ÂDÚis_firstc              ƒ   óÆ  K  — g }g }|D ]õ}|j         pd}g }|                     ¦   «         D ]¢}t          |t          ¦  «        r|                     |j        ¦  «         Œ2t          |t          ¦  «        r[|                      |¦  «        }	|	rD|r-|                     dd                     |¦  «        dœ¦  «         g }|                     |	¦  «         Œ£|r/|› dd                     |¦  «        › �}
|                     |
¦  «         Œö|r+|                     dd                     |¦  «        dœ¦  «         |r«|r©|d          	                    d¦  «        r#| j
        dz   |d         d         z   |d         d<   n!|                     dd| j
        dz   dœ¦  «         |d	          	                    d¦  «        r|d	         dxx         d
z  cc<   n|                     dddœ¦  «         |rd|dœgS g S )z0Format agent messages into conversation history.ÚAgentr   ú
r   z: ú r   z
<history>
éÿÿÿÿz
</history>z
</history>r   r   )r(   r0   r1   r   r2   r   r   r7   Újoinrg   r†   Úinsert)r>   r   r–   Úconversation_blocksÚaccumulated_textr@   Ú
agent_nameÚagent_text_partsrC   rH   r‹   s              rN   r�   z2AnthropicMultiAgentFormatter._format_agent_messageÍ  s¨  è è € ð !ÐØÐàð 	7ð 	7ˆCØœÐ, WˆJØ!Ðà×/Ò/Ñ1Ô1ð Dð D�Ý˜e¥YÑ/Ô/ð DØ$×+Ò+¨E¬JÑ7Ô7Ð7Ð7Ý ¥yÑ1Ô1ð DØ&*×&GÒ&GÈÑ&NÔ&N�OØ&ð 	DØ+ð 2Ø/×6Ò6à,2Ø,0¯IªIÐ6FÑ,GÔ,Gð!"ð !"ñô ð ð 02Ð,Ø+×2Ò2°?ÑCÔCÐCøàð 7Ø#-Ð MÐ M°·²Ð:JÑ1KÔ1KÐ MÐ M�Ø ×'Ò'¨Ñ6Ô6Ð6øàð 	Ø×&Ò&à"Ø ŸIšIÐ&6Ñ7Ô7ðð ñô ð ð ð 	 8ð 	Ø" 1Ô%×)Ò)¨&Ñ1Ô1ð àÔ4Ø#ñ$à)¨!Ô,¨VÔ4ñ5ð $ AÔ& vÑ.Ð.ð $×*Ò*Øà &Ø $Ô @Ø'ñ!(ðð ñô ð ð # 2Ô&×*Ò*¨6Ñ2Ô2ð Ø# BÔ'¨Ð/Ð/Ô/°>ÑAÐ/Ð/Ñ/Ð/à#×*Ò*Ø#¨\Ð:Ð:ñô ð ð ð 	ð #Ø2ðð ðð ð ˆ	rP   r@   c              ƒ   óÎ   K  — g }|                       ¦   «         D ]1}t          |t          ¦  «        r|                     |j        ¦  «         Œ2ddd                     |¦  «        dœgdœS )zFormat a system message.r‰   r   r™   r   r   )r0   r1   r   r2   r   rœ   )r@   Ú
text_partsrC   s      rN   r�   z3AnthropicMultiAgentFormatter._format_system_message  sˆ   è è € ð ˆ
Ø×+Ò+Ñ-Ô-ð 	.ð 	.ˆEÝ˜%¥Ñ+Ô+ð .Ø×!Ò! %¤*Ñ-Ô-Ð-øð ð #Ø ŸIšI jÑ1Ô1ðð ðð
ð 
ð 	
rP   N)rm   rn   ro   rp   r   r†   r6   r‚   r   rq   r   rr   r   r�   Úboolr�   rs   r�   rt   rP   rN   r„   r„   ˜  s?  € € € € € € ðð ð (- uð*ð Nð(ñ (ô (Ð ð ð ñ ð #˜UØ9Ð9ð9ðñ ô €K��c”ð ð ñ ð  c¤ð ¨t°D¸¸c¸´NÔ/Cð ð ð ð ð>Ià�3ŒiðIð ðIð 
ˆd�3˜�8ŒnÔ	ð	Ið Ið Ið IðV ð
¨#ð 
°$°s¸C°x´.ð 
ð 
ð 
ñ „\ð
ð 
ð 
rP   r„   )rp   r]   rS   Úabcr   Útypingr   rf   Úpydanticr   Ú_formatter_baser   Ú_loggingr	   Ú_utils._commonr
   Úmessager   r   r   r   r   r   r   r   r   r   rv   r„   rt   rP   rN   ú<module>r¬      s¨  ðà %Ð %Ø €€€Ø €€€Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð à *Ð *Ð *Ð *Ð *Ð *Ø Ð Ð Ð Ð Ð Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4ð
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ðUIð UIð UIð UIð UI˜m¨Sñ UIô UIð UIðp
"1ð "1ð "1ð "1ð "1Ð4ñ "1ô "1ð "1ðJP
ð P
ð P
ð P
ð P
Ð#:ñ P
ô P
ð P
ð P
ð P
rP   