
    i#                       U d Z ddlmZ ddlZddlZddlm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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 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* ddl+m,Z- e*e-fZ.de/d<   ddZ1 ede      Z2 e e3      Z4e G d dee2                Z5 G d d      Z6ddZ7e G d d             Z8y# e0$ r dZ.Y Pw xY w)z?Function introspection and schema generation for FastMCP tools.    )annotationsN)Callable)	dataclass)	AnnotatedAnyGenericUnionget_args
get_originget_type_hints)PydanticSchemaGenerationError)TypeVar)transform_context_annotationswithout_injected_parameters)
ToolResult)compress_schema)
get_logger)AudioFileImagecreate_function_without_paramsget_cached_typeadapterreplace_type)	PrefabApp)	Componentztuple[type, ...]_PREFAB_TYPES c                    t        | t              rt        | t              ryt	        |       }|t
        u s|t        j                  u s|t        u rt        d t        |       D              S y)zSCheck if *tp* is or contains a prefab type, recursing through unions and Annotated.Tc              3  2   K   | ]  }t        |        y w)N)_contains_prefab_type).0as     n/Users/bowang/.openclaw/workspace/ChatDev/.venv/lib/python3.12/site-packages/fastmcp/tools/function_parsing.py	<genexpr>z(_contains_prefab_type.<locals>.<genexpr>.   s     B\(+\s   F)
isinstancetype
issubclassr   r   r	   types	UnionTyper   anyr
   )tporigins     r#   r    r    (   sR    "d
2} =^F&EOO3v7JBXb\BBB    T)defaultc                      e Zd ZU dZded<   y)_WrappedResultz,Generic wrapper for non-object return types.r.   resultN)__name__
__module____qualname____doc____annotations__r   r-   r#   r1   r1   7   s
    6Ir-   r1   c                      e Zd Zy)_UnserializableTypeN)r3   r4   r5   r   r-   r#   r9   r9   >   s    r-   r9   c                J    | j                  d      dk(  ryd| v ryd| v xr d| v S )z1Check if a JSON schema represents an object type.r&   objectT
propertiesz$refz$defs)get)schemas    r#   _is_object_schemar?   B   s<     zz&X% v V16 11r-   c                  z    e Zd ZU ded<   ded<   ded<   ded<   d	ed
<   dZded<   e	 	 	 d	 	 	 	 	 	 	 	 	 dd       Zy)ParsedFunctionCallable[..., Any]fnstrnamez
str | Nonedescriptiondict[str, Any]input_schemazdict[str, Any] | Noneoutput_schemaNr   return_typec                   |rt        j                  |      }|j                  j                         D ]f  }|j                  t         j
                  j                  k(  rt        d      |j                  t         j
                  j                  k(  s]t        d       |ri|D ]d  }||j                  vrt        d| d      |j                  |   }|j                  t         j
                  j                  k(  sWt        d| d       t        |dd       xs |j                  j                  }t        j                  |      }	t        j                  |      s|j                   }t#        |t$              r|j&                  }t)        |      }t+        |      }
|rt-        |
t/        |            }
t1        |
      }|j3                         }|rt/        |      nd }t5        ||d      }d }t        j                  |      }|j6                  }t#        |t8              r 	 t;        |d	      }|j=                  d
|      }|}|t         jD                  d tF        dfvr9tH        rtK        |      rtL        }tO        |tP        jS                  tT        tV        tX        tZ        t\        j^                  j`                  t\        j^                  jb                  t\        j^                  jd                  t\        j^                  jf                  t\        j^                  jh                  g	tH        tL                    }	 t1        |      }|j3                  d      }|r7tk        |      s,tl        |   }t1        |      }|j3                  d      }d|d<   n|}t5        |d      } | |||	||xs d |      S # t>        $ r!}t@        jC                  d|       Y d }~d }~ww xY w# tn        $ r/}dt9        |      vrt@        jC                  d|       Y d }~rd }~ww xY w)Nz/Functions with *args are not supported as toolsz2Functions with **kwargs are not supported as toolszParameter 'z-' in exclude_args does not exist in function.z,' in exclude_args must have a default value.r3   T)prune_paramsprune_titles)include_extrasreturnz5Failed to resolve type hint for return annotation: %s.serialization)modezx-fastmcp-wrap-result)rM   r9   z#Unable to generate schema for type )rC   rE   rF   rH   rI   rJ   )8inspect	signature
parametersvalueskind	ParameterVAR_POSITIONAL
ValueErrorVAR_KEYWORDr/   emptygetattr	__class__r3   getdoc	isroutine__call__r%   staticmethod__func__r   r   r   listr   json_schemar   return_annotationrD   r   r=   	Exceptionloggerdebug_emptyr   r   r    r9   r   dictfromkeysr   r   r   r   mcpr(   TextContentImageContentAudioContentResourceLinkEmbeddedResourcer?   r1   r   )clsrC   exclude_argsvalidatewrap_non_object_output_schemasigparamarg_namefn_namefn_doc
wrapper_fninput_type_adapterrH   rL   rI   output_type
type_hintseoriginal_output_typeclean_output_typetype_adapterbase_schemawrapped_typewrapped_adapters                           r#   from_functionzParsedFunction.from_functionZ   s    ##B'C..0::!2!2!A!AA$%VWW::!2!2!>!>>$L 	 1  ,Hs~~5()(3`a   NN84E}}(9(9(?(??()(3_`  !- "j$/H2<<3H3H#   $Bb,'B +2. 14
 7
DDVWJ3J?)557 .:tL)t&|$
 #++ k3'Y ,BtD
(nnX{C  +w~~tS#>> !6{!C1 !-"		--		..		..		..		22 ' (!&X56GH*66O6L 19J;9W#12C#DL&<\&JO$3$?$?_$?$UM=AM"9:$/M /D Q %'/4,
 	
  YTVWXXYv 1 X(A6LL#F{o!VWXs1   N 'A%O 	O'OO	P%O>>P)NTT)
rC   rB   rs   zlist[str] | Nonert   boolru   r   rO   rA   )r3   r4   r5   r7   rJ   classmethodr   r   r-   r#   rA   rA   Q   s}    
I  ((K *..2P
P
 'P
 	P

 (,P
 
P
 P
r-   rA   )r+   r   rO   r   )r>   rG   rO   r   )9r6   
__future__r   rR   r(   collections.abcr   dataclassesr   typingr   r   r   r	   r
   r   r   	mcp.typesrl   pydanticr   typing_extensionsr   
TypeVarExtfastmcp.server.dependenciesr   r   fastmcp.tools.toolr   fastmcp.utilities.json_schemar   fastmcp.utilities.loggingr   fastmcp.utilities.typesr   r   r   r   r   r   prefab_ui.appr   
_PrefabAppprefab_ui.components.baser   _PrefabComponentr   r7   ImportErrorr    r.   r3   rg   r1   r9   r?   rA   r   r-   r#   <module>r      s    E "   $ ! W W W  2 3 * 9 0 5G'13C&DM#D
 sC 	H	 WQZ  	 	2 Y
 Y
 Y
]  Ms   +C CC