Initialize human input values by field type

This commit is contained in:
JzoNg 2026-04-22 08:12:31 +08:00
parent 5309b56225
commit 80ede7cdb5
2 changed files with 89 additions and 27 deletions

View File

@ -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>): FormInputItem => ({
label: 'field',
variable: 'field',
required: false,
max_length: 128,
const paragraphInput = (overrides: Partial<Extract<FormInputItem, { type: InputVarType.paragraph }>> = {}): 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<Extract<FormInputItem, { type: InputVarType.select }>> = {}): FormInputItem => ({
type: InputVarType.select,
output_variable_name: 'field',
option_source: {
type: 'constant',
value: ['option-a', 'option-b'],
selector: [],
},
...overrides,
})
const fileInput = (overrides: Partial<Extract<FormInputItem, { type: InputVarType.singleFile }>> = {}): 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<Extract<FormInputItem, { type: InputVarType.multiFiles }>> = {}): 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: [] },
}),

View File

@ -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
}