default.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/blocks'
  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, advanced_settings } = payload
  24. const { group_enabled = false, groups = [] } = advanced_settings || {}
  25. // enable group
  26. const validateVariables = (variables: any[], field: string) => {
  27. variables.forEach((variable) => {
  28. if (!variable || variable.length === 0)
  29. errorMessages = t(`${i18nPrefix}.errorMsg.fieldRequired`, { field: t(field) })
  30. })
  31. }
  32. if (group_enabled) {
  33. if (!groups || groups.length === 0) {
  34. errorMessages = t(`${i18nPrefix}.errorMsg.fieldRequired`, { field: t(`${i18nPrefix}.nodes.variableAssigner.title`) })
  35. }
  36. else if (!errorMessages) {
  37. groups.forEach((group) => {
  38. validateVariables(group.variables || [], `${i18nPrefix}.errorMsg.fields.variableValue`)
  39. })
  40. }
  41. }
  42. else {
  43. if (!variables || variables.length === 0)
  44. errorMessages = t(`${i18nPrefix}.errorMsg.fieldRequired`, { field: t(`${i18nPrefix}.nodes.variableAssigner.title`) })
  45. else if (!errorMessages)
  46. validateVariables(variables, `${i18nPrefix}.errorMsg.fields.variableValue`)
  47. }
  48. return {
  49. isValid: !errorMessages,
  50. errorMessage: errorMessages,
  51. }
  52. },
  53. }
  54. export default nodeDefault