Ë
    µŒj·,  ã                   ó~   — d Z ddlZddlmZmZmZmZ ddlZddlZddl	m
Z
 ddlmZmZmZ ddlmZmZ  G d„ de«      Zy)	a  Util that can interact with Zapier NLA.

Full docs here: https://nla.zapier.com/start/

Note: this wrapper currently only implemented the `api_key` auth method for testing
and server-side production use cases (using the developer's connected accounts on
Zapier.com)

For use-cases where LangChain + Zapier NLA is powering a user-facing application, and
LangChain needs access to the end-user's connected accounts on Zapier.com, you'll need
to use oauth. Review the full docs above and reach out to nla@zapier.com for
developer support.
é    N)ÚAnyÚDictÚListÚOptional©Úget_from_dict_or_env)Ú	BaseModelÚ
ConfigDictÚmodel_validator)ÚRequestÚSessionc                   ó,  — e Zd ZU dZeed<   eed<   dZeed<    ed¬«      Zde	eef   fd	„Z
defd
„Zdededede	eef   fd„Z	 	 d(dedee	   dede	fd„Zdedefd„Z	 	 d(dededee	   dedef
d„Z ed¬«      ede	defd„«       «       Zdee	   fd„Zdee	   fd„Z	 d)dededee	   de	fd„Z	 d)dededee	   de	fd„Z	 d)dededee	   de	fd„Z	 d)dededee	   de	fd „Zd!ededefd"„Zd!ededefd#„Z d!ededefd$„Z!d!ededefd%„Z"defd&„Z#defd'„Z$y)*ÚZapierNLAWrapperað  Wrapper for Zapier NLA.

    Full docs here: https://nla.zapier.com/start/

    This wrapper supports both API Key and OAuth Credential auth methods. API Key
    is the fastest way to get started using this wrapper.

    Call this wrapper with either `zapier_nla_api_key` or
    `zapier_nla_oauth_access_token` arguments, or set the `ZAPIER_NLA_API_KEY`
    environment variable. If both arguments are set, the Access Token will take
    precedence.

    For use-cases where LangChain + Zapier NLA is powering a user-facing application,
    and LangChain needs access to the end-user's connected accounts on Zapier.com,
    you'll need to use OAuth. Review the full docs above to learn how to create
    your own provider and generate credentials.
    Úzapier_nla_api_keyÚzapier_nla_oauth_access_tokenzhttps://nla.zapier.com/api/v1/Úzapier_nla_api_baseÚforbid)ÚextraÚreturnc                 ó¦   — dddœ}| j                   r"|j                  dd| j                   › �i«       |S |j                  d| j                  i«       |S )zFormat headers for requests.zapplication/json)ÚAcceptzContent-TypeÚAuthorizationzBearer z	X-API-Key)r   Úupdater   )ÚselfÚheaderss     ún/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain_community/utilities/zapier.pyÚ_format_headersz ZapierNLAWrapper._format_headers4   sc   € ð )Ø.ñ
ˆð
 ×-Ò-Ø�N‰NØ  G¨D×,NÑ,NÐ+OÐ"PÐQôð ˆð �N‰N˜K¨×)@Ñ)@ÐAÔBàˆó    c                 ó€   — t        j                  «       }|j                  j                  | j	                  «       «       |S ©N)Úrequestsr   r   r   r   )r   Úsessions     r   Ú_get_sessionzZapierNLAWrapper._get_sessionD   s0   € Ü×"Ñ"Ó$ˆØ�‰×Ñ˜t×3Ñ3Ó5Ô6Øˆr   ÚmethodÚurlÚkwargsc              ‹   ó¾  K  — t        j                  | j                  «       ¬«      4 ƒd{  –—† } |j                  ||fi |¤Ž4 ƒd{  –—† }|j	                  «        |j                  «       ƒ d{  –—† cddd«      ƒd{  –—†  cddd«      ƒd{  –—†  S 7 Œk7 ŒP7 Œ*7 Œ7 Œ# 1 ƒd{  –—†7  sw Y   nxY wddd«      ƒd{  –—†7   y# 1 ƒd{  –—†7  sw Y   yxY w­w)zMake an async request.)r   N)ÚaiohttpÚClientSessionr   ÚrequestÚraise_for_statusÚjson)r   r$   r%   r&   r"   Úresponses         r   Ú	_arequestzZapierNLAWrapper._arequestI   s�   è ø€ ä×(Ñ(°×1EÑ1EÓ1G×HÕHÈGØ&�w—‘ v¨sÑ=°f×=Ò=ÀØ×)Ñ)Ô+Ø%Ÿ]™]›_×,÷ >×=Ð=÷ I×HÒHøØ=øà,øð >øð Iøß=×=Ñ=ú÷ I×H×H×HÓHüs°   ‚)C«B¬C¯CÁBÁ	CÁ$B!Á0BÁ1B!Á4CÂ BÂCÂCÂBÂCÂCÂB!ÂCÂCÂ!B3	Â'B*Â(B3	Â/CÂ6CÃCÃCÃCÃCÃCÃCNÚinstructionsÚparamsÚpreview_onlyc                 ób   — |r|ni }|j                  d|i«       |r|j                  ddi«       |S )zCreate a payload for an action.r/   r1   T)r   )r   r/   r0   r1   Údatas        r   Ú_create_action_payloadz'ZapierNLAWrapper._create_action_payloadP   s>   € ñ  ‰v RˆØ�‰à ðô	
ñ
 Ø�K‰K˜¨Ð.Ô/Øˆr   Ú	action_idc                 ó(   — | j                   d|› d�z   S )zCreate a url for an action.úexposed/z	/execute/)r   )r   r5   s     r   Ú_create_action_urlz#ZapierNLAWrapper._create_action_urla   s   € à×'Ñ'¨H°Y°K¸yÐ*IÑIÐIr   c                 ób   — | j                  |||«      }t        d| j                  |«      |¬«      S )NÚPOST©r,   )r4   r   r8   )r   r5   r/   r0   r1   r3   s         r   Ú_create_action_requestz'ZapierNLAWrapper._create_action_requeste   s9   € ð ×*Ñ*¨<¸ÀÓNˆÜØØ×#Ñ# IÓ.Øô
ð 	
r   Úbefore)ÚmodeÚvaluesc                 óH   — d}d|v rd}nd|d<   t        |dd|«      }||d<   |S )z,Validate that api key exists in environment.Nr   Ú r   ÚZAPIER_NLA_API_KEYr   )Úclsr?   Úzapier_nla_api_key_defaultr   s       r   Úvalidate_environmentz%ZapierNLAWrapper.validate_environments   sP   € ð
 &*Ð"ð +¨fÑ4Ø)+Ñ&à68ˆFÐ2Ñ3ô 2ØØ Ø Ø&ó	
Ðð (:ˆÐ#Ñ$àˆr   c              ƒ   óf   K  — | j                  d| j                  dz   «      ƒ d{  –—† }|d   S 7 Œ	­w)a´  Returns a list of all exposed (enabled) actions associated with
        current user (associated with the set api_key). Change your exposed
        actions here: https://nla.zapier.com/demo/start/

        The return list can be empty if no actions exposed. Else will contain
        a list of action objects:

        [{
            "id": str,
            "description": str,
            "params": Dict[str, str]
        }]

        `params` will always contain an `instructions` key, the only required
        param. All others optional and if provided will override any AI guesses
        (see "understanding the AI guessing flow" here:
        https://nla.zapier.com/api/v1/docs)
        ÚGETr7   NÚresults)r.   r   )r   r-   s     r   ÚalistzZapierNLAWrapper.alist�   s7   è ø€ ð& Ÿ™¨¨t×/GÑ/GÈ*Ñ/TÓU×UˆØ˜	Ñ"Ð"ð Vús   ‚#1¥/¦
1c                 ó~  — | j                  «       }	 |j                  | j                  dz   «      }|j                  «        |j                  «       d   S # t        j
                  $ rV}j                  dk(  r@| j                  rt	        j
                  d|› �|¬«      ‚t	        j
                  d|› �|¬«      ‚|‚d}~ww xY w)aÇ  Returns a list of all exposed (enabled) actions associated with
        current user (associated with the set api_key). Change your exposed
        actions here: https://nla.zapier.com/demo/start/

        The return list can be empty if no actions exposed. Else will contain
        a list of action objects:

        [{
            "id": str,
            "description": str,
            "params": Dict[str, str]
        }]

        `params` will always contain an `instructions` key, the only required
        param. All others optional and if provided will override any AI guesses
        (see "understanding the AI guessing flow" here:
        https://nla.zapier.com/docs/using-the-api#ai-guessing)
        r7   i‘  zrAn unauthorized response occurred. Check that your access token is correct and doesn't need to be refreshed. Err: )r-   zLAn unauthorized response occurred. Check that your api key is correct. Err: NrH   )	r#   Úgetr   r+   r!   Ú	HTTPErrorÚstatus_coder   r,   )r   r"   r-   Úhttp_errs       r   ÚlistzZapierNLAWrapper.list£   sÓ   € ð& ×#Ñ#Ó%ˆð	Ø—{‘{ 4×#;Ñ#;¸jÑ#HÓIˆHØ×%Ñ%Ô'ð  �}‰}‹˜yÑ)Ð)øô ×!Ñ!ò 	Ø×#Ñ# sÒ*Ø×5Ò5Ü"×,Ñ,ð+à+3¨*ð6ð "*ô	ð ô ×(Ñ(ð,Ø,4¨:ð7à%ôð ð
 ˆNûð	ús   ’.A ÁB<Á&AB7Â7B<c                 óÎ   — | j                  «       }| j                  |||«      }|j                  |j                  |«      «      }|j	                  «        |j                  «       d   S )á  Executes an action that is identified by action_id, must be exposed
        (enabled) by the current user (associated with the set api_key). Change
        your exposed actions here: https://nla.zapier.com/demo/start/

        The return JSON is guaranteed to be less than ~500 words (350
        tokens) making it safe to inject into the prompt of another LLM
        call.
        Úresult)r#   r<   ÚsendÚprepare_requestr+   r,   ©r   r5   r/   r0   r"   r*   r-   s          r   ÚrunzZapierNLAWrapper.runË   s\   € ð ×#Ñ#Ó%ˆØ×-Ñ-¨i¸ÀvÓNˆØ—<‘< × 7Ñ 7¸Ó @ÓAˆØ×!Ñ!Ô#Ø�}‰}‹˜xÑ(Ð(r   c              ƒ   óŽ   K  — | j                  d| j                  |«      | j                  ||«      ¬«      ƒ d{  –—† }|d   S 7 Œ	­w)rQ   r:   r;   NrR   ©r.   r8   r4   ©r   r5   r/   r0   r-   s        r   ÚarunzZapierNLAWrapper.arunÜ   sS   è ø€ ð Ÿ™ØØ×#Ñ# IÓ.Ø×,Ñ,¨\¸6ÓBð (ó 
÷ 
ˆð
 ˜Ñ!Ð!ð
ús   ‚7A¹Aº
Ac                 ó  — | j                  «       }|r|ni }|j                  ddi«       | j                  |||d«      }|j                  |j	                  |«      «      }|j                  «        |j                  «       d   S )úËSame as run, but instead of actually executing the action, will
        instead return a preview of params that have been guessed by the AI in
        case you need to explicitly review before executing.r1   TÚinput_params)r#   r   r<   rS   rT   r+   r,   rU   s          r   ÚpreviewzZapierNLAWrapper.previewî   sx   € ð ×#Ñ#Ó%ˆÙ!‘ rˆØ�‰�~ tÐ,Ô-Ø×-Ñ-¨i¸ÀvÈtÓTˆØ—<‘< × 7Ñ 7¸Ó @ÓAˆØ×!Ñ!Ô#Ø�}‰}‹˜~Ñ.Ð.r   c           	   ƒ   ó’   K  — | j                  d| j                  |«      | j                  ||d¬«      ¬«      ƒ d{  –—† }|d   S 7 Œ	­w)r\   r:   T)r1   r;   NrR   rX   rY   s        r   ÚapreviewzZapierNLAWrapper.apreviewü   sY   è ø€ ð Ÿ™ØØ×#Ñ# IÓ.Ø×,Ñ,¨\¸6ÐPTÐ,ÓUð (ó 
÷ 
ˆð
 ˜Ñ!Ð!ð
ús   ‚9A»A¼
AÚargsc                 óP   —  | j                   |i |¤Ž}t        j                  |«      S )úcSame as run, but returns a stringified version of the JSON for
        insertting back into an LLM.)rV   r,   Údumps©r   ra   r&   r3   s       r   Ú
run_as_strzZapierNLAWrapper.run_as_str	  s(   € ð ˆt�x‰x˜Ð( Ñ(ˆÜ�z‰z˜$ÓÐr   c              �   ól   K  —  | j                   |i |¤Žƒ d{  –—† }t        j                  |«      S 7 Œ­w)rc   N)rZ   r,   rd   re   s       r   Úarun_as_strzZapierNLAWrapper.arun_as_str  s4   è ø€ ð �T—Y‘Y Ð/¨Ñ/×/ˆÜ�z‰z˜$ÓÐð 0úó   ‚4˜2™4c                 óP   —  | j                   |i |¤Ž}t        j                  |«      S )úgSame as preview, but returns a stringified version of the JSON for
        insertting back into an LLM.)r^   r,   rd   re   s       r   Úpreview_as_strzZapierNLAWrapper.preview_as_str  s(   € ð ˆt�|‰|˜TÐ, VÑ,ˆÜ�z‰z˜$ÓÐr   c              �   ól   K  —  | j                   |i |¤Žƒ d{  –—† }t        j                  |«      S 7 Œ­w)rk   N)r`   r,   rd   re   s       r   Úapreview_as_strz ZapierNLAWrapper.apreview_as_str  s4   è ø€ ð #�T—]‘] DÐ3¨FÑ3×3ˆÜ�z‰z˜$ÓÐð 4úri   c                 óL   — | j                  «       }t        j                  |«      S )údSame as list, but returns a stringified version of the JSON for
        insertting back into an LLM.)rO   r,   rd   ©r   Úactionss     r   Úlist_as_strzZapierNLAWrapper.list_as_str!  s   € ð —)‘)“+ˆÜ�z‰z˜'Ó"Ð"r   c              ƒ   óh   K  — | j                  «       ƒ d{  –—† }t        j                  |«      S 7 Œ­w)rp   N)rI   r,   rd   rq   s     r   Úalist_as_strzZapierNLAWrapper.alist_as_str'  s*   è ø€ ð Ÿ
™
›×$ˆÜ�z‰z˜'Ó"Ð"ð %ús   ‚2–0—2)NFr    )%Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__r   r
   Úmodel_configr   r   r   r#   r   r.   r   Úboolr4   r8   r   r<   r   ÚclassmethodrE   r   rI   rO   rV   rZ   r^   r`   rf   rh   rl   rn   rs   ru   © r   r   r   r      s•  … ñð$ ÓØ#&Ó&Ø?Ð˜Ó?áØô€Lð  c¨3 h¡ó ð ˜gó ð
- cð -°ð -¸sð -ÀtÈCÐQTÈHÁ~ó -ð "&Ø"ñ	àðð ˜‘ðð ð	ð
 
óð"J¨Cð J°Có Jð "&Ø"ñ
àð
ð ð
ð ˜‘ð	
ð
 ð
ð 
ó
ñ ˜(Ô#Øð¨$ð °3ò ó ó $ðð0#˜T $™Zó #ð,&*�d˜4‘jó &*ðR KOñ)Øð)Ø,/ð)Ø9AÀ$¹ð)à	ó)ð$ KOñ"Øð"Ø,/ð"Ø9AÀ$¹ð"à	ó"ð& KOñ/Øð/Ø,/ð/Ø9AÀ$¹ð/à	ó/ð KOñ"Øð"Ø,/ð"Ø9AÀ$¹ð"à	ó"ð  ð  ¨sð  °só  ð  sð  °cð  ¸có  ð  Cð  °3ð  ¸3ó  ð ¨3ð  ¸#ð  À#ó  ð#˜Só #ð# Cô #r   r   )ry   r,   Útypingr   r   r   r   r(   r!   Úlangchain_core.utilsr   Úpydanticr	   r
   r   r   r   r   r   r   r   Ú<module>rƒ      s5   ðñó ß ,Ó ,ã Û Ý 5ß ;Ñ ;ß %ôR#�yõ R#r   