
    iV                         d 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  G d
 d      Z G d d      Z G d d      Zy)z4Execution strategies for different graph topologies.    )Counter)CallableDictListSequence)Node)Message)
LogManager)DAGExecutor)CycleExecutor)ParallelExecutorc            
       V    e Zd ZdZdedeeef   deee      de	egdf   ddf
dZ
d
d	Zy)DagExecutionStrategyz.Executes acyclic graphs using the DAGExecutor.log_managernodeslayersexecute_node_funcNreturnc                 <    || _         || _        || _        || _        y Nr   r   r   r   )selfr   r   r   r   s        P/Users/bowang/.openclaw/workspace/ChatDev/workflow/runtime/execution_strategy.py__init__zDagExecutionStrategy.__init__   s"     '
!2    c                     t        | j                  | j                  | j                  | j                        }|j                          y )Nr   )r   r   r   r   r   execute)r   dag_executors     r   runzDagExecutionStrategy.run   s:    "((**;;"44	
 	r   r   N__name__
__module____qualname____doc__r
   r   strr   r   r   r   r    r   r   r   r      sZ    8
3
3 CI
3 T#Y	
3
 $TFDL1
3 

3r   r   c            
       Z    e Zd ZdZdedeeef   deeeef      de	egdf   ddf
dZ
d
d	Zy)CycleExecutionStrategyz4Executes graphs containing cycles via CycleExecutor.r   r   cycle_execution_orderr   Nr   c                 J    || _         || _        || _        || _        || _        y r   r   r   r*   cycle_managerr   )r   r   r   r*   r-   r   s         r   r   zCycleExecutionStrategy.__init__*   s+     '
%:"*!2r   c                     t        | j                  | j                  | j                  | j                  | j
                        }|j                          y )Nr,   )r   r   r   r*   r-   r   r   )r   cycle_executors     r   r   zCycleExecutionStrategy.run8   sE    &((**"&"<"<,,"44
 	 r   r    r!   r'   r   r   r)   r)   '   s_    >33 CI3  $DcN3	3 $TFDL13 
3!r   r)   c                   p    e Zd ZdZdedeeef   dee	   de
egdf   de
egef   ddfd	Zdefd
ZdefdZy)MajorityVoteStrategyz:Executes graphs configured for majority voting (no edges).r   r   initial_messagesr   Npayload_to_text_funcr   c                 J    || _         || _        || _        || _        || _        y r   )r   r   r2   r   payload_to_text)r   r   r   r2   r   r3   s         r   r   zMajorityVoteStrategy.__init__F   s+     '
 0!23r   c                 &     j                   j                  d       t         j                  j	                               }|s j                   j                  d       y|D ]B  }|j                           j                  D ]!  }|j                  |j                                # D |D cg c]  }|j                   }}dt        dd f fd}t         j                    j                        }|j                  ||        j                         S c c}w )Nz-Executing graph with majority voting approachz+No nodes to execute in majority voting mode node_idr   c                 B    j                  j                  |           y r   )r   r   )r8   r   s    r   _executez*MajorityVoteStrategy.run.<locals>._executeb   s    ""4::g#67r   )r   infolistr   valueserrorclear_inputr2   append_inputcloneidr&   r   execute_nodes_parallel_collect_majority_result)r   	all_nodesnodemessagenode_idsr:   parallel_executors   `      r   r   zMajorityVoteStrategy.runT   s    MN**,-	""#PQD00!!'--/2 1 
 )22	DGG	2	8c 	8d 	8 -T-=-=tzzJ008D,,.. 3s   )Dc                    g }| j                   j                         D ]Q  \  }}|j                  r| j                  |j                  d         }nd}|j	                  ||j
                  |d       S |D cg c]  }|d   	 }}t        |      }|D cg c]  }|j                         s| }	}|	rt        |	      }|s| j                  j                  d       y|j                  d      d   \  }
}| j                  j                  d|
|d	
       | j                  j                  dd|D cg c](  }|d   t        |d         dkD  r|d   d d dz   n|d   f* c}i
       |
S c c}w c c}w c c}w )Nr7   )r8   	node_typeoutputrM   z(No outputs available for majority voting   r   zMajority output determined)resultvotes)detailszAll node outputsoutputsr8   2   z...)r   itemsrM   r5   appendrL   r   stripr   warningmost_commonr;   len)r   node_outputsr8   rF   output_textitemoutput_valuesoutput_countsvaluenon_empty_outputsmajority_outputcounts               r   rD   z-MajorityVoteStrategy._collect_majority_resultj   s   -/!ZZ--/MGT{{"224;;r?C &!%) 0 5AALDhLA.0=OuUO#$56M$$%OP!.!:!:1!=a!@(.? 	 	
 	
 !-
 !- Y7:4>7JR7OXs+e3UYZbUc !- 	 	
 9 B P s   5E#E((E(+-E-)r"   r#   r$   r%   r
   r   r&   r   r   r	   r   objectr   r   rD   r'   r   r   r1   r1   C   s    D44 CI4 #7+	4
 $TFDL14 'x}54 
4/S /,+# +r   r1   N)r%   collectionsr   typingr   r   r   r   entity.configsr   entity.messagesr	   utils.log_managerr
   workflow.executor.dag_executorr    workflow.executor.cycle_executorr   #workflow.executor.parallel_executorr   r   r)   r1   r'   r   r   <module>rl      sC    :  1 1  # ( 6 : @ 2! !8R Rr   