default.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { type NodeDefault, VarType } from '../../types'
  2. import { BlockEnum } from '../../types'
  3. import type { VariableAssignerNodeType } from './types'
  4. import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/constants'
  5. const i18nPrefix = 'workflow'
  6. const nodeDefault: NodeDefault<VariableAssignerNodeType> = {
  7. defaultValue: {
  8. output_type: VarType.any,
  9. variables: [],
  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: VariableAssignerNodeType, t: any) {
  22. let errorMessages = ''
  23. const { variables } = payload
  24. if (!variables || variables.length === 0)
  25. errorMessages = t(`${i18nPrefix}.errorMsg.fieldRequired`, { field: t(`${i18nPrefix}.nodes.variableAssigner.title`) })
  26. if (!errorMessages) {
  27. variables.forEach((variable) => {
  28. if (!variable || variable.length === 0)
  29. errorMessages = t(`${i18nPrefix}.errorMsg.fieldRequired`, { field: t(`${i18nPrefix}.errorMsg.fields.variableValue`) })
  30. })
  31. }
  32. return {
  33. isValid: !errorMessages,
  34. errorMessage: errorMessages,
  35. }
  36. },
  37. }
  38. export default nodeDefault