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

use-checklist.ts 11KB

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