
    dh,                     ~    S r SSKrSSKJrJrJrJr  SSKrSSKrSSK	J
r
  SSKJrJrJr  SSKJrJr   " S S\5      rg)	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                   Z   \ rS rSr% Sr\\S'   \\S'   Sr\\S'   \" SS9r	S	\
\\4   4S
 jrS	\4S jrS\S\S\S	\
\\4   4S jr  S*S\S\\
   S\S	\
4S jjrS\S	\4S jr  S*S\S\S\\
   S\S	\4
S jjr\" SS9\S\
S	\4S j5       5       rS	\\
   4S jrS	\\
   4S jr S+S\S\S\\
   S	\
4S jjr S+S\S\S\\
   S	\
4S jjr S+S\S\S\\
   S	\
4S  jjr S+S\S\S\\
   S	\
4S! jjrS"\S\S	\4S# jr S"\S\S	\4S$ jr!S"\S\S	\4S% jr"S"\S\S	\4S& jr#S	\4S' jr$S	\4S( jr%S)r&g),ZapierNLAWrapper   a  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                     SSS.nU R                   (       a"  UR                  SSU R                    305        U$ UR                  SU R                  05        U$ )zFormat headers for requests.zapplication/json)AcceptzContent-TypeAuthorizationzBearer z	X-API-Key)r   updater   )selfheaderss     \/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/utilities/zapier.py_format_headers ZapierNLAWrapper._format_headers4   sc     ).

 --NN GD,N,N+O"PQ  NNK)@)@AB    c                     [         R                  " 5       nUR                  R                  U R	                  5       5        U$ N)requestsr   r   r   r   )r   sessions     r   _get_sessionZapierNLAWrapper._get_sessionD   s0    ""$t3356r    methodurlkwargsc                   #    [         R                  " U R                  5       S9 ISh  vN nUR                  " X40 UD6 ISh  vN nUR	                  5         UR                  5       I Sh  vN sSSS5      ISh  vN   sSSS5      ISh  vN   $  Nj NP N* N N! , ISh  vN  (       d  f       O= fSSS5      ISh  vN    g! , ISh  vN  (       d  f       g= f7f)zMake an async request.)r   N)aiohttpClientSessionr   requestraise_for_statusjson)r   r'   r(   r)   r$   responses         r   	_arequestZapierNLAWrapper._arequestI   s     ((1E1E1GHHGv=f==))+%]]_, >== IHH=, > I=== IHHHHHs   'C$BC$C
BC
	$B-B.B1C
=B>C
C$BC$C
BC
C$B5	$B'%B5	1C
8C$CC$
C!CC!C$Ninstructionsparamspreview_onlyc                 v    U(       a  UO0 nUR                  SU05        U(       a  UR                  SS05        U$ )zCreate a payload for an action.r3   r5   T)r   )r   r3   r4   r5   datas        r   _create_action_payload'ZapierNLAWrapper._create_action_payloadP   s>      vR	

 KK./r    	action_idc                 (    U R                   SU S3-   $ )zCreate a url for an action.exposed/z	/execute/)r   )r   r:   s     r   _create_action_url#ZapierNLAWrapper._create_action_urla   s    ''HYKy*IIIr    c                 Z    U R                  X#U5      n[        SU R                  U5      US9$ )NPOSTr/   )r8   r   r=   )r   r:   r3   r4   r5   r7   s         r   _create_action_request'ZapierNLAWrapper._create_action_requeste   s7     **<N##I.
 	
r    before)modevaluesc                 J    SnSU;   a  SnOSUS'   [        USSU5      nX1S'   U$ )z,Validate that api key exists in environment.Nr    r   ZAPIER_NLA_API_KEYr   )clsrF   zapier_nla_api_key_defaultr   s       r   validate_environment%ZapierNLAWrapper.validate_environments   sN    
 &*" +f4)+&68F23 2  &	
 (:#$r    c                 f   #    U R                  SU R                  S-   5      I Sh  vN nUS   $  N	7f)aD  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)r1   r   )r   r0   s     r   alistZapierNLAWrapper.alist   s6     & t/G/G*/TUU	"" Vs   #1/
1c                    U R                  5       n UR                  U R                  S-   5      nUR                  5         UR                  5       S   $ ! [        R
                   aX  nWR                  S:X  aA  U R                  (       a  [        R
                  " SU 3US9e[        R
                  " SU 3US9eUeSnAff = f)aW  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: )r0   zLAn unauthorized response occurred. Check that your api key is correct. Err: NrP   )	r%   getr   r.   r#   	HTTPErrorstatus_coder   r/   )r   r$   r0   http_errs       r   listZapierNLAWrapper.list   s    & ##%	{{4#;#;j#HIH%%'  }}y)) !! 	##s*55",,++3*6 "*	  ((,,4:7% 
 N	s   .A B?'AB::B?c                     U R                  5       nU R                  XU5      nUR                  UR                  U5      5      nUR	                  5         UR                  5       S   $ )O  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%   rB   sendprepare_requestr.   r/   r   r:   r3   r4   r$   r-   r0   s          r   runZapierNLAWrapper.run   sZ     ##%--ivN<< 7 7 @A!!#}}x((r    c                    #    U R                  SU R                  U5      U R                  X#5      S9I Sh  vN nUS   $  N	7f)r[   r@   rA   Nr\   r1   r=   r8   r   r:   r3   r4   r0   s        r   arunZapierNLAWrapper.arun   sP      ##I.,,\B ( 
 

 !!
s   3A?
Ac                 
   U R                  5       nU(       a  UO0 nUR                  SS05        U R                  XUS5      nUR                  UR	                  U5      5      nUR                  5         UR                  5       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.r5   Tinput_params)r%   r   rB   r]   r^   r.   r/   r_   s          r   previewZapierNLAWrapper.preview   sv     ##%!r~t,---ivtT<< 7 7 @A!!#}}~..r    c           
         #    U R                  SU R                  U5      U R                  X#SS9S9I Sh  vN nUS   $  N	7f)rh   r@   T)r5   rA   Nr\   rc   rd   s        r   apreviewZapierNLAWrapper.apreview   sV      ##I.,,\PT,U ( 
 

 !!
s   2A >
A argsc                 R    U R                   " U0 UD6n[        R                  " U5      $ )[Same as run, but returns a stringified version of the JSON for
insertting back into an LLM.)r`   r/   dumpsr   ro   r)   r7   s       r   
run_as_strZapierNLAWrapper.run_as_str	  s&     xx((zz$r    c                 n   #    U R                   " U0 UD6I Sh  vN n[        R                  " U5      $  N7f)rq   N)re   r/   rr   rs   s       r   arun_as_strZapierNLAWrapper.arun_as_str  s1      YY///zz$ 0   535c                 R    U R                   " U0 UD6n[        R                  " U5      $ )_Same as preview, but returns a stringified version of the JSON for
insertting back into an LLM.)rj   r/   rr   rs   s       r   preview_as_strZapierNLAWrapper.preview_as_str  s&     ||T,V,zz$r    c                 n   #    U R                   " U0 UD6I Sh  vN n[        R                  " U5      $  N7f)r{   N)rm   r/   rr   rs   s       r   apreview_as_str ZapierNLAWrapper.apreview_as_str  s1      ]]D3F33zz$ 4ry   c                 N    U R                  5       n[        R                  " U5      $ )\Same as list, but returns a stringified version of the JSON for
insertting back into an LLM.)rX   r/   rr   r   actionss     r   list_as_strZapierNLAWrapper.list_as_str!  s     ))+zz'""r    c                 j   #    U R                  5       I Sh  vN n[        R                  " U5      $  N7f)r   N)rQ   r/   rr   r   s     r   alist_as_strZapierNLAWrapper.alist_as_str'  s)      

$zz'"" %s   313 )NFr"   )'__name__
__module____qualname____firstlineno____doc__str__annotations__r   r
   model_configr   r   r   r%   r   r1   r   boolr8   r=   r   rB   r   classmethodrL   r   rQ   rX   r`   re   rj   rm   rt   rw   r|   r   r   r   __static_attributes__r   r    r   r   r      s   $ #&&??Lc3h  g 
-c - -s -tCQTH~ - "&"	  	
 
"JC JC J "&"

 
 	

 
 

 (#$ 3   $0#T$Z #,&*d4j &*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   )r   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    