
    A'h]                     N   d dl mZmZ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mZ  G d de      Z G d	 d
e      Zeeef   Zdddddddedeeef   dee   deeeef      dee	   dededefdZdddededefdZdeee   ef   deee   ef   dee   fdZy)    )AnyLiteralOptionalUnioncast)uuid4)
AnyMessage)	TypedDict)CONFCONFIG_KEY_SEND)
get_configget_stream_writerc                   ^    e Zd ZU dZed   ed<   eed<   eed<   eeef   ed<   eeef   ed<   y)		UIMessagea  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.
    uitypeidnamepropsmetadataN)	__name__
__module____qualname____doc__r   __annotations__strdictr        Y/home/kushmeetdev/Regenta/Chatbot/venv/lib/python3.12/site-packages/langgraph/graph/ui.pyr   r      s9     $-G
IS>38nr   r   c                   ,    e Zd ZU dZed   ed<   eed<   y)RemoveUIMessagea:  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.
    	remove-uir   r   N)r   r   r   r   r   r   r   r   r   r    r"   r"       s     +
Gr   r"   Nr   F)r   r   message	state_keymerger   r   r   r   r$   r%   r&   returnc                   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	}
 ||
        |t           t           ||
fg       |
S )
aw  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.
            Defaults to "ui".

    Returns:
        The created UI message.

    Example:

    .. code-block:: python

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

    Nr   r   run_idtagsrun_name)r&   r)   r*   r   
message_id)r   r   r   r   r   )r   r   
isinstancer   gethasattrr   r   r   r   r   )r   r   r   r   r$   r%   r&   writerconfigr,   evts              r    push_ui_messager3   2   s    L  F\FJgt$ T*JWd# J  CLjj40JJvt,JJz40	

 ~2
 .8j)R
C 3K!F4L!Is#3"45Jr   )r%   c                z    t               }t               }d| d} ||        |t           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:

    .. code-block:: python

        delete_ui_message("message-123")

    r#   )r   r   )r   r   r   r   )r   r%   r0   r1   r2   s        r    delete_ui_messager5   w   sD    (  F\F$/r:C
3K!F4L!Is#3"45Jr   leftrightc                 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:

    .. code-block:: python

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

    r   r   r#   r   r&   Fr   zCAttempting to delete an UI message with an ID that doesn't exist ('z'))r-   listcopy	enumerater.   setadddiscardr   r   
ValueErrorlenappend)r6   r7   mergedimmerged_by_idids_to_removemsgmsg_idexisting_idxprev_msgs              r    ui_message_reducerrK      s   6 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)typingr   r   r   r   r   uuidr   langchain_core.messagesr	   typing_extensionsr
   langgraph.constantsr   r   langgraph.utils.configr   r   r   r"   AnyUIMessager   r   boolr3   r5   r9   rK   r   r   r    <module>rT      s.   6 6  . ' 5 @	 *i  Y/0 )-$(B
BS>B 		B
 tCH~&B j!B B B BJ 48 # S O >?
\"L0
1?l#\12? 
,?r   