
    +j-                        % S r SSKJr  SSKJrJrJr  SSKJrJ	r	J
r
JrJrJrJrJrJrJr  SSKJrJrJrJrJr  SrSrSrS	S
SSSS.r\\\4   \S'   S1r1 Skr SSSS.r!\\\"4   \S'   / SQr#S\4S jr$S\S\%\&\&4   S-  4S jr'S\S\&S-  4S jr(S\S\4S  jr)S!\*\RV                     S"\,\RZ                     S\,\   4S# jr. " S$ S%\RV                  5      r/ " S& S'\5      r0S\04S( jr1g))z^API Nodes for Anthropic Claude (Messages API). See: https://docs.anthropic.com/en/api/messages    )override)IOComfyExtensionInput)
AnthropicImageContentAnthropicImageSourceUrlAnthropicMessageAnthropicMessagesRequestAnthropicMessagesResponseAnthropicOutputConfigAnthropicResponseTextBlockAnthropicRoleAnthropicTextContentAnthropicThinkingConfig)ApiEndpointget_number_of_imagessync_opupload_images_to_comfyapivalidate_stringz/proxy/anthropic/v1/messagesi %    zclaude-opus-4-7zclaude-opus-4-6zclaude-sonnet-4-6zclaude-sonnet-4-5-20250929zclaude-haiku-4-5-20251001)Opus 4.7Opus 4.6
Sonnet 4.6z
Sonnet 4.5	Haiku 4.5CLAUDE_MODELSr   >   r   r   r   i   i    i @  )lowmediumhigh_REASONING_BUDGET)offr   r   r   model_labelc                    [         R                  R                  SSSSSSS9[         R                  R                  SS	S
S	SSSS9/nU [        ;  a4  UR                  [         R                  R                  S[        SSSS95        U$ )N
max_tokens   i   i   zNMaximum number of tokens to generate (includes reasoning tokens when enabled).T)defaultminmaxtooltipadvancedtemperature      ?g        g{Gz?zControls randomness. 0.0 is deterministic, 1.0 is most random. Ignored for Opus 4.7 and any model when reasoning_effort is set.)r%   r&   r'   stepr(   r)   reasoning_effortr    z3Extended thinking effort. 'off' disables reasoning.)optionsr%   r(   r)   )r   Intr   Float_THINKING_UNSUPPORTEDappendCombo_REASONING_EFFORTS)r!   inputss     ?/home/wildlama/comfy/ComfyUI/comfy_api_nodes/nodes_anthropic.py_claude_model_inputsr7   5   s    
d 	 	
 	S  	 	
F, //HHNN"*M  	
 M    modelreturnNc                 F    SU ;   d  SU ;   d  SU ;   a  gSU ;   a  gSU ;   a  gg	)
zPReturn (input_per_1M, output_per_1M) USD for a Claude model, or None if unknown.zopus-4-7zopus-4-6zopus-4-5)      @g      9@zsonnet-4)g      @g      .@z	haiku-4-5)r+   r<   N )r9   s    r6   _model_price_per_millionr>   Y   s4    UjE1Z55HUer8   responsec                 j   U R                   (       a  U R                  (       d  g[        U R                  5      nUc  gUu  p#U R                   R                  =(       d    SnU R                   R                  =(       d    SnU R                   R
                  =(       d    SnSnSnU R                   R                  (       aR  U R                   R                  R                  =(       d    SnU R                   R                  R                  =(       d    SnXB-  XS-  -   Xb-  S-  -   Xr-  S-  -   X-  S-  -   n	U	S-  $ )zXCompute approximate USD price from response usage. Server-side billing is authoritative.Nr   g?g      ?g       @g    .A)	usager9   r>   input_tokensoutput_tokenscache_read_input_tokenscache_creationephemeral_5m_input_tokensephemeral_1h_input_tokens)
r?   rates
input_rateoutput_raterB   rC   
cache_readcache_5mcache_1htotals
             r6   calculate_tokens_pricerO   d   s   >>$X^^4E}#J>>..3!LNN005AM77<1JHH~~$$>>00JJOa>>00JJOa!

%	&

!C
'	( 
$
&	' 
#
%		& 
 ;r8   c                 j    U R                   (       d  gSR                  S U R                    5       5      $ )N 
c              3      #    U  H;  n[        U[        5      (       d  M  UR                  (       d  M-  UR                  v   M=     g 7fN)
isinstancer   text).0blocks     r6   	<genexpr>*_get_text_from_response.<locals>.<genexpr>   s4       0ue78 	=BZZ 	

 0s   AAA)contentjoin)r?   s    r6   _get_text_from_responser]   ~   s2    99  ( 0 0  r8   clsimage_tensorsc           	         #    [        U U[        [        SS9I S h  vN nU Vs/ s H  n[        [	        US9S9PM     sn$  N%s  snf 7f)NzUploading reference images)
max_imagestotal_pixels
wait_label)url)source)r   CLAUDE_MAX_IMAGESANTHROPIC_IMAGE_MAX_PIXELSr   r   )r^   r_   urlsrd   s       r6   _build_image_content_blocksri      sW      +$// D W[[VZs!)@S)IJVZ[[ \s   AAAAAAc                   v    \ rS rSrSr\S 5       r\  SS\S\S\	S\S-  S	\S
\
R                  4S jj5       rSrg)
ClaudeNode   z7Generate text responses from an Anthropic Claude model.c                    [         R                  " SSSSS[         R                  R                  SSSS	S
9[         R                  R                  S[
         Vs/ s H,  n[         R                  R                  U[        U5      5      PM.     snSS9[         R                  R                  SSSSSSS9[         R                  R                  S[         R                  R                  [         R                  R                  S5      [        S[        S-   5       Vs/ s H  nSU 3PM
     snSS9S[         S3S9[         R                  R                  SSSSSSS9/[         R                  R                  5       /[         R                  R                   [         R                  R"                  [         R                  R$                  /S[         R&                  " [         R(                  " S/S9SS 9S!9
$ s  snf s  snf )"Nrk   zAnthropic Claudezpartner/text/AnthropiczText GenerationzGenerate text responses with Anthropic's Claude models. Provide a text prompt and optionally one or more images for multimodal context.promptTrQ   zText input to the model.)	multiliner%   r(   r9   z/The Claude model used to generate the response.)r.   r(   seedr   iz_Seed controls whether the node should re-run; results are non-deterministic regardless of seed.)r%   r&   r'   control_after_generater(   imagesimage   image_)namesr&   z9Optional image(s) to use as context for the model. Up to z images.)templater(   system_promptz<Foundational instructions that dictate the model's behavior.)ro   r%   optionalr)   r(   )widgetsa  
                (
                  $m := widgets.model;
                  $contains($m, "opus") ? {
                    "type": "list_usd",
                    "usd": [0.005, 0.025],
                    "format": { "approximate": true, "separator": "-", "suffix": " per 1K tokens" }
                  }
                  : $contains($m, "sonnet") ? {
                    "type": "list_usd",
                    "usd": [0.003, 0.015],
                    "format": { "approximate": true, "separator": "-", "suffix": " per 1K tokens" }
                  }
                  : $contains($m, "haiku") ? {
                    "type": "list_usd",
                    "usd": [0.001, 0.005],
                    "format": { "approximate": true, "separator": "-", "suffix": " per 1K tokens" }
                  }
                  : {"type":"text", "text":"Token-based"}
                )
                )
depends_onexpr)
node_iddisplay_namecategoryessentials_categorydescriptionr5   outputshiddenis_api_nodeprice_badge)r   SchemaStringr   DynamicCombor   Optionr7   r/   AutogrowTemplateNamesImagerangerf   OutputHiddenauth_token_comfy_orgapi_key_comfy_org	unique_id
PriceBadgePriceBadgeDepends)r^   labelis      r6   define_schemaClaudeNode.define_schema   s   yy +- 1^ 		"6	    %% &3%2E ..u6J56QR%2 N &  "+/H   !![[66w/5:1>ORS>S5TU5Ts|5TU 7 
 XXiWjjrs "  		#"!!Z   C)T YY%%'(		..		++		##
 //	BqP
 P	
 & Vs   3G,G1$Nrn   r9   rp   rr   rx   r:   c                   #    [        USSS9  US   nUR                  SS5      nUR                  SS5      nUS	;  =(       a	    U[        ;  n	U	(       d  US
:X  a  S n
OUR                  SS5      n
S nS nU	(       aI  U[        ;   a  [	        SS9n[        US9nO,[        U   n[        U[        SUS-
  5      5      n[	        SUS9nU=(       d    0 R                  5        Vs/ s H	  oc  M  UPM     nn[        S U 5       5      [        :  a  [        S[         S35      e/ nU(       a"  UR                  [        X5      I S h  vN 5        UR                  [!        US95        [#        U [%        [&        SS9[(        [+        [,        U   U[/        [0        R2                  US9/U=(       d    S U
UUS9[4        S9I S h  vN n[6        R8                  " [;        U5      =(       d    S5      $ s  snf  N N37f)NTrt   )strip_whitespace
min_lengthr9   r#   r$   r-   r    )r    Nr   r*   r+   adaptive)type)efforti   enabled)r   budget_tokensc              3   8   #    U  H  n[        U5      v   M     g 7frT   )r   )rW   ts     r6   rY   %ClaudeNode.execute.<locals>.<genexpr>  s     >1#A&&s   zUp to z" images are supported per request.)rV   POST)pathmethod)roler[   )r9   r#   messagessystemr*   thinkingoutput_config)response_modeldataprice_extractorz!Empty response from Claude model.)r   getr1   _ADAPTIVE_THINKING_MODELSr   r   r   r&   r'   valuessumrf   
ValueErrorextendri   r2   r   r   r   ANTHROPIC_MESSAGES_ENDPOINTr   r
   r   r	   r   userrO   r   
NodeOutputr]   )r^   rn   r9   rp   rr   rx   r!   r#   r-   thinking_enabledr*   thinking_cfg
output_cfgbudgetr   r_   r[   r?   s                     r6   executeClaudeNode.execute   s     	!DGnYY|U3
 99%7?+=@m[XmEm {j8K))M37K7;37
776JG2:JK
 ++;<VSzD/@%AB6IU[\8>"7L7L7N+`7N!A7N+`>>>ARRv&7%88Z[\\FHNN!<S!PPQ+89 8H4)#K0%*0B0BGTU$,'%( 3
 
 }}4X>eBeff1 ,a Q
s8   CG-G$'G$-AG->G)?A8G-7G+82G-+G-r=   )NrQ   )__name__
__module____qualname____firstlineno____doc__classmethodr   strdictintr   r   r   __static_attributes__r=   r8   r6   rk   rk      s    AQ
 Q
f  #:g:g :g 	:g
 t:g :g 
:g :gr8   rk   c                   L    \ rS rSr\S\\\R                        4S j5       r	Sr
g)AnthropicExtensioni+  r:   c                    #    [         /$ 7frT   )rk   )selfs    r6   get_node_list AnthropicExtension.get_node_list,  s     |s   
r=   N)r   r   r   r   r   listr   r   	ComfyNoder   r   r=   r8   r6   r   r   +  s)    T$r||*<%=  r8   r   c                     #    [        5       $ 7frT   )r   r=   r8   r6   comfy_entrypointr   1  s     s   )2r   typing_extensionsr   comfy_api.latestr   r   r   comfy_api_nodes.apis.anthropicr   r   r	   r
   r   r   r   r   r   r   comfy_api_nodes.utilr   r   r   r   r   r   rg   rf   r   r   r   __annotations__r1   r   r   r   r4   r7   tuplefloatr>   rO   r]   r   r   r   r   ri   rk   r   r   r=   r8   r6   <module>r      se   d & 6 6    = (   "!%.,!tCH~  %  C 
 % 4S> 
 6 !c !HC E%,,?$,F %> 54< 4&? C \	bll	\$\ 

 \Rg Rgj   2  r8   