
    i                         d Z ddlmZmZ ddlmZ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 dd	lmZ dd
lmZ  G d de      Z ed      Z G d d      Zy)z*Abstract base classes for agent providers.    )ABCabstractmethod)AnyDictListOptional)AgentConfig)Message)register_model_provider_schema)ToolSpec)ModelResponse)
TokenUsage)Registryc            
           e Zd ZdZdefdZed        Ze	 ddee	   dee
   deee      d	efd
       Zede
d	efd       Zy)ModelProviderz,Abstract base class for all agent providers.configc                 "   || _         |j                  | _        |j                  | _        t        |j                  t
              r|j                  nt        |j                        | _        |j                  | _        |j                  xs i | _        y)z
        Initialize the agent provider with configuration.
        
        Args:
            config: Agent configuration instance
        N)	r   base_urlapi_key
isinstancenamestr
model_nameproviderparams)selfr   s     N/Users/bowang/.openclaw/workspace/ChatDev/runtime/node/agent/providers/base.py__init__zModelProvider.__init__   s_     ~~)3FKK)E&++3v{{K[mm)r    c                      y)z
        Create and return the appropriate client for this provider.
        
        Returns:
            Client instance for making API calls
        N )r   s    r   create_clientzModelProvider.create_client   s     	r   Nconversationtimeline
tool_specsreturnc                      y)a  
        Call the model with the given messages and parameters.
        
        Args:
            client: Provider-specific client instance
            conversation: List of messages in the conversation
            tool_specs: Tool specifications available for this call
            **kwargs: Additional parameters for the model call
            
        Returns:
            ModelResponse containing content and potentially tool calls
        Nr!   )r   clientr#   r$   r%   kwargss         r   
call_modelzModelProvider.call_model)   s    * 	r   responsec                      y)z
        Extract token usage from the API response.
        
        Args:
            response: Raw API response from the model call
            
        Returns:
            TokenUsage instance with token counts
        Nr!   )r   r+   s     r   extract_token_usagez!ModelProvider.extract_token_usage@   s     	r   N)__name__
__module____qualname____doc__r	   r   r   r"   r   r
   r   r   r   r   r*   r   r-   r!   r   r   r   r      s    6*{ *    04 7m s)	
 T(^, 
 , 
C 
J 
 
r   r   agent_providerc                       e Zd ZdZeddddedededz  dedz  ddf
d	       Zedededz  fd
       Zede	e   fd       Z
edeeeeef   f   fd       Zy)ProviderRegistryz$Registry facade for agent providers.Nlabelsummaryr   provider_classr7   r8   r&   c                    ||d}|j                         D ci c]  \  }}|	|| }}}t        j                  |||       t        |||       y c c}}w )Nr6   )targetmetadata)items_provider_registryregisterr   )clsr   r9   r7   r8   r<   keyvalues           r   r?   zProviderRegistry.registerT   sd     

 2:1AW1A:3UEVCJ1AW##D(#S&t5'J Xs
   
AAc                 l    	 t         j                  |      }|j                         S # t        $ r Y y w xY wr.   )r>   get	Exceptionloadr@   r   entrys      r   get_providerzProviderRegistry.get_providerf   s9    	&**40E zz|  		s   ' 	33c                 <    t        t        j                               S r.   )listr>   names)r@   s    r   list_providerszProviderRegistry.list_providersn   s    &,,.//r   c           	          t         j                         D ci c]  \  }}|t        |j                  xs i       ! c}}S c c}}w r.   )r>   r=   dictr<   rG   s      r   iter_metadatazProviderRegistry.iter_metadatar   s<    DVD\D\D^_D^[T5d5>>/R00D^___s   $A )r/   r0   r1   r2   classmethodr   typer?   rI   r   rM   r   r   rP   r!   r   r   r5   r5   Q   s    . !"KK K
 TzK tK 
K K"  t   0tCy 0 0 `d3S#X#67 ` `r   r5   N)r2   abcr   r   typingr   r   r   r   entity.configsr	   entity.messagesr
   schema_registryr   entity.tool_specr   %runtime.node.agent.providers.responser   utils.token_trackerr   utils.registryr   r   r>   r5   r!   r   r   <module>r\      sM    0 # , , & # : % ? * #=C =@ ./ #` #`r   