o
    i                     @   sT   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 deZdS )	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                       sB   e Zd ZdZejfdef fddZdedef fddZ  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                    s,   t    || _|| _|| _|| _g | _dS )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__ X/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/processors/aggregators/gated.pyr      s   

zGatedAggregator.__init__framec                    s  t  ||I dH  t|tr| ||I dH  dS || jkr+| ||I dH  dS | j}| jr9| | | _n| || _|| jkrV| jrIdnd}t	
d| d|  | jrx| ||I dH  | jD ]\}}| ||I dH  qeg | _dS | j||f dS )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   r   r   r   5   s*   



zGatedAggregator.process_frame)
__name__
__module____qualname____doc__r   
DOWNSTREAMr   r   r   __classcell__r   r   r   r   r	      s    r	   N)r)   typingr   r   logurur   pipecat.frames.framesr   r   "pipecat.processors.frame_processorr   r   r	   r   r   r   r   <module>   s   