diff --git a/web/app/components/base/chat/chat/answer/human-input-content/__tests__/utils.spec.ts b/web/app/components/base/chat/chat/answer/human-input-content/__tests__/utils.spec.ts index 96ecda2430..99bc2363e2 100644 --- a/web/app/components/base/chat/chat/answer/human-input-content/__tests__/utils.spec.ts +++ b/web/app/components/base/chat/chat/answer/human-input-content/__tests__/utils.spec.ts @@ -1,7 +1,8 @@ import type { FormInputItem } from '@/app/components/workflow/nodes/human-input/types' import type { Locale } from '@/i18n-config/language' import { UserActionButtonType } from '@/app/components/workflow/nodes/human-input/types' -import { InputVarType } from '@/app/components/workflow/types' +import { InputVarType, SupportUploadFileTypes } from '@/app/components/workflow/types' +import { TransferMethod } from '@/types/app' import { getButtonStyle, getRelativeTime, @@ -10,20 +11,46 @@ import { splitByOutputVar, } from '../utils' -const createInput = (overrides: Partial): FormInputItem => ({ - label: 'field', - variable: 'field', - required: false, - max_length: 128, +const paragraphInput = (overrides: Partial> = {}): FormInputItem => ({ type: InputVarType.paragraph, - default: { - type: 'constant' as const, - value: '', - selector: [], // Dummy selector - }, output_variable_name: 'field', + default: { + type: 'constant', + value: '', + selector: [], + }, ...overrides, -} as unknown as FormInputItem) +}) + +const selectInput = (overrides: Partial> = {}): FormInputItem => ({ + type: InputVarType.select, + output_variable_name: 'field', + option_source: { + type: 'constant', + value: ['option-a', 'option-b'], + selector: [], + }, + ...overrides, +}) + +const fileInput = (overrides: Partial> = {}): FormInputItem => ({ + type: InputVarType.singleFile, + output_variable_name: 'field', + allowed_file_extensions: [], + allowed_file_types: [SupportUploadFileTypes.image], + allowed_file_upload_methods: [TransferMethod.local_file], + ...overrides, +}) + +const fileListInput = (overrides: Partial> = {}): FormInputItem => ({ + type: InputVarType.multiFiles, + output_variable_name: 'field', + allowed_file_extensions: [], + allowed_file_types: [SupportUploadFileTypes.image], + allowed_file_upload_methods: [TransferMethod.local_file], + max_upload_count: 5, + ...overrides, +}) describe('human-input utils', () => { describe('getButtonStyle', () => { @@ -54,15 +81,13 @@ describe('human-input utils', () => { }) describe('initializeInputs', () => { - it('should initialize text fields with constants and variable defaults', () => { - const formInputs = [ - createInput({ - type: InputVarType.paragraph, + it('should initialize paragraph fields with constants and variable defaults', () => { + const formInputs: FormInputItem[] = [ + paragraphInput({ output_variable_name: 'name', default: { type: 'constant', value: 'John', selector: [] }, }), - createInput({ - type: InputVarType.paragraph, + paragraphInput({ output_variable_name: 'bio', default: { type: 'variable', value: '', selector: [] }, }), @@ -74,23 +99,45 @@ describe('human-input utils', () => { }) }) - it('should set non text-like inputs to undefined', () => { - const formInputs = [ - createInput({ - type: InputVarType.select, + it('should initialize select fields with empty strings', () => { + const formInputs: FormInputItem[] = [ + selectInput({ output_variable_name: 'role', }), ] expect(initializeInputs(formInputs)).toEqual({ - role: undefined, + role: '', + }) + }) + + it('should initialize single file fields with null', () => { + const formInputs: FormInputItem[] = [ + fileInput({ + output_variable_name: 'avatar', + }), + ] + + expect(initializeInputs(formInputs)).toEqual({ + avatar: null, + }) + }) + + it('should initialize file list fields with empty arrays', () => { + const formInputs: FormInputItem[] = [ + fileListInput({ + output_variable_name: 'attachments', + }), + ] + + expect(initializeInputs(formInputs)).toEqual({ + attachments: [], }) }) it('should fallback to empty string when variable default is missing', () => { - const formInputs = [ - createInput({ - type: InputVarType.paragraph, + const formInputs: FormInputItem[] = [ + paragraphInput({ output_variable_name: 'summary', default: { type: 'variable', value: '', selector: [] }, }), diff --git a/web/app/components/base/chat/chat/answer/human-input-content/utils.ts b/web/app/components/base/chat/chat/answer/human-input-content/utils.ts index ecf82f6f7c..c5e90fbf63 100644 --- a/web/app/components/base/chat/chat/answer/human-input-content/utils.ts +++ b/web/app/components/base/chat/chat/answer/human-input-content/utils.ts @@ -7,7 +7,10 @@ import isSameOrAfter from 'dayjs/plugin/isSameOrAfter' import relativeTime from 'dayjs/plugin/relativeTime' import utc from 'dayjs/plugin/utc' import { + isFileFormInput, + isFileListFormInput, isParagraphFormInput, + isSelectFormInput, UserActionButtonType, } from '@/app/components/workflow/nodes/human-input/types' import 'dayjs/locale/en' @@ -47,7 +50,19 @@ export const initializeInputs = (formInputs: FormInputItem[], defaultValues: Rec return } - initialInputs[item.output_variable_name] = '' + if (isSelectFormInput(item)) { + initialInputs[item.output_variable_name] = '' + return + } + + if (isFileFormInput(item)) { + initialInputs[item.output_variable_name] = null + return + } + + if (isFileListFormInput(item)) { + initialInputs[item.output_variable_name] = [] + } }) return initialInputs }