o
    i                     @  sd   d dl mZ d dlZd dlmZ ddlmZ dddddZddddddZddddddZ	dS )    )annotationsN)rtc   )ATTRIBUTE_AGENT_NAME
agent_nameroomrtc.Roomr   
str | Nonereturnrtc.RemoteParticipantc             
     s  |   s	tdt d fdddfd	d
}dfdd}dfdd}| d| | d| | d| z;| j D ]}|r`|  W | d| | d| | d| S qCI dH W | d| | d| | d| S | d| | d| | d| w )a  
    Wait for an agent participant to join the room.

    Args:
        room: The room to wait for the agent in.
        agent_name: If provided, waits for an agent with matching lk.agent.name attribute.
                   If None, returns the first agent participant found.

    Returns:
        The agent participant.

    Raises:
        RuntimeError: If the room is not connected.
    room is not connectedpr   r   boolc                   s.   | j tjjkr	dS  d u rdS | jt kS )NFT)kindr   ParticipantKindPARTICIPANT_KIND_AGENT
attributesgetr   r   r    b/var/www/html/livekit_bhavya/venv/lib/python3.10/site-packages/livekit/agents/utils/participant.pymatches_agent"   s
   z%wait_for_agent.<locals>.matches_agentNonec                   s&   | r   s |  d S d S d S N)done
set_resultr   futr   r   r   on_participant_connected)   s   z0wait_for_agent.<locals>.on_participant_connectedchanged	list[str]rtc.Participantc                   s6   t |tjr|r  s | d S d S d S d S r   )
isinstancer   RemoteParticipantr   r   )r    r   r   r   r   on_attributes_changed-   s   z-wait_for_agent.<locals>.on_attributes_changedstateintc                   .   | t jjkr  s td d S d S d S )Nz5room disconnected while waiting for agent participantr   ConnectionStateCONN_DISCONNECTEDr   set_exceptionRuntimeErrorr&   r   r   r   on_connection_state_changed1      z3wait_for_agent.<locals>.on_connection_state_changedparticipant_connectedparticipant_attributes_changedconnection_state_changedNr   r   r   r   r   r   r   r   )r    r!   r   r"   r   r   r&   r'   r   r   )isconnectedr-   asyncioFutureonremote_participantsvaluesoff)r   r   r   r%   r0   r   r   )r   r   r   r   wait_for_agent
   s4   
r?   )identityr   r@   r   Jlist[rtc.ParticipantKind.ValueType] | rtc.ParticipantKind.ValueType | Nonec             
     s   |   s	tdtjtj   dfddd fd	d
}d fdd}| d| | d| z$| j D ]}||  	 rE nq9 I dH W | 
d| | 
d| S | 
d| | 
d| w )z
    Returns a participant that matches the given identity. If identity is None, the first
    participant that joins the room will be returned.
    If the participant has already joined, the function will return immediately.
    r   r   r   r   r   c                   s*    d u rdS t  tr| j v S | j kS NT)r#   listr   r   r   r   r   
kind_matchV   s
   


z(wait_for_participant.<locals>.kind_matchr   c                   s<   d u s	| j kr| r  s |  d S d S d S d S r   )r@   r   r   r   r   r@   rE   r   r   _on_participant_connected_   s
   z7wait_for_participant.<locals>._on_participant_connectedr&   r'   c                   r(   )Nz/room disconnected while waiting for participantr)   r.   r/   r   r   _on_connection_state_changedd   r1   z:wait_for_participant.<locals>._on_connection_state_changedr2   r4   Nr5   r6   r7   )r8   r-   r9   r:   r   r$   r;   r<   r=   r   r>   )r   r@   r   rG   rH   r   r   r   r@   r   rE   r   wait_for_participantF   s(   	
rJ   >list[rtc.TrackKind.ValueType] | rtc.TrackKind.ValueType | Nonertc.RemoteTrackPublicationc             
     s   |   s	tdtjtj   dfddd fdd}d fdd}| d| | d| z-| j D ]}|j	 D ]}|||  
 rM nq@q9 I dH W | d| | d| S | d| | d| w )zReturns a remote track matching the given identity and kind.
    If identity is None, the first track matching the kind will be returned.
    If the track has already been published, the function will return immediately.
    r   krtc.TrackKind.ValueTyper   r   c                   s&    d u rdS t  tr|  v S |  kS rB   )r#   rC   )rM   rD   r   r   rE      s
   
z.wait_for_track_publication.<locals>.kind_matchpublicationrL   participantr   r   c                   s>      rd S d u s|jkr| jr |  d S d S d S r   )r   r@   r   r   )rO   rP   rF   r   r   _on_track_published   s
   z7wait_for_track_publication.<locals>._on_track_publishedr&   r'   c                   r(   )Nz5room disconnected while waiting for track publicationr)   r.   r/   r   r   rH      r1   z@wait_for_track_publication.<locals>._on_connection_state_changedtrack_publishedr4   N)rM   rN   r   r   )rO   rL   rP   r   r   r   r7   )r8   r-   r9   r:   r   RemoteTrackPublicationr;   r<   r=   track_publicationsr   r>   )r   r@   r   rQ   rH   r   rO   r   rI   r   wait_for_track_publicationw   s,   
		

rU   )r   r	   r   r
   r   r   )r   r	   r@   r
   r   rA   r   r   )r   r	   r@   r
   r   rK   r   rL   )

__future__r   r9   livekitr   typesr   r?   rJ   rU   r   r   r   r   <module>   s    ?4