
    Cj>                       S SK Jr  S SKrS SKrS SKrS SKrS SKJr  S SKJ	r	  S SK
JrJrJrJrJrJr  S SKJrJr  S SKJrJrJr  S SKJr  S	S
KJr  S	SKJr  S	SKJr  S	SK J!r!  \	 " S S5      5       r"\	 " S S5      5       r#\S   r$SS jr%\RL                  S 5       r' S     SS jjr(SS jr)SS jr*SS jr+     S              S!S jjr,g)"    )annotationsN)Callable)	dataclass)	AnnotatedAnyLiteralget_args
get_originget_type_hints)	DocstringDocstringSectionKind)	BaseModelFieldcreate_model)	FieldInfo   )	UserError)RunContextWrapper)ensure_strict_json_schema)ToolContextc                      \ rS rSr% SrS\S'    S\S'    S\S'    S	\S
'    S\S'    SrS\S'    SrS\S'    SS jrSr	g)
FuncSchema   z_
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_schemac                @   / n0 nSn[        U R                  R                  R                  5       5       H  u  nu  pgU R                  (       a  US:X  a  M!  [        XS5      nUR                  UR                  :X  a  UR                  U=(       d    / 5        SnMe  UR                  UR                  :X  a  UR                  U=(       d    0 5        M  UR                  UR                  UR                  4;   a   U(       d  UR                  U5        M  XU'   M  XU'   M     X#4$ )zr
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)	selfdatapositional_argskeyword_argsseen_var_positionalidxr   paramvalues	            h/home/rurouni/.local/share/pipx/venvs/strix-agent/lib/python3.13/site-packages/agents/function_schema.pyto_call_argsFuncSchema.to_call_args,   s    
 &(')# #,DNN,E,E,K,K,M"NC$!!cQhD-EzzU111&&u{3&*#u000##EKR0 5 5u7R7RSS*#**51).& &+T"+ #O, ,,     N)r2   r   returnz tuple[list[Any], dict[str, Any]])
__name__
__module____qualname____firstlineno____doc____annotations__r"   r#   r:   __static_attributes__r=   r<   r9   r   r      sW     I#***E&&Y  (M4_##= -r<   r   c                  <    \ rS rSr% SrS\S'    S\S'    S\S'   S	rg
)FuncDocumentationO   zHContains metadata about a Python function, extracted from its docstring.r   r   r   r   zdict[str, str] | Noneparam_descriptionsr=   N)r?   r@   rA   rB   rC   rD   rE   r=   r<   r9   rG   rG   O   s    R
I3F--Qr<   rG   )googlenumpysphinxc                &   SSSS.n/ SQnU H<  n[         R                  " X0[         R                  5      (       d  M/  US==   S-  ss'   M>     / SQnU H<  n[         R                  " X0[         R                  5      (       d  M/  US==   S-  ss'   M>     / SQnU H<  n[         R                  " X0[         R                  5      (       d  M/  US	==   S-  ss'   M>     [        UR	                  5       5      nUS:X  a  g	/ SQnU H  nX   U:X  d  M  Us  $    g	)
Nr   )rL   rK   rJ   )z	^:param\sz^:type\sz	^:return:z^:rtype:rL   r   )z^Parameters\s*\n\s*-{3,}z^Returns\s*\n\s*-{3,}z^Yields\s*\n\s*-{3,}rK   )z^(Args|Arguments):z^(Returns):z
^(Raises):rJ   )research	MULTILINEmaxvalues)	docscoressphinx_patternspatternnumpy_patternsgoogle_patterns	max_scorestylesstyles	            r9   _detect_docstring_styler\   `   s    34qA(NF MO"99W2<<008! #N
 "99W2<<007Oq O "
 MO"99W2<<008! # FMMO$IA~ $AF=I%L  r<   c               #     #    [         R                  " S5      n U R                  5       nU R                  [         R                  5         S v   U R                  U5        g ! U R                  U5        f = f7f)Ngriffe)logging	getLoggergetEffectiveLevelsetLevelERROR)loggerprevious_levels     r9   _suppress_griffe_loggingrf      sU      x(F--/N
OOGMM"(''s   AA5	A A5A22A5c                4   U R                   n[        R                  " U 5      nU(       d  [        USSS9$ [	        5          [        USU=(       d    [        U5      S9nUR                  5       nSSS5        [        S W 5       S5      nU VVs0 s HN  nUR                  [        R                  :X  d  M#  UR                    H  nUR                  UR                  _M     MP     n	nn[        U R                   UU	=(       d    SS9$ ! , (       d  f       N= fs  snnf )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   rI   r   )linenoparserc              3  x   #    U  H0  oR                   [        R                  :X  d  M"  UR                  v   M2     g 7fN)r)   r   textr8   ).0sections     r9   	<genexpr>.generate_func_documentation.<locals>.<genexpr>   s&     Zf7@T@Y@Y0Yfs   !::)r?   inspectgetdocrG   rf   r   r\   parsenextr)   r   r&   r8   r   r   )
funcr[   r   rS   	docstringparsedr   rn   r7   rI   s
             r9   generate_func_documentationrx      s    ==D
..
C dQUVV	!	#c!E4Y=TUX=YZ	" 
$ #ZfZ\`K *G<</::: 	 ]]E 	

E%%% # 	  * ]]-5  
$	#*s   .D"D3/D
Dc                    SnU n[        U5      [        L a9  [        U5      nU(       d   X!4$ US   n/ UQUSS Q7n[        U5      [        L a  M9  X!4$ )zIReturns the underlying annotation and any metadata from typing.Annotated.r=   r   r   N)r
   r   r	   )
annotationmetadataannargss       r9   _strip_annotatedr~      sk     !#H
C
S/Y
&} = 1g)X)QR) S/Y
& =r<   c                H    U  H  n[        U[        5      (       d  M  Us  $    g)zIExtracts a human readable description from Annotated metadata if present.N)
isinstancer   r{   items     r9   "_extract_description_from_metadatar      s%     dC  K  r<   c                H    U  H  n[        U[        5      (       d  M  Us  $    g)z;Returns the first FieldInfo in Annotated metadata, or None.N)r   r   r   s     r9   !_extract_field_info_from_metadatar      s%     dI&&K  r<   c           
     
   U(       a*  [        X5      n[        UR                  =(       d    0 5      nOSn0 n[        U SS9n0 n	0 n
0 nUR	                  5        H6  u  pUS:X  a  M  [        U5      u  pXU'   XU'   [        U5      nUc  M2  UX'   M8     U
R	                  5        H  u  nnUR                  UU5        M     U=(       d    U(       a  UR                  OU R                  n[        R                  " U 5      n[        UR                  R	                  5       5      nSn/ nU(       a  US   u  nnU	R                  UUR                  5      nU[        R                   :w  a=  [#        U5      =(       d    UnU[$        L d	  U[&        L a  SnO'UR)                  UU45        OUR)                  UU45        USS  H  u  nnU	R                  UUR                  5      nU[        R                   :w  a>  [#        U5      =(       d    UnU[$        L d	  U[&        L a  [+        SU R                   35      eUR)                  UU45        M     0 nU GH  u  nnU	R                  UUR                  5      nUR,                  nU[        R                   :X  a  [.        nUR                  US5      nUR0                  UR2                  :X  aq  [#        U5      [4        L aA  [7        U5      n[9        U5      S	:X  a  US   [:        L a  [        US      nO[        [.           nO	[        U   nU[=        [        US
94UU'   M  UR0                  UR>                  :X  aw  [#        U5      [        L a@  [7        U5      n[9        U5      S	:X  a  [        US   US   4   nO#[        [@        [.        4   nO[        [@        U4   nU[=        [        US
94UU'   GMw  UR                  US5      n[C        U5      n U b  [D        RF                  " U U=(       d    U RH                  S9n!U[        R                   :w  a+  [K        U[D        5      (       d  [D        RF                  " U!US9n!O,[K        U[D        5      (       a  [D        RF                  " U!U5      n!UU!4UU'   GM4  U[        R                   :X  a  U[=        SUS94UU'   GMZ  [K        U[D        5      (       a0  U[D        RF                  " UU=(       d    URH                  S94UU'   GM  U[=        UUS94UU'   GM     [M        U S34S[N        0UD6n"U"RQ                  5       n#U(       a  [S        U#5      n#[U        UU=(       d    U(       a  URH                  OSU"U#UUUS9$ )a7  
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 producing
        correct JSON input.

Returns:
    A `FuncSchema` object containing the function's name, description, parameter descriptions,
    and other metadata.
NT)include_extrasr>   Fr   r   zLRunContextWrapper/ToolContext param found at non-first position in function    )default_factoryr   r=   )r   )default.)r   r   _args__base__)r   r   r   r   r    r"   r#   )+rx   dictrI   r   r'   r~   r   
setdefaultr   r?   rq   r    listr&   getrz   _emptyr
   r   r   r0   r   r   r   r)   r*   tupler	   lenEllipsisr   r,   r   r   r   merge_field_infosr   r   r   r   model_json_schemar   r   )$ru   docstring_stylename_overridedescription_overrideuse_docstring_infor#   doc_infoparam_descstype_hints_with_extras
type_hintsannotated_param_descsparam_metadatar   rz   stripped_annr{   r   	func_namesigparamsr"   filtered_params
first_namefirst_paramr|   originr7   fieldsr   field_descriptionargs_of_tuple	dict_argsfield_info_from_annotatedmergeddynamic_modeljson_schemas$                                       r9   function_schemar      s   @ .tE866<"=+DF!#J,.13N288:8!1*!='4't8B"*5!' ; 388:kt[1 ; O8(--I 

D
!C#..&&()FMO"()
KnnZ)?)?@'.. _+F**f.C $&&
K'@A""J#<= abzennT5#3#34'.. _+F**f.C(  	e}- "  F&ennT5#3#34-- '.. C (OOD$7 ::---#%' (}%*}Q/?8/K}Q/0Cs)C 3i d8IJF4L
 ZZ5,,,#$&$SM	y>Q&y|Yq\9:CsCx.C 38n d8IJF4L &))$3H(I((S%(4"44- 1 Z5N5Z5Z gnn,Z5S5S&88QF33&88IF #V}tGNN* #+<= t GY// //->-U'BUBU t '7HI ta 'l !I;e!4SySFSM  113K/< (XXX-A-ASW+&#-	 	r<   )rS   r   r>   DocstringStylerk   )ru   Callable[..., Any]r[   DocstringStyle | Noner>   rG   )rz   r   r>   ztuple[Any, tuple[Any, ...]])r{   tuple[Any, ...]r>   r   )r{   r   r>   zFieldInfo | None)NNNTT)ru   r   r   r   r   r   r   r   r   r!   r#   r!   r>   r   )-
__future__r   
contextlibrq   r_   rN   collections.abcr   dataclassesr   typingr   r   r   r	   r
   r   r^   r   r   pydanticr   r   r   pydantic.fieldsr   
exceptionsr   run_contextr   strict_schemar   tool_contextr   r   rG   r   r\   contextmanagerrf   rx   r~   r   r   r   r=   r<   r9   <module>r      s0   "    	 $ ! P P 3 3 3 % ! * 4 % 5- 5- 5-p R R R 45
%P ( ( >B'
'%:''T  .2 $'+##H
H*H H %	H
 H H Hr<   