default.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. payload.break_conditions!.forEach((condition) => {
  30. if (!errorMessages && (!condition.variable_selector || condition.variable_selector.length === 0))
  31. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variable`) })
  32. if (!errorMessages && !condition.comparison_operator)
  33. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.ifElse.operator') })
  34. if (!errorMessages) {
  35. if (condition.sub_variable_condition
  36. && ![ComparisonOperator.empty, ComparisonOperator.notEmpty].includes(condition.comparison_operator!)) {
  37. const isSet = condition.sub_variable_condition.conditions.every((c) => {
  38. if (!c.comparison_operator)
  39. return false
  40. if (isEmptyRelatedOperator(c.comparison_operator!))
  41. return true
  42. return !!c.value
  43. })
  44. if (!isSet)
  45. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variableValue`) })
  46. }
  47. else {
  48. if (!isEmptyRelatedOperator(condition.comparison_operator!) && !condition.value)
  49. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variableValue`) })
  50. }
  51. }
  52. })
  53. if (!errorMessages && (
  54. Number.isNaN(Number(payload.loop_count))
  55. || !Number.isInteger(Number(payload.loop_count))
  56. || payload.loop_count < 1
  57. || payload.loop_count > LOOP_NODE_MAX_COUNT
  58. ))
  59. errorMessages = t('workflow.nodes.loop.loopMaxCountError', { maxCount: LOOP_NODE_MAX_COUNT })
  60. return {
  61. isValid: !errorMessages,
  62. errorMessage: errorMessages,
  63. }
  64. },
  65. }
  66. export const FILE_TYPE_OPTIONS = [
  67. { value: 'image', i18nKey: 'image' },
  68. { value: 'document', i18nKey: 'doc' },
  69. { value: 'audio', i18nKey: 'audio' },
  70. { value: 'video', i18nKey: 'video' },
  71. ]
  72. export const TRANSFER_METHOD = [
  73. { value: TransferMethod.local_file, i18nKey: 'localUpload' },
  74. { value: TransferMethod.remote_url, i18nKey: 'url' },
  75. ]
  76. export const SUB_VARIABLES = ['type', 'size', 'name', 'url', 'extension', 'mime_type', 'transfer_method', 'related_id']
  77. export const OUTPUT_FILE_SUB_VARIABLES = SUB_VARIABLES.filter(key => key !== 'transfer_method')
  78. export default nodeDefault