
    i                         d Z ddlmZm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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 ddlm Z  e G d d             Z! G d de      Z"y)zkAbstract base classes for node executors.

Defines the interfaces that every node executor must implement.
    )ABCabstractmethod)	dataclassfield)AnyDictOptionalList)Node)MessageMessageContentMessageRoleserialize_messages)MemoryManager)ThinkingManagerBase)ToolManager)FunctionManager)HumanPromptService)
LogManager)TokenTracker)WorkflowCancelledErrorc                   B   e Zd ZU dZeed<   eed<   eed<    ee	      Z
eeef   ed<    ee	      Zeeef   ed<   dZee   ed	<    ee	      Zeeef   ed
<   dZee   ed<   dZee   ed<   dZee   ed<   dedee   fdZdedee   fdZdee   fdZdee   fdZy)ExecutionContexta  Node execution context that bundles every service and state the executor needs.
    
    Attributes:
        tool_manager: Tool manager shared by executors
        function_manager: Function manager registry
        log_manager: Structured log manager
        memory_managers: Mapping of node_id to ``MemoryManager`` instances
        thinking_managers: Mapping of node_id to ``ThinkingManagerBase`` instances
        token_tracker: Token tracker used for accounting
        global_state: Shared global state dictionary
    tool_managerfunction_managerlog_manager)default_factorymemory_managersthinking_managersNtoken_trackerglobal_stateworkspace_hookhuman_prompt_servicecancel_eventnode_idreturnc                 8    | j                   j                  |      S )z+Return the memory manager for a given node.)r   getselfr%   s     G/Users/bowang/.openclaw/workspace/ChatDev/runtime/node/executor/base.pyget_memory_managerz#ExecutionContext.get_memory_manager.   s    ##''00    c                 8    | j                   j                  |      S )z-Return the thinking manager for a given node.)r   r(   r)   s     r+   get_thinking_managerz%ExecutionContext.get_thinking_manager2   s    %%))'22r-   c                     | j                   S )z$Return the configured token tracker.)r    r*   s    r+   get_token_trackerz"ExecutionContext.get_token_tracker6   s    !!!r-   c                     | j                   S )z,Return the interactive human prompt service.)r#   r1   s    r+   get_human_prompt_servicez)ExecutionContext.get_human_prompt_service:   s    (((r-   )__name__
__module____qualname____doc__r   __annotations__r   r   r   dictr   r   strr   r   r   r    r	   r   r!   r   r"   r#   r   r$   r,   r/   r2   r4    r-   r+   r   r      s    
 %%05d0KOT#},-K8=d8StC!445S,0M8L)0#(#>L$sCx.>$(NHSM(9=(#56="&L(3-&1# 1(=2I 13C 3H=P4Q 3"8L#9 ")(3E*F )r-   r   c                      e Zd ZdZdefdZededee	   dee	   fd       Z
edefd       Zedefd	       Zedefd
       Zdee	   defdZdee	   dedz  fdZdddddedededz  deeef   dz  dede	fdZdee	   dee	   fdZddZy)NodeExecutorzAbstract base class for node executors.
    
    Every concrete executor must inherit from this class and implement ``execute``.
    contextc                     || _         y)zInitialize the executor with the shared execution context.
        
        Args:
            context: Execution context
        N)r?   )r*   r?   s     r+   __init__zNodeExecutor.__init__E   s     r-   nodeinputsr&   c                      y)a  Execute the node logic.
        
        Args:
            node: Node definition to execute
            inputs: Input queue for the node
        
        Returns:
            List of payload messages produced by the node. Empty list when the
            node intentionally suppresses downstream propagation. Standard nodes
            return a single-element list.
            
        Raises:
            Exception: Raised when execution fails
        Nr<   )r*   rB   rC   s      r+   executezNodeExecutor.executeM   s      	r-   c                 .    | j                   j                  S )zReturn the shared tool manager.)r?   r   r1   s    r+   r   zNodeExecutor.tool_manager_   s     ||(((r-   c                 .    | j                   j                  S )z#Return the shared function manager.)r?   r   r1   s    r+   r   zNodeExecutor.function_managerd   s     ||,,,r-   c                 .    | j                   j                  S )z"Return the structured log manager.)r?   r   r1   s    r+   r   zNodeExecutor.log_manageri   s     ||'''r-   c           
          |syg }|D ]Z  }|j                   j                  dd      }|j                  d| d|j                  j                   d|j                                 \ dj                  |      S )N sourceUNKNOWNz=== INPUT FROM z (z) ===

z

)metadatar(   appendrolevaluetext_contentjoin)r*   rC   partsmessagerK   s        r+   _inputs_to_textzNodeExecutor._inputs_to_textn   sw    G%%))(I>FLL!&GLL,>,>+?yI]I]I_H`a 
 {{5!!r-   Nc                     |sy t        |      S N)r   )r*   rC   s     r+   _inputs_to_message_jsonz$NodeExecutor._inputs_to_message_jsony   s    !&))r-   F)rK   rM   preserve_rolerO   contentrK   rM   rY   c                f    t        |xs i       }|r|j                  d|       t        ||||      S )NrK   )rO   rZ   rM   rY   )r:   
setdefaultr   )r*   rO   rZ   rK   rM   rY   metas          r+   _build_messagezNodeExecutor._build_message~   s5     HN#OOHf-D'DP]^^r-   messagesc                 H    |D cg c]  }|j                          c}S c c}w rW   )clone)r*   r_   rT   s      r+   _clone_messageszNodeExecutor._clone_messages   s     /78xGx888s   c                 n    t        | j                  dd       }||j                         rt        d      y y )Nr$   zWorkflow execution cancelled)getattrr?   is_setr   )r*   events     r+   _ensure_not_cancelledz"NodeExecutor._ensure_not_cancelled   s6    nd;()GHH "0r-   )r&   N)r5   r6   r7   r8   r   rA   r   r   r
   r   rE   propertyr   r   r   r   r   r   r;   rU   rX   r   r   r   r   boolr^   rb   rg   r<   r-   r+   r>   r>   ?   sK   
 0  D $w- DM  " )k ) ) -/ - - (Z ( (	"d7m 	" 	"*d7m *d
 * "*.#__  _
 d
_ sCx.4'_ _ 
_9W 9$w- 9Ir-   r>   N)#r8   abcr   r   dataclassesr   r   typingr   r   r	   r
   entity.configsr   entity.messagesr   r   r   r   runtime.node.agentr   r   r   utils.function_managerr   utils.human_promptr   utils.log_managerr   utils.token_trackerr   utils.exceptionsr   r   r>   r<   r-   r+   <module>ru      sc   
 $ ( , ,  T T , 2 * 2 1 ( , 3 %) %) %)PSI3 SIr-   