default.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { BlockEnum } from '../../types'
  2. import type { NodeDefault } from '../../types'
  3. import { CodeLanguage, type CodeNodeType } from './types'
  4. import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/constants'
  5. const i18nPrefix = 'workflow.errorMsg'
  6. const nodeDefault: NodeDefault<CodeNodeType> = {
  7. defaultValue: {
  8. code: '',
  9. code_language: CodeLanguage.python3,
  10. variables: [],
  11. outputs: {},
  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: CodeNodeType, t: any) {
  24. let errorMessages = ''
  25. const { code, variables } = payload
  26. if (!errorMessages && variables.filter(v => !v.variable).length > 0)
  27. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variable`) })
  28. if (!errorMessages && variables.filter(v => !v.value_selector.length).length > 0)
  29. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variableValue`) })
  30. if (!errorMessages && !code)
  31. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.code`) })
  32. return {
  33. isValid: !errorMessages,
  34. errorMessage: errorMessages,
  35. }
  36. },
  37. }
  38. export default nodeDefault