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

��j�H���dZddlZddlZddlZddlZddlZddlmZddlm	Z	d�Z
d�Zd�Zd�Z
d	�Zd
�Zdd�Zd�Zd
�Zd�Zd�Zd�Zd�Zy)zLThe event loop, task queue and task-state helpers that drive a chat request.�N)�resolve_image_path)�Mc��tj5|tjvrQtj|jd�dk7r,dtj|d<|tj|d<ddd�y#1swYyxYw)N�status�	cancelled�working�message�r�
_data_lock�tasks�get)�task_idr	s  �:/home/palash/git/local-ai/server/features/orchestration.py�
set_statusr
sk��	
���2��a�g�g��!�'�'�'�"2�"6�"6�x�"@�K�"O�)2�A�G�G�G��X�&�*1�A�G�G�G��Y�'�2�2�2�s�A$A>�>Bc�D�tjrtjSy�N�r�_client_location��r�location_strrs������!�!�!�rc��|t_yrr)�values r�set_client_locationrs
���A�rc��tj5tjj|i�jdd�cddd�S#1swYyxYw)N�_user�r
)rs r�
_task_userrs=��	
���9��w�w�{�{�7�B�'�+�+�G�R�8�9�9�9�s�0A�Ac��|syt|�}|jd�s>tjd|�s(|jd�r|j	dd�d}|St|�}|rH	t
|d�5}tj|j��j�cddd�Sy#1swYyxYw#t$rYywxYw)	aNormalize an uploaded image to base64 bytes.

    The UI may pass a ``/uploads/...`` link (uploaded ahead of time) instead of
    raw base64. Image code downstream (edit_image, ComfyUI input writing)
    expects actual bytes, so resolve links to their on-disk contents here.
    N)z	/uploads/�/output/z/api/image/z
^https?://zdata:image/�,�������rb)�str�
startswith�re�match�splitr�open�base64�	b64encode�read�decode�OSError)�image�s�fpath�fs    r�_image_bytes_b64r4#s������E�
�A�
�L�L�A�B�b�h�h��q�G�
�<�<�
�&�����Q���#�A����q�!�E��	��e�T�"�
;�a��'�'�����1�8�8�:�
;�
;��	
;����	���	�s0�,C�81B4�)	C�4B=�9C�=C�	C�Cc�B�tj5tjj|i�}|jd�r)tjjdd�cddd�S	ddd�tjjdd�S#1swY�)xYw)z�Tool-loop round budget for a task: 10 for normal chats, 50 when the
    UI's research toggle is on (stored on the task as ``research``).�research�2N�default�
)rrrr
�MAX_TOOL_ROUNDS)r�ts  r�_task_max_roundsr<=s~��
���9�
�G�G�K�K���$���5�5����$�$�(�(��R�8�9�9��9�
��� � ��B�/�/�	9�9�s�AB�Bc��tj5|tjvr�tj|}d}|jd��2t	tj
�|jd�z
dz�}dt
|�|jd|�|d�tj|<ddd�y#1swYyxYw)N�_started_at���error�
session_id)rr@rA�_elapsed_ms)rrrr
�int�timer%)rr@�sid�d�
elapsed_mss     r�_set_task_errorrHGs���	
�����a�g�g������ �A��J��u�u�]�#�/� �$�)�)�+����m�0D�"D��!L�M�
�!��U���e�e�L�#�6�)�	 �A�G�G�G��
���s�BB6�6B?c��tj5tjj|�}|s
	ddd�y|jd�}ddd�sytj
j
|�r|n-tj
jtj|�}	tj
j|�r't	j|�td|�d|���yy#1swY��xYw#t$rYywxYw)zGRemove the generated image file attached to a (cancelled) task, if any.N�
image_filez*[cancel] Removed image for cancelled task �: )
rrrr
�os�path�isabs�join�IMG_PATH�exists�remove�printr/)rr;�fnamer2s    r�_delete_task_imagerUVs���	
���$�
�G�G�K�K�� ����$�$����l�#��	$�
���W�W�]�]�5�)�E�r�w�w�|�|�A�J�J��/N�E�
�
�7�7�>�>�%� ��I�I�e���>�w�i�r�%��Q�R�!�$�$���
��
�s#�#C0�C0�)AC<�0C9�<	D�Dc
��tj5tjj|�}|s
	ddd�yt	|jdg��}t	|jdg��}|jd�}|jd�}|jd�}	|jd�}
|jd�}ddd�rd|��nd}|rtd	|�d
|�d|���|jdi�}
|
jd
�}tj5jd�}ddd�d}�#t
tj�|z
dz�}|jdig�djdi�jdd�}|s|rd}d|||	tjd��|d�
}
�
|
|d<|d<tj|�}tj5|tjvrWtj|j|�tj�tjj|i�d<|dk(r)|t_tj�t_ntj�t_ddd�tj"�tj5|tjvr�d||tjj|i�jdd�d�tj$|tjj|g���||||||	|||d�	�tj|<|
�,|
tj|d<tj|d<ddd�y#1swY���xYw#1swY���xYw#1swY��xYw#1swYyxYw)N�_tools_used�_search_detailsrJ�
gen_prompt�_image_model�
_verification�_verification_durationr z[finalize] image_file='u' → image_url='z' for task �timings�predicted_per_secondr>r?�choicesrr	�reasoning_contentrz(No response content generated)�	assistantr6)
�role�content�
_reasoningrW�
_image_url�_gen_promptrZrX�	_researchrB�updated�gpu�done�name)r�responserA�session_name)	r^�
tools_usedr0rerYrZrX�	reasoningrB)rrrr
�listrSrCrD�bool�	task_mode�sessions�append�
sessions_meta�
setdefault�	_last_tps�
_last_llm_use�_cpu_last_llm_use�
save_sessions�context_token_report)rrE�msg_content�bodyr;rn�search_details�image_filenamerY�image_model�verification�verification_duration�	image_urlr]r^�
started_atrGro�	msg_entry�modes                    r�_finalize_taskr�hsy��	
���
@�
�G�G�K�K�� ����
@�
@��!�%�%�
�r�2�3�
��a�e�e�$5�r�:�;�����|�,���U�U�<�(�
��e�e�N�+���u�u�_�-�� !���&>� ?��
@�0>�(�>�*�+�4�I��
�'��'7�7H���S^�_f�^g�h�i��h�h�y�"�%�G�"�;�;�'=�>��	
���*��U�U�=�)�
�*��J����$�)�)�+�
�2�d�:�;�
�����R�D�!�!�$�(�(��B�7�;�;�<O�QS�T���9�7�����!��!�#�)��!�%�%�
�+�,�!��I���%1�	�/�"�.C�	�*�+��;�;�w��D�	
���.��!�*�*��
�J�J�s�O�"�"�9�-�=A�Y�Y�[�A�O�O�&�&�s�B�/�	�:��5�=�.�A�K�"�i�i�k�A�O�"&�)�)�+�A��.��O�O��	
���S��a�g�g�� �'�!� !��� 3� 3�C�� <� @� @��� L�	 �
�(�(��a�j�j�n�n�S�"�.E�F� �)=�(�"�'�(� +�#1�&�)� �A�G�G�G�� �'�4@����� ��1�=R����� �!9�:�)S�S�i
@�
@�� *�*��4.�.��S�S�s7�#N&�BN&�"N3�5B5O�CO
�&N0�3N=�O
�
Oc�H�tjj|||f�yr)r�_event_queue�put)�ev_typer�datas   r�_event_postr��s���N�N�����$�/�0rc�
�	tjj�\}}}tjj|�}|s�E|jd�dk(rtj|��o|dk(�r-|d}|d}|jd�}|jd�}|jdd	�}|jd
�}	tj
5dd|gg|t
|�|||	|jd
�t|jd��t|jd��t|jd��|jd�d�tj|<ddd�tj||||||	�tj||d��n�|dk(�r�|jd�dk7r���|d}|d}
|d}|ddd}tj|�}
tj
5|
dk(rtj�t_ntj�t_
ddd�|jd��rtj
5tjj|�}|r=|jdg�|jdg�|jdd	�}|r||d<ddd�t|d�}t!d|
�d |�d!|���tj
5tjj|�}|r
d"|d<||d#<ddd�tj
5|tj"vr�d$d%i}|jd&�r|d&|d&<|jd�r|d|d<tj"|j%|�tj�tj&j|i�d'<ddd�tj(�tj|�}t+|d�D]J\}}tj,|j/tj0||||jd(�|
|��L�nt!d|
�d)|���|jd�r�tj2|d*�tj
5tjj|�}|rd+|d<ddd�tj,|
j/tj4|||jd&�xsd	||
��nMtj6|||jd&�xsd	|��n |d,k(rA|jd�dk7r��|tj8||d-|jd���n�|d.k(�r�|d}|d/}|d0}tj
5|tj"vrltj"|j%d1||d2��tj�tj&j|i�d'<t!d3|�d4|���tjj|�}|r|jd�d5vr	ddd����|r|jd#d�d6z
nd}|r||d#<ddd�tj(�t!d7|�d8���|dk�r�|jdd�d6z}
t!d9|�d:|
�d;��tj
5tjj|�}|r|
|d<<ddd�|
tj:|�krtj|||
��n
tj8|d=|��n�|d>k(�r�|jd0t=j>d-|jd-d?�i��}tj
5|jd�tj"vrdtj"|dj%d1|d/|d2��tj�tj&j|di�d'<tjj|�}|r|jd�d5vr	ddd����|r|jd#d�d6z
nd}|r||d#<ddd�tj(�dkr�|jdd�d6z}
tj
5tjj|�}|r|
|d<<ddd�|
tj:|�krtj||d|
�ntj8|d=|d��	��#1swY��xYw#1swY��TxYw#1swY���xYw#1swY��xYw#1swY���xYw#1swY���xYw#1swY��)xYw#1swY���xYw#1swY��.xYw#1swY��xYw)@Nrr�startrEr	r0�audio�userr�client_timestamprzProcessing task...r�r6�cpu�no_toolsr>)rr	rArWrX�_original_message�_original_image�_audior�_client_timestampr�r6r�r�r>r�llm_ok�_state�llm_waiting�roundr}r_�
tool_callsrWrXr`roz[llm_ok] Round z: LLM requested z tool(s) for task �
tools_running�_pending_toolsrbrarcrhr�z8: LLM generated final response (no tool calls) for task zVerifying sources...�critic_running�llm_errr@�tool_ok�tc_id�result�tool)rb�tool_call_idrcz*[tool_ok] Appended tool result to session z
 for task )rjr@r"z&[tool_ok] Pending tools left for task rKz"[tool_ok] All tools done for task z. Starting LLM round z  with search results in context.�_roundzMax tool rounds exceeded�tool_errz
Tool error) rr�r
rrUrr4rq�_prepare_session�_start_llm_roundrrrDryrxrv�lenrSrsrtrurz�	enumerate�_tool_pools�submit�_tool_workerr�run_verification_workerr�rHr<�json�dumps)r�rr�r;rE�user_message�	image_b64�	audio_b64r��	client_ts�	round_numr}�msgr��tt�rc�pending�
assistant_msg�	tool_mode�i�tcr�r�s                       r�_event_loopr��s���
�!"���!3�!3�!5����$�
�G�G�K�K�� �����5�5��?�k�)�
� � ��)���g���u�+�C��	�?�L�����)�I�����)�I��8�8�F�B�'�D����!3�4�I����
�'�3�"%�#%�')�)5�'7�	�'B�'�!�)2��E�E�&�M� $�T�X�X�j�%9� :�������0� $�T�X�X�j�%9� :�#$�5�5��#7�$����� �
�(
���w��\�9�i�QZ�[�
���w��Q�/�
��
 ��u�u�X��-�/���u�+�C��W�
�I���<�D��y�/�!�$�Y�/�C��;�;�w�'�D����
2��5�=�*.�)�)�+�A�'�&*�i�i�k�A�O�	
2�
�w�w�|�$��\�\�1������W�-�B���
�
�m�R�8��
�
�&7��<� �W�W�%8�"�=���.0�B�{�O�1��c�,�/�0����	�{�2B�7�)�K]�^e�]f�g�h��\�\�7������W�-�B��'6��8��/6��+�,�	7�
�\�\�U��a�j�j�(�)/��(=�
��7�7�9�-�7:�9�~�M�)�4��7�7�<�0�:=�l�:K�M�,�7��
�
�3��.�.�}�=�IM�������2�2�3��;�I�F�U����!��K�K��0�	�&�s�<�'8�9�	�E�A�r��M�M�)�,�3�3����������/�0�!���	���	�{�2j�kr�js�t�u��5�5��$��L�L��*@�A����<��W�W�[�[��1���+;�B�x�L�<��M�M�$�'�.�.��1�1�������+�1�r���
��$�$�W�c�C�G�G�I�4F�4L�"�t�T�
�	�
!��u�u�X��-�/��
���g�t�G�}�d�h�h�u�o�F�
�	�
!��u�+�C���M�E��(�^�F����
3��!�*�*�$��J�J�s�O�*�*�!'��6�R��FJ�Y�Y�[�A�O�O�.�.�s�B�7�	�B��F�s�e�:�V]�U^�_�`��W�W�[�[��)���R�V�V�H�-�1B�B��
3�
3�@B�2�6�6�"2�A�6��:�q���+2�B�'�(�
3�
�O�O���:�7�)�2�g�Y�O�P��!�|� �H�H�W�a�0�1�4�	��:�7�)�CX�Yb�Xc�dD�E�F��\�\�1������W�-�B��'0��8��1��q�1�1�'�:�:��&�&�w��Y�?��%�%�g�/I�3�O�
�
�
"��X�X��$�*�*�g�t�x�x���/N�%O�P��F����
3��8�8�E�?�a�j�j�0��J�J�t�E�{�+�2�2�$*�,0��M�'-���NR�Y�Y�[�A�O�O�.�.�t�E�{�B�?�	�J��W�W�[�[��)���R�V�V�H�-�1B�B��
3�
3�@B�2�6�6�"2�A�6��:�q���+2�B�'�(�
3� 
�O�O���!�|� �H�H�W�a�0�1�4�	��\�\�1������W�-�B��'0��8��1��q�1�1�'�:�:��&�&�w��U��Y�G��%�%�g�/I�4�PU�;�W�]� 
�
��>
2�
2��1�1��7�7��
U�U��2<�<��0
3�
3��$1�1��
3�
3��&1�1�s��Bf�>Af�)Af�,f+�B f8�<'g�
B4g� g�'g�B;g,�  g,�'g9�f�f�f(�+f5�8g�g�g�g)�,g6�9hc��y)a&
    # Removed the following check as self-agent bots will continue on CPU
    # Not needed anymore
    
    with M._queue_locks["gpu"]:
        if M._current_task_ids["gpu"] is not None or M._task_queues["gpu"]:
            return True
    now = time.time()
    with M._tokens_lock:
        for token, entry in M._active_tokens.items():
            if token in M._agent_tokens or entry.get("user") in M._agent_users:
                continue
            if now - entry.get("last_seen", 0) <= M.ACTIVE_WINDOW_SECONDS:
                return True
    Frrrr�_human_priority_activer�js�� rc��tj|}tj|}tj|}	|dk(r*t	�r tjd�t	�r� d}|5|s|j�|s�tj5tj}ddd�r|dk(stjr^|rdnd}|D]8}|d}|tjvs�dd	|�d
�|dd�tj|<�:|jd
�	ddd���|dk(ritj�rU|D]4}|d}|tjvs�dd|dd�tj|<�6|jd
�	ddd���V|jd�}|dtj|<tj5|dtjvr+tj
�tj|dd<ddd�ddd�tjd|d|d|d|jd�|jd�|jdd�|jd�|jd�|jd�|jd���	tj5tjj|di�jd�}	ddd�	dvrntjd��f|5dtj|<|j!�ddd���#1swY���xYw#1swY��IxYw#1swY��NxYw#1swY�{xYw#1swY�>xYw)u5Drain the task queue for ``mode`` ("gpu" for interactive UI users,
    "cpu" for self-chat agents).

    Each lane runs on its own thread with its own lock/condition/queue, so a
    self-chat agent task sitting in the CPU lane can never make an
    interactive UI user in the GPU lane wait in line — they only share
    physical hardware if they both actually need the GPU (chat model load or
    image generation), which is arbitrated separately.

    The CPU lane additionally yields to any human presence (see
    ``_human_priority_active``) before starting its *next* task. An
    already-running self-chat round is never interrupted — it runs on its own
    hardware and was already established not to block the GPU lane — this
    only holds the CPU lane from picking up new work while a human is around.
    r�g�?NrizGPU overheatingu#RAM pressure — restarting serversr�waitinguServer paused — z. Will resume shortly.rA)rr	rA�z%Yielding to an active user session...rr>r�r	r0r�r�rr�r6r�)	rEr	r0r�r�r�r6r�r�r)rjr@rg�?)r�_queue_locks�_queue_conds�_task_queuesr�rD�sleep�waitr�_overheated�_ram_evacuatingr�pop�_current_task_idsr�r
�
notify_all)
r��
queue_lock�
queue_cond�
task_queue�item�oh�label�qitem�tid�sts
          r�
_queue_workerr�}s5�� ����%�J�����%�J�����%�J�
��5�=�(�*��
�
�3��)�*���
�#	J� ����!�!����
#��]�]��
#�
�t�u�}��):�):�-/�)�5Z��'��E��	�*�C��a�g�g�~�&/�);�E�7�BX�'Y�*/��*=�(�����������"��'#	J�#	J�(�u�}��!9�!9�!;�'��E��	�*�C��a�g�g�~�&/�'N�*/��*=�(�����������"��=#	J�#	J�>�>�>�!�$�D�(,�Y��A����%����
J��	�?�a�g�g�-�>B�i�i�k�A�G�G�D��O�,�]�;�
J�C#	J�H	
�
�
����O��\�"���O��(�(�7�#��(�(�7�#����&�"�%�!�X�X�&8�9��X�X�j�)�������X�X�j�)�	
� ����
D��W�W�[�[��i��"�5�9�9�(�C��
D��3�3���J�J�s�O���	$�(,�A����%��!�!�#�	$�C�
#�
#��<
J�
J��C#	J�#	J��j
D�
D��
	$�	$�st�.M�M�M�%AM�'2M�"5M�.M�7M�AM�M�<3M)�$M5�M	�M�M	�M�M&�)M2�5M>r)�__doc__r+r�rLr'rD�server.features.contextr�server.features.staterrrrrr4r<rHrUr�r�r�r�r�rrr�<module>r�se��R�
��	�	��6�#�2���9�
�40��
�$IS�X1�oX�d�&V$r