perf: value type checker (#5255)

* perf: value type checker

* fix: ts
This commit is contained in:
Archer
2025-07-18 16:39:42 +08:00
committed by GitHub
parent 6f75c1b80e
commit 593c816c1a
4 changed files with 32 additions and 33 deletions

View File

@@ -97,58 +97,50 @@ export const valueTypeFormat = (value: any, type?: WorkflowIOValueTypeEnum) => {
} }
// 4.3 字符串转对象 // 4.3 字符串转对象
if ( if (type === WorkflowIOValueTypeEnum.object) {
(type === WorkflowIOValueTypeEnum.object || type.startsWith('array')) && if (isObjectString(value)) {
typeof value === 'string' && const trimmedValue = value.trim();
value.trim()
) {
const trimmedValue = value.trim();
const isJsonString = isObjectString(trimmedValue);
if (isJsonString) {
try { try {
const parsed = json5.parse(trimmedValue); return json5.parse(trimmedValue);
// 检测解析结果与目标类型是否一致
if (type.startsWith('array') && Array.isArray(parsed)) return parsed;
if (type === WorkflowIOValueTypeEnum.object && typeof parsed === 'object') return parsed;
} catch (error) {} } catch (error) {}
} }
return {};
} }
// 4.4 数组类型(这里 value 不是数组类型)TODO: 嵌套数据类型转化) // 4.4 数组类型(这里 value 不是数组类型)TODO: 嵌套数据类型转化)
if (type.startsWith('array')) { if (type.startsWith('array')) {
if (isObjectString(value)) {
try {
return json5.parse(value);
} catch (error) {}
}
return [value]; return [value];
} }
// 4.5 特殊类型处理 // 4.5 特殊类型处理
if ( if (
[WorkflowIOValueTypeEnum.datasetQuote, WorkflowIOValueTypeEnum.selectDataset].includes(type) [
WorkflowIOValueTypeEnum.datasetQuote,
WorkflowIOValueTypeEnum.selectDataset,
WorkflowIOValueTypeEnum.selectApp
].includes(type)
) { ) {
if (isObjectString(value)) { if (isObjectString(value)) {
try { try {
return json5.parse(value); return json5.parse(value);
} catch (error) { } catch (error) {}
return [];
}
} }
return []; return [];
} }
if (
[WorkflowIOValueTypeEnum.selectApp, WorkflowIOValueTypeEnum.object].includes(type) && // Invalid history type
typeof value === 'string' if (type === WorkflowIOValueTypeEnum.chatHistory) {
) {
if (isObjectString(value)) { if (isObjectString(value)) {
try { try {
return json5.parse(value); return json5.parse(value);
} catch (error) { } catch (error) {}
return {};
}
} }
return {}; return [];
}
// Invalid history type
if (type === WorkflowIOValueTypeEnum.chatHistory) {
return 0;
} }
// 5. 默认返回原值 // 5. 默认返回原值

View File

@@ -58,6 +58,7 @@ export const dispatchUpdateVariable = async (props: Props): Promise<Response> =>
variables variables
}) })
: item.value?.[1]; : item.value?.[1];
return valueTypeFormat(val, item.valueType); return valueTypeFormat(val, item.valueType);
} else { } else {
return getReferenceVariableValue({ return getReferenceVariableValue({

View File

@@ -97,7 +97,13 @@ const PromptEditor = ({
/> />
)} )}
</Box> </Box>
<MyModal isOpen={isOpen} onClose={onClose} iconSrc="modal/edit" title={title} w={'full'}> <MyModal
isOpen={isOpen}
onClose={onClose}
iconSrc="modal/edit"
title={title || t('common:Edit')}
w={'full'}
>
<ModalBody> <ModalBody>
<Editor <Editor
minH={400} minH={400}

View File

@@ -48,7 +48,7 @@ describe('valueTypeFormat', () => {
{ {
value: 'false', value: 'false',
type: WorkflowIOValueTypeEnum.selectApp, type: WorkflowIOValueTypeEnum.selectApp,
result: {} result: []
}, },
{ {
value: 'false', value: 'false',
@@ -171,7 +171,7 @@ describe('valueTypeFormat', () => {
{ {
value: true, value: true,
type: WorkflowIOValueTypeEnum.object, type: WorkflowIOValueTypeEnum.object,
result: true result: {}
} }
]; ];
boolTestList.forEach((item, index) => { boolTestList.forEach((item, index) => {
@@ -263,7 +263,7 @@ describe('valueTypeFormat', () => {
{ {
value: '1', value: '1',
type: WorkflowIOValueTypeEnum.chatHistory, type: WorkflowIOValueTypeEnum.chatHistory,
result: 0 result: []
} }
]; ];
chatHistoryTestList.forEach((item, index) => { chatHistoryTestList.forEach((item, index) => {