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

use-checklist.ts 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import {
  2. useCallback,
  3. useMemo,
  4. useRef,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import { useStoreApi } from 'reactflow'
  8. import type {
  9. CommonNodeType,
  10. Edge,
  11. Node,
  12. } from '../types'
  13. import { BlockEnum } from '../types'
  14. import { useStore } from '../store'
  15. import {
  16. getToolCheckParams,
  17. getValidTreeNodes,
  18. transformStartNodeVariables,
  19. } from '../utils'
  20. import {
  21. CUSTOM_NODE,
  22. MAX_TREE_DEPTH,
  23. } from '../constants'
  24. import type { ToolNodeType } from '../nodes/tool/types'
  25. import { useIsChatMode } from './use-workflow'
  26. import { useNodesExtraData } from './use-nodes-data'
  27. import { useToastContext } from '@/app/components/base/toast'
  28. import { CollectionType } from '@/app/components/tools/types'
  29. import { useGetLanguage } from '@/context/i18n'
  30. import type { AgentNodeType } from '../nodes/agent/types'
  31. import { useStrategyProviders } from '@/service/use-strategy'
  32. import { canFindTool } from '@/utils'
  33. import { useDatasetsDetailStore } from '../datasets-detail-store/store'
  34. import type { KnowledgeRetrievalNodeType } from '../nodes/knowledge-retrieval/types'
  35. import type { DataSet } from '@/models/datasets'
  36. import { fetchDatasets } from '@/service/datasets'
  37. export const useChecklist = (nodes: Node[], edges: Edge[]) => {
  38. const { t } = useTranslation()
  39. const language = useGetLanguage()
  40. const nodesExtraData = useNodesExtraData()
  41. const isChatMode = useIsChatMode()
  42. const buildInTools = useStore(s => s.buildInTools)
  43. const customTools = useStore(s => s.customTools)
  44. const workflowTools = useStore(s => s.workflowTools)
  45. const { data: strategyProviders } = useStrategyProviders()
  46. const datasetsDetail = useDatasetsDetailStore(s => s.datasetsDetail)
  47. const chatVarList = useStore(s => s.conversationVariables)
  48. const environmentVariables = useStore(s => s.environmentVariables)
  49. const getCheckData = useCallback((data: CommonNodeType<{}>) => {
  50. let checkData = data
  51. if (data.type === BlockEnum.KnowledgeRetrieval) {
  52. const datasetIds = (data as CommonNodeType<KnowledgeRetrievalNodeType>).dataset_ids
  53. const _datasets = datasetIds.reduce<DataSet[]>((acc, id) => {
  54. if (datasetsDetail[id])
  55. acc.push(datasetsDetail[id])
  56. return acc
  57. }, [])
  58. checkData = {
  59. ...data,
  60. _datasets,
  61. } as CommonNodeType<KnowledgeRetrievalNodeType>
  62. }
  63. return checkData
  64. }, [datasetsDetail])
  65. const needWarningNodes = useMemo(() => {
  66. const list = []
  67. const { validNodes } = getValidTreeNodes(nodes.filter(node => node.type === CUSTOM_NODE), edges, true)
  68. const allVariablesMap = transformStartNodeVariables(chatVarList, environmentVariables)
  69. const errMessageMap = new Map()
  70. for (let i = 0; i < nodes.length; i++) {
  71. const node = nodes[i]
  72. let toolIcon
  73. let moreDataForCheckValid
  74. if (node.data.type === BlockEnum.Tool) {
  75. const { provider_type } = node.data
  76. moreDataForCheckValid = getToolCheckParams(node.data as ToolNodeType, buildInTools, customTools, workflowTools, language)
  77. if (provider_type === CollectionType.builtIn)
  78. toolIcon = buildInTools.find(tool => canFindTool(tool.id, node.data.provider_id || ''))?.icon
  79. if (provider_type === CollectionType.custom)
  80. toolIcon = customTools.find(tool => tool.id === node.data.provider_id)?.icon
  81. if (provider_type === CollectionType.workflow)
  82. toolIcon = workflowTools.find(tool => tool.id === node.data.provider_id)?.icon
  83. }
  84. if (node.data.type === BlockEnum.Agent) {
  85. const data = node.data as AgentNodeType
  86. const isReadyForCheckValid = !!strategyProviders
  87. const provider = strategyProviders?.find(provider => provider.declaration.identity.name === data.agent_strategy_provider_name)
  88. const strategy = provider?.declaration.strategies?.find(s => s.identity.name === data.agent_strategy_name)
  89. moreDataForCheckValid = {
  90. provider,
  91. strategy,
  92. language,
  93. isReadyForCheckValid,
  94. }
  95. }
  96. if (node.type === CUSTOM_NODE) {
  97. const checkData = getCheckData(node.data)
  98. const { errorMessage } = nodesExtraData[node.data.type].checkValid(checkData, t, moreDataForCheckValid)
  99. if (errorMessage || !validNodes.find(n => n.id === node.id)) {
  100. list.push({
  101. id: node.id,
  102. type: node.data.type,
  103. title: node.data.title,
  104. toolIcon,
  105. unConnected: !validNodes.find(n => n.id === node.id),
  106. errorMessage,
  107. varErrorMessage: [],
  108. })
  109. }
  110. errMessageMap.set(node.id, list[list.length - 1])
  111. if (nodesExtraData[node.data.type as BlockEnum].checkVarValid) {
  112. const { errorMessage: varErrorMessages } = nodesExtraData[node.data.type as BlockEnum].checkVarValid(node.data, { ...allVariablesMap, ...node._parentOutputVarMap }, t)
  113. if (varErrorMessages?.length) {
  114. const errMessage = errMessageMap.get(node.id)
  115. if (errMessage) {
  116. errMessage.varErrorMessage = varErrorMessages
  117. }
  118. else {
  119. list.push({
  120. id: node.id,
  121. type: node.data.type,
  122. title: node.data.title,
  123. toolIcon,
  124. unConnected: !validNodes.find(n => n.id === node.id),
  125. errorMessage: '',
  126. varErrorMessage: varErrorMessages,
  127. })
  128. errMessageMap.set(node.id, list[list.length - 1])
  129. }
  130. }
  131. }
  132. }
  133. }
  134. if (isChatMode && !nodes.find(node => node.data.type === BlockEnum.Answer)) {
  135. list.push({
  136. id: 'answer-need-added',
  137. type: BlockEnum.Answer,
  138. title: t('workflow.blocks.answer'),
  139. errorMessage: t('workflow.common.needAnswerNode'),
  140. })
  141. }
  142. if (!isChatMode && !nodes.find(node => node.data.type === BlockEnum.End)) {
  143. list.push({
  144. id: 'end-need-added',
  145. type: BlockEnum.End,
  146. title: t('workflow.blocks.end'),
  147. errorMessage: t('workflow.common.needEndNode'),
  148. })
  149. }
  150. for (let i = 0; i < validNodes.length; i++) {
  151. const node = validNodes[i]
  152. delete node._parentOutputVarMap
  153. }
  154. return list
  155. }, [nodes, edges, isChatMode, buildInTools, customTools, workflowTools, language, nodesExtraData, t, strategyProviders, getCheckData, chatVarList, environmentVariables])
  156. return needWarningNodes
  157. }
  158. export const useChecklistBeforePublish = () => {
  159. const { t } = useTranslation()
  160. const language = useGetLanguage()
  161. const buildInTools = useStore(s => s.buildInTools)
  162. const customTools = useStore(s => s.customTools)
  163. const workflowTools = useStore(s => s.workflowTools)
  164. const { notify } = useToastContext()
  165. const isChatMode = useIsChatMode()
  166. const store = useStoreApi()
  167. const nodesExtraData = useNodesExtraData()
  168. const { data: strategyProviders } = useStrategyProviders()
  169. const updateDatasetsDetail = useDatasetsDetailStore(s => s.updateDatasetsDetail)
  170. const updateTime = useRef(0)
  171. const chatVarList = useStore(s => s.conversationVariables)
  172. const environmentVariables = useStore(s => s.environmentVariables)
  173. const getCheckData = useCallback((data: CommonNodeType<{}>, datasets: DataSet[]) => {
  174. let checkData = data
  175. if (data.type === BlockEnum.KnowledgeRetrieval) {
  176. const datasetIds = (data as CommonNodeType<KnowledgeRetrievalNodeType>).dataset_ids
  177. const datasetsDetail = datasets.reduce<Record<string, DataSet>>((acc, dataset) => {
  178. acc[dataset.id] = dataset
  179. return acc
  180. }, {})
  181. const _datasets = datasetIds.reduce<DataSet[]>((acc, id) => {
  182. if (datasetsDetail[id])
  183. acc.push(datasetsDetail[id])
  184. return acc
  185. }, [])
  186. checkData = {
  187. ...data,
  188. _datasets,
  189. } as CommonNodeType<KnowledgeRetrievalNodeType>
  190. }
  191. return checkData
  192. }, [])
  193. const handleCheckBeforePublish = useCallback(async () => {
  194. const {
  195. getNodes,
  196. edges,
  197. } = store.getState()
  198. const nodes = getNodes().filter(node => node.type === CUSTOM_NODE)
  199. const {
  200. validNodes,
  201. maxDepth,
  202. } = getValidTreeNodes(nodes.filter(node => node.type === CUSTOM_NODE), edges, true)
  203. if (maxDepth > MAX_TREE_DEPTH) {
  204. notify({ type: 'error', message: t('workflow.common.maxTreeDepth', { depth: MAX_TREE_DEPTH }) })
  205. return false
  206. }
  207. const allVariablesMap = transformStartNodeVariables(chatVarList, environmentVariables)
  208. // Before publish, we need to fetch datasets detail, in case of the settings of datasets have been changed
  209. const knowledgeRetrievalNodes = nodes.filter(node => node.data.type === BlockEnum.KnowledgeRetrieval)
  210. const allDatasetIds = knowledgeRetrievalNodes.reduce<string[]>((acc, node) => {
  211. return Array.from(new Set([...acc, ...(node.data as CommonNodeType<KnowledgeRetrievalNodeType>).dataset_ids]))
  212. }, [])
  213. let datasets: DataSet[] = []
  214. if (allDatasetIds.length > 0) {
  215. updateTime.current = updateTime.current + 1
  216. const currUpdateTime = updateTime.current
  217. const { data: datasetsDetail } = await fetchDatasets({ url: '/datasets', params: { page: 1, ids: allDatasetIds } })
  218. if (datasetsDetail && datasetsDetail.length > 0) {
  219. // avoid old data to overwrite the new data
  220. if (currUpdateTime < updateTime.current)
  221. return false
  222. datasets = datasetsDetail
  223. updateDatasetsDetail(datasetsDetail)
  224. }
  225. }
  226. for (let i = 0; i < nodes.length; i++) {
  227. const node = nodes[i]
  228. let moreDataForCheckValid
  229. if (node.data.type === BlockEnum.Tool)
  230. moreDataForCheckValid = getToolCheckParams(node.data as ToolNodeType, buildInTools, customTools, workflowTools, language)
  231. if (node.data.type === BlockEnum.Agent) {
  232. const data = node.data as AgentNodeType
  233. const isReadyForCheckValid = !!strategyProviders
  234. const provider = strategyProviders?.find(provider => provider.declaration.identity.name === data.agent_strategy_provider_name)
  235. const strategy = provider?.declaration.strategies?.find(s => s.identity.name === data.agent_strategy_name)
  236. moreDataForCheckValid = {
  237. provider,
  238. strategy,
  239. language,
  240. isReadyForCheckValid,
  241. }
  242. }
  243. const checkData = getCheckData(node.data, datasets)
  244. const { errorMessage } = nodesExtraData[node.data.type as BlockEnum].checkValid(checkData, t, moreDataForCheckValid)
  245. if (errorMessage) {
  246. notify({ type: 'error', message: `[${node.data.title}] ${errorMessage}` })
  247. return false
  248. }
  249. if (!validNodes.find(n => n.id === node.id)) {
  250. notify({ type: 'error', message: `[${node.data.title}] ${t('workflow.common.needConnectTip')}` })
  251. return false
  252. }
  253. if (nodesExtraData[node.data.type as BlockEnum].checkVarValid) {
  254. const { errorMessage: varErrorMessage } = nodesExtraData[node.data.type as BlockEnum].checkVarValid(node.data, { ...allVariablesMap, ...node._parentOutputVarMap }, t)
  255. if (varErrorMessage?.length) {
  256. notify({ type: 'error', message: `[${node.data.title}] ${varErrorMessage[0]}` })
  257. return false
  258. }
  259. }
  260. }
  261. if (isChatMode && !nodes.find(node => node.data.type === BlockEnum.Answer)) {
  262. notify({ type: 'error', message: t('workflow.common.needAnswerNode') })
  263. return false
  264. }
  265. if (!isChatMode && !nodes.find(node => node.data.type === BlockEnum.End)) {
  266. notify({ type: 'error', message: t('workflow.common.needEndNode') })
  267. return false
  268. }
  269. return true
  270. }, [store, isChatMode, notify, t, buildInTools, customTools, workflowTools, language, nodesExtraData, strategyProviders, updateDatasetsDetail, getCheckData, chatVarList, environmentVariables])
  271. return {
  272. handleCheckBeforePublish,
  273. }
  274. }