o
    i-                     @  s  d Z ddlmZ ddlmZmZ ddlmZm	Z	 ddl
mZmZ ddl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%m&Z&m'Z' ddl(m)Z) ddl*m+Z+ ddl,m-Z-m.Z.m/Z/m0Z0 erddl1m2Z2m3Z3 dZ4eddG dd dZ5			d?d@d'd(Z6e%d)Z7e'd*Z8eee+e e7f e8f Z9ee7e8f Z:e9e7e8f e:e7e8f B Z;dAd.d/Z<dBd9d:Z=dCd=d>Z>dS )DzUsed 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)	AwaitableCallable)	dataclassfield)	Parameter	signature)TYPE_CHECKINGAnyConcatenatecast
get_origin)
ConfigDict)_decorators_generate_schema_typing_extra)ConfigWrapper)	FieldInfo)GenerateJsonSchema)create_schema_validator)SchemaValidatorcore_schema)	ParamSpecTypeIsTypeVar   )doc_descriptions)
RunContext)check_object_json_schemais_async_callableis_model_likerun_in_executor)DocstringFormatObjectJsonSchema)function_schemaT)kw_onlyc                   @  s   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< dZded< eedZded< dZ	ded< dddZ
dddZdS ) FunctionSchemaz-Internal information about a function schema.Callable[..., Any]functionz
str | Nonedescriptionr   	validatorr#   json_schemabool	takes_ctxis_asyncNsingle_arg_name)default_factoryz	list[str]positional_fieldsvar_positional_field	args_dictdict[str, Any]ctxRunContext[Any]returnr
   c                   sx   |  ||\}}| jr#tttgtt f | j}||i |I d H S tttgtf | j}t|g|R i |I d H S N)	
_call_argsr.   r   r   r
   r   strr(   r!   )selfr3   r5   argskwargsr(    r>   b/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_ai/_function_schema.pycall0   s   zFunctionSchema.call tuple[list[Any], dict[str, Any]]c                 C  s\   | j r| j |i}| jr|gng }| jD ]
}||| q| jr*||| j ||fS r8   )r/   r-   r1   appendpopr2   extend)r;   r3   r5   r<   positional_fieldr>   r>   r?   r9   9   s   

zFunctionSchema._call_args)r3   r4   r5   r6   r7   r
   )r3   r4   r5   r6   r7   rA   )__name__
__module____qualname____doc____annotations__r/   r   listr1   r2   r@   r9   r>   r>   r>   r?   r&   !   s   
 
	r&   NautoFr(   r'   schema_generatortype[GenerateJsonSchema]r-   bool | Nonedocstring_formatr"   require_parameter_descriptionsr,   r7   c           #        s\  |du rt | }t| jdd}t|}t|}g }zt|  W n ty? }	 z|t	|	 tdd  W Y d}	~	nd}	~	ww t
| }
d}i }g }d}t }t|  |d\}}|r|rqt fdd jD }|t| }n	t jt| }|r|d	d
|  t j D ]\}\}}|j ju r|r|dkrqt}n.|
| }|dkr|rt|s|d q|st|r|d q|dkrt|r|d q|j}|jtjkr||}q|jtjkrt| }|jtju }t t!t |}|rt"#|}nt"$||j}|j%du r|&||_%|j'||||d ||< }t(||)di d< |jtj*kr8|| q|jtjkrA|}q|r[ddl+m,} d|}|d| j- d| |.d}|rednd|d< t/||||\}} |0|}t1|| | j2| j-d||j3}!t t4|!}!| 5|}"| dur|s|"6dd}t7||!t8|"| |||t9| | d	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.
        docstring_format: The docstring format to use.
        require_parameter_descriptions: Whether to require descriptions for all tool function parameters.
        schema_generator: The JSON schema generator class to use.

    Returns:
        A `FunctionSchema` instance.
    NT)titleuse_attribute_docstringsc                   S  s   d S r8   r>   r>   r>   r>   r?   <lambda>i   s    z!function_schema.<locals>.<lambda>)rP   c                 3  s$    | ]}t  j| js|V  qd S r8   )_is_call_ctx
parameters
annotation).0namesigr>   r?   	<genexpr>w   s    
z"function_schema.<locals>.<genexpr>z#Missing parameter descriptions for z, r   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)requiredmetadatar    r   )	UserErrorz
  zError generating schema for z:
  allowforbidextra_fields_behaviorvalidate_callr)   )	r)   r*   r+   r/   r1   r2   r-   r.   r(   ):
_takes_ctxr   rF   r   r   GenerateSchemar   
ValueErrorrB   r:   r   get_function_type_hintsr   DecoratorInfosr   setrV   join	enumerateitemsrW   emptyr
   rU   rY   kindr   VAR_KEYWORDgenerate_schemaVAR_POSITIONALrK   defaultr   typer   from_annotationfrom_annotated_attributer)   get_generate_td_field_schemar    
setdefaultPOSITIONAL_ONLY
exceptionsr_   rH   core_config_build_schemaclean_schemar   rG   plugin_settingsr   generaterC   r&   r   r   )#r(   rM   r-   rP   rQ   configconfig_wrapper
gen_schemaerrorse
type_hintsvar_kwargs_schemafieldsr1   r2   
decoratorsr)   field_descriptionsparameters_without_ctxmissing_paramsindexrY   prW   
field_namer]   
field_info	td_schemar_   error_detailsr{   schemar/   schema_validatorr+   r>   rZ   r?   r$   J   s   









r$   PRcallable_objTargetCallable[P, R]TypeIs[WithCtx[P, R]]c                 C  s   zt | }W n
 ty   Y dS w ztt|j }W n
 ty&   Y dS w t| tj	s>t
t| dd}|dur<|} ndS tt| }||}|du rQdS d|juoYt|S )zCheck if a callable takes a `RunContext` first argument.

    Args:
        callable_obj: The callable to check.

    Returns:
        `True` if the callable takes a `RunContext` as first argument, `False` otherwise.
    F__call__NT)r   rf   nextiterrV   keysStopIteration
isinstancer   _function_likegetattrrs   r   rg   unwrap_wrapped_functionrv   rm   rU   )r   r[   first_param_name	call_funcr   rW   r>   r>   r?   rd      s(   	
rd   r   %dict[str, core_schema.TypedDictField]r   core_schema.CoreSchema | Noner   _generate_schema.GenerateSchemar{   core_schema.CoreConfig)tuple[core_schema.CoreSchema, str | None]c                 C  sf   t | dkr |du r tt| }| | }|d d r |d |fS tj| ||r+||nd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    r   )r   extras_schema)lenr   r   r   typed_dict_schemarp   )r   r   r   r{   rY   td_fieldr   r>   r>   r?   r|     s   r|   rW   r
   c                 C  s   | t u p	t| t u S )zNReturn whether the annotation is the `RunContext` class, parameterized or not.)r   r   )rW   r>   r>   r?   rU   -  s   rU   )NrL   F)r(   r'   rM   rN   r-   rO   rP   r"   rQ   r,   r7   r&   )r   r   r7   r   )
r   r   r   r   r   r   r{   r   r7   r   )rW   r
   r7   r,   )?rI   
__future__r   _annotationscollections.abcr   r   dataclassesr   r   inspectr   r   typingr	   r
   r   r   r   pydanticr   pydantic._internalr   r   r   pydantic._internal._configr   pydantic.fieldsr   pydantic.json_schemar   !pydantic.plugin._schema_validatorr   pydantic_corer   r   typing_extensionsr   r   r   _griffer   _run_contextr   _utilsr   r   r    r!   toolsr"   r#   __all__r&   r$   r   r   WithCtx
WithoutCtxTargetCallablerd   r|   rU   r>   r>   r>   r?   <module>   sF    + 

!