
    Ngt-                         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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get_from_dict_or_env)	BaseModel
ConfigDictmodel_validator)RequestSessionc            
       X   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	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          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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_baseforbid)extra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     `/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/utilities/zapier.py_format_headersz ZapierNLAWrapper._format_headers4   st     ).
 

 - 	CNN "PD,N"P"PQ    NNK)@ABBB    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#     (1E1E1G1GHHH 	- 	- 	- 	- 	- 	- 	-G&wvs==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.r/   preview_onlyT)r   )r   r/   r0   r2   datas        r   _create_action_payloadz'ZapierNLAWrapper._create_action_payloadP   sX      'vvR	
 	
 	

  	0KK.///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_url^   s    '*IY*I*I*IIIr   c                 z    |                      |||          }t          d|                     |          |          S )NPOSTr,   )r4   r   r8   )r   r5   r/   r0   r2   r3   s         r   _create_action_requestz'ZapierNLAWrapper._create_action_requestb   sI     **<NN##I..
 
 
 	
r   before)mode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   )clsr?   zapier_nla_api_key_defaultr   s       r   validate_environmentz%ZapierNLAWrapper.validate_environmentp   s[    
 &*" +f44)+&&68F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)
        GETr7   Nresults)r.   r   )r   r-   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)
        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IH%%''''! 	 	 	#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#   r<   sendprepare_requestr+   r,   r   r5   r/   r0   r"   r*   r-   s          r   runzZapierNLAWrapper.run   sp     ##%%--ivNN<< 7 7 @ @AA!!###}}x((r   c                    K   |                      d|                     |          |                     ||                     d{V }|d         S )rQ   r:   r;   NrR   r.   r8   r4   r   r5   r/   r0   r-   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.r2   Tinput_params)r#   r   r<   rS   rT   r+   r,   rU   s          r   previewzZapierNLAWrapper.preview   s     ##%%!)r~t,-----ivtTT<< 7 7 @ @AA!!###}}~..r   c           	         K   |                      d|                     |          |                     ||d                     d{V }|d         S )r\   r:   T)r2   r;   NrR   rX   rY   s        r   apreviewzZapierNLAWrapper.apreview   sx       ##I..,,\6PT,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.)rV   r,   dumpsr   argsr&   r3   s       r   
run_as_strzZapierNLAWrapper.run_as_str  s*     tx(((z$r   c                 T   K    | j         |i | d{V }t          j        |          S )rb   N)rZ   r,   rc   rd   s       r   arun_as_strzZapierNLAWrapper.arun_as_str  s@       TY/////////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.)r^   r,   rc   rd   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 )rj   N)r`   r,   rc   rd   s       r   apreview_as_strz ZapierNLAWrapper.apreview_as_str  s@      
 #T]D3F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.)rO   r,   rc   r   actionss     r   list_as_strzZapierNLAWrapper.list_as_str   s!     ))++z'"""r   c                 b   K   |                                   d{V }t          j        |          S )ro   N)rI   r,   rc   rp   s     r   alist_as_strzZapierNLAWrapper.alist_as_str&  s7       

$$$$$$z'"""r   )NFr    )$__name__
__module____qualname____doc__str__annotations__r   r
   model_configr   r   r   r#   r   r.   r   r4   r8   r   r<   r   classmethodrE   r   rI   rO   rV   rZ   r^   r`   rf   rh   rk   rm   rr   rt    r   r   r   r      s         $ #&&&&????:  Lc3h     g    
-c - -s -tCQTH~ - - - - NS )1$	   JC JC J J J J "&
 

 
 	
 

 
 
 
 _(###$ 3    [ $#0#T$Z # # # #,&*d4j &* &* &* &*R KO) )),/)9A$)	) ) ) )$ KO" "",/"9A$"	" " " "& KO/ //,//9A$/	/ / / / KO" "",/"9A$"	" " " " S         C                  	       #S # # # ##C # # # # # #r   r   )rx   r,   typingr   r   r   r   r(   r!   langchain_core.utilsr   pydanticr	   r
   r   r   r   r   r}   r   r   <module>r      s      , , , , , , , , , , , ,   5 5 5 5 5 5 ; ; ; ; ; ; ; ; ; ; % % % % % % % %Q# Q# Q# Q# Q#y Q# Q# Q# Q# Q#r   