
    g                        d dl mZ d dlmZmZ d dlZd dlmZmZm	Z	 ddl
mZmZ erd dlmZ d dlmZ d	d
lmZ dZddZddZ G d de      ZddZ	 	 ddZy)    )annotations)TYPE_CHECKINGAnyN)Message	TextBlock	TextDelta   )EndpointConfigStreamState)FinalRequestOptions)	ResponseT   )LogfireSpan)get_endpoint_configon_responseis_async_clientc                    | j                   }| j                  }t        |t              si }|dk(  rt	        dd|it
              S t	        d||d      S )zJReturns the endpoint config for Anthropic or Bedrock depending on the url.z/v1/messagesz$Message with {request_data[model]!r}request_data)message_template	span_datastream_state_clszAnthropic API call to {url!r})r   url)r   r   )r   	json_data
isinstancedictr
   AnthropicMessageStreamState)optionsr   r   s      k/var/www/openai/venv/lib/python3.12/site-packages/logfire/_internal/integrations/llm_providers/anthropic.pyr   r      sb    
++C!!Ii&	
nC%y18
 	
 <'0=
 	
    c                    t        | d      r2t        | j                  t              r| j                  j                  S d S t        | d      r2t        | j
                  t              r| j
                  j                  S d S y )Ncontent_blockdelta)hasattrr   r!   r   textr"   r   )chunks    r   content_from_messagesr&   ,   sb    uo&+5e6I6I9+Uu""''_[__ug#-ekk9#Eu{{O4Or   c                  "    e Zd Zd ZddZddZy)r   c                    g | _         y N)_contentselfs    r   __init__z$AnthropicMessageStreamState.__init__5   s	    #%r   c                V    t        |      }|r| j                  j                  |       y y r)   )r&   r*   append)r,   r%   contents      r   record_chunkz(AnthropicMessageStreamState.record_chunk8   s&    '.MM  ) r   c                d    dj                  | j                        t        | j                        dS )N )combined_chunk_contentchunk_count)joinr*   lenr+   s    r   get_response_dataz-AnthropicMessageStreamState.get_response_data=   s'    *,''$--*@QTUYUbUbQcddr   N)r%   "anthropic.types.MessageStreamEventreturnNone)r:   r   )__name__
__module____qualname__r-   r1   r8    r   r   r   r   4   s    &*
er   r   c                L   t        | t              r| j                  d   }ddi}|j                  dk(  r|j                  |d<   n=| j                  D cg c]$  }d|j                  dh      |j                  d	i& c}|d
<   |j                  d|| j                  d       | S c c}w )z/Updates the span based on the type of response.r   role	assistantr$   r0   functioninput)include)	argumentsname
tool_callsresponse_data)messageusage)	r   r   r0   typer$   model_dump_jsonrG   set_attributerK   )responsespanblockrJ   s       r   r   r   A   s    (G$  ##);"7::!&GI &--% .E %*%:%:G9%:%M %

! .%GL! 	?(..,YZO%s   )B!c                    t        | t        j                  t        j                  f      ryt        | t        j                  t        j
                  f      s
J d|         y)z3Returns whether or not the `client` class is async.FzYExpected Anthropic, AsyncAnthropic, AnthropicBedrock or AsyncAnthropicBedrock type, got: T)
issubclass	anthropic	AnthropicAnthropicBedrockAsyncAnthropicAsyncAnthropicBedrock)clients    r   r   r   V   sb     &9..	0J0JKL))9+J+JK l	bcibjkl  r   )r   r   r:   r
   )r%   r9   r:   z
str | None)rO   r   rP   r   r:   r   )rY   ztype[anthropic.Anthropic] | type[anthropic.AsyncAnthropic] | type[anthropic.AnthropicBedrock] | type[anthropic.AsyncAnthropicBedrock])
__future__r   typingr   r   rT   anthropic.typesr   r   r   typesr
   r   anthropic._modelsr   anthropic._typesr   mainr   __all__r   r&   r   r   r   r?   r   r   <module>rb      sV    " %  9 9 .5*#
*
e+ 
e*,r   