85 lines
2.2 KiB
Python
85 lines
2.2 KiB
Python
"""
|
|
模板渲染节点
|
|
"""
|
|
import logging
|
|
from typing import Any, Dict
|
|
|
|
from ..base import BaseNode, NodeContext, NodeResult
|
|
from ..registry import NodeRegistry
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@NodeRegistry.register
|
|
class TemplateNode(BaseNode):
|
|
"""
|
|
模板渲染节点
|
|
|
|
使用变量渲染模板字符串
|
|
"""
|
|
|
|
node_type = 'template'
|
|
node_name = '模板'
|
|
node_category = 'data'
|
|
node_icon = 'file-text'
|
|
node_description = '使用变量渲染模板字符串'
|
|
|
|
inputs = [
|
|
{
|
|
'name': 'template',
|
|
'type': 'string',
|
|
'description': '模板字符串',
|
|
},
|
|
]
|
|
|
|
outputs = [
|
|
{
|
|
'name': 'result',
|
|
'type': 'string',
|
|
'description': '渲染结果',
|
|
},
|
|
]
|
|
|
|
def execute(self, context: NodeContext) -> NodeResult:
|
|
"""执行模板渲染"""
|
|
try:
|
|
template = self.config.get('template', '')
|
|
output_variable = self.config.get('output_variable', 'template_result')
|
|
|
|
# 渲染模板
|
|
result = context.resolve_template(template)
|
|
|
|
return NodeResult(
|
|
success=True,
|
|
output=result,
|
|
output_variables={output_variable: result},
|
|
)
|
|
|
|
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': {
|
|
'template': {
|
|
'type': 'string',
|
|
'title': '模板',
|
|
'description': '支持变量引用,如 {{variable_name}}',
|
|
'format': 'textarea',
|
|
},
|
|
'output_variable': {
|
|
'type': 'string',
|
|
'title': '输出变量名',
|
|
'default': 'template_result',
|
|
},
|
|
},
|
|
'required': ['template'],
|
|
}
|