pyc • Lines: 530�
;�j5 � � � d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl
mZ dZd� 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 � � t j j t j dt j
| � � 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_filer 3 s0 � �
�7�7�<�<����)�A�4D�4D�T�4J�3K�5�(Q�R�R� c � � | syt j | � }t j � j � d�}t j t j d�� t
j j t j |� }t |d� 5 }|j |� ddd� d|� �S # 1 sw Y �xY w)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_DIRr r �open�write)� image_b64r �raw�fname�fpath�fs r
�_save_upload_imager# 7 s� � � ��
�
�
�9�
%�C��z�z�|��� ��%�E��K�K��
�
��-��G�G�L�L�����.�E�
�e�T� � �a� ������
�u�g���� �s �B8�8Cc �0 � | syt | � j � }|j d� r|j dd� d }|j d� st j
d|� r|S t
||� S # t t j f$ r Y yw xY w)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/�,� �����)r z/output/z/api/image/z
^https?://)
�str�strip�
startswith�split�re�matchr# �
ValueError�binascii�Error)�imager �ss r
�_resolve_image_urlr3 I s� � � ���E�
����A��|�|�M�"�
�G�G�C��O�B����|�|�<�=�����q�B� ��
�!�!�T�*�*������'�
���
�s �-A9 �9B�Bc �~ � d}| D �] }|j d� }t |t � s�&g }|D ]� }t |t � r�|j d� dk( r�|j di � j dd� }t |t � r^|j d� rM |j
dd � d
}t ||j dd� � }|r|j dd|id
�� d}��|j |� �� |st |� t |� k7 s��||d<