
    .hm2                    R   d dl mZ d dlZd dlZd dl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 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 e G d d                      Ze G d d                      Zed         Zd$dZej         d             Z!	 d%d&dZ"	 	 	 	 	 d'd(d#Z#dS ))    )annotationsN)	dataclass)AnyCallableLiteralget_args
get_originget_type_hints)	DocstringDocstringSectionKind)	BaseModelFieldcreate_model   )	UserError)RunContextWrapper)ensure_strict_json_schemac                  x    e Zd ZU dZded<   	 ded<   	 ded<   	 ded	<   	 d
ed<   	 dZded<   	 dZded<   	 ddZdS )
FuncSchemazg
    Captures the schema for a python function, in preparation for sending it to an LLM as a tool.
    strname
str | Nonedescriptionztype[BaseModel]params_pydantic_modelzdict[str, Any]params_json_schemazinspect.Signature	signatureFbooltakes_contextTstrict_json_schemadatar   return tuple[list[Any], dict[str, Any]]c                   g }i }d}t          | j        j                                                  D ]\  }\  }}| j        r|dk    rt          ||d          }|j        |j        k    r|                    |pg            d}Q|j        |j	        k    r|
                    |pi            y|j        |j        |j        fv r|s|                    |           |||<   |||<   ||fS )z
        Converts validated data from the Pydantic model into (args, kwargs), suitable for calling
        the original function.
        Fr   NT)	enumerater   
parametersitemsr   getattrkindVAR_POSITIONALextendVAR_KEYWORDupdatePOSITIONAL_ONLYPOSITIONAL_OR_KEYWORDappend)	selfr    positional_argskeyword_argsseen_var_positionalidxr   paramvalues	            S/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/agents/function_schema.pyto_call_argszFuncSchema.to_call_args(   s(   
 &(')# #,DN,E,K,K,M,M"N"N 	+ 	+C$! cQhhD$--EzU111&&u{333&*##u000##EKR0000 5u7RSSS* /#**51111).L&& &+T"",,    N)r    r   r!   r"   )__name__
__module____qualname____doc____annotations__r   r   r8    r9   r7   r   r      s           III#*****E&&&&Y    (M_#####= -  -  -  -  -  -r9   r   c                  6    e Zd ZU dZded<   	 ded<   	 ded<   dS )	FuncDocumentationzHContains metadata about a python function, extracted from its docstring.r   r   r   r   zdict[str, str] | Noneparam_descriptionsN)r:   r;   r<   r=   r>   r?   r9   r7   rA   rA   K   s?         RRIII3F----QQr9   rA   )googlenumpysphinxdocr   r!   DocstringStylec                   dddd}g d}|D ]2}t          j        || t           j                  r|dxx         dz  cc<   3g d}|D ]2}t          j        || t           j                  r|dxx         dz  cc<   3g d}|D ]2}t          j        || t           j                  r|d	xx         dz  cc<   3t          |                                          }|dk    rd	S g d}|D ]}||         |k    r|c S d	S )
Nr   )rE   rD   rC   )z	^:param\sz^:type\sz	^:return:z^:rtype:rE   r   )z^Parameters\s*\n\s*-{3,}z^Returns\s*\n\s*-{3,}z^Yields\s*\n\s*-{3,}rD   )z^(Args|Arguments):z^(Returns):z
^(Raises):rC   )research	MULTILINEmaxvalues)	rF   scoressphinx_patternspatternnumpy_patternsgoogle_patterns	max_scorestylesstyles	            r7   _detect_docstring_stylerV   \   sj   34qA(N(NF MLLO" " "9Wc2<00 	"8!  N
 " ! !9Wc2<00 	!7OOOq OOO MLLO" " "9Wc2<00 	"8!FMMOO$$IA~~x $A#@#@F  %=I%%LLL & 8r9   c               #     K   t          j        d          } |                                 }|                     t           j                   	 d V  |                     |           d S # |                     |           w xY w)Ngriffe)logging	getLoggergetEffectiveLevelsetLevelERROR)loggerprevious_levels     r7   _suppress_griffe_loggingr`      s{       x((F--//N
OOGM"""('''''''''s   A& &A=funcCallable[..., Any]rU   DocstringStyle | Nonec                   | j         }t          j        |           }|st          |dd          S t	                      5  t          |d|pt          |                    }|                                }ddd           n# 1 swxY w Y   t          d |D             d          }d |D             }t          | j         ||pd          S )a  
    Extracts metadata from a function docstring, in preparation for sending it to an LLM as a tool.

    Args:
        func: The function to extract documentation from.
        style: The style of the docstring to use for parsing. If not provided, we will attempt to
            auto-detect the style.

    Returns:
        A FuncDocumentation object containing the function's name, description, and parameter
        descriptions.
    N)r   r   rB   r   )linenoparserc              3  N   K   | ] }|j         t          j        k    |j        V  !d S N)r(   r   textr6   ).0sections     r7   	<genexpr>z.generate_func_documentation.<locals>.<genexpr>   s4      ZZ7@T@Y0Y0Y0Y0Y0Y0YZZr9   c                f    i | ].}|j         t          j        k    |j        D ]}|j        |j        /S r?   )r(   r   r%   r6   r   r   )rj   rk   r5   s      r7   
<dictcomp>z/generate_func_documentation.<locals>.<dictcomp>   sM     * * *</:::] ;: 	
E%::::r9   )	r:   inspectgetdocrA   r`   r   rV   parsenext)ra   rU   r   rF   	docstringparsedr   rB   s           r7   generate_func_documentationru      s9    =D
.

C W dQUVVVV	!	#	# # #c!E4Y=TUX=Y=YZZZ	""# # # # # # # # # # # # # # # #ZZfZZZ\` K* ** * * ]-5   s   6B  BBTdocstring_stylename_overrider   description_overrideuse_docstring_infor   r   c           	        |rt          | |          }|j        pi }nd}i }|r	|p|j        n| j        }t	          j        |           }	t          |           }
t          |	j        	                                          }d}g }|r|d         \  }}|

                    ||j                  }|t          j        k    r5t          |          p|}|t          u rd}n/|                    ||f           n|                    ||f           |dd         D ]x\  }}|

                    ||j                  }|t          j        k    r1t          |          p|}|t          u rt!          d| j                   |                    ||f           yi }|D ]\  }}|

                    ||j                  }|j        }|t          j        k    rt$          }|
                    |d          }|j        |j        k    rt          |          t*          u rXt-          |          }t/          |          dk    r#|d         t0          u rt          |d                  }n t          t$                   }nt          |         }|t3          t          |          f||<   |j        |j        k    rt          |          t6          u rXt-          |          }t/          |          dk    rt6          |d         |d         f         }n.t6          t8          t$          f         }nt6          t8          |f         }|t3          t6          |          f||<   |t          j        k    r|t3          d	|
          f||<   |t3          ||          f||<   t;          | dfdt<          i|}|                                }|rtA          |          }tC          ||r	|p|j"        nd|||	||          S )a  
    Given a python function, extracts a `FuncSchema` from it, capturing the name, description,
    parameter descriptions, and other metadata.

    Args:
        func: The function to extract the schema from.
        docstring_style: The style of the docstring to use for parsing. If not provided, we will
            attempt to auto-detect the style.
        name_override: If provided, use this name instead of the function's `__name__`.
        description_override: If provided, use this description instead of the one derived from the
            docstring.
        use_docstring_info: If True, uses the docstring to generate the description and parameter
            descriptions.
        strict_json_schema: Whether the JSON schema is in strict mode. If True, we'll ensure that
            the schema adheres to the "strict" standard the OpenAI API expects. We **strongly**
            recommend setting this to True, as it increases the likelihood of the LLM providing
            correct JSON input.

    Returns:
        A `FuncSchema` object containing the function's name, description, parameter descriptions,
        and other metadata.
    NFr   Tr   z@RunContextWrapper param found at non-first position in function    )default_factoryr   .)r   )defaultr   _args__base__)r   r   r   r   r   r   r   )#ru   rB   r   r:   ro   r   r
   listr%   r&   get
annotation_emptyr	   r   r/   r   r}   r   r(   r)   tupler   lenEllipsisr   r+   dictr   r   r   model_json_schemar   r   r   )ra   rv   rw   rx   ry   r   doc_infoparam_descs	func_namesig
type_hintsparamsr   filtered_params
first_namefirst_paramannoriginr   r5   fieldsr}   field_descriptionargs_of_tuple	dict_argsdynamic_modeljson_schemas                              r7   function_schemar      s5   @  .t_EE17R2:M.I 
D
!
!C%%J#.&&(())FMO >"()
KnnZ)?@@'.  __+F*** $&&
K'@AAAA""J#<=== abbz 	. 	.ennT5#344'.  __+F***(( (   	e}----  F& > >ennT5#344- '.  C (OOD$77 :---#%'' (}%%**}Q/?8/K/K}Q/0CCs)CC 3i d8IJJJF4LL
 Z5,,,#$&&$SMM	y>>Q&&y|Yq\9:CCsCx.CC 38n d8IJJJF4LL '.(( #+<=== t '7HIII t !I!4!4!4SSySFSSM  1133K =/<< DLV(@H,@RV+&#-   r9   )rF   r   r!   rG   rh   )ra   rb   rU   rc   r!   rA   )NNNTT)ra   rb   rv   rc   rw   r   rx   r   ry   r   r   r   r!   r   )$
__future__r   
contextlibro   rY   rI   dataclassesr   typingr   r   r   r   r	   r
   rX   r   r   pydanticr   r   r   
exceptionsr   run_contextr   strict_schemar   r   rA   rG   rV   contextmanagerr`   ru   r   r?   r9   r7   <module>r      s   " " " " " "       				 ! ! ! ! ! ! O O O O O O O O O O O O O O O O 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 ! ! ! ! ! ! * * * * * * 4 4 4 4 4 4 5- 5- 5- 5- 5- 5- 5- 5-p R R R R R R R R 45
% % % %P ( ( ( >B' ' ' ' 'X .2 $'+##^ ^ ^ ^ ^ ^ ^r9   