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

;�j5���dZddlZddlZddlZddlZddlZddlZddlZddlZddl	m	Z	ddl
mZdZd�Z
dd�Zdd�Zd	�Zd
�Zd�Zd�Zd
�Zdd�Zy)zEConversation session persistence and per-request session preparation.�N)�datetime)�Mu7	## Research Mode
You are performing deep, sourced research on the user's question.
- Plan: break the question into a few sub-questions/angles before answering.
- Gather: use web_search and fetch_page repeatedly. Fetch full pages and, when
  a page is long, read through it (a page may be returned in chunks).
- Cite: attach the exact source to every fact in EXACTLY the inline form
  `(Author, Venue, Year) [https://exact-page-url]` right at the claim. Use
  ROUND brackets (…) for the metadata and SQUARE brackets [url] for the URL.
  The metadata and the URL must both be present for EVERY factual claim. A
  citation with an empty or missing URL is strictly forbidden — never write
  `[...] []` or `(...) []`. Never cite a URL you did not actually open with
  fetch_page or see listed in a web_search result. Never reuse one URL as the
  support for many unrelated claims. If you are not certain about a metadata
  field, write "(Author, Venue, uncertain)" — never guess a year or author.
- Never invent: never write facts, sources, papers, or findings from memory or
  imagination and present them as researched. If you do not have a fetched
  source backing a claim, you do not have the claim yet.
- Resource failures are a signal to search MORE, not to improvise: if a fetch
  fails (403/404/timeout/blocked), re-search for the same article (mirrors,
  snippets, alternate hosts) and fetch again; keep searching and fetching new
  material until every claim is grounded in a source you actually opened. If a
  sub-answer genuinely has no findable source, state that it is UNSUPPORTED
  instead of fabricating support.
- Verify: cross-check important claims against more than one source.
- Conclude: answer only once the question is fully covered, then write a
  structured report (summary, findings with citations, limitations).
- Budget: you may keep searching/fetching for up to 50 rounds of tools, but
  stop as soon as the question is actually answered.
- Social Media & Unverified Content: Treat social media platforms 
  (X/Twitter, Reddit, forums, public blogs) strictly as anecdotal opinions or leads, 
  never as primary factual proof. Do not cite social media claims as verified facts 
  unless cross-checked and corroborated by an authoritative primary source 
 (official documentation, peer-reviewed study, or established publication).c��tjjtjdtj
|��d��S)N�	sessions_z.json)�os�path�joinr�SESSIONS_DIR�_safe_username)�users �5/home/palash/git/local-ai/server/features/sessions.py�
_session_filer3s0��
�7�7�<�<����)�A�4D�4D�T�4J�3K�5�(Q�R�R�c��|sytj|�}tj�j�d�}tjtjd��t
jjtj|�}t|d�5}|j|�ddd�d|��S#1swY�xYw)a*Persist a base64-encoded upload to the uploads dir and return its URL.

    Keeps image bytes on disk instead of inside the conversation history, so
    sessions stay small and the LLM only receives the bytes for images it
    actually needs (see ``read_image`` / ``prepare_context_for_llm``).
    Nz.jpgT��exist_ok�wb�	/uploads/)
�base64�	b64decode�uuid�uuid4�hexr�makedirsr�UPLOADS_DIRrr	�open�write)�	image_b64r�raw�fname�fpath�fs      r
�_save_upload_imager#7s�����
�
�
�9�
%�C��z�z�|��� ��%�E��K�K��
�
��-��G�G�L�L�����.�E�	
�e�T�	��a�	������
�u�g�����s�B8�8Cc�0�|syt|�j�}|jd�r|jdd�d}|jd�st	j
d|�r|S	t
||�S#ttjf$rYywxYw)a�Return the stored URL for a chat image that may be base64 or a link.

    The UI now uploads attached photos up front (``/api/upload-image``) and
    passes the resulting ``/uploads/...`` link with the chat request instead of
    embedding the raw base64. Accept either form so both old and new clients
    keep working: raw base64 blobs are written to disk, already-stored links
    and ``data:`` URLs are returned as-is.
    Nzdata:image/�,������)rz/output/z/api/image/z
^https?://)
�str�strip�
startswith�split�re�matchr#�
ValueError�binascii�Error)�imager�ss   r
�_resolve_image_urlr3Is������E�
����A��|�|�M�"�
�G�G�C��O�B����|�|�<�=�����q�B���
�!�!�T�*�*������'�
���
�s�-A9�9B�Bc	�~�d}|D�]}|jd�}t|t�s�&g}|D]�}t|t�r�|jd�dk(r�|jdi�jdd�}t|t�r^|jd�rM	|j
dd	�d
}t||jdd��}|r|jdd|id
��d}��|j|���|st|�t|�k7s��||d<��|S#ttjf$rd}Y�pwxYw)a%Rewrite legacy ``data:image`` content parts to ``/uploads/`` file URLs.

    Older sessions stored uploaded images inline as base64 (a single session
    could reach ~19 MB). This writes those bytes to the uploads dir once and
    replaces the part URL, so the stored history stays small.
    F�content�type�	image_url�url�z
data:imager%r&r'�_userN�r6r7T)
�get�
isinstance�list�dictr(r*r+r#r.r/r0�append�len)	�messages�changed�msgr5�parts�pr8�b64�new_urls	         r
�_migrate_data_urlsrIbs5���G��#���'�'�)�$���'�4�(�����
	�A��!�T�"�q�u�u�V�}��'C��e�e�K��,�0�0���;���c�3�'�C�N�N�<�,H�'�!�i�i��Q�/��3��"4�S�#�'�'�'�2�:N�"O������k��PW�HX�%Y�Z�"&�� ��L�L��O�
	��c�%�j�C��L�0�"�C�	�N�)#�*�N��'����7�'�"&��'�s�1D�D<�;D<c
�>�|jdd�|jdtj��|jdtj��|jdd�|jdg�|jdi�|jd	d�d
�S)N�name�Chat�created�updated�user_idr9�system_prompts�context_tokens�
system_prompt)rKrMrNrOrPrQrR)r<�time)�sdatas r
�_session_meta_fromrU�s|���	�	�&�&�)��9�9�Y��	�	��4��9�9�Y��	�	��4��9�9�Y��+��)�)�$4�b�9��)�)�$4�b�9����?�B�7��rc��g}|xsgD�]%}t|t�rN|jd�xsd}|j�s�:|j	|jd�xsd|d���bt|t
�s�stjjtjj|��}tjj|�s��	t|dd��5}|j	tjj|�|j�d��d	d	d	���(|S#1swY�xYw#t$rY��CwxYw)
z�Normalize a list of extra system prompt sources into [{name, content}].

    Each item may be a {name, content} dict or a server-side file path string.
    r5r9rK�
System Prompt)rKr5�rzutf-8)�encodingN)r=r?r<r)r@r(rr�abspath�
expanduser�isfiler�basename�read�OSError)�items�blocks�itr5rFr"s      r
�_load_extra_promptsrc�s��
�F��k�r����b�$���f�f�Y�'�-�2�G��=�=�?���M�M������:�?�w�O�
���C�
 ��������� 2� 2�2� 6�7�A��w�w�~�~�a� ���a��w�7��1��
�
�%'�W�W�%5�%5�b�%9�a�f�f�h�O�����$�M�
�������s+�E�'AD4�'E�4D=	�9E�	E
�E
c��tjtjd��tj5tj
j
�tjj
�ddd�d}tjtjjtjd��D]�}	t|�5}tj|�}ddd�tj5j!di�j#�D]S\}}|j!dg�}t%|�rd}|tj
|<t'|�tj|<�U	ddd���tjjtjd�}tjj)|�r�	t|�5}tj|�}ddd�tj5j!di�j#�D]f\}}|tj
vs�|j!dg�}t%|�rd}|tj
|<t'|�tj|<�h	ddd�	tj*|�|rt/�yy#1swY��JxYw#1swY���xYw#ttjf$rY��3wxYw#1swY��AxYw#1swY��xYw#1swY��xYw#ttjf$rY��wxYw#t,$rY��wxYw)NTrFzsessions_*.json�sessionsrBz
sessions.json)rrrr
�
_data_lockre�clear�
sessions_meta�globrr	r�json�load�FileNotFoundError�JSONDecodeErrorr<r`rIrU�exists�remover_�
save_sessions)�migratedrr"�data�sidrT�msgs�stales        r
�
load_sessionsrv�s����K�K�����.�	
��� �	�
�
����	������ ��H��	�	�"�'�'�,�,�q�~�~�7H�I�J�A��	��d��
$�q��y�y��|��
$��\�\�	A�"�h�h�z�2�6�<�<�>�
A�
��U��y�y��R�0��%�d�+�#�H�"&��
�
�3��'9�%�'@�����$�
A�	A�	A�
A�
�G�G�L�L�����9�E�	�w�w�~�~�e��	��e��
$���y�y��|��
$����
I�"&�(�(�:�r�":�"@�"@�"B�I�J�C���!�*�*�,�$�y�y��R�8��-�d�3�'+�H�*.��
�
�3��/A�%�/H�����,�
I�
I�	��I�I�e������I � ��
$�
$��!�4�#7�#7�8�	��	��	A�	A��
$�
$��
I�
I��"�4�#7�#7�8�	��	���	��	�s��=J;�K�
K�#K�;A7K5�L�L�,L�9L�>AL�L�L:�;K�K	�
K�K2�1K2�5K?	�L�L�L�L�L7�6L7�:	M�Mc�@�tjtjd��i}tj5tj
D]�}tjj|dtj�tj�d��}|jdd�}|d|d|d	|jdd�|jd
g�|jdi�|jdd�tj
|d
�|j|i�jdi�|<��	ddd�|j�D];\}}tt|�d�5}tj||d��ddd��=y#1swY�XxYw#1swY�TxYw)NTrrL�rKrMrNrOr9rKrMrNrPrQrR)rKrMrNrOrPrQrRrBre�w�)�indent)rrrr
rfrerhr<rS�
setdefaultr`rrrj�dump)�by_userrs�metarrrr"s      r
rprp�s]���K�K�����.��G�	
�����:�:�	�C��?�?�&�&��f���������U��D��8�8�I�r�*�D��V���	�?��	�?��8�8�I�r�2�"&�(�(�+;�R�"@�"&�(�(�+;�R�"@�!%���/�2�!>��J�J�s�O�	L�G���t�R�(�3�3�J��C�C�H�	�� �m�m�o�)�
��d�
�-��%�s�
+�	)�q��I�I�d�A�a�(�	)�	)�)�!���"	)�	)�s�C8F�$F�F�F	c�F�	|r&tj|jdd��}ntj�}tj�}|rd|�d�nd}d|jd��d|��}	d}
g}i}d}
t
j5t
jj|�}|r|jdd�}
t
jj|i�}|jd	g�}|jd
i�}|jdd�}
ddd�|
rtj|
�nd}|rd|��nd}|
r|
nt
j}|�d
|	�|��}|D]-}|d|jdd��d|jdd���z
}�/t
j5t
jj|i�jd�r|d
t��z
}ddd�|jd|jd��}|r|jd|�}n$|jdd�}|jdd�}|j�D]\}}|j||�}�t!||
�}|rt#dt%|��d|
�d��t
j5|t
j&vst
j&|sd|d�gt
j&|<nht
j&|djd�dk(r|t
j&|dd<n&t
j&|j)dd|d��|t
jvr>|ddt+j*�t+j*�d �t
j|<g}|r|j-d!d"|id#��|r|j-d$d%d&��|j-d$|d&��t
j&|j-d'|tj�j/�t1t
jj|i�jd��d(��t
j|dd)vr,|ddt%|�dkDrd*ndzt
j|d<t+j*�t
j|d+<ddd�tj2�tj4|�}t
j5|d,k(rt
j6nt
j8}ddd�d-k7rtj:|�yy#t$rtj�}Y��/wxYw#1swY��^xYw#1swY���xYw#1swY��xYw#1swY�oxYw).N�Zz+00:00z [User location: �]r9z[Current date: z%Y-%m-%d %A %H:%Mr:rPrQrRz

## User Context
z

z

## rKrW�
r5�researchz%current_time%z%current_location%z5Currently the server is hosted on %current_location%.z
not availablez[context] Injected z chars of context for user '�'�system)�roler5rr��2rxr7r8r;�textu🎤 Audio message)r6r�r)r�r5�
_timestamp�	_research)zNew Chatr9z...rN�cpu�chat_loaded)r�
fromisoformat�replace�now�	Exceptionr�location_str�strftimerf�tasksr<rh�read_user_context�SYS_CONTENT�RESEARCH_DIRECTIVEr`r3�printrAre�insertrSr@�	isoformat�boolrp�	task_mode�_cpu_model_status�model_status�load_llama_model)�task_idrs�user_messager�	audio_b64�	client_ts�ts�loc�loc_context�date_loc_contextr�
extra_promptsrQrR�tr�user_context�
context_block�base_sys�full_sys_content�blk�token�valuer7r5�mode�mss                           r
�_prepare_sessionr��s)�����'�'�	�(9�(9�#�x�(H�I�B�����B��.�.�
�C�03�%�c�U�!�,��K�(����5H�)I�(J�!�K�=�Y��
�D��M��N��M�	
���6�
�G�G�K�K�� ����5�5��"�%�D����"�"�3��+�����!1�2�6�
����"2�B�7������"�5�
�6�15�1�&�&�t�,�"�L�>J�+�L�>�:�PR�M�!.�}�1�=�=�H�"��4�(8�'9�-��I���c���g�c�g�g�f�o�&F�%G�r�#�'�'�R[�]_�J`�Ia�b�b��c�	
���<��7�7�;�;�w��#�'�'�
�3��$�'9�&:� ;�;��<�(�/�/��"�+�+�&9�:����+�3�3�4H�#�N��+�3�3�C�R�
��,�3�3�4H�/�Z��&�,�,�.�B���u�+�3�3�E�5�A��B�"�9�d�3�I��
�!�#�l�"3�!4�4P�QU�PV�VW�X�	
�
���)6��a�j�j� ��
�
�3��(0�=M�N�O�A�J�J�s�O�
�Z�Z��_�Q�
�
#�
#�F�
+�x�
7�,<�A�J�J�s�O�A��y�)�
�J�J�s�O�"�"�1�x�DT�&U�V��a�o�o�%�$�S�b�)��9�9�;��9�9�;�$�A�O�O�C� �
����N�N�'�"'��!3��
���N�N�F�4N�O�P�����$�
�	
�	
�
�
�3�����"�&�l�l�n�6�6�8�!�!�'�'�+�+�g�r�":�">�">�z�"J�K�	
�	
�
�?�?�3���'�+;�;�+7���+<��\�*�R�/��R�,�A�O�O�C� ��(�+/�)�)�+������Y�'�S)6�T�O�O���;�;�w��D�	
���F�$(�E�M�Q�
 �
 �q�~�~��F�	�]��	���4� ���w��
�\�\�^����6�6�� <�<��()6�)6��XF�F�sC�<U
�B
U1�<U>�H#V�&V�
U.�-U.�1U;�>V�V�V )r9)NN)�__doc__rr/rirjrr,rSrr�server.features.staterr�rr#r3rIrUrcrvrpr��rr
�<module>r�sa��K�
����	�	����#� O��FS��$�2�@	��6'�T)�0b!r