95 lines
3.0 KiB
TypeScript
Raw Normal View History

2025-04-22 16:46:33 +08:00
import type { NodeDefault } from '../../types'
import type { DataSourceNodeType } from './types'
2025-06-05 16:51:01 +08:00
import { DataSourceClassification } from './types'
2025-04-25 11:32:17 +08:00
import { genNodeMetaData } from '@/app/components/workflow/utils'
import { BlockEnum } from '@/app/components/workflow/types'
2025-06-05 16:51:01 +08:00
import {
COMMON_OUTPUT,
2025-06-06 14:21:23 +08:00
LOCAL_FILE_OUTPUT,
ONLINE_DOCUMENT_OUTPUT,
ONLINE_DRIVE_OUTPUT,
2025-06-06 14:21:23 +08:00
WEBSITE_CRAWL_OUTPUT,
2025-06-05 16:51:01 +08:00
} from './constants'
2025-06-06 10:54:44 +08:00
import { VarType as VarKindType } from '@/app/components/workflow/nodes/tool/types'
const i18nPrefix = 'workflow.errorMsg'
2025-04-22 16:46:33 +08:00
2025-04-29 16:11:54 +08:00
const metaData = genNodeMetaData({
sort: -1,
type: BlockEnum.DataSource,
})
2025-04-22 16:46:33 +08:00
const nodeDefault: NodeDefault<DataSourceNodeType> = {
2025-04-29 16:11:54 +08:00
metaData,
2025-05-23 18:19:01 +08:00
defaultValue: {
datasource_parameters: {},
datasource_configurations: {},
},
2025-06-06 10:54:44 +08:00
checkValid(payload, t, moreDataForCheckValid) {
const { dataSourceInputsSchema, notAuthed } = moreDataForCheckValid
let errorMessage = ''
if (notAuthed)
errorMessage = t(`${i18nPrefix}.authRequired`)
if (!errorMessage) {
dataSourceInputsSchema.filter((field: any) => {
return field.required
}).forEach((field: any) => {
const targetVar = payload.datasource_parameters[field.variable]
if (!targetVar) {
errorMessage = t(`${i18nPrefix}.fieldRequired`, { field: field.label })
return
}
const { type: variable_type, value } = targetVar
if (variable_type === VarKindType.variable) {
if (!errorMessage && (!value || value.length === 0))
errorMessage = t(`${i18nPrefix}.fieldRequired`, { field: field.label })
}
else {
if (!errorMessage && (value === undefined || value === null || value === ''))
errorMessage = t(`${i18nPrefix}.fieldRequired`, { field: field.label })
}
})
}
2025-04-22 16:46:33 +08:00
return {
2025-06-06 10:54:44 +08:00
isValid: !errorMessage,
errorMessage,
2025-04-22 16:46:33 +08:00
}
},
2025-06-03 16:07:58 +08:00
getOutputVars(payload, ragVars = []) {
2025-05-27 18:44:04 +08:00
const {
provider_type,
} = payload
2025-06-06 14:21:23 +08:00
const isLocalFile = provider_type === DataSourceClassification.localFile
const isWebsiteCrawl = provider_type === DataSourceClassification.websiteCrawl
const isOnlineDocument = provider_type === DataSourceClassification.onlineDocument
const isOnlineDrive = provider_type === DataSourceClassification.onlineDrive
2025-05-23 18:19:01 +08:00
return [
2025-06-05 16:51:01 +08:00
...COMMON_OUTPUT.map(item => ({ variable: item.name, type: item.type })),
2025-05-23 18:19:01 +08:00
...(
isLocalFile
2025-06-06 14:21:23 +08:00
? LOCAL_FILE_OUTPUT.map(item => ({ variable: item.name, type: item.type }))
2025-06-05 16:51:01 +08:00
: []
),
...(
isWebsiteCrawl
2025-06-06 14:21:23 +08:00
? WEBSITE_CRAWL_OUTPUT.map(item => ({ variable: item.name, type: item.type }))
: []
),
...(
isOnlineDocument
? ONLINE_DOCUMENT_OUTPUT.map(item => ({ variable: item.name, type: item.type }))
2025-05-23 18:19:01 +08:00
: []
),
...(
isOnlineDrive
? ONLINE_DRIVE_OUTPUT.map(item => ({ variable: item.name, type: item.type }))
: []
),
2025-06-03 16:07:58 +08:00
...ragVars,
2025-05-23 18:19:01 +08:00
]
},
2025-04-22 16:46:33 +08:00
}
export default nodeDefault