default.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { BlockEnum } from '../../types'
  2. import type { NodeDefault } from '../../types'
  3. import type { ToolNodeType } from './types'
  4. import { VarType as VarKindType } from '@/app/components/workflow/nodes/tool/types'
  5. import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/constants'
  6. const i18nPrefix = 'workflow.errorMsg'
  7. const nodeDefault: NodeDefault<ToolNodeType> = {
  8. defaultValue: {
  9. tool_parameters: {},
  10. tool_configurations: {},
  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: ToolNodeType, t: any, moreDataForCheckValid: any) {
  23. const { toolInputsSchema, toolSettingSchema, language, notAuthed } = moreDataForCheckValid
  24. let errorMessages = ''
  25. if (notAuthed)
  26. errorMessages = t(`${i18nPrefix}.authRequired`)
  27. if (!errorMessages) {
  28. toolInputsSchema.filter((field: any) => {
  29. return field.required
  30. }).forEach((field: any) => {
  31. const targetVar = payload.tool_parameters[field.variable]
  32. if (!targetVar) {
  33. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: field.label })
  34. return
  35. }
  36. const { type: variable_type, value } = targetVar
  37. if (variable_type === VarKindType.variable) {
  38. if (!errorMessages && (!value || value.length === 0))
  39. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: field.label })
  40. }
  41. else {
  42. if (!errorMessages && (value === undefined || value === null || value === ''))
  43. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: field.label })
  44. }
  45. })
  46. }
  47. if (!errorMessages) {
  48. toolSettingSchema.filter((field: any) => {
  49. return field.required
  50. }).forEach((field: any) => {
  51. const value = payload.tool_configurations[field.variable]
  52. if (!errorMessages && (value === undefined || value === null || value === ''))
  53. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: field.label[language] })
  54. })
  55. }
  56. return {
  57. isValid: !errorMessages,
  58. errorMessage: errorMessages,
  59. }
  60. },
  61. }
  62. export default nodeDefault