|
| 1 | +# coding=utf-8 |
| 2 | +""" |
| 3 | +@project: MaxKB |
| 4 | +@Author: 虎虎虎 |
| 5 | +@file: tool_start_node.py |
| 6 | +@date: 2026/9/3 17:20 |
| 7 | +@desc: 工具工作流的起始节点,负责把工具入参写入全局变量、初始化输出字段 |
| 8 | +""" |
| 9 | + |
| 10 | +from application.workflow.common import WorkflowType |
| 11 | +from application.workflow.i_node import INode |
| 12 | + |
| 13 | + |
| 14 | +class ToolStartNode(INode): |
| 15 | + supported_workflow_type_list = [WorkflowType.TOOL] |
| 16 | + type = "tool-start-node" |
| 17 | + |
| 18 | + def execute(self): |
| 19 | + workflow_params = self.get_workflow_parameters() |
| 20 | + base_node = self.workflow_manage.workflow.get_node("tool-base-node") |
| 21 | + user_input_field_list = base_node.properties.get("user_input_field_list", []) if base_node else [] |
| 22 | + user_output_field_list = base_node.properties.get("user_output_field_list", []) if base_node else [] |
| 23 | + |
| 24 | + # 入参 -> 全局变量(引用约定 global.<field>) |
| 25 | + for item in user_input_field_list: |
| 26 | + field = item.get("field") |
| 27 | + self.workflow_manage.write_context("global", field, workflow_params.get(field)) |
| 28 | + |
| 29 | + # 初始化输出字段默认值 -> output(工作流内由变量赋值节点覆写) |
| 30 | + for item in user_output_field_list: |
| 31 | + if item.get("default_value", None) is not None: |
| 32 | + self.workflow_manage.write_context("output", item.get("field"), item.get("default_value")) |
| 33 | + |
| 34 | + self.write_context("question", workflow_params.get("question", "")) |
| 35 | + |
| 36 | + def get_details(self, index: int = 0, position: dict = None, old_details: dict = None, **kwargs): |
| 37 | + details = super().get_details(index, position, old_details, **kwargs) |
| 38 | + global_fields = [] |
| 39 | + for field in (self.node.properties.get("config") or {}).get("globalFields", []) or []: |
| 40 | + key = field.get("value") |
| 41 | + global_fields.append( |
| 42 | + { |
| 43 | + "label": field.get("label"), |
| 44 | + "key": key, |
| 45 | + "value": self.workflow_manage.get_context("global", key) or "", |
| 46 | + } |
| 47 | + ) |
| 48 | + details.update( |
| 49 | + { |
| 50 | + "question": self.get_context("question"), |
| 51 | + "global_fields": global_fields, |
| 52 | + } |
| 53 | + ) |
| 54 | + return details |
0 commit comments