
    :j?                        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mZ ddlmZmZmZ e G d d             Ze G d	 d
             Zdeeef   fdZdeeef   fdZe G d d             Ze G d d             Ze G d d             Ze G d d             Ze G d d             Ze G d d             Ze G d d             Ze G d d             Ze G d d             Z e G d  d!             Z!e G d" d#             Z"y$)%aG  
Configuration Data Schemas

This module defines all configuration data classes used throughout the application.
It consolidates and replaces duplicate configuration definitions from multiple files.

Key Features:
- Type-safe configuration structures
- Default value support
- Validation methods
- Unified configuration schema
    )	dataclassfield)AnyDictListOptional)urlparse)LoadBalanceStrategyPipelineStage)	ConditionPatternsRateLimitConfigc                   ~    e Zd ZU dZ ee      Zee   e	d<    ee      Z
ee   e	d<   ej                  Zee	d<   d Zy)CredentialsConfigz4GitHub credentials configuration with load balancingdefault_factorysessionstokensstrategyc                    t        | j                  t              st               | _        t        | j                  t              st               | _        t        | j                  t
              rt        | j                        | _        yy)z"Validate credentials configurationN)
isinstancer   listr   r   strr
   selfs    5/root/.openclaw/workspace/harvester/config/schemas.py__post_init__zCredentialsConfig.__post_init__    sW     $--. FDM$++t,&DK dmmS)/>DM *    N)__name__
__module____qualname____doc__r   r   r   r   r   __annotations__r   r
   ROUND_ROBINr   r    r   r   r   r      sA    >5Hd3i5d3FDI3$7$C$CH!C?r   r   c                       e Zd ZU dZdZeed<   dZeed<   dZ	eed<   dZ
ee   ed	<    ee
      Zee   ed<   d Zedee   defd       Zy)GlobalConfigz Global application configurationz./data	workspace   max_retries_requeued proxyNgithub_credentialsr   user_agentsc                     | j                  | j                        | _        | j                  !t        g g t        j
                        | _        | j                  s
g d| _        yy)z#Set default values if none providedN)r   r   r   )zoMozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36zuMozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36zeMozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36)_normalize_proxyr,   r-   r   r
   r$   r.   r   s    r   r   zGlobalConfig.__post_init__9   s\    **4::6
 ""*&7,88'D#  D  r   returnc                 |   | yt        | t              st        d      | j                         } | syt	        |       }|j
                  j                         }|dvrt        d      |j                  st        d      	 |j                  }|dk(  r|t        d	      | S # t        $ r}t        d|       |d}~ww xY w)
z1Validate and normalize the global HTTP proxy URL.Nr+   zproxy must be a string>   httphttpssocks5z0proxy scheme must be one of: http, https, socks5zproxy must include a hostzinvalid proxy port: r5   z socks5 proxy must include a port)	r   r   
ValueErrorstripr	   schemelowerhostnameport)r,   parsedr8   r;   es        r   r0   zGlobalConfig._normalize_proxyM   s     =%%566%$$&44OPP899	@;;D X$,?@@  	@3A378a?	@s   >B 	B;'B66B;)r   r    r!   r"   r(   r   r#   r*   intr,   r-   r   r   r   r   r.   r   r   staticmethodr0   r%   r   r   r'   r'   /   sr    *Is !#!E3O6:!23:"48Kc8(  #  r   r'   r1   c                      t         j                  j                  dt         j                  j                  dt         j                  j                  dt         j
                  j                  diS )zAGet default thread configuration using StandardPipelineStage enum            r   SEARCHvalueGATHERCHECKINSPECTr%   r   r   _get_default_threadsrK   n   sR     	""A""A!!1##Q	 r   c                      t         j                  j                  dt         j                  j                  dt         j                  j                  dt         j
                  j                  diS )z8Get default queue sizes using StandardPipelineStage enumi i@ i  i@B rE   r%   r   r   _get_default_queue_sizesrM   x   sR     	""F""F!!6##W	 r   c                   d    e Zd ZU dZ ee      Zeee	f   e
d<    ee      Zeee	f   e
d<   d Zy)PipelineConfigzPipeline stage configurationr   threadsqueue_sizesc                 r    | j                   st               | _         | j                  st               | _        y y )N)rP   rK   rQ   rM   r   s    r   r   zPipelineConfig.__post_init__   s,    ||/1DL79D  r   N)r   r    r!   r"   r   rK   rP   r   r   r>   r#   rM   rQ   r   r%   r   r   rO   rO      s:    &#4HIGT#s(^I"'8P"QKc3hQ:r   rO   c                       e Zd ZU dZdZeed<   dZeed<   dZe	ed<   dZ
e	ed	<   d
Zeed<   d ZdedefdZde	defdZde	defdZdedefdZy)MonitoringConfigz,System monitoring and alerting configurationg       @update_intervalg?error_thresholdi  queue_thresholdi   @memory_threshold      @response_thresholdc                 ,   | j                   dk  rt        d      d| j                  cxk  rdk  st        d       t        d      | j                  dk  rt        d      | j                  dk  rt        d      | j
                  dk  rt        d      y)	z!Validate monitoring configurationr   z update_interval must be positiverA   z'error_threshold must be between 0 and 1z$queue_threshold must be non-negativez!memory_threshold must be positivez#response_threshold must be positiveN)rU   r6   rV   rW   rX   rZ   r   s    r   r   zMonitoringConfig.__post_init__   s    1$?@@T)).Q.FGG /FGG!#CDD  A%@AA""a'BCC (r   
error_rater1   c                      || j                   kD  S )z%Check if error rate exceeds threshold)rV   )r   r\   s     r   is_error_criticalz"MonitoringConfig.is_error_critical       D0000r   
queue_sizec                      || j                   kD  S )z%Check if queue size exceeds threshold)rW   )r   r`   s     r   is_queue_criticalz"MonitoringConfig.is_queue_critical   r_   r   memory_usage_mbc                      || j                   kD  S )z'Check if memory usage exceeds threshold)rX   )r   rc   s     r   is_memory_criticalz#MonitoringConfig.is_memory_critical   s    !6!666r   response_timec                      || j                   kD  S )z(Check if response time exceeds threshold)rZ   )r   rf   s     r   is_response_criticalz%MonitoringConfig.is_response_critical   s    t6666r   N)r   r    r!   r"   rU   floatr#   rV   rW   r>   rX   rZ   r   boolr^   rb   re   rh   r%   r   r   rT   rT      s    6 OU  OU OS&c& ##D1E 1d 11C 1D 17# 7$ 77% 7D 7r   rT   c                   t    e Zd ZU dZdZeed<   dZeed<   dZ	eed<   dZ
eed<   dZeed	<   d
Zeed<   dZeed<   y)DisplayContextConfigz,Display configuration for a specific contextr+   titleTshow_workersshow_alertsFshow_performanceshow_newline_prefixP   widthr)   max_alerts_per_levelN)r   r    r!   r"   rm   r   r#   rn   rj   ro   rp   rq   rs   r>   rt   r%   r   r   rl   rl      sN    6E3OL$K"d" %% E3O !#!r   rl   c                   N    e Zd ZU dZ ee      Zeeeee	f   f   e
d<   d Zd Zy)DisplayConfigz&Display configuration for all contextsr   contextsc                 >    | j                   s| j                          yy)z3Set default display configurations if none providedN)rw   _set_default_contextsr   s    r   r   zDisplayConfig.__post_init__   s    }}&&( r   c           	         t        dddd      t        dddd      t        ddddd      d| j                  d<   t        d	ddd      t        d
dddd      d| j                  d<   t        dddd      t        dddd      d| j                  d<   t        dddd      t        dddd      d| j                  d<   dt        dddd      i| j                  d<   y)z*Set default display context configurationszSystem StatusTF)rm   rn   ro   rp   zDetailed System Status)rm   rn   ro   rp   rq   )standardcompactdetailedsystemzPipeline MonitoringzDetailed Pipeline Monitoring)r{   r}   
monitoringzTask Manager Status)r{   r|   taskzApplication StatuszDetailed Application Statusapplicationr{   zPipeline StatusmainN)rl   rw   r   s    r   ry   z#DisplayConfig._set_default_contexts   s(    -%Dd]b ,%Eu_d -.! !%$(#
h$ -+$Dcg -4! !%$(	'
l# -+$Edi ,+%Uej	!
f -*Dch -3$TXko	(
m$ ,'d`e!
fr   N)r   r    r!   r"   r   dictrw   r   r   rl   r#   r   ry   r%   r   r   rv   rv      s4    0;@QU;VHd3S"66778V)
:
r   rv   c                   t    e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	eed	<   d
Z
eed<   dZeed<   dZeed<   y)PersistenceConfigz&Persistence and recovery configuration2   
batch_size   save_interval<   queue_intervali,  snapshot_intervalTauto_restoreshutdown_timeoutFsimpleN)r   r    r!   r"   r   r>   r#   r   r   r   r   rj   r   r   r%   r   r   r   r     sM    0JM3NC s L$cFDr   r   c                       e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	eed<   dZ
eed<    ee	      Zeeef   ed
<   dZeed<   dZeed<   dZeed<   y)	ApiConfigz API configuration for a providerr+   base_urlcompletion_path
model_pathdefault_modelAuthorizationauth_keyr   extra_headersapi_versionr   timeoutr)   retriesN)r   r    r!   r"   r   r   r#   r   r   r   r   r   r   r   r   r   r   r>   r   r%   r   r   r   r     sl    *HcOSJM3#Hc#$)$$?M4S>?KGSGSr   r   c                   R    e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	eed<   d	dZ
y)
StageConfigz1Pipeline stage configuration for individual tasksTsearchgathercheckinspectNc                 L    | j                   s| j                  rt        d      yy)zValidate stage dependenciesz0inspect stage requires check stage to be enabledN)r   r   r6   r   s    r   validatezStageConfig.validate4  s"    zzdllOPP +zr   )r1   N)r   r    r!   r"   r   rj   r#   r   r   r   r   r%   r   r   r   r   +  s3    ;FDFDE4GTQr   r   c                   .    e Zd ZU dZdZeed<   dZeed<   y)StorageConfigz+Result storage grouping for a provider taskr+   	directoryplanN)r   r    r!   r"   r   r   r#   r   r%   r   r   r   r   :  s    5IsD#Nr   r   c                      e Zd ZU dZdZeed<   dZeed<   dZ	eed<   dZ
eed<    ee	      Zeed
<    ee	      Zeed<    ee	      Zeeef   ed<    ee	      Zeed<    ee	      Zeed<    ee	      Zee   ed<    ee	      Zeed<   y)
TaskConfigz(Configuration for a single provider taskr+   nameTenabledprovider_typeFuse_apir   stagesstorageextrasapipatterns
conditions
rate_limitN)r   r    r!   r"   r   r   r#   r   rj   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r%   r   r   r   r   B  s    2D#NGTM3GT<FK<"=AG]A"48FDcN895C5x8Hh8"'"=JY="'"HJHr   r   c                       e Zd ZU dZdZeed<   dZeed<   dZ	eed<   dZ
eed	<   d
Zeed<   dZeed<   dZeed<   dZeed<   d ZdedefdZdedefdZdededefdZy)WorkerManagerConfigz:Worker manager configuration for dynamic thread managementFr   rA   min_workers
   max_workersd   target_queue_sizerY   adjustment_intervalg?scale_up_thresholdg?scale_down_thresholdTlog_recommendationsc                    | j                   dk  rt        d      | j                  | j                   k  rt        d      | j                  dk  rt        d      | j                  dk  rt        d      d| j
                  cxk  rdk  st        d       t        d      d| j                  cxk  rdk  st        d       t        d      | j                  | j
                  k\  rt        d	      y
)z%Validate worker manager configurationrA   zmin_workers must be at least 1z"max_workers must be >= min_workersr   z&target_queue_size must be non-negativez$adjustment_interval must be positivez*scale_up_threshold must be between 0 and 1z,scale_down_threshold must be between 0 and 1z1scale_down_threshold must be < scale_up_thresholdN)r   r6   r   r   r   r   r   r   s    r   r   z!WorkerManagerConfig.__post_init__c  s    a=>>d...ABB!!A%EFF##q(CDDD++/a/IJJ 0IJJD--11KLL 2KLL$$(?(??PQQ @r   queue_ratior1   c                      || j                   kD  S )z0Check if scale up is needed based on queue ratio)r   r   r   s     r   is_scale_up_neededz&WorkerManagerConfig.is_scale_up_neededt  s    T4444r   c                      || j                   k  S )z2Check if scale down is needed based on queue ratio)r   r   s     r   is_scale_down_neededz(WorkerManagerConfig.is_scale_down_neededx  s    T6666r   current_queue_sizecurrent_workersc                     |dk(  rt        | j                  |dz
        S |t        | j                  d      z  }|| j                  kD  rt	        | j
                  |dz         }|S || j                  k  rt        | j                  |dz
        }|S |}|S )z;Calculate target number of workers based on current metricsr   rA   )maxr   r   r   minr   r   )r   r   r   r   targets        r   calculate_target_workersz,WorkerManagerConfig.calculate_target_workers|  s    "t''1)<==(3t/E/Eq+II000))?Q+>?F  4444))?Q+>?F  %Fr   N)r   r    r!   r"   r   rj   r#   r   r>   r   r   r   ri   r   r   r   r   r   r   r   r%   r   r   r   r   S  s    D GTKK s !$$ ##"%%% !%$R"5e 5 57 7$ 73 QT Y\ r   r   c                   .   e Zd ZU dZ ee      Zeed<    ee      Z	eed<    ee
      Ze
ed<    ee      Zeed<    ee      Zeed<    ee      Zeed<    ee      Zeeef   ed	<    ee      Zee   ed
<   d Zdeeef   fdZdedefdZy)ConfigzMain configuration containerr   global_configpipeliner   displaypersistenceworker
ratelimitstasksc                 b    | j                   s#t        ddd      t        ddd      d| _         yy)	z(Set default rate limits if none providedg333333?r)   T)	base_rateburst_limitadaptiveg      ?rD   )
github_api
github_webN)r   r   r   s    r   r   zConfig.__post_init__  s1    -!VZ[-UYZDO r   r1   c                 R   | j                  | j                        | j                  | j                        | j                  | j                        | j                  | j                        | j                  | j
                        | j                  | j                        | j                  | j                        | j                  j                         D ci c]  \  }}|| j                  |       c}}| j                  D cg c]  }| j                  |       c}d	S c c}}w c c}w )zConvert Config object to dictionary

        Returns:
            Dict[str, Any]: Configuration as dictionary with proper structure
        )	globalr   statsr   r   r   r   r   r   )_dataclass_to_dictr   r   r   r   r   r   r   r   itemsr   )r   kvr   s       r   to_dictzConfig.to_dict  s     --d.@.@A//>,,TZZ811$//B..t||<2243C3CD--dkk:EI__EZEZE\]TQ1d55a88]@D

Kd--d3K

 
	
 ^Ks   DD$objc                    t        |d      rCi }|j                  j                         D ]"  }t        ||      }| j	                  |      ||<   $ |S t        |t              r3|j                         D ci c]  \  }}|| j	                  |       c}}S t        |t        t        f      r|D cg c]  }| j	                  |       c}S t        |d      r|j                  S |S c c}}w c c}w )zConvert dataclass object to dictionary recursively

        Args:
            obj: Object to convert (dataclass, dict, list, or primitive)

        Returns:
            Any: Converted object
        __dataclass_fields__rG   )hasattrr   keysgetattrr   r   r   r   r   tuplerG   )r   r   result
field_namerG   r   r   items           r   r   zConfig._dataclass_to_dict  s     3./F!66;;= D
Z0%)%<%<U%Cz"D MT">AiikJdaAt..q11JJdE]+>ABdD++D1BBS'"99 J K Cs   3C"-C(N)r   r    r!   r"   r   r'   r   r#   rO   r   rT   r   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r%   r   r   r   r     s    &"'"EM<E$^DHnD#(9I#JJ J"=AG]A%*;L%MK"M"'8K"LFL-24-HJS/)*H#D9E4
9
c3h 
$c c r   r   N)#r"   dataclassesr   r   typingr   r   r   r   urllib.parser	   
core.enumsr
   r   core.modelsr   r   r   r   r'   r   r>   rK   rM   rO   rT   rl   rv   r   r   r   r   r   r   r   r%   r   r   <module>r      s   ) , , ! 9 < < ? ? ?, ; ; ;|d38n $sCx.  
: 
: 
: $7 $7 $7N " " " D
 D
 D
N 	 	 	    Q Q Q    I I I  6 6 6r A A Ar   