Ë
    úmxi=  ã                  óú   — d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	 ddl
mZ ddlmZ erddlmZmZmZ dd	lmZ dd
lmZ dd„ZdZ G d„ de«      Z G d„ de	«      Z G d„ d«      Z G d„ d«      Z G d„ de«      Zy)aÑ  
LangChain CallbackHandler that prints to streamlit.

This is a special API that's imported and used by LangChain itself. Any updates
to the public API (the StreamlitCallbackHandler constructor, and the entirety
of LLMThoughtLabeler) *must* remain backwards-compatible to avoid breaking
LangChain.

This means that it's acceptable to add new optional kwargs to StreamlitCallbackHandler,
but no new positional args or required kwargs should be added, and no existing
args should be removed. If we need to overhaul the API, we must ensure that a
compatible API continues to exist.

Any major change to the StreamlitCallbackHandler should be tested by importing
the API *from LangChain itself*.

This module is lazy-loaded.
é    )ÚannotationsN)ÚEnum)ÚTYPE_CHECKINGÚAnyÚ
NamedTuple)ÚBaseCallbackHandler)Úgather_metrics)ÚAgentActionÚAgentFinishÚ	LLMResult)ÚDeltaGenerator)ÚStatusContainerc                ó&   — | j                  dd«      S )zZConvert newline characters to markdown newline sequences
    (space, space, newline).
    ú
z  
)Úreplace)Útexts    ún/home/htdocs/ttos/venv/lib/python3.12/site-packages/streamlit/external/langchain/streamlit_callback_handler.pyÚ_convert_newlinesr   B   s   € ð �<‰<˜˜fÓ%Ð%ó    é<   c                  ó   — e Zd ZdZdZdZdZy)ÚLLMThoughtStateÚTHINKINGÚRUNNING_TOOLÚCOMPLETEÚERRORN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   © r   r   r   r   N   s   „ à€Hà!€Là€Hà�Er   r   c                  ó"   — e Zd ZU ded<   ded<   y)Ú
ToolRecordÚstrÚnameÚ	input_strN)r   r   r   Ú__annotations__r    r   r   r"   r"   Y   s   … Ø
ƒIØ„Nr   r"   c                  ó(   — e Zd ZdZdd„Zdd„Zdd„Zy)ÚLLMThoughtLabelerz©
    Generates markdown labels for LLMThought containers. Pass a custom
    subclass of this to StreamlitCallbackHandler to override its default
    labeling logic.
    c                 ó   — y)ziReturn the markdown label for a new LLMThought that doesn't have
        an associated tool yet.
        zThinking...r    ©Úselfs    r   Úget_initial_labelz#LLMThoughtLabeler.get_initial_labele   s   € ð r   c                óê   — |j                   }|j                  }|dk(  rd}t        t        t	        |«      «      }|d| }t	        |j                   «      |kD  r|dz   }|j                  dd«      }d|› d|› �S )	a}  Return the label for an LLMThought that has an associated
        tool.

        Parameters
        ----------
        tool
            The tool's ToolRecord

        is_complete
            True if the thought is complete; False if the thought
            is still receiving input.

        Returns
        -------
        The markdown label for the thought's container.

        Ú
_ExceptionzParsing errorNz...r   ú z**z:** )r%   r$   ÚminÚMAX_TOOL_INPUT_STR_LENGTHÚlenr   )r+   ÚtoolÚis_completer%   r$   Úinput_str_lens         r   Úget_tool_labelz LLMThoughtLabeler.get_tool_labelk   s€   € ð$ —N‘Nˆ	Ø�y‰yˆØ�<ÒØ"ˆDÜÔ5´s¸9³~ÓFˆØ˜n˜}Ð-ˆ	Üˆt�~‰~Ó Ò.Ø! EÑ)ˆIØ×%Ñ% d¨CÓ0ˆ	Ø�D�6˜˜i˜[Ð)Ð)r   c                 ó   — y)z”Return the markdown label for the agent's final thought -
        the "Now I have the answer" thought, that doesn't involve
        a tool.
        z**Complete!**r    r*   s    r   Úget_final_agent_thought_labelz/LLMThoughtLabeler.get_final_agent_thought_labelˆ   s   € ð
 r   N)Úreturnr#   )r3   r"   r4   Úboolr9   r#   )r   r   r   Ú__doc__r,   r6   r8   r    r   r   r(   r(   ^   s   „ ñóó*ô:r   r(   c                  óâ   — e Zd ZdZ	 	 	 	 	 	 	 	 	 	 dd„Zedd„«       Zedd„«       Zdd„Zdd„Z	dd„Z
dd„Zdd	„Z	 	 	 	 	 	 	 	 dd
„Z	 	 	 d	 	 	 	 	 	 	 	 	 	 	 dd„Zdd„Z	 d	 	 	 	 	 	 	 dd„Zddd„Zy)Ú
LLMThoughta)  Encapsulates the Streamlit UI for a single LLM 'thought' during a LangChain Agent
    run. Each tool usage gets its own thought; and runs also generally having a
    concluding thought where the Agent determines that it has an answer to the prompt.

    Each thought gets its own expander UI.
    c                óÀ   — |j                  |j                  «       |¬«      | _        t        j                  | _        d| _        d | _        d | _        || _	        || _
        y )N)ÚexpandedÚ )Ústatusr,   Ú
_containerr   r   Ú_stateÚ_llm_token_streamÚ_llm_token_stream_placeholderÚ
_last_toolÚ_collapse_on_completeÚ_labeler)r+   Úparent_containerÚlabelerr?   Úcollapse_on_completes        r   Ú__init__zLLMThought.__init__˜   s^   € ð +×1Ñ1Ø×%Ñ%Ó'°(ð 2ó 
ˆŒô &×.Ñ.ˆŒØ!#ˆÔØDHˆÔ*Ø-1ˆŒØ%9ˆÔ"Øˆ�r   c                ó   — | j                   S )z!The container we're writing into.)rB   r*   s    r   Ú	containerzLLMThought.containerª   ó   € ð �‰Ðr   c                ó   — | j                   S )z'The last tool executed by this thought.)rF   r*   s    r   Ú	last_toolzLLMThought.last_tool¯   rO   r   c                ó‚   — | j                   �%| j                   j                  | j                  «       d| _        d | _         y )Nr@   )rE   ÚmarkdownrD   r*   s    r   Ú_reset_llm_token_streamz"LLMThought._reset_llm_token_stream´   s8   € Ø×-Ñ-Ð9Ø×.Ñ.×7Ñ7¸×8NÑ8NÔOà!#ˆÔØ-1ˆÕ*r   c                ó$   — | j                  «        y ©N)rT   )r+   Ú
serializedÚpromptss      r   Úon_llm_startzLLMThought.on_llm_start»   s   € Ø×$Ñ$Õ&r   c                óæ   — | xj                   t        |«      z  c_         | j                  €| j                  j	                  «       | _        | j                  j                  | j                   dz   «       y )Nu   â–•)rD   r   rE   rB   ÚemptyrS   ©r+   ÚtokenÚkwargss      r   Úon_llm_new_tokenzLLMThought.on_llm_new_token¾   sY   € à×ÒÔ"3°EÓ":Ñ:ÕØ×-Ñ-Ð5Ø15·±×1FÑ1FÓ1HˆDÔ.Ø×*Ñ*×3Ñ3°D×4JÑ4JÈUÑ4RÕSr   c                óv   — | j                  «        | j                  | j                  j                  «       «       y rV   )rT   ÚcompleterH   r8   ©r+   Úresponser^   s      r   Ú
on_llm_endzLLMThought.on_llm_endÅ   s)   € ð 	×$Ñ$Ô&à�‰�d—m‘m×AÑAÓCÕDr   c                ó†   — | j                   j                  |«       t        j                  | _        | j                  d«       y )NzLLM encountered an error...)rB   Ú	exceptionr   r   rC   ra   ©r+   ÚerrorÚargsr^   s       r   Úon_llm_errorzLLMThought.on_llm_errorÍ   s.   € Ø�‰×!Ñ! %Ô(Ü%×+Ñ+ˆŒØ�‰Ð3Õ4r   c                óD  — t         j                  | _        |d   }t        ||¬«      | _        | j
                  j                  | j                  j                  | j                  d¬«      d¬«       t        |«      t        kD  r | j
                  j                  d|› d�«       y y )	Nr$   )r$   r%   F©r4   Úrunning)ÚlabelÚstatez**Input:**

z

**Output:**)r   r   rC   r"   rF   rB   ÚupdaterH   r6   r2   r1   rS   )r+   rW   r%   r^   Ú	tool_names        r   Úon_tool_startzLLMThought.on_tool_startÒ   s�   € ô
 &×2Ñ2ˆŒØ˜vÑ&ˆ	Ü$¨)¸yÔIˆŒØ�‰×ÑØ—-‘-×.Ñ.¨t¯©ÈEÐ.ÓRØð 	ô 	
ô ˆy‹>Ô5Ò5à�O‰O×$Ñ$ ~°i°[ÀÐ%PÕQð 6r   Nc                ó:   — | j                   j                  |«       y rV   )rB   rS   ©r+   ÚoutputÚcolorÚobservation_prefixÚ
llm_prefixr^   s         r   Úon_tool_endzLLMThought.on_tool_endâ   s   € ð 	�‰× Ñ  Õ(r   c                ó¨   — | j                   j                  d«       | j                   j                  |«       | j                   j                  d¬«       y )Nz **Tool encountered an error...**rh   )ro   )rB   rS   rf   rp   rg   s       r   Úon_tool_errorzLLMThought.on_tool_errorì   s<   € Ø�‰× Ñ Ð!CÔDØ�‰×!Ñ! %Ô(Ø�‰×Ñ WÐÕ-r   c                 ó   — y rV   r    ©r+   Úactionrv   r^   s       r   Úon_agent_actionzLLMThought.on_agent_actionñ   s   € ð 	r   c                ó|  — |€[| j                   t        j                  k(  r>| j                  €t	        d«      ‚| j
                  j                  | j                  d¬«      }| j                  r/| j                  j                  dk(  rt        j                  | _         n2| j                   t        j                  k7  rt        j                  | _         | j                  rt        j                  d«       | j                  j                  || j                  rdnd| j                   t        j                  k(  rd¬
«       yd	¬
«       y)zFinish the thought.Nz;_last_tool should never be null when _state == RUNNING_TOOLTrl   r.   g      Ð?Frh   ra   )rn   r?   ro   )rC   r   r   rF   ÚRuntimeErrorrH   r6   r$   r   r   rG   ÚtimeÚsleeprB   rp   )r+   Úfinal_labels     r   ra   zLLMThought.completeú   sþ   € àÐ 4§;¡;´/×2NÑ2NÒ#NØ�‰Ð&Ü"ØQóð ð Ÿ-™-×6Ñ6Ø—‘¨Tð 7ó ˆKð �?Š?˜tŸ™×3Ñ3°|ÒCÜ)×/Ñ/ˆD�KØ�[‰[œO×1Ñ1Ò1Ü)×2Ñ2ˆDŒKà×%Ò%ä�J‰J�tÔà�‰×ÑØØ"×8Ò8‘U¸dØ!Ÿ[™[¬O×,AÑ,AÒA�'ð 	õ 	
ð HRð 	õ 	
r   )
rI   r   rJ   r(   r?   r:   rK   r:   r9   ÚNone)r9   r   )r9   zToolRecord | None)r9   r…   )rW   údict[str, Any]rX   ú	list[str]r9   r…   ©r]   r#   r^   r   r9   r…   ©rc   r   r^   r   r9   r…   ©rh   ÚBaseExceptionri   r   r^   r   r9   r…   ©rW   r†   r%   r#   r^   r   r9   r…   ©NNN©ru   r#   rv   ú
str | Nonerw   r�   rx   r�   r^   r   r9   r…   rV   ©r~   r
   rv   r�   r^   r   r9   r   ©r„   r�   r9   r…   )r   r   r   r;   rL   ÚpropertyrN   rQ   rT   rY   r_   rd   rj   rr   ry   r{   r   ra   r    r   r   r=   r=   �   s'  „ ñð à(ð ð #ð ð ð	 ð
 #ð ð 
ó ð$ òó ðð òó ðó2ó'óTóEó5ð
RØ(ðRØ58ðRØDGðRà	óRð& !Ø)-Ø!%ð)àð)ð ð)ð 'ð	)ð
 ð)ð ð)ð 
ó)ó.ð 8<ðØ!ðØ*4ðØGJðà	óõ
r   r=   c                  ó  — e Zd Z ed«      dddddœ	 	 	 	 	 	 	 	 	 	 	 dd„«       Zdd„Zdd„Zddd	„Z	 	 	 	 	 	 	 	 dd
„Zdd„Z	dd„Z
dd„Z	 	 	 	 	 	 	 	 dd„Z	 	 	 d	 	 	 	 	 	 	 	 	 	 	 dd„Zdd„Z	 d	 	 	 	 	 	 	 dd„Z	 d	 	 	 	 	 	 	 d d„Zy)!ÚStreamlitCallbackHandlerz+external.langchain.StreamlitCallbackHandleré   FN)Úmax_thought_containersÚexpand_new_thoughtsÚcollapse_completed_thoughtsÚthought_labelerc               ó¼   — || _         |j                  «       | _        d| _        g | _        t        |d«      | _        || _        || _        |xs
 t        «       | _
        y)a<  Construct a new StreamlitCallbackHandler. This CallbackHandler is geared
        towards use with a LangChain Agent; it displays the Agent's LLM and tool-usage
        "thoughts" inside a series of Streamlit expanders.

        Parameters
        ----------
        parent_container
            The `st.container` that will contain all the Streamlit elements that the
            Handler creates.

        max_thought_containers

            .. note::
                This parameter is deprecated and is ignored in the latest version of
                the callback handler.

            The max number of completed LLM thought containers to show at once. When
            this threshold is reached, a new thought will cause the oldest thoughts to
            be collapsed into a "History" expander. Defaults to 4.

        expand_new_thoughts
            Each LLM "thought" gets its own `st.expander`. This param controls whether
            that expander is expanded by default. Defaults to False.

        collapse_completed_thoughts
            If True, LLM thought expanders will be collapsed when completed.
            Defaults to False.

        thought_labeler
            An optional custom LLMThoughtLabeler instance. If unspecified, the handler
            will use the default thought labeling logic. Defaults to None.
        Né   )Ú_parent_containerrN   Ú_history_parentÚ_current_thoughtÚ_completed_thoughtsÚmaxÚ_max_thought_containersÚ_expand_new_thoughtsÚ_collapse_completed_thoughtsr(   Ú_thought_labeler)r+   rI   r–   r—   r˜   r™   s         r   rL   z!StreamlitCallbackHandler.__init__  s_   € ðT "2ˆÔØ/×9Ñ9Ó;ˆÔØ37ˆÔØ57ˆÔ Ü'*Ð+AÀ1Ó'EˆÔ$Ø$7ˆÔ!Ø,GˆÔ)Ø /Ò FÔ3DÓ3FˆÕr   c                óH   — | j                   €t        d«      ‚| j                   S )z]Return our current LLMThought. Raise an error if we have no current
        thought.
        z(Current LLMThought is unexpectedly None!)rž   r�   r*   s    r   Ú_require_current_thoughtz1StreamlitCallbackHandler._require_current_thoughtI  s(   € ð × Ñ Ð(ÜÐIÓJÐJØ×$Ñ$Ð$r   c                ó~   — t        | j                  «      dkD  r%| j                  t        | j                  «      dz
     S y)zJReturn our most recent completed LLMThought, or None if we don't have one.r   r›   N)r2   rŸ   r*   s    r   Ú_get_last_completed_thoughtz4StreamlitCallbackHandler._get_last_completed_thoughtQ  s9   € äˆt×'Ñ'Ó(¨1Ò,Ø×+Ñ+¬C°×0HÑ0HÓ,IÈAÑ,MÑNÐNØr   c                óŠ   — | j                  «       }|j                  |«       | j                  j                  |«       d| _        y)z{Complete the current thought, optionally assigning it a new label.
        Add it to our _completed_thoughts list.
        N)r¦   ra   rŸ   Úappendrž   )r+   r„   Úthoughts      r   Ú_complete_current_thoughtz2StreamlitCallbackHandler._complete_current_thoughtW  s<   € ð ×/Ñ/Ó1ˆØ×Ñ˜Ô%Ø× Ñ ×'Ñ'¨Ô0Ø $ˆÕr   c                óÌ   — | j                   €<t        | j                  | j                  | j                  | j
                  ¬«      | _         | j                   j                  ||«       y )N)rI   r?   rK   rJ   )rž   r=   rœ   r¢   r£   r¤   rY   )r+   rW   rX   r^   s       r   rY   z%StreamlitCallbackHandler.on_llm_start`  sY   € ð × Ñ Ð(Ü$.Ø!%×!7Ñ!7Ø×2Ñ2Ø%)×%FÑ%FØ×-Ñ-ô	%ˆDÔ!ð 	×Ñ×*Ñ*¨:°wÕ?r   c                óF   —  | j                  «       j                  |fi |¤Ž y rV   )r¦   r_   r\   s      r   r_   z)StreamlitCallbackHandler.on_llm_new_tokenp  s    € Ø8ˆ×%Ñ%Ó'×8Ñ8¸ÑIÀ&ÓIr   c                óF   —  | j                  «       j                  |fi |¤Ž y rV   )r¦   rd   rb   s      r   rd   z#StreamlitCallbackHandler.on_llm_ends  s    € Ø2ˆ×%Ñ%Ó'×2Ñ2°8ÑF¸vÓFr   c                óF   —  | j                  «       j                  |fi |¤Ž y rV   )r¦   rj   rg   s       r   rj   z%StreamlitCallbackHandler.on_llm_errorv  s    € Ø4ˆ×%Ñ%Ó'×4Ñ4°UÑE¸fÓEr   c                óH   —  | j                  «       j                  ||fi |¤Ž y rV   )r¦   rr   )r+   rW   r%   r^   s       r   rr   z&StreamlitCallbackHandler.on_tool_starty  s$   € ð 	6ˆ×%Ñ%Ó'×5Ñ5°jÀ)ÑVÈvÓVr   c                ól   —  | j                  «       j                  ||||fi |¤Ž | j                  «        y rV   )r¦   ry   r¬   rt   s         r   ry   z$StreamlitCallbackHandler.on_tool_end~  s=   € ð 	4ˆ×%Ñ%Ó'×3Ñ3Ø�EÐ-¨zñ	
Ø=Cò	
ð 	×&Ñ&Õ(r   c                óF   —  | j                  «       j                  |fi |¤Ž y rV   )r¦   r{   rg   s       r   r{   z&StreamlitCallbackHandler.on_tool_error‹  s    € Ø5ˆ×%Ñ%Ó'×5Ñ5°eÑF¸vÓFr   c                óH   —  | j                  «       j                  ||fi |¤Ž y rV   )r¦   r   r}   s       r   r   z(StreamlitCallbackHandler.on_agent_actionŽ  s$   € ð 	8ˆ×%Ñ%Ó'×7Ñ7¸ÀÑPÈÓPr   c                ó’   — | j                   �;| j                   j                  | j                  j                  «       «       d | _         y y rV   )rž   ra   r¤   r8   )r+   Úfinishrv   r^   s       r   Úon_agent_finishz(StreamlitCallbackHandler.on_agent_finish“  sE   € ð × Ñ Ð,Ø×!Ñ!×*Ñ*Ø×%Ñ%×CÑCÓEôð %)ˆDÕ!ð	 -r   )rI   r   r–   Úintr—   r:   r˜   r:   r™   zLLMThoughtLabeler | Noner9   r…   )r9   r=   )r9   zLLMThought | NonerV   r‘   )rW   r†   rX   r‡   r^   r   r9   r…   rˆ   r‰   rŠ   rŒ   r�   rŽ   r�   )r¶   r   rv   r�   r^   r   r9   r…   )r   r   r   r	   rL   r¦   r¨   r¬   rY   r_   rd   rj   rr   ry   r{   r   r·   r    r   r   r”   r”     sŠ  „ ÙÐAÓBð
 '(Ø$)Ø,1Ø48ñ0Gà(ð0Gð !$ð	0Gð
 "ð0Gð &*ð0Gð 2ð0Gð 
ò0Gó Cð0Gód%óô%ð@Ø(ð@Ø3<ð@ØHKð@à	ó@ó JóGóFðWØ(ðWØ58ðWØDGðWà	óWð !Ø)-Ø!%ð)àð)ð ð)ð 'ð	)ð
 ð)ð ð)ð 
ó)óGð 8<ðQØ!ðQØ*4ðQØGJðQà	óQð 8<ð)Ø!ð)Ø*4ð)ØGJð)à	ô)r   r”   )r   r#   r9   r#   )r;   Ú
__future__r   r‚   Úenumr   Útypingr   r   r   Úlangchain.callbacks.baser   Ústreamlit.runtime.metrics_utilr	   Úlangchain.schemar
   r   r   Ústreamlit.delta_generatorr   Ú/streamlit.elements.lib.mutable_status_containerr   r   r1   r   r"   r(   r=   r”   r    r   r   ú<module>rÁ      s†   ðñõ8 #ã Ý ß 1Ñ 1õõ :á÷ñ õ 9ÝOó&ð Ð ô�dô ô�ô ÷
/ñ /÷dB
ñ B
ôJE)Ð2õ E)r   