default.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { BlockEnum } from '../../types'
  2. import type { NodeDefault } from '../../types'
  3. import { type AssignerNodeType, WriteMode } from './types'
  4. import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/constants'
  5. const i18nPrefix = 'workflow.errorMsg'
  6. const nodeDefault: NodeDefault<AssignerNodeType> = {
  7. defaultValue: {
  8. version: '2',
  9. items: [],
  10. },
  11. getAvailablePrevNodes(isChatMode: boolean) {
  12. const nodes = isChatMode
  13. ? ALL_CHAT_AVAILABLE_BLOCKS
  14. : ALL_COMPLETION_AVAILABLE_BLOCKS.filter(type => type !== BlockEnum.End)
  15. return nodes
  16. },
  17. getAvailableNextNodes(isChatMode: boolean) {
  18. const nodes = isChatMode ? ALL_CHAT_AVAILABLE_BLOCKS : ALL_COMPLETION_AVAILABLE_BLOCKS
  19. return nodes
  20. },
  21. checkValid(payload: AssignerNodeType, t: any) {
  22. let errorMessages = ''
  23. const {
  24. items: operationItems,
  25. } = payload
  26. operationItems?.forEach((value) => {
  27. if (!errorMessages && !value.variable_selector?.length)
  28. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.assigner.assignedVariable') })
  29. if (!errorMessages && value.operation !== WriteMode.clear) {
  30. if (value.operation === WriteMode.set) {
  31. if (!value.value && typeof value.value !== 'number')
  32. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.assigner.variable') })
  33. }
  34. else if (!value.value?.length) {
  35. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.assigner.variable') })
  36. }
  37. }
  38. })
  39. return {
  40. isValid: !errorMessages,
  41. errorMessage: errorMessages,
  42. }
  43. },
  44. }
  45. export default nodeDefault