From 0e5397bcf4b1fd1715e4ac3f0eba05b101dafae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=9F=E6=B0=B8=E8=B1=AA?= <1533512157@qq.com> Date: Tue, 30 Dec 2025 07:53:49 +0000 Subject: [PATCH] Merge #86 into develop from feature/20251219_myh fix(workflow): prevent duplicate conditional branches in if-else nodes * feature/20251219_myh: (3 commits squashed) - perf(workflow): adjust default template to be compatible with frontend format - fix: correct function naming for memory retrieval - fix(workflow): prevent duplicate conditional branches in if-else nodes Signed-off-by: Eternity <1533512157@qq.com> Reviewed-by: aliyun6762716068 Merged-by: aliyun6762716068 CR-link: https://codeup.aliyun.com/redbearai/python/redbear-mem-open/change/86 --- api/app/core/workflow/executor.py | 38 +++++++++++++++++++------------ 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/api/app/core/workflow/executor.py b/api/app/core/workflow/executor.py index 1cbfb66b..0d0879d7 100644 --- a/api/app/core/workflow/executor.py +++ b/api/app/core/workflow/executor.py @@ -307,22 +307,30 @@ class WorkflowExecutor: if condition: # 条件边 - def router(state: WorkflowState, cond=condition, tgt=target): - """条件路由函数""" - if evaluate_condition( - cond, - state.get("variables", {}), - state.get("node_outputs", {}), - { - "execution_id": state.get("execution_id"), - "workspace_id": state.get("workspace_id"), - "user_id": state.get("user_id") - } - ): - return tgt - return END # 条件不满足,结束 + def make_router(cond, tgt): + """Dynamically generate a conditional router function to ensure each branch has a unique name.""" - workflow.add_conditional_edges(source, router) + + def router_fn(state: WorkflowState): + if evaluate_condition( + cond, + state.get("variables", {}), + state.get("node_outputs", {}), + { + "execution_id": state.get("execution_id"), + "workspace_id": state.get("workspace_id"), + "user_id": state.get("user_id") + } + ): + return tgt + return END + + # 动态修改函数名,避免重复 + router_fn.__name__ = f"router_{tgt}" + return router_fn + + router_fn = make_router(condition, target) + workflow.add_conditional_edges(source, router_fn) logger.debug(f"添加条件边: {source} -> {target} (condition={condition})") else: # 普通边