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.5KB

6 months ago
6 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { VarType } from '../../types'
  2. import type { NodeDefault } from '../../types'
  3. import { ComparisonOperator, LogicalOperator, type LoopNodeType } from './types'
  4. import { isEmptyRelatedOperator } from './utils'
  5. import { TransferMethod } from '@/types/app'
  6. import { LOOP_NODE_MAX_COUNT } from '@/config'
  7. import { genNodeMetaData } from '@/app/components/workflow/utils'
  8. import { BlockEnum } from '@/app/components/workflow/types'
  9. import { BlockClassificationEnum } from '@/app/components/workflow/block-selector/types'
  10. const i18nPrefix = 'workflow.errorMsg'
  11. const metaData = genNodeMetaData({
  12. classification: BlockClassificationEnum.Logic,
  13. sort: 3,
  14. type: BlockEnum.Loop,
  15. author: 'AICT-Team',
  16. })
  17. const nodeDefault: NodeDefault<LoopNodeType> = {
  18. metaData,
  19. defaultValue: {
  20. start_node_id: '',
  21. break_conditions: [],
  22. loop_count: 10,
  23. _children: [],
  24. logical_operator: LogicalOperator.and,
  25. },
  26. checkValid(payload: LoopNodeType, t: any) {
  27. let errorMessages = ''
  28. payload.loop_variables?.forEach((variable) => {
  29. if (!variable.label)
  30. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variable`) })
  31. })
  32. payload.break_conditions!.forEach((condition) => {
  33. if (!errorMessages && (!condition.variable_selector || condition.variable_selector.length === 0))
  34. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variable`) })
  35. if (!errorMessages && !condition.comparison_operator)
  36. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.ifElse.operator') })
  37. if (!errorMessages) {
  38. if (condition.sub_variable_condition
  39. && ![ComparisonOperator.empty, ComparisonOperator.notEmpty].includes(condition.comparison_operator!)) {
  40. const isSet = condition.sub_variable_condition.conditions.every((c) => {
  41. if (!c.comparison_operator)
  42. return false
  43. if (isEmptyRelatedOperator(c.comparison_operator!))
  44. return true
  45. return !!c.value
  46. })
  47. if (!isSet)
  48. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variableValue`) })
  49. }
  50. else {
  51. if (!isEmptyRelatedOperator(condition.comparison_operator!) && (condition.varType === VarType.boolean ? condition.value === undefined : !condition.value))
  52. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variableValue`) })
  53. }
  54. }
  55. })
  56. if (!errorMessages && (
  57. Number.isNaN(Number(payload.loop_count))
  58. || !Number.isInteger(Number(payload.loop_count))
  59. || payload.loop_count < 1
  60. || payload.loop_count > LOOP_NODE_MAX_COUNT
  61. ))
  62. errorMessages = t('workflow.nodes.loop.loopMaxCountError', { maxCount: LOOP_NODE_MAX_COUNT })
  63. return {
  64. isValid: !errorMessages,
  65. errorMessage: errorMessages,
  66. }
  67. },
  68. }
  69. export const FILE_TYPE_OPTIONS = [
  70. { value: 'image', i18nKey: 'image' },
  71. { value: 'document', i18nKey: 'doc' },
  72. { value: 'audio', i18nKey: 'audio' },
  73. { value: 'video', i18nKey: 'video' },
  74. ]
  75. export const TRANSFER_METHOD = [
  76. { value: TransferMethod.local_file, i18nKey: 'localUpload' },
  77. { value: TransferMethod.remote_url, i18nKey: 'url' },
  78. ]
  79. export const SUB_VARIABLES = ['type', 'size', 'name', 'url', 'extension', 'mime_type', 'transfer_method', 'related_id']
  80. export const OUTPUT_FILE_SUB_VARIABLES = SUB_VARIABLES.filter(key => key !== 'transfer_method')
  81. export default nodeDefault