
    3fi=S                       d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
mZmZmZmZmZ ddlmZmZ ddlmZmZ  ej,                  e      Zeeeee
e	ddZ G d	 d
e      ZdZej@                  ejB                  ejD                  hZ#dde#D  cg c]  } | jH                   c}  z   Z%eeee&def   Z' G d de      Z(erddl)m*Z*m+Z+m,Z,m-Z-  G d de(      Z. G d de(      Z/ G d de      Z0 G d de      Z1yc c} w )z,Pydantic models for parsing an OpenAPI spec.    )annotationsN)Enum)	TYPE_CHECKINGAnyDictListOptionalSequenceTupleTypeUnion)	BaseModelField)HTTPVerbOpenAPISpec)integernumberstringbooleanarrayobjectnullc                  2    e Zd ZdZdZdZdZdZedd       Z	y)	APIPropertyLocationzThe location of the property.querypathheadercookiec                ^    	  | |      S # t         $ r t        d| j                         w xY w)zParse an APIPropertyLocation.z.Invalid APIPropertyLocation. Valid values are )
ValueError__members__)clslocations     p/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/tools/openapi/utils/api_models.pyfrom_strzAPIPropertyLocation.from_str-   s<    	x=  	@@QR 	s   
 ",N)r#   strreturnz'APIPropertyLocation')
__name__
__module____qualname____doc__QUERYPATHHEADERCOOKIEclassmethodr%        r$   r   r   %   s+    'EDFF r2   r   )zapplication/jsonzCUnsupported APIPropertyLocation "{location}" for parameter {name}. zValid values are c                      e Zd ZU dZ ed      Zded<   	  ed      Zded<   	  ed      Zded<   	  ed	d
      Z	ded	<   	  edd
      Z
ded<   y
)APIPropertyBasezBase model for an API property.namealiasr&   requiredbooltypeSCHEMA_TYPEdefaultN)r7   r<   zOptional[Any]descriptionOptional[str])r(   r)   r*   r+   r   r5   __annotations__r8   r:   r<   r=   r1   r2   r$   r4   r4   H   si    ) F#D###,Hd,+F+D++4
 #DAG]A,!&]D!IKI*r2   r4   )	MediaType	ParameterRequestBodySchemac                      e Zd ZU dZ ed      Zded<   	 e	 	 	 	 dd       Zedd       Z	e	 	 	 	 dd       Z
edd       Zedd	       Zedd
       Zedd       Zedd       Zedd       Zy)APIPropertyzDA model for a property in the query, path, header, or cookie params.r#   r6   r   c                T    | j                   }t        |t              s|S t        |      S N)r:   
isinstancelisttuple)schematype_s     r$   _cast_schema_list_typez"APIProperty._cast_schema_list_typet   s&     %&L<r2   c           	         | j                    d}t        ||j                  D ci c]  }t        |      | c}      S c c}w )z2Get the schema type when the parameter is an enum.r   )r5   r   enumr&   )	parameterrK   
param_namevs       r$   _get_schema_type_for_enumz%APIProperty._get_schema_type_for_enum~   s<     "'t,
JFKK @qQ @AA @s   >
c                   ddl m}m} | j                  }t	        ||      rt
        j                  |      }n;t	        ||      r!|j                  j                  d      d   }|}nt        d|       t	        |t              r|f}|S )Nr   	ReferencerC   /zUnsupported array items: )openapi_pydanticrV   rC   itemsrH   rE   rM   refsplitr    r&   )rK   rV   rC   rZ   schema_typeref_names         r$   _get_schema_type_for_arrayz&APIProperty._get_schema_type_for_array   s|    	

 eV$%<<UCKy)yys+B/H"K8@AAk3'&.Kr2   c                   |y t         j                  |      }|dk(  rt         j                  |      }|S |dk(  rt        d      |t        v r'|j
                  rt         j                  | |      }|S 	 |S t        d|       )Nr   r   zObjects not yet supportedUnsupported type: )rE   rM   r_   NotImplementedErrorPRIMITIVE_TYPESrO   rS   )rP   rK   r]   s      r$   _get_schema_typezAPIProperty._get_schema_type   s    >#.#E#Ef#M'!%@@HK  H$%&ABBO+{{)CCIvV 	   &(:;-&HIIr2   c                T    | t         vr t        t        j                  | |            y )Nr#   r5   )SUPPORTED_LOCATIONSrb   INVALID_LOCATION_TEMPLformatrf   s     r$   _validate_locationzAPIProperty._validate_location   s0    ..%&--xd-K  /r2   c                    | rt        d      y )NzmAPI Properties with media content not supported. Media content only supported within APIRequestBodyProperty's)r    )contents    r$   _validate_contentzAPIProperty._validate_content   s    O  r2   c                    ddl m}m} | j                  }t	        ||      r|j                  |      }|S |y t	        ||      st        d|       |S )Nr   rU   zError dereferencing schema: )rY   rV   rC   param_schemarH   get_referenced_schemar    )rP   specrV   rC   rK   s        r$   _get_schemazAPIProperty._get_schema   sa    	

 ''fi(//7F  ^FF+;F8DEEr2   c                X    	 t         j                  |       t        v S # t        $ r Y yw xY w)z2Return whether the provided location is supported.F)r   r%   rg   r    )r#   s    r$   is_supported_locationz!APIProperty.is_supported_location   s0    	&//9=PPP 		s    	))c                n   t         j                  |j                        }| j                  ||j                         | j                  |j                         | j                  ||      }| j                  ||      }||j                  nd} | |j                  |||j                  |j                  |      S )z&Instantiate from an OpenAPI Parameter.N)r5   r#   r<   r=   r8   r:   )r   r%   param_inrj   r5   rm   rl   rr   rd   r<   r=   r8   )r"   rP   rq   r#   rK   r]   default_vals          r$   from_parameterzAPIProperty.from_parameter   s     '//	0B0BCNN	
 	i//0D1**9f=(.(:fnn!--''
 	
r2   N)rK   rC   r'   z%Optional[Union[str, Tuple[str, ...]]])rP   rA   rK   rC   r'   r   )rP   rA   rK   Optional[Schema]r'   r;   )r#   r   r5   r&   r'   None)rl   zOptional[Dict[str, MediaType]]r'   rz   )rP   rA   rq   r   r'   ry   )r#   r&   r'   r9   )rP   rA   rq   r   r'   z'APIProperty')r(   r)   r*   r+   r   r#   r?   staticmethodrM   rS   r_   rd   rj   rm   rr   rt   r0   rx   r1   r2   r$   rE   rE   n   s    N$)
$;H!;9  	.    B B
 	. .  (          
 
r2   rE   c                      e Zd ZU dZ ed      Zded<   	  ed      Zded<   	 e	 	 	 	 	 	 	 	 dd       Z	e	 	 	 	 	 	 	 	 	 	 dd       Z
e	 d	 	 	 	 	 	 	 	 	 	 	 dd
       Zy	)APIRequestBodyPropertyz$A model for a request body property.
propertiesr6   zList['APIRequestBodyProperty']references_used	List[str]c           
        ddl m} g }|j                  xs g }|j                  t	        d|       |j                  j                         D ]~  \  }}t        ||      rF|j                  j                  d      d   }	|	|vr#|j                  |	       |j                  |      }nX|j                  | j                  ||||v ||              |j                  |fS )Nr   rV   z3No properties found when processing object schema: rW   rX   rK   r5   r8   rq   r   )rY   rV   r8   r~   r    rZ   rH   r[   r\   appendrp   from_schemar:   )
r"   rK   rq   r   rV   r~   required_props	prop_nameprop_schemar^   s
             r$   _process_object_schemaz-APIRequestBodyProperty._process_object_schema   s    	
 
.B$EfXN  '-&7&7&=&=&? 	"I{+y1&??005b9?2#**84"&"<"<["IK&"&.8$3   	$ {{J&&r2   c                L   ddl m}m} |j                  }|t	        ||      rL|j
                  j                  d      d   }||vr#|j                  |       |j                  |      }n	 d| dS 	 t	        ||      r)| j                  || dd||	      }	d|	j                   dS y
)Nr   rU   rW   rX   Array<>ItemTr   r   )rY   rV   rC   rZ   rH   r[   r\   r   rp   r   r:   )
r"   rK   r5   rq   r   rV   rC   rZ   r^   
array_types
             r$   _process_array_schemaz,APIRequestBodyProperty._process_array_schema  s     	7%+ 99??3/3?2#**84 66u=Ez++%( __  6!$3 - 
  
022r2   Nc           	     $   |g }|j                   }g }|dk(  r#|j                  r| j                  |||      \  }}n4|dk(  r| j                  ||||      }n|t        v rn|nt        d|        | ||||j                  |j                  ||      S )z,Recursively populate from an OpenAPI Schema.r   r   ra   r5   r8   r:   r<   r=   r~   r   )r:   r~   r   r   rc   r    r<   r=   )r"   rK   r5   r8   rq   r   r]   r~   s           r$   r   z"APIRequestBodyProperty.from_schema?  s     " Okk35
("v'8'8&)&@&@o'#K G#33FD$XKO+ 1+?@@NN**!+
 	
r2   )rK   rC   rq   r   r   r   r'   zBTuple[Union[str, List[str], None], List['APIRequestBodyProperty']])
rK   rC   r5   r&   rq   r   r   r   r'   r&   rG   )rK   rC   r5   r&   r8   r9   rq   r   r   zOptional[List[str]]r'   z'APIRequestBodyProperty')r(   r)   r*   r+   r   r~   r?   r   r0   r   r   r   r1   r2   r$   r}   r}      s    .16\1JJ.J- "'->!?OY?.''#.'AJ'	K' 'B      	 
 #  
   D  04%
%
 %
 	%

 %
 -%
 
"%
 %
r2   r}   c                      e Zd ZU dZ ed      Zded<   	  ed      Zded<    ed      Zded<   	 e		 	 	 	 	 	 dd	       Z
e		 	 	 	 	 	 dd
       Zy)APIRequestBodyzA model for a request body.r=   r6   r>   r~   List[APIRequestBodyProperty]
media_typer&   c                   ddl m} g }|j                  }t        ||      r>|j	                  |j
                  j                  d      d          |j                  |      }|t        d|       g }|j                  xs g }|j                  dk(  rw|j                  rk|j                  j                         D ]L  \  }}	t        |	|      r|j                  |	      }	|j	                  t        j                  |	|||v |             N |S |j	                  t        dd	|j                  |j                  |j                   g |
             |S )z+Process the media type of the request body.r   r   rW   rX   z)Could not resolve schema for media type: r   )rK   r5   r8   rq   bodyTr   )rY   rV   media_type_schemarH   r   r[   r\   rp   r    r8   r:   r~   rZ   r}   r   r<   r=   )
r"   media_type_objrq   rV   r   rK   api_request_body_propertiesrequired_propertiesr   r   s
             r$   _process_supported_media_typez,APIRequestBody._process_supported_media_typeu  sV    	/11fi(""6::#3#3C#8#<=//7F>;N;KL  ')#$oo3;;("v'8'8*0*;*;*A*A*C &	;k95"&"<"<["IK+22*66*&!*.A!A!	 7 	2 +* (..&!"NN & 2 2!$3
 +*r2   c                    g }|j                   j                         D ]1  \  }}|t        vr| j                  ||      }|j	                  |       3  | |j
                  |      S )z(Instantiate from an OpenAPI RequestBody.)r=   r~   r   )rl   rZ   _SUPPORTED_MEDIA_TYPESr   extendr=   )r"   request_bodyrq   r~   r   r   r   s          r$   from_request_bodyz APIRequestBody.from_request_body  s~    
 
*6*>*>*D*D*F 	;&J!77*-*K*K+' 9:	; $00!!
 	
r2   N)r   r@   rq   r   r'   r   )r   rB   rq   r   r'   z'APIRequestBody')r(   r)   r*   r+   r   r=   r?   r~   r   r0   r   r   r1   r2   r$   r   r   i  s    %!&]!;K;./4</HJ,H ,/J/--+!-+ -+ 
&	-+ -+^ 
&
.9
	
 
r2   r   c                     e Zd ZU dZ ed      Zded<   	  ed      Zded<   	  ed      Zded<   	  ed      Z	ded<   	  ed	      Z
d
ed	<   	  ed      Zded<    ed      Zded<   	 e	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 	 	 dd       Zedd       Z	 d	 	 	 	 	 ddZddZed d       Zed d       Zed d       Zy)!APIOperationz#A model for a single API operation.operation_idr6   r&   r=   r>   base_urlr   methodr   r~   zSequence[APIProperty]r   zOptional[APIRequestBody]c                   g }| D ]  }t         j                  |j                        r&|j                  t         j	                  ||             H|j
                  r4t        t        j                  |j                  |j                              t        j                  t        j                  |j                  |j                        dz           |S )z$Get the properties of the operation.rf   z Ignoring optional parameter)rE   rt   rv   r   rx   r8   r    rh   ri   r5   loggerwarning)
parametersrq   r~   params       r$   _get_properties_from_parametersz,APIOperation._get_properties_from_parameters  s    
 
 	E00@!!+"<"<UD"IJ *11!&ejj 2   *11!&ejj 2  55 !	" r2   c                R    t        j                  |      }| j                  |||      S )z+Create an APIOperation from an OpenAPI URL.)r   from_urlfrom_openapi_spec)r"   spec_urlr   r   rq   s        r$   from_openapi_urlzAPIOperation.from_openapi_url  s)     ##H-$$T488r2   c           	        |j                  ||      }|j                  |      }| j                  ||      }t        j                  |||      }|j                  |      }|t        j                  ||      nd}	|j                  xs |j                  }
|
s@|j                  4|j                  |   j                  xs |j                  |   j                  }
 | ||
xs d|j                  ||||	      S )z,Create an APIOperation from an OpenAPI spec.N )r   r=   r   r   r   r~   r   )get_operationget_parameters_for_operationr   r   get_cleaned_operation_idget_request_body_for_operationr   r   r=   summarypathsr   )r"   rq   r   r   	operationr   r~   r   r   api_request_bodyr=   s              r$   r   zAPIOperation.from_openapi_spec  s     &&tV4	66yA
88TJ
";;ItVT::9E ' ,,\4@ 	
  ++@y/@/@tzz5**T*66R$**T:J:R:RK%#)r]]!)
 	
r2   c                h   | yt        | t              rdddddj                  | |       S t        | t              rdt        j                  | d          dS t        | t              r=t        | t              r-dj                  | D cg c]  }d	|j                   d	 c}      S t        |       S c c}w )
Nanyr   r   )r&   r   floatz	date-timer   r   r   z | ')rH   r&   getrJ   r   ts_type_from_pythonr:   
issubclassr   joinvalue)rL   es     r$   r   z APIOperation.ts_type_from_python  s    = s##!%	
 c%  u%L<<U1XFGqIIt$E4)@::u=!177)1~=>>u: >s   B/c                   g }|D ]  }|j                   }| j                  |j                        }|j                  rdnd}|j                  rd|j                   dnd}|j
                  r+| j                  |j
                  |dz         }	d|	 dd|z   d	}|j                  | dd|z   | | d
| d        dj                  |      S )zFormat nested properties.r   ?/*  */   z{

 }: ,)	r5   r   r:   r8   r=   r~   _format_nested_propertiesr   r   )
selfr~   indentformatted_propspropr   	prop_typeprop_required	prop_descnested_propss
             r$   r   z&APIOperation._format_nested_properties0  s      	D		I00;I"&--BSM7;7G7G#d../s3RI#==OOVaZ  #<.3<.C	""+Rf~i[r)TUV	  yy))r2   c           
     X   | j                   }g }| j                  r6| j                  | j                  j                        }|j	                  |       | j                  D ]s  }|j
                  }| j                  |j                        }|j                  rdnd}|j                  rd|j                   dnd}|j	                  | d| | d| d       u dj                  |      j                         }	| j                  rd| j                   dnd}
d|
 d	| d
|	 d}|j                         S )z6Get typescript string representation of the operation.r   r   r   r   z
		r   r   r   z
type z	 = (_: {
z
}) => any;
)r   r   r   r~   r   r5   r   r:   r8   r=   r   strip)r   operation_nameparamsformatted_request_body_propsr   r   r   r   r   formatted_paramsdescription_strtypescript_definitions               r$   to_typescriptzAPIOperation.to_typescriptH  sD   **+/+I+I!!,,,( MM67OO 	XD		I00;I"&--BSM7;7G7G#d../s3RIMMYKvi[r)TUVW	X  99V,2249=9I9IC 0 015r%     ! %**,,r2   c                    | j                   D cg c]+  }|j                  t        j                  k(  r|j                  - c}S c c}w rG   )r~   r#   r   r,   r5   r   propertys     r$   query_paramszAPIOperation.query_paramsd  sA     !OO
  $7$=$== MM
 	
 
   0Ac                    | j                   D cg c]+  }|j                  t        j                  k(  r|j                  - c}S c c}w rG   )r~   r#   r   r-   r5   r   s     r$   path_paramszAPIOperation.path_paramsl  sA     !OO
  $7$<$<< MM
 	
 
r   c                    | j                   g S | j                   j                  D cg c]  }|j                   c}S c c}w rG   )r   r~   r5   )r   r   s     r$   body_paramszAPIOperation.body_paramst  s7    $I&*&7&7&B&BCd		CCCs   =N)r   zList[Parameter]rq   r   r'   zList[APIProperty])r   r&   r   r&   r   r&   r'   'APIOperation')rq   r   r   r&   r   r&   r'   r   )rL   r;   r'   r&   )r   )r~   r   r   intr'   r&   )r'   r&   )r'   r   )r(   r)   r*   r+   r   r   r?   r=   r   r   r   r~   r   r{   r   r0   r   r   r   r   r   r   r   r   r   r1   r2   r$   r   r     s   -N3L#31!&]!;K;+
+Hc+(F#D##$8,FH,+(-L(AJ%A .3-HL*H,#+6	 0 99 9 	9
 
9 9 

 
 	

 

 
<  ( GH*6*@C*	*0-8 
 
 
 
 D Dr2   r   )2r+   
__future__r   loggingrO   r   typingr   r   r   r   r	   r
   r   r   r   pydanticr   r   5langchain_community.tools.openapi.utils.openapi_utilsr   r   	getLoggerr(   r   r   r   r&   r9   rc   r   r   r.   r,   r-   rg   r   rh   rJ   r;   r4   rY   r@   rA   rB   rC   rE   r}   r   r   )locs   0r$   <module>r      s0   2 "  
 
 
 & W			8	$$ & /   0CD399DEFG  CudD01+i +:  ~
/ ~
Bv
_ v
tO
Y O
h{D9 {Dw Es   C5