
    qi                     Z    d Z ddlmZmZ ddlmZ ddlmZmZ ddl	m
Z
mZ  G d de      Zy)	zGated frame aggregator for conditional frame accumulation.

This module provides a gated aggregator that accumulates frames based on
custom gate open/close functions, allowing for conditional frame buffering
and release in frame processing pipelines.
    )ListTuple)logger)FrameSystemFrame)FrameDirectionFrameProcessorc                   T     e Zd ZdZej
                  fdef fdZdedef fdZ xZ	S )GatedAggregatorah  Accumulate frames, with custom functions to start and stop accumulation.

    Yields gate-opening frame before any accumulated frames, then ensuing frames
    until and not including the gate-closed frame. The aggregator maintains an
    internal gate state that controls whether frames are passed through immediately
    or accumulated for later release.
    	directionc                 h    t         |           || _        || _        || _        || _        g | _        y)a{  Initialize the gated aggregator.

        Args:
            gate_open_fn: Function that returns True when a frame should open the gate.
            gate_close_fn: Function that returns True when a frame should close the gate.
            start_open: Whether the gate should start in the open state.
            direction: The frame direction this aggregator operates on.
        N)super__init___gate_open_fn_gate_close_fn
_gate_open
_direction_accumulator)selfgate_open_fngate_close_fn
start_openr   	__class__s        V/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/processors/aggregators/gated.pyr   zGatedAggregator.__init__   s6     	)+$#@B    framec                   K   t         |   ||       d{    t        |t              r| j	                  ||       d{    y|| j
                  k7  r| j	                  ||       d{    y| j                  }| j                  r| j                  |       | _        n| j                  |      | _        || j                  k7  r+| j                  rdnd}t        j                  d| d|        | j                  rP| j	                  ||       d{    | j                  D ]  \  }}| j	                  ||       d{    ! g | _        y| j                  j                  ||f       y7 T7 -7 7 a7 7w)zProcess incoming frames with gated accumulation logic.

        Args:
            frame: The frame to process.
            direction: The direction of the frame flow.
        NopenclosedzGate is now z because of )r   process_frame
isinstancer   
push_framer   r   r   r   r   debugr   append)r   r   r   	old_statestatefdr   s          r   r    zGatedAggregator.process_frame5   sS     g#E9555 e[)//%333 '//%333OO	??"&"5"5e"<<DO"007DO'"ooF8ELL<wl5'BC??//%333)) ,1ooa+++, "D$$eY%789 	6 4
 4 4+sX   E8E+)E8E.)E8+E1,B&E8E4+E8>E6?-E8.E81E84E86E8)
__name__
__module____qualname____doc__r   
DOWNSTREAMr   r   r    __classcell__)r   s   @r   r   r      s<     %3$=$=C
 "C,#9 #9> #9 #9r   r   N)r,   typingr   r   logurur   pipecat.frames.framesr   r   "pipecat.processors.frame_processorr   r	   r    r   r   <module>r4      s&      4 MB9n B9r   