CoolFace
Apppublic

ashnaali22/phase-3-h2

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
chatkit_adapter.cpython-313.pyc43 linesDownload Raw Back to __pycache__
1�

2��Ji
��v�SrSSKrSSKrSSKJr SSKJr SSKJr \R"\35r"SS\5rg)z�4ChatKit Adapter Middleware5 6Translates ChatKit protocol requests to custom endpoint format.7 8Converts:9  {"type": "threads.create", "params": {"input": {"content": "Hello"}}}10To:11  {"message": "Hello", "conversation_id": null}12�N��Request)�BaseHTTPMiddleware)�ASGIAppc�B^�\rSrSrSrS\4U4SjjrS\4SjrSr	U=r13$)�ChatKitAdapterMiddleware�a$14Middleware to adapt ChatKit protocol requests to custom format.15 16ChatKit sends requests in its protocol format with types like:17- threads.create18- threads.add_user_message19- threads.list20etc.21 22This middleware translates those to the custom {"message": "...", "conversation_id": ...} format.23�appc�$>�[TU]U5 g)N)�super�__init__)�selfr24�	__class__s  ��QD:\code\Q4\hackathon-2\todo-app\phase-3\backend\src\middleware\chatkit_adapter.pyr
�!ChatKitAdapterMiddleware.__init__"s���
������requestc���^# �URRS:XGa�URS:XGa�UR5IShv�Nn[R25"U5nUR
S5n[RSU35 US:XaUUR
S05nUR
S05nUR
SS	5nSn	[RS26USSS35 O�US
:Xa�UR
S05nUR
S05nUR
SS	5nUR
S5n27U28(a!U29R5(a[U305n	OSn	[RSU31SUSSS35 O)[RSUS35 U"U5IShv�N$UU	S.m[RST35 U4SjnSSKJ
n URn
U4SjnX�lU"U5IShv�NnU$GN�NZ![Ra"n[R!SU35 SnANHSnAf["a!n[R!SU3SS9 SnANqSnAff=fNm7f)Nz/api/v1/chat�POST�typez([ChatKitAdapter] Received request type: zthreads.create�params�input�content�z+[ChatKitAdapter] threads.create - message: �2z...zthreads.add_user_message�	thread_idz7[ChatKitAdapter] threads.add_user_message - thread_id: z, message: z+[ChatKitAdapter] Unsupported request type: z, passing through unchanged)�message�conversation_idz [ChatKitAdapter] Translated to: c��Z># �S[R"T5R5S.$7f�Nzhttp.request)r�body��json�dumps�encode��32custom_reqs�r�receive�2ChatKitAdapterMiddleware.dispatch.<locals>.receiveZ�(���� .� $�33�34�:� 6� =� =� ?�����(+rrc��Z># �S[R"T5R5S.$7fr r"r&s�r�custom_receive�9ChatKitAdapterMiddleware.dispatch.<locals>.custom_receivefr*r+z'[ChatKitAdapter] Failed to parse JSON: z+[ChatKitAdapter] Error processing request: T)�exc_info)�url�path�methodr!r#�loads�get�logger�info�isdigit�int�warning�fastapir�_receive�JSONDecodeError�error�	Exception)rr�	call_next�35body_bytes�chatkit_req�req_typer�36input_datarrrr(�FastAPIRequest�original_receiver-�e�responser's                 @r�dispatch�!ChatKitAdapterMiddleware.dispatch%sQ�����;�;���~�-�'�.�.�F�2J�&�|�|�~�-�J�G
_�"�j�j��4��&�?�?�6�2�����F�x�j�Q�R��/�/�(�_�_�X�r�:�F�!'���G�R�!8�J�(�n�n�Y��;�G�&*�O��K�K�"M�g�VY�WY�l�^�[^� _�`��!;�;�(�_�_�X�r�:�F�!'���G�R�!8�J�(�n�n�Y��;�G� &�37�38�;� 7�I�!�Y�%6�%6�%8�%8�*-�i�.��*.���K�K�"Y�Zc�Yd�do�pw�x{�y{�p|�o}�~A�!B�C��N�N�%P�QY�PZ�Zu�#v�w�!*�7�!3�3�3� '�'6��39�40���>�z�l�K�L��>�$+�#3�#3� ��$2� �#�7�+�+����].�J4��@�'�'�
L����F�q�c�J�K�K���
_����J�1�#�N�Y]��^��
_��41,�sz�AI�G6�I�	EG;�G9�G;�"I�#AG;�$I�/I�0I�9G;�;I�H,�'I�,
I�9I�I�I�I�)�__name__�42__module__�__qualname__�__firstlineno__�__doc__rr
rrH�__static_attributes__�
__classcell__)rs@rrrs(���43��G��R�g�R�Rrr)
rOr#�loggingr:r�starlette.middleware.baser�starlette.typesr�	getLoggerrKr5rrJrr�<module>rVs:��	����8�#�	�	�	�8�	$��b�1�br