@@ -74,6 +74,15 @@ class ToolWorkflowLibNode(INode):
7474 supported_workflow_type_list = [WorkflowType .APPLICATION , WorkflowType .KNOWLEDGE , WorkflowType .TOOL ]
7575 type = "tool-workflow-lib-node"
7676
77+ def __init__ (self , node , workflow_manage , get_node_parameters ):
78+ super ().__init__ (node , workflow_manage , get_node_parameters )
79+ self ._current_sub_manage = None
80+
81+ def cancel (self ):
82+ super ().cancel ()
83+ if self ._current_sub_manage :
84+ self ._current_sub_manage .cancel ()
85+
7786 def _run (self ):
7887 # 完成时机由子工作流的 on_complete 回调驱动,这里不自动 complete
7988 self .execute ()
@@ -120,6 +129,7 @@ def on_next(wf_manage, content):
120129 self .write (content )
121130
122131 def on_complete (wf_manage , error ):
132+ self ._current_sub_manage = None
123133 # 收集工具工作流输出(tool-start-node 初始化、变量赋值节点覆写)
124134 output = dict (wf_manage .context .get ("output" , {}) or {})
125135 # 只有需要给下游引用的数据才写 context:各输出字段
@@ -131,6 +141,10 @@ def on_complete(wf_manage, error):
131141 self .data ["message_tokens" ] = _sum_tokens (wf_manage .context , "message_tokens" )
132142 self .data ["answer_tokens" ] = _sum_tokens (wf_manage .context , "answer_tokens" )
133143
144+ # 子工作流被取消:如实以 CANCELLED 结束本节点
145+ if wf_manage .signal == Signal .CANCELLED or self .workflow_manage .signal == Signal .CANCELLED :
146+ self .complete (Status .CANCELLED )
147+ return
134148 if error :
135149 self .complete (Status .FAIL , error = error )
136150 return
@@ -167,6 +181,11 @@ def get_start_node_fn(wf, wm):
167181 get_start_node = get_start_node_fn ,
168182 )
169183 sub_manage .start_node .workflow_manage = sub_manage
184+ self ._current_sub_manage = sub_manage
185+ # 取消发生在构造之后、启动之前:不再启动子工作流
186+ if self .status == Status .CANCELLED or self .workflow_manage .signal == Signal .CANCELLED :
187+ self .complete (Status .CANCELLED )
188+ return
170189 # 子工作流的输出已在 on_next 中逐块转发给父工作流,无需按 is_result 重复输出
171190 sub_manage .run ()
172191
0 commit comments