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

default.ts 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { BlockEnum } from '../../types'
  2. import type { NodeDefault, Var } 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. import { getNotExistVariablesByArray, getNotExistVariablesByText } from '../../utils/workflow'
  7. const i18nPrefix = 'workflow.errorMsg'
  8. const nodeDefault: NodeDefault<ToolNodeType> = {
  9. defaultValue: {
  10. tool_parameters: {},
  11. tool_configurations: {},
  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. })
  56. }
  57. return {
  58. isValid: !errorMessages,
  59. errorMessage: errorMessages,
  60. }
  61. },
  62. checkVarValid(payload: ToolNodeType, varMap: Record<string, Var>, t: any) {
  63. const errorMessageArr = []
  64. const tool_parametersMap = payload.tool_parameters
  65. const tool_parameters_array = Object.values(tool_parametersMap)
  66. const tool_parameters_warnings: string[] = []
  67. tool_parameters_array?.forEach((item) => {
  68. if (!item.value)
  69. return
  70. if (Array.isArray(item.value)) {
  71. const warnings = getNotExistVariablesByArray([item.value], varMap)
  72. if (warnings.length)
  73. tool_parameters_warnings.push(...warnings)
  74. return
  75. }
  76. if (typeof item.value === 'string') {
  77. const warnings = getNotExistVariablesByText(item.value, varMap)
  78. if (warnings.length)
  79. tool_parameters_warnings.push(...warnings)
  80. }
  81. })
  82. if (tool_parameters_warnings.length)
  83. errorMessageArr.push(`${t('workflow.nodes.tool.inputVars')} ${t('workflow.common.referenceVar')}${tool_parameters_warnings.join('、')}${t('workflow.common.noExist')}`)
  84. return {
  85. isValid: true,
  86. warning_vars: tool_parameters_warnings,
  87. errorMessage: errorMessageArr,
  88. }
  89. },
  90. }
  91. export default nodeDefault