Team Ai
Apppublic

Yash030/claude-code-proxy

sourceHugging Faceupdated 5mo agoView on Hugging Face
2likes
session.cpython-314.pyc84 linesDownload Raw Back to __pycache__
1+
2�Ej�,��n�Rt^RIt^RIt^RIt^RIt^RIt^RIHtHt^RIH	t	^RI3Ht!RR4tR#)z�4Session Store for Messaging Platforms5 6Provides persistent storage for mapping platform messages to Claude CLI session IDs7and message trees for conversation continuation.8N)�UTC�datetime)�Any)�loggerc�a�]tRt^toRtR-RR/V3RlRllltV3RlRltV3RlR	ltV3R9lRltV3RlR
lt	V3RlRlt10V3RlRltV3RlRltV3RlRlt
V3RlRltV3RlRltV3RlRltV3RlRltV3RlRltV3R lR!ltV3R"lR#ltV3R$lR%ltV3R&lR'ltV3R(lR)ltV3R*lR+ltR,tVtR#).�SessionStoreu�11Persistent storage for message ↔ Claude session mappings and message trees.12 13Uses a JSON file for storage with thread-safe operations.14Platform-agnostic: works with any messaging platform.15�message_log_capNc�4<�V^8�dQhRS[RS[R,/#)��storage_pathrN)�str�int)�format�
__classdict__s"��<C:\Users\yashw\.claude\claude-code-free\messaging\session.py�__annotate__�SessionStore.__annotate__s#��������t��	�c���Wn\P!4Vn/Vn/Vn/Vn/VnRVnRVn	RVn16W nVP4R#)FNg�?)
r�	threading�Lock�_lock�_trees�
_node_to_tree�_message_log�_message_log_ids�_dirty�_save_timer�_save_debounce_secs�_message_log_cap�_load)�selfrrs&&$r�__init__�SessionStore.__init__s`��)���^�^�%��17�')���-/���>@���57������37���#&�� �,;���18�19�rc�,<�V^8�dQhRS[RS[RS[/#�r20�platform�chat_id�return�r)rrs"�rrr/s"���'�'�s�'�S�'�S�'rc��VRV2#)�:�)r!r&r's&&&r�_make_chat_key�SessionStore._make_chat_key/s����1�W�I�&�&rc�<�V^8�dQhRR/#�r21r(Nr,)rrs"�rrr2s���1:�1:�t�1:rc��\PPVP4'gR#\	VPRR7;_uu_4p\22P!V4pRRR4XPR/4VnVPR/4Vn	VPR/4;'g/p\V\4'Ed[/Vn/Vn
VP4EF7wrE\V\4'd\V\ 4'gK4.p\#4pVF�p\V\4'gKVPR4p	V	fK2\V	4p23W�9dKEVP%V244TP'RT25R\VPR4;'gR	4R26\VPR274;'gR	4R\VPR4;'gR	4/4K�	W`PV&WpPV&EK:	\(P*!R\-VP4R
\/RVPP1444RVP24R# +'giEL2;i \2d%p\(P4!RT24Rp?R#Rp?ii;i)z"Load sessions and trees from disk.N�utf-8��encoding�trees�node_to_tree�message_log�28message_id�ts��	direction�kindzLoaded z trees and c3�8"�TFp\V4x�K	R#5i)N)�len)�.0�vs& r�	<genexpr>�%SessionStore._load.<locals>.<genexpr>`s���B�'A�!�s�1�v�v�'A�s�z msg_ids from zFailed to load sessions: )�os�path�existsr�open�json�load�getrr�29isinstance�dictrr�itemsr�list�set�add�appendr�infor>�sum�values�	Exception�error)r!�f�data�raw_log�chat_keyrL�cleaned�seen�it�mid�mid_s�es&           rr �SessionStore._load2s<���w�w�~�~�d�/�/�0�0��,	:��d�'�'�'�:�:�a��y�y��|��;��(�(�7�B�/�D�K�!%���.�"�!=�D���h�h�}�b�1�7�7�R�G��'�4�(�(�$&��!�(*��%�'.�}�}��O�H�%�h��4�4�J�u�d�<S�<S� �46�G�%(�U�D�#��)�"�d�3�3�$� �f�f�\�2���;�$� #�C��� �=�$��������� ,�e� $�c�"�&�&��,�*<�*<�"�&=� +�S�����1D�1J�1J��-K� &��B�F�F�6�N�,@�,@�b�(A�	��$�$3:�%�%�h�/�6:�)�)�(�3�1(7�4
�K�K��#�d�k�k�*�+�;��B�t�'8�'8�'?�'?�'A�B�B�C�>�RV�Rc�Rc�Qd�f�
�M;�:�:��T�	:��L�L�4�Q�C�8�9�9��	:�sJ�J&�J�)AJ&�8C:J&�3#J&�#J&�;BJ&�J#	�	J&�&K�1K�Kc� <�V^8�dQhRS[/#�r30r(�rK)rrs"�rrres���31�32�4�33rc
���R\VP4R\VP4RVPP	4UUu/uFwrV\V4bK	upp/#uuppi)zFSnapshot current state for serialization. Caller must hold self._lock.r5r6r7)rKrrrrLrM)r!�kr@s&  r�	_snapshot�SessionStore._snapshotesa��
�T�$�+�+�&��D��!3�!3�4��4�3D�3D�3J�3J�3L�M�3L�4�1�A�t�A�w�J�3L�M�34�	35��Ns�	A%c�$<�V^8�dQhRS[RR/#)r36rWr(Nrc)rrs"�rrrms��������rc���\PPVP4p\PP	V4;'gRp\37P!VRRR7wrE\P!VRRR7;_uu_4p\P!W^R7VP4\P!VP44R	R	R	4\P!WR4R	# +'giL(;i \dW\P !\"4;_uu_4\P$!T4R	R	R	4h +'gih;ii;i)38zNAtomically write data dict to disk. Must be called WITHOUT holding self._lock.�.z39.sessions.z	.tmp.json)�dir�prefix�suffix�wr2r3)�indentN)rCrD�abspathr�dirname�tempfile�mkstemp�fdopenrG�dump�flush�fsync�fileno�replace�
BaseException�40contextlib�suppress�OSError�unlink)r!rW�41abs_target�dir_name�fd�tmp_pathrVs&&     r�_write_data�SessionStore._write_datams����W�W�_�_�T�%6�%6�7�42��7�7�?�?�:�.�5�5�#���'�'���k�43���		����2�s�W�5�5���	�	�$�!�,����	��������$�6�
�J�J�x�,�	6�5��44�	��$�$�W�-�-��	�	�(�#�.��.�-���	�s=�/!D�A
C=�D�=D
	�D�-E1�=E�	E1�E-�(	E1c�<�V^8�dQhRR/#r0r,)rrs"�rrrs���45!�46!��47!rc�(�RVnVPe"VPP4RVn\P!VP48VP4VnRVPnVPP4R#)z7Schedule a debounced save. Caller must hold self._lock.TN)	rr�cancelr�Timerr�_save_from_timer�daemon�start�r!s&r�_schedule_save�SessionStore._schedule_savess��������'����#�#�%�#�D��$�?�?��$�$�d�&;�&;�49���#'���������� rc�<�V^8�dQhRR/#r0r,)rrs"�rrr�s���#�#�$�#rc���VP;_uu_4VP'gRVnRRR4R#VP4pRVnRVnRRR4VP	X4R# +'giL$;i \50dap\P!RT24TP;_uu_4RTnRRR4Rp?R# +'giRp?R#;iRp?ii;i)z4Timer callback: save if dirty. Runs in timer thread.NF�Failed to save sessions: T)rrrrfr�rTrrU�r!�snapshotr_s&  rr��SessionStore._save_from_timer�s���
�Z�Z�Z��;�;�;�#'�� ���Z��~�~�'�H��D�K�#�D��
�	#����X�&��Z���	#��L�L�4�Q�C�8�9�����"���������	#�sF�A4�A4�!B�4B	�C2�-C-�?C�C-�C*� C-�*C-�-C2c� <�V^8�dQhRS[/#rbrc)rrs"�rrr�s��� � �T� rc��VPe"VPP4RVnRVnVP4#)z�Cancel pending timer and snapshot current state. Caller must hold self._lock.51Returns snapshot dict; caller must call _write_data(snapshot) outside the lock.NF)rr�rrfr�s&r�_flush_save�SessionStore._flush_save�s>�����'����#�#�%�#�D������~�~��rc�<�V^8�dQhRR/#r0r,)rrs"�rrr�s���	#�	#�D�	#rc��VP;_uu_4VP4pRRR4VPX4R# +'giL$;i \dap\P52!RT24TP;_uu_4RTnRRR4Rp?R# +'giRp?R#;iRp?ii;i)zHFlush any pending debounced save. Call on shutdown to avoid losing data.Nr�T)rr�r�rTrrUrr�s&  r�flush_pending_save�SessionStore.flush_pending_save�s{��
�Z�Z�Z��'�'�)�H��	#����X�&��Z���	#��L�L�4�Q�C�8�9�����"���������	#�s@�A�A�A	�C� -B;�
B#�B;�#B8�.B;�8B;�;Cc�<<�V^8�dQhRS[RS[RS[RS[RS[RR/#)r53r&r'r8r;r<r(Nr))rrs"�rrr�sG���&"�&"��&"��&"��	&"�54�&"��
&"�55�&"rc���VfR#VP\V4\V44p\V4pVP;_uu_4VPP	V\44pWx9dRRR4R#RVR\P!\4P4R\V4R\V4/p	VPP	V.4PV	4VPV4VPe�VP^8�d�VPPV.4p56\V574VP8�deW�P)RVPV&VPV,Uu0uFp\VPR44kK	upVPV&VP!4RRR4R#uupi +'giR#;i)z<Record a message_id for later best-effort deletion (/clear).Nr8r9r;r<)r-rrr�58setdefaultrNr�nowr�	isoformatrrPrOrrIr>r�)r!r&r'r8r;r<rYr]r[�recrL�xs&&&&&&      r�record_message_id�SessionStore.record_message_id�s�������&�&�s�8�}�c�'�l�C���*�o��
�Z�Z�Z��(�(�3�3�H�c�e�D�D��{���Z��c��h�l�l�3�'�1�1�3��S��^���D�	�	�C�
���(�(��2�6�=�=�c�B��H�H�S�M��$�$�0�T�5J�5J�Q�5N��)�)�-�-�h��;���u�:�� 5� 5�5�27�9N�9N�8N�8P�2Q�D�%�%�h�/�:>�:K�:K�H�:U�7�:U�Q��A�E�E�,�/�0�:U�7�D�)�)�(�3�
���!�/�Z��&7�'�Z�Z�s%�59,G�DG�#G�' G�G�G'	c�<<�V^8�dQhRS[RS[RS[S[,/#r%)rrM)rrs"�rrr�s&���	�	��	�s�	�t�C�y�	rc60�p�VP\V4\V44pVP;_uu_4VPP	V.4pVUu.uF2pVP	R4fK\VP	R44NK4	upuuRRR4#uupi +'giR#;i)z=Get all recorded message IDs for a chat (in insertion order).r8N)r-rrrrI)r!r&r'rYrLr�s&&&   r�get_message_ids_for_chat�%SessionStore.get_message_ids_for_chat�s����&�&�s�8�}�c�'�l�C��
�Z�Z�Z��%�%�)�)�(�B�7�E����A��5�5��&�)��A�E�E�,�'�(����Z����Z�Z�s#�!B$�B�4B�B$�B$�$B5	c�<�V^8�dQhRR/#r0r,)rrs"�rrr�s���
#�
#�4�
#rc�V�VP;_uu_4VPP4VPP4VPP4VP61P4VP
4pRRR4VPX4R# +'giL$;i \dap\P!RT24TP;_uu_4RTnRRR4Rp?R# +'giRp?R#;iRp?ii;i)zDClear all stored sessions/trees/mappings and persist an empty store.Nr�T)rr�clearrrrr�r�rTrrUrr�s&  r�	clear_all�SessionStore.clear_all�s���
�Z�Z�Z��K�K�������$�$�&����#�#�%��!�!�'�'�)��'�'�)�H��	#����X�&��Z���	#��L�L�4�Q�C�8�9�����"���������	#�sB�A9B*�B=�*B:	�=D(�-D#�5D�=D#�D �D#� D#�#D(c�*<�V^8�dQhRS[RS[RR/#)r62�root_id�	tree_datar(N�rrK)rrs"�rrr�s"���2�2��2��2�$�2rc�$�VP;_uu_4W PV&VPR/4FpWPV&K	VP	4\63P!RV24RRR4R# +'giR#;i)z{64Save a message tree.65 66Args:67    root_id: Root node ID of the tree68    tree_data: Serialized tree data from tree.to_dict()69�nodeszSaved tree N)rrrIrr�r�debug�r!r�r��node_ids&&& r�	save_tree�SessionStore.save_tree�sk���Z�Z�Z�#,�K�K�� �%�=�=��"�5��.5�"�"�7�+�6�
���!��L�L�;�w�i�0�1��Z�Z�Z�s�AA>�>B	c�4<�V^8�dQhRS[RS[R,/#�r70r�r(Nr�)rrs"�rrrs���,�,��,��t��,rc��VP;_uu_4VPPV4uuRRR4# +'giR#;i)zGet a tree by its root ID.N)rrrI)r!r�s&&r�get_tree�SessionStore.get_trees'��
�Z�Z�Z��;�;�?�?�7�+��Z�Z�Z�s	�;�A	c�*<�V^8�dQhRS[RS[RR/#)r71r�r�r(Nr))rrs"�rrr72s"���"�"�S�"�3�"�4�"rc��VP;_uu_4W PV&VP4RRR4R# +'giR#;i)z"Register a node ID to a tree root.N)rrr�)r!r�r�s&&&r�
register_node�SessionStore.register_node73s1��
�Z�Z�Z�*1���w�'����!��Z�Z�Z�s	�>�A	c�4<�V^8�dQhRS[S[,RR/#)r74�node_idsr(N)rMr)rrs"�rrrs���"�"�T�#�Y�"�4�"rc���VP;_uu_4VFpVPPVR4K!	VP4RRR4R# +'giR#;i)z.Remove node IDs from the node-to-tree mapping.N)rr�popr�)r!r��nids&& r�remove_node_mappings�!SessionStore.remove_node_mappingssC��
�Z�Z�Z����"�"�&�&�s�D�1� ����!��Z�Z�Z�s�6A�A&	c�$<�V^8�dQhRS[RR/#r�r))rrs"�rrrs���&�&�3�&�4�&rc�:�VP;_uu_4VPPVR4pV'dFVPR/4FpVPPVR4K!	VP4RRR4R# +'giR#;i)z7Remove a tree and all its node mappings from the store.Nr�)rrr�rIrr�r�s&&  r�remove_tree�SessionStore.remove_treese��
�Z�Z�Z��������6�I��(�}�}�W�b�9�G��&�&�*�*�7�D�9� :��#�#�%��Z�Z�Z�s�A*B	�	B	c�6<�V^8�dQhRS[S[S[3,/#rb�rKr)rrs"�rrr s���%�%�t�C��I��%rc��VP;_uu_4\VP4uuRRR4# +'giR#;i)z'Get all stored trees (public accessor).N)rrKrr�s&r�
get_all_trees�SessionStore.get_all_trees s#��
�Z�Z�Z�����$��Z�Z�Z��	�5�A	c�6<�V^8�dQhRS[S[S[3,/#rbr�)rrs"�rrr%s���,�,�$�s�C�x�.�,rc��VP;_uu_4\VP4uuRRR4# +'giR#;i)z/Get the node-to-tree mapping (public accessor).N)rrKrr�s&r�get_node_mapping�SessionStore.get_node_mapping%s%��
�Z�Z�Z���*�*�+��Z�Z�Z�r�c�V<�V^8�dQhRS[S[S[3,RS[S[S[3,RR/#)r75r5r6r(Nr�)rrs"�rrr*s5���"�"��#�t�)�_�"�48��c��N�"�	
�"rc��VP;_uu_4WnW nVP4RRR4R# +'giR#;i)z8Sync internal tree state from external data and persist.N)rrrr�)r!r5r6s&&&r�sync_from_tree_data� SessionStore.sync_from_tree_data*s0���Z�Z�Z��K�!-�����!��Z�Z�Z�s	�<�A
	)76rrrrrrrrrr)z
sessions.json)�__name__�77__module__�__qualname__�__firstlineno__�__doc__r"r-r rfr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r��__static_attributes__�__classdictcell__)rs@rrrs�������'+�	��('�'�1:�1:�f78�79���$80!�81!�#�#�  � �	#�	#�&"�&"�P	�	�
#�
#�"2�2�$,�,�82"�"�"�"�&�&�%�%�83,�,�84"�"rr)
r�r{rGrCrrrrr�typingr�logururrr,rr�<module>r�s0�����	���"���^"�^"r