default.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { BlockEnum, type NodeDefault } from '../../types'
  2. import { type IfElseNodeType, LogicalOperator } from './types'
  3. import { isEmptyRelatedOperator } from './utils'
  4. import { TransferMethod } from '@/types/app'
  5. import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/constants'
  6. const i18nPrefix = 'workflow.errorMsg'
  7. const nodeDefault: NodeDefault<IfElseNodeType> = {
  8. defaultValue: {
  9. _targetBranches: [
  10. {
  11. id: 'true',
  12. name: 'IF',
  13. },
  14. {
  15. id: 'false',
  16. name: 'ELSE',
  17. },
  18. ],
  19. cases: [
  20. {
  21. case_id: 'true',
  22. logical_operator: LogicalOperator.and,
  23. conditions: [],
  24. },
  25. ],
  26. },
  27. getAvailablePrevNodes(isChatMode: boolean) {
  28. const nodes = isChatMode
  29. ? ALL_CHAT_AVAILABLE_BLOCKS
  30. : ALL_COMPLETION_AVAILABLE_BLOCKS.filter(type => type !== BlockEnum.End)
  31. return nodes
  32. },
  33. getAvailableNextNodes(isChatMode: boolean) {
  34. const nodes = isChatMode ? ALL_CHAT_AVAILABLE_BLOCKS : ALL_COMPLETION_AVAILABLE_BLOCKS
  35. return nodes
  36. },
  37. checkValid(payload: IfElseNodeType, t: any) {
  38. let errorMessages = ''
  39. const { cases } = payload
  40. if (!cases || cases.length === 0)
  41. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: 'IF' })
  42. cases.forEach((caseItem, index) => {
  43. if (!caseItem.conditions.length)
  44. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: index === 0 ? 'IF' : 'ELIF' })
  45. caseItem.conditions.forEach((condition) => {
  46. if (!errorMessages && (!condition.variable_selector || condition.variable_selector.length === 0))
  47. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variable`) })
  48. if (!errorMessages && !condition.comparison_operator)
  49. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.ifElse.operator') })
  50. if (!errorMessages) {
  51. if (condition.sub_variable_condition) {
  52. const isSet = condition.sub_variable_condition.conditions.every((c) => {
  53. if (!c.comparison_operator)
  54. return false
  55. if (isEmptyRelatedOperator(c.comparison_operator!))
  56. return true
  57. return !!c.value
  58. })
  59. if (!isSet)
  60. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variableValue`) })
  61. }
  62. else {
  63. if (!isEmptyRelatedOperator(condition.comparison_operator!) && !condition.value)
  64. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variableValue`) })
  65. }
  66. }
  67. })
  68. })
  69. return {
  70. isValid: !errorMessages,
  71. errorMessage: errorMessages,
  72. }
  73. },
  74. }
  75. export default nodeDefault
  76. export const FILE_TYPE_OPTIONS = [
  77. { value: 'image', i18nKey: 'image' },
  78. { value: 'document', i18nKey: 'doc' },
  79. { value: 'audio', i18nKey: 'audio' },
  80. { value: 'video', i18nKey: 'video' },
  81. ]
  82. export const TRANSFER_METHOD = [
  83. { value: TransferMethod.local_file, i18nKey: 'localUpload' },
  84. { value: TransferMethod.remote_url, i18nKey: 'url' },
  85. ]
  86. export const SUB_VARIABLES = ['type', 'size', 'name', 'url', 'extension', 'mime_type', 'transfer_method']
  87. export const OUTPUT_FILE_SUB_VARIABLES = SUB_VARIABLES.filter(key => key !== 'transfer_method')