You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

default.ts 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/blocks'
  6. const i18nPrefix = 'workflow.errorMsg'
  7. const nodeDefault: NodeDefault<ToolNodeType> = {
  8. defaultValue: {
  9. tool_parameters: {},
  10. tool_configurations: {},
  11. tool_node_version: '2',
  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: ToolNodeType, t: any, moreDataForCheckValid: any) {
  24. const { toolInputsSchema, toolSettingSchema, language, notAuthed } = moreDataForCheckValid
  25. let errorMessages = ''
  26. if (notAuthed)
  27. errorMessages = t(`${i18nPrefix}.authRequired`)
  28. if (!errorMessages) {
  29. toolInputsSchema.filter((field: any) => {
  30. return field.required
  31. }).forEach((field: any) => {
  32. const targetVar = payload.tool_parameters[field.variable]
  33. if (!targetVar) {
  34. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: field.label })
  35. return
  36. }
  37. const { type: variable_type, value } = targetVar
  38. if (variable_type === VarKindType.variable) {
  39. if (!errorMessages && (!value || value.length === 0))
  40. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: field.label })
  41. }
  42. else {
  43. if (!errorMessages && (value === undefined || value === null || value === ''))
  44. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: field.label })
  45. }
  46. })
  47. }
  48. if (!errorMessages) {
  49. toolSettingSchema.filter((field: any) => {
  50. return field.required
  51. }).forEach((field: any) => {
  52. const value = payload.tool_configurations[field.variable]
  53. if (!errorMessages && (value === undefined || value === null || value === ''))
  54. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: field.label[language] })
  55. if (!errorMessages && typeof value === 'object' && !!value.type && (value.value === undefined || value.value === null || value.value === '' || (Array.isArray(value.value) && value.value.length === 0)))
  56. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: field.label[language] })
  57. })
  58. }
  59. return {
  60. isValid: !errorMessages,
  61. errorMessage: errorMessages,
  62. }
  63. },
  64. }
  65. export default nodeDefault