def parse_start_end_tags(
    text: str,
    tags: Sequence[StartEndTags],
    current_tag: Optional[StartEndTags],
    current_tag_index: int,
) -> Tuple[Optional[StartEndTags], int]:
    """Parses the given text to identify a pair of start/end tags.

    If a start tag was previously found (i.e. current_tags is valid), wait for
    the corresponding end tag.  Otherwise, wait for a start tag.

    This function will return the index in the text that we should start parsing
    in the next call and the current or new tags.

    Parameters:
    - text (str): The text to be parsed.
    - tags (Sequence[StartEndTags]): List of tuples containing start and end tags.
    - current_tags (Optional[StartEndTags]): The currently active tags, if any.
    - current_tags_index (int): The current index in the text.

    Returns:
    Tuple[Optional[StartEndTags], int]: A tuple containing None or the current
    tag and the index of the text.

    """
    # If we are already inside a tag, check if the end tag is in the text.
    if current_tag:
        _, end_tag = current_tag
        if end_tag in text[current_tag_index:]:
            return (None, len(text))
        return (current_tag, current_tag_index)

    # Check if any start tag appears in the text
    for start_tag, end_tag in tags:
        start_tag_count = text[current_tag_index:].count(start_tag)
        end_tag_count = text[current_tag_index:].count(end_tag)
        if start_tag_count == 0 and end_tag_count == 0:
            return (None, current_tag_index)
        elif start_tag_count > end_tag_count:
            return ((start_tag, end_tag), len(text))
        elif start_tag_count == end_tag_count:
            return (None, len(text))

    return (None, current_tag_index)
Parses the given text to identify a pair of start/end tags.

    If a start tag was previously found (i.e. current_tags is valid), wait for
    the corresponding end tag.  Otherwise, wait for a start tag.

    This function will return the index in the text that we should start parsing
    in the next call and the current or new tags.

    Parameters:
    - text (str): The text to be parsed.
    - tags (Sequence[StartEndTags]): List of tuples containing start and end tags.
    - current_tags (Optional[StartEndTags]): The currently active tags, if any.
    - current_tags_index (int): The current index in the text.

    Returns:
    Tuple[Optional[StartEndTags], int]: A tuple containing None or the current
    tag and the index of the text.

    
src/pipecat/utils/string.py