
    j                         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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                 "   | j         }t          |          dk    rdS g }|D ]Y}|j        xdk    r |                    |j                   +xdk    r5 |                    d|j         dt          |j                   d           fxdk    r5 |                    d	|j         dt          |j                   d           d
k    r|j        }t          |d          r5t          |j        t                    r|                    |j                   t          |d          rUt          |j        t                    r;|                    d|j         d|j         dt          |j                   d           Y[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 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        g/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/huggingface_hub/inference/_mcp/utils.pyformat_resultr'      s	    nG
7||q~!#O  i&&ty1111&&Sdm S S?OPTPY?Z?Z S S S   
 &&Sdm S S?OPTPY?Z?Z S S S   
 =8V,, HM31O1O #**8=9999Xv.. :hmS3Q3Q #**WHL W WX=N W W,X];;W W W    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   J   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_dir\   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   tupler   r   r!   r	   
expanduseris_filer<   r=   r>   is_dirr   r
   	Exceptionr   )r3   rD   pathrepo_direrrs        r&   _load_agent_configrQ   Y   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   huggingface_hubr   huggingface_hub.errorsr   	constantsr	   r
   r   r   r   r   mcp	mcp_typesr!   r'   intr   rI   rQ    r(   r&   <module>r\      s?           * * * * * * * * - - - - - - 5 5 5 5 5 5 X X X X X X X X X X X X        '&&&&&&1&4 1& 1& 1& 1& 1&h0 0 0 0 0 0)8C= )U;QT;U5V ) ) ) ) ) )r(   