feat: restore source parity and harden agent runtime
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
条件分支节点处理器
|
||||
处理条件判断和分支选择
|
||||
"""
|
||||
import logging
|
||||
from typing import Dict, TYPE_CHECKING
|
||||
|
||||
from online_dev.workflow.engine.handlers.base_handler import BaseNodeHandler
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from online_dev.workflow.engine.base import ExecutionContext
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ConditionHandler(BaseNodeHandler):
|
||||
"""
|
||||
条件分支节点处理器
|
||||
|
||||
根据条件表达式选择执行的分支
|
||||
- 条件组之间是 OR 关系
|
||||
- 组内条件是 AND 关系
|
||||
- 支持默认分支
|
||||
"""
|
||||
|
||||
async def execute(self, context: 'ExecutionContext', node: Dict) -> None:
|
||||
"""
|
||||
执行条件节点:评估条件并选择分支
|
||||
"""
|
||||
from online_dev.workflow.engine.condition_evaluator import condition_evaluator
|
||||
|
||||
branches = node.get('branches', [])
|
||||
node_id = self.get_node_id(node)
|
||||
|
||||
logger.info(f"处理条件分支 - 节点: {node_id}, 分支数: {len(branches)}")
|
||||
|
||||
# 按优先级评估每个分支
|
||||
for branch in branches:
|
||||
config = branch.get('config', {})
|
||||
|
||||
# 默认分支
|
||||
if config.get('isDefault'):
|
||||
logger.info(f"进入默认分支: {branch.get('name')}")
|
||||
await self._enter_branch(context, node, branch)
|
||||
return
|
||||
|
||||
# 评估条件
|
||||
groups = config.get('groups', [])
|
||||
if condition_evaluator.evaluate_groups(groups, context.form_data):
|
||||
logger.info(f"条件满足,进入分支: {branch.get('name')}")
|
||||
await self._enter_branch(context, node, branch)
|
||||
return
|
||||
|
||||
# 没有分支满足条件
|
||||
logger.warning(f"条件节点 {node_id} 没有满足的分支,结束流程")
|
||||
await self.engine._end_instance(context, 'rejected')
|
||||
|
||||
async def _enter_branch(
|
||||
self,
|
||||
context: 'ExecutionContext',
|
||||
condition_node: Dict,
|
||||
branch: Dict,
|
||||
) -> None:
|
||||
"""
|
||||
进入分支
|
||||
|
||||
Args:
|
||||
context: 执行上下文
|
||||
condition_node: 条件节点
|
||||
branch: 选中的分支
|
||||
"""
|
||||
# 记录条件分支选择日志(系统自动执行,不关联用户)
|
||||
await self.create_log(
|
||||
context, condition_node, 'condition',
|
||||
comment=f'进入分支: {branch.get("name", "")}',
|
||||
extra_data={
|
||||
'branch_name': branch.get('name', ''),
|
||||
'branch_id': branch.get('id', ''),
|
||||
},
|
||||
operator_id='',
|
||||
)
|
||||
|
||||
branch_children = branch.get('children')
|
||||
|
||||
if branch_children:
|
||||
# 分支有子节点,推进到分支内的第一个节点
|
||||
await self.advance_to_next(context, {'children': branch_children})
|
||||
else:
|
||||
# 分支无子节点,继续推进条件节点的下一节点
|
||||
await self.advance_to_next(context, condition_node)
|
||||
Reference in New Issue
Block a user