您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

default.ts 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import type { Var } from '../../types'
  2. import { type NodeDefault, VarType } from '../../types'
  3. import { BlockEnum } from '../../types'
  4. import { getNotExistVariablesByArray } from '../../utils/workflow'
  5. import type { VariableAssignerNodeType } from './types'
  6. import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/blocks'
  7. const i18nPrefix = 'workflow'
  8. const nodeDefault: NodeDefault<VariableAssignerNodeType> = {
  9. defaultValue: {
  10. output_type: VarType.any,
  11. variables: [],
  12. },
  13. getAvailablePrevNodes(isChatMode: boolean) {
  14. const nodes = isChatMode
  15. ? ALL_CHAT_AVAILABLE_BLOCKS
  16. : ALL_COMPLETION_AVAILABLE_BLOCKS.filter(type => type !== BlockEnum.End)
  17. return nodes
  18. },
  19. getAvailableNextNodes(isChatMode: boolean) {
  20. const nodes = isChatMode ? ALL_CHAT_AVAILABLE_BLOCKS : ALL_COMPLETION_AVAILABLE_BLOCKS
  21. return nodes
  22. },
  23. checkValid(payload: VariableAssignerNodeType, t: any) {
  24. let errorMessages = ''
  25. const { variables, advanced_settings } = payload
  26. const { group_enabled = false, groups = [] } = advanced_settings || {}
  27. // enable group
  28. const validateVariables = (variables: any[], field: string) => {
  29. variables.forEach((variable) => {
  30. if (!variable || variable.length === 0)
  31. errorMessages = t(`${i18nPrefix}.errorMsg.fieldRequired`, { field: t(field) })
  32. })
  33. }
  34. if (group_enabled) {
  35. if (!groups || groups.length === 0) {
  36. errorMessages = t(`${i18nPrefix}.errorMsg.fieldRequired`, { field: t(`${i18nPrefix}.nodes.variableAssigner.title`) })
  37. }
  38. else if (!errorMessages) {
  39. groups.forEach((group) => {
  40. validateVariables(group.variables || [], `${i18nPrefix}.errorMsg.fields.variableValue`)
  41. })
  42. }
  43. }
  44. else {
  45. if (!variables || variables.length === 0)
  46. errorMessages = t(`${i18nPrefix}.errorMsg.fieldRequired`, { field: t(`${i18nPrefix}.nodes.variableAssigner.title`) })
  47. else if (!errorMessages)
  48. validateVariables(variables, `${i18nPrefix}.errorMsg.fields.variableValue`)
  49. }
  50. return {
  51. isValid: !errorMessages,
  52. errorMessage: errorMessages,
  53. }
  54. },
  55. checkVarValid(payload: VariableAssignerNodeType, varMap: Record<string, Var>, t: any) {
  56. const errorMessageArr: string[] = []
  57. const variables_warnings = getNotExistVariablesByArray(payload.variables ?? [], varMap)
  58. if (variables_warnings.length)
  59. errorMessageArr.push(`${t('workflow.nodes.variableAssigner.title')} ${t('workflow.common.referenceVar')}${variables_warnings.join('、')}${t('workflow.common.noExist')}`)
  60. return {
  61. isValid: true,
  62. warning_vars: variables_warnings,
  63. errorMessage: errorMessageArr,
  64. }
  65. },
  66. }
  67. export default nodeDefault