fix: value type (#6076)
Some checks failed
Build FastGPT images in Personal warehouse / get-vars (push) Has been cancelled
Build FastGPT images in Personal warehouse / build-fastgpt-images (map[arch:amd64 runs-on:ubuntu-24.04]) (push) Has been cancelled
Build FastGPT images in Personal warehouse / build-fastgpt-images (map[arch:arm64 runs-on:ubuntu-24.04-arm]) (push) Has been cancelled
Build FastGPT images in Personal warehouse / release-fastgpt-images (push) Has been cancelled

* fix: value type

* perf: empty val check
This commit is contained in:
Archer 2025-12-10 22:23:40 +08:00 committed by GitHub
parent 2da73a6555
commit eaeabf825a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 21 additions and 133 deletions

View File

@ -67,25 +67,8 @@ export const valueTypeFormat = (value: any, valueType?: WorkflowIOValueTypeEnum)
};
// Handle null/undefined, return default value by type
if (value === undefined || value === null) {
if (!valueType || valueType === WorkflowIOValueTypeEnum.any) return value;
// Default value map (use function to ensure new reference each time)
const defaultValueMap: Partial<Record<WorkflowIOValueTypeEnum, () => any>> = {
[WorkflowIOValueTypeEnum.string]: () => '',
[WorkflowIOValueTypeEnum.number]: () => 0,
[WorkflowIOValueTypeEnum.boolean]: () => false,
[WorkflowIOValueTypeEnum.arrayString]: () => [],
[WorkflowIOValueTypeEnum.arrayNumber]: () => [],
[WorkflowIOValueTypeEnum.arrayBoolean]: () => [],
[WorkflowIOValueTypeEnum.arrayObject]: () => [],
[WorkflowIOValueTypeEnum.arrayAny]: () => [],
[WorkflowIOValueTypeEnum.object]: () => ({})
};
const getDefaultValue = defaultValueMap[valueType];
return getDefaultValue ? getDefaultValue() : value;
}
if (value === undefined || value === null) return value;
if (!valueType || valueType === WorkflowIOValueTypeEnum.any) return value;
// Password check
if (valueType === WorkflowIOValueTypeEnum.string && isSecretValue(value)) return value;

View File

@ -537,7 +537,7 @@ const llmCompletionsBodyFormat = async <T extends CompletionsBodyType>({
: undefined,
...modelData?.defaultConfig,
response_format,
stop: stop?.split('|'),
stop: stop?.split('|').filter((item) => !!item.trim()),
...(toolCallMode === 'toolChoice' && {
tools,
tool_choice,

View File

@ -20,7 +20,7 @@ export const computedMaxToken = ({
if (maxToken === undefined) return;
maxToken = Math.min(maxToken, model.maxResponse);
return Math.max(maxToken, min || 0);
return Math.max(maxToken, min || 1);
};
// FastGPT temperature range: [0,10], ai temperature:[0,2],{0,1]……

View File

@ -62,14 +62,13 @@ const UNDEFINED_SIGN = 'UNDEFINED_SIGN';
export const dispatchHttp468Request = async (props: HttpRequestProps): Promise<HttpResponse> => {
let {
runningAppInfo: { id: appId, teamId, tmbId },
runningAppInfo: { id: appId },
chatId,
responseChatItemId,
variables,
node,
runtimeNodes,
histories,
workflowStreamResponse,
params: {
system_httpMethod: httpMethod = 'POST',
system_httpReqUrl: httpReqUrl,
@ -332,8 +331,10 @@ export const replaceJsonBodyString = (
};
const valToStr = (val: any, isQuoted = false) => {
if (val === undefined) return 'null';
if (val === null) return 'null';
if (val === undefined || val === null) {
if (isQuoted) return '';
return 'null';
}
if (typeof val === 'object') {
const jsonStr = JSON.stringify(val);

View File

@ -19,6 +19,8 @@ describe('replaceJsonBodyString', () => {
] as unknown as RuntimeNodeItemType[];
const mockVariables = {
undefinedVar: undefined,
nullVar: null,
userName: 'John Doe',
userAge: 30,
isActive: true,
@ -43,6 +45,14 @@ describe('replaceJsonBodyString', () => {
};
describe('Basic variable replacement functionality', () => {
it('字符串为空', () => {
const input = '{"name": "{{undefinedVar}}"}';
const expected = '{"name": ""}';
const result = replaceJsonBodyString({ text: input }, mockProps);
expect(result).toBe(expected);
});
it('should correctly replace string variables', () => {
const input = '{"name": "{{userName}}", "greeting": "Hello {{userName}}"}';
const expected = '{"name": "John Doe", "greeting": "Hello John Doe"}';
@ -241,7 +251,7 @@ describe('replaceJsonBodyString', () => {
it('should handle non-existent variables', () => {
const input = '{"missing": "{{nonExistentVar}}", "static": "value"}';
const expected = '{"missing": "null", "static": "value"}';
const expected = '{"missing": "", "static": "value"}';
const result = replaceJsonBodyString({ text: input }, mockProps);
expect(result).toBe(expected);
@ -626,7 +636,7 @@ describe('replaceJsonBodyString', () => {
// Verify no code execution occurred (template injection variable was replaced with null because it doesn't exist)
expect(typeof parsed.templateTests.inject).toBe('string');
expect(parsed.templateTests.inject).toBe('null'); // Non-existent variables become "null"
expect(parsed.templateTests.inject).toBe(''); // Non-existent variables become "null"
});
});
});

View File

@ -311,109 +311,3 @@ describe('valueTypeFormat', () => {
// });
// });
});
import { getHistories } from '@fastgpt/service/core/workflow/dispatch/utils';
import { ChatItemValueTypeEnum, ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import type { ChatItemType } from '@fastgpt/global/core/chat/type';
describe('getHistories test', async () => {
const MockHistories: ChatItemType[] = [
{
obj: ChatRoleEnum.System,
value: [
{
type: ChatItemValueTypeEnum.text,
text: {
content: '你好'
}
}
]
},
{
obj: ChatRoleEnum.Human,
value: [
{
type: ChatItemValueTypeEnum.text,
text: {
content: '你好'
}
}
]
},
{
obj: ChatRoleEnum.AI,
value: [
{
type: ChatItemValueTypeEnum.text,
text: {
content: '你好2'
}
}
]
},
{
obj: ChatRoleEnum.Human,
value: [
{
type: ChatItemValueTypeEnum.text,
text: {
content: '你好3'
}
}
]
},
{
obj: ChatRoleEnum.AI,
value: [
{
type: ChatItemValueTypeEnum.text,
text: {
content: '你好4'
}
}
]
}
];
it('getHistories', async () => {
// Number
expect(getHistories(1, MockHistories)).toEqual([
...MockHistories.slice(0, 1),
...MockHistories.slice(-2)
]);
expect(getHistories(2, MockHistories)).toEqual([...MockHistories.slice(0)]);
expect(getHistories(4, MockHistories)).toEqual([...MockHistories.slice(0)]);
// Array
expect(
getHistories(
[
{
obj: ChatRoleEnum.Human,
value: [
{
type: ChatItemValueTypeEnum.text,
text: {
content: '你好'
}
}
]
}
],
MockHistories
)
).toEqual([
{
obj: ChatRoleEnum.Human,
value: [
{
type: ChatItemValueTypeEnum.text,
text: {
content: '你好'
}
}
]
}
]);
});
});