default.ts 3.5 KB

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