+
    ~jA                     N   ^ RI t ^ RIt^ RIt^ RIHt ^ RIHt ^ RIHtH	t	 ^ RI
Ht ^RIHtHt ^RIHt ^RIHtHtHt ^R	IHt ^R
IHt ^RIHtHt ^RIHtHtHtH t  Rt!Rt"Rt#Rt$Rt%^t&Rt' ! R R](]4      t) ! R R]4      t*Rt+R R lt, ! R R]]	R,          ,          4      t-R# )    N)Callable)Enum)AnyLiteral)	BaseModel)ChatContextChatMessage)LLM)ToolToolContextfunction_tool)execute_function_calllogger)	NOT_GIVEN
NotGivenOr)EventEmitteraiois_givenlog_exceptionsg      @g      ?g      ?g      $@g      4@c                   *    ] tR t^tRtRtRtRtRtRt	R# )AMDCategoryhumanzmachine-ivrz
machine-vmzmachine-unavailable	uncertain N)
__name__
__module____qualname____firstlineno__HUMANMACHINE_IVR
MACHINE_VMMACHINE_UNAVAILABLE	UNCERTAIN__static_attributes__r       x/Users/mitch_tango/dev/rabbit-r1-livekit/agent/.venv/lib/python3.14/site-packages/livekit/agents/voice/amd/classifier.pyr   r      s    EKJ/Ir&   r   c                   h   a  ] tR t^$t o Rt]V 3R lR l4       t]V 3R lR l4       tV 3R ltRt	V t
R# )	AMDPredictionEventamd_predictionc                    < V ^8  d   QhRS[ /#    returnbool)format__classdict__s   "r'   __annotate__AMDPredictionEvent.__annotate__-   s     2 2$ 2r&   c                <    V P                   \        P                  8H  # N)categoryr   r    selfs   &r'   is_humanAMDPredictionEvent.is_human,   s    }} 1 111r&   c                    < V ^8  d   QhRS[ /# r,   r/   )r1   r2   s   "r'   r3   r4   1   s     
 
D 
r&   c                z    V P                   \        P                  \        P                  \        P                  39   # r6   )r7   r   r!   r"   r#   r8   s   &r'   
is_machineAMDPredictionEvent.is_machine0   s3    }}##""++!
 
 	
r&   c                p   < V ^8  d   Qh/ S[ R,          ;R&   S[;R&   S[;R&   S[;R&   S[;R&   S[;R&   # )r-   r*   typespeech_durationr7   reason
transcriptdelay)r   floatr   str)r1   r2   s   "r'   r3   r4   $   sR     
"
#6     K	 
 O  L r&   r   N)r   r   r   r   rA   propertyr:   r>   __annotate_func__r%   __classdictcell__)r2   s   @r'   r)   r)   $   s9     &6D 2 2 
 
  r&   r)   a  Task:
Classify the call greeting transcript into exactly one of these categories:

human: A person answered (e.g., "Hello?", "This is John.").
machine-ivr: A prompt to press a key (e.g., "Press 1 to continue").
machine-vm: A voicemail greeting where leaving a message IS possible.
machine-unavailable: Any greeting indicating it's NOT possible to leave message, eg because mailbox is full, not setup, etc.
uncertain: For partial transcripts that are ambiguous.

Examples:
Input: "The person you called has a voice mailbox that hasn't been set up yet. Goodbye."
Output: machine-unavailable

Input: "Thank you for calling Truly Pizza in Dana Pointe. Our hours of operation are 11AM to 8PM, Sunday through Thursday, 11AM to 9PM, Friday and Saturday, and we're closed on Tuesdays."
Output: uncertain

Input: "You for calling Truly Pizza in Dana Pointe. Our hours of operation are 11AM to 8PM, Sunday through Thursday, 11AM to 9PM, Friday and Saturday, and we're closed on Tuesdays. If you'd like to place an order, please press 1 or head to our website to order online for pickup and local delivery."
Output: machine-ivr

Input: "Please state your name and why you're calling, and I will check if the person is available"
Output: machine-ivr
Note: this should apply for any call screening prompts.

Input: "I'm away from my desk. If you leave a message, I will get back to you."
Output: machine-vm

Input: "Hello, this is Lisa."
Output: humanc                d    V ^8  d   QhR\         R\        3,          R\         R\        3,          /# )r-   method.r.   )r   r   )r1   s   "r'   r3   r3   Z   s,      #s(+ c0B r&   c                 L   a  \         P                  ! S 4      R  V 3R ll4       pV# )c                @    V ^8  d   QhRRR\         R\         R\         /# )r-   r9   _AMDClassifierargskwargsr.   )r   )r1   s   "r'   r3   "_state_guard.<locals>.__annotate__\   s)     - -& -s -c -c -r&   c                    < V P                   '       g   V P                  '       g/   \        P                  ! R V P                  V P                   4       R# S! V .VO5/ VB # )z+AMD state is invalid: started=%s, closed=%sN)closedstartedr   warning)r9   rP   rQ   rL   s   &*,r'   wrapper_state_guard.<locals>.wrapper[   sK    ;;;dlllNN=
 d,T,V,,r&   )	functoolswraps)rL   rW   s   f r'   _state_guardr[   Z   s%    __V- - Nr&   c                     a a ] tR t^it o]]]]]]	R3V3R lV 3R lllt
V3R lR ltV3R lR lt]V3R lR	 l4       t]V3R
 lR l4       tV3R lR ltV3R lR lt]! ]R7      ]]]R3V3R lR ll4       4       t]R#V3R lR ll4       tV3R lR lt]! ]R7      V3R lR l4       tV3R lR lt]V3R lR l4       t]V3R lR l4       t]V3R  lR! l4       tR"tVtV ;t # )$rO   sttc                J   < V ^8  d   QhRS[ RS[RS[RS[RS[RS[RS[RS[/# )	r-   llmhuman_speech_thresholdhuman_silence_thresholdmachine_silence_thresholdno_speech_thresholdtimeoutpromptsource)r
   rF   rG   )r1   r2   s   "r'   r3   _AMDClassifier.__annotate__j   s[     &" &"&" !&&" "'	&"
 $)&" #&" &" &" &"r&   c	                  < \         S	V `  4        W n        W0n        W@n        WPn        W`n        Wn        \        P                  ! 4       V n
        R V n        R V n        R V n        R V n        R V n        R V n        \"        P$                  ! 4       V n        Wn        Wpn        R V n        R V n        RV n        RV n        RV n        RV n        RV n        ^ V n        R # )NF )super__init___human_speech_threshold_human_silence_threshold_machine_silence_threshold_no_speech_threshold_timeout_sourcer   Chan	_input_ch_classify_task_no_speech_timer_silence_timer_silence_timer_trigger_detection_timeout_timer_verdict_resultasyncioEvent_verdict_ready_llm_prompt_speech_started_at_speech_ended_at_started_closed_machine_silence_reached_emitted_transcript_extension_count)
r9   r_   r`   ra   rb   rc   rd   re   rf   	__class__s
   &&&&&&&&&r'   rk   _AMDClassifier.__init__j   s     	'=$(?%*C'$7!(+
9=<@:>UY#DH%:>%mmo	04.2(-% !r&   c                   < V ^8  d   QhRR/# r-   r.   Nr   )r1   r2   s   "r'   r3   rg      s      t r&   c                <    V P                   '       d   R# RV n         R# )zQMark classifier as started (enables state guard). Call start_timers() separately.NTr   r8   s   &r'   start_AMDClassifier.start   s    ===r&   c                   < V ^8  d   QhRR/# r   r   )r1   r2   s   "r'   r3   rg      s     
 
d 
r&   c           	        V P                   '       d   V P                  '       d   R# \        P                  ! 4       P	                  V P
                  \        P                  ! V P                  \        P                  RR7      4      V n        \        P                  ! 4       P	                  V P                  \        P                  ! V P                  \        P                  RR7      4      V n        R# )zEStart the no-speech and detection-timeout timers. Call after start().Nno_speech_timeout)r7   rC   detection_timeout)r   r   rz   get_running_loop
call_laterro   rY   partial_silence_timer_callbackr   r#   ru   rp   r$   rx   r8   s   &r'   start_timers_AMDClassifier.start_timers   s    }}} ' 8 8 : E E%%,,$88*!
 )0(@(@(B(M(MMM,,$..*)
%r&   c                   < V ^8  d   QhRR/# r   r   )r1   r2   s   "r'   r3   rg      s     
. 
. 
.r&   c                .   V P                   e)   V P                   P                  4        R V n         R V n        V P                  e"   V P                  P                  4        R V n        V P                  f   \
        P
                  ! 4       V n        RV n        R # )NF)rv   cancelrw   ru   r   timer   r8   s   &r'   on_user_speech_started%_AMDClassifier.on_user_speech_started   sx    *&&("&D*.D'  ,!!((*$(D!""*&*iikD#(-%r&   c                $   < V ^8  d   QhRS[ RR/# )r-   silence_durationr.   NrF   )r1   r2   s   "r'   r3   rg      s     -4 -4U -4t -4r&   c           
     
   V P                   f   \        P                  ! R4       R # \        P                  ! 4       V,
          V n        V P                  V P                   ,
          pW P
                  8:  Ed5   V P                  e)   V P                  P                  4        R V n        R V n        V P                  '       g   \        P                  ! 4       P                  \        ^ V P                  V,
          4      \        P                   ! V P"                  \$        P&                  RVR7      4      V n        RV n        R # \        P                  ! 4       P                  \        ^ V P(                  V,
          4      \        P                   ! V P"                  VR7      4      V n        RV n        R # V P*                  f*   \        P,                  ! V P/                  4       4      V n        V P                  e)   V P                  P                  4        R V n        R V n        \        P                  ! 4       P                  \        ^ V P(                  V,
          4      \        P                   ! V P"                  VR7      4      V n        RV n        R # )Nz9on_user_speech_ended called before on_user_speech_startedshort_greeting)r7   rC   rB   short_speechrB   long_speech)r   r   rV   r   r   rl   rv   r   rw   r   rz   r   r   maxrm   rY   r   r   r   r    rn   rt   create_task_classify_user_speech)r9   r   rB   s   && r'   on_user_speech_ended#_AMDClassifier.on_user_speech_ended   s   ""*NNVW $		.> >//$2I2II:::"".##**,&*#.2+###&-&>&>&@&K&K488;KKL%%44!,!2!2/(7	'# /=+  '.&>&>&@&K&K4::=MMN%%44(7'# /<+&")"5"5d6P6P6R"SD*&&("&D*.D'%668CC4225EEFd::O\
 '4#r&   c                $   < V ^8  d   QhRS[ RR/# )r-   resultr.   N)r)   )r1   r2   s   "r'   r3   rg      s        #5  $  r&   c                2    Wn         V P                  4        R # r6   )ry   _try_emit_result)r9   r   s   &&r'   _set_verdict_AMDClassifier._set_verdict   s    %r&   c                   < V ^8  d   QhRR/# r   r   )r1   r2   s   "r'   r3   rg      s      $ r&   c                r   V P                   f   R # V P                  '       g   R # V P                  '       g   V P                  '       d   R # V P                  P                  4        V P                  e"   V P                  P                  4        R V n        V P                  RV P                   4       RV n        R # )Nr*   T)	ry   r   r   r   r|   setrx   r   emitr8   s   &r'   r   _AMDClassifier._try_emit_result   s    ',,,<<<4===!((4))002,0D)		"D$8$89r&   r   Nc                ^   < V ^8  d   QhRS[ S[,          RS[ S[,          RS[R,          RR/# )r-   r7   rC   rB   Nr.   )r   r   rG   rF   )r1   r2   s   "r'   r3   rg      s@        [)  3  	 
 
 r&   c           
        V P                   '       d)   V P                   P                  4        R V n         R V n        \        V4      '       d   \        V4      '       dz   V P                  fl   T P                  \        T;'       g    V P                  TTRV P                  '       d'   \        P                  ! 4       V P                  ,
          MRR7      4       RV n
        V P                  4        R # )Nri           rB   r7   rC   rD   rE   T)rv   r   rw   r   ry   r   r)   rB   r   r   r   r   )r9   r7   rC   rB   s   &&&&r'   r   &_AMDClassifier._silence_timer_callback   s     &&("&D*.D'H(6"2"2t7K7K7S"$3$K$Kt7K7K%!!CGCXCXCX499;)>)>>^a )-%r&   c                *   < V ^8  d   QhRS[ RS[ RR/# )r-   textrf   r.   NrG   )r1   r2   s   "r'   r3   rg     s'      D  Dc  D3  D4  Dr&   c                   V P                   P                  '       d   \        P                  ! R4       R# W P                  8w  d   R# V P
                  e   V P                  R8X  d   V P
                  P                  4        RV n        RV n        V P                  f   Q hV P                  V P                  ,           \        P                  ! 4       ,
          p\        P                  ! 4       P                  \        ^ V4      \        P                   ! V P"                  V P$                  R7      4      V n        RV n        V P&                  f*   \        P(                  ! V P+                  4       4      V n        V P,                  e"   V P,                  P                  4        RV n        V P                   P/                  V4       V P0                  R,           V,           P3                  4       V n        R# )z+Push transcript text to the AMD classifier.zpush_text called after closeNr   r   r    )rs   rT   r   debugrq   rv   rw   r   r   rn   r   rz   r   r   r   rY   r   r   rB   rt   r   r   ru   send_nowaitr   lstrip)r9   r   rf   	remainings   &&& r'   	push_text_AMDClassifier.push_text  sv    >>   LL78\\!*t/J/Jn/\&&("&D*.D' ((444..1P1PPTXT]T]T__I")":":"<"G"GAy!!!00$($8$8#D +8D'&")"5"5d6P6P6R"SD  ,!!((*$(D!""4( ,,s2T9AACr&   c                   < V ^8  d   QhRR/# r   r   )r1   r2   s   "r'   r3   rg   :  s      4 r&   c                v    V P                   P                  '       d   R # V P                   P                  4        R # r6   )rs   rT   closer8   s   &r'   	end_input_AMDClassifier.end_input:  s%    >>   r&   c                   < V ^8  d   QhRR/# r   r   )r1   r2   s   "r'   r3   rg   @  s     P5 P5T P5r&   c                :  a aaa"   R R/oRpR VV 3R lloR V 3R llo\        \        R7      R VVVV 3R	 ll4       p S P                    Rj  xL
  pSR ,          R
,           V,           P                  4       SR &   Ve   \        P
                  ! V4      G Rj  xL
  \        P                  ! V! SR ,          4      4      pKy   Lt L+Dw Te"   \        P
                  ! T4      G Rj  xL 
  R# R#   Te!   \        P
                  ! T4      G Rj  xL 
  i i ; i5i)rD   ri   Nc                (    V ^8  d   QhR\         RR/# )r-   labelr.   N)r   )r1   s   "r'   r3   :_AMDClassifier._classify_user_speech.<locals>.__annotate__D  s     	 	 	 	r&   c           
       <"   V \         P                  8w  dk   SP                  \        SP                  T RSR,          SP
                  '       d'   \        P                  ! 4       SP
                  ,
          MRR7      4       R# R# 5i)z#Save the prediction to the verdict.r_   rD   r   r   N)r   r$   r   r)   rB   r   r   )r   ctxr9   s   &r'   save_prediction=_AMDClassifier._classify_user_speech.<locals>.save_predictionD  sk     ---!!&(,(<(<!&$#&|#4000  $yy{T-B-BB 
 .s   BBc                0    V ^8  d   QhR\         R\        /# )r-   secondsr.   )rF   rG   )r1   s   "r'   r3   r   S  s     	< 	< 	<# 	<r&   c                N  <"   \        V \        4      pS;P                  ^,          un        SP                  e)   SP                  P	                  4        RSn        RSn        \        P                  ! 4       pR V3R llpVP                  W4      Sn        RSn        RVR R2# 5i)zPostpone the termination of the classification task.
Use when the transcript is ambiguous and more audio is expected.

Args:
    seconds: Additional seconds to wait (max 10).
Nc                    V ^8  d   QhRR/# r   r   )r1   s   "r'   r3   X_AMDClassifier._classify_user_speech.<locals>.postpone_termination.<locals>.__annotate__c  s     ( ($ (r&   c                     < R S n         S P                  P                  '       g   S P                  P                  R4       S P	                  4        R# )Tri   N)r   rs   rT   r   r   r8   s   r'   _on_postpone_elapsed`_AMDClassifier._classify_user_speech.<locals>.postpone_termination.<locals>._on_postpone_elapsedc  s=    
 15-~~,,, NN..r2%%'r&   r   zwaiting z.1fzs for more audio)	minMAX_EXTENSION_SECSr   rv   r   rw   rz   r   r   )r   clampedloopr   r9   s   &   r'   postpone_terminationB_AMDClassifier._classify_user_speech.<locals>.postpone_terminationS  s      '#56G!!Q&!"".##**,&*#.2+++-D( ( #'//'"PD*7D'gc]*:;;s   B"B%r   c                (    V ^8  d   QhR\         RR/# )r-   rD   r.   Nr   )r1   s   "r'   r3   r   u  s     	R 	R3 	R4 	Rr&   c           
        <"   V SR &   \        S4      .pSP                  \        8  d   VP                  \        S4      4       SP                  P                  \        \        RSP                  .R7      \        RV .R7      .R7      VRR7      pVP                  4       G Rj  xL
 pVP                   F*  p\        V\        VP                  4      4      G Rj  xL
  K,  	  R#  L@ L5i)rD   system)rolecontentuser)itemsrequired)chat_ctxtoolstool_choiceN)r   r   MAX_EXTENSIONSappendr}   chatr   r	   r~   collect
tool_callsr   r   r   )	rD   r   streamresponse	tool_callr   r   r   r9   s	   &    r'   _run2_AMDClassifier._classify_user_speech.<locals>._runt  s      *C!.!? @E$$~5]+?@AYY^^$#DLL>J#*F & $ 	F $^^--H%00	+I{6<<7PQQQ 1 .Qs$   B C(#C$$5C(C&C(&C(r   )r   r   rs   r   r   cancel_and_waitrz   r   )r9   	run_ataskr   r   r   r   r   s   f   @@@r'   r   $_AMDClassifier._classify_user_speech?  s    R 		 		< 	<B 
v	&	R 	R 
'	R&	5"nn I Id%(%6%<t%C$K$K$ML!(--i888#//S5F0GH		I 9 - $)))444 %y$)))444 %sl   9D C2 C	CC	AC2 C)C2 C	C2 	C2 D(C+)	D2DDDDc                   < V ^8  d   QhRR/# r   r   )r1   r2   s   "r'   r3   rg     s      T r&   c                  "   V P                   '       d   R # V P                  P                  4        V P                  P                  '       g   V P                  P                  4        V P                  e"   V P                  P                  4        R V n        V P                  e)   V P                  P                  4        R V n        R V n	        V P                  e"   V P                  P                  4        R V n
        V P                  e)   \        P                  ! V P                  4      G R j  xL
  RV n         RV n        R #  L5i)NTF)r   r|   r   rs   rT   r   ru   r   rv   rw   rx   rt   r   r   r   r8   s   &r'   r   _AMDClassifier.close  s     <<<!~~$$$NN  "  ,!!((*$(D!*&&("&D*.D'((4))002,0D)*%%d&9&9::: ;s   A
ECE,E-Ec                    < V ^8  d   QhRS[ /# r,   r/   )r1   r2   s   "r'   r3   rg     s       r&   c                    V P                   # r6   r   r8   s   &r'   rU   _AMDClassifier.started  s    }}r&   c                    < V ^8  d   QhRS[ /# r,   r/   )r1   r2   s   "r'   r3   rg     s       r&   c                    V P                   # r6   )r   r8   s   &r'   rT   _AMDClassifier.closed  s    ||r&   c                    < V ^8  d   QhRS[ /# r,   r   )r1   r2   s   "r'   r3   rg     s     
 
 
r&   c                    V P                   e;   V P                  ;'       g    \        P                  ! 4       V P                   ,
          # R# )Nr   )r   r   r   r8   s   &r'   rB   _AMDClassifier.speech_duration  sC     &&2 ""11diikT5L5LL	
 	
r&   )rt   r   rx   r   r   rm   rl   rs   r}   r   rn   ro   ru   r~   rv   rw   rq   r   r   r   rp   r   r|   ry   )r]   )!r   r   r   r   HUMAN_SPEECH_THRESHOLDHUMAN_SILENCE_THRESHOLDMACHINE_SILENCE_THRESHOLDNO_SPEECH_THRESHOLDTIMEOUT
AMD_PROMPTrk   r   r   r[   r   r   r   r   r   r   r   r   r   r   r   r   rH   rU   rT   rB   r%   rJ   __classcell__)r   r2   s   @@r'   rO   rO   i   s7     )?)@+D%8  &" &"P 
 
* 
. 
. -4 -4^     6" -6"+(,	     # 2  D  D  DD 
 6"P5 #P5d 2     
 
 
r&   rO   r*   ).rz   rY   r   collections.abcr   enumr   typingr   r   pydanticr   llm.chat_contextr   r	   llm.llmr
   llm.tool_contextr   r   r   	llm.utilsr   logr   typesr   r   utilsr   r   r   r   r  r  r	  r
  r  r   r   rG   r   r)   r  r[   rO   r   r&   r'   <module>r     s       $    8  @ @ .  * @ @    
 #t 
 
,
@P
\'*:";< P
r&   