You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import type { NodeDefault } from '../../types'
  2. import type { DataSourceNodeType } from './types'
  3. import { DataSourceClassification } from './types'
  4. import { genNodeMetaData } from '@/app/components/workflow/utils'
  5. import { BlockEnum } from '@/app/components/workflow/types'
  6. import {
  7. COMMON_OUTPUT,
  8. LOCAL_FILE_OUTPUT,
  9. } from './constants'
  10. import { VarType as VarKindType } from '@/app/components/workflow/nodes/tool/types'
  11. import { getMatchedSchemaType } from '../_base/components/variable/use-match-schema-type'
  12. const i18nPrefix = 'workflow.errorMsg'
  13. const metaData = genNodeMetaData({
  14. sort: -1,
  15. type: BlockEnum.DataSource,
  16. isStart: true,
  17. isRequired: true,
  18. })
  19. const nodeDefault: NodeDefault<DataSourceNodeType> = {
  20. metaData,
  21. defaultValue: {
  22. datasource_parameters: {},
  23. datasource_configurations: {},
  24. },
  25. checkValid(payload, t, moreDataForCheckValid) {
  26. const { dataSourceInputsSchema, notAuthed } = moreDataForCheckValid
  27. let errorMessage = ''
  28. if (notAuthed)
  29. errorMessage = t(`${i18nPrefix}.authRequired`)
  30. if (!errorMessage) {
  31. dataSourceInputsSchema.filter((field: any) => {
  32. return field.required
  33. }).forEach((field: any) => {
  34. const targetVar = payload.datasource_parameters[field.variable]
  35. if (!targetVar) {
  36. errorMessage = t(`${i18nPrefix}.fieldRequired`, { field: field.label })
  37. return
  38. }
  39. const { type: variable_type, value } = targetVar
  40. if (variable_type === VarKindType.variable) {
  41. if (!errorMessage && (!value || value.length === 0))
  42. errorMessage = t(`${i18nPrefix}.fieldRequired`, { field: field.label })
  43. }
  44. else {
  45. if (!errorMessage && (value === undefined || value === null || value === ''))
  46. errorMessage = t(`${i18nPrefix}.fieldRequired`, { field: field.label })
  47. }
  48. })
  49. }
  50. return {
  51. isValid: !errorMessage,
  52. errorMessage,
  53. }
  54. },
  55. getOutputVars(payload, allPluginInfoList, ragVars = [], { schemaTypeDefinitions } = { schemaTypeDefinitions: [] }) {
  56. const {
  57. plugin_id,
  58. datasource_name,
  59. provider_type,
  60. } = payload
  61. const isLocalFile = provider_type === DataSourceClassification.localFile
  62. const currentDataSource = allPluginInfoList.dataSourceList?.find((ds: any) => ds.plugin_id === plugin_id)
  63. const currentDataSourceItem = currentDataSource?.tools?.find((tool: any) => tool.name === datasource_name)
  64. const output_schema = currentDataSourceItem?.output_schema
  65. const dynamicOutputSchema: any[] = []
  66. if (output_schema?.properties) {
  67. Object.keys(output_schema.properties).forEach((outputKey) => {
  68. const output = output_schema.properties[outputKey]
  69. const dataType = output.type
  70. let type = dataType === 'array'
  71. ? `array[${output.items?.type.slice(0, 1).toLocaleLowerCase()}${output.items?.type.slice(1)}]`
  72. : `${dataType.slice(0, 1).toLocaleLowerCase()}${dataType.slice(1)}`
  73. const schemaType = getMatchedSchemaType?.(output, schemaTypeDefinitions)
  74. if (type === 'object' && schemaType === 'file')
  75. type = 'file'
  76. dynamicOutputSchema.push({
  77. variable: outputKey,
  78. type,
  79. description: output.description,
  80. schemaType,
  81. children: output.type === 'object' ? {
  82. schema: {
  83. type: 'object',
  84. properties: output.properties,
  85. },
  86. } : undefined,
  87. })
  88. })
  89. }
  90. return [
  91. ...COMMON_OUTPUT.map(item => ({ variable: item.name, type: item.type })),
  92. ...(
  93. isLocalFile
  94. ? LOCAL_FILE_OUTPUT.map(item => ({ variable: item.name, type: item.type }))
  95. : []
  96. ),
  97. ...ragVars,
  98. ...dynamicOutputSchema,
  99. ]
  100. },
  101. }
  102. export default nodeDefault