§
    æ˜ºh’-  ã                   ó†   — 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
mZmZ ddlmZ ddlmZmZ  G d„ de
¦  «        ZdS )	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)Ú	BaseModelÚExtraÚroot_validator©Úget_from_dict_or_env)ÚRequestÚSessionc            
       óJ  — e Zd ZU dZeed<   eed<   dZeed<    G d„ 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fd„Zdedefd„Z	 	 d'dededee         defd„Z ed¬¦  «        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fd!„Zdefd"„Zdefd#„Zdefd$„Zdefd%„Z defd&„Z!dS ))Ú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_basec                   ó    — e Zd ZdZej        ZdS )úZapierNLAWrapper.Configz'Configuration for this pydantic object.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚforbidÚextra© ó    úl/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/langchain_community/utilities/zapier.pyÚConfigr   /   s   € € € € € Ø5Ð5à”ˆˆˆr   r   Úreturnc                 ó–   — dddœ}| j         r |                     dd| j         › �i¦  «         n|                     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     r   Ú_format_headersz ZapierNLAWrapper._format_headers4   st   € ð )Ø.ð
ð 
ˆð
 Ô-ð 	CØ�NŠNØ Ð"P¨DÔ,NÐ"PÐ"PÐQñô ð ð ð �NŠN˜K¨Ô)@ÐAÑBÔBÐBàˆr   c                 ó„   — t          j        ¦   «         }|j                             |                      ¦   «         ¦  «         |S ©N)Úrequestsr   r%   r#   r&   )r$   Úsessions     r   Ú_get_sessionzZapierNLAWrapper._get_sessionD   s7   € ÝÔ"Ñ$Ô$ˆØŒ×Ò˜t×3Ò3Ñ5Ô5Ñ6Ô6Ð6Øˆr   ÚmethodÚurlÚkwargsc              ‹   ó¢  K  — t          j        |                      ¦   «         ¬¦  «        4 ƒd{V —†} |j        ||fi |¤Ž4 ƒd{V —†}|                     ¦   «          |                     ¦   «         ƒ d{V —†cddd¦  «        ƒd{V —† cddd¦  «        ƒd{V —† S # 1 ƒd{V —†swxY w Y   	 ddd¦  «        ƒd{V —† dS # 1 ƒd{V —†swxY w Y   dS )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Ô1GÐHÑHÔHð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ÈGØ&�w” v¨sÐ=Ð=°fÐ=Ð=ð -ð -ð -ð -ð -ð -ð -ÀØ×)Ò)Ñ+Ô+Ð+Ø%Ÿ]š]™_œ_Ð,Ð,Ð,Ð,Ð,Ð,ð-ð -ð -ð -ñ -ô -ð -ð -ð -ð -ð -ð -ð -ð	-ð 	-ð 	-ð 	-ñ 	-ô 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð-ð -ð -ð -ð -ð -ð -ð -ð -ð -øøøð -ð -ð -ð -ð -ð	-ð 	-ð 	-ñ 	-ô 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-ð 	-øøøð 	-ð 	-ð 	-ð 	-ð 	-ð 	-s5   °B>Á.BÁ4B>Â
B#	Â#B>Â&B#	Â'B>Â>
CÃCNFÚinstructionsÚparamsc                 ór   — |r|ni }|                      d|i¦  «         |r|                      ddi¦  «         |S )zCreate a payload for an action.r7   Úpreview_onlyT)r#   )r$   r7   r8   r:   Údatas        r   Ú_create_action_payloadz'ZapierNLAWrapper._create_action_payloadP   sX   € ð  Ð'ˆvˆv RˆØ�Šà ðñ	
ô 	
ð 	
ð
 ð 	0Ø�KŠK˜¨Ð.Ñ/Ô/Ð/Øˆr   Ú	action_idc                 ó   — | j         d|› d�z   S )zCreate a url for an action.úexposed/z	/execute/)r   )r$   r=   s     r   Ú_create_action_urlz#ZapierNLAWrapper._create_action_url^   s   € àÔ'Ð*I°YÐ*IÐ*IÐ*IÑIÐIr   c                 óz   — |                       |||¦  «        }t          d|                      |¦  «        |¬¦  «        S )NÚPOST©r4   )r<   r   r@   )r$   r=   r7   r8   r:   r;   s         r   Ú_create_action_requestz'ZapierNLAWrapper._create_action_requestb   sI   € ð ×*Ò*¨<¸ÀÑNÔNˆÝØØ×#Ò# IÑ.Ô.Øð
ñ 
ô 
ð 	
r   T)ÚpreÚvaluesc                 óP   — 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
   )ÚclsrF   Úzapier_nla_api_key_defaultr   s       r   Úvalidate_environmentz%ZapierNLAWrapper.validate_environmentp   s[   € ð &*Ð"ð +¨fÐ4Ð4Ø)+Ð&Ð&à68ˆFÐ2Ñ3õ 2ØØ Ø Ø&ñ	
ô 
Ðð (:ˆÐ#Ñ$àˆr   c              ƒ   ó^   K  — |                       d| j        dz   ¦  «        ƒ d{V —†}|d         S )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)
        ÚGETr?   NÚresults)r6   r   )r$   r5   s     r   ÚalistzZapierNLAWrapper.alist‰   s@   è è € ð& Ÿš¨¨tÔ/GÈ*Ñ/TÑUÔUÐUÐUÐUÐUÐUÐUˆØ˜	Ô"Ð"r   c                 ó~  — |                       ¦   «         }	 |                     | j        dz   ¦  «        }|                     ¦   «          n]# t          j        $ rK}|j        dk    r9| j        rt	          j        d|› �|¬¦  «        ‚t	          j        d|› �|¬¦  «        ‚|‚d}~ww xY w|                     ¦   «         d         S )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)
        r?   i‘  zrAn unauthorized response occurred. Check that your access token is correct and doesn't need to be refreshed. Err: )r5   zLAn unauthorized response occurred. Check that your api key is correct. Err: NrO   )	r+   Úgetr   r3   r)   Ú	HTTPErrorÚstatus_coder   r4   )r$   r*   r5   Úhttp_errs       r   ÚlistzZapierNLAWrapper.listŸ   s  € ð& ×#Ò#Ñ%Ô%ˆð	Ø—{’{ 4Ô#;¸jÑ#HÑIÔIˆHØ×%Ò%Ñ'Ô'Ð'Ð'øÝÔ!ð 	ð 	ð 	ØÔ# sÒ*Ð*ØÔ5ð Ý"Ô,ð6à+3ð6ð 6ð "*ð	ñ ô ð õ Ô(ð7Ø,4ð7ð 7à%ðñ ô ð ð
 ˆNøøøøð	øøøð �}Š}‰Œ˜yÔ)Ð)s   –1A ÁB"ÁABÂB"c                 ó  — |                       ¦   «         }|                      |||¦  «        }|                     |                     |¦  «        ¦  «        }|                     ¦   «          |                     ¦   «         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+   rD   ÚsendÚprepare_requestr3   r4   ©r$   r=   r7   r8   r*   r2   r5   s          r   ÚrunzZapierNLAWrapper.runÇ   sp   € ð ×#Ò#Ñ%Ô%ˆØ×-Ò-¨i¸ÀvÑNÔNˆØ—<’< × 7Ò 7¸Ñ @Ô @ÑAÔAˆØ×!Ò!Ñ#Ô#Ð#Ø�}Š}‰Œ˜xÔ(Ð(r   c              ƒ   ó    K  — |                       d|                      |¦  «        |                      ||¦  «        ¬¦  «        ƒ d{V —†}|d         S )rX   rB   rC   NrY   ©r6   r@   r<   ©r$   r=   r7   r8   r5   s        r   ÚarunzZapierNLAWrapper.arunØ   sr   è è € ð ŸšØØ×#Ò# IÑ.Ô.Ø×,Ò,¨\¸6ÑBÔBð (ñ 
ô 
ð 
ð 
ð 
ð 
ð 
ð 
ˆð
 ˜Ô!Ð!r   c                 ó@  — |                       ¦   «         }|r|ni }|                     ddi¦  «         |                      |||d¦  «        }|                     |                     |¦  «        ¦  «        }|                     ¦   «          |                     ¦   «         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.r:   TÚinput_params)r+   r#   rD   rZ   r[   r3   r4   r\   s          r   ÚpreviewzZapierNLAWrapper.previewê   s—   € ð ×#Ò#Ñ%Ô%ˆØ!Ð)�� rˆØ�Š�~ tÐ,Ñ-Ô-Ð-Ø×-Ò-¨i¸ÀvÈtÑTÔTˆØ—<’< × 7Ò 7¸Ñ @Ô @ÑAÔAˆØ×!Ò!Ñ#Ô#Ð#Ø�}Š}‰Œ˜~Ô.Ð.r   c           	   ƒ   ó¤   K  — |                       d|                      |¦  «        |                      ||d¬¦  «        ¬¦  «        ƒ d{V —†}|d         S )rc   rB   T)r:   rC   NrY   r_   r`   s        r   ÚapreviewzZapierNLAWrapper.apreviewø   sx   è è € ð ŸšØØ×#Ò# IÑ.Ô.Ø×,Ò,¨\¸6ÐPTÐ,ÑUÔUð (ñ 
ô 
ð 
ð 
ð 
ð 
ð 
ð 
ˆð
 ˜Ô!Ð!r   c                 óD   —  | j         |i |¤Ž}t          j        |¦  «        S )úcSame as run, but returns a stringified version of the JSON for
        insertting back into an LLM.)r]   r4   Údumps©r$   Úargsr.   r;   s       r   Ú
run_as_strzZapierNLAWrapper.run_as_str  s*   € ð ˆtŒx˜Ð( Ð(Ð(ˆÝŒz˜$ÑÔÐr   c              �   óT   K  —  | j         |i |¤Žƒ d{V —†}t          j        |¦  «        S )ri   N)ra   r4   rj   rk   s       r   Úarun_as_strzZapierNLAWrapper.arun_as_str  s@   è è € ð �T”Y Ð/¨Ð/Ð/Ð/Ð/Ð/Ð/Ð/Ð/ˆÝŒz˜$ÑÔÐr   c                 óD   —  | j         |i |¤Ž}t          j        |¦  «        S )úgSame as preview, but returns a stringified version of the JSON for
        insertting back into an LLM.)re   r4   rj   rk   s       r   Úpreview_as_strzZapierNLAWrapper.preview_as_str  s*   € ð ˆtŒ|˜TÐ, VÐ,Ð,ˆÝŒz˜$ÑÔÐr   c              �   óT   K  —  | j         |i |¤Žƒ d{V —†}t          j        |¦  «        S )rq   N)rg   r4   rj   rk   s       r   Úapreview_as_strz ZapierNLAWrapper.apreview_as_str  s@   è è € ð
 #�T”] DÐ3¨FÐ3Ð3Ð3Ð3Ð3Ð3Ð3Ð3ˆÝŒz˜$ÑÔÐr   c                 óR   — |                       ¦   «         }t          j        |¦  «        S )údSame as list, but returns a stringified version of the JSON for
        insertting back into an LLM.)rV   r4   rj   ©r$   Úactionss     r   Úlist_as_strzZapierNLAWrapper.list_as_str  s!   € ð —)’)‘+”+ˆÝŒz˜'Ñ"Ô"Ð"r   c              ƒ   ób   K  — |                       ¦   «         ƒ d{V —†}t          j        |¦  «        S )rv   N)rP   r4   rj   rw   s     r   Úalist_as_strzZapierNLAWrapper.alist_as_str%  s7   è è € ð Ÿ
š
™œÐ$Ð$Ð$Ð$Ð$Ð$ˆÝŒz˜'Ñ"Ô"Ð"r   )NFr(   )"r   r   r   r   ÚstrÚ__annotations__r   r   r   r&   r   r+   r   r6   r   r<   r@   r   rD   r	   rL   r   rP   rV   r]   ra   re   rg   rm   ro   rr   rt   ry   r{   r   r   r   r   r      sÇ  € € € € € € ðð ð$ ÐÐÑØ#&Ð&Ð&Ñ&Ø?Ð˜Ð?Ð?Ñ?ðð ð ð ð ñ ô ð ð
  c¨3 h¤ð ð ð ð ð ˜gð ð ð ð ð
- cð -°ð -¸sð -ÀtÈCÐQTÈHÄ~ð -ð -ð -ð -ð NSðð ØðØ)1°$¬ðà	ðð ð ð ðJ¨Cð J°Cð Jð Jð Jð Jð "&Øð
ð 
àð
ð ð
ð ˜”ð	
ð 
ð
ð 
ð 
ð 
ð €^˜ÐÑÔð¨$ð °4ð ð ð ñ Ôðð0#˜T $œZð #ð #ð #ð #ð,&*�d˜4”jð &*ð &*ð &*ð &*ðR KOð)ð )Øð)Ø,/ð)Ø9AÀ$¼ð)à	ð)ð )ð )ð )ð$ KOð"ð "Øð"Ø,/ð"Ø9AÀ$¼ð"à	ð"ð "ð "ð "ð& KOð/ð /Øð/Ø,/ð/Ø9AÀ$¼ð/à	ð/ð /ð /ð /ð KOð"ð "Øð"Ø,/ð"Ø9AÀ$¼ð"à	ð"ð "ð "ð "ð ¨Sð  ð  ð  ð  ð °Cð  ð  ð  ð  ð °ð  ð  ð  ð  ð à	ð ð  ð  ð  ð#˜Sð #ð #ð #ð #ð# Cð #ð #ð #ð #ð #ð #r   r   )r   r4   Útypingr   r   r   r   r0   r)   Úlangchain_core.pydantic_v1r   r   r	   Úlangchain_core.utilsr   r   r   r   r   r   r   ú<module>r�      sÖ   ððð ð €€€Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,à €€€Ø €€€Ø GÐ GÐ GÐ GÐ GÐ GÐ GÐ GÐ GÐ GØ 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø %Ð %Ð %Ð %Ð %Ð %Ð %Ð %ðQ#ð Q#ð Q#ð Q#ð Q#�yñ Q#ô Q#ð Q#ð Q#ð Q#r   