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

default.ts 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { BlockEnum } from '../../types'
  2. import type { NodeDefault, Var } from '../../types'
  3. import { getNotExistVariablesByArray } from '../../utils/workflow'
  4. import { CodeLanguage, type CodeNodeType } 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<CodeNodeType> = {
  8. defaultValue: {
  9. code: '',
  10. code_language: CodeLanguage.python3,
  11. variables: [],
  12. outputs: {},
  13. },
  14. getAvailablePrevNodes(isChatMode: boolean) {
  15. const nodes = isChatMode
  16. ? ALL_CHAT_AVAILABLE_BLOCKS
  17. : ALL_COMPLETION_AVAILABLE_BLOCKS.filter(type => type !== BlockEnum.End)
  18. return nodes
  19. },
  20. getAvailableNextNodes(isChatMode: boolean) {
  21. const nodes = isChatMode ? ALL_CHAT_AVAILABLE_BLOCKS : ALL_COMPLETION_AVAILABLE_BLOCKS
  22. return nodes
  23. },
  24. checkValid(payload: CodeNodeType, t: any) {
  25. let errorMessages = ''
  26. const { code, variables } = payload
  27. if (!errorMessages && variables.filter(v => !v.variable).length > 0)
  28. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variable`) })
  29. if (!errorMessages && variables.filter(v => !v.value_selector.length).length > 0)
  30. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variableValue`) })
  31. if (!errorMessages && !code)
  32. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.code`) })
  33. return {
  34. isValid: !errorMessages,
  35. errorMessage: errorMessages,
  36. }
  37. },
  38. checkVarValid(payload: CodeNodeType, varMap: Record<string, Var>, t: any) {
  39. const errorMessageArr = []
  40. const variables_selector = payload.variables.map(v => v.value_selector)
  41. const variables_selector_warnings = getNotExistVariablesByArray(variables_selector, varMap)
  42. if (variables_selector_warnings.length)
  43. errorMessageArr.push(`${t('workflow.nodes.code.inputVars')} ${t('workflow.common.referenceVar')}${variables_selector_warnings.join('、')}${t('workflow.common.noExist')}`)
  44. return {
  45. isValid: true,
  46. warning_vars: variables_selector_warnings,
  47. errorMessage: errorMessageArr,
  48. }
  49. },
  50. }
  51. export default nodeDefault