
    kh                         d Z ddlZddlmZ ddlmZmZ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ListOptionalTuple)snapshot_download)EntryNotFoundError   )DEFAULT_AGENTDEFAULT_REPO_IDFILENAME_CONFIGFILENAME_PROMPT)AgentConfig)typesresultzmcp_types.CallToolResultreturnc                    | j                   }t        |      dk(  ryg }|D ]L  }|j                  dk(  r|j                  |j                         /|j                  dk(  r6|j                  d|j
                   dt        |j                         d       t|j                  dk(  r6|j                  d	|j
                   dt        |j                         d       |j                  d
k(  s|j                  }t        |d      r|j                  |j                         t        |d      s|j                  d|j                   d|j
                   dt        |j                         d       O 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urir   join)r   r   formatted_partsitemr   s        `/var/www/teggl/fontify/venv/lib/python3.12/site-packages/huggingface_hub/inference/_mcp/utils.pyformat_resultr'      sk    nnG
7|q!#O 99""499-YY'!"")$--;KDII;V:W XN O
 YY'!"")$--;KDII;V:W XN O
 YY*$}}Hx(&&x}}56*&&'~S9J9J8K2N^_g_l_lNmMn oR S/8 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   H   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            }| t        z  }|j                         r|j                  d      nd }||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_fileprompts        r&   	_read_dirz%_load_agent_config.<locals>._read_dirZ   s}    . #&@Ko$pqq"jj););W);)MN/1KVK]K]K_ 5 5w 5 Gei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   r   r   r   strr   
expanduseris_filer<   r=   r>   is_dirr   r   	Exceptionr	   )r3   rC   pathrepo_direrrs        r&   _load_agent_configrP   W   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   r   r   huggingface_hubr   huggingface_hub.errorsr	   	constantsr   r   r   r   r   r   mcp	mcp_typesrH   r'   intr   rP    r(   r&   <module>r[      s}      7 7 - 5 W W  &/&4 /& /&d0 0 0%8C= %U;QT;U5V %r(   