From 343db4ac5200cae69f187ae74a7ee8a911ee5e0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Fri, 24 Apr 2026 18:02:58 +0800 Subject: [PATCH 1/5] perf: Optimize the `convert_value` method --- .../tool_lib_node/impl/base_tool_lib_node.py | 23 ++------------ .../tool_node/impl/base_tool_node.py | 23 ++------------ apps/common/utils/common.py | 29 +++++++++++++++++ apps/tools/serializers/tool.py | 22 ++----------- .../impl/task/tool_task/base_tool_task.py | 29 ++--------------- .../impl/task/tool_task/workflow_tool_task.py | 31 ++----------------- 6 files changed, 41 insertions(+), 116 deletions(-) diff --git a/apps/application/flow/step_node/tool_lib_node/impl/base_tool_lib_node.py b/apps/application/flow/step_node/tool_lib_node/impl/base_tool_lib_node.py index bbb2be7f418..dafbe7edb9f 100644 --- a/apps/application/flow/step_node/tool_lib_node/impl/base_tool_lib_node.py +++ b/apps/application/flow/step_node/tool_lib_node/impl/base_tool_lib_node.py @@ -25,6 +25,7 @@ from application.flow.step_node.tool_lib_node.i_tool_lib_node import IToolLibNode from common.database_model_manage.database_model_manage import DatabaseModelManage from common.exception.app_exception import AppApiException +from common.utils.common import common_convert_value from common.utils.logger import maxkb_logger from common.utils.rsa_util import rsa_long_decrypt from common.utils.tool_code import ToolExecutor @@ -85,7 +86,7 @@ def valid_reference_value(_type, value, name): return value -def convert_value(name: str, value, _type, is_required, source, node): +def convert_value(name: str, value, _type, is_required, source, node: IToolLibNode): if not is_required and (value is None or ((isinstance(value, str) or isinstance(value, list)) and len(value) == 0)): return None if source == 'reference': @@ -107,25 +108,7 @@ def convert_value(name: str, value, _type, is_required, source, node): return value try: value = node.workflow_manage.generate_prompt(value) - if _type == 'int': - return int(value) - if _type == 'boolean': - if isinstance(value, str) and value.lower() in ('false', '0', '[]', ''): - return False - return bool(value) - if _type == 'float': - return float(value) - if _type == 'dict': - v = json.loads(value) - if isinstance(v, dict): - return v - raise Exception(_('type error')) - if _type == 'array': - v = json.loads(value) - if isinstance(v, list): - return v - raise Exception(_('type error')) - return value + return common_convert_value(_type, value) except Exception as e: raise Exception( _('Field: {name} Type: {_type} Value: {value} Type error').format(name=name, _type=_type, diff --git a/apps/application/flow/step_node/tool_node/impl/base_tool_node.py b/apps/application/flow/step_node/tool_node/impl/base_tool_node.py index 16d13fa94ec..07c9f643fad 100644 --- a/apps/application/flow/step_node/tool_node/impl/base_tool_node.py +++ b/apps/application/flow/step_node/tool_node/impl/base_tool_node.py @@ -14,6 +14,7 @@ from application.flow.i_step_node import NodeResult from application.flow.step_node.tool_node.i_tool_node import IToolNode +from common.utils.common import common_convert_value from common.utils.tool_code import ToolExecutor from maxkb.const import CONFIG @@ -60,7 +61,7 @@ def valid_reference_value(_type, value, name): return value -def convert_value(name: str, value, _type, is_required, source, node): +def convert_value(name: str, value, _type, is_required, source, node: IToolNode): if not is_required and (value is None or ((isinstance(value, str) or isinstance(value, list)) and len(value) == 0)): return None if source == 'reference': @@ -82,25 +83,7 @@ def convert_value(name: str, value, _type, is_required, source, node): return value try: value = node.workflow_manage.generate_prompt(value) - if _type == 'int': - return int(value) - if _type == 'boolean': - if isinstance(value, str) and value.lower() in ('false', '0', '[]', ''): - return False - return bool(value) - if _type == 'float': - return float(value) - if _type == 'dict': - v = json.loads(value) - if isinstance(v, dict): - return v - raise Exception(_('type error')) - if _type == 'array': - v = json.loads(value) - if isinstance(v, list): - return v - raise Exception(_('type error')) - return value + return common_convert_value(_type, value) except Exception as e: raise Exception( _('Field: {name} Type: {_type} Value: {value} Type error').format(name=name, _type=_type, diff --git a/apps/common/utils/common.py b/apps/common/utils/common.py index e2c6ace2721..f234ebeaf8c 100644 --- a/apps/common/utils/common.py +++ b/apps/common/utils/common.py @@ -8,6 +8,7 @@ """ import hashlib import io +import json import mimetypes import pickle import random @@ -360,3 +361,31 @@ def is_valid_uuid(uuid_string): return str(uuid_obj) == uuid_string except ValueError: return False + +def common_convert_value(_type, value): + if value is None: + return None + + if _type == 'int': + return int(value) + if _type == 'boolean': + if isinstance(value, str) and value.lower() in ('false', '0', '[]', ''): + return False + return bool(value) + if _type == 'float': + return float(value) + if _type == 'dict': + if isinstance(value, dict): + return value + v = json.loads(value) + if isinstance(v, dict): + return v + raise Exception(_('type error')) + if _type == 'array': + if isinstance(value, list): + return value + v = json.loads(value) + if isinstance(v, list): + return v + raise Exception(_('type error')) + return value diff --git a/apps/tools/serializers/tool.py b/apps/tools/serializers/tool.py index a807022dfdf..4bbaa171fe2 100644 --- a/apps/tools/serializers/tool.py +++ b/apps/tools/serializers/tool.py @@ -33,7 +33,7 @@ from common.exception.app_exception import AppApiException from common.field.common import UploadedImageField from common.result import result -from common.utils.common import get_file_content, generate_uuid, bytes_to_uploaded_file +from common.utils.common import get_file_content, generate_uuid, bytes_to_uploaded_file, common_convert_value from common.utils.logger import maxkb_logger from common.utils.rsa_util import rsa_long_decrypt, rsa_long_encrypt from common.utils.tool_code import ToolExecutor @@ -533,25 +533,7 @@ def convert_value(name: str, value: str, _type: str, is_required: bool): if not is_required and (value is None or (isinstance(value, str) and len(value.strip()) == 0)): return None try: - if _type == 'int': - return int(value) - if _type == 'boolean': - if isinstance(value, str) and value.lower() in ('false', '0', '[]', ''): - return False - return bool(value) - if _type == 'float': - return float(value) - if _type == 'dict': - v = json.loads(value) - if isinstance(v, dict): - return v - raise Exception(_('type error')) - if _type == 'array': - v = json.loads(value) - if isinstance(v, list): - return v - raise Exception(_('type error')) - return value + return common_convert_value(_type, value) except Exception as e: raise AppApiException(500, _('Field: {name} Type: {type} Value: {value} Type conversion error').format( name=name, type=_type, value=value diff --git a/apps/trigger/handler/impl/task/tool_task/base_tool_task.py b/apps/trigger/handler/impl/task/tool_task/base_tool_task.py index 09e320560c0..0661d7b7287 100644 --- a/apps/trigger/handler/impl/task/tool_task/base_tool_task.py +++ b/apps/trigger/handler/impl/task/tool_task/base_tool_task.py @@ -12,8 +12,8 @@ import uuid_utils.compat as uuid from django.db.models import QuerySet -from django.utils.translation import gettext as _ +from common.utils.common import common_convert_value from common.utils.logger import maxkb_logger from common.utils.rsa_util import rsa_long_decrypt from common.utils.tool_code import ToolExecutor @@ -43,38 +43,13 @@ def get_field_value(value, kwargs): return get_reference(value.get('value'), kwargs) -def _convert_value(_type, value): - if value is None: - return None - - if _type == 'int': - return int(value) - if _type == 'boolean': - if isinstance(value, str) and value.lower() in ('false', '0', '[]', ''): - return False - return bool(value) - if _type == 'float': - return float(value) - if _type == 'dict': - v = json.loads(value) - if isinstance(v, dict): - return v - raise Exception(_('type error')) - if _type == 'array': - v = json.loads(value) - if isinstance(v, list): - return v - raise Exception(_('type error')) - return value - - def get_tool_execute_parameters(input_field_list, parameter_setting, kwargs): type_map = {f.get("name"): f.get("type") for f in (input_field_list or []) if f.get("name")} parameters = {} for key, value in parameter_setting.items(): raw = get_field_value(value, kwargs) - parameters[key] = _convert_value(type_map.get(key), raw) + parameters[key] = common_convert_value(type_map.get(key), raw) return parameters diff --git a/apps/trigger/handler/impl/task/tool_task/workflow_tool_task.py b/apps/trigger/handler/impl/task/tool_task/workflow_tool_task.py index c0ca5cf86aa..9ef050fb92d 100644 --- a/apps/trigger/handler/impl/task/tool_task/workflow_tool_task.py +++ b/apps/trigger/handler/impl/task/tool_task/workflow_tool_task.py @@ -6,17 +6,16 @@ @date:2026/3/27 18:47 @desc: """ -import json import time import traceback import uuid_utils.compat as uuid from django.db.models import QuerySet -from django.utils.translation import gettext as _ from application.flow.common import WorkflowMode, Workflow from application.flow.i_step_node import ToolWorkflowPostHandler, get_tool_workflow_state from application.serializers.common import ToolExecute +from common.utils.common import common_convert_value from common.utils.logger import maxkb_logger from common.utils.tool_code import ToolExecutor from knowledge.models.knowledge_action import State @@ -44,32 +43,6 @@ def get_field_value(value, kwargs): else: return get_reference(value.get('value'), kwargs) - -def _convert_value(_type, value): - if value is None: - return None - - if _type == 'int': - return int(value) - if _type == 'boolean': - if isinstance(value, str) and value.lower() in ('false', '0', '[]', ''): - return False - return bool(value) - if _type == 'float': - return float(value) - if _type == 'dict': - v = json.loads(value) - if isinstance(v, dict): - return v - raise Exception(_('type error')) - if _type == 'array': - v = json.loads(value) - if isinstance(v, list): - return v - raise Exception(_('type error')) - return value - - def get_tool_execute_parameters(input_field_list, parameter_setting, kwargs): type_map = {f.get("name"): f.get("type") for f in (input_field_list or []) if f.get("name")} @@ -77,7 +50,7 @@ def get_tool_execute_parameters(input_field_list, parameter_setting, kwargs): if parameter_setting: for key, value in parameter_setting.items(): raw = get_field_value(value, kwargs) - parameters[key] = _convert_value(type_map.get(key), raw) + parameters[key] = common_convert_value(type_map.get(key), raw) return parameters From cf51c9cbe9e22af50e9f928eefde58c09d79364a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Fri, 24 Apr 2026 18:56:57 +0800 Subject: [PATCH 2/5] =?UTF-8?q?not=5Fcontain=20=E6=80=A7=E8=83=BD=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/application/flow/compare/not_contain_compare.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/apps/application/flow/compare/not_contain_compare.py b/apps/application/flow/compare/not_contain_compare.py index 2279be33c8d..193aa1c1f78 100644 --- a/apps/application/flow/compare/not_contain_compare.py +++ b/apps/application/flow/compare/not_contain_compare.py @@ -18,9 +18,14 @@ def support(self, node_id, fields: List[str], source_value, compare, target_valu return True def compare(self, source_value, compare, target_value): + target_value = str(target_value) + if isinstance(source_value, str): - return str(target_value) not in source_value + return target_value not in source_value elif isinstance(source_value, list): - return not any([str(item) == str(target_value) for item in source_value]) + for item in source_value: + if str(item) == target_value: + return False + return True else: - return str(target_value) not in str(source_value) + return target_value not in str(source_value) From e54e243e945d9ee46e6c7204b9b2d815a5772fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Fri, 24 Apr 2026 19:01:41 +0800 Subject: [PATCH 3/5] =?UTF-8?q?contain=20compare=20=E6=80=A7=E8=83=BD?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/application/flow/compare/contain_compare.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/apps/application/flow/compare/contain_compare.py b/apps/application/flow/compare/contain_compare.py index 0ea3fd87179..9e45dfccd71 100644 --- a/apps/application/flow/compare/contain_compare.py +++ b/apps/application/flow/compare/contain_compare.py @@ -18,9 +18,14 @@ def support(self, node_id, fields: List[str], source_value, compare, target_valu return True def compare(self, source_value, compare, target_value): + target_value = str(target_value) + if isinstance(source_value, str): - return str(target_value) in source_value + return target_value in source_value elif isinstance(source_value, list): - return any([str(item) == str(target_value) for item in source_value]) + for item in source_value: + if str(item) != target_value: + return False + return True else: - return str(target_value) in str(source_value) + return target_value in str(source_value) From ade823695f61a30fb6c3e97982c703bcc507e19f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Fri, 24 Apr 2026 19:07:05 +0800 Subject: [PATCH 4/5] fix --- apps/application/flow/compare/contain_compare.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/application/flow/compare/contain_compare.py b/apps/application/flow/compare/contain_compare.py index 9e45dfccd71..b79b9cad728 100644 --- a/apps/application/flow/compare/contain_compare.py +++ b/apps/application/flow/compare/contain_compare.py @@ -24,8 +24,8 @@ def compare(self, source_value, compare, target_value): return target_value in source_value elif isinstance(source_value, list): for item in source_value: - if str(item) != target_value: - return False - return True + if str(item) == target_value: + return True + return False else: return target_value in str(source_value) From dc791a696f0832cfe4b03991010a88f3aa5d6ad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Fri, 24 Apr 2026 21:16:08 +0800 Subject: [PATCH 5/5] revert --- .../flow/step_node/tool_lib_node/impl/base_tool_lib_node.py | 2 +- .../application/flow/step_node/tool_node/impl/base_tool_node.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/application/flow/step_node/tool_lib_node/impl/base_tool_lib_node.py b/apps/application/flow/step_node/tool_lib_node/impl/base_tool_lib_node.py index dafbe7edb9f..0f8df18bc6a 100644 --- a/apps/application/flow/step_node/tool_lib_node/impl/base_tool_lib_node.py +++ b/apps/application/flow/step_node/tool_lib_node/impl/base_tool_lib_node.py @@ -86,7 +86,7 @@ def valid_reference_value(_type, value, name): return value -def convert_value(name: str, value, _type, is_required, source, node: IToolLibNode): +def convert_value(name: str, value, _type, is_required, source, node): if not is_required and (value is None or ((isinstance(value, str) or isinstance(value, list)) and len(value) == 0)): return None if source == 'reference': diff --git a/apps/application/flow/step_node/tool_node/impl/base_tool_node.py b/apps/application/flow/step_node/tool_node/impl/base_tool_node.py index 07c9f643fad..f637abf0784 100644 --- a/apps/application/flow/step_node/tool_node/impl/base_tool_node.py +++ b/apps/application/flow/step_node/tool_node/impl/base_tool_node.py @@ -61,7 +61,7 @@ def valid_reference_value(_type, value, name): return value -def convert_value(name: str, value, _type, is_required, source, node: IToolNode): +def convert_value(name: str, value, _type, is_required, source, node): if not is_required and (value is None or ((isinstance(value, str) or isinstance(value, list)) and len(value) == 0)): return None if source == 'reference':