
    i                         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ZdedefdZdee   deeee   f   fdZy)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                 8   | j                   }t        |      dk(  ryg }|D ]g  }|j                  xdk(  r |j                  |j                         1xdk(  r7 |j                  d|j
                   dt        |j                         d       mxdk(  r7 |j                  d	|j
                   dt        |j                         d       d
k(  s|j                  }t        |d      r6t        |j                  t              r|j                  |j                         t        |d      s
t        |j                  t              s&|j                  d|j                   d|j
                   dt        |j                         d       j dj                  |      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 Useraudioz[Binary Content: Audio resourceblobz[Binary Content (z): 
)contentlentypeappendr   mimeType_get_base64_sizedatar   hasattr
isinstancestrr   urijoin)r   r   formatted_partsitemr   s        u/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/huggingface_hub/inference/_mcp/utils.pyformat_resultr'      sn    nnG
7|q!#O ii&&tyy1&&-dmm_B?OPTPYPY?Z>[ \R S
 &&-dmm_B?OPTPYPY?Z>[ \R S
 ==8V,HMM31O#**8==9Xv.:hmmS3Q#**+HLL>X=N=N<O P,X]];< =VW1< 99_%%    
base64_strc                     d| v r| j                  d      d   } d}| j                  d      rd}n| j                  d      rd}t        |       dz  dz  |z
  S )	z2Estimate the byte size of a base64-encoded string.,r   r   z==   =      )splitendswithr   )r)   paddings     r&   r   r   J   se     j%%c*1-
G4 			S	!
OaA%//r(   
agent_pathc                    dt         dt        t        t        t           f   fd}| t
        dfS t        |       j                         }|j                         r't        j                  |j                  d            dfS |j                         r ||      S 	 t        t        t        |  dd	            } ||| z        S # t        $ r}t        d
|  d      |d}~ww xY w)zLoad server config and prompt.	directoryr   c                    | t         z  }|j                         st        d|  d      t        j                  |j                  d            }d }t        D ].  }| |z  }|j                         s|j                  d      } ||fS  ||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   )r5   cfg_fileconfigpromptfilenameprompt_files         r&   	_read_dirz%_load_agent_config.<locals>._read_dir\   s    . #&@Ko$pqq"jj););W);)MN $( 	H#h.K!!#$...@v~	
 v~r(   Nr7   r8   z/*dataset)repo_idallow_patterns	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_filer<   r=   r>   is_dirr   r
   	Exceptionr   )r3   rD   pathrepo_direrrs        r&   _load_agent_configrQ   Y   s    T eK#,F&G  d""
&&(D||~zz$..'.:;TAA{{}'",R0#
 J.//  j\  "_  `
	s   (C 	C
CC)__doc__r<   pathlibr   typingr   r   huggingface_hubr   huggingface_hub.errorsr   	constantsr	   r
   r   r   r   r   mcp	mcp_typesr!   r'   intr   rI   rQ    r(   r&   <module>r\      sz      * - 5 X X  &1&4 1& 1&h0 0 0)8C= )U;QT;U5V )r(   