default.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import type { StrategyDetail, StrategyPluginDetail } from '@/app/components/plugins/types'
  2. import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/blocks'
  3. import type { NodeDefault } from '../../types'
  4. import type { AgentNodeType } from './types'
  5. import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  6. import { renderI18nObject } from '@/hooks/use-i18n'
  7. const nodeDefault: NodeDefault<AgentNodeType> = {
  8. defaultValue: {
  9. },
  10. getAvailablePrevNodes(isChatMode) {
  11. return isChatMode
  12. ? ALL_CHAT_AVAILABLE_BLOCKS
  13. : ALL_COMPLETION_AVAILABLE_BLOCKS
  14. },
  15. getAvailableNextNodes(isChatMode) {
  16. return isChatMode
  17. ? ALL_CHAT_AVAILABLE_BLOCKS
  18. : ALL_COMPLETION_AVAILABLE_BLOCKS
  19. },
  20. checkValid(payload, t, moreDataForCheckValid: {
  21. strategyProvider?: StrategyPluginDetail,
  22. strategy?: StrategyDetail
  23. language: string
  24. isReadyForCheckValid: boolean
  25. }) {
  26. const { strategy, language, isReadyForCheckValid } = moreDataForCheckValid
  27. if (!isReadyForCheckValid) {
  28. return {
  29. isValid: true,
  30. errorMessage: '',
  31. }
  32. }
  33. if (!strategy) {
  34. return {
  35. isValid: false,
  36. errorMessage: t('workflow.nodes.agent.checkList.strategyNotSelected'),
  37. }
  38. }
  39. for (const param of strategy.parameters) {
  40. // single tool
  41. if (param.required && param.type === FormTypeEnum.toolSelector) {
  42. // no value
  43. const toolValue = payload.agent_parameters?.[param.name]?.value
  44. if (!toolValue) {
  45. return {
  46. isValid: false,
  47. errorMessage: t('workflow.errorMsg.fieldRequired', { field: renderI18nObject(param.label, language) }),
  48. }
  49. }
  50. // not enabled
  51. else if (!toolValue.enabled) {
  52. return {
  53. isValid: false,
  54. errorMessage: t('workflow.errorMsg.noValidTool', { field: renderI18nObject(param.label, language) }),
  55. }
  56. }
  57. // check form of tool
  58. else {
  59. const schemas = toolValue.schemas || []
  60. const userSettings = toolValue.settings
  61. const reasoningConfig = toolValue.parameters
  62. schemas.forEach((schema: any) => {
  63. if (schema?.required) {
  64. if (schema.form === 'form' && !userSettings[schema.name]?.value) {
  65. return {
  66. isValid: false,
  67. errorMessage: t('workflow.errorMsg.toolParameterRequired', { field: renderI18nObject(param.label, language), param: renderI18nObject(schema.label, language) }),
  68. }
  69. }
  70. if (schema.form === 'llm' && reasoningConfig[schema.name].auto === 0 && !userSettings[schema.name]?.value) {
  71. return {
  72. isValid: false,
  73. errorMessage: t('workflow.errorMsg.toolParameterRequired', { field: renderI18nObject(param.label, language), param: renderI18nObject(schema.label, language) }),
  74. }
  75. }
  76. }
  77. })
  78. }
  79. }
  80. // multiple tools
  81. if (param.required && param.type === FormTypeEnum.multiToolSelector) {
  82. const tools = payload.agent_parameters?.[param.name]?.value || []
  83. // no value
  84. if (!tools.length) {
  85. return {
  86. isValid: false,
  87. errorMessage: t('workflow.errorMsg.fieldRequired', { field: renderI18nObject(param.label, language) }),
  88. }
  89. }
  90. // not enabled
  91. else if (tools.every((tool: any) => !tool.enabled)) {
  92. return {
  93. isValid: false,
  94. errorMessage: t('workflow.errorMsg.noValidTool', { field: renderI18nObject(param.label, language) }),
  95. }
  96. }
  97. // check form of tools
  98. else {
  99. let validState = {
  100. isValid: true,
  101. errorMessage: '',
  102. }
  103. for (const tool of tools) {
  104. const schemas = tool.schemas || []
  105. const userSettings = tool.settings
  106. const reasoningConfig = tool.parameters
  107. schemas.forEach((schema: any) => {
  108. if (schema?.required) {
  109. if (schema.form === 'form' && !userSettings[schema.name]?.value) {
  110. return validState = {
  111. isValid: false,
  112. errorMessage: t('workflow.errorMsg.toolParameterRequired', { field: renderI18nObject(param.label, language), param: renderI18nObject(schema.label, language) }),
  113. }
  114. }
  115. if (schema.form === 'llm' && reasoningConfig[schema.name]?.auto === 0 && !reasoningConfig[schema.name]?.value) {
  116. return validState = {
  117. isValid: false,
  118. errorMessage: t('workflow.errorMsg.toolParameterRequired', { field: renderI18nObject(param.label, language), param: renderI18nObject(schema.label, language) }),
  119. }
  120. }
  121. }
  122. })
  123. }
  124. return validState
  125. }
  126. }
  127. // common params
  128. if (param.required && !payload.agent_parameters?.[param.name]?.value) {
  129. return {
  130. isValid: false,
  131. errorMessage: t('workflow.errorMsg.fieldRequired', { field: renderI18nObject(param.label, language) }),
  132. }
  133. }
  134. }
  135. return {
  136. isValid: true,
  137. errorMessage: '',
  138. }
  139. },
  140. }
  141. export default nodeDefault