
    iL                         d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ dededefdZdefdZdefdZdefdZdefdZdefdZdefdZd Zy)u  
Function call handlers — wired to the LLM via llm.register_function().

Each handler receives a FunctionCallParams object and uses result_callback
to return the result to the LLM context.

Handlers also push RTVIServerMessageFrame so the Android client receives
structured data (image URLs, video URLs, product cards, search links, etc.)
over the WebRTC data channel. The Android app listens for these in
VoiceClientManager.handleServerMessage().
    N)loggerRTVIServerMessageFrame)FunctionCallParams)generate_image)
edit_image)
web_search)shopping_search)generate_videomsg_typedatareturnc                      t        | |d      S )zBuild an RTVI server message frame for the Android client.

    The RTVIProcessor wraps this into {label: "rtvi-ai", type: "server-message",
    data: ...} which the Pipecat Android SDK delivers to onServerMessage().
    )typer   )r   r   )r   r   s     H/Users/thippareddysaicharanreddy/Desktop/agent_all/functions/handlers.py	_rtvi_msgr      s     "$'GHH    paramsc                 x  K   | j                   }|j                  dg       }|j                  dd      }dt        t        j                         dz         }|s|j                  d      r|d   |d   dz   g}t	        |      dk  r|dz  d	d }|d	d
 }t	        |      }|r|d   nd}t        j                  d| d|        t        |      D ]#  \  }}t        j                  d|dz    d|       % | j                  j                  t        dd||d             d	{    |D cg c]  }t        ||       }	}t        j                  |	ddi d	{   }
g }t        |
      D ]h  \  }}t        |t              r"t        j                   d|dz    d| d|        8|j                  d      sJ|d   }|j#                  | d| ||d       j |r| j                  j                  t        d|||d t	        |      d!             d	{    t	        |      dkD  rd"t	        |       d#nd$}| j%                  dt	        |      || d%d&       d	{    y	d'}| j                  j                  t        d(d)|i             d	{    | j%                  d)|d*       d	{    y	7 c c}w 7 [7 7 `7 07 w)+aP  Handle generate_image function call from the LLM.

    Supports generating 2-7 images in parallel with DIFFERENT prompts.
    Each prompt describes a unique angle/view/style. All Replicate calls
    run concurrently and successful URLs are sent as an array in a single
    RTVI message so the Android app can display them together.
    promptsaspect_ratioz1:1	imgbatch_  promptz!, different angle and perspective   N   r    z![Handler] generate_image called: z prompts, aspect_ratio=z[Handler]   Prompt    z: generation_startedimage)generation_typedescription
num_images)r   r   return_exceptionsTz[Handler] Image /z	 failed: successurl_)image_id	image_urls3_urlimage_generation_complete!black-forest-labs/flux-2-klein-4b)batch_idimagesr"   image_modelcountzAll z images arezThe image isza now visible in the user's app. Do NOT read out any URL. Just tell the user the images are ready.)statusimages_generatedimages_requestedmessagez/All image generations failed. Please try again.generation_endederrorr2   r5   )	argumentsgetinttimelenr   info	enumeratellm
push_framer   r   asynciogather
isinstance	Exceptionr7   appendresult_callback)r   argsr   r   r.   r#   r"   iptasksresultsr/   resultr'   	count_msg	error_msgs                   r   handle_generate_imagerP   (   s.     Dhhy"%G88NE2L3tyy{T1234H txx)>4>4W#WX 7|aQ;#bqkGWJ ''!*RK
KK
+J< 8$~	' '"1)!A#b67 # **

&&&$)
 	   A 	al; 
  NNEBTBBG Fw'	6fi(LL+AaC5*YvhOP::i -CMM'j!-   ( jj##1$ *BV4 
 	
 	
 036{Qd3v;-{+ 	 $$#$'K$. k "( (	
 	
 	
 F	jj##(7I*>?
 	
 	
 $$!$
 	
 	
A C$	
	
	
	
s   DJ:J'J:&J*:J:J/AJ:0AJ:
J2AJ:J41J:J6J:!J8"J:*J:2J:4J:6J:8J:c                 L  K   | j                   }|j                  dd      }dt        t        j                         dz         }t	        j
                  d|       | j                  j                  t        dd|d             d	{    | j                  j                  t        d
||d             d	{    t        |       d	{   }|j                  d      rU|d   }| j                  j                  t        d|||ddd             d	{    | j                  ddd       d	{    y	|j                  dd      }| j                  j                  t        d||d             d	{    | j                  d|d       d	{    y	7 7 7 7 7 m7 ,7 w)z1Handle generate_video function call from the LLM.r   r   video_r   z([Handler] generate_video called: prompt=r   videor!   r"   Nvideo_generation_started)
request_idr   )r   r&   r'   video_generation_completezwan-video/wan-2.2-t2v-fast        )rV   	video_urlr   video_modeldurationzVideo generated successfully. The video is now visible in the user's app. Do NOT read out any URL. Just tell the user the video is ready.r8   r7   Video generation failed.video_generation_errorrV   r7   )r9   r:   r;   r<   r   r>   r@   rA   r   r   rG   )r   rH   r   rV   rM   rY   rO   s          r   handle_generate_videor_      s    DXXh#F#diikD0123J
KK:6*EF **

&&!)
 	   **

,$/
 	   "00Fzz)5M	 jj##1(& ;4 
 	
 	
 $$#3	
 		
 		
 JJw(BC	 jj##.("1 
 	
 	
 $$!$
 	
 	
e 1	
		
	
	
s   BF$F/F$7F8F$FAF$FF$0F1AF$3F 4F$F"F$F$F$F$F$ F$"F$c           
        K   | j                   }|j                  dd      }t        j                  d|       | j                  j                  t        dd|d             d{    t        |       d{   }|j                  d	      r/|j                  d
g       }|j                  dd      }| j                  d	|dd       d{    d}	 ddl	m
}  ||||       d{   }|j                  d	      r5|j                  dd      }t        j                  dt        |       d       n't        j                  d|j                  d              | j                  j                  t        d|||dd |d             d{    | j                  j                  t        dddi             d{    y| j                  j                  t        dd|j                  dd      d             d{    | j                  d|j                  dd      d       d{    y7 7 7 f7 M# t        $ r#}	t        j                  d|	        Y d}	~	d}	~	ww xY w7 7 7 p7 Fw)u  Handle web_search function call from the LLM.

    Sends the text summary back to the LLM for TTS, and also pushes
    structured source links + C1 visual card to the Android app via RTVI.
    C1 visual card generation runs in parallel — it doesn't block the
    voice response.
    queryr   z#[Handler] web_search called: query=r   searchrT   Nra   r&   sourcessummaryzSearch complete. Source links and a visual card are shown in the user's app. Do NOT read out any URLs. Present the information in natural spoken language.)r2   search_resultsr5   r   )generate_visual_card)ra   re   rd   c1_dslz$[Handler] C1 visual card generated (z chars)z[Handler] C1 card failed: r7   z&[Handler] C1 card generation skipped: rf      )ra   re   rd   rh   r6   r!   zSearch failed.r!   r7   r8   )r9   r:   r   r>   r@   rA   r   r	   rG   services.c1_servicerg   r=   warningrE   )
r   rH   ra   rM   rd   re   rh   rg   	c1_resultes
             r   handle_web_searchro      s     DHHWb!E
KK5eY?@ **

&' )
 	   E**Fzz)**Y+**Y+ $$#")J	

 
	
 
	
 	I@2Wg I }}Y'"x4B3v;-wWX!;IMM'<R;STU
 jj##&""2A; 	) 
 	
 	
 jj##(+<h*GH
 	
 	

 jj##(#+G-=>+ 
 	
 	
 $$!!::g/?@
 	
 	
C +
	
"  	INNCA3GHH	I	
	

	
	
s   A"I.$H+%I.8H.9AI.H1I.H7 'H4(A1H7 0I.	I&
.I.8I(9A I.9I*:+I.%I,&I..I.1I.4H7 7	I# II.I##I.(I.*I.,I.c           
      j  K   | j                   }|j                  dd      }t        j                  d|       | j                  j                  t        dd|d             d{    t        |       d{   }|j                  d	      r|j                  d
g       }|r:| j                  j                  t        d||t        |      d             d{    | j                  j                  t        dddi             d{    | j                  d	|d   t        |      dd       d{    y| j                  j                  t        dd|j                  dd      d             d{    | j                  d|j                  dd      d       d{    y7 ?7 .7 7 7 |7 =7 w)zHandle shopping_search function call from the LLM.

    Sends structured product cards to the Android app via RTVI, and returns
    a spoken summary to the LLM for TTS.
    ra   r   z([Handler] shopping_search called: query=r   shoppingrT   Nrc   r&   productsshopping_results)ra   rr   r1   r6   r!   re   zShopping results are now displayed as product cards in the user's app with images, prices, and buy links. Do NOT read out any URLs or links. Just summarize the top products with their names, prices, and store names in natural spoken language.)r2   re   product_countr5   r7   zShopping search failed.rj   r8   )
r9   r:   r   r>   r@   rA   r   r
   r=   rG   )r   rH   ra   rM   rr   s        r   handle_shopping_searchru   4  s     DHHWb!E
KK:5)DE **

&) )
 	   #//Fzz)::j"-**''," ( ]/    jj##(+<j*IJ
 	
 	
 $$#!),!$X2
 	
 	
  jj##(#-G-FG+ 
 	
 	
 $$!!::g/HI
 	
 	
e 0	
	
 	
	
s   A"F3$F#%F38F&9AF3F).F3F+)F30F-1A F31F/2+F3F1F3&F3)F3+F3-F3/F31F3c                   K   ddl }| j                  }|j                  dd      }dt        t	        j                         dz         }t        j                  d|       |j                  s| j                  dd	d
       d{    y|j                  d   }|j                  d   }| j                  j                  t        dd|d             d{    t        |||       d{   }|j                  d      rU|d   }| j                  j                  t        dd|||dd             d{    | j                  ddd
       d{    y|j                  dd      }	| j                  j                  t        dd|	d             d{    | j                  d|	d
       d{    y7 /7 7 7 7 m7 ,7 w)zHandle edit_image function call from the LLM.

    Takes the user's uploaded photo from memory and sends it directly to
    the model with the raw edit instruction. No S3, no prompt rewriting.
    r   Nedit_instructionr   edit_r   z[Handler] edit_image called: r7   zVNo photo uploaded yet. Ask the user to upload a photo first before requesting an edit.r8   base64	mime_typer   
image_editrT   )image_base64instructionrz   r&   r'   image_edit_complete	completedr-   )r2   edit_idedited_image_urlrw   r0   zpThe edited image is now visible in the user's app. Do NOT read out any URL. Just tell the user the edit is done.zImage edit failed.r6   rj   )botr9   r:   r;   r<   r   r>   uploaded_imagerG   r@   rA   r   r   )
r   
bot_modulerH   rw   r   r|   rz   rM   
edited_urlrO   s
             r   handle_edit_imager   }  s     Dxx 2B7c$))+,-./G
KK/0@/CDE $$$$-&
  	 	 	,,X6L))+6I **

&++)
 	   !$ F zz)E]
 jj##+%"$.$4B. 
 	
 	
 $$P&
  	 	 JJw(<=	jj##(#/"+ 
 	
 	
 $$ &
  	 	s		
		
	s   BGF2AGF5G(F7)AG1F92GF;AGF=G,F?-G5G7G9G;G=G?Gc                   K   ddl }| j                  }|j                  dd      }dt        t	        j                         dz         }t        j                  d|       |j                  s| j                  dd	d
       d{    y|j                  d   }|j                  d   }d| d| }| j                  j                  t        dd|d             d{    | j                  j                  t        d||dd             d{    t        ||       d{   }|j                  d      rV|d   }	| j                  j                  t        d||	|dddd             d{    | j                  ddd
       d{    y|j                  dd      }
| j                  j                  t        d||
d             d{    | j                  d|
d
       d{    y7 h7 7 7 7 7 p7 /7 w) zHandle generate_video_from_image function call from the LLM.

    Takes the user's uploaded photo from memory and sends it directly to
    Replicate's video model as a base64 data URI.
    r   Nr   r   rR   r   z,[Handler] generate_video_from_image called: r7   zVNo photo uploaded yet. Ask the user to upload a photo first before requesting a video.r8   ry   rz   zdata:z;base64,r   rS   rT   rU   r   )rV   r   source)r   r*   r&   r'   rW   zlightricks/ltx-2-distilledrX   )rV   rY   r   rZ   r[   r   zVideo generated from the uploaded photo! The video is now visible in the user's app. Do NOT read out any URL. Just tell the user the video is ready.r\   r]   r^   )r   r9   r:   r;   r<   r   r>   r   rG   r@   rA   r   r   )r   r   rH   r   rV   r|   rz   data_urirM   rY   rO   s              r    handle_generate_video_from_imager     sQ     DXXh#F#diikD0123J
KK>vjIJ $$$$-&
  	 	 	,,X6L))+6I yk,8H **

&&!)
 	   **

,$&/
 	   "8DDFzz)5M	jj##1(& ;*4 	
 		
 		
 $$9&
  	 	 JJw(BC	jj##.("1 
 	
 	
 $$ &
  	 	C	  E
		
		
	s   BG=G+AG=G.0G=G1G= G3!A	G=*G5+G=G7AG=	G9
G=%G;&G=.G=1G=3G=5G=7G=9G=;G=c                 F   | j                  dt        d       | j                  dt        d       | j                  dt        d       | j                  dt        d       | j                  dt
               | j                  dt               t        j                  d	       y
)zRegister all function handlers with the LLM service.

    Image and video generation are long-running (10-60s+), so we set
    cancel_on_interruption=False so the user can keep talking without
    cancelling the generation.
    r   F)cancel_on_interruptionr   r   generate_video_from_imager	   r
   z[Handler] Function handlers registered (generate_image, generate_video, edit_image, generate_video_from_image, web_search, shopping_search)N)	register_functionrP   r_   r   r   ro   ru   r   r>   )r@   s    r   register_all_handlersr   &  s     /   /   '   #($  
 ,(9:+-CD
KK	Br   )__doc__rB   r<   logurur   "pipecat.processors.frameworks.rtvir   pipecat.services.llm_servicer   services.image_servicer   services.edit_image_servicer   services.search_servicer	   services.shopping_servicer
   services.video_servicer   strdictr   rP   r_   ro   ru   r   r   r    r   r   <module>r      s   
    E ; 1 2 . 5 1I I4 I,B If
(: f
X@
(: @
LT
$6 T
tC
); C
RL$6 LdT3E Tnr   