o
    i"                    @   s@  d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	m
Z
mZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. ddl/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< ddl=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZN deOd	ePfd
dZQdZRdeR dZSdeR dZTdeR dZUdeR dZVdeR dZWdeR dZXdeR dZYdeR dZZdZ[dZ\dZ]dZ^dZ_dZ`dZadZbdZcdZddZedZfdd d!d"d#d$d%d&d'd(d)
Zg	 d*d+d,d-d.d/d'd(d0Zh	 i d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRi dSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcdddedfdgdhdidjdkdldmdndodpdqdrdsdti dudvdwdxdydzd{d|d}d~ddddddddddddddddddddddddddddddddddd
Zidejd	ejfddZkdelejm de;d	ejnfddZodePep d	eqfddZrded	eqfddZsded	eqfddZtde d	eqfddZudejdejdeOd	eqfddZvdddZwdddZxdddZydejzd	dfddZ{d	e!fddZ|d	ejdB fddZ}d	ePe" fddĄZ~d	ejdB fddƄZdePe" d	ejzfddɄZ	ddelejm dejdejdepdejdejdejdejdedB d	ejnfddфZ		ddelejm dejzdejdejdejdepdejdejdejdedB dejzdB d	ejnfddՄZ		ddelejm deqde)e*B dejdedejzdejzdB dedB d	eejejejf fddۄZ						ddelejm de
jde
jdB dejdB dejdB dejdB depdB dejdB d	ejnfddZG dd dejmZG dd dejmZG dd dejmZG dd dejmZG dd dejmZG dd dejmZG dd dejmZG dd dejmZG dd dejmZG dd dejmZG dd dejmZG dd dejmZG dd dejmZG dd dejmZG d d dejmZG dd dejmZG dd dejmZG dd dejmZG dd	 d	ejmZG d
d dejmZG dd dejmZG dd dejmZG dd dejmZG dd dejmZG dd dejmZG dd de	Zd	efddZdS (  zKling API Nodes

For source of truth on the allowed permutations of request fields, please reference:
- [Compatibility Table](https://app.klingai.com/global/dev/document-api/apiReference/model/skillsMap)
    N)override)IOComfyExtensionInput	InputImpl)"KlingCameraControlKlingCameraConfigKlingCameraControlTypeKlingVideoGenDurationKlingVideoGenModeKlingVideoGenAspectRatioKlingVideoGenModelNameKlingText2VideoRequestKlingText2VideoResponseKlingImage2VideoRequestKlingImage2VideoResponseKlingVideoExtendRequestKlingVideoExtendResponseKlingLipSyncVoiceLanguageKlingLipSyncInputObjectKlingLipSyncRequestKlingLipSyncResponseKlingVirtualTryOnModelNameKlingVirtualTryOnRequestKlingVirtualTryOnResponseKlingVideoResultKlingImageResultKlingImageGenerationsRequestKlingImageGenerationsResponseKlingImageGenImageReferenceTypeKlingImageGenAspectRatioKlingVideoEffectsRequestKlingVideoEffectsResponseKlingDualCharacterEffectsSceneKlingSingleImageEffectsSceneKlingDualCharacterEffectInputKlingSingleImageEffectInputKlingCharacterEffectModelNameKlingSingleImageEffectModelName)ImageToVideoWithAudioRequestKlingAvatarRequestMotionControlRequestMultiPromptEntryOmniImageParamImageOmniParamImageOmniParamVideoOmniProFirstLastFrameRequestOmniProImageRequestOmniProReferences2VideoRequestOmniProText2VideoRequestTaskStatusResponseTextToVideoWithAudioRequest)ApiEndpointdownload_url_to_image_tensordownload_url_to_video_outputget_number_of_imagespoll_opsync_optensor_to_base64_stringupload_audio_to_comfyapiupload_image_to_comfyapiupload_images_to_comfyapiupload_video_to_comfyapivalidate_audio_durationvalidate_image_aspect_ratiovalidate_image_dimensionsvalidate_stringvalidate_video_dimensionsvalidate_video_durationcountreturnc                 C   sr   g }t d| d D ]-}|tjjd| dddd| ddtjjd| d	d
ddtjjd| ddg q	|S )N   storyboard__promptT zPrompt for storyboard segment z. Max 512 characters.)	multilinedefaulttooltip	_duration      z Duration for storyboard segment z in seconds.)rN   minmaxdisplay_moderO   )rangeextendr   Stringr   IntNumberDisplayslider)rG   inputsi r^   9/mnt/c/Users/fbmor/ComfyUI/comfy_api_nodes/nodes_kling.py_generate_storyboard_inputsW   s(   



r`   v1z/proxy/kling/z/videos/text2videoz/videos/image2videoz/videos/video-extendz/videos/lip-syncz/videos/effectsz/images/generationsz/images/kolors-virtual-try-on	  i     x   i?     i         i@  )std5
kling-v1-6)rh   10rj   )prori   kling-v2-master)rl   rk   rm   )rh   ri   rm   )rh   rk   rm   )rl   ri   kling-v2-1-master)rl   rk   rn   )rl   ri   kling-v2-5-turbo)rl   rk   ro   )
z(standard mode / 5s duration / kling-v1-6z)standard mode / 10s duration / kling-v1-6z(pro mode / 5s duration / kling-v2-masterz)pro mode / 10s duration / kling-v2-masterz-standard mode / 5s duration / kling-v2-masterz.standard mode / 10s duration / kling-v2-masterz*pro mode / 5s duration / kling-v2-1-masterz+pro mode / 10s duration / kling-v2-1-master)pro mode / 5s duration / kling-v2-5-turbo*pro mode / 10s duration / kling-v2-5-turbo)rl   ri   
kling-v1-5)rl   rk   rr   )rl   ri   rj   )rl   rk   rj   )rl   ri   
kling-v2-1)rl   rk   rs   )z#pro mode / 5s duration / kling-v1-5z$pro mode / 10s duration / kling-v1-5z#pro mode / 5s duration / kling-v1-6z$pro mode / 10s duration / kling-v1-6z#pro mode / 5s duration / kling-v2-1z$pro mode / 10s duration / kling-v2-1rp   rq   Melody)girlfriend_4_speech02enSunny)genshin_vindi2rv   Sage)zhinen_xueshengrv   Ace)AOTrv   Blossom)
ai_shatangrv   Peppy)genshin_klee2rv   Dove)genshin_kirararv   Shine)ai_kaiyarv   Anchor)oversea_male1rv   Lyric)ai_chenjiahao_712rv   Tender)chat1_female_new-3rv   Siren)zchat_0407_5-1rv   Zippy)cartoon-boy-07rv   Bud)uk_boy1rv   Sprite)cartoon-girl-01rv   Candy)PeppaPig_platformrv   Beacon)ai_huangzhong_712rv   Rock)ai_huangyaoshi_712rv   Titan)ai_laoguowang_712rv   Grace)chengshu_jiejierv   Helen)you_pingjingrv   Lore)calm_story1rv   Crag)uk_man2rv   Prattle)laopopo_speech02rv   Hearth)heainainai_speech02rv   z
The Reader)zreader_en_m-v1rv   zCommercial Lady)zcommercial_lady_en_f-v1rv   u   阳光少年)rx   zhu   懂事小弟)rz   r   u   运动少年)tiyuxi_xuedir   u   青春少女)r~   r   u   温柔小妹)r   r   u   元气少女)r   r   u   阳光男生)r   r   u   幽默小哥)tiexin_nanyour   u   文艺小哥)r   r   u   甜美邻家)girlfriend_1_speech02r   u   温柔姐姐)r   r   u   职场女青)girlfriend_2_speech02r   u   活泼男童)r   r   u   俏皮女童)r   r   u   稳重老爸)r   r   u   温柔妈妈)r   r   u   严肃上司)r   r   u   优雅贵妇)r   r   u   慈祥爷爷)zhuxi_speech02r   u   唠叨爷爷)
uk_oldman3r   u   唠叨奶奶)r   r   u   和蔼奶奶)r   r   u   东北老铁)dongbeilaotie_speech02r   u   重庆小伙)chongqingxiaohuo_speech02r   )chuanmeizi_speech02r   )chaoshandashu_speech02r   )ai_taiwan_man2_speech02r   )xianzhanggui_speech02r   )tianjinjiejie_speech02r   )zdiyinnansang_DB_CN_M_04-v2r   )zyizhipiannan-v1r   )ztianmeixuemei-v1r   )zdaopianyansang-v1r   )z	mengwa-v1r   )
u   四川妹子u   潮汕大叔u   台湾男生u   西安掌柜u   天津姐姐u   新闻播报男u   译制片男u   撒娇女友u   刀片烟嗓u   乖巧正太promptc                 C   s4   | s| S dd }dd }t d|| } t d|| S )av  
    Rewrites Kling Omni-style placeholders used in the app, like:

        @image, @image1, @image2, ... @imageN
        @video, @video1, @video2, ... @videoN

    into the API-compatible form:

        <<<image_1>>>, <<<image_2>>>, ...
        <<<video_1>>>, <<<video_2>>>, ...

    This is a UX shim for ComfyUI so users can type the same syntax as in the Kling app.
    c                 S      d|  dpd dS )Nz	<<<image_idx1>>>groupmatchr^   r^   r_   _image_repl     z5normalize_omni_prompt_references.<locals>._image_replc                 S   r   )Nz	<<<video_r   r   r   r   r   r^   r^   r_   _video_repl  r   z5normalize_omni_prompt_references.<locals>._video_replz(?<!\w)@image(?P<idx>\d*)(?!\w)z(?<!\w)@video(?P<idx>\d*)(?!\w))resub)r   r   r   r^   r^   r_    normalize_omni_prompt_references   s   r   clsresponsec                    sr   |j rtd|j  d|j d|j t| td|jj dtdd dI d H }t	t
|jjjd	 jI d H S )
NKling request failed. Code: , Message: , Data: z"/proxy/kling/v1/videos/omni-video/pathc                 S      | j r| j jS d S Ndatatask_statusrr^   r^   r_   <lambda>      z(finish_omni_video_task.<locals>.<lambda>response_modelstatus_extractorr   )codeRuntimeErrormessager   r:   r6   task_idr4   r   
NodeOutputr8   task_resultvideosurl)r   r   final_responser^   r^   r_   finish_omni_video_task  s    r   configsc                 C   s   t dd | D S )zDVerifies that at least one camera control configuration is non-zero.c                 s   s    | ]
}t |d  V  qdS )        N)mathisclose).0valuer^   r^   r_   	<genexpr>      z2is_valid_camera_control_configs.<locals>.<genexpr>)any)r   r^   r^   r_   is_valid_camera_control_configs  s   r   c                 C   s   t | jjS )z6Verifies that the initial response contains a task ID.)boolr   r   r   r^   r^   r_   is_valid_task_creation_response!  s   r   c                 C   6   | j duo| j jduo| j jjduot| j jjdkS )zJVerifies that the response contains a task result with at least one video.Nr   )r   r   r   lenr   r^   r^   r_   is_valid_video_response&     

r   c                 C   r   )zJVerifies that the response contains a task result with at least one image.Nr   )r   r   imagesr   r   r^   r^   r_   is_valid_image_response0  r   r   negative_prompt
max_lengthc                 C   sT   | st dt| |krt dt|  d|r(t||kr(t dt| ddS )zRVerifies that the positive prompt is not empty and that neither promt is too long.zPositive prompt is emptyzPositive prompt is too long: z characterszNegative prompt is too long: T)
ValueErrorr   )r   r   r   r^   r^   r_   validate_prompts:  s   r   c                 C   s:   t | sd| j d| j d| j }t| t|dS )z>Validates that the Kling task creation request was successful.z$Kling initial request failed. Code: r   r   N)r   r   r   r   loggingerror	Exceptionr   	error_msgr^   r^   r_   validate_task_creation_responseG  s
   
r   c                 C   2   t | sd| jj d}td||  t|dS )z6Validates that the Kling task result contains a video.Kling task z/ succeeded but no video data found in response.Error: %s.
Response: %sN)r   r   r   r   r   r   r   r^   r^   r_   validate_video_result_responseO  
   r   c                 C   r   )z7Validates that the Kling task result contains an image.r   z/ succeeded but no image data found in response.r   N)r   r   r   r   r   r   r   r^   r^   r_   validate_image_result_responseW  r  r  imagec                 C   s   t | ddd t| dd dS )a,  
    Validates the input image adheres to the expectations of the Kling API:
    - The image resolution should not be less than 300*300px
    - The aspect ratio of the image should be between 1:2.5 ~ 2.5:1

    See: https://app.klingai.com/global/dev/document-api/apiReference/model/imageToVideo
    ,  	min_width
min_heightrI         @r	  rI   N)rC   rB   r  r^   r^   r_   validate_input_image_  s   r  c                 C   s&   | j jjd }td| j j|j |S )zReturns the first video object from the Kling video generation task result.
    Will raise an error if the response is not valid.
    r   z&Kling task %s succeeded. Video URL: %s)r   r   r   r   infor   r   )r   videor^   r^   r_   get_video_from_responsek  s
   r  c                 C   s   | rt | rtt| jS dS )zReturns the first video url from the Kling video generation task result.
    Will not raise an error if the response is not valid.
    N)r   strr  r   r   r^   r^   r_   get_video_url_from_responsev  s   r  c                 C   s    | j jj}td| j j| |S )zReturns the list of image objects from the Kling image generation task result.
    Will raise an error if the response is not valid.
    z#Kling task %s succeeded. Images: %s)r   r   r   r   r  r   )r   r   r^   r^   r_   get_images_from_response  s   
r  c                 C   s0   | rt | rt| }dd |D }d|S dS )zReturns the list of image urls from the Kling image generation task result.
    Will not raise an error if the response is not valid. If there is only one image, returns the url as a string. If there are multiple images, returns a list of urls.
    c                 S   s   g | ]}t |jqS r^   )r  r   r   r  r^   r^   r_   
<listcomp>  s    z1get_images_urls_from_response.<locals>.<listcomp>
N)r   r  join)r   r   
image_urlsr^   r^   r_   get_images_urls_from_response  s
   
r  r   c                    s@   t | dkrtt| d jI dH S tdd | D I dH S )z
    Converts a KlingImageResult to a tuple containing a [B, H, W, C] tensor.
    If multiple images are returned, they will be stacked along the batch dimension.
    rI   r   Nc                    s"   g | ]}t t|jI d H qS r   )r7   r  r   r  r^   r^   r_   r    s     z/image_result_to_node_output.<locals>.<listcomp>)r   r7   r  r   torchcat)r   r^   r^   r_   image_result_to_node_output  s   r  	cfg_scale
model_name
model_modedurationaspect_ratiocamera_controlc	                    s   t ||t t| ttddtt|r|nd |r|nd t|t|t	||t
||ddI d H }	t|	 |	jj}
t| tt d|
 dttdd d	I d H }t| t|}ttt|jI d H t|jt|jS )
NPOSTr   method)r   r   r  moder  r  r   r!  r   r   /r   c                 S      | j r| j jr| j jjS d S r   r   r   r   r   r^   r^   r_   r         z$execute_text2video.<locals>.<lambda>r   estimated_durationr   )r   MAX_PROMPT_LENGTH_T2Vr;   r6   PATH_TEXT_TO_VIDEOr   r   r
   r   r   r   r   r   r   r:   AVERAGE_DURATION_T2Vr   r  r   r   r8   r  r   idr  )r   r   r   r  r  r  r  r   r!  task_creation_responser   r   r  r^   r^   r_   execute_text2video  s:   


*r2  start_frame	end_framec                    s  t ||t t| |	d urtj|	_|dkr|tjjkrd}t	| t
tddttt|t||
d ur6t|
nd ||r<|nd |t|t||	d	dI d H }t| |jj}t| t
t d| dttd	d
 dI d H }t| t|}ttt|jI d H t|jt|jS )Nrh   rl   r"  r#  )	r  r  
image_tailr   r   r  r%  r  r!  r&  r'  r   c                 S   r(  r   r)  r   r^   r^   r_   r     r*  z%execute_image2video.<locals>.<lambda>r+  )r   MAX_PROMPT_LENGTH_I2Vr  r	   simpletyper   kling_v2_5_turbor   r;   r6   PATH_IMAGE_TO_VIDEOr   r   r<   r   r
   r   r   r   r:   AVERAGE_DURATION_I2Vr   r  r   r   r8   r  r   r0  r  )r   r3  r   r   r  r  r  r   r  r!  r4  r1  r   r   r  r^   r^   r_   execute_image2video  sJ   

*r<  dual_charactereffect_sceneimage_1image_2c                    s   |rt ||t|t|g|d}n	t|t||d}t| ttddtt||ddI d H }	t|	 |	j	j
}
t| tt d|
 dttd	d
 dI d H }t| t|}tt|jI d H t|jt|jfS )N)r  r%  r   r  )r  r  r  r"  r#  )r>  input)endpointr   r   r'  r   c                 S   r(  r   r)  r   r^   r^   r_   r   7  r*  z&execute_video_effect.<locals>.<lambda>r+  )r%   r<   r&   r;   r6   PATH_VIDEO_EFFECTSr"   r!   r   r   r   r:   AVERAGE_DURATION_VIDEO_EFFECTSr   r  r8   r  r   r0  r  )r   r=  r>  r  r  r?  r@  r  request_input_fieldr1  r   r   r  r^   r^   r_   execute_video_effect
  sH   



&rF  r  audiovoice_languagetextvoice_speedvoice_idc                    s"  |r
t |dtd t|dd t|dd t| |I d H }td| |r9t| |dd	d
dI d H }	td|	 nd }	t| t	t
dttt|||||d|	|dddI d H }
t|
 |
jj}t| t	t
 d| dttdd dI d H }t| t|}ttt|jI d H t|jt|jS )NText
field_namer     i     
   z$Uploaded video to Comfy API. URL: %smp3
libmp3lame
audio/mpegcontainer_format
codec_name	mime_typez$Uploaded audio to Comfy API. URL: %sr"  r   )	video_urlr%  rI  rH  rJ  
audio_type	audio_urlrK  )rA  r&  r'  r   c                 S   r(  r   r)  r   r^   r^   r_   r   u  r*  z!execute_lipsync.<locals>.<lambda>r+  )rD   MAX_PROMPT_LENGTH_LIP_SYNCrE   rF   r@   r   r  r=   r;   r6   PATH_LIP_SYNCr   r   r   r   r   r   r:   AVERAGE_DURATION_LIP_SYNCr   r  r   r   r8   r  r   r0  r  )r   r  rG  rH  r  rI  rJ  rK  rY  r[  r1  r   r   r^   r^   r_   execute_lipsync?  sT   

*r_  c                   @   s   e Zd ZdZedejfddZededededed	ed
ede	e
B fddZede
dedededed	ed
edejfddZdS )KlingCameraControlszKling Camera Controls NoderH   c                 C   s   t jddddt jjdtdt jjddd	d
dt jjddt jjddd	d
dt jjddt jjddd	d
dt jjddt jjddd	d
dt jjddt jjddd	d
dt jjddt jjddd	d
dt jjddgt dj	ddgdS )Nr`  zKling Camera Controlsapi node/video/Klingz]Allows specifying configuration options for Kling Camera Controls and motion control effects.camera_control_typeoptionshorizontal_movementr   g      $g      $@g      ?zlControls camera's movement along horizontal axis (x-axis). Negative indicates left, positive indicates right)rN   rS   rT   steprU   rO   vertical_movementzpControls camera's movement along vertical axis (y-axis). Negative indicates downward, positive indicates upward.pan      ?zControls camera's rotation in vertical plane (x-axis). Negative indicates downward rotation, positive indicates upward rotation.tiltz}Controls camera's rotation in horizontal plane (y-axis). Negative indicates left rotation, positive indicates right rotation.rollzmControls camera's rolling amount (z-axis). Negative indicates counterclockwise, positive indicates clockwise.zoomz|Controls change in camera's focal length. Negative indicates narrower field of view, positive indicates wider field of view.CAMERA_CONTROLr!  display_name)node_idro  categorydescriptionr\   outputs)
r   SchemaCombor   r	   FloatrZ   r[   CustomOutputr   r^   r^   r_   define_schema  s~   					9z!KlingCameraControls.define_schemare  rg  rh  rj  rk  rl  c                 C   s   t ||||||gsdS dS )NzKInvalid camera control configs: at least one of the values must be non-zeroT)r   )r   re  rg  rh  rj  rk  rl  r^   r^   r_   validate_inputs  s   

z#KlingCameraControls.validate_inputsrb  c                 C   s&   t tt|t||||||ddS )N)
horizontalverticalrh  rk  rj  rl  )r8  config)r   r   r   r	   r   )r   rb  re  rg  rh  rj  rk  rl  r^   r^   r_   execute  s   zKlingCameraControls.executeN)__name__
__module____qualname____doc__classmethodr   rt  rz  floatr   r  r{  r   r  r^   r^   r^   r_   r`  }  sL    B	r`  c                   @   sL   e Zd ZdZedejfddZededede	ded	edej
fd
dZdS )KlingTextToVideoNodeKling Text to Video NoderH   c                 C   s   t t }tjddddtjjddddtjjd	dd
dtjjdddddtjjdt	ddtjjd||d ddgtj
 tjjddtjjddgtjjtjjtjjgdtjtjdgdddd	S )Nr  zKling Text to Videora  r  r   TPositive text promptrM   rO   r   Negative text promptr        ?r   rN   rS   rT   r   16:9rd  rN   r%     eThe configuration to use for the video generation following the format: mode / duration / model_name.rd  rN   rO   video_idrn  r  widgetsaT  
                (
                  $m := widgets.mode;
                  $contains($m,"v2-5-turbo")
                    ? ($contains($m,"10") ? {"type":"usd","usd":0.7} : {"type":"usd","usd":0.35})
                    : $contains($m,"v2-1-master")
                      ? ($contains($m,"10s") ? {"type":"usd","usd":2.8} : {"type":"usd","usd":1.4})
                      : $contains($m,"v2-master")
                        ? ($contains($m,"10s") ? {"type":"usd","usd":2.8} : {"type":"usd","usd":1.4})
                        : $contains($m,"v1-6")
                          ? (
                              $contains($m,"pro")
                                ? ($contains($m,"10s") ? {"type":"usd","usd":0.98} : {"type":"usd","usd":0.49})
                                : ($contains($m,"10s") ? {"type":"usd","usd":0.56} : {"type":"usd","usd":0.28})
                            )
                          : $contains($m,"v1")
                            ? (
                                $contains($m,"pro")
                                  ? ($contains($m,"10s") ? {"type":"usd","usd":0.98} : {"type":"usd","usd":0.49})
                                  : ($contains($m,"10s") ? {"type":"usd","usd":0.28} : {"type":"usd","usd":0.14})
                              )
                            : {"type":"usd","usd":0.14}
                )
                
depends_onexpr	rp  ro  rq  rr  r\   rs  hiddenis_api_nodeprice_badge)listMODE_TEXT2VIDEOkeysr   rt  rX   r   rv  ru  r   Videorx  Hiddenauth_token_comfy_orgapi_key_comfy_org	unique_id
PriceBadgePriceBadgeDependsr   modesr^   r^   r_   rz    sF   z"KlingTextToVideoNode.define_schemar   r   r  r%  r   c           	   
      s.   t | \}}}t| |||||||dI d H S )N)r   r   r  r  r   r  r  )r  r2  )	r   r   r   r  r%  r   r  r  r  r^   r^   r_   r  7  s   	zKlingTextToVideoNode.executeN)r  r  r  r  r  r   rt  rz  r  r  r   r  r^   r^   r^   r_   r    s$    ?r  c                   @   sb   e Zd ZedejfddZe				dded	ed
ededede	dB de
dedejfddZdS )OmniProTextToVideoNoderH   c                 C   sX  t jddddt jjdddgdt jjd	d
ddt jjdg ddt jjddddt jjdt jjdddgd
dt jjdt j	dg t j	dt
dt j	dt
dt j	dt
dt j	dt
d t j	d!t
dt j	d"t
d#gd$d
d%t jjd&d'd
d(t jjd)d*d*d+t jjd
d,d
d-gt j gt jjt jjt jjgd
t jt jg d.d/d0d1d2	S )3NKlingOmniProTextToVideoNodezKling 3.0 Omni Text to Videora  z@Use text prompts to generate videos with the latest Kling model.r  kling-v3-omnikling-video-o1rc  r   TA text prompt describing the video content. This can include both positive and negative descriptions. Ignored when storyboards are enabled.r  r   r  9:161:1r        rR   rN   rS   rT   rU   
resolution1080p720prd  optionalstoryboardsdisabled1 storyboardrI   2 storyboardsrP  3 storyboards4 storyboardsrQ   5 storyboards6 storyboards   z`Generate a series of video segments with individual prompts and durations. Ignored for o1 model.rd  rO   r  generate_audioF)rN   r  seedr   _Seed controls whether the node should re-run; results are non-deterministic regardless of seed.rN   rS   rT   rU   control_after_generaterO   r  r  r  r  r  r    
                (
                  $mode := (widgets.resolution = "720p") ? "std" : "pro";
                  $isV3 := $contains(widgets.model_name, "v3");
                  $audio := $isV3 and widgets.generate_audio;
                  $rates := $audio
                    ? {"std": 0.112, "pro": 0.14}
                    : {"std": 0.084, "pro": 0.112};
                  {"type":"usd","usd": $lookup($rates, $mode) * widgets.duration}
                )
                r  r  )r   rt  ru  r   rX   rY   rZ   r[   DynamicComboOptionr`   Booleannumberr  rx  r  r  r  r  r  r  ry  r^   r^   r_   rz  O  sf   	)z$OmniProTextToVideoNode.define_schemar  NFr   r  r   r   r  r  r  r  r  c	                    s  |}	|dkr|dvrt d|rt d|d uo|d dk}
|
r)|dkr)t dt|d|
r0d	nd
dd d }d }|
rt|d  d	 }d}g }td
|d
 D ]*}|d| d }|d| d }t|d| dd
dd |t||t|d qPtdd |D }||krt d| d| dt	| t
dddtt|||t||dkrdnd|||rdnd |rdnd d!	d"I d H }t| |I d H S )#Nr  r  rQ  z:kling-video-o1 only supports durations of 5 or 10 seconds.1kling-video-o1 does not support audio generation.r  r  ,kling-video-o1 does not support storyboards.Tr   rI   rb   strip_whitespace
min_lengthr   rJ   rK   rP      rN  r  r   indexr   r  c                 s       | ]}t |jV  qd S r   intr  r   er^   r^   r_   r         z1OmniProTextToVideoNode.execute.<locals>.<genexpr>Total storyboard duration (#s) must equal the global duration (s).!/proxy/kling/v1/videos/omni-videor"  r#  r  rl   rh   	customizeonoff)	r  r   r   r  r%  
multi_shotmulti_prompt	shot_typesoundr&  )r   rD   r  splitrV   appendr,   r  sumr;   r6   r4   r3   r   )r   r  r   r   r  r  r  r  r  _stories_enabledr  multi_prompt_listrG   r]   	sb_promptsb_durationtotal_storyboard_durationr   r^   r^   r_   r    sh   


zOmniProTextToVideoNode.executer  NFr   )r  r  r  r  r   rt  rz  r  r  dictr   r   r  r^   r^   r^   r_   r  M  s6    G	
r  c                   @   s|   e Zd ZedejfddZe						dded	ed
ede	j
de	j
dB de	j
dB dededB dededejfddZdS )OmniProFirstLastFrameNoderH   c                 C   sr  t jddddt jjdddgdt jjd	d
ddt jjddddt jjdt jdt jjdd
ddt jjdd
ddt jjdddgd
dt j	jdt j	
dg t j	
dtdt j	
d td!t j	
d"tdt j	
d#td$t j	
d%tdt j	
d&td'gd(d
d)t jjd*d+d
d,d-t jjd.d/d/d0t jjd
d1d
d2g
t j gt jjt jjt jjgd
t jt jg d3d4d5d6d7	S )8NKlingOmniProFirstLastFrameNodez(Kling 3.0 Omni First-Last-Frame to Videora  zZUse a start frame, an optional end frame, or reference images with the latest Kling model.r  r  r  rc  r   Tr  r  r  r  r  rR   r  first_framer4  zAn optional end frame for the video. This cannot be used simultaneously with 'reference_images'. Does not work with storyboards.r  rO   reference_imagesz$Up to 6 additional reference images.r  r  r  r  r  r  r  rI   r  rP  r  r  rQ   r  r  r  lGenerate a series of video segments with individual prompts and durations. Only supported for kling-v3-omni.r  r  F?Generate audio for the video. Only supported for kling-v3-omni.rN   r  rO   r  r   r  r  r  r  r  r  r  r  r   rt  ru  r   rX   rY   rZ   r[   Imager  r  r`   r  r  r  rx  r  r  r  r  r  r  ry  r^   r^   r_   rz    s   
	:z'OmniProFirstLastFrameNode.define_schemaNr  Fr   r  r   r  r  r4  r  r  r  r  r  c                    s  |
}|dkr|dkrt d|	rt d|d uo|d dk}|r)|dkr)t dt|}t|d|r4d	nd
dd |d urE|d urEt d|d urO|rOt d|dkrc|dvrc|d u rc|d u rct dd }d }|rt|d  d	 }d}g }td
|d
 D ]*}|d| d }|d| d }t|d| dd
dd |t||t|d q~t	dd |D }||krt d| d| dt
|ddd t|dd tt| |d d!I d H d	 d"d#g}|d urt
|ddd t|dd |tt| |d$d!I d H d	 d%d# |d ur=t|d&krt d'|D ]}t
|ddd t|dd qt| |d(d!I d H D ]}|t|d) q1t| td*d+d,tt||t|||d-krSd.nd/|	rYd0nd1|||rad2nd d3	d4I d H }t| |I d H S )5Nr  rQ  Bkling-video-o1 does not support durations greater than 10 seconds.r  r  r  r  Tr   rI   rb   r  zLThe 'end_frame' input cannot be used simultaneously with 'reference_images'.zEThe 'end_frame' input cannot be used simultaneously with storyboards.r  z\Duration is only supported for 5 or 10 seconds if there is no end frame or reference images.rJ   rK   rP   r  r  r  c                 s   r  r   r  r  r^   r^   r_   r   l  r  z4OmniProFirstLastFrameNode.execute.<locals>.<genexpr>r  r  r  r  r  r  r
  Uploading first frame
wait_labelr  )	image_urlr8  Uploading end framer4  r  z4The maximum number of reference images allowed is 6.zUploading reference frame(s)r  r  r"  r#  r  rl   rh   r  r  r  )	r  r   r  
image_listr%  r  r  r  r  r&  )r   r   rD   r  r  rV   r  r,   r  r  rC   rB   r.   r?   r9   r;   r6   r4   r0   r   )r   r  r   r  r  r4  r  r  r  r  r  r  r  r  r  rG   r]   r  r  r  r  r   r^   r^   r_   r  4  s   


z!OmniProFirstLastFrameNode.execute)NNr  NFr   r  r  r  r  r   rt  rz  r  r  r   r  r  r   r   r  r^   r^   r^   r_   r    sB    X	
r  c                   @   sh   e Zd ZedejfddZe				dded	ed
edede	j
dededB dededejfddZdS )OmniProImageToVideoNoderH   c                 C   sh  t jddddt jjdddgdt jjd	d
ddt jjdg ddt jjddddt jjdt jjdddt jjdddgd
dt j	jdt j	
dg t j	
dtdt j	
dtd t j	
d!tdt j	
d"td#t j	
d$tdt j	
d%td&gd'd
d(t jjd)d*d
d+d,t jjd-d.d.d/t jjd
d0d
d1g	t j gt jjt jjt jjgd
t jt jg d2d3d4d5d6	S )7NKlingOmniProImageToVideoNodezKling 3.0 Omni Image to Videora  zMUse up to 7 reference images to generate a video with the latest Kling model.r  r  r  rc  r   Tr  r  r   r  r  r  r  rR   r  r  zUp to 7 reference images.rO   r  r  r  r  r  r  r  rI   r  rP  r  r  rQ   r  r  r  r  r  r  Fr  r  r  r   r  r  r  r  r  r  r  r  r  ry  r^   r^   r_   rz    sx   	2z%OmniProImageToVideoNode.define_schemar  NFr   r  r   r   r  r  r  r  r  r  c
                    s  |	}
|dkr|dkrt d|rt d|d uo|d dk}|r)|dkr)t dt|}t|d|r4d	nd
dd d }d }|rt|d  d	 }d}g }td
|d
 D ]*}|d| d }|d| d }t|d| dd
dd |t||t|d qTt	dd |D }||krt d| d| dt
|dkrt d|D ]}t|ddd t|dd qg }t| |ddI d H D ]
}|t|d  qt| td!d"d#tt|||t|||d$krd%nd&|rd'nd(|||rd)nd d*
d+I d H }t| |I d H S ),Nr  rQ  r  r  r  r  r  Tr   rI   rb   r  rJ   rK   rP   r  r  r  c                 s   r  r   r  r  r^   r^   r_   r     r  z2OmniProImageToVideoNode.execute.<locals>.<genexpr>r  r  r     z,The maximum number of reference images is 7.r  r  r  r
  Uploading reference imager   r  r  r"  r#  r  rl   rh   r  r  r  )
r  r   r   r  r  r%  r  r  r  r  r&  )r   r   rD   r  r  rV   r  r,   r  r  r9   rC   rB   r?   r.   r;   r6   r4   r2   r   )r   r  r   r   r  r  r  r  r  r  r  r  r  r  rG   r]   r  r  r  r  r   r^   r^   r_   r    s|   


zOmniProImageToVideoNode.executer  r  r^   r^   r^   r_   r    s:    P	
r  c                   @   sh   e Zd ZedejfddZe			ddeded	ed
ede	j
dede	jdB dededejfddZdS )OmniProVideoToVideoNoderH   c                 C   s   t jddddt jjdddgdt jjd	d
ddt jjdg ddt jjddddt jjdt jjdddt j	jdd
dt j
jddd
dt jjdddgd
dt jjdd d d!t jjd
d"d
d#g	t j gt jjt jjt jjgd
t jt jddgd$d%d&d'	S )(NKlingOmniProVideoToVideoNodezKling 3.0 Omni Video to Videora  zYUse a video and up to 4 reference images to generate a video with the latest Kling model.r  r  r  rc  r   TeA text prompt describing the video content. This can include both positive and negative descriptions.r  r   r  r  r  rQ  r  reference_videozVideo to use as a reference.r	  keep_original_soundrN   r  $Up to 4 additional reference images.rO   r  r  r  r  r  r  r   r  r  r  r  a  
                (
                  $mode := (widgets.resolution = "720p") ? "std" : "pro";
                  $rates := {"std": 0.126, "pro": 0.168};
                  {"type":"usd","usd": $lookup($rates, $mode) * widgets.duration}
                )
                r  r  )r   rt  ru  r   rX   rY   rZ   r[   r  r  r  r  rx  r  r  r  r  r  r  ry  r^   r^   r_   rz  A  sX   z%OmniProVideoToVideoNode.define_schemaNr  r   r  r   r   r  r  r  r  r  r  c
                    s*  |	}
t |}t|ddd t|ddd t|ddddd	 g }|d urUt|d
kr.td|D ]}t|ddd t|dd q0t| |ddI d H D ]
}|	t
|d qJtt| |ddI d H d|rddnddg}t| tdddtt|||t||r||nd ||dkrdndddI d H }t| |I d H S ) NrI   rb   r  r         @皙$@min_durationmax_durationrO  p  r  r  	max_width
max_heightrQ   GThe maximum number of reference images allowed with a video input is 4.r  r  r  r
  r  r   r  zUploading reference videofeatureyesnorY  
refer_typer  r  r"  r#  r  rl   rh   r  r   r   r  r  
video_listr%  r&  )r   rD   rF   rE   r9   r   rC   rB   r?   r  r.   r/   r@   r;   r6   r4   r2   r  r   )r   r  r   r   r  r  r  r  r  r  r  r  r]   r%  r   r^   r^   r_   r  {  sH   


zOmniProVideoToVideoNode.executeNr  r   )r  r  r  r  r   rt  rz  r  r  r   r  r   r  r   r  r^   r^   r^   r_   r  ?  s8    9		
r  c                   @   s`   e Zd ZedejfddZe			ddeded	ej	d
e
dejdB dededejfddZdS )OmniProEditVideoNoderH   c                 C   s   t jdddddt jjdddgd	t jjd
dddt jjdddt jjdddt jjddddt jjdddgddt jjddddt j	j
ddddgt j gt jjt jjt jjgdt jt jdgdd d!d"
S )#NKlingOmniProEditVideoNodezKling 3.0 Omni Edit Videora  Video Generationz8Edit an existing video with the latest model from Kling.r  r  r  rc  r   Tr  r  r  z<Video for editing. The output video length will be the same.r	  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  a  
                (
                  $mode := (widgets.resolution = "720p") ? "std" : "pro";
                  $rates := {"std": 0.126, "pro": 0.168};
                  {"type":"usd","usd": $lookup($rates, $mode), "format":{"suffix":"/second"}}
                )
                r  
rp  ro  rq  essentials_categoryrr  r\   rs  r  r  r  )r   rt  ru  r   rX   r  r  r  rY   rZ   r  rx  r  r  r  r  r  r  ry  r^   r^   r_   rz    sV   z"OmniProEditVideoNode.define_schemaNr  r   r  r   r  r  r  r  r  c                    s&  |}t |}t|ddd t|ddd t|ddddd	 g }	|d urUt|d
kr.td|D ]}
t|
ddd t|
dd q0t| |ddI d H D ]
}
|		t
|
d qJtt| |ddI d H d|rddnddg}t| tdddtt||d d |	rz|	nd ||dkrdndddI d H }t| |I d H S ) NrI   rb   r  r  r  r  rO  r  r  rQ   r  r  r  r  r
  r  r   r  zUploading base videobaser   r!  r"  r  r"  r#  r  rl   rh   r$  r&  )r   rD   rF   rE   r9   r   rC   rB   r?   r  r.   r/   r@   r;   r6   r4   r2   r   )r   r  r   r  r  r  r  r  r  r  r]   r%  r   r^   r^   r_   r    sH   


zOmniProEditVideoNode.executer&  )r  r  r  r  r   rt  rz  r  r   r  r   r  r  r   r  r^   r^   r^   r_   r'    s0    8	r'  c                   @   s^   e Zd ZedejfddZe			ddeded	ed
ededej	dB de
dejfddZdS )OmniProImageNoderH   c                 C   s   t jddddt jjdddgdt jjd	d
ddt jjdg ddt jjdg ddt jjdg dddt jjddd
dt jjddddt jjd
dd
dgt j	 gt j
jt j
jt j
jgd
t jt jg dddd d!	S )"NKlingOmniProImageNodezKling 3.0 Omni Imageapi node/image/Klingz7Create or edit images with the latest model from Kling.r  r  kling-image-o1rc  r   TzeA text prompt describing the image content. This can include both positive and negative descriptions.r  r  )1K2K4Kr   )r  r  r  z4:3z3:4z3:2z2:3z21:9series_amount)	r  234ri   6789z>Generate a series of images. Not supported for kling-image-o1.rd  rO   r  z%Up to 10 additional reference images.r  r  r   r  r  r  )r  r4  r  r  a  
                (
                  $prices := {"1k": 0.028, "2k": 0.028, "4k": 0.056};
                  $base := $lookup($prices, widgets.resolution);
                  $isO1 := widgets.model_name = "kling-image-o1";
                  $mult := ($isO1 or widgets.series_amount = "disabled") ? 1 : $number(widgets.series_amount);
                  {"type":"usd","usd": $base * $mult}
                )
                r  r  )r   rt  ru  r   rX   r  rY   rZ   r  rx  r  r  r  r  r  r  ry  r^   r^   r_   rz    sb   $zOmniProImageNode.define_schemar  Nr   r  r   r  r   r4  r  r  c                    s  |}|dkr|dkrt dt|}t|ddd g }	|d urQt|dkr*t d|D ]}
t|
d	d	d
 t|
dd q,t| |ddI d H D ]
}
|	t|
d qF|dk}|r_|dkr_t dt	| t
dddtt||| ||	rr|	nd |rwdnd |r~t|nd ddI d H }|jrtd|j d|j d|j t| t
d|jj dtdd dI d H }|jjjp|jjj}d d! |D I d H }ttj|d"d#S )$Nr0  r3  z84K resolution is not supported for kling-image-o1 model.rI   rb   r  rQ  z-The maximum number of reference images is 10.r  r  r  r
  r  r   r  r  z2kling-image-o1 does not support series generation.z!/proxy/kling/v1/images/omni-imager"  r#  series)r  r   r  r   r  result_typer4  r&  r   r   r   z"/proxy/kling/v1/images/omni-image/r   c                 S   r   r   r   r   r^   r^   r_   r     r   z*OmniProImageNode.execute.<locals>.<lambda>r   c                    s   g | ]
}t |jI d H qS r   )r7   r   )r   imgr^   r^   r_   r    r   z,OmniProImageNode.execute.<locals>.<listcomp>r   )dim)r   r   rD   r9   rC   rB   r?   r  r-   r;   r6   r4   r1   lowerr  r   r   r   r   r:   r   r   series_imagesr   r   r   r  r  )r   r  r   r  r   r4  r  r  r  r  r]   
use_seriesr   r   r   tensorsr^   r^   r_   r  ]  sZ   


zOmniProImageNode.execute)r  Nr   )r  r  r  r  r   rt  rz  r  r   r  r  r   r  r^   r^   r^   r_   r-    s0    @	r-  c                   @   sT   e Zd ZdZedejfddZe	ddedede	d	ed
e
dB dejfddZdS )KlingCameraControlT2VNodea.  
    Kling Text to Video Camera Control Node. This node is a text to video node, but it supports controlling the camera.
    Duration, mode, and model_name request fields are hard-coded because camera control is only supported in pro mode with the kling-v1-5 model at 5s duration as of 2025-05-02.
    rH   c                 C   s   t jddddt jjddddt jjd	dd
dt jjdddddt jjdtddt djdddgt j	 t jj	ddt jj	ddgt j
jt j
jt j
jgdt jddd	S )NrE  z$Kling Text to Video (Camera Control)ra  zTransform text into cinematic videos with professional camera movements that simulate real-world cinematography. Control virtual camera actions including zoom, rotation, pan, tilt, and first-person view, while maintaining focus on your original text.r   Tr  r  r   r  r        ?r   r  r  r   r  r  rm  r!  yCan be created using the Kling Camera Controls node. Controls the camera movement and motion during the video generation.r	  r  rn  r  z{"type":"usd","usd":0.14}r  r  )r   rt  rX   r   rv  ru  r   rw  r  rx  r  r  r  r  r  ry  r^   r^   r_   rz    s>   
z'KlingCameraControlT2VNode.define_schemaNr   r   r  r   r!  c                    s,   t | tj|tjt|tj|||d	I d H S )N)r  r  r  r   r  r   r   r!  )r2  r   kling_v1r   rh   r   r
   field_5)r   r   r   r  r   r!  r^   r^   r_   r    s   	z!KlingCameraControlT2VNode.executer   )r  r  r  r  r  r   rt  rz  r  r  r   r   r  r^   r^   r^   r_   rE    s&    $rE  c                   @   sr   e Zd ZdZedejfddZe		ddej	de
de
d	e
d
ede
de
de
dedB dej	dB dejfddZdS )KlingImage2VideoNodezKling Image to Video NoderH   c                 C   s   t jdddt jjdddt jjddd	d
t jjdddd
t jjdtddt jjdddddt jjdttj	dt jjdt
t
jdt jjdttjdgt j t jjddt jjddgt jjt jjt jjgdt jt jg dddddS )NrK  z!Kling Image(First Frame) to Videora  r3  z/The reference image used to generate the video.r	  r   Tr  r  r   r  r  rm   r  r  皙?r   r  r  r%  r   r  r  rn  r%  r  r  r  a{  
                (
                  $mode := widgets.mode;
                  $model := widgets.model_name;
                  $dur := widgets.duration;
                  $contains($model,"v2-5-turbo")
                    ? ($contains($dur,"10") ? {"type":"usd","usd":0.7} : {"type":"usd","usd":0.35})
                    : ($contains($model,"v2-1-master") or $contains($model,"v2-master"))
                      ? ($contains($dur,"10") ? {"type":"usd","usd":2.8} : {"type":"usd","usd":1.4})
                      : ($contains($model,"v2-1") or $contains($model,"v1-6") or $contains($model,"v1-5"))
                        ? (
                            $contains($mode,"pro")
                              ? ($contains($dur,"10") ? {"type":"usd","usd":0.98} : {"type":"usd","usd":0.49})
                              : ($contains($dur,"10") ? {"type":"usd","usd":0.56} : {"type":"usd","usd":0.28})
                          )
                        : $contains($model,"v1")
                          ? (
                              $contains($mode,"pro")
                                ? ($contains($dur,"10") ? {"type":"usd","usd":0.98} : {"type":"usd","usd":0.49})
                                : ($contains($dur,"10") ? {"type":"usd","usd":0.28} : {"type":"usd","usd":0.14})
                            )
                          : {"type":"usd","usd":0.14}
                )
                r  rp  ro  rq  r\   rs  r  r  r  )r   rt  r  r   rX   ru  r   rv  r   rh   r   
field_16_9r
   rJ  r  rx  r  r  r  r  r  r  ry  r^   r^   r_   rz    sF   z"KlingImage2VideoNode.define_schemaNr3  r   r   r  r  r%  r   r  r!  r4  c                    s&   t | |||||||||	|
dI d H S )N)
r3  r   r   r  r  r   r  r  r!  r4  )r<  )r   r3  r   r   r  r  r%  r   r  r!  r4  r^   r^   r_   r    s   zKlingImage2VideoNode.executeNNr  r  r  r  r  r   rt  rz  r  Tensorr  r  r   r   r  r^   r^   r^   r_   rK    s<    ?	
rK  c                   @   sR   e Zd ZdZedejfddZedej	de
de
ded	e
d
edejfddZdS )KlingCameraControlI2VNodea0  
    Kling Image to Video Camera Control Node. This node is a image to video node, but it supports controlling the camera.
    Duration, mode, and model_name request fields are hard-coded because camera control is only supported in pro mode with the kling-v1-5 model at 5s duration as of 2025-05-02.
    rH   c                 C   s   t jddddt jjdddt jjdd	d
dt jjdd	ddt jjdddddt jjdttjdt 	djdddgt j
 t jjddt jjddgt jjt jjt jjgd	t jddd	S )NrS  z%Kling Image to Video (Camera Control)ra  a  Transform still images into cinematic videos with professional camera movements that simulate real-world cinematography. Control virtual camera actions including zoom, rotation, pan, tilt, and first-person view, while maintaining focus on your original image.r3  Reference Image - URL or Base64 encoded string, cannot exceed 10MB, resolution not less than 300*300px, aspect ratio between 1:2.5 ~ 2.5:1. Base64 should not include data:image prefix.r	  r   Tr  r  r   r  r  rF  r   r  r  r   r  rm  r!  rG  r  rn  r  z{"type":"usd","usd":0.49}rH  r  )r   rt  r  r   rX   rv  ru  r   rO  rw  r  rx  r  r  r  r  r  ry  r^   r^   r_   rz  =  sF   
z'KlingCameraControlI2VNode.define_schemar3  r   r   r  r   r!  c                    s.   t | tj||tjt|tj|||d
I d H S )N)	r  r3  r  r  r   r  r   r   r!  )r<  r   
kling_v1_5r   rl   r   r
   rJ  )r   r3  r   r   r  r   r!  r^   r^   r_   r  f  s   
z!KlingCameraControlI2VNode.executeNrQ  r^   r^   r^   r_   rS  7  s(    (rS  c                   @   sX   e Zd ZdZedejfddZedej	dej	de
de
d	ed
e
de
dejfddZdS )KlingStartEndFrameNodez
    Kling First Last Frame Node. This node allows creation of a video from a first and last frame. It calls the normal image to video endpoint, but only allows the subset of input options that support the `image_tail` request field.
    rH   c                 C   s   t t }tjddddtjjdddtjjdd	dtjjd
dddtjjddddtjjdddddtj	jdg ddtj	jd||d ddgtj
 tjjddtjjddgtjjtjjtjjgdtjtjdgdd d!d"	S )#NrV  zKling Start-End Frame to Videora  zGenerate a video sequence that transitions between your provided start and end images. The node creates all frames in between, producing a smooth transformation from the first frame to the last.r3  rT  r	  r4  zReference Image - End frame control. URL or Base64 encoded string, cannot exceed 10MB, resolution not less than 300*300px. Base64 should not include data:image prefix.r   Tr  r  r   r  r  ri  r   r  r  r   r  rc  r%  r  r  r  r  rn  r  r  aO  
                (
                  $m := widgets.mode;
                  $contains($m,"v2-5-turbo")
                    ? ($contains($m,"10") ? {"type":"usd","usd":0.7} : {"type":"usd","usd":0.35})
                    : $contains($m,"v2-1")
                      ? ($contains($m,"10s") ? {"type":"usd","usd":0.98} : {"type":"usd","usd":0.49})
                      : $contains($m,"v2-master")
                        ? ($contains($m,"10s") ? {"type":"usd","usd":2.8} : {"type":"usd","usd":1.4})
                        : $contains($m,"v1-6")
                          ? (
                              $contains($m,"pro")
                                ? ($contains($m,"10s") ? {"type":"usd","usd":0.98} : {"type":"usd","usd":0.49})
                                : ($contains($m,"10s") ? {"type":"usd","usd":0.56} : {"type":"usd","usd":0.28})
                            )
                          : $contains($m,"v1")
                            ? (
                                $contains($m,"pro")
                                  ? ($contains($m,"10s") ? {"type":"usd","usd":0.98} : {"type":"usd","usd":0.49})
                                  : ($contains($m,"10s") ? {"type":"usd","usd":0.28} : {"type":"usd","usd":0.14})
                              )
                            : {"type":"usd","usd":0.14}
                )
                r  r  )r  MODE_START_END_FRAMEr  r   rt  r  r   rX   rv  ru  r  rx  r  r  r  r  r  r  r  r^   r^   r_   rz    sN   z$KlingStartEndFrameNode.define_schemar3  r4  r   r   r  r   r%  c           
         s2   t | \}}}	t| |||	||||||d
I d H S )N)	r   r   r  r3  r  r  r   r  r4  )rW  r<  )
r   r3  r4  r   r   r  r   r%  r  r  r^   r^   r_   r    s   zKlingStartEndFrameNode.executeN)r  r  r  r  r  r   rt  rz  r  rR  r  r  r   r  r^   r^   r^   r_   rV  ~  s,    C	rV  c                   @   sD   e Zd ZedejfddZedededededej	f
dd	Z
d
S )KlingVideoExtendNoderH   c                 C   s   t jddddt jjddddt jjd	dd
dt jjdddddt jjddddgt j t jjddt jjddgt jjt jj	t jj
gdt jddd	S )NrX  zKling Video Extendra  zuKling Video Extend Node. Extend videos made by other Kling nodes. The video_id is created by using other Kling Nodes.r   Tz4Positive text prompt for guiding the video extensionr  r   z@Negative text prompt for elements to avoid in the extended videor  ri  r   r  r  r  zThe ID of the video to be extended. Supports videos generated by text-to-video, image-to-video, and previous video extension operations. Cannot exceed 3 minutes total duration after extension.)force_inputrO   rn  r  z{"type":"usd","usd":0.28}rH  r  )r   rt  rX   r   rv  r  rx  r  r  r  r  r  ry  r^   r^   r_   rz    sF   z"KlingVideoExtendNode.define_schemar   r   r  r  c           	   
      s   t ||t t| ttddtt|r|nd |r|nd ||ddI d H }t| |jj	}t
| tt d| dttdd d	I d H }t| t|}ttt|jI d H t|jt|jS )
Nr"  r#  )r   r   r  r  r&  r'  r   c                 S   r(  r   r)  r   r^   r^   r_   r   (  r*  z.KlingVideoExtendNode.execute.<locals>.<lambda>r+  )r   r-  r;   r6   PATH_VIDEO_EXTENDr   r   r   r   r   r:   AVERAGE_DURATION_VIDEO_EXTENDr   r  r   r   r8   r  r   r0  r  )	r   r   r   r  r  r1  r   r   r  r^   r^   r_   r    s2   


*zKlingVideoExtendNode.executeN)r  r  r  r  r   rt  rz  r  r  r   r  r^   r^   r^   r_   rX    s    (rX  c                   @   sT   e Zd ZdZedejfddZedej	dej	de
ded	ed
edejfddZdS )!KlingDualCharacterVideoEffectNodez&Kling Dual Character Video Effect NoderH   c                 C   s   t jddddt jjdddt jjdd	dt jjd
dd tD dt jjddd tD ddt jjddd tD ddt jjddd tD dgt j	
 t jj
ddgt jjt jjt jjgdt jt jg ddddd	S )Nr\  z"Kling Dual Character Video Effectsra  zAchieve different special effects when generating a video based on the effect_scene. First image will be positioned on left side, second on right side of the composite.
image_leftzLeft side imager	  image_rightzRight side imager>  c                 S      g | ]}|j qS r^   r   r   r]   r^   r^   r_   r  ?  r   zCKlingDualCharacterVideoEffectNode.define_schema.<locals>.<listcomp>rc  r  c                 S   r_  r^   r`  ra  r^   r^   r_   r  C  r   zkling-v1r  r%  c                 S   r_  r^   r`  ra  r^   r^   r_   r  H  r   rh   r  c                 S   r_  r^   r`  ra  r^   r^   r_   r  M  r   rn  TrM  r  a  
                (
                  $mode := widgets.mode;
                  $model := widgets.model_name;
                  $dur := widgets.duration;
                  ($contains($model,"v1-6") or $contains($model,"v1-5"))
                    ? (
                        $contains($mode,"pro")
                          ? ($contains($dur,"10") ? {"type":"usd","usd":0.98} : {"type":"usd","usd":0.49})
                          : ($contains($dur,"10") ? {"type":"usd","usd":0.56} : {"type":"usd","usd":0.28})
                      )
                    : $contains($model,"v1")
                      ? (
                          $contains($mode,"pro")
                            ? ($contains($dur,"10") ? {"type":"usd","usd":0.98} : {"type":"usd","usd":0.49})
                            : ($contains($dur,"10") ? {"type":"usd","usd":0.28} : {"type":"usd","usd":0.14})
                        )
                      : {"type":"usd","usd":0.14}
                )
                r  r  )r   rt  r  r   ru  r#   r'   r   r
   r  rx  rX   r  r  r  r  r  r  ry  r^   r^   r_   rz  3  sN   z/KlingDualCharacterVideoEffectNode.define_schemar]  r^  r>  r  r%  r  c           	   
      s2   t | d||||||dI d H \}}}t||S )NT)r=  r>  r  r  r  r?  r@  )rF  r   r   )	r   r]  r^  r>  r  r%  r  r  r  r^   r^   r_   r  s  s   

z)KlingDualCharacterVideoEffectNode.executeN)r  r  r  r  r  r   rt  rz  r  rR  r#   r'   r   r
   r   r  r^   r^   r^   r_   r\  0  s(    ?r\  c                   @   sJ   e Zd ZdZedejfddZedej	de
dededejf
d	d
ZdS )KlingSingleImageVideoEffectNodez$Kling Single Image Video Effect NoderH   c                 C   s   t jddddt jjdddt jjdd	d
 tD dt jjddd
 tD dt jjddd
 tD dgt j	 t j
j	ddt j
j	ddgt jjt jjt jjgdt jt jdgdddd	S )Nrb  zKling Video Effectsra  zTAchieve different special effects when generating a video based on the effect_scene.r  z Reference Image. URL or Base64 encoded string (without data:image prefix). File size cannot exceed 10MB, resolution not less than 300*300px, aspect ratio between 1:2.5 ~ 2.5:1r	  r>  c                 S   r_  r^   r`  ra  r^   r^   r_   r    r   zAKlingSingleImageVideoEffectNode.define_schema.<locals>.<listcomp>rc  r  c                 S   r_  r^   r`  ra  r^   r^   r_   r    r   r  c                 S   r_  r^   r`  ra  r^   r^   r_   r    r   r  rn  Tr  a  
                (
                  ($contains(widgets.effect_scene,"dizzydizzy") or $contains(widgets.effect_scene,"bloombloom"))
                    ? {"type":"usd","usd":0.49}
                    : {"type":"usd","usd":0.28}
                )
                r  r  )r   rt  r  r   ru  r$   r(   r
   r  rx  rX   r  r  r  r  r  r  ry  r^   r^   r_   rz    sH   z-KlingSingleImageVideoEffectNode.define_schemar  r>  r  r  c              	      s"   t jt| d||||dI d H  S )NF)r=  r>  r  r  r?  )r   r   rF  )r   r  r>  r  r  r^   r^   r_   r    s   
z'KlingSingleImageVideoEffectNode.executeN)r  r  r  r  r  r   rt  rz  r  rR  r$   r(   r
   r   r  r^   r^   r^   r_   rb    s     /rb  c                	   @   sH   e Zd ZdZedejfddZedej	dej
dedejfdd	Zd
S )KlingLipSyncAudioToVideoNodezpKling Lip Sync Audio to Video Node. Syncs mouth movements in a video file to the audio content of an audio file.rH   c                 C   s   t jdddddt jdt jdt jjdd	d
 tD ddgt j t jjddt jjddgt j	j
t j	jt j	jgdt jddd
S )Nrc  zKling Lip Sync Video with Audiora  r)  a  Kling Lip Sync Audio to Video Node. Syncs mouth movements in a video file to the audio content of an audio file. When using, ensure that the audio contains clearly distinguishable vocals and that the video contains a distinct face. The audio file should not be larger than 5MB. The video file should not be larger than 100MB, should have height/width between 720px and 1920px, and should be between 2s and 10s in length.r  rG  rH  c                 S   r_  r^   r`  ra  r^   r^   r_   r    r   z>KlingLipSyncAudioToVideoNode.define_schema.<locals>.<listcomp>rv   r  r  rn  r  T6{"type":"usd","usd":0.1,"format":{"approximate":true}}rH  r*  )r   rt  r  r   Audioru  r   rx  rX   r  r  r  r  r  ry  r^   r^   r_   rz    s6   


z*KlingLipSyncAudioToVideoNode.define_schemar  rG  rH  c                    s   t | |||ddI d H S )Naudio2video)r  rG  rH  r  )r_  )r   r  rG  rH  r^   r^   r_   r    s   z$KlingLipSyncAudioToVideoNode.executeN)r  r  r  r  r  r   rt  rz  r   r  re  r  r   r  r^   r^   r^   r_   rc    s     rc  c                   @   sJ   e Zd ZdZedejfddZedej	de
de
dedejf
d	d
ZdS )KlingLipSyncTextToVideoNodezZKling Lip Sync Text to Video Node. Syncs mouth movements in a video file to a text prompt.rH   c                 C   s   t jddddt jdt jjdddd	t jjd
tt ddt j	jddddt j
jdddgt j t jjddt jjddgt jjt jjt jjgdt jddd	S )Nrg  zKling Lip Sync Video with Textra  zKling Lip Sync Text to Video Node. Syncs mouth movements in a video file to a text prompt. The video file should not be larger than 100MB, should have height/width between 720px and 1920px, and should be between 2s and 10s in length.r  rI  TzoText Content for Lip-Sync Video Generation. Required when mode is text2video. Maximum length is 120 characters.r  voicert   r  rJ  rI   rL  g       @zASpeech Rate. Valid range: 0.8~2.0, accurate to one decimal place.)rN   rS   rT   rU   rO   advancedr  rn  r  rd  rH  r  )r   rt  r  r   rX   ru  r  VOICES_CONFIGr  rv  rZ   r[   rx  r  r  r  r  r  ry  r^   r^   r_   rz  
	  sN   

z)KlingLipSyncTextToVideoNode.define_schemar  rI  rh  rJ  c              	      s*   t | \}}t| |||||ddI d H S )N
text2video)r  rI  rH  rK  rJ  r  )rj  r_  )r   r  rI  rh  rJ  rK  rH  r^   r^   r_   r  7	  s   z#KlingLipSyncTextToVideoNode.executeN)r  r  r  r  r  r   rt  rz  r   r  r  r  r   r  r^   r^   r^   r_   rg  	  s     ,rg  c                	   @   sH   e Zd ZdZedejfddZedej	dej	de
dejfdd	Zd
S )KlingVirtualTryOnNodezKling Virtual Try On Node.rH   c                 C   sl   t jddddt jdt jdt jjddd	 tD d
dgt j gt jjt jj	t jj
gdt jddd	S )Nrl  zKling Virtual Try Onr/  zKling Virtual Try On Node. Input a human image and a cloth image to try on the cloth on the human. You can merge multiple clothing item pictures into one image with a white background.human_imagecloth_imager  c                 S   r_  r^   r`  ra  r^   r^   r_   r  Z	  r   z7KlingVirtualTryOnNode.define_schema.<locals>.<listcomp>zkolors-virtual-try-on-v1r  Tz{"type":"usd","usd":0.7}rH  r  )r   rt  r  r   ru  r   rx  r  r  r  r  r  ry  r^   r^   r_   rz  N	  s0   


z#KlingVirtualTryOnNode.define_schemarm  rn  r  c              	      s   t | ttddttt|t||ddI d H }t| |jj}t	| tt d| dtt
dd d	I d H }t| t|}tt|I d H S )
Nr"  r#  )rm  rn  r  r&  r'  r   c                 S   r(  r   r)  r   r^   r^   r_   r   	  r*  z/KlingVirtualTryOnNode.execute.<locals>.<lambda>r+  )r;   r6   PATH_VIRTUAL_TRY_ONr   r   r<   r   r   r   r:   AVERAGE_DURATION_VIRTUAL_TRY_ONr  r  r   r   r  )r   rm  rn  r  r1  r   r   r   r^   r^   r_   r  l	  s.   
zKlingVirtualTryOnNode.executeN)r  r  r  r  r  r   rt  rz  r  rR  r   r   r  r^   r^   r^   r_   rl  K	  s    rl  c                   @   sl   e Zd ZdZedejfddZe		ddeded	ed
e	de
de
dededejdB dedejfddZdS )KlingImageGenerationNodecKling Image Generation Node. Generate an image from a text prompt with an optional reference image.rH   c                 C   s2  t jddddt jjddddt jjd	dd
dt jjddd tD ddt jjdddddt jjdddt jjdddddt jjdddt jjdg ddt jjddd t	D ddt j
jd d!d!d"d#d$t jjd%dd&t j
jd'd(d(d)t jjdd*dd+g
t j gt jjt jjt jjgdt jt jdd gd%gd,d-d.d/	S )0Nrq  zKling 3.0 Imager/  rr  r   Tr  r  r   r  
image_typec                 S   r_  r^   r`  ra  r^   r^   r_   r  	  r   z:KlingImageGenerationNode.define_schema.<locals>.<listcomp>)rd  ri  image_fidelityri  r   r  g{Gz?z,Reference intensity for user-uploaded images)rN   rS   rT   rf  rU   rO   ri  human_fidelityg?zSubject reference similarityr  )kling-v3zkling-v2rr   rc  r   c                 S   r_  r^   r`  ra  r^   r^   r_   r  	  r   r  r  nrI   	   zNumber of generated images)rN   rS   rT   rO   r  )r  r  r   r  r  r  )r  r\   ah  
                (
                  $m := widgets.model_name;
                  $base :=
                    $contains($m,"kling-v1-5")
                      ? (inputs.image.connected ? 0.028 : 0.014)
                      : $contains($m,"kling-v3") ? 0.028 : 0.014;
                  {"type":"usd","usd": $base * widgets.n}
                )
                r  r  )r   rt  rX   r   ru  r   rv  rZ   r[   r    rY   r  r  rx  r  r  r  r  r  r  ry  r^   r^   r_   rz  	  s   

7z&KlingImageGenerationNode.define_schemaNr   r  r   r   rs  rt  ru  rw  r   r  r  c                    s   |
}t |ddtd t |dtd t| ttddtt||||	d ur&t|	nd |	d ur-|nd ||||d	d	I d H }t| |j	j
}t| tt d
| dttdd dI d H }t| t|}tt|I d H S )Nr   rI   r  r   rM  r"  r#  )	r  r   r   r  image_referencert  ru  rw  r   r&  r'  r   c                 S   r(  r   r)  r   r^   r^   r_   r   
  r*  z2KlingImageGenerationNode.execute.<locals>.<lambda>r+  )rD   MAX_PROMPT_LENGTH_IMAGE_GENr;   r6   PATH_IMAGE_GENERATIONSr   r   r<   r   r   r   r:   AVERAGE_DURATION_IMAGE_GENr  r  r   r   r  )r   r  r   r   rs  rt  ru  rw  r   r  r  r  r1  r   r   r   r^   r^   r_   r  	  s@   
z KlingImageGenerationNode.execute)Nr   )r  r  r  r  r  r   rt  rz  r  r   r  r  r    r  rR  r   r  r^   r^   r^   r_   rq  	  s<    T	
rq  c                   @   sL   e Zd ZedejfddZedededededed	e	dej
fd
dZdS )TextToVideoWithAudiorH   c                 C   s   t jdddt jjddgdt jjddd	d
t jjddgdt jjdg ddt jjdddgdt jjddddgt j gt jj	t jj
t jjgdt jt jddgddddS )NKlingTextToVideoWithAudioz"Kling 2.6 Text to Video with Audiora  r  
kling-v2-6rc  r   TPositive text prompt.r  r%  rl   r   r  r  r  rQ  r  rN   ri  r  P{"type":"usd","usd": 0.07 * widgets.duration * (widgets.generate_audio ? 2 : 1)}r  rN  )r   rt  ru  r   rX   r  r  rx  r  r  r  r  r  r  ry  r^   r^   r_   rz  
  s.   	z"TextToVideoWithAudio.define_schemar  r   r%  r   r  r  c           	         s   t |ddd t| tdddtt||||t||rdndd	d
I d H }|jr8td|j d|j d|j	 t
| td|j	j dtdd dI d H }tt|j	jjd jI d H S )NrI   rb   r  !/proxy/kling/v1/videos/text2videor"  r#  r  r  )r  r   r%  r   r  r  r&  r   r   r   "/proxy/kling/v1/videos/text2video/r   c                 S   r   r   r   r   r^   r^   r_   r   W
  r   z.TextToVideoWithAudio.execute.<locals>.<lambda>r   r   )rD   r;   r6   r4   r5   r  r   r   r   r   r:   r   r   r   r8   r   r   r   )	r   r  r   r%  r   r  r  r   r   r^   r^   r_   r  7
  s4   


 zTextToVideoWithAudio.executeN)r  r  r  r  r   rt  rz  r  r  r   r   r  r^   r^   r^   r_   r}  
  s&    r}  c                   @   sN   e Zd ZedejfddZededej	dedede
d	edejfd
dZdS )ImageToVideoWithAudiorH   c                 C   s   t jdddt jjddgdt jdt jjdd	d
dt jjddgdt jjdddgdt jjdd	d	dgt j gt j	j
t j	jt j	jgd	t jt jddgddddS )NKlingImageToVideoWithAudioz0Kling 2.6 Image(First Frame) to Video with Audiora  r  r  rc  r3  r   Tr  r  r%  rl   r  r  rQ  r  r  r  r  r  rN  )r   rt  ru  r   r  rX   r  r  rx  r  r  r  r  r  r  ry  r^   r^   r_   rz  ^
  s.   
	z#ImageToVideoWithAudio.define_schemar  r3  r   r%  r  r  c           	         s   t |ddd t|ddd t|dd t| tdd	d
tt|t| |I d H d ||t||r1dndddI d H }|j	rMt
d|j	 d|j d|j t| td|jj dtdd dI d H }tt|jjjd jI d H S )NrI   rb   r  r  r  r  r
  "/proxy/kling/v1/videos/image2videor"  r#  r   r  r  )r  r  r   r%  r  r  r&  r   r   r   #/proxy/kling/v1/videos/image2video/r   c                 S   r   r   r   r   r^   r^   r_   r   
  r   z/ImageToVideoWithAudio.execute.<locals>.<lambda>r   )rD   rC   rB   r;   r6   r4   r)   r?   r  r   r   r   r   r:   r   r   r   r8   r   r   r   )	r   r  r3  r   r%  r  r  r   r   r^   r^   r_   r  {
  s8   


 zImageToVideoWithAudio.executeN)r  r  r  r  r   rt  rz  r  r   r  r  r   r   r  r^   r^   r^   r_   r  \
  s&    r  c                   @   sX   e Zd ZedejfddZe	ddedej	dej
ded	ed
ededejfddZdS )MotionControlrH   c                 C   s   t jdddt jjdddt jdt jjdd	d
t jjdddt jjdddgddt jjdddgdt jjdddgddgt j gt j	j
t j	jt j	jgdt jt jdgddddS )NKlingMotionControlzKling Motion Controlra  r   T)rM   reference_imager  u   Motion reference video used to drive movement/expression.
Duration limits depend on character_orientation:
 - image: 3–10s (max 10s)
 - video: 3–30s (max 30s)r	  r  r  character_orientationr  r  a_  Controls where the character's facing/orientation comes from.
video: movements, expressions, camera moves, and orientation follow the motion reference video (other details via prompt).
image: movements and expressions still follow the motion reference video, but the character orientation matches the reference image (camera/other details via prompt).r<  r%  rl   rh   rc  modelrv  r  r  r  z
                (
                  $prices := {"std": 0.07, "pro": 0.112};
                  {"type":"usd","usd": $lookup($prices, widgets.mode), "format":{"suffix":"/second"}}
                )
                r  rN  )r   rt  rX   r   r  r  r  ru  rx  r  r  r  r  r  r  ry  r^   r^   r_   rz  
  s>   
	zMotionControl.define_schemar  r   r  r  r  r  r%  r  c           
         s$  t |dd t|ddd t|dd |dkr t|dd	d
 nt|ddd
 t|ddddd t| tdddtt|t	| |I d H d t
| |I d H |rNdnd|||ddI d H }|jrmtd|j d|j d|j t| td|jj dtdd dI d H }	tt|	jjjd jI d H S )Nrb   )r   iT  r  r  r
  r  r  rQ  r     i
  r  z%/proxy/kling/v1/videos/motion-controlr"  r#  r   r   r!  )r   r  rY  r  r  r%  r  r&  r   r   r   z&/proxy/kling/v1/videos/motion-control/r   c                 S   r   r   r   r   r^   r^   r_   r   
  r   z'MotionControl.execute.<locals>.<lambda>r   )rD   rC   rB   rF   rE   r;   r6   r4   r+   r?   r@   r   r   r   r   r:   r   r   r   r8   r   r   r   )
r   r   r  r  r  r  r%  r  r   r   r^   r^   r_   r  
  sB   

 zMotionControl.executeN)r  )r  r  r  r  r   rt  rz  r  r   r  r  r   r   r  r^   r^   r^   r_   r  
  s,    0		r  c                   @   sR   e Zd ZedejfddZe	ddededede	d	e
jdB dejfd
dZdS )KlingVideoNoderH   c                 C   s|  t jddddt jjdt jdt jjddd	d
t jjddd	d
t jjddddt jjdgt jdt	dt jdt	dt jdt	dt jdt	dt jdt	dt jdt	dgddt j
jdddt jjdt jd t jjd!d"d#gd$t jjd%g d&d'dggd(dt jjd)d*d*d+t jjdd,d-t jjd.dd/d0gt j gt jjt jjt jjgdt jt jg d1d2d3d4d5	S )6Nr  zKling 3.0 Videora  zGenerate videos with Kling V3. Supports text-to-video and image-to-video with optional storyboard multi-prompt and audio generation.r  r  r   TrL   rM   rN   r   r  r  r  rR   r  r  rI   r  rP  r  r  rQ   r  r  r  zJGenerate a series of video segments with individual prompts and durations.r<  r  r  r  rv  r  r  r  rc  r   r  zIgnored in image-to-video mode.Model and generation settings.r  r   r  r  rN   rS   rT   rU   r  rO   r3  zLOptional start frame image. When connected, switches to image-to-video mode.r  )
model.resolutionr  r  zmulti_shot.durationz multi_shot.storyboard_1_durationz multi_shot.storyboard_2_durationz multi_shot.storyboard_3_durationz multi_shot.storyboard_4_durationz multi_shot.storyboard_5_durationz multi_shot.storyboard_6_durationr  a  
                (
                  $rates := {"1080p": {"off": 0.112, "on": 0.168}, "720p": {"off": 0.084, "on": 0.126}};
                  $res := $lookup(widgets, "model.resolution");
                  $audio := widgets.generate_audio ? "on" : "off";
                  $rate := $lookup($lookup($rates, $res), $audio);
                  $ms := widgets.multi_shot;
                  $isSb := $ms != "disabled";
                  $n := $isSb ? $number($substring($ms, 0, 1)) : 0;
                  $d1 := $lookup(widgets, "multi_shot.storyboard_1_duration");
                  $d2 := $n >= 2 ? $lookup(widgets, "multi_shot.storyboard_2_duration") : 0;
                  $d3 := $n >= 3 ? $lookup(widgets, "multi_shot.storyboard_3_duration") : 0;
                  $d4 := $n >= 4 ? $lookup(widgets, "multi_shot.storyboard_4_duration") : 0;
                  $d5 := $n >= 5 ? $lookup(widgets, "multi_shot.storyboard_5_duration") : 0;
                  $d6 := $n >= 6 ? $lookup(widgets, "multi_shot.storyboard_6_duration") : 0;
                  $dur := $isSb ? $d1 + $d2 + $d3 + $d4 + $d5 + $d6 : $lookup(widgets, "multi_shot.duration");
                  {"type":"usd","usd": $rate * $dur}
                )
                r  r  )r   rt  r  r   r  rX   rY   rZ   r[   r`   r  ru  r  r  r  rx  r  r  r  r  r  r  ry  r^   r^   r_   rz    s   
>zKlingVideoNode.define_schemaNr  r  r  r  r3  c                    s  |}|d dkrdnd}d}|d dkrd }	nd}	d	}d }
|	dkrzt |d  d
 }g }
td|d D ]*}|d| d }|d| d }t|d| dddd |
t||t|d q5tdd |
D }|dk sq|dkrytd| dn|d }t|d ddd |d urt	|ddd t
|dd t| |d d!I d H }t| td"d#d$tt|d% ||rd n|d |rd n|d& |t||rd'nd(|	rd	nd |
|	d)
d*I d H }d+|jj }n>t| td,d#d$tt|d% |d- |rd n|d |rd n|d& |t||rd'nd(|	r	d	nd |
|	d.
d*I d H }d/|jj }|jr/td0|j d1|j d2|j t| t|d3td4d5 d6I d H }tt|jjjd
 jI d H S )7Nr  r  rl   rh   Fr  r  r  Tr   rI   rJ   rK   rP   r  r  r  c                 s   r  r   r  r  r^   r^   r_   r     r  z)KlingVideoNode.execute.<locals>.<genexpr>r  rR   r  z$s) must be between 3 and 15 seconds.r  r   rb   r  r  r  r  r
  zUploading start framer   r  r"  r#  r  r   r  r  )
r  r  r   r   r%  r  r  r  r  r  r&  r  r  r   )
r  r   r   r   r%  r  r  r  r  r  r  r   r   r   r   c                 S   r   r   r   r   r^   r^   r_   r     r   z(KlingVideoNode.execute.<locals>.<lambda>r   )r  r  rV   rD   r  r,   r  r  r   rC   rB   r>   r;   r6   r4   r)   r   r   r5   r   r   r   r:   r   r   r8   r   r   r   )r   r  r  r  r  r3  r  r%  custom_multi_shotr  r  rG   r]   r  r  r  r  r   	poll_pathr   r^   r^   r_   r  x  s   	




 zKlingVideoNode.executer   )r  r  r  r  r   rt  rz  r  r   r  r   r  r   r  r^   r^   r^   r_   r    s$    rr  c                   @   sT   e Zd ZedejfddZededede	j
de	j
ded	ed
edejfddZdS )KlingFirstLastFrameNoderH   c                 C   s   t jddddt jjddddt jjd	d
ddt jjdt jdt jdt jjdddt j	jdt j	
dt jjdddgdggddt jjddddt jjdddgt j gt jjt jjt jjgdt jt jg dd d!d"d#	S )$Nr  z#Kling 3.0 First-Last-Frame to Videora  z:Generate videos with Kling V3 using first and last frames.r   TrL   r  r  r  r  rR   r  r  r4  r  r  r  rv  r  r  r  rc  r  r<  r  r   r  r  r  )r  r  r  r  a  
                (
                  $rates := {"1080p": {"off": 0.112, "on": 0.168}, "720p": {"off": 0.084, "on": 0.126}};
                  $res := $lookup(widgets, "model.resolution");
                  $audio := widgets.generate_audio ? "on" : "off";
                  $rate := $lookup($lookup($rates, $res), $audio);
                  {"type":"usd","usd": $rate * widgets.duration}
                )
                r  r  )r   rt  rX   r   rY   rZ   r[   r  r  r  r  ru  r  r  rx  r  r  r  r  r  r  ry  r^   r^   r_   rz    sd   

$z%KlingFirstLastFrameNode.define_schemar   r  r  r4  r  r  r  c                    s0  |}t |ddd t|ddd t|dd t|ddd t|dd t| |dd	I d H }	t| |d
d	I d H }
t| tdddtt|d |	|
||d dkrOdndt||rWdndddI d H }|j	rst
d|j	 d|j d|j t| td|jj dtdd dI d H }tt|jjjd jI d H S ) NrI   rb   r  r  r  r  r
  r  r   r  r  r"  r#  r  r  r  rl   rh   r  r  )r  r  r5  r   r%  r  r  r&  r   r   r   r  r   c                 S   r   r   r   r   r^   r^   r_   r   G  r   z1KlingFirstLastFrameNode.execute.<locals>.<lambda>r   r   )rD   rC   rB   r>   r;   r6   r4   r)   r  r   r   r   r   r:   r   r   r   r8   r   r   r   )r   r   r  r  r4  r  r  r  r  r  image_tail_urlr   r   r^   r^   r_   r    sD   

 zKlingFirstLastFrameNode.executeN)r  r  r  r  r   rt  rz  r  r  r   r  r   r  r   r  r^   r^   r^   r_   r    s*    B	r  c                   @   sP   e Zd ZedejfddZe	ddejdej	de
ded	e
dejfd
dZdS )KlingAvatarNoderH   c                 C   s   t jddddt jjdddt jjdd	dt jjd
ddgdt jjddddddt jjddddt jj	dddgt j
 gt jjt jjt jjgdt jt jd
gdddd	S )Nr  zKling Avatar 2.0ra  zTGenerate broadcast-style digital human videos from a single photo and an audio file.r  znAvatar reference image. Width and height must be at least 300px. Aspect ratio must be between 1:2.5 and 2.5:1.r	  
sound_filez;Audio input. Must be between 2 and 300 seconds in duration.r%  rh   rl   rc  r   TrL   zIOptional prompt to define avatar actions, emotions, and camera movements.)rM   rN   r  rO   r  r   r  r  r  r  z
                (
                  $prices := {"std": 0.056, "pro": 0.112};
                  {"type":"usd","usd": $lookup($prices, widgets.mode), "format":{"suffix":"/second"}}
                )
                r  r  )r   rt  r  r   re  ru  rX   rY   rZ   r  r  rx  r  r  r  r  r  r  ry  r^   r^   r_   rz  N  sV   zKlingAvatarNode.define_schemarL   r  r  r%  r  r   c                    s   t |ddd t|dd t|ddd t| tddd	ttt| |I d H t| |d
dddI d H |p3d |ddI d H }|j	rOt
d|j	 d|j d|j t| td|jj dtdd ddI d H }tt|jjjd jI d H S )Nr  r  r  r
  rP  r  z)/proxy/kling/v1/videos/avatar/image2videor"  r#  rR  rS  rT  rU  )r  r  r   r%  r&  r   r   r   z*/proxy/kling/v1/videos/avatar/image2video/r   c                 S   r   r   r   r   r^   r^   r_   r     r   z)KlingAvatarNode.execute.<locals>.<lambda>i   )r   r   max_poll_attemptsr   )rC   rB   rA   r;   r6   r4   r*   r>   r=   r   r   r   r   r:   r   r   r   r8   r   r   r   )r   r  r  r%  r  r   r   r   r^   r^   r_   r    s:   	


 zKlingAvatarNode.executeN)rL   )r  r  r  r  r   rt  rz  r   r  re  r  r  r   r  r^   r^   r^   r_   r  L  s$    7r  c                   @   s(   e Zd Zedeeej  fddZdS )KlingExtensionrH   c                    s8   t ttttttttt	t
ttttttttttttttgS r   )r`  r  rK  rS  rE  rV  rX  rc  rg  rl  rq  rb  r\  r  r  r  r  r'  r-  r}  r  r  r  r  r  )selfr^   r^   r_   get_node_list  s6   zKlingExtension.get_node_listN)	r  r  r  r   r  r8  r   	ComfyNoder  r^   r^   r^   r_   r    s    r  c                      s   t  S r   )r  r^   r^   r^   r_   comfy_entrypoint  s   r  )rH   Nr   rP  )NNNNNN)r  r   r   r   r  typing_extensionsr   comfy_api.latestr   r   r   r   comfy_api_nodes.apisr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   comfy_api_nodes.apis.klingr)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   comfy_api_nodes.utilr6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   r  r  r`   KLING_API_VERSIONr.  r:  rZ  r]  rC  PATH_CHARACTER_IMAGEro  r{  r-  r6  rz  $MAX_NEGATIVE_PROMPT_LENGTH_IMAGE_GENr\  r/  r;  r^  rp  r|  rD  r[  r  rW  rj  r  r   r8  r  r   r   r  r   r   r   r   r   r   r   r   r  rR  r  r  r  r  r  r  r2  r<  tupleVideoFromFilerF  r  re  r_  r`  r  r  r  r  r  r'  r-  rE  rK  rS  rV  rX  r\  rb  rc  rg  rl  rq  r}  r  r  r  r  r  r  r  r^   r^   r^   r_   <module>   sv   <$L	
	
 !"#$%&'()*+,-./0123456C


	
	

6	

C	
8	
>wY  G !ol|A`GcOZI4DC DFa Wsa 