
    iM)                        d dl Z d dlZd dlZd dlZd dlmZmZmZmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZmZ  ede       dZd	efd
ZdedefdZdee   dee   fdZdee   defdZd Zdedeeeef   fdZ G d d      Zy)    N)OptionalDictUnionTupleList)urlsplit)_types)version)warnsimplefilteralwaysz0.5.23
suggestionc                 2    d|  }t        |t        d       y )NzfThe `langchainhub sdk` is deprecated.
Please use the `langsmith sdk` instead:
  pip install langsmith
   )
stacklevel)r   DeprecationWarning)r   messages     ^/opt/tws/multiagentes-mcp/cliente-mcp/venv/lib/python3.12/site-packages/langchainhub/client.pydeprecation_warningr      s'    	$ ,	  	$3    urlreturnc                    	 t        |       j                  j                  d      d   }t        j                  |      }|dk(  xs$ |j                  d      xs |j                  d      S # t        j                  $ r Y yw xY w)zCheck if the URL is localhost.

    Parameters
    ----------
    url : str
        The URL to check.

    Returns
    -------
    bool
        True if the URL is localhost, False otherwise.
    :r   z	127.0.0.1z0.0.0.0z::F)r   netlocsplitsocketgethostbyname
startswithgaierror)r   r   ips      r   _is_localhostr"      ss    #%%++C03!!&)[ SBMM)$<Sd@SS?? s   A&A) )A?>A?api_keyc                    | xsC t        j                  d      xs, t        j                  d      xs t        j                  d      } | | j                         sy | j                         j                  d      j                  d      xs d S )NLANGCHAIN_HUB_API_KEYLANGCHAIN_API_KEYLANGSMITH_API_KEY"')osgetenvstripr#   s    r   _get_api_keyr.   -   s{     	*99,-	*99()	* 99()	  gmmo==?  %++C08D8r   api_urlc                     d}| | nt        j                  d|      }|j                         st        d      |j                         j                  d      j                  d      j	                  d      S )Nzhttps://api.hub.langchain.comLANGCHAIN_HUB_API_URLz%LangChain Hub API URL cannot be emptyr(   r)   /)r*   r+   r,   
ValueErrorrstrip)r/   default_api_url_api_urls      r   _get_api_urlr7   9   sq    5O  	YY#
  >>@AA>>!!#&,,S188==r   c                 `    t        j                  |       }t        j                  |      }||k\  S N)r
   parse)current_versiontarget_versioncurrenttargets       r   is_version_greater_or_equalr?   H   s)    mmO,G]]>*Ffr   
identifierc                     | }d}d| v r| j                  dd      \  }}d|vrd||fS |j                  dd      \  }}|||fS )zp
    Parses a string in the format of `owner/repo:commit` and returns a tuple of
    (owner, repo, commit).
    latestr      r2   -)r   )r@   
owner_repocommitownerrepos        r   parse_owner_repo_commitrI   N   se    
 JF
j'--c15
F
*J&&""3*KE4$r   c                      e Zd ZdZd,dddee   dee   fdZd Zed        Z	ed	efd
       Z
d ZdefdZd-dedefdZdefdZddddededefdZd-dededefdZdefdZdefdZded	ee   fdZdddddedee   dee   deee      fd Zd!d"dd#ded$ed%ee   d&ed'ef
d(Zd)ed	ej4                  fd*Zd)efd+Zy).Clientz(
    An API Client for LangChainHub
    Nr-   r/   r#   c                D    t        |      | _        t        |      | _        y r9   )r.   r#   r7   r/   )selfr/   r#   s      r   __init__zClient.__init__d   s    #G,#G,r   c                 @    i }| j                   | j                   |d<   |S )Nz	x-api-keyr-   )rM   headerss     r   _get_headerszClient._get_headersh   s$    <<##'<<GK r   c                 f    t        j                  | j                   d      j                         }|S )Nz/info)requestsgetr/   jsonrM   ress     r   _get_ls_infozClient._get_ls_infon   s)    lldll^512779
r   r   c                     t        | j                        rd}|S d| j                  j                  dd      d   v rd}|S d| j                  j                  dd      d   v rd	}|S d
}|S )zThe web host url.zhttp://localhostbeta.rC   )maxsplitr   z https://beta.smith.langchain.comdevzhttps://dev.smith.langchain.comzhttps://smith.langchain.com)r"   r/   r   )rM   links     r   	_host_urlzClient._host_urls   s     &%D  t||))#):1==5D
 	 dll((q(9!<<4D  1Dr   c                     t        j                  | j                   d| j                               }|j	                          |j                         S )Nz	/settingsrP   rS   rT   r/   rQ   raise_for_statusrU   rV   s     r   get_settingszClient.get_settings   sD    ll||nI&%%'
 	xxzr   tenant_handlec                     t        j                  | j                   d| j                         d|i      }|j	                          |j                         S )Nz/settings/handlere   rP   rU   rS   postr/   rQ   rc   rU   )rM   re   rW   s      r   set_tenant_handlezClient.set_tenant_handle   sM    mm||n,-%%'!=1

 	xxzr   limitoffsetc                     t        d       t        j                  | j                   d| d| | j	                               }|j                          |j                         S )NzUse the `list_prompts` method.z/repos?limit=&offset=ra   )r   rS   rT   r/   rQ   rc   rU   )rM   rk   rl   rW   s       r   
list_reposzClient.list_repos   sW    <=ll||nM%A%%'
 	xxzr   repo_full_namec                     t        j                  | j                   d| | j                               }|j	                          |j                         S )N/repos/ra   rb   rM   rp   rW   s      r   get_repozClient.get_repo   sI    ll||nGN#34%%'
 	xxzr    T)description	is_publicrepo_handlerv   rw   c                    |||d}t        j                  | j                   d| j                         |      }|j	                          |j                         S )N)rx   rw   rv   rr   rg   rh   )rM   rx   rv   rw   rU   rW   s         r   create_repozClient.create_repo   s[     '"&

 mm||nG$%%'

 	xxzr   c           	          t        j                  | j                   d| d| d| | j                               }|j	                          |j                         S )N	/commits/z/?limit=rn   ra   rb   )rM   rp   rk   rl   rW   s        r   list_commitszClient.list_commits   sU    ll||nIn%5XeWHVHU%%'
 	xxzr   c                     t        j                  | j                   d| | j                         ddi      }|j	                          |j                         S )N/likes/likeTrg   rh   rs   s      r   	like_repozClient.like_repo   sP    mm||nGN#34%%'$

 	xxzr   c                     t        j                  | j                   d| | j                         ddi      }|j	                          |j                         S )Nr   r   Frg   rh   rs   s      r   unlike_repozClient.unlike_repo   sP    mm||nGN#34%%'%

 	xxzr   c                 \    | j                  |      }|d   }t        |      dk(  ry |d   d   S )Ncommitsr   commit_hash)r}   len)rM   rp   commits_respr   s       r   _get_latest_commit_hashzClient._get_latest_commit_hash   s:    ((8y)w<1qz-((r   )rv   rw   tagsr   c                    i }|||d<   |||d<   |||d<   t        j                  | j                   d| | j                         |      }|j	                          |j                         S )Nrv   rw   r   rr   rg   )rS   patchr/   rQ   rc   rU   )rM   rp   rv   rw   r   rU   rW   s          r   update_repozClient.update_repo   s     8:""-D  )DDLnn||nGN#34%%'

 	xxzr   rB   F)parent_commit_hashnew_repo_is_publicnew_repo_descriptionmanifest_jsonr   r   r   c                
   t        d       | j                         }|r|d   st        d      t        |      \  }}}	| d| }
	 | j	                  |
      }	t        j                  |      }|d
k(  r| j                  |
      }||d}t        j                  | j                   d|
 | j!                         |      }|j#                          |j                         }|d   d   }|d d }| j$                  d| d| d|d    z   }|S # t
        j                  j                  $ r`}|j                  j                  dk7  r||d   r!|dk7  r|d   |k7  rt        d|d    d|       | j                  |||	       Y d }~'d }~ww xY w)NzUse the `push_prompt` method.re   a  
                  Cannot create public prompt without first creating a LangChain Hub handle.
                  
                  You can add a handle by creating a public prompt at:
                      https://smith.langchain.com/prompts
                  
                  This is a workspace-level handle and will be associated with all of your workspace's public prompts in the LangChain Hub.
                  r2   i  rD   zTenant z is not the owner of repo )rw   rv   rB   )parent_commitmanifestr|   rg   rF   r      z	/prompts/z?organizationId=id)r   rd   r3   rI   rt   rS   
exceptions	HTTPErrorresponsestatus_coderz   rU   loadsr   ri   r/   rQ   rc   r_   )rM   rp   r   r   r   r   settingsrG   rx   _	full_repoemanifest_dictrequest_dictrW   r   
short_hashr   s                     r   pushzClient.push   s    	;<$$&O, 	 	 !8 G{AgQ{m,		i(A( 

=1)!%!=!=i!H);Wmm||nIi[1%%'

 	hhj(mM2 !_
NN+a
|3CHTNCSTU 	 
I "",, 	zz%%, )SL_-6 h788RSaRbc  ,0   	s   D F"AE==Fowner_repo_commitc           	         t        d       | j                  }|d   }t        |t              }t	        |      \  }}}|s*||dk(  r#| j                  | d|       }|t        d      t        j                  | j                   d| d| d| | j                               }|j                          |j                         }	||d	|	S )
NUse the `pull_prompt` method.r
   )r;   r<   rB   r2   zNo commits foundr|   ra   )rG   rH   )r   rX   r?   LS_VERSION_WITH_OPTMIZATIONrI   r   r3   rS   rT   r/   rQ   rc   rU   )
rM   r   info
ls_versionuse_optimizationrG   rH   r   rW   results
             r   	pull_repozClient.pull_repo,  s    ;<  )_
6&7R
 $;;L#M t["kX&="::eWAdV;LM&$%788ll||nIeWAdV1[MB%%'
 	777r   c                 j    t        d       | j                  |      }t        j                  |d         S )Nr   r   )r   r   rU   dumps)rM   r   res_dicts      r   pullzClient.pullD  s.    ;<>>"34zz(:.//r   r9   )d   r   )__name__
__module____qualname____doc__r   strrN   rQ   propertyrX   r_   rd   rj   intro   rt   boolrz   r}   r   r   r   r   r   r   r	   Repor   r    r   r   rK   rK   _   s   -RV - -(3- -   
3 
 
s  3 s  79D03EI 3 s #  # )c )hsm ) &*$($( c]	
 D> tCy!8 -5#($&@@ @
 %SM@ !@ "@D83 86;; 800c 0r   rK   )rU   r*   rS   r   typingr   r   r   r   r   urllib.parser   langchainhubr	   	packagingr
   warningsr   r   r   r   r   r   r   r"   r.   r7   r?   rI   rK   r   r   r   <module>r      s     	   5 5 !   ' X) *& 4C 4s t *	9(3- 	9HSM 	9>(3- >C > c3m0D "h0 h0r   