
    g+                       d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
 d dlZd dlmZmZ d dlmZ d dlmZ d dlmZ er$d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlm Z   ed       G d d             Z! ed       G d d             Z" ed       G d d             Z#e
e"e#f   Z$e G d d             Z%	  G d dejL                        Z'	 	 	 	 	 	 	 	 ddZ(	 	 	 	 ddZ)y)    )annotationsN)	dataclass)TYPE_CHECKINGAnyLiteralUnion)runtimeutilshow_deprecation_warning)CacheReplayClosureError)in_cached_function)Iterator)FunctionType)MessageDeltaGenerator)Block)	CacheTypeT)frozenc                  ,    e Zd ZU ded<   ded<   ded<   y)MediaMsgDatabytes | strmediastrmimetypemedia_idN__name__
__module____qualname____annotations__     d/var/www/openai/venv/lib/python3.12/site-packages/streamlit/runtime/caching/cached_message_replay.pyr   r   )   s    MMr$   r   c                  H    e Zd ZU dZded<   ded<   ded<   ded<   dZd	ed
<   y)ElementMsgDatazAn element's message and related metadata for
    replaying that element's function call.

    media_data is filled in iff this is a media element (image, audio, video).
    r   
delta_typer   messageid_of_dg_called_onreturned_dgs_idNzlist[MediaMsgData] | None
media_data)r   r    r!   __doc__r"   r,   r#   r$   r%   r'   r'   0   s+     O,0J)0r$   r'   c                  ,    e Zd ZU ded<   ded<   ded<   y)BlockMsgDatar   r)   r   r*   r+   Nr   r#   r$   r%   r/   r/   ?   s    Nr$   r/   c                  :    e Zd ZU dZded<   ded<   ded<   ded<   y	)
CachedResultz}The full results of calling a cache-decorated function, enough to
    replay the st functions called while executing it.
    r   valuezlist[MsgData]messagesr   main_id
sidebar_idN)r   r    r!   r-   r"   r#   r$   r%   r1   r1   I   s     JLOr$   r1   c                      e Zd ZdZd
dZddZej                  dd       Z	 	 	 	 	 	 	 	 	 	 	 	 ddZ		 	 	 	 	 	 	 	 	 	 ddZ
ddZ	 	 	 	 	 	 	 	 ddZy	)CachedMessageReplayContextzA utility for storing messages generated by `st` commands called inside
    a cached function.

    Data is stored in a thread-local object, so it's safe to use an instance
    of this class across multiple threads.
    c                J    g | _         g | _        g | _        g | _        || _        y N)_cached_message_stack_seen_dg_stack_most_recent_messages_media_data_cache_type)self
cache_types     r%   __init__z#CachedMessageReplayContext.__init__|   s*    :<".046"/1%r$   c                ,    t        j                  |       S r9   )r
   repr_)r?   s    r%   __repr__z#CachedMessageReplayContext.__repr__   s    zz$r$   c              #  :  K   | j                   j                  g        | j                  j                  t                      d}t	        j
                         rd}t	        j                  d       	 d | j                   j                         | _        | j                  j                          |st	        j                  d       yy# | j                   j                         | _        | j                  j                          |st	        j                  d       w w xY ww)zContext manager that should wrap the invocation of a cached function.
        It allows us to track any `st.foo` messages that are generated from inside the
        function for playback during cache retrieval.
        FTN)r:   appendr;   setr   getpopr<   )r?   funcnested_calls      r%   calling_cached_functionz2CachedMessageReplayContext.calling_cached_function   s      	""))"-""35)!!#K 	t$	.)-)C)C)G)G)ID&##% #&&u-  *.)C)C)G)G)ID&##% #&&u- s    A,D/C 3ADADDc                N   t        j                         syt        | j                        dk\  rO| j	                  ||      }| j
                  }t        |||||      }| j                  D ]  }	|	j                  |        g | _        | j                  D ]  }
|
j                  |        y)zRecord the element protobuf as having been produced during any currently
        executing cached functions, so they can be replayed any time the function's
        execution is skipped because they're in the cache.
        N   )
r	   existslenr:   select_dg_to_saver=   r'   rF   r;   add)r?   r(   element_protoinvoked_dg_id
used_dg_idreturned_dg_id
id_to_saver,   element_msg_datamsgsss              r%   save_element_messagez/CachedMessageReplayContext.save_element_message   s     ~~t))*a///zJJ))J-  22,- 3
 $$AEE.! %r$   c                    | j                  ||      }| j                  D ]  }|j                  t        |||               | j                  D ]  }|j                  |        y r9   )rQ   r:   rF   r/   r;   rR   )r?   block_protorT   rU   rV   rW   rY   rZ   s           r%   save_block_messagez-CachedMessageReplayContext.save_block_message   sW     ++M:F
..DKK[*nMN /$$AEE.! %r$   c                \    t        | j                        dkD  r|| j                  d   v r|S |S )a  Select the id of the DG that this message should be invoked on
        during message replay.

        See Note [DeltaGenerator method invocation]

        invoked_id is the DG the st function was called on, usually `st._main`.
        acting_on_id is the DG the st function ultimately runs on, which may be different
        if the invoked DG delegated to another one because it was in a `with` block.
        r   )rP   r;   )r?   
invoked_idacting_on_ids      r%   rQ   z,CachedMessageReplayContext.select_dg_to_save   s6     t""#a'LD<O<OPR<S,Sr$   c                P    | j                   j                  t        |||             y r9   )r=   rF   r   )r?   
image_datar   image_ids       r%   save_image_dataz*CachedMessageReplayContext.save_image_data   s!     	Z8 LMr$   N)r@   r   )returnr   )rJ   r   rg   zIterator[None])r(   r   rS   r   rT   r   rU   r   rV   r   rg   None)
r]   r   rT   r   rU   r   rV   r   rg   rh   )ra   r   rb   r   rg   r   )rd   r   r   r   re   r   rg   rh   )r   r    r!   r-   rA   rD   
contextlibcontextmanagerrL   r[   r^   rQ   rf   r#   r$   r%   r7   r7   t   s    &  . .2"""" "" 	""
 "" "" 
""H"" " 	"
 " 
"N%N14N@CN	Nr$   r7   c                   ddl m} | j                  t        j                  | j
                  t        j                  i}	 | j                  D ]  }t        |t              r|j                  ^|j                  D ]O  }t        j                         j                  j                  |j                  |j                   |j"                         Q ||j$                     }|j'                  |j(                  |j*                        }t        ||      s|||j,                  <   t        |t.              s||j$                     }|j1                  |j*                        }	|	||j,                  <    y# t2        $ r}
t5        ||      |
d}
~
ww xY w)a  Replay the st element function calls that happened when executing a
    cache-decorated function.

    When a cache function is executed, we record the element and block messages
    produced, and use those to reproduce the DeltaGenerator calls, so the elements
    will appear in the web app even when execution of the function is skipped
    because the result was cached.

    To make this work, for each st function call we record an identifier for the
    DG it was effectively called on (see Note [DeltaGenerator method invocation]).
    We also record the identifier for each DG returned by an st function call, if
    it returns one. Then, for each recorded message, we get the current DG instance
    corresponding to the DG the message was originally called on, and enqueue the
    message using that, recording any new DGs produced in case a later st function
    call is on one of them.
    r   r   N)streamlit.delta_generatorr   r4   st_mainr5   sidebarr3   
isinstancer'   r,   r	   get_instancemedia_file_mgrrR   r   r   r   r*   _enqueuer(   r)   r+   r/   _blockKeyErrorr   )resultr@   cached_funcr   returned_dgsmsgdatadgmaybe_dgnew_dgexs              r%   replay_cached_messagesr      s5   & 9 	2::/LG??C#~.>>- #,,.==AA JJt}} !/ "#"8"89;;s~~s{{Ch78@L!4!45C.!#"8"893;;/4:S001 #  G%j+>BFGs$   CE( 
 E( +<E( (	F1E>>Fc                "    t        d|  d       y )NzThe cached widget replay feature was removed in 1.38. The `experimental_allow_widgets` parameter will also be removed in a future release. Please remove the `experimental_allow_widgets` parameter from the `@st.a  ` decorator and move all widget commands outside of cached functions.

To speed up your app, we recommend moving your widgets into fragments. Find out more about fragments in [our docs](https://docs.streamlit.io/develop/api-reference/execution-flow/st.fragment). 

If you have a specific use-case that requires the `experimental_allow_widgets` functionality, please tell us via an [issue on Github](https://github.com/streamlit/streamlit/issues).r   )	decorators    r%   show_widget_replay_deprecationr     s$     	 # $L		Lr$   )rv   r1   r@   r   rw   r   rg   rh   )r   z'Literal['cache_data', 'cache_resource']rg   rh   )*
__future__r   ri   	threadingdataclassesr   typingr   r   r   r   	streamlitrm   r	   r
   streamlit.deprecation_utilr   &streamlit.runtime.caching.cache_errorsr   7streamlit.runtime.scriptrunner_utils.script_run_contextr   collections.abcr   typesr   google.protobuf.messager   rl   r   streamlit.proto.Block_pb2r   $streamlit.runtime.caching.cache_typer   r   r'   r/   MsgDatar1   localr7   r   r   r#   r$   r%   <module>r      s   #   ! 5 5  # ? J ("/8/> $   $1 1 1 $   ,
-   >oN oNd+G+G&/+G>J+G	+G\6	r$   