o
    0i                     @   s   d Z 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mZ dd	lmZ er:dd
lmZ dddefddZdedefddZdee deeee f fddZdS )z
Utility functions for MCPClient and Tiny Agents.

Formatting utilities taken from the JS SDK: https://github.com/huggingface/huggingface.js/blob/main/packages/mcp-client/src/ResultFormatter.ts.
    N)Path)TYPE_CHECKINGOptional)snapshot_download)EntryNotFoundError   )DEFAULT_AGENTDEFAULT_REPO_IDFILENAME_CONFIGPROMPT_FILENAMES)AgentConfig)typesresultzmcp_types.CallToolResultreturnc              
   C   s  | j }t|dkrdS g }|D ]s}|jdkr||j q|jdkr3|d|j dt|j d q|jdkrI|d	|j dt|j d q|jd
kr|j}t	|drct
|jtrc||j qt	|drt
|jtr|d|j d|j dt|j d qd|S )a  
    Formats a mcp.types.CallToolResult content into a human-readable string.

    Args:
        result (CallToolResult)
            Object returned by mcp.ClientSession.call_tool.

    Returns:
        str
            A formatted string representing the content of the result.
    r   z[No content]textimagez[Binary Content: Image z, zC bytes]
The task is complete and the content accessible to the UserZaudioz[Binary Content: Audio resourceblobz[Binary Content (z): 
)contentlentypeappendr   ZmimeType_get_base64_sizedatar   hasattr
isinstancestrr   urijoin)r   r   Zformatted_partsitemr    r!   ^/home/kim/smarthome/.venv/lib/python3.10/site-packages/huggingface_hub/inference/_mcp/utils.pyformat_result   s2   



 
r#   
base64_strc                 C   sL   d| v r|  dd } d}| drd}n| drd}t| d d | S )	z2Estimate the byte size of a base64-encoded string.,r   r   z==   =      )splitendswithr   )r$   paddingr!   r!   r"   r   H   s   

r   
agent_pathc              
   C   s   dt dtttt f fdd}| du rtdfS t |  }| r,t	|j
dddfS | r4||S zt tt|  dd	d
}|||  W S  ty\ } z	td|  d|d}~ww )zLoad server config and prompt.	directoryr   c                 S   sn   | t  }| std|  dt|jdd}d }tD ]}| | }| r2|jdd} ||fS q||fS )Nz Config file not found in z$! Please make sure it exists locallyutf-8encoding)r
   existsFileNotFoundErrorjsonloads	read_textr   )r.   Zcfg_fileconfigpromptfilenameZprompt_filer!   r!   r"   	_read_dirZ   s   z%_load_agent_config.<locals>._read_dirNr/   r0   z/*Zdataset)Zrepo_idZallow_patternsZ	repo_typez Agent z} not found in tiny-agents/tiny-agents! Please make sure it exists in https://huggingface.co/datasets/tiny-agents/tiny-agents.)r   tupler   r   r   r   
expanduseris_filer4   r5   r6   is_dirr   r	   	Exceptionr   )r-   r:   pathrepo_direrrr!   r!   r"   _load_agent_configW   s2   
rC   )__doc__r4   pathlibr   typingr   r   Zhuggingface_hubr   Zhuggingface_hub.errorsr   	constantsr   r	   r
   r   r   r   ZmcpZ	mcp_typesr   r#   intr   r;   rC   r!   r!   r!   r"   <module>   s    2&