
    i+_              	          d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	m
Z
mZmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZmZ dd	lm Z m!Z!  ed
      Z"dZ#ddde$de%e   de$dz  ddfdZ&de$de%e   fdZ'de
e$e%e   f   fdZ(de
e$e
e$e	f   f   fdZ)e G d de             Z*e G d de             Z+e G d de             Z,e G d de             Z- e&de+d        e&d e,d!        e&d"e-d#       e G d$ d%e             Z.y)&zTooling configuration models.    N)deepcopy)	dataclassfieldreplace)AnyDictListMappingTuple)
BaseConfigConfigErrorConfigFieldSpec
EnumOptionChildKeyensure_listoptional_booloptional_strrequire_mappingrequire_strextend_path)enum_options_from_values)RegistryRegistryError)FunctionCatalogget_function_catalogtooling_typez:All)descriptionname
config_clsr   returnc                D    |rd|ind }t         j                  | ||       y )Nsummary)targetmetadata)tooling_type_registryregister)r   r   r   r$   s       H/Users/bowang/.openclaw/workspace/ChatDev/entity/configs/node/tooling.pyregister_tooling_typer(      s'     ,7	;'DH""4
X"N    c                     t         j                  |       }|j                         }t        |t              rt        |t              st        d|  d      |S )NzEntry 'z' is not a BaseConfig subclass)r%   getload
isinstancetype
issubclassr   r   )r   entryr   s      r'   get_tooling_type_configr1   (   sK    !%%d+EJj$'z*j/QgdV+IJKKr)   c                  z    t         j                         D  ci c]  \  } }| |j                          c}} S c c}} w N)r%   itemsr,   r   r0   s     r'   iter_tooling_type_registrationsr6   0   s4    2G2M2M2OP2O;4D%**,2OPPPs   7c            	          t         j                         D  ci c]  \  } }| t        |j                  xs i       ! c}} S c c}} w r3   )r%   r4   dictr$   r5   s     r'   iter_tooling_type_metadatar9   4   s<    @U@[@[@]^@]uD$u~~+,,@]^^^s   $A c                        e Zd ZU dZdZedz  ed<   dZedz  ed<   dZe	ee
f   dz  ed<   dZeed<   d eddd	dd
      iZede	eef   f fd       Z xZS )FunctionToolEntryConfigz4Schema helper used to describe per-function options.Nr   r   
parametersT	auto_fillzFunction Namestrz7Function name from functions/function_calling directoryr   display_name	type_hintrequiredr   r    c                 .   t         |          }t               }|j                         }|j	                  d      }|W|j
                  xs d}d }d }|j                  r| d|j                   d}n|s| d}ng }g }|D ]  \  }}	| t         }
|j                  |
       dj                  d |	d d D              }t        |	      dkD  rd	nd
}| d}|j                  t        |
|
dt        |	       d| |rd| | dnd
z                 |D ]o  \  }}	|	D ]e  }| d|j                   }|j                  |j                         |j
                  xs d}|j                  t        |j                  ||             g q t        ||||      |d<   |S )Nr   zFunction namez (loading failed: )z" (no functions found in directory), c              3   4   K   | ]  }|j                     y wr3   )r   ).0metas     r'   	<genexpr>z6FunctionToolEntryConfig.field_specs.<locals>.<genexpr>x   s     'Hid		is      z... z.pyz	Load all z functions from z ()valuelabelr   :z*This function does not provide a docstring)enumenum_optionsr   )superfield_specsr   iter_modulesr+   r   
load_errorMODULE_ALL_SUFFIXappendjoinlenr   r   r   )clsspecscatalogmodules	name_specr   rP   enum_valuesmodule_namemetas	all_labelpreviewsuffixmodule_hintrH   rM   option_description	__class__s                    r'   rR   z#FunctionToolEntryConfig.field_specsd   s   #%&(&&(IIf%	 #//B?K48L,0K!!!,-?@R@R?SSTU!,-OP! *1&K#.-0A/B CI&&y1"ii'HeBQi'HHG&)%j1nU"F%0M"5K ''""+"+"+CJ<7G} U>ERy#:2!O		 +2  +2&K %#.-q <#**4995-1-=-=-mAm*$++&&*ii&+,>	 !& +2 $ )'	E&M r)   )__name__
__module____qualname____doc__r   r>   __annotations__r   r<   r   r   r=   boolr   FIELD_SPECSclassmethodrR   __classcell__rf   s   @r'   r;   r;   8   s    >D#*"Kt"(,JS#X%,It 	(Q
!KF 2Do!56 2 2r)   r;   c                      e Zd ZU eeeef      ed<   dZe	ed<   dZ
edz  ed<    eddddde	       edd
dddd      dZedeeef   dedd fd       Zedededz  fd       Zededededeeef   deeeeef   ef      f
d       Zy)FunctionToolConfigtoolsT	auto_loadNtimeoutzFunction Tool Listzlist[FunctionToolEntryConfig]z%Function tool list, at least one item)r   r@   rA   rB   r   childzExecution TimeoutfloatFz Tool execution timeout (seconds)r   r@   rA   rB   r   advance)rs   ru   datapathr    c          	         t        ||      }t        |j                  d            }|st        dt	        |d            t               }g }t        |      D ]  \  }}t	        |d| d      }	t        |t              st        d|	      t        |      }
|
j                  d      }t        |t              r|j                         st        dt	        |	d            |j                         }||
d<   | j                  |      }|r%|j                  | j                  |||	|
             |j                  |
|	f        g }i }|D ]w  \  }}t        |      }
|
j                  d      }t        |t              r|j                         st        dt	        |d            |j                  |      }|t        d	| d
t	        |d            |j                  |      }|t        d	| d| dt	        |d            |||<   |
j                  dd      }t        |t               st        dt	        |d            t        |
      }|rU|j                  d      s|j"                  r|j"                  |d<   |j                  d      st%        |j&                        |d<   |j)                  dd        |j                  |       z t+        |d|d      }|j                  d      }|,t        |t,        t.        f      st        dt	        |d             | ||t!        |      ndt        |t,        t.        f      rt/        |      |      S d |      S )Nrs   z+tools must be provided for function toolingztools[]ztool entry must be a mappingr   ztool name is required)r_   r[   r{   originalz
function 'z$' not found under function directoryz&' is declared multiple times (also in rD   r=   Tzauto_fill must be booleanr   r<   rt   defaultru   timeout must be numeric)rs   rt   ru   r{   )r   r   r+   r   r   r   	enumerater-   r
   r8   r>   strip_extract_module_from_allextend_expand_module_all_entryrV   rl   r   r   parameters_schemapopr   intrw   )rY   rz   r{   mappingrs   r[   expanded_toolsidxtool	tool_path
normalizedraw_namer   r_   
tool_specsseen_functionsr0   
entry_pathr$   previousr=   mergedrt   timeout_values                           r'   	from_dictzFunctionToolConfig.from_dict   sH   !$-GKK01K[Y]_fMghh&(;="5)IC#DF3%q/:IdG,!"@)LLdJ!~~f-Hh,HNN4D!"9;yRX;YZZ>>#D!%Jv66t<K%%00$/ '&!+	 1  !!:y"9:+ *. ,.
)+!/E:eJ>>&)DdC(

!"9;zSY;Z[[{{4(H! &JK
F3  &))$/H#! &LXJVWX
F3  $.N4 "{D9Ii.!"={:Wb?cdd*%Fzz-0X5I5I,4,@,@F=)zz,/+3H4N4N+OF<(JJ{D)f%; "0> "';dK	I.$ZU|-T7T99UVV )2)>d9oD,6}sEl,SE-(
 	
 Z^
 	
r)   rL   c                 |    | j                  t              sy | d t        t                j                         }|xs d S r3   )endswithrU   rX   r   )rL   modules     r'   r   z+FunctionToolConfig._extract_module_from_all  s;    ~~/00#/001779~r)   r_   r[   r~   c                 6   dD cg c]	  }||v s| }}|r2dj                  |      }t        |  t         d| t        |d            |j	                  |       }|st        d|  dt        |d            g }|D ]  }	|j                  d|	i|f        |S c c}w )N)r   r<   r=   rE   z does not support overriding r   zmodule 'z+' has no functions under function directory)rW   r   rU   r   functions_for_modulerV   )
r_   r[   r{   r~   key
disallowedfields	functionsentriesfn_names
             r'   r   z+FunctionToolConfig._expand_module_all_entry  s     &Pc%OcSVZbSbc%O
cYYz*F- 122OPVxXD&)  00=	;-'RSD&)  57 GNNVW-t45 !! ds
   	BB)rg   rh   ri   r	   r   r>   r   rk   rt   rl   ru   rw   r   r;   rm   rn   r
   r   staticmethodr   r   r   r    r)   r'   rr   rr      s0   S#XIt GUT\  !-5?)
" #,:
%!KF M
WS#X. M
 M
AU M
 M
^  d
    ! 	
 #s(# 
eDcNC'(	) r)   rr   c                   4   e Zd ZU eed<    ee      Zeeef   ed<   dZ	e
dz  ed<   dZe
ed<   dZee   dz  ed<    edd	d
dd       edddddd       edddddd       edddddd       edddddd      dZedeeef   dedd fd       Zdefd Zy)!McpRemoteConfigserverdefault_factoryheadersNru           	cache_ttltool_sourceszMCP Server URLr>   Tz"HTTP(S) endpoint of the MCP serverr?   zCustom Headersdict[str, str]Fz/Additional request headers (e.g. Authorization)rx   zClient Timeoutrw   zPer-request timeout in secondsTool Cache TTL@Seconds to cache MCP tool list; 0 disables cache for hot updateszTool Sources Filter	list[str]z[Only include MCP tools whose meta.source is in this list; omit to default to ['mcp_tools'].)r   r   ru   r   r   rz   r{   r    c          
         t        ||      }t        |d|      }|j                  d      }i }|[t        |t              st        dt        |d            |j                         D ci c]  \  }}t        |      t        |       }}}|j                  d      }	|	d }
n8t        |	t        t        f      rt        |	      }
nt        dt        |d            |j                  dd      }|d}n8t        |t        t        f      rt        |      }nt        dt        |d            |j                  d	      }d }|qt        |      }g }t        |      D ]S  \  }}t        |t              st        d
t        |d| d            |j                         }|sC|j                  |       U |}ndg} | |||
|||      S c c}}w )Nr   r   zheaders must be a mappingru   r   r   r   cache_ttl must be numericr   z&tool_sources must be a list of stringsztool_sources[r}   	mcp_tools)r   r   ru   r   r   r{   )r   r   r+   r-   r
   r   r   r4   r>   r   rw   r   r   r   rV   )rY   rz   r{   r   r   headers_rawr   kvr   ru   cache_ttl_valuer   tool_sources_rawr   r   r   r   r0   rL   s                       r'   r   zMcpRemoteConfig.from_dicth  s   !$-Wh5kk),"$"k73!"={4QZ?[\\2=2C2C2EF2E$!Qs1vs1v~2EGFI. GU|4M*G7T99UVV!++k37"I#u6o.I9;t[;YZZ";;~6)-'!"23G$&J'0
U!%-%@#DM#a*@A  %%e, 1 &L'=L%
 	
K Gs   (F>c                     | j                   t        t        | j                  j	                                     | j
                  f}t        j                  t        |      j                  d            j                         S )Nutf-8)r   tuplesortedr   r4   ru   hashlibsha1reprencode	hexdigestselfpayloads     r'   	cache_keyzMcpRemoteConfig.cache_key  sZ    KK&++-./LL

 ||DM009:DDFFr)   )rg   rh   ri   r>   rk   r   r8   r   r   ru   rw   r   r   r	   r   rm   rn   r
   r   r   r   r   r)   r'   r   r   6  s   K#D9GT#s(^9 GUT\ Iu%)L$s)d") ")<
 #)&I
 #)8
 %)Z
 (.!u
A(KT 5
WS#X. 5
 5
AR 5
 5
nG3 Gr)   r   c                      e Zd ZU eed<    ee      Zee   ed<   dZ	edz  ed<    ee
      Zeeef   ed<   dZeed<   d	Zeed
<   dZedz  ed<   dZeed<    eddddd       eddddd       edddddd       edddddd       eddddddd       ed
d d!dd	d"d       edd#ddd$d       edd%d!dd&d      d'Zed(eeef   d)ed*d fd+       Zd*efd,Zy)-McpLocalConfigcommandr   argsNcwdenvTinherit_env      $@startup_timeoutwait_for_logr   r   zLaunch Commandr>   z8Executable used to start the MCP stdio server (e.g. uvx)r?   	Argumentsr   Fz)Command arguments, defaults to empty listzWorking Directoryz1Optional working directory for the launch commandrx   zEnvironment Variablesr   z0Additional environment variables for the processzInherit Parent Envrl   z:Whether to start from parent env before applying overrides)r   r@   rA   rB   r   r   ry   zStartup Timeoutrw   z"Seconds to wait for readiness logszReady Log Patternz6Regex that marks readiness when matched against stdoutr   r   )r   r   r   r   r   r   r   r   rz   r{   r    c                   t        ||      }t        |d|      }t        |j                  d            }g }t	        |      D ]B  \  }}t        |d| d      }	t        |t              st        d|	      |j                  |       D t        |d|      }
t        |d|d	      }|d}|j                  d
      }|\t        |t              st        dt        |d
            |j                         D ci c]  \  }}t        |      t        |       }}}ni }|j                  dd      }|d}n8t        |t        t        f      rt        |      }nt        dt        |d            t        |d|      }|j                  dd      }|d}n8t        |t        t        f      rt        |      }nt        dt        |d             | |||
|t!        |      ||||	      S c c}}w )Nr   r   zargs[r}   zargs entries must be stringsr   r   Tr   r   zenv must be a mappingr   r   zstartup_timeout must be numericr   r   r   r   )	r   r   r   r   r   r   r   r   r{   )r   r   r   r+   r   r   r-   r>   r   rV   r   r   r
   r4   r   rw   rl   )rY   rz   r{   r   r   args_rawnormalized_argsr   argarg_pathr   r   env_mappingr   r   r   r   r   r   r   r   s                        r'   r   zMcpLocalConfig.from_dict  s   !$-gy$7w{{623%'!(+HC"45Q8Hc3'!"@(KK""3'	 , 7E40#G]D$OKkk%("k73!"9;tU;STT.9.?.?.AB.Ada3q63q6>.ACBC$5t< "OU|4#M2O?TSdAeff#G^TB!++k37"I#u6o.I9;t[;YZZ [)+%

 
	
) Cs   2Gc           	      x   | j                   t        | j                        | j                  xs dt        t	        | j
                  j                                     | j                  | j                  | j                  xs df}t        j                  t        |      j                  d            j                         S )NrK   r   )r   r   r   r   r   r   r4   r   r   r   r   r   r   r   r   r   s     r'   r   zMcpLocalConfig.cache_key,  s    LL$))HHN&)*+  #
 ||DM009:DDFFr)   )rg   rh   ri   r>   rk   r   listr   r	   r   r8   r   r   r   rl   r   rw   r   r   r   rm   rn   r
   r   r   r   r   r)   r'   r   r     s   LD1D$s)1Ct5Cc3h5K!OU!#L#*#Iu #)R
  $!C
 ,K
 0&J
 '-T
 +"*<
 (,P
 %)Z
sAKF 2
WS#X. 2
 2
AQ 2
 2
h
G3 
Gr)   r   functionzUse local Python functions)r   r   
mcp_remotez#Connect to an HTTP-based MCP server	mcp_localz.Launch and connect to a local stdio MCP serverc            	       
    e Zd ZU eed<   dZedz  ed<   dZedz  ed<    eddddd	       edd
dddd       eddddd	      dZ	e
deeee   f   fd       Ze
deeef   dedd fd       Ze
deeef   f fd       Z xZS )ToolingConfigr.   Nconfigprefixz	Tool Typer>   TzfSelect a tooling adapter registered via tooling_type_registry (function, mcp_remote, mcp_local, etc.).r?   zTool PrefixFzXOptional prefix for all tools from this source to prevent name collisions (e.g. 'mcp1').rx   zTool ConfigurationobjectzConfiguration block validated by the chosen tool type (Python function list, MCP server settings, local command MCP launch, etc.).)r.   r   r   r    c                 |    t               j                         D ci c]  \  }}t        d|      | c}}S c c}}w )Nr   )r   rL   )r6   r4   r   )rY   r   r   s      r'   child_routeszToolingConfig.child_routesh  sG     %D$E$K$K$M
$M j 840*<$M
 	
 
s   8rz   r{   c                   t        ||      }t        |d|      }	 t        |      }|j                  d      }|t	        dt        |d            |j                  |t        |d            }t        |d|      }	 | |||	|      S # t        $ r>}t	        dt        t               j                                t        |d            |d }~ww xY w)Nr.   ztooling.type must be one of r   ztooling requires config block)r{   r   )r.   r   r   r{   )r   r   r1   r   r   r   r6   keysr   r+   r   r   )
rY   rz   r{   r   r   r   excconfig_payload
config_objr   s
             r'   r   zToolingConfig.from_dicto  s    !$-"7FD9	0>J !X.!={4QY?Z[[)).{4QY?Z)[
gx6ZTRR  	.t4S4U4Z4Z4\/].^_D&) 	s   B 	C9CCc           	      \   t         	|          }|j                  d      }|rt               }t	               }t        |j                               }|r|d   nd }|D ci c]'  }||j                  |      xs i j                  d      ) }}t        |||t        ||            |d<   |S c c}w )Nr.   r   r"   )rO   r   rP   )	rQ   rR   r+   r6   r9   r   r   r   r   )
rY   rZ   	type_specregistrationsr$   
type_namesdefault_valuer   descriptionsrf   s
            r'   rR   zToolingConfig.field_specs  s    #%IIf%	;=M13Hm0023J-7JqMTMXbcXbPTD8<<#5#;"@"@"KKXbLc#%5j,O	E&M  ds   ,B))rg   rh   ri   r>   rk   r   r   r   r   rm   rn   r   r   r.   r   r
   r   r   rR   ro   rp   s   @r'   r   r   I  s   
I $FJ$FC$J  $ A
 "&r
 "- ]
!K2 
T(D,<"<= 
 
 SWS#X. S S S S( Do!56  r)   r   )/rj   r   copyr   dataclassesr   r   r   typingr   r   r	   r
   r   entity.configs.baser   r   r   r   r   r   r   r   r   r   r   entity.enum_optionsr   utils.registryr   r   utils.function_catalogr   r   r%   rU   r>   r.   r(   r1   r6   r9   r;   rr   r   r   r   r   r)   r'   <module>r      s   #   1 1 2 2    9 2 H !0   #	O
O Z O t	O
 
O# $z*: Qc4
3C.C)D Q_Dd38n)<$= _ ^j ^ ^B X X Xv oGj oG oGd LGZ LG LG\ !,
 5
 @ JJ J Jr)   