o
    z­jã  ã                   @   s¸   d dl mZmZmZ d dlmZmZ d dlZd dlm	Z	m
Z
mZmZ d dlmZmZmZmZ d dlmZ eƒ ZG dd„ deƒZG d	d
„ d
eƒZejdedd�edƒfdefdd„ƒZdS )é    )Ú	APIRouterÚHTTPExceptionÚBody)Ú	BaseModelÚFieldN)ÚOptionalÚDictÚAnyÚList)Ú	AgentTypeÚMessageÚcoordinatorÚcontext_protocol)Úprocess_voice_command_aic                   @   sŠ   e Zd ZU edddd�Zeed< edddd	�Zeed
< eddd�Ze	e ed< eddd�Z
e	e ed< eddd�Ze	eeef  ed< dS )ÚVoiceCommandInput.é   z,The transcribed text from the user's speech.)Ú
min_lengthÚdescriptionÚ
transcriptÚenz^(en|hi|mr)$z&Language code (e.g., 'en', 'hi', 'mr'))ÚdefaultÚpatternr   ÚlanguageNz0Optional session ID for continuing conversations)r   Ú
session_idÚBaramatiz(User's location for contextual responsesÚlocationz(Additional context for the voice commandÚcontext_data)Ú__name__Ú
__module__Ú__qualname__r   r   ÚstrÚ__annotations__r   r   r   r   r   r   r	   © r"   r"   ú:/var/www/html/CropPilot/backend/app/api/endpoints/voice.pyr      s   
 $r   c                   @   sZ   e Zd ZU eed< dZee ed< eed< dZee ed< dZe	ed< dZ
ee ed< dS )	ÚVoiceCommandResponseÚresponse_textNÚ	audio_urlr   Údetected_intentFÚrequires_followupÚfollowup_type)r   r   r   r    r!   r&   r   r'   r(   Úboolr)   r"   r"   r"   r#   r$      s   
 r$   z/commandéÈ   )Úresponse_modelÚstatus_code.Úcommand_inputc              
   Ã   sL  �zy| j p
tt ¡ ƒ}t || j| jpd| jdœ| j	pi ¥¡ t
 ttjtjd| jidd|id�¡I dH }|r<d|jv rM|rE|j dd	¡nd
}td|d�‚|j dd¡}|j dd¡}|j d¡}|re|nd}d}t||||||rv|d�W S dd�W S  tyˆ }	 z|	‚d}	~	w ty¥ }
 ztd|
› �ƒ tddt|
ƒ› �d�‚d}
~
ww )z“
    Receives transcribed text, processes the command using the multi-agent system,
    and returns a text response in the specified language.
    r   )r   r   Úlast_transcriptr   Úprocess_commandr   )ÚsenderÚreceiverÚcontentÚmessage_typeÚcontextNÚerrorz&Unknown error processing voice commandzNo response from voice agenti÷  )r-   ÚdetailÚresponsez'Sorry, I couldn't process that command.Úshould_routeFÚtarget_agentÚgeneral_query)r%   r&   r   r'   r(   r)   z'Unexpected error in /command endpoint: iô  z#An internal server error occurred: )r   r    ÚuuidÚuuid4r   Úset_contextr   r   r   r   r   Úroute_messager   r   ÚCOORDINATORÚVOICE_ASSISTANTr3   Úgetr   r$   Ú	ExceptionÚprint)r.   r   Úmessager6   r%   r9   r:   r'   r&   Úhttp_excÚer"   r"   r#   Úhandle_voice_command   s\   €ýüÿù
ÿúú	€€ýrH   )Úfastapir   r   r   Úpydanticr   r   r<   Útypingr   r   r	   r
   Úapp.core.multi_agentr   r   r   r   Úapp.core.ai_servicesr   Úrouterr   r$   ÚpostrH   r"   r"   r"   r#   Ú<module>   s    ÿÿ