
    wi\                         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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ListOptionalTuple)snapshot_download)EntryNotFoundError   )DEFAULT_AGENTDEFAULT_REPO_IDFILENAME_CONFIGPROMPT_FILENAMES)AgentConfig)typesresultzmcp_types.CallToolResultreturnc                    | j         }t          |          dk    rdS g }|D ]/}|j        dk    r|                    |j                   )|j        dk    r4|                    d|j         dt          |j                   d           h|j        dk    r4|                    d	|j         dt          |j                   d           |j        d
k    r}|j        }t          |d          r|                    |j                   t          |d          r;|                    d|j
         d|j         dt          |j                   d           1d                    |          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/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/huggingface_hub/inference/_mcp/utils.pyformat_resultr'      s    nG
7||q~!#O  9""49----Y'!!""O$- O O;KDI;V;V O O O   
 Y'!!""O$- O O;KDI;V;V O O O   
 Y*$$}Hx(( &&x}55556** &&S S S9J S SN^_g_lNmNm S S S  
 99_%%%    
base64_strc                     d| v r|                      d          d         } d}|                     d          rd}n|                     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   s     j%%c**1-
G4   			S	!	! 
OOaA%//r(   
agent_pathc                 *   dt           dt          t          t          t                   f         fd}| 	t
          dfS t          |                                           }|                                r*t          j	        |
                    d                    dfS |                                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                 6   | t           z  }|                                st          d|  d          t          j        |                    d                    }d }t          D ]3}| |z  }|                                r|                    d          } n4||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_dirZ   s    .   	r#$p$p$p$pqqq"j););W);)M)MNN $( 	 	H#h.K!!## $...@@ 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   rD   pathrepo_direrrs        r&   _load_agent_configrQ   W   sM   T eK#,F&G     d""
&&((D||~~ Bz$..'.::;;TAA{{}} y'",000#  
 
 yJ.///     `j  `  `  `
 
	s   :4C/ /
D9DD)__doc__r<   pathlibr   typingr   r   r   r   huggingface_hubr   huggingface_hub.errorsr	   	constantsr   r   r   r   r   r   mcp	mcp_typesrI   r'   intr   rQ    r(   r&   <module>r\      sK           7 7 7 7 7 7 7 7 7 7 7 7 - - - - - - 5 5 5 5 5 5 X X X X X X X X X X X X        '&&&&&&/&4 /& /& /& /& /&d0 0 0 0 0 0)8C= )U;QT;U5V ) ) ) ) ) )r(   