""" 并行分支节点 """ import logging from typing import Any, Dict, List from ..base import BaseNode, NodeContext, NodeResult from ..registry import NodeRegistry logger = logging.getLogger(__name__) @NodeRegistry.register class ParallelNode(BaseNode): """ 并行分支节点 将工作流分成多个并行分支同时执行 """ node_type = 'parallel' node_name = '并行分支' node_category = 'logic' node_icon = 'git-fork' node_description = '将工作流分成多个并行分支同时执行' supports_branches = True inputs = [ { 'name': 'input', 'type': 'any', 'description': '输入数据', }, ] outputs = [ { 'name': 'branches', 'type': 'array', 'description': '并行分支 ID 列表', }, ] def execute(self, context: NodeContext) -> NodeResult: """ 执行并行分支节点 返回所有需要并行执行的分支 ID 列表 """ try: # 获取配置的分支 branches = self.config.get('branches', []) if not branches: # 如果没有配置分支,返回默认的两个分支 branches = [ {'id': 'branch_1', 'name': '分支 1'}, {'id': 'branch_2', 'name': '分支 2'}, ] branch_ids = [b.get('id') for b in branches if b.get('id')] return NodeResult( success=True, output=branch_ids, output_variables={ 'parallel_branches': branch_ids, }, metadata={ 'is_parallel': True, 'branch_count': len(branch_ids), 'branches': branches, }, ) except Exception as e: logger.exception(f'并行分支节点执行失败: {e}') return NodeResult( success=False, error=str(e), ) @classmethod def get_config_schema(cls) -> Dict[str, Any]: """获取配置 Schema""" return { 'type': 'object', 'properties': { 'branches': { 'type': 'array', 'title': '并行分支', 'description': '定义并行执行的分支', 'items': { 'type': 'object', 'properties': { 'id': { 'type': 'string', 'title': '分支 ID', }, 'name': { 'type': 'string', 'title': '分支名称', }, }, 'required': ['id'], }, 'default': [ {'id': 'branch_1', 'name': '分支 1'}, {'id': 'branch_2', 'name': '分支 2'}, ], }, }, }