2025-06-25 16:15:59 +08:00
|
|
|
import { useMemo } from 'react'
|
2025-06-17 16:28:50 +08:00
|
|
|
import type { BaseConfiguration } from '@/app/components/base/form/form-scenarios/base/types'
|
|
|
|
import { type RAGPipelineVariables, VAR_TYPE_MAP } from '@/models/pipeline'
|
2025-06-25 16:15:59 +08:00
|
|
|
import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
2025-05-21 10:53:18 +08:00
|
|
|
|
2025-06-25 16:15:59 +08:00
|
|
|
export const useInitialData = (variables: RAGPipelineVariables, lastRunInputData?: Record<string, any>) => {
|
2025-05-21 10:53:18 +08:00
|
|
|
const initialData = useMemo(() => {
|
2025-06-17 16:28:50 +08:00
|
|
|
return variables.reduce((acc, item) => {
|
|
|
|
const type = VAR_TYPE_MAP[item.type]
|
2025-06-25 16:15:59 +08:00
|
|
|
const variableName = item.variable
|
|
|
|
const defaultValue = lastRunInputData?.[variableName] || item.default_value
|
2025-06-17 16:28:50 +08:00
|
|
|
if ([BaseFieldType.textInput, BaseFieldType.paragraph, BaseFieldType.select].includes(type))
|
2025-06-25 16:15:59 +08:00
|
|
|
acc[variableName] = defaultValue ?? ''
|
2025-06-17 16:28:50 +08:00
|
|
|
if (type === BaseFieldType.numberInput)
|
2025-07-01 11:24:30 +08:00
|
|
|
acc[variableName] = defaultValue
|
2025-06-17 16:28:50 +08:00
|
|
|
if (type === BaseFieldType.checkbox)
|
2025-06-25 16:15:59 +08:00
|
|
|
acc[variableName] = defaultValue ?? false
|
2025-06-17 16:28:50 +08:00
|
|
|
if ([BaseFieldType.file, BaseFieldType.fileList].includes(type))
|
2025-06-25 16:15:59 +08:00
|
|
|
acc[variableName] = defaultValue ?? []
|
2025-06-17 16:28:50 +08:00
|
|
|
return acc
|
|
|
|
}, {} as Record<string, any>)
|
2025-06-25 16:15:59 +08:00
|
|
|
}, [lastRunInputData, variables])
|
2025-05-21 10:53:18 +08:00
|
|
|
|
|
|
|
return initialData
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useConfigurations = (variables: RAGPipelineVariables) => {
|
|
|
|
const configurations = useMemo(() => {
|
|
|
|
const configurations: BaseConfiguration[] = []
|
|
|
|
variables.forEach((item) => {
|
|
|
|
configurations.push({
|
2025-06-17 16:28:50 +08:00
|
|
|
type: VAR_TYPE_MAP[item.type],
|
2025-05-21 10:53:18 +08:00
|
|
|
variable: item.variable,
|
|
|
|
label: item.label,
|
|
|
|
required: item.required,
|
2025-06-17 16:28:50 +08:00
|
|
|
maxLength: item.max_length,
|
2025-05-21 10:53:18 +08:00
|
|
|
options: item.options?.map(option => ({
|
|
|
|
label: option,
|
|
|
|
value: option,
|
|
|
|
})),
|
|
|
|
showConditions: [],
|
2025-06-17 16:28:50 +08:00
|
|
|
placeholder: item.placeholder,
|
|
|
|
tooltip: item.tooltips,
|
|
|
|
unit: item.unit,
|
2025-05-21 10:53:18 +08:00
|
|
|
allowedFileTypes: item.allowed_file_types,
|
|
|
|
allowedFileExtensions: item.allowed_file_extensions,
|
2025-06-17 16:28:50 +08:00
|
|
|
allowedFileUploadMethods: item.allowed_file_upload_methods,
|
2025-05-21 10:53:18 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
return configurations
|
|
|
|
}, [variables])
|
|
|
|
|
|
|
|
return configurations
|
|
|
|
}
|