99 lines
3.2 KiB
Python
99 lines
3.2 KiB
Python
"""
|
||
结束节点
|
||
"""
|
||
from typing import Any, Dict
|
||
|
||
from ..base import BaseNode, NodeContext, NodeResult
|
||
from ..registry import NodeRegistry
|
||
|
||
|
||
@NodeRegistry.register
|
||
class EndNode(BaseNode):
|
||
"""
|
||
结束节点
|
||
|
||
工作流的出口节点,输出最终结果
|
||
"""
|
||
|
||
node_type = 'end'
|
||
node_name = '结束'
|
||
node_category = 'basic'
|
||
node_icon = 'stop-circle'
|
||
node_description = '工作流的结束节点,输出最终结果'
|
||
|
||
inputs = [
|
||
{
|
||
'name': 'output',
|
||
'type': 'any',
|
||
'description': '输出内容',
|
||
},
|
||
]
|
||
|
||
outputs = []
|
||
|
||
def execute(self, context: NodeContext) -> NodeResult:
|
||
"""执行结束节点"""
|
||
# 获取输出内容配置(前端保存的是字符串模板,如 "{{llm_response}}")
|
||
output_template = self.config.get('output', '')
|
||
|
||
if output_template and isinstance(output_template, str) and output_template.strip():
|
||
# 如果配置了输出模板,解析模板中的变量
|
||
output = context.resolve_template(output_template)
|
||
else:
|
||
# 没有配置输出模板,不输出任何内容
|
||
output = None
|
||
|
||
# 处理输出变量(用于结构化输出)
|
||
# 只有当 output 不为 None 时才处理结构化输出
|
||
outputs_config = self.config.get('outputs', [])
|
||
if output is not None and outputs_config and isinstance(outputs_config, list):
|
||
# 如果定义了输出变量,构建结构化输出
|
||
structured_output = {}
|
||
for out_var in outputs_config:
|
||
if isinstance(out_var, dict):
|
||
var_name = out_var.get('variable', '')
|
||
if var_name:
|
||
# 从上下文获取变量值
|
||
structured_output[var_name] = context.get_variable(var_name, None)
|
||
|
||
# 如果有结构化输出,合并到结果中
|
||
if structured_output:
|
||
if isinstance(output, dict):
|
||
output = {**output, **structured_output}
|
||
else:
|
||
output = {
|
||
'result': output,
|
||
**structured_output
|
||
}
|
||
|
||
return NodeResult(
|
||
success=True,
|
||
output=output,
|
||
)
|
||
|
||
@classmethod
|
||
def get_config_schema(cls) -> Dict[str, Any]:
|
||
"""获取配置 Schema"""
|
||
return {
|
||
'type': 'object',
|
||
'properties': {
|
||
'output': {
|
||
'type': 'object',
|
||
'title': '输出配置',
|
||
'properties': {
|
||
'type': {
|
||
'type': 'string',
|
||
'title': '输出类型',
|
||
'enum': ['variable', 'template', 'previous'],
|
||
'default': 'previous',
|
||
},
|
||
'value': {
|
||
'type': 'string',
|
||
'title': '输出值',
|
||
'description': '变量名或模板',
|
||
},
|
||
},
|
||
},
|
||
},
|
||
}
|