Ë
    ÅÏÉi,  ã                  ó–   — 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y)z-Pagination utilities for MCP list operations.é    )ÚannotationsN)ÚSequence)Ú	dataclass)ÚTypeVarÚTc                  ó6   — e Zd ZU dZded<   dd„Zedd„«       Zy)	ÚCursorStatezÊInternal 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Úoffsetc                ó¦   — t        j                  d| j                  i«      }t        j                  |j                  «       «      j                  «       S )z(Encode cursor state to an opaque string.Úo)ÚjsonÚdumpsr   Úbase64Úurlsafe_b64encodeÚencodeÚdecode)ÚselfÚdatas     úl/Users/bowang/.openclaw/workspace/ChatDev/.venv/lib/python3.12/site-packages/fastmcp/utilities/pagination.pyr   zCursorState.encode   s9   € äz‰z˜3 §¡Ð,Ó-ˆÜ×'Ñ'¨¯©«Ó6×=Ñ=Ó?Ð?ó    c                ó6  — 	 t        j                  t        j                  |j	                  «       «      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)ÚclsÚcursorr   Úes       r   r   zCursorState.decode   sƒ   € ð
	AÜ—:‘:œf×6Ñ6°v·}±}³ÓG×NÑNÓPÓQˆDÙ˜d 3™iÔ(Ð(øä× Ñ ÜÜÜÜN‰Nð
ò 	Aô Ð/°¨xÐ8Ó9¸qÐ@ûð	Aús   ‚AA Á2BÂBÂBN)ÚreturnÚstr)r#   r&   r%   r	   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   Úclassmethodr   © r   r   r	   r	      s*   … ñð ƒKó@ð
 òAó ñAr   r	   c                óÎ   — d}|r!t         j                  |«      }|j                  }||z   }t        | || «      }d}|t	        | «      k  rt        |¬«      j                  «       }||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   )Úitemsr#   Ú	page_sizer   ÚstateÚendÚpageÚnext_cursors           r   Úpaginate_sequencer7   2   so   € ð$ €FÙÜ×"Ñ" 6Ó*ˆØ—‘ˆà
9Ñ
€CÜf˜SÐ!Ó"€Dà€KØ
ŒS‹ZÒÜ!¨Ô-×4Ñ4Ó6ˆàÐÐr   )r1   zSequence[T]r#   z
str | Noner2   r
   r%   ztuple[list[T], str | None])r*   Ú
__future__r   r   r    r   Úcollections.abcr   Údataclassesr   Útypingr   r   r	   r7   r-   r   r   Ú<module>r<      sl   ðÙ 3å "ã Û Û Ý $Ý !Ý áˆCƒL€ð ÷Að Aó ðAðDØðàðð ðð  ô	r   