
    љi              	          S SK r S SKrS SKJr  S SKJrJrJrJrJ	r	  S SK
JrJr  SSKJrJr  SSKJr  SS	KJr  \ R(                  " \5      r " S
 S\5      r " S S\5      r " S S\5      r " S S5      rSSS.S\S\S\S\4S jjrg)    N)Optional)	APIRouterDependsFastAPIHTTPExceptionstatus)	BaseModelField   )
STSRequestSTSResponse)Adapter   )create_api_key_dependencyc                   4    \ rS rSr% Sr\" SSSS9r\\S'   Sr	g	)
APIResponse   z
APIResponse is a standard response model for successful API operations.

Used to return a simple success message or status information.
.zMessage from APIz"Success message from API operationexampledescriptionmessage N)
__name__
__module____qualname____firstlineno____doc__r
   r   str__annotations____static_attributes__r       P/home/james-whalen/.local/lib/python3.13/site-packages/aiavatar/admin/control.pyr   r      s#    
 "8GS r!   r   c                   T    \ rS rSr% Sr\" SSSS9r\\S'   \" SS	S
S9r	\
\   \S'   Srg)SpeechRequest   z
SpeechRequest contains text for avatar speech synthesis and performance.

Supports control tags for face expressions and animations embedded in text.
.z![face:joy]Hi, let's talk with me!z@Text to synthesize with optional face and animation control tagsr   textNlocal_sessionRSession Id to route the response to a specific client (e.g., WebSocket connection)defaultr   r   
session_idr   )r   r   r   r   r   r
   r&   r   r   r+   r   r    r   r!   r"   r$   r$      sC    
 3VD# 
 !&h!J r!   r$   c                   t    \ rS rSr% Sr\" SSSS9r\\S'   \" SS	S
S9r	\
\   \S'   \" SSSS9r\
\   \S'   Srg)ChatRequest,   z
ChatRequest contains a text message for conversation processing.

Used to send messages to the AIAvatar's conversation processing pipeline.
.u   こんにちは！z2Text message to send to the conversation processorr   r&   Nr'   r(   r)   r+   user_001zCUser Id for conversation context management (e.g., per-user memory)user_idr   )r   r   r   r   r   r
   r&   r   r   r+   r   r0   r    r   r!   r"   r-   r-   ,   sc    
 $HD# 
 !&h!J 
 #YGXc] r!   r-   c                   J    \ rS rSrSS\S\4S jjrS\S\4S jrS\4S	 jr	S
r
g)
ControlAPIC   Nadapterdefault_session_idc                     Xl         X l        g Nr4   r5   )selfr4   r5   s      r"   __init__ControlAPI.__init__D   s    "4r!   r&   returnc                 Z    Un[         R                  " SSU5      nUR                  5       nU$ )Nz\[(\w+):([^\]]+)\] )resubstrip)r9   r&   
clean_texts      r"   remove_control_tagsControlAPI.remove_control_tagsH   s.    
VV12zB
%%'
r!   c                    ^  [        5       nUR                  SS/SSSSS0SS0SS	0S
.S9S[        S[        4U 4S jj5       nUR                  SS/SSSSS0SS0SS0SS	0S.S9S[        S[        4U 4S jj5       nU$ )Nz/avatar/performzAvatar Controlz#Perform avatar speech with controlszUSynthesize speech and perform avatar controls (face/animation) based on embedded tagsz"Performance completed successfullyr   )Avatar performance completed successfullyzInvalid text or control tagszInternal server error)       )tagssummaryr   response_description	responsesrequestr<   c                   >#     U R                   =(       d    TR                  nTR                  R                  R                  R                  TR                  U R                  5      S9I Sh  vN nTR                  R                  US5      I Sh  vN   TR                  U R                  5      nTR                  R                  [        SUU R                  UUSS0S95      I Sh  vN   TR                  R                  [        SUU R                  U0 S	95      I Sh  vN   [        S
S9$  N N NI N! [         a2  n[        R                  S5        [        [         R"                  SS9eSnAff = f7f)a
  
Perform comprehensive avatar actions including speech, face, and animation.

This endpoint processes text with embedded control tags:
- Synthesizes speech from the provided text (excluding control tags)
- Parses control tags for face expressions and animations
- Executes synchronized avatar performance

Control tag format:
- Face: [face:expression_name] (e.g., [face:joy])
- Animation: [animation:animation_name] (e.g., [animation:wave_hands])

Example: "[face:joy]Hello there! [animation:wave_hands]Nice to meet you!"
)r&   N_chunkis_first_chunkT)typer+   r&   
voice_text
audio_datametadatafinal)rS   r+   r&   rT   rV   rF   r   zError performing avatar actionsz5Internal server error while performing avatar actionsstatus_codedetail)r+   r5   r4   ststts
synthesizerC   r&   stop_responsehandle_responser   r   	Exceptionlogger	exceptionr   r   HTTP_500_INTERNAL_SERVER_ERROR)rN   r+   voicerT   exr9   s        r"   post_avatar_perform2ControlAPI.get_router.<locals>.post_avatar_performQ   sW    6$//J43J3J
"ll..22==4C[C[\c\h\hCi=jjll00SAAA!55gllC
ll22; ) )$.54    ll22; ) )4    #+VWW+ kA    #BD# & E ER sr   E4A(D5 -D-.#D5 D/AD5 'D1(7D5 D3 D5 ,E4-D5 /D5 1D5 3D5 5
E1?-E,,E11E4z/conversationz&Send message to conversation processorzDSend a text message to the AIAvatar conversation processing pipelinez=Success message indicating conversation processing completionMessage processed successfullyzAIAvatar is not listeningzInvalid message format)rG   i  rH   rI   c           	        >#     U R                   =(       d    TR                  nU(       a0  TR                  R                  R                  R                  US5      OSnTR                  R                  R                  [        XR                  X R                  S95        Sh  vN nUR                  S:X  aB  U(       a;  TR                  R                  R                  R                  USUR                  5        TR                  R                  U5      I Sh  vN   M   N{ N
 [        SS9$ ! [         a    e [          a5  n["        R%                  SU 35        [        [&        R(                  SS	9eSnAff = f7f)
a  
Send a text message to the conversation processing pipeline.

This endpoint processes a text message through the complete STS pipeline:
- Validates that the avatar is currently listening
- Invokes the Speech-to-Speech pipeline with the text input
- Processes LLM response and generates appropriate avatar actions
- Handles TTS synthesis and avatar control synchronization

The message will be processed as if it were spoken input, triggering
the full conversation flow including context management and response generation.

context_idN)r+   r0   rk   r&   startri   rX   z'Error processing conversation message: z;Internal server error while processing conversation messagerY   )r+   r5   r4   r\   vadget_session_datainvoker   r0   r&   rS   set_session_datark   r`   r   r   ra   rb   errorr   rd   )rN   r+   rk   resprf   r9   s        r"   processor_chat-ControlAPI.get_router.<locals>.processor_chat   sC    4$//J43J3J
`jT\\--11BB:|\pt
"&,,"2"2"9"9*PZdsds  AK  R^  R^  ;_  #` =$yyG+% LL,,00AA*l\`\k\kl,,66t<<<	= =	 #` #+KLL   FrdKL# & E EX s`   E8BD, D""D#D"&A1D, D D, D" D, "	D, +E8,E5 0E00E55E8)r   postr$   r   r-   )r9   routerrg   rs   s   `   r"   
get_routerControlAPI.get_routerN   s    	"#9o!E#%PQ#%CD#%<= 
 

-	} -	 -	

-	^ 
"#<^!`#%EF#%@A#%=>#%<=	 
 

	+ 	+ 	

	B r!   r8   r7   )r   r   r   r   r   r   r:   rC   r   rw   r    r   r!   r"   r2   r2   C   s6    5 5S 5  lI lr!   r2   )r5   api_keyappr4   r5   ry   c                    U(       a  [        [        U5      5      /O/ nU R                  [        XS9R	                  5       US9  g )Nr8   )dependencies)r   r   include_routerr2   rw   )rz   r4   r5   ry   depss        r"   setup_control_apir      sC     =DG-g678D7JUUW  r!   )loggingr?   typingr   fastapir   r   r   r   r   pydanticr	   r
   
sts.modelsr   r   adapter.baser   authr   	getLoggerr   rb   r   r$   r-   r2   r   r   r   r!   r"   <module>r      s     	  F F % 0 " +			8	$
) 
I $) .w w| #	  	
 r!   