o
    io                     @   s   d dl Z d dlmZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZmZ G dd deZe Zd	ed
efddZdeded
edB fddZdee d
eeeef dB fddZd
edB fddZdS )    Ndatetime)OptionalUnion)Console)Panel)PANEL_TITLE_ERRORPANEL_TITLE_SUCCESSPIPECAT_CLI_NAMEc                   @   s   e Zd Z			ddee dee dee fddZ			ddee dee dee fddZd	d
 Zdd Z			ddee	ee
f  dee defddZdS )PipecatConsoleNtitletitle_extrasubtitlec              
   C   H   |st  |d urd| nd }| t|d| d|dddd d S )N -  z[bold green]z[/bold green]leftgreenr   r   title_alignsubtitle_alignborder_style)r	   printr   selfmessager   r   r    r   U/home/ubuntu/.local/lib/python3.10/site-packages/pipecatcloud/_utils/console_utils.pysuccess      
zPipecatConsole.successc              
   C   r   )Nr   r   
[bold red][/bold red]r   redr   )r   r   r   r   r   r   r   error'   r   zPipecatConsole.errorc                 C   s   |  d d S )Nz"[yellow]Cancelled by user[/yellow])r   r   r   r   r   cancel<   s   zPipecatConsole.cancelc              
   C   s.   |  tdt ddt dddddd d S )	NzQUnauthorized request / invalid user token.

Please log in again using [bold cyan]z auth login[/bold cyan]r    z  - Unauthorized (401)[/bold red]r   r   r"   r   )r   r   r
   r   r$   r   r   r   unauthorized?   s   

zPipecatConsole.unauthorized	API ErrorF
error_codehide_subtitlec              
   C   s   d}t |tr|dd p|dd p|}|d}n
|r!t|n|}d }|s)d}| td| d| dt |r=d	| nd
 d|sK|rKd| dnd dddd d S )Nz&Unknown error. Please contact support.r#   r   codeTz[red]z"[/red]

[dim]Error message:[/dim]
r    r   r   r!   z<[dim]Docs: https://docs.pipecat.daily.co/agents/error-codes#z[/dim]r   r"   r   )
isinstancedictgetstrr   r   r   )r   r(   r   r)   DEFAULT_ERROR_MESSAGEerror_messager*   r   r   r   	api_errorK   s4   

zPipecatConsole.api_error)NNN)Nr'   F)__name__
__module____qualname__r   r.   r   r#   r%   r&   r   r,   boolr1   r   r   r   r   r      sB    

r   	timestampreturnc              	   C   s   ddl m } z
|| ddW S  ty   Y nw z<| d}t|dkrQ|d drT|d d	d
 d	d dd}|d  d| d}||ddW S W | S W | S  ttfyb   Y | S w )a/  
    Format a timestamp string to a more readable format.
    Handles timestamps with variable microsecond precision.

    Args:
        timestamp (str): The timestamp string in ISO format with microseconds (e.g. "2024-01-01T12:34:56.789Z")

    Returns:
        str: The formatted timestamp string
    r   r   z%Y-%m-%dT%H:%M:%S.%fZz%Y-%m-%d %H:%M:%S.      ZN   0)	r   strptimestrftime
ValueErrorsplitlenendswithljust
IndexError)r6   r   partsmicroseconds
normalizedr   r   r   format_timestampr   s,   
 rJ   created_at_strended_at_strc           	      C   s   |sdS z8t | dd}t |dd}|| }t| }|d }|d d }|d }|dd|dd|dW S  tyF   Y dS w )z1Calculate and format session duration as HH:MM:SSNr;   z+00:00i  <   02d:)r   fromisoformatreplaceinttotal_seconds	Exception)	rK   rL   
created_atended_atdurationrS   hoursminutessecondsr   r   r   format_duration   s   r[   datac                 C   s^   | sdS t | }t| dkr|| d | d fS t| }dd }||d}||d}|||fS )a3  
    Calculate average, 5th percentile, and 95th percentile for a list of numeric data.

    Args:
        data: List of float values to analyze

    Returns:
        Tuple of (average, p5, p95) if data is provided, None if empty list.
        For single values, p5 and p95 will equal the single value.
    Nr:   r   c                 S   sd   | sdS t | d | d }t|}|| }|d t | k r.| | d|  | |d  |  S | | S )Nr   r:   d   )rC   rR   )	data_listpkfcr   r   r   
percentile   s    z)calculate_percentiles.<locals>.percentile   _   )
statisticsmeanrC   sorted)r\   avgsorted_datarc   p5p95r   r   r   calculate_percentiles   s   



rm   c               	      s  ddl } zddlm} |d}W n
 ty   Y dS w d}zZ|  4 I dH D}||I dH }|js@	 W d  I dH  W dS | I dH }|d d }||kr^|W  d  I dH  W S 	 W d  I dH  W dS 1 I dH sqw   Y  W dS  ty   Y dS w )zE
    Check if there are updates available for the CLI from PyPI.
    r   N)versionpipecatcloudz'https://pypi.org/pypi/pipecatcloud/jsoninforn   )	aiohttpimportlib.metadatarn   ImportErrorClientSessionr-   okjsonrT   )rq   get_versioncurrent_versionpypi_urlsessionresponser\   latest_versionr   r   r   cli_updates_available   s2   
4r}   )rf   r   typingr   r   rich.consoler   
rich.panelr   pipecatcloud.clir   r	   r
   r   consoler.   rJ   r[   listfloattuplerm   r}   r   r   r   r   <module>   s   ^$$&