
    i                      f    d Z ddlmZmZmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZ  G d de      Zy	)
z!Loop counter guard node executor.    )ListDictAny)Node)LoopCounterConfig)MessageMessageRole)NodeExecutorc                   T    e Zd ZdZdZdedee   dee   fdZde	e
e	e
ef   f   fdZy)	LoopCounterNodeExecutorzCTrack loop iterations and emit output only after hitting the limit.loop_counternodeinputsreturnc           	         |j                  t              }|t        d|j                   d      | j	                         }|j                  |j                  ddi      }|dxx   dz  cc<   |d   }||j                  k  r;| j                  j                  d|j                   d| d|j                   d	       g S |j                  rd|d<   |j                  xs d
|j                   d}d||j                  |j                  di}| j                  j                  d|j                   d| d|j                   d       t        t        j                  ||      gS )NzNode z# missing loop_counter configurationcountr      zLoopCounter z: iteration /z (suppress downstream)zLoop limit reached ()r   )r   maxreset_on_emitz  reached limit, releasing output)rolecontentmetadata)	as_configr   
ValueErrorid
_get_state
setdefaultmax_iterationslog_managerdebugr   messager   r	   	ASSISTANT)	selfr   r   configstatecounterr   r   r   s	            X/Users/bowang/.openclaw/workspace/ChatDev/runtime/node/executor/loop_counter_executor.pyexecutezLoopCounterNodeExecutor.execute   sp    12>uTWWI-PQRR!""477WaL9A 6(((""twwi|E7!F<Q<Q;RRhi I GG..S&:6;P;P:QQR$S,,!'!5!5
 	477)<wa8M8M7NNno	
 &&
  	    c                 b    | j                   j                  j                  | j                  i       S )N)contextglobal_stater   	STATE_KEY)r%   s    r)   r   z"LoopCounterNodeExecutor._get_state6   s#    ||((33DNNBGGr+   N)__name__
__module____qualname____doc__r/   r   r   r   r*   r   strr   r    r+   r)   r   r      sM    MI$D $$w- $DM $LHDd38n!45 Hr+   r   N)r3   typingr   r   r   entity.configsr    entity.configs.node.loop_counterr   entity.messagesr   r	   runtime.node.executor.baser
   r   r5   r+   r)   <module>r;      s(    ' " "  > 0 3,Hl ,Hr+   