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.

default.ts 3.7KB

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