Skip to content

Commit a6ed82f

Browse files
perf: Optimize the convert_value method and reuse it (#5166)
1 parent ec8e4c4 commit a6ed82f

8 files changed

Lines changed: 55 additions & 120 deletions

File tree

apps/application/flow/compare/contain_compare.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ def support(self, node_id, fields: List[str], source_value, compare, target_valu
1818
return True
1919

2020
def compare(self, source_value, compare, target_value):
21+
target_value = str(target_value)
22+
2123
if isinstance(source_value, str):
22-
return str(target_value) in source_value
24+
return target_value in source_value
2325
elif isinstance(source_value, list):
24-
return any([str(item) == str(target_value) for item in source_value])
26+
for item in source_value:
27+
if str(item) == target_value:
28+
return True
29+
return False
2530
else:
26-
return str(target_value) in str(source_value)
31+
return target_value in str(source_value)

apps/application/flow/compare/not_contain_compare.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ def support(self, node_id, fields: List[str], source_value, compare, target_valu
1818
return True
1919

2020
def compare(self, source_value, compare, target_value):
21+
target_value = str(target_value)
22+
2123
if isinstance(source_value, str):
22-
return str(target_value) not in source_value
24+
return target_value not in source_value
2325
elif isinstance(source_value, list):
24-
return not any([str(item) == str(target_value) for item in source_value])
26+
for item in source_value:
27+
if str(item) == target_value:
28+
return False
29+
return True
2530
else:
26-
return str(target_value) not in str(source_value)
31+
return target_value not in str(source_value)

apps/application/flow/step_node/tool_lib_node/impl/base_tool_lib_node.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from application.flow.step_node.tool_lib_node.i_tool_lib_node import IToolLibNode
2626
from common.database_model_manage.database_model_manage import DatabaseModelManage
2727
from common.exception.app_exception import AppApiException
28+
from common.utils.common import common_convert_value
2829
from common.utils.logger import maxkb_logger
2930
from common.utils.rsa_util import rsa_long_decrypt
3031
from common.utils.tool_code import ToolExecutor
@@ -107,25 +108,7 @@ def convert_value(name: str, value, _type, is_required, source, node):
107108
return value
108109
try:
109110
value = node.workflow_manage.generate_prompt(value)
110-
if _type == 'int':
111-
return int(value)
112-
if _type == 'boolean':
113-
if isinstance(value, str) and value.lower() in ('false', '0', '[]', ''):
114-
return False
115-
return bool(value)
116-
if _type == 'float':
117-
return float(value)
118-
if _type == 'dict':
119-
v = json.loads(value)
120-
if isinstance(v, dict):
121-
return v
122-
raise Exception(_('type error'))
123-
if _type == 'array':
124-
v = json.loads(value)
125-
if isinstance(v, list):
126-
return v
127-
raise Exception(_('type error'))
128-
return value
111+
return common_convert_value(_type, value)
129112
except Exception as e:
130113
raise Exception(
131114
_('Field: {name} Type: {_type} Value: {value} Type error').format(name=name, _type=_type,

apps/application/flow/step_node/tool_node/impl/base_tool_node.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from application.flow.i_step_node import NodeResult
1616
from application.flow.step_node.tool_node.i_tool_node import IToolNode
17+
from common.utils.common import common_convert_value
1718
from common.utils.tool_code import ToolExecutor
1819
from maxkb.const import CONFIG
1920

@@ -82,25 +83,7 @@ def convert_value(name: str, value, _type, is_required, source, node):
8283
return value
8384
try:
8485
value = node.workflow_manage.generate_prompt(value)
85-
if _type == 'int':
86-
return int(value)
87-
if _type == 'boolean':
88-
if isinstance(value, str) and value.lower() in ('false', '0', '[]', ''):
89-
return False
90-
return bool(value)
91-
if _type == 'float':
92-
return float(value)
93-
if _type == 'dict':
94-
v = json.loads(value)
95-
if isinstance(v, dict):
96-
return v
97-
raise Exception(_('type error'))
98-
if _type == 'array':
99-
v = json.loads(value)
100-
if isinstance(v, list):
101-
return v
102-
raise Exception(_('type error'))
103-
return value
86+
return common_convert_value(_type, value)
10487
except Exception as e:
10588
raise Exception(
10689
_('Field: {name} Type: {_type} Value: {value} Type error').format(name=name, _type=_type,

apps/common/utils/common.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"""
99
import hashlib
1010
import io
11+
import json
1112
import mimetypes
1213
import pickle
1314
import random
@@ -360,3 +361,31 @@ def is_valid_uuid(uuid_string):
360361
return str(uuid_obj) == uuid_string
361362
except ValueError:
362363
return False
364+
365+
def common_convert_value(_type, value):
366+
if value is None:
367+
return None
368+
369+
if _type == 'int':
370+
return int(value)
371+
if _type == 'boolean':
372+
if isinstance(value, str) and value.lower() in ('false', '0', '[]', ''):
373+
return False
374+
return bool(value)
375+
if _type == 'float':
376+
return float(value)
377+
if _type == 'dict':
378+
if isinstance(value, dict):
379+
return value
380+
v = json.loads(value)
381+
if isinstance(v, dict):
382+
return v
383+
raise Exception(_('type error'))
384+
if _type == 'array':
385+
if isinstance(value, list):
386+
return value
387+
v = json.loads(value)
388+
if isinstance(v, list):
389+
return v
390+
raise Exception(_('type error'))
391+
return value

apps/tools/serializers/tool.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from common.exception.app_exception import AppApiException
3434
from common.field.common import UploadedImageField
3535
from common.result import result
36-
from common.utils.common import get_file_content, generate_uuid, bytes_to_uploaded_file
36+
from common.utils.common import get_file_content, generate_uuid, bytes_to_uploaded_file, common_convert_value
3737
from common.utils.logger import maxkb_logger
3838
from common.utils.rsa_util import rsa_long_decrypt, rsa_long_encrypt
3939
from common.utils.tool_code import ToolExecutor
@@ -533,25 +533,7 @@ def convert_value(name: str, value: str, _type: str, is_required: bool):
533533
if not is_required and (value is None or (isinstance(value, str) and len(value.strip()) == 0)):
534534
return None
535535
try:
536-
if _type == 'int':
537-
return int(value)
538-
if _type == 'boolean':
539-
if isinstance(value, str) and value.lower() in ('false', '0', '[]', ''):
540-
return False
541-
return bool(value)
542-
if _type == 'float':
543-
return float(value)
544-
if _type == 'dict':
545-
v = json.loads(value)
546-
if isinstance(v, dict):
547-
return v
548-
raise Exception(_('type error'))
549-
if _type == 'array':
550-
v = json.loads(value)
551-
if isinstance(v, list):
552-
return v
553-
raise Exception(_('type error'))
554-
return value
536+
return common_convert_value(_type, value)
555537
except Exception as e:
556538
raise AppApiException(500, _('Field: {name} Type: {type} Value: {value} Type conversion error').format(
557539
name=name, type=_type, value=value

apps/trigger/handler/impl/task/tool_task/base_tool_task.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
import uuid_utils.compat as uuid
1414
from django.db.models import QuerySet
15-
from django.utils.translation import gettext as _
1615

16+
from common.utils.common import common_convert_value
1717
from common.utils.logger import maxkb_logger
1818
from common.utils.rsa_util import rsa_long_decrypt
1919
from common.utils.tool_code import ToolExecutor
@@ -43,38 +43,13 @@ def get_field_value(value, kwargs):
4343
return get_reference(value.get('value'), kwargs)
4444

4545

46-
def _convert_value(_type, value):
47-
if value is None:
48-
return None
49-
50-
if _type == 'int':
51-
return int(value)
52-
if _type == 'boolean':
53-
if isinstance(value, str) and value.lower() in ('false', '0', '[]', ''):
54-
return False
55-
return bool(value)
56-
if _type == 'float':
57-
return float(value)
58-
if _type == 'dict':
59-
v = json.loads(value)
60-
if isinstance(v, dict):
61-
return v
62-
raise Exception(_('type error'))
63-
if _type == 'array':
64-
v = json.loads(value)
65-
if isinstance(v, list):
66-
return v
67-
raise Exception(_('type error'))
68-
return value
69-
70-
7146
def get_tool_execute_parameters(input_field_list, parameter_setting, kwargs):
7247
type_map = {f.get("name"): f.get("type") for f in (input_field_list or []) if f.get("name")}
7348

7449
parameters = {}
7550
for key, value in parameter_setting.items():
7651
raw = get_field_value(value, kwargs)
77-
parameters[key] = _convert_value(type_map.get(key), raw)
52+
parameters[key] = common_convert_value(type_map.get(key), raw)
7853
return parameters
7954

8055

apps/trigger/handler/impl/task/tool_task/workflow_tool_task.py

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66
@date:2026/3/27 18:47
77
@desc:
88
"""
9-
import json
109
import time
1110
import traceback
1211

1312
import uuid_utils.compat as uuid
1413
from django.db.models import QuerySet
15-
from django.utils.translation import gettext as _
1614

1715
from application.flow.common import WorkflowMode, Workflow
1816
from application.flow.i_step_node import ToolWorkflowPostHandler, get_tool_workflow_state
1917
from application.serializers.common import ToolExecute
18+
from common.utils.common import common_convert_value
2019
from common.utils.logger import maxkb_logger
2120
from common.utils.tool_code import ToolExecutor
2221
from knowledge.models.knowledge_action import State
@@ -44,40 +43,14 @@ def get_field_value(value, kwargs):
4443
else:
4544
return get_reference(value.get('value'), kwargs)
4645

47-
48-
def _convert_value(_type, value):
49-
if value is None:
50-
return None
51-
52-
if _type == 'int':
53-
return int(value)
54-
if _type == 'boolean':
55-
if isinstance(value, str) and value.lower() in ('false', '0', '[]', ''):
56-
return False
57-
return bool(value)
58-
if _type == 'float':
59-
return float(value)
60-
if _type == 'dict':
61-
v = json.loads(value)
62-
if isinstance(v, dict):
63-
return v
64-
raise Exception(_('type error'))
65-
if _type == 'array':
66-
v = json.loads(value)
67-
if isinstance(v, list):
68-
return v
69-
raise Exception(_('type error'))
70-
return value
71-
72-
7346
def get_tool_execute_parameters(input_field_list, parameter_setting, kwargs):
7447
type_map = {f.get("name"): f.get("type") for f in (input_field_list or []) if f.get("name")}
7548

7649
parameters = {}
7750
if parameter_setting:
7851
for key, value in parameter_setting.items():
7952
raw = get_field_value(value, kwargs)
80-
parameters[key] = _convert_value(type_map.get(key), raw)
53+
parameters[key] = common_convert_value(type_map.get(key), raw)
8154
return parameters
8255

8356

0 commit comments

Comments
 (0)