diff --git a/apps/application/flow/compare/contain_compare.py b/apps/application/flow/compare/contain_compare.py index 0ea3fd87179..b79b9cad728 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 True + return False else: - return str(target_value) in str(source_value) + return target_value in str(source_value) 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) 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..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 @@ -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 @@ -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..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 @@ -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 @@ -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