
    9i                        d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZ dZ G d	 d
e      Z G d de      Zeez  Zdddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddddZ	 	 	 	 	 	 ddZy)    )annotations)AnyLiteralcast)uuid4)
AnyMessage)	TypedDict)
get_configget_stream_writer)CONF)	UIMessageRemoveUIMessageAnyUIMessagepush_ui_messagedelete_ui_messageui_message_reducerc                  D    e Zd ZU dZded<   ded<   ded<   ded<   ded	<   y
)r   a  A message type for UI updates in LangGraph.

    This TypedDict represents a UI message that can be sent to update the UI state.
    It contains information about the UI component to render and its properties.

    Attributes:
        type: Literal type indicating this is a UI message.
        id: Unique identifier for the UI message.
        name: Name of the UI component to render.
        props: Properties to pass to the UI component.
        metadata: Additional metadata about the UI message.
    zLiteral['ui']typestridnamedict[str, Any]propsmetadataN__name__
__module____qualname____doc____annotations__     \/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/langgraph/graph/ui.pyr   r      s%     G
Ir"   r   c                  &    e Zd ZU dZded<   ded<   y)r   a:  A message type for removing UI components in LangGraph.

    This TypedDict represents a message that can be sent to remove a UI component
    from the current state.

    Attributes:
        type: Literal type indicating this is a remove-ui message.
        id: Unique identifier of the UI message to remove.
    zLiteral['remove-ui']r   r   r   Nr   r!   r"   r#   r   r   +   s     Gr"   r   NuiF)r   r   message	state_keymergec                  ddl m} t               }t               }	d}
|r>t	        |t
              rd|v r|j                  d      }
nt        |d      r|j                  }
d|xs t        t                     | |||	j                  dd      |	j                  dd      |	j                  dd      d	|xs i |
rd
|
ini d} ||       |r |	t           |   ||fg       |S )a  Push a new UI message to update the UI state.

    This function creates and sends a UI message that will be rendered in the UI.
    It also updates the graph state with the new UI message.

    Args:
        name: Name of the UI component to render.
        props: Properties to pass to the UI component.
        id: Optional unique identifier for the UI message.
            If not provided, a random UUID will be generated.
        metadata: Optional additional metadata about the UI message.
        message: Optional message object to associate with the UI message.
        state_key: Key in the graph state where the UI messages are stored.
        merge: Whether to merge props with existing UI message (True) or replace
            them (False).

    Returns:
        The created UI message.

    Example:
        ```python
        push_ui_message(
            name="component-name",
            props={"content": "Hello world"},
        )
        ```

    r   CONFIG_KEY_SENDNr   r%   run_idtagsrun_name)r(   r,   r-   r   
message_id)r   r   r   r   r   )langgraph._internal._constantsr+   r   r
   
isinstancedictgethasattrr   r   r   r   )r   r   r   r   r&   r'   r(   r+   writerconfigr/   evts               r#   r   r   =   s    L ? F\FJgt$ T*JWd# J  CLjj40JJvt,JJz40	

 ~2
 .8j)R
C 3K%t_%	3'7&89Jr"   )r'   c               ~    ddl m} t               }t               }d| d} ||        |t           |   ||fg       |S )a  Delete a UI message by ID from the UI state.

    This function creates and sends a message to remove a UI component from the current state.
    It also updates the graph state to remove the UI message.

    Args:
        id: Unique identifier of the UI component to remove.
        state_key: Key in the graph state where the UI messages are stored. Defaults to "ui".

    Returns:
        The remove UI message.

    Example:
        ```python
        delete_ui_message("message-123")
        ```

    r   r*   	remove-ui)r   r   )r0   r+   r   r
   r   )r   r'   r+   r5   r6   r7   s         r#   r   r      sG    & ? F\F$/r:C
3K!F4L!Is#3"45Jr"   c                8   t        | t              s| g} t        |t              s|g}| j                         }t        |      D ci c]  \  }}|j	                  d      | }}}t               }|D ]  }|j	                  d      }|j	                  |      x}	|j	                  d      dk(  r|j                  |       M|j                  |       t        t        |      j	                  di       j	                  dd      r$||	   }
|j                         }i |
d   |d   |d<   |||	<   |j	                  d      dk(  rt        d| d	      t        |      ||<   |j                  |        |D cg c]  }|j	                  d      |vs| }}|S c c}}w c c}w )
a  Merge two lists of UI messages, supporting removing UI messages.

    This function combines two lists of UI messages, handling both regular UI messages
    and `remove-ui` messages. When a `remove-ui` message is encountered, it removes any
    UI message with the matching ID from the current state.

    Args:
        left: First list of UI messages or single UI message.
        right: Second list of UI messages or single UI message.

    Returns:
        Combined list of UI messages with removals applied.

    Example:
        ```python
        messages = ui_message_reducer(
            [{"type": "ui", "id": "1", "name": "Chat", "props": {}}],
            {"type": "remove-ui", "id": "1"},
        )
        ```

    r   r   r9   r   r(   Fr   zCAttempting to delete an UI message with an ID that doesn't exist ('z'))r1   listcopy	enumerater3   setadddiscardr   r   
ValueErrorlenappend)leftrightmergedimmerged_by_idids_to_removemsgmsg_idexisting_idxprev_msgs              r#   r   r      s   4 dD!veT" YY[F/8/@Atq!AEE$KNALAEM (,,V44LAwwv+-!!&)%%f-	3'++J;??O%l3H((*C#Hhw&7#H3w<#HCL'*|$wwv+- YZ`Yaacd  $'v;L MM#-0  DA155;m#CaDFDM9 B6 Es   F0F	F)r   r   r   r   r   
str | Noner   zdict[str, Any] | Noner&   zAnyMessage | Noner'   rO   r(   boolreturnr   )r   r   r'   r   rQ   r   )rD   !list[AnyUIMessage] | AnyUIMessagerE   rR   rQ   zlist[AnyUIMessage])
__future__r   typingr   r   r   uuidr   langchain_core.messagesr   typing_extensionsr	   langgraph.configr
   r   langgraph.constantsr   __all__r   r   r   r   r   r   r!   r"   r#   <module>r[      s    " % %  . ' : $	 *i  ?* &*!% E
EE 		E
 $E E E E EP 48 @>
+>,> >r"   