
    g                       d Z ddlmZ ddlmZmZ ddlmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlm Z  ddl!m"Z"m#Z# erddl$m%Z% dZ& G d de      Z'ddZ(ddZ)	 	 	 	 	 	 	 	 	 	 ddZ*ddZ+y)zUsed to build pydantic validators and JSON schemas from functions.

This module has to use numerous internal Pydantic APIs and is therefore brittle to changes in Pydantic.
    )annotations)	Parameter	signature)TYPE_CHECKINGAnyCallable	TypedDictcast
get_origin)
ConfigDict)_decorators_generate_schema_typing_extra)ConfigWrapper)	FieldInfo)GenerateJsonSchema)create_schema_validator)SchemaValidatorcore_schema   )doc_descriptions)check_object_json_schemais_model_like)ObjectJsonSchema)function_schemac                  N    e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   ded<   y)FunctionSchemaz-Internal information about a function schema.strdescriptionr   	validatorr   json_schemaz
str | Nonesingle_arg_namez	list[str]positional_fieldsvar_positional_fieldN)__name__
__module____qualname____doc____annotations__     J/var/www/openai/venv/lib/python3.12/site-packages/pydantic_ai/_pydantic.pyr   r      s)    7!!  $$r+   r   c           	        t        | j                        }t        |      }t        j                  |      }t        |       }t        j                  |       }d}i }g }	d}
g }t        j                         }t        | |      \  }}t        |j                  j                               D ]  \  }\  }}|j                  |j                  u r|r|dk(  r*t         }nj||   }|dk(  r|rt#        |      s|j%                  d       Z|st#        |      r|j%                  d       y|dk7  rt#        |      r|j%                  d       |j&                  }|j(                  t*        j,                  k(  r|j/                  |      }|j(                  t*        j0                  k(  r	t2        |   }t5        t6        t            |      }t9        j:                  |      }|j<                  |j?                  |      |_        |jA                  |||      x||<   }tC        |      |jE                  di       d<   |j(                  t*        jF                  k(  r|	j%                  |       |j(                  t*        j0                  k(  s|}
 |r/d	d
l$m%} djM                  |      } |d| jN                   d|       |jQ                  d      }|rdnd|d<   tS        ||||      \  }}|jU                  |      }tW        || | jX                  | jN                  d||jZ                        }t5        t\        |      }t_               ja                  |      }|tc        |      |d<   n|s|je                  dd      }tg        ||ti        |      ||	|
      S )a  Build a Pydantic validator and JSON schema from a tool function.

    Args:
        function: The function to build a validator and JSON schema for.
        takes_ctx: Whether the function takes a `RunContext` first argument.

    Returns:
        A `FunctionSchema` instance.
    )titleNr   zQFirst parameter of tools that take context must be annotated with RunContext[...]zDRunContext annotations can only be used with tools that take contextz=RunContext annotations can only be used as the first argumentmetadatar   r   )	UserErrorz
  zError generating schema for z:
  allowforbidextra_fields_behaviorvalidate_calladditionalPropertiesr   )r   r    r!   r"   r#   r$   )5r   r%   r   r   GenerateSchemar   r   get_function_type_hintsr   DecoratorInfosr   	enumerate
parametersitems
annotationemptyr   _is_call_ctxappendnamekindr   VAR_KEYWORDgenerate_schemaVAR_POSITIONALlistr
   typer   from_annotationr   get_generate_td_field_schemar   
setdefaultPOSITIONAL_ONLY
exceptionsr0   joinr'   core_config_build_schemaclean_schemar   r&   plugin_settingsr   r   generateboolpopr   r   )function	takes_ctxconfigconfig_wrapper
gen_schemasig
type_hintsvar_kwargs_schemafieldsr#   r$   errors
decoratorsr   field_descriptionsindexr@   pr<   
field_name
field_info	td_schemar0   error_detailsrN   schemar"   schema_validatorr!   s                                r,   r   r   )   s[    h//0F"6*N!00@J
H
C66x@J7;46F#%'+F++-J&6x&E#K#%cnn&:&:&<=ya<<399$UaZJ#D)Jzi#J/MM"uv<
#;de!Z 8]^VV
66Y*** * : :: Fvv111!*-
 d3i4J"22:>J%%-);)?)?
)K
&-7-Q-Q. F: ERR\D]I  R0Avv222!((49333'1$[ >^ )F+6x7L7L6MUS`Rabcc ,,T2K6G7XK'(+F4EzS^_FO$$V,F.&& O-=>$&//7K
 .23D.E*+ "oomT:",[9'+1 r+   c                    t        |       }	 t        t        |j                  j	                                     }t        j                  |       }||   }||j                  uxr t        |      S # t        $ r Y yw xY w)zCheck if a function takes a `RunContext` first argument.

    Args:
        function: The function to check.

    Returns:
        `True` if the function takes a `RunContext` as first argument, `False` otherwise.
    F)
r   nextiterr:   keysr   r7   r=   r>   StopIteration)rU   rZ   first_param_namer[   r<   s        r,   rV   rV      s{     H
CHS^^%8%8%: ;< #::8D
 01
*G|J/GG  s   ,A. .	A:9A:c                    t        |       dk(  r*|(t        t        |             }| |   }|d   d   r|d   |fS t        j                  | ||r|j                  |      nd      }|dfS )ap  Generate a typed dict schema for function parameters.

    Args:
        fields: The fields to generate a typed dict schema for.
        var_kwargs_schema: The variable keyword arguments schema.
        gen_schema: The `GenerateSchema` instance.
        core_config: The core configuration.

    Returns:
        tuple of (generated core schema, single arg name).
    r   Nr/   r   rg   )rW   extras_schema)lenrj   rk   r   typed_dict_schemarC   )r]   r\   rY   rN   r@   td_fieldre   s          r,   rO   rO      s    " 6{a-5DL!$<J0H%t++--GXj001BC^bI
 d?r+   c                b    ddl m} | |u xs$ t        j                  |       xr t	        |       |u S )Nr   )
RunContext)toolsru   r   is_generic_aliasr   )r<   ru   s     r,   r>   r>      s5    !# &&z2[z*7MQ[7[r+   N)rU   Callable[..., Any]rV   rS   returnr   )rU   rx   ry   rS   )
r]   z%dict[str, core_schema.TypedDictField]r\   zcore_schema.CoreSchema | NonerY   z_generate_schema.GenerateSchemarN   zcore_schema.CoreConfigry   z)tuple[core_schema.CoreSchema, str | None])r<   r   ry   rS   ),r(   
__future__r   _annotationsinspectr   r   typingr   r   r   r	   r
   r   pydanticr   pydantic._internalr   r   r   pydantic._internal._configr   pydantic.fieldsr   pydantic.json_schemar   !pydantic.plugin._schema_validatorr   pydantic_corer   r   _griffer   _utilsr   r   rv   r   __all__r   r   rV   rO   r>   r*   r+   r,   <module>r      s   
 3 ( L L  K K 4 % 3 E 6 % ;' 	%Y 	%tnH(14 0 (	
 />r+   