Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

default.ts 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { BlockEnum } from '../../types'
  2. import type { NodeDefault, Var } from '../../types'
  3. import { getNotExistVariablesByArray } from '../../utils/workflow'
  4. import { type AssignerNodeType, WriteMode } from './types'
  5. import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/blocks'
  6. const i18nPrefix = 'workflow.errorMsg'
  7. const nodeDefault: NodeDefault<AssignerNodeType> = {
  8. defaultValue: {
  9. version: '2',
  10. items: [],
  11. },
  12. getAvailablePrevNodes(isChatMode: boolean) {
  13. const nodes = isChatMode
  14. ? ALL_CHAT_AVAILABLE_BLOCKS
  15. : ALL_COMPLETION_AVAILABLE_BLOCKS.filter(type => type !== BlockEnum.End)
  16. return nodes
  17. },
  18. getAvailableNextNodes(isChatMode: boolean) {
  19. const nodes = isChatMode ? ALL_CHAT_AVAILABLE_BLOCKS : ALL_COMPLETION_AVAILABLE_BLOCKS
  20. return nodes
  21. },
  22. checkValid(payload: AssignerNodeType, t: any) {
  23. let errorMessages = ''
  24. const {
  25. items: operationItems,
  26. } = payload
  27. operationItems?.forEach((value) => {
  28. if (!errorMessages && !value.variable_selector?.length)
  29. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.assigner.assignedVariable') })
  30. if (!errorMessages && value.operation !== WriteMode.clear && value.operation !== WriteMode.removeFirst && value.operation !== WriteMode.removeLast) {
  31. if (value.operation === WriteMode.set || value.operation === WriteMode.increment
  32. || value.operation === WriteMode.decrement || value.operation === WriteMode.multiply
  33. || value.operation === WriteMode.divide) {
  34. if (!value.value && typeof value.value !== 'number')
  35. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.assigner.variable') })
  36. }
  37. else if (!value.value?.length) {
  38. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.assigner.variable') })
  39. }
  40. }
  41. })
  42. return {
  43. isValid: !errorMessages,
  44. errorMessage: errorMessages,
  45. }
  46. },
  47. checkVarValid(payload: AssignerNodeType, varMap: Record<string, Var>, t: any) {
  48. const errorMessageArr: string[] = []
  49. const items = payload.items ?? []
  50. const variables_warnings = getNotExistVariablesByArray(items.map(item => item.variable_selector ?? []) ?? [], varMap)
  51. if (variables_warnings.length)
  52. errorMessageArr.push(`${t('workflow.nodes.assigner.assignedVariable')} ${t('workflow.common.referenceVar')}${variables_warnings.join('、')}${t('workflow.common.noExist')}`)
  53. const value_warnings = getNotExistVariablesByArray(items.map(item => item.value ?? []) ?? [], varMap)
  54. if (value_warnings.length)
  55. errorMessageArr.push(`${t('workflow.nodes.assigner.setVariable')} ${t('workflow.common.referenceVar')}${value_warnings.join('、')}${t('workflow.common.noExist')}`)
  56. return {
  57. isValid: true,
  58. warning_vars: [...variables_warnings, ...value_warnings],
  59. errorMessage: errorMessageArr,
  60. }
  61. },
  62. }
  63. export default nodeDefault