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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { genNodeMetaData } from '@/app/components/workflow/utils'
  2. import { BlockEnum, VarType } from '@/app/components/workflow/types'
  3. import type { NodeDefault, ToolWithProvider } from '../../types'
  4. import type { ToolNodeType } from './types'
  5. import { VarType as VarKindType } from '@/app/components/workflow/nodes/tool/types'
  6. import { TOOL_OUTPUT_STRUCT } from '../../constants'
  7. import { CollectionType } from '@/app/components/tools/types'
  8. import { canFindTool } from '@/utils'
  9. import { getMatchedSchemaType } from '../_base/components/variable/use-match-schema-type'
  10. const i18nPrefix = 'workflow.errorMsg'
  11. const metaData = genNodeMetaData({
  12. sort: -1,
  13. type: BlockEnum.Tool,
  14. helpLinkUri: 'tools',
  15. })
  16. const nodeDefault: NodeDefault<ToolNodeType> = {
  17. metaData,
  18. defaultValue: {
  19. tool_parameters: {},
  20. tool_configurations: {},
  21. tool_node_version: '2',
  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. getOutputVars(payload: ToolNodeType, allPluginInfoList: Record<string, ToolWithProvider[]>, _ragVars: any, { schemaTypeDefinitions } = { schemaTypeDefinitions: [] }) {
  65. const { provider_id, provider_type } = payload
  66. let currentTools: ToolWithProvider[] = []
  67. switch (provider_type) {
  68. case CollectionType.builtIn:
  69. currentTools = allPluginInfoList.buildInTools ?? []
  70. break
  71. case CollectionType.custom:
  72. currentTools = allPluginInfoList.customTools ?? []
  73. break
  74. case CollectionType.workflow:
  75. currentTools = allPluginInfoList.workflowTools ?? []
  76. break
  77. case CollectionType.mcp:
  78. currentTools = allPluginInfoList.mcpTools ?? []
  79. break
  80. default:
  81. currentTools = []
  82. }
  83. const currCollection = currentTools.find(item => canFindTool(item.id, provider_id))
  84. const currTool = currCollection?.tools.find(tool => tool.name === payload.tool_name)
  85. const output_schema = currTool?.output_schema
  86. let res: any[] = []
  87. if (!output_schema) {
  88. res = TOOL_OUTPUT_STRUCT
  89. }
  90. else {
  91. const outputSchema: any[] = []
  92. Object.keys(output_schema.properties).forEach((outputKey) => {
  93. const output = output_schema.properties[outputKey]
  94. const dataType = output.type
  95. const schemaType = getMatchedSchemaType(output, schemaTypeDefinitions)
  96. let type = dataType === 'array'
  97. ? `array[${output.items?.type.slice(0, 1).toLocaleLowerCase()}${output.items?.type.slice(1)}]`
  98. : `${output.type.slice(0, 1).toLocaleLowerCase()}${output.type.slice(1)}`
  99. if (type === VarType.object && schemaType === 'file')
  100. type = VarType.file
  101. outputSchema.push({
  102. variable: outputKey,
  103. type,
  104. description: output.description,
  105. schemaType,
  106. children: output.type === 'object' ? {
  107. schema: {
  108. type: 'object',
  109. properties: output.properties,
  110. },
  111. } : undefined,
  112. })
  113. })
  114. res = [
  115. ...TOOL_OUTPUT_STRUCT,
  116. ...outputSchema,
  117. ]
  118. }
  119. return res
  120. },
  121. }
  122. export default nodeDefault