default.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { BlockEnum, type NodeDefault } from '../../types'
  2. import { type IfElseNodeType, LogicalOperator } from './types'
  3. import { isEmptyRelatedOperator } from './utils'
  4. import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/constants'
  5. const i18nPrefix = 'workflow.errorMsg'
  6. const nodeDefault: NodeDefault<IfElseNodeType> = {
  7. defaultValue: {
  8. _targetBranches: [
  9. {
  10. id: 'true',
  11. name: 'IS TRUE',
  12. },
  13. {
  14. id: 'false',
  15. name: 'IS FALSE',
  16. },
  17. ],
  18. logical_operator: LogicalOperator.and,
  19. conditions: [],
  20. },
  21. getAvailablePrevNodes(isChatMode: boolean) {
  22. const nodes = isChatMode
  23. ? ALL_CHAT_AVAILABLE_BLOCKS
  24. : ALL_COMPLETION_AVAILABLE_BLOCKS.filter(type => type !== BlockEnum.End)
  25. return nodes
  26. },
  27. getAvailableNextNodes(isChatMode: boolean) {
  28. const nodes = isChatMode ? ALL_CHAT_AVAILABLE_BLOCKS : ALL_COMPLETION_AVAILABLE_BLOCKS
  29. return nodes.filter(type => type !== BlockEnum.VariableAssigner)
  30. },
  31. checkValid(payload: IfElseNodeType, t: any) {
  32. let errorMessages = ''
  33. const { conditions } = payload
  34. if (!conditions || conditions.length === 0)
  35. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: 'IF' })
  36. conditions.forEach((condition) => {
  37. if (!errorMessages && (!condition.variable_selector || condition.variable_selector.length === 0))
  38. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variable`) })
  39. if (!errorMessages && !condition.comparison_operator)
  40. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.ifElse.operator') })
  41. if (!errorMessages && !isEmptyRelatedOperator(condition.comparison_operator!) && !condition.value)
  42. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variableValue`) })
  43. })
  44. return {
  45. isValid: !errorMessages,
  46. errorMessage: errorMessages,
  47. }
  48. },
  49. }
  50. export default nodeDefault