o
    i                     @  s  d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
mZ dZdd	 Zd
d Zi dededededededejdejdejdejdejdejdejdejdejdedeZG dd dejZ G dd  d e
Z!d$d"d#Z"dS )%zMath expression node using simpleeval for safe evaluation.

Provides a ComfyMathExpression node that evaluates math expressions
against dynamically-grown numeric inputs.
    )annotationsN)simple_eval)override)ComfyExtensionioi  c                  G  s.   t | dkrt| d drt| d S t| S )z*Support both sum(values) and sum(a, b, c).   r   __iter__)lenhasattrsum)args r   5/mnt/c/Users/fbmor/ComfyUI/comfy_extras/nodes_math.py_variadic_sum   s   r   c                 C  s,   t |tkrtd| dt dt| |S )zWrap pow() with an exponent cap to prevent DoS via huge exponents.

    The ** operator is already guarded by simpleeval's safe_power, but
    pow() as a callable bypasses that guard.
    z	Exponent z exceeds maximum allowed ())absMAX_EXPONENT
ValueErrorpow)baseexpr   r   r   	_safe_pow   s   
r   r   minmaxr   roundr   sqrtceilfloorloglog2log10sincostanintfloatc                   @  s,   e Zd ZdZedddZedddZdS )MathExpressionNodez=Evaluates a math expression against dynamically-grown inputs.return	io.Schemac              
   C  sz   t jjt jdt jt jgttj	dd}t j
dddg dt jjdd	d
dt jjd|dgt jjddt jjddgdS )Nvaluer   )inputnamesr   ComfyMathExpressionzMath Expressionmath)
expressionformula	calculate
calculatorevalr-   r.   za + bT)default	multilinevalues)templateFLOAT)display_nameINT)node_idr8   categorysearch_aliasesinputsoutputs)r   AutogrowTemplateNames	MultiTypeInputFloatIntliststringascii_lowercaseSchemaStringOutput)clsautogrowr   r   r   define_schema?   s"   z MathExpressionNode.define_schemar.   strr5   io.Autogrow.Typeio.NodeOutputc                 C  s   |  stdt|}t| |d< t||td}t|ts't|t	t
fs7td| dt|j d|t|sFtd| d| tt
|t	|S )NzExpression cannot be empty.r5   )r+   	functionszMath Expression 'z)' must evaluate to a numeric result, got z: z ' produced a non-finite result: )stripr   dictrE   r5   r   MATH_FUNCTIONS
isinstanceboolr$   r%   type__name__r-   isfiniter   
NodeOutput)rK   r.   r5   contextresultr   r   r   executeX   s$   
zMathExpressionNode.executeN)r'   r(   )r.   rN   r5   rO   r'   rP   )rX   
__module____qualname____doc__classmethodrM   r]   r   r   r   r   r&   <   s    r&   c                   @  s   e Zd ZedddZdS )MathExtensionr'   list[type[io.ComfyNode]]c                   s   t gS N)r&   )selfr   r   r   get_node_listq   s   zMathExtension.get_node_listN)r'   rc   )rX   r^   r_   r   rf   r   r   r   r   rb   p   s    rb   r'   c                     s   t  S rd   )rb   r   r   r   r   comfy_entrypointv   s   rg   )r'   rb   )#r`   
__future__r   r-   rF   
simpleevalr   typing_extensionsr   comfy_api.latestr   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   rT   	ComfyNoder&   rb   rg   r   r   r   r   <module>   s`    	
4