
    .Ui:                       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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 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jJ                  d        Z&	 d	 	 	 	 	 ddZ'ddZ(ddZ)	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ*y)    )annotationsN)	dataclass)	AnnotatedAnyCallableLiteralget_args
get_originget_type_hints)	DocstringDocstringSectionKind)	BaseModelFieldcreate_model)	FieldInfo   )	UserError)RunContextWrapper)ensure_strict_json_schema)ToolContextc                  v    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y)
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_schemac                
   g }i }d}t        | j                  j                  j                               D ]  \  }\  }}| j                  r|dk(  rt        ||d      }|j                  |j                  k(  r|j                  |xs g        d}Y|j                  |j                  k(  r|j                  |xs i        |j                  |j                  |j                  fv r|s|j                  |       |||<   |||<    ||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)	selfdatapositional_argskeyword_argsseen_var_positionalidxr   paramvalues	            r/home/jay/workspace/tools/ai-image-gen/jaaz-app/server/venv/lib/python3.12/site-packages/agents/function_schema.pyto_call_argsz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).L& &+T"+	+, ,,    N)r1   r   returnz tuple[list[Any], dict[str, Any]])__name__
__module____qualname____doc____annotations__r!   r"   r9    r:   r8   r   r      sW     I#***E&&Y  (M4_##= -r:   r   c                  4    e Zd ZU dZded<   	 ded<   	 ded<   y)	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@   rA   r:   r8   rC   rC   M   s    R
I3F--Qr:   rC   )googlenumpysphinxc                   dddd}g d}|D ]5  }t        j                  || t         j                        s)|dxx   dz  cc<   7 g d}|D ]5  }t        j                  || t         j                        s)|dxx   dz  cc<   7 g d}|D ]5  }t        j                  || t         j                        s)|d	xx   dz  cc<   7 t        |j	                               }|dk(  ry	g d}|D ]  }||   |k(  s|c S  y	)
Nr   )rG   rF   rE   )z	^:param\sz^:type\sz	^:return:z^:rtype:rG   r   )z^Parameters\s*\n\s*-{3,}z^Returns\s*\n\s*-{3,}z^Yields\s*\n\s*-{3,}rF   )z^(Args|Arguments):z^(Returns):z
^(Raises):rE   )research	MULTILINEmaxvalues)	docscoressphinx_patternspatternnumpy_patternsgoogle_patterns	max_scorestylesstyles	            r8   _detect_docstring_stylerW   ^   s   34qA(NF MO" "99Wc2<<08!"N
 " !99Wc2<<07Oq O!
 MO" "99Wc2<<08!" FMMO$IA~ $AF %=I%L r:   c               #     K   t        j                  d      } | j                         }| j                  t         j                         	 d  | j                  |       y # | j                  |       w xY ww)Ngriffe)logging	getLoggergetEffectiveLevelsetLevelERROR)loggerprevious_levels     r8   _suppress_griffe_loggingra      sV      x(F--/N
OOGMM"(''s   AA4A A4A11A4c                
   | j                   }t        j                  |       }|st        |dd      S t	               5  t        |d|xs t        |            }|j                         }ddd       t        d D        d      }|D ci c]G  }|j                  t        j                  k(  r(|j                  D ]  }|j                  |j                   I }	}}t        | j                   ||	xs d      S # 1 sw Y   xY wc c}}w )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   rD   r   )linenoparserc              3  p   K   | ].  }|j                   t        j                  k(  s!|j                   0 y wN)r(   r   textr7   ).0sections     r8   	<genexpr>z.generate_func_documentation.<locals>.<genexpr>   s&     Z7@T@Y@Y0YZs   "66)r<   inspectgetdocrC   ra   r   rW   parsenextr(   r   r%   r7   r   r   )
funcrV   r   rN   	docstringparsedr   ri   r6   rD   s
             r8   generate_func_documentationrr      s    ==D
..
C dQUVV	!	# #c!E4Y=TUX=YZ	"# #ZfZ\`K *<</:::]]	*  	

E%%%** * ]]-5 # #*s   ,C3AC?3C<c                    d}| }t        |      t        u r3t        |      }|s	 ||fS |d   }g ||dd }t        |      t        u r3||fS )zIReturns the underlying annotation and any metadata from typing.Annotated.rA   r   r   N)r
   r   r	   )
annotationmetadataannargss       r8   _strip_annotatedrx      so     !#H
C
S/Y
&} = 1g)X)QR) S/Y
& =r:   c                :    | D ]  }t        |t              s|c S  y)zIExtracts a human readable description from Annotated metadata if present.N)
isinstancer   )ru   items     r8   "_extract_description_from_metadatar|      s'      dC K r:   c           	        |r&t        | |      }t        |j                  xs i       }nd}i }t        | d      }i }	i }
|j	                         D ]1  \  }}|dk(  rt        |      \  }}||	|<   t        |      }|-||
|<   3 |
j	                         D ]  \  }}|j                  ||        |xs |r|j                  n| j                  }t        j                  |       }t        |j                  j	                               }d}g }|r|d   \  }}|	j                  ||j                        }|t        j                   k7  r6t#        |      xs |}|t$        u s|t&        u rd}n'|j)                  ||f       n|j)                  ||f       |dd D ]~  \  }}|	j                  ||j                        }|t        j                   k7  r7t#        |      xs |}|t$        u s|t&        u rt+        d| j                         |j)                  ||f        i }|D ]  \  }}|	j                  ||j                        }|j,                  }|t        j                   k(  rt.        }|j                  |d      }|j0                  |j2                  k(  rpt#        |      t4        u r?t7        |      }t9        |      d	k(  r|d   t:        u rt        |d      }nt        t.           }n	t        |   }|t=        t        |
      f||<   |j0                  |j>                  k(  rwt#        |      t        u r?t7        |      }t9        |      d	k(  rt        |d   |d   f   }n#t        t@        t.        f   }nt        t@        |f   }|t=        t        |
      f||<   s|t        j                   k(  r|t=        d|      f||<   tC        |tD              r,|tE        jF                  ||xs |jH                        f||<   |t=        ||      f||<    tK        | dfdtL        i|}|jO                         } |rtQ        |       } tS        ||xs |r|jH                  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 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   )defaultr   _args__base__)r   r   r   r   r   r!   r"   )*rr   dictrD   r   r&   rx   r|   
setdefaultr   r<   rk   r   listr%   getrt   _emptyr
   r   r   r/   r   r   r   r(   r)   tupler	   lenEllipsisr   r+   r   rz   r   merge_field_infosr   r   r   model_json_schemar   r   )!ro   docstring_stylename_overridedescription_overrideuse_docstring_infor"   doc_infoparam_descstype_hints_with_extras
type_hintsannotated_param_descsr   rt   stripped_annru   r   	func_namesigparamsr!   filtered_params
first_namefirst_paramrv   originr6   fieldsr   field_descriptionargs_of_tuple	dict_argsdynamic_modeljson_schemas!                                    r8   function_schemar      s   @ .t_E866<"=+DF!#J,.288: 	6j8!1*!=h'
48B"*5!$'	6 388: 2kt[12 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& 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 '..( #+<= t GY/ //->-U'BUBU t '7HI tGFR !I;e!4SySFSM  113K/< (XXX-A-ASW+&#-	 	r:   )rN   r   r;   DocstringStylerf   )ro   Callable[..., Any]rV   DocstringStyle | Noner;   rC   )rt   r   r;   ztuple[Any, tuple[Any, ...]])ru   ztuple[Any, ...]r;   r   )NNNTT)ro   r   r   r   r   r   r   r   r   r    r"   r    r;   r   )+
__future__r   
contextlibrk   rZ   rI   dataclassesr   typingr   r   r   r   r	   r
   r   rY   r   r   pydanticr   r   r   pydantic.fieldsr   
exceptionsr   run_contextr   strict_schemar   tool_contextr   r   rC   r   rW   contextmanagerra   rr   rx   r|   r   rA   r:   r8   <module>r      s)   "    	 ! Z Z Z 2 3 3 % ! * 4 % 5- 5- 5-p R R R 45
%P ( ( >B'
'%:''T  .2 $'+##y
y*y y %	y
 y y yr:   