Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

default.ts 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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-config'
  7. const nodeDefault: NodeDefault<AgentNodeType> = {
  8. defaultValue: {
  9. tool_node_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. const toolNodeVersion = payload.tool_node_version
  65. const mergeVersion = version || toolNodeVersion
  66. schemas.forEach((schema: any) => {
  67. if (schema?.required) {
  68. if (schema.form === 'form' && !mergeVersion && !userSettings[schema.name]?.value) {
  69. return {
  70. isValid: false,
  71. errorMessage: t('workflow.errorMsg.toolParameterRequired', { field: renderI18nObject(param.label, language), param: renderI18nObject(schema.label, language) }),
  72. }
  73. }
  74. if (schema.form === 'form' && mergeVersion && !userSettings[schema.name]?.value.value) {
  75. return {
  76. isValid: false,
  77. errorMessage: t('workflow.errorMsg.toolParameterRequired', { field: renderI18nObject(param.label, language), param: renderI18nObject(schema.label, language) }),
  78. }
  79. }
  80. if (schema.form === 'llm' && !mergeVersion && reasoningConfig[schema.name].auto === 0 && !reasoningConfig[schema.name]?.value) {
  81. return {
  82. isValid: false,
  83. errorMessage: t('workflow.errorMsg.toolParameterRequired', { field: renderI18nObject(param.label, language), param: renderI18nObject(schema.label, language) }),
  84. }
  85. }
  86. if (schema.form === 'llm' && mergeVersion && reasoningConfig[schema.name].auto === 0 && !reasoningConfig[schema.name]?.value.value) {
  87. return {
  88. isValid: false,
  89. errorMessage: t('workflow.errorMsg.toolParameterRequired', { field: renderI18nObject(param.label, language), param: renderI18nObject(schema.label, language) }),
  90. }
  91. }
  92. }
  93. })
  94. }
  95. }
  96. // multiple tools
  97. if (param.required && param.type === FormTypeEnum.multiToolSelector) {
  98. const tools = payload.agent_parameters?.[param.name]?.value || []
  99. // no value
  100. if (!tools.length) {
  101. return {
  102. isValid: false,
  103. errorMessage: t('workflow.errorMsg.fieldRequired', { field: renderI18nObject(param.label, language) }),
  104. }
  105. }
  106. // not enabled
  107. else if (tools.every((tool: any) => !tool.enabled)) {
  108. return {
  109. isValid: false,
  110. errorMessage: t('workflow.errorMsg.noValidTool', { field: renderI18nObject(param.label, language) }),
  111. }
  112. }
  113. // check form of tools
  114. else {
  115. const validState = {
  116. isValid: true,
  117. errorMessage: '',
  118. }
  119. for (const tool of tools) {
  120. const schemas = tool.schemas || []
  121. const userSettings = tool.settings
  122. const reasoningConfig = tool.parameters
  123. schemas.forEach((schema: any) => {
  124. if (schema?.required) {
  125. if (schema.form === 'form' && !userSettings[schema.name]?.value) {
  126. return {
  127. isValid: false,
  128. errorMessage: t('workflow.errorMsg.toolParameterRequired', { field: renderI18nObject(param.label, language), param: renderI18nObject(schema.label, language) }),
  129. }
  130. }
  131. if (schema.form === 'llm' && reasoningConfig[schema.name]?.auto === 0 && !reasoningConfig[schema.name]?.value) {
  132. return {
  133. isValid: false,
  134. errorMessage: t('workflow.errorMsg.toolParameterRequired', { field: renderI18nObject(param.label, language), param: renderI18nObject(schema.label, language) }),
  135. }
  136. }
  137. }
  138. })
  139. }
  140. return validState
  141. }
  142. }
  143. // common params
  144. if (param.required && !(payload.agent_parameters?.[param.name]?.value || param.default)) {
  145. return {
  146. isValid: false,
  147. errorMessage: t('workflow.errorMsg.fieldRequired', { field: renderI18nObject(param.label, language) }),
  148. }
  149. }
  150. }
  151. return {
  152. isValid: true,
  153. errorMessage: '',
  154. }
  155. },
  156. }
  157. export default nodeDefault