default.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { BlockEnum, VarType } from '../../types'
  2. import type { NodeDefault } from '../../types'
  3. import { comparisonOperatorNotRequireValue } from '../if-else/utils'
  4. import { type ListFilterNodeType, OrderBy } from './types'
  5. import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/constants'
  6. const i18nPrefix = 'workflow.errorMsg'
  7. const nodeDefault: NodeDefault<ListFilterNodeType> = {
  8. defaultValue: {
  9. variable: [],
  10. filter_by: {
  11. enabled: false,
  12. conditions: [],
  13. },
  14. extract_by: {
  15. enabled: false,
  16. serial: '1',
  17. },
  18. order_by: {
  19. enabled: false,
  20. key: '',
  21. value: OrderBy.ASC,
  22. },
  23. limit: {
  24. enabled: false,
  25. size: 10,
  26. },
  27. },
  28. getAvailablePrevNodes(isChatMode: boolean) {
  29. const nodes = isChatMode
  30. ? ALL_CHAT_AVAILABLE_BLOCKS
  31. : ALL_COMPLETION_AVAILABLE_BLOCKS.filter(type => type !== BlockEnum.End)
  32. return nodes
  33. },
  34. getAvailableNextNodes(isChatMode: boolean) {
  35. const nodes = isChatMode ? ALL_CHAT_AVAILABLE_BLOCKS : ALL_COMPLETION_AVAILABLE_BLOCKS
  36. return nodes
  37. },
  38. checkValid(payload: ListFilterNodeType, t: any) {
  39. let errorMessages = ''
  40. const { variable, var_type, filter_by } = payload
  41. if (!errorMessages && !variable?.length)
  42. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.listFilter.inputVar') })
  43. // Check filter condition
  44. if (!errorMessages && filter_by?.enabled) {
  45. if (var_type === VarType.arrayFile && !filter_by.conditions[0]?.key)
  46. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.listFilter.filterConditionKey') })
  47. if (!errorMessages && !filter_by.conditions[0]?.comparison_operator)
  48. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.listFilter.filterConditionComparisonOperator') })
  49. if (!errorMessages && !comparisonOperatorNotRequireValue(filter_by.conditions[0]?.comparison_operator) && !filter_by.conditions[0]?.value)
  50. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.listFilter.filterConditionComparisonValue') })
  51. }
  52. return {
  53. isValid: !errorMessages,
  54. errorMessage: errorMessages,
  55. }
  56. },
  57. }
  58. export default nodeDefault