mirror of
https://github.com/langgenius/dify.git
synced 2025-11-15 19:03:51 +00:00
feat: centralize variable type mapping by introducing VAR_TYPE_MAP in pipeline model
This commit is contained in:
parent
7ac0f0c08c
commit
796797d12b
@ -1,19 +1,9 @@
|
|||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
||||||
import { usePublishedPipelineProcessingParams } from '@/service/use-pipeline'
|
import { usePublishedPipelineProcessingParams } from '@/service/use-pipeline'
|
||||||
import { PipelineInputVarType } from '@/models/pipeline'
|
import { VAR_TYPE_MAP } from '@/models/pipeline'
|
||||||
import { useDatasetDetailContextWithSelector } from '@/context/dataset-detail'
|
import { useDatasetDetailContextWithSelector } from '@/context/dataset-detail'
|
||||||
|
|
||||||
const VAR_TYPE_MAP: Record<PipelineInputVarType, BaseFieldType> = {
|
|
||||||
[PipelineInputVarType.textInput]: BaseFieldType.textInput,
|
|
||||||
[PipelineInputVarType.paragraph]: BaseFieldType.paragraph,
|
|
||||||
[PipelineInputVarType.select]: BaseFieldType.select,
|
|
||||||
[PipelineInputVarType.singleFile]: BaseFieldType.file,
|
|
||||||
[PipelineInputVarType.multiFiles]: BaseFieldType.fileList,
|
|
||||||
[PipelineInputVarType.number]: BaseFieldType.numberInput,
|
|
||||||
[PipelineInputVarType.checkbox]: BaseFieldType.checkbox,
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useConfigurations = (datasourceNodeId: string) => {
|
export const useConfigurations = (datasourceNodeId: string) => {
|
||||||
const pipelineId = useDatasetDetailContextWithSelector(state => state.dataset?.pipeline_id)
|
const pipelineId = useDatasetDetailContextWithSelector(state => state.dataset?.pipeline_id)
|
||||||
const { data: paramsConfig, isFetching: isFetchingParams } = usePublishedPipelineProcessingParams({
|
const { data: paramsConfig, isFetching: isFetchingParams } = usePublishedPipelineProcessingParams({
|
||||||
|
|||||||
@ -1,21 +1,22 @@
|
|||||||
import type { BaseConfiguration, BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
import type { BaseConfiguration } from '@/app/components/base/form/form-scenarios/base/types'
|
||||||
import { PipelineInputVarType, type RAGPipelineVariables } from '@/models/pipeline'
|
import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
||||||
|
import { type RAGPipelineVariables, VAR_TYPE_MAP } from '@/models/pipeline'
|
||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
|
|
||||||
export const useInitialData = (variables: RAGPipelineVariables) => {
|
export const useInitialData = (variables: RAGPipelineVariables) => {
|
||||||
const initialData = useMemo(() => {
|
const initialData = useMemo(() => {
|
||||||
const initialData: Record<string, any> = {}
|
return variables.reduce((acc, item) => {
|
||||||
variables.forEach((item) => {
|
const type = VAR_TYPE_MAP[item.type]
|
||||||
if ([PipelineInputVarType.textInput, PipelineInputVarType.paragraph, PipelineInputVarType.select].includes(item.type))
|
if ([BaseFieldType.textInput, BaseFieldType.paragraph, BaseFieldType.select].includes(type))
|
||||||
initialData[item.variable] = item.default_value || ''
|
acc[item.variable] = item.default_value ?? ''
|
||||||
if (item.type === PipelineInputVarType.number)
|
if (type === BaseFieldType.numberInput)
|
||||||
initialData[item.variable] = item.default_value || 0
|
acc[item.variable] = item.default_value ?? 0
|
||||||
if ([PipelineInputVarType.singleFile, PipelineInputVarType.multiFiles].includes(item.type))
|
if (type === BaseFieldType.checkbox)
|
||||||
initialData[item.variable] = []
|
acc[item.variable] = true
|
||||||
if (item.type === PipelineInputVarType.checkbox)
|
if ([BaseFieldType.file, BaseFieldType.fileList].includes(type))
|
||||||
initialData[item.variable] = item.default_value || true
|
acc[item.variable] = []
|
||||||
})
|
return acc
|
||||||
return initialData
|
}, {} as Record<string, any>)
|
||||||
}, [variables])
|
}, [variables])
|
||||||
|
|
||||||
return initialData
|
return initialData
|
||||||
@ -26,21 +27,22 @@ export const useConfigurations = (variables: RAGPipelineVariables) => {
|
|||||||
const configurations: BaseConfiguration[] = []
|
const configurations: BaseConfiguration[] = []
|
||||||
variables.forEach((item) => {
|
variables.forEach((item) => {
|
||||||
configurations.push({
|
configurations.push({
|
||||||
type: item.type as unknown as BaseFieldType,
|
type: VAR_TYPE_MAP[item.type],
|
||||||
variable: item.variable,
|
variable: item.variable,
|
||||||
label: item.label,
|
label: item.label,
|
||||||
required: item.required,
|
required: item.required,
|
||||||
placeholder: item.placeholder,
|
maxLength: item.max_length,
|
||||||
tooltip: item.tooltips,
|
|
||||||
options: item.options?.map(option => ({
|
options: item.options?.map(option => ({
|
||||||
label: option,
|
label: option,
|
||||||
value: option,
|
value: option,
|
||||||
})),
|
})),
|
||||||
maxLength: item.max_length,
|
|
||||||
showConditions: [],
|
showConditions: [],
|
||||||
allowedFileUploadMethods: item.allowed_file_upload_methods,
|
placeholder: item.placeholder,
|
||||||
|
tooltip: item.tooltips,
|
||||||
|
unit: item.unit,
|
||||||
allowedFileTypes: item.allowed_file_types,
|
allowedFileTypes: item.allowed_file_types,
|
||||||
allowedFileExtensions: item.allowed_file_extensions,
|
allowedFileExtensions: item.allowed_file_extensions,
|
||||||
|
allowedFileUploadMethods: item.allowed_file_upload_methods,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
return configurations
|
return configurations
|
||||||
|
|||||||
@ -3,17 +3,7 @@ import type { BaseConfiguration } from '@/app/components/base/form/form-scenario
|
|||||||
import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
||||||
import { useStore } from '@/app/components/workflow/store'
|
import { useStore } from '@/app/components/workflow/store'
|
||||||
import { useDraftPipelineProcessingParams } from '@/service/use-pipeline'
|
import { useDraftPipelineProcessingParams } from '@/service/use-pipeline'
|
||||||
import { PipelineInputVarType } from '@/models/pipeline'
|
import { VAR_TYPE_MAP } from '@/models/pipeline'
|
||||||
|
|
||||||
const VAR_TYPE_MAP: Record<PipelineInputVarType, BaseFieldType> = {
|
|
||||||
[PipelineInputVarType.textInput]: BaseFieldType.textInput,
|
|
||||||
[PipelineInputVarType.paragraph]: BaseFieldType.paragraph,
|
|
||||||
[PipelineInputVarType.select]: BaseFieldType.select,
|
|
||||||
[PipelineInputVarType.singleFile]: BaseFieldType.file,
|
|
||||||
[PipelineInputVarType.multiFiles]: BaseFieldType.fileList,
|
|
||||||
[PipelineInputVarType.number]: BaseFieldType.numberInput,
|
|
||||||
[PipelineInputVarType.checkbox]: BaseFieldType.checkbox,
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useConfigurations = (datasourceNodeId: string) => {
|
export const useConfigurations = (datasourceNodeId: string) => {
|
||||||
const pipelineId = useStore(state => state.pipelineId)
|
const pipelineId = useStore(state => state.pipelineId)
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import type { Dependency } from '@/app/components/plugins/types'
|
|||||||
import type { AppIconSelection } from '@/app/components/base/app-icon-picker'
|
import type { AppIconSelection } from '@/app/components/base/app-icon-picker'
|
||||||
import type { Viewport } from 'reactflow'
|
import type { Viewport } from 'reactflow'
|
||||||
import type { TransferMethod } from '@/types/app'
|
import type { TransferMethod } from '@/types/app'
|
||||||
|
import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
||||||
|
|
||||||
export enum DatasourceType {
|
export enum DatasourceType {
|
||||||
localFile = 'local_file',
|
localFile = 'local_file',
|
||||||
@ -119,6 +120,16 @@ export enum PipelineInputVarType {
|
|||||||
checkbox = 'checkbox',
|
checkbox = 'checkbox',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const VAR_TYPE_MAP: Record<PipelineInputVarType, BaseFieldType> = {
|
||||||
|
[PipelineInputVarType.textInput]: BaseFieldType.textInput,
|
||||||
|
[PipelineInputVarType.paragraph]: BaseFieldType.paragraph,
|
||||||
|
[PipelineInputVarType.select]: BaseFieldType.select,
|
||||||
|
[PipelineInputVarType.singleFile]: BaseFieldType.file,
|
||||||
|
[PipelineInputVarType.multiFiles]: BaseFieldType.fileList,
|
||||||
|
[PipelineInputVarType.number]: BaseFieldType.numberInput,
|
||||||
|
[PipelineInputVarType.checkbox]: BaseFieldType.checkbox,
|
||||||
|
}
|
||||||
|
|
||||||
export type RAGPipelineVariable = {
|
export type RAGPipelineVariable = {
|
||||||
belong_to_node_id: string // indicates belong to which node or 'shared'
|
belong_to_node_id: string // indicates belong to which node or 'shared'
|
||||||
type: PipelineInputVarType
|
type: PipelineInputVarType
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user