o
    RTh3                     @   sx   d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
mZ d dlmZ dd ZG dd	 d	eeZG d
d dZdS )    N)hexlify)Enum)AnyDictIteratorOptionalTupleUnion)display_functionsc                  C   s`   z%ddl m}  |  }|du rW dS |jj}|dkrW dS |dkr#W dS W dS  ty/   Y dS w )z=Determine (roughly) if the client can handle display updates.r   )get_ipythonNFZMQInteractiveShellTTerminalInteractiveShell)IPythonr   	__class____name__ImportError)r   ipythonname r   P/home/air/segue/gemini/backup/venv/lib/python3.10/site-packages/genai/display.pycan_handle_display_updates	   s   r   c                   @   s   e Zd ZdZdZdZdZdS )Stagez The stage of feedback generationstarting
generatingfinishedN)r   
__module____qualname____doc__STARTING
GENERATINGFINISHEDr   r   r   r   r       s
    r   c                   @   s   e Zd ZdZddedee ddfddZd	eddfd
dZde	e ddfddZ
dddZdddZdefddZdeeeeeeef f f fddZedefddZejdeddfddZedee fddZejdeddfddZdS ) GenaiMarkdownag  
    A class for displaying a markdown string that can be updated in place.

    This class provides an easy way to create and update a Markdown string in Jupyter Notebooks. It
    supports real-time updates of Markdown content which is useful for emitting ChatGPT suggestions
    as they are generated.

    Attributes:
        message (str): The Markdown string to display
        stage (Optional[Stage]): The current stage of feedback generation

    Example:
        >>> from genai.display import GenaiMarkdown, Stage
        ...
        >>> markdown = UpdatingMarkdown()
        >>> markdown.append("Hello")
        >>> markdown.append(" world!")
        >>> markdown.display()
        # Displays "Hello world!" in the notebook
        ...
        >>> markdown.append(" This is an update!")
        # Displays "Hello world! This is an update!" in the notebook
        ...
        >>> def text_generator():
        ...    yield " 1"
        ...    yield " 2"
        ...    yield " 3"
        ...
        >>> markdown.consume(text_generator())
        # Displays "Hello world! This is an update! 1 2 3" in the notebook
     Nmessagestagereturnc                 C   s&   || _ ttdd| _|| _d S )N   ascii)_messager   osurandomdecode_display_id_stage)selfr#   r$   r   r   r   __init__I   s   
zGenaiMarkdown.__init__deltac                 C   s   |  j |7  _ d S N)r#   )r.   r0   r   r   r   appendN   s   zGenaiMarkdown.appenddelta_generatorc                 C   s   |D ]}|  | qd S r1   )r2   )r.   r3   r0   r   r   r   consumeQ   s   zGenaiMarkdown.consumec                 C   s   t j| | jd dS )zFDisplay the `UpdatingMarkdown` with a display ID for receiving updates)
display_idNr
   displayr,   r.   r   r   r   r7   U   s   zGenaiMarkdown.displayc                 C   s   t j| | jdd dS )zForce an update to all displaysT)r5   updateNr6   r8   r   r   r   update_displaysY   s   zGenaiMarkdown.update_displaysc                 C   s   | j }|d u s|dkrd}|S )Nr"    r(   )r.   r#   r   r   r   __repr__]   s   zGenaiMarkdown.__repr__c                 C   s>   | j }|d u s|dkrd}| jd u r|S dd| jii}||fS )Nr"   r;   genair$   )r(   r-   )r.   r#   metadatar   r   r   _repr_markdown_c   s   
zGenaiMarkdown._repr_markdown_c                 C      | j S r1   r<   r8   r   r   r   r#   t      zGenaiMarkdown.messagevaluec                 C      || _ |   d S r1   )r(   r:   )r.   rC   r   r   r   r#   x      c                 C   rA   r1   )r-   r8   r   r   r   r$   }   rB   zGenaiMarkdown.stagec                 C   rD   r1   )r-   r:   )r.   r$   r   r   r   r$      rE   )r"   N)r%   N)r   r   r   r   strr   r   r/   r2   r   r4   r7   r:   r=   r	   r   r   r   r@   propertyr#   setterr$   r   r   r   r   r!   (   s"     

&r!   )r)   binasciir   enumr   typingr   r   r   r   r   r	   IPython.corer
   r   rF   r   r!   r   r   r   r   <module>   s     