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 6.2KB

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