📄 shares.cpython-312.pyc
/home/palash/git/local-ai/server/features/__pycache__/shares.cpython-312.pyc
Language: pyc • Lines: 240
�

(�~j���p�dZddlZddlZddlZddlZddlZddlmZd�Zd�Z	dZ
d�Zd�Zd	�Z
d
�Zd�Zd�Zy)
aaPublic message sharing: create, fetch and revoke single-message shares.

A share is a capability URL: whoever knows the unguessable token can read the
message without logging in. The message content is snapshotted at share time so
later edits to or deletion of the session never change or break the share.

Storage is a simple JSON file (``SHARES_FILE``) shaped as::

    {
        "<token>": {
            "session_id": "...",
            "msg_index": 3,
            "owner": "alice",
            "created": 1234567890.0,
            "message": { "role": "assistant", "content": "...", ... }
        }
    }
�N)�Mc�F�tj5tjj�ddd�tj}|rt
jj|�sy	t|d��5}tj|�}ddd�tj5xsij�D]\}}|tj|<�	ddd�y#1swY��xYw#1swY�_xYw#ttjf$rYywxYw#1swYyxYw)zBLoad share records from disk into the shared ``shares`` container.N�utf-8��encoding)r�
_data_lock�shares�clear�SHARES_FILE�os�path�exists�open�json�load�OSError�JSONDecodeError�items)r
�f�data�token�recs     �3/home/palash/git/local-ai/server/features/shares.py�load_sharesrs���	
����	��������=�=�D��r�w�w�~�~�d�+���
�$��
)�	 �Q��9�9�Q�<�D�	 �
���"��:�2�,�,�.�	"�J�E�3�!�A�H�H�U�O�	"�"�"����	 �	 ���T�)�)�*�����"�"�sA�C �+
C8�8C,�C8�&0D� C)�,C5�1C8�8D�D�D c��tj5tjt	tj
��}ddd�tj}|sytjtjj|�d��t|dd��5}tj|d��ddd�y#1swY��xYw#1swYyxYw)	z3Persist the in-memory ``shares`` container to disk.NT)�exist_ok�wrr�)�indent)rr�copy�deepcopy�dictr	rr�makedirsr
�dirnamerr�dump)rr
rs   r�save_sharesr&.s���	
���-��}�}�T�!�(�(�^�,��-��=�=�D����K�K�������%��5�	
�d�C�'�	*�%�a��	�	�$��!�$�%�%�
-�-��%�%�s�-B=�C	�=C�	C)
�role�content�
_image_url�_image_model�_gen_prompt�
_reasoning�_tools_used�_search_details�_elapsed_ms�
_timestampc�`�i}tD]"}||vs�tj||�||<�$|S)N)�_SNAPSHOT_KEYSr r!)�msg�snap�keys   r�_snapshot_messager6Js:��
�D��0���#�:��
�
�c�#�h�/�D��I�0��K�c�>�tj}|r|�d|��Sd|��S)aIPublic URL for a share.

    Uses ``SHARE_BASE_URL`` (a portless origin, see config) when set, otherwise
    falls back to a site-relative path. A ported origin like ``:3001`` is
    deliberately avoided because WhatsApp stops auto-linking URLs at the ":" of
    a port, which truncates every share link to a dead short URL.
    z/s/)r�SHARE_BASE_URL)r�bases  r�
_share_urlr;Rs-��
���D�"&�d�V�3�u�g��9�c�%��M�9r7c��	t|�}tj5tj
j
|�}|std��|j
dd�|k7rtd��tjj
|�}|r|dks|t|�k\rtd��||}|j
d�d	k7rtd
��	ddd�tj�j}tj5|||tj�t�d�tj|<ddd�t�|t!|�fS#ttf$rtd��wxYw#1swY��xYw#1swY�JxYw)
aCreate a share for a single assistant message.

    Validates session ownership and that the target message is an assistant
    (bot) message, then snapshots it. Returns ``(token, url)``.

    Raises ``ValueError`` with a user-facing message on any validation failure.
    zInvalid message indexzSession not found�user_id�zNot your sessionrzMessage not foundr'�	assistantz%Only assistant messages can be sharedN)�
session_id�	msg_index�owner�created�message)�int�	TypeError�
ValueErrorrr�
sessions_meta�get�sessions�len�uuid�uuid4�hex�timer6r	r&r;)�userr@rA�meta�msgsr3rs       r�create_sharerS^sa��2��	�N�	�
���F����"�"�:�.����0�1�1��8�8�I�r�"�d�*��/�0�0��z�z�~�~�j�)���y�1�}�	�S��Y�(>��0�1�1��9�o���7�7�6�?�k�)��D�E�E�*�F�
�J�J�L���E�	
���
�$�"���y�y�{�(��-�
������
��M��*�U�#�#�#��5
�z�"�2��0�1�1�2��F�F��
�
�s#�E�B0E6�5F�E3�6E?�Fc��|sytj5tjj|�cddd�S#1swYyxYw)zDReturn the share record for a token, or ``None`` if missing/revoked.N)rrr	rI)rs r�	get_sharerU�s4����	
���#��x�x�|�|�E�"�#�#�#�s	�=�Ac��tj5tjj|�}|r|jd�|k7r
	ddd�ytj|=ddd�t	�y#1swYt	�yxYw)z�Remove a share. Only the owner may revoke.

    Returns ``True`` on success, ``False`` if the share does not exist or the
    user is not its owner.
    rBNFT)rrr	rIr&)rrPrs   r�revoke_sharerW�sn��
�����h�h�l�l�5�!���c�g�g�g�&�$�.����
�H�H�U�O�	�
�M��
�
�M��s�7A5�A5�5Bc��g}tj5tjj�D]�\}}|j	d�|k7r�|j	di�j	dd�}d}t|t�r|dd}nVt|t�rF|D]A}t|t�s�|j	d�dk(s�)|j	d�xsddd}n|j|t|�|j	d	�|j	d
�|j	d�|d����	ddd�t|d
�d��S#1swY�xYw)z>Return the caller's share records (with a plain-text preview).rBrDr(r>N�x�type�textr@rArC)r�urlr@rArC�previewc�,�|jd�xsdS)NrCr)rI)�ss r�<lambda>zlist_shares.<locals>.<lambda>�s��Q�U�U�9�%5�%:��r7T)r5�reverse)rrr	rrI�
isinstance�str�listr"�appendr;�sorted)rP�outrrr(r]�parts       r�list_sharesri�s3��
�C�	
�����(�(�.�.�*�	�J�E�3��w�w�w��4�'���g�g�i��,�0�0��B�?�G��G��'�3�'�!�$�3�-���G�T�*�#��D�!�$��-�$�(�(�6�2B�f�2L�#'�8�8�F�#3�#9�r�4�C�"@����
�J�J�"�%�e�,�"%�'�'�,�"7�!$����!5�"�w�w�y�1�&�
�	
�	��.�#�:�D�I�I�/��s�BE�-E�A+E�E)�__doc__r rrrOrL�server.features.staterrr&r2r6r;rSrUrWri�r7r�<module>rmsO���&��	���#�"�"	%����	:�$$�N#��Jr7