
    i)                        d Z ddlm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 ddlmZ erddlmZ dd	lmZ dd
lmZ ddlmZ  ej0                  d      Z G d de      Zy)z2Namespace transform for prefixing component names.    )annotationsN)Sequence)TYPE_CHECKING)GetPromptNextGetResourceNextGetResourceTemplateNextGetToolNext	Transform)VersionSpec)Prompt)Resource)ResourceTemplate)Toolz^([^:]+://)(.*?)$c                      e Zd ZdZddZddZddZddZddZddZ	ddZ
d	d
	 	 	 	 	 	 	 ddZddZd	d
	 	 	 	 	 	 	 ddZ	 	 	 	 ddZd	d
	 	 	 	 	 	 	 ddZddZd	d
	 	 	 	 	 	 	 ddZy	) 	Namespaceu  Prefixes component names with a namespace.

    - Tools: name → namespace_name
    - Prompts: name → namespace_name
    - Resources: protocol://path → protocol://namespace/path
    - Resource Templates: same as resources

    Example:
        ```python
        transform = Namespace("math")
        # Tool "add" becomes "math_add"
        # Resource "file://data.txt" becomes "file://math/data.txt"
        ```
    c                &    || _         | d| _        y)zjInitialize Namespace transform.

        Args:
            prefix: The namespace prefix to apply.
        _N)_prefix_name_prefix)selfprefixs     s/Users/bowang/.openclaw/workspace/ChatDev/.venv/lib/python3.12/site-packages/fastmcp/server/transforms/namespace.py__init__zNamespace.__init__,   s     %haL    c                "    d| j                   dS )Nz
Namespace())r   )r   s    r   __repr__zNamespace.__repr__5   s    DLL+1--r   c                "    | j                    | S )z!Apply namespace prefix to a name.)r   r   names     r   _transform_namezNamespace._transform_name<   s    ##$TF++r   c                j    |j                  | j                        r|t        | j                        d S y)z9Remove namespace prefix from a name, or None if no match.N)
startswithr   lenr   s     r   _reverse_namezNamespace._reverse_name@   s0    ??4,,-D--.011r   c                    t         j                  |      }|r&|j                         \  }}| | j                   d| S |S )uH   Apply namespace to a URI: protocol://path → protocol://namespace/path./)_URI_PATTERNmatchgroupsr   )r   urir)   protocolpaths        r   _transform_urizNamespace._transform_uriJ   sB    ""3'"\\^NHdZ~Qtf55
r   c                    t         j                  |      }|rF|j                         \  }}| j                   d}|j	                  |      r| |t        |      d  S yy)z1Remove namespace from a URI, or None if no match.r'   N)r(   r)   r*   r   r#   r$   )r   r+   r)   r,   r-   r   s         r   _reverse_urizNamespace._reverse_uriR   sa    ""3'"\\^NHd~Q'Fv&"DV$7#899r   c           	        K   |D cg c]/  }|j                  d| j                  |j                        i      1 c}S c c}w w)z!Prefix tool names with namespace.r    update
model_copyr!   r    )r   toolsts      r   
list_toolszNamespace.list_toolsa   sK      RW
QVAALL)=)=aff)E FLGQV
 	
 
   A4>ANversionc                  K   | j                  |      }|y |||       d{   }|r|j                  d|i      S y7 w)zGet tool by namespaced name.Nr:   r    r2   r%   r5   )r   r    	call_nextr;   originaltools         r   get_toolzNamespace.get_toolg   sP      %%d+x99??64.?99 :   "AA Ac                   K   |D cg c]8  }|j                  d| j                  t        |j                              i      : c}S c c}w w)z,Add namespace path segment to resource URIs.r+   r2   )r5   r.   strr+   )r   	resourcesrs      r   list_resourceszNamespace.list_resourcesw   sP      
 LL(;(;CJ(G HLI
 	
 
s   A=AAc                  K   | j                  |      }|y |||       d{   }|r|j                  d|i      S y7 w)zGet resource by namespaced URI.Nr:   r+   r2   )r0   r5   )r   r+   r>   r;   r?   resources         r   get_resourcezNamespace.get_resource~   sS      $$S)"8W==&&ucl&;; >rB   c           	        K   |D cg c]/  }|j                  d| j                  |j                        i      1 c}S c c}w w)z,Add namespace path segment to template URIs.uri_templater2   )r5   r.   rL   )r   	templatesr7   s      r   list_resource_templatesz!Namespace.list_resource_templates   sL      
 LL1D1DQ^^1T ULV
 	
 
r9   c                  K   | j                  |      }|y |||       d{   }|r-|j                  d| j                  |j                        i      S y7 4w)z(Get resource template by namespaced URI.Nr:   rL   r2   )r0   r5   r.   rL   )r   r+   r>   r;   r?   templates         r   get_resource_templatezNamespace.get_resource_template   sn      $$S)"8W==&&&(;(;H<Q<Q(RS '    >s   "AA5Ac           	        K   |D cg c]/  }|j                  d| j                  |j                        i      1 c}S c c}w w)z#Prefix prompt names with namespace.r    r2   r4   )r   promptsps      r   list_promptszNamespace.list_prompts   sK      RY
QXAALL)=)=aff)E FLGQX
 	
 
r9   c                  K   | j                  |      }|y |||       d{   }|r|j                  d|i      S y7 w)zGet prompt by namespaced name.Nr:   r    r2   r=   )r   r    r>   r;   r?   prompts         r   
get_promptzNamespace.get_prompt   sS      %%d+ 7;;$$VTN$;; <rB   )r   rD   returnNone)rY   rD   )r    rD   rY   rD   )r    rD   rY   
str | None)r+   rD   rY   rD   )r+   rD   rY   r[   )r6   Sequence[Tool]rY   r\   )r    rD   r>   r	   r;   VersionSpec | NonerY   zTool | None)rE   Sequence[Resource]rY   r^   )r+   rD   r>   r   r;   r]   rY   zResource | None)rM   Sequence[ResourceTemplate]rY   r_   )r+   rD   r>   r   r;   r]   rY   zResourceTemplate | None)rS   Sequence[Prompt]rY   r`   )r    rD   r>   r   r;   r]   rY   zPrompt | None)__name__
__module____qualname____doc__r   r   r!   r%   r.   r0   r8   rA   rG   rJ   rN   rQ   rU   rX    r   r   r   r      s   ).,	
 SW

$/
=O
	
 
 '+ #
 $ 
(
3
	#
 '+ +
 $ 
!,
 UY

$1
?Q
	
r   r   )rd   
__future__r   recollections.abcr   typingr   fastmcp.server.transformsr   r   r   r	   r
   fastmcp.utilities.versionsr   fastmcp.prompts.promptr   fastmcp.resources.resourcer   fastmcp.resources.templater   fastmcp.tools.toolr   compiler(   r   re   r   r   <module>rq      sS    8 " 	 $    3-3;' rzz./e	 er   