
    qi2                     ^    d Z ddlZddlmZmZmZmZ ddlmZm	Z	 ddl
mZmZ  G d de      Zy)z:Idle frame processor for timeout-based callback execution.    N)	AwaitableCallableListOptional)Frame
StartFrame)FrameDirectionFrameProcessorc            	       z     e Zd ZdZddded ged   f   dedeee	      f fdZ
ded	ef fd
Zd Zd Zd Z xZS )IdleFrameProcessora  Monitors frame activity and triggers callbacks on timeout.

    This processor waits to receive any frame or specific frame types within a
    given timeout period. If the timeout is reached before receiving the expected
    frames, the provided callback will be executed.
    N)typescallbacktimeoutr   c                d    t        |   di | || _        || _        |xs g | _        d| _        y)a  Initialize the idle frame processor.

        Args:
            callback: Async callback function to execute on timeout. Receives
                this processor instance as an argument.
            timeout: Timeout duration in seconds before triggering the callback.
            types: Optional list of frame types to monitor. If None, monitors
                all frames.
            **kwargs: Additional arguments passed to parent class.
        N )super__init__	_callback_timeout_types
_idle_task)selfr   r   r   kwargs	__class__s        Y/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/processors/idle_frame_processor.pyr   zIdleFrameProcessor.__init__   s5    $ 	"6"!kr    frame	directionc                 x  K   t         |   ||       d{    t        |t              r| j	                          | j                  ||       d{    | j                  s| j                  j                          y| j                  D ])  }t        ||      s| j                  j                          + y7 7 fw)zProcess incoming frames and manage idle timeout monitoring.

        Args:
            frame: The frame to process.
            direction: The direction of frame flow in the pipeline.
        N)	r   process_frame
isinstancer   _create_idle_task
push_framer   _idle_eventset)r   r   r   tr   s       r   r    z IdleFrameProcessor.process_frame1   s      g#E9555eZ(""$ooeY/// {{  "[[ +eQ'$$((*+ 	6
 	0s(   B:B69B:B8AB:B:8B:c                 p   K   | j                   r$| j                  | j                          d{    yy7 w)z,Clean up resources and cancel pending tasks.N)r   cancel_taskr   s    r   cleanupzIdleFrameProcessor.cleanupH   s-     ??""4??333 3s   +646c                     | j                   s>t        j                         | _        | j	                  | j                               | _         yy)z*Create and start the idle monitoring task.N)r   asyncioEventr$   create_task_idle_task_handlerr)   s    r   r"   z$IdleFrameProcessor._create_idle_taskM   s7    &}}D"..t/F/F/HIDO r   c                 n  K   	 	 t        j                  | j                  j                         | j                         d{    | j                  j                          ]7 # t         j
                  $ r | j                  |        d{  7   Y Lw xY w# | j                  j                          w xY ww)z6Handle idle timeout monitoring and callback execution.)r   N)r,   wait_forr$   waitr   TimeoutErrorr   clearr)   s    r   r/   z%IdleFrameProcessor._idle_task_handlerS   s     )&&t'7'7'<'<'>VVV   &&( V'' +nnT***+   &&(sR   B5<A# A!A# B5!A# #'B
BBB BB B22B5)__name__
__module____qualname____doc__r   r   floatr   r   typer   r   r	   r    r*   r"   r/   __classcell__)r   s   @r   r   r      sn     '+ 019T?BC 	
 T
#2+ +> +.4
J)r   r   )r8   r,   typingr   r   r   r   pipecat.frames.framesr   r   "pipecat.processors.frame_processorr	   r
   r   r   r   r   <module>r?      s(    A  6 6 3 MK) K)r   