
    $j,                        d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZ  ed          Ze	 G d d	                      ZddZdS )z-Pagination utilities for MCP list operations.    )annotationsN)Sequence)	dataclass)TypeVarTc                  >    e Zd ZU dZded<   d
dZedd            Zd	S )CursorStatezInternal representation of pagination cursor state.

    The cursor encodes the offset into the result set. This is opaque to clients
    per the MCP spec - they should not parse or modify cursors.
    intoffsetreturnstrc                    t          j        d| j        i          }t          j        |                                                                          S )z(Encode cursor state to an opaque string.o)jsondumpsr   base64urlsafe_b64encodeencodedecode)selfdatas     d/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/fastmcp/utilities/pagination.pyr   zCursorState.encode   s=    z3,--'66==???    cursorc                L   	 t          j        t          j        |                                                                                    } | |d                   S # t           j        t          t          t          t          j        f$ r}t          d|           |d}~ww xY w)z}Decode cursor from an opaque string.

        Raises:
            ValueError: If the cursor is invalid or malformed.
        r   r   zInvalid cursor: N)r   loadsr   urlsafe_b64decoder   r   JSONDecodeErrorKeyError
ValueError	TypeErrorbinasciiError)clsr   r   es       r   r   zCursorState.decode   s    
	A:f6v}}GGNNPPQQD3d3i(((( N
 	A 	A 	A 88899q@	As   AA -B#BB#N)r   r   )r   r   r   r	   )__name__
__module____qualname____doc____annotations__r   classmethodr    r   r   r	   r	      si           KKK@ @ @ @
 A A A [A A Ar   r	   itemsSequence[T]r   
str | None	page_sizer
   r   tuple[list[T], str | None]c                    d}|r!t                               |          }|j        }||z   }t          | ||                   }d}|t	          |           k     r"t          |                                          }||fS )ay  Paginate a sequence of items.

    Args:
        items: The full sequence to paginate.
        cursor: Optional cursor from a previous request. None for first page.
        page_size: Maximum number of items per page.

    Returns:
        Tuple of (page_items, next_cursor). next_cursor is None if no more pages.

    Raises:
        ValueError: If the cursor is invalid.
    r   Nr   )r	   r   r   listlenr   )r.   r   r1   r   stateendpagenext_cursors           r   paginate_sequencer:   2   s    $ F ""6**
9
CfSj!""DK
SZZ!---4466r   )r.   r/   r   r0   r1   r
   r   r2   )r*   
__future__r   r   r#   r   collections.abcr   dataclassesr   typingr   r   r	   r:   r-   r   r   <module>r?      s    3 3 " " " " " "    $ $ $ $ $ $ ! ! ! ! ! !      GCLL A A A A A A A AD     r   