use-config.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import { useCallback } from 'react'
  2. import produce from 'immer'
  3. import { useBoolean } from 'ahooks'
  4. import { uuid4 } from '@sentry/utils'
  5. import {
  6. useIsChatMode,
  7. useIsNodeInLoop,
  8. useNodesReadOnly,
  9. useWorkflow,
  10. } from '../../hooks'
  11. import { VarType } from '../../types'
  12. import type { ErrorHandleMode, ValueSelector, Var } from '../../types'
  13. import useNodeCrud from '../_base/hooks/use-node-crud'
  14. import { getNodeInfoById, getNodeUsedVarPassToServerKey, getNodeUsedVars, isSystemVar, toNodeOutputVars } from '../_base/components/variable/utils'
  15. import useOneStepRun from '../_base/hooks/use-one-step-run'
  16. import { getOperators } from './utils'
  17. import { LogicalOperator } from './types'
  18. import type { HandleAddCondition, HandleAddSubVariableCondition, HandleRemoveCondition, HandleToggleConditionLogicalOperator, HandleToggleSubVariableConditionLogicalOperator, HandleUpdateCondition, HandleUpdateSubVariableCondition, LoopNodeType } from './types'
  19. import useIsVarFileAttribute from './use-is-var-file-attribute'
  20. const DELIMITER = '@@@@@'
  21. const useConfig = (id: string, payload: LoopNodeType) => {
  22. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  23. const { isNodeInLoop } = useIsNodeInLoop(id)
  24. const isChatMode = useIsChatMode()
  25. const { inputs, setInputs } = useNodeCrud<LoopNodeType>(id, payload)
  26. const filterInputVar = useCallback((varPayload: Var) => {
  27. return [VarType.array, VarType.arrayString, VarType.arrayNumber, VarType.arrayObject, VarType.arrayFile].includes(varPayload.type)
  28. }, [])
  29. // output
  30. const { getLoopNodeChildren, getBeforeNodesInSameBranch } = useWorkflow()
  31. const beforeNodes = getBeforeNodesInSameBranch(id)
  32. const loopChildrenNodes = getLoopNodeChildren(id)
  33. const canChooseVarNodes = [...beforeNodes, ...loopChildrenNodes]
  34. const childrenNodeVars = toNodeOutputVars(loopChildrenNodes, isChatMode)
  35. // single run
  36. const loopInputKey = `${id}.input_selector`
  37. const {
  38. isShowSingleRun,
  39. showSingleRun,
  40. hideSingleRun,
  41. toVarInputs,
  42. runningStatus,
  43. handleRun: doHandleRun,
  44. handleStop,
  45. runInputData,
  46. setRunInputData,
  47. runResult,
  48. loopRunResult,
  49. } = useOneStepRun<LoopNodeType>({
  50. id,
  51. data: inputs,
  52. loopInputKey,
  53. defaultRunInputData: {
  54. [loopInputKey]: [''],
  55. },
  56. })
  57. const [isShowLoopDetail, {
  58. setTrue: doShowLoopDetail,
  59. setFalse: doHideLoopDetail,
  60. }] = useBoolean(false)
  61. const hideLoopDetail = useCallback(() => {
  62. hideSingleRun()
  63. doHideLoopDetail()
  64. }, [doHideLoopDetail, hideSingleRun])
  65. const showLoopDetail = useCallback(() => {
  66. doShowLoopDetail()
  67. }, [doShowLoopDetail])
  68. const backToSingleRun = useCallback(() => {
  69. hideLoopDetail()
  70. showSingleRun()
  71. }, [hideLoopDetail, showSingleRun])
  72. const {
  73. getIsVarFileAttribute,
  74. } = useIsVarFileAttribute({
  75. nodeId: id,
  76. })
  77. const { usedOutVars, allVarObject } = (() => {
  78. const vars: ValueSelector[] = []
  79. const varObjs: Record<string, boolean> = {}
  80. const allVarObject: Record<string, {
  81. inSingleRunPassedKey: string
  82. }> = {}
  83. loopChildrenNodes.forEach((node) => {
  84. const nodeVars = getNodeUsedVars(node).filter(item => item && item.length > 0)
  85. nodeVars.forEach((varSelector) => {
  86. if (varSelector[0] === id) { // skip Loop node itself variable: item, index
  87. return
  88. }
  89. const isInLoop = isNodeInLoop(varSelector[0])
  90. if (isInLoop) // not pass loop inner variable
  91. return
  92. const varSectorStr = varSelector.join('.')
  93. if (!varObjs[varSectorStr]) {
  94. varObjs[varSectorStr] = true
  95. vars.push(varSelector)
  96. }
  97. let passToServerKeys = getNodeUsedVarPassToServerKey(node, varSelector)
  98. if (typeof passToServerKeys === 'string')
  99. passToServerKeys = [passToServerKeys]
  100. passToServerKeys.forEach((key: string, index: number) => {
  101. allVarObject[[varSectorStr, node.id, index].join(DELIMITER)] = {
  102. inSingleRunPassedKey: key,
  103. }
  104. })
  105. })
  106. })
  107. const res = toVarInputs(vars.map((item) => {
  108. const varInfo = getNodeInfoById(canChooseVarNodes, item[0])
  109. return {
  110. label: {
  111. nodeType: varInfo?.data.type,
  112. nodeName: varInfo?.data.title || canChooseVarNodes[0]?.data.title, // default start node title
  113. variable: isSystemVar(item) ? item.join('.') : item[item.length - 1],
  114. },
  115. variable: `${item.join('.')}`,
  116. value_selector: item,
  117. }
  118. }))
  119. return {
  120. usedOutVars: res,
  121. allVarObject,
  122. }
  123. })()
  124. const handleRun = useCallback((data: Record<string, any>) => {
  125. const formattedData: Record<string, any> = {}
  126. Object.keys(allVarObject).forEach((key) => {
  127. const [varSectorStr, nodeId] = key.split(DELIMITER)
  128. formattedData[`${nodeId}.${allVarObject[key].inSingleRunPassedKey}`] = data[varSectorStr]
  129. })
  130. formattedData[loopInputKey] = data[loopInputKey]
  131. doHandleRun(formattedData)
  132. }, [allVarObject, doHandleRun, loopInputKey])
  133. const inputVarValues = (() => {
  134. const vars: Record<string, any> = {}
  135. Object.keys(runInputData)
  136. .filter(key => ![loopInputKey].includes(key))
  137. .forEach((key) => {
  138. vars[key] = runInputData[key]
  139. })
  140. return vars
  141. })()
  142. const setInputVarValues = useCallback((newPayload: Record<string, any>) => {
  143. const newVars = {
  144. ...newPayload,
  145. [loopInputKey]: runInputData[loopInputKey],
  146. }
  147. setRunInputData(newVars)
  148. }, [loopInputKey, runInputData, setRunInputData])
  149. const loop = runInputData[loopInputKey]
  150. const setLoop = useCallback((newLoop: string[]) => {
  151. setRunInputData({
  152. ...runInputData,
  153. [loopInputKey]: newLoop,
  154. })
  155. }, [loopInputKey, runInputData, setRunInputData])
  156. const changeErrorResponseMode = useCallback((item: { value: unknown }) => {
  157. const newInputs = produce(inputs, (draft) => {
  158. draft.error_handle_mode = item.value as ErrorHandleMode
  159. })
  160. setInputs(newInputs)
  161. }, [inputs, setInputs])
  162. const handleAddCondition = useCallback<HandleAddCondition>((valueSelector, varItem) => {
  163. const newInputs = produce(inputs, (draft) => {
  164. if (!draft.break_conditions)
  165. draft.break_conditions = []
  166. draft.break_conditions?.push({
  167. id: uuid4(),
  168. varType: varItem.type,
  169. variable_selector: valueSelector,
  170. comparison_operator: getOperators(varItem.type, getIsVarFileAttribute(valueSelector) ? { key: valueSelector.slice(-1)[0] } : undefined)[0],
  171. value: '',
  172. })
  173. })
  174. setInputs(newInputs)
  175. }, [getIsVarFileAttribute, inputs, setInputs])
  176. const handleRemoveCondition = useCallback<HandleRemoveCondition>((conditionId) => {
  177. const newInputs = produce(inputs, (draft) => {
  178. draft.break_conditions = draft.break_conditions?.filter(item => item.id !== conditionId)
  179. })
  180. setInputs(newInputs)
  181. }, [inputs, setInputs])
  182. const handleUpdateCondition = useCallback<HandleUpdateCondition>((conditionId, newCondition) => {
  183. const newInputs = produce(inputs, (draft) => {
  184. const targetCondition = draft.break_conditions?.find(item => item.id === conditionId)
  185. if (targetCondition)
  186. Object.assign(targetCondition, newCondition)
  187. })
  188. setInputs(newInputs)
  189. }, [inputs, setInputs])
  190. const handleToggleConditionLogicalOperator = useCallback<HandleToggleConditionLogicalOperator>(() => {
  191. const newInputs = produce(inputs, (draft) => {
  192. draft.logical_operator = draft.logical_operator === LogicalOperator.and ? LogicalOperator.or : LogicalOperator.and
  193. })
  194. setInputs(newInputs)
  195. }, [inputs, setInputs])
  196. const handleAddSubVariableCondition = useCallback<HandleAddSubVariableCondition>((conditionId: string, key?: string) => {
  197. const newInputs = produce(inputs, (draft) => {
  198. const condition = draft.break_conditions?.find(item => item.id === conditionId)
  199. if (!condition)
  200. return
  201. if (!condition?.sub_variable_condition) {
  202. condition.sub_variable_condition = {
  203. logical_operator: LogicalOperator.and,
  204. conditions: [],
  205. }
  206. }
  207. const subVarCondition = condition.sub_variable_condition
  208. if (subVarCondition) {
  209. if (!subVarCondition.conditions)
  210. subVarCondition.conditions = []
  211. const svcComparisonOperators = getOperators(VarType.string, { key: key || '' })
  212. subVarCondition.conditions.push({
  213. id: uuid4(),
  214. key: key || '',
  215. varType: VarType.string,
  216. comparison_operator: (svcComparisonOperators && svcComparisonOperators.length) ? svcComparisonOperators[0] : undefined,
  217. value: '',
  218. })
  219. }
  220. })
  221. setInputs(newInputs)
  222. }, [inputs, setInputs])
  223. const handleRemoveSubVariableCondition = useCallback((conditionId: string, subConditionId: string) => {
  224. const newInputs = produce(inputs, (draft) => {
  225. const condition = draft.break_conditions?.find(item => item.id === conditionId)
  226. if (!condition)
  227. return
  228. if (!condition?.sub_variable_condition)
  229. return
  230. const subVarCondition = condition.sub_variable_condition
  231. if (subVarCondition)
  232. subVarCondition.conditions = subVarCondition.conditions.filter(item => item.id !== subConditionId)
  233. })
  234. setInputs(newInputs)
  235. }, [inputs, setInputs])
  236. const handleUpdateSubVariableCondition = useCallback<HandleUpdateSubVariableCondition>((conditionId, subConditionId, newSubCondition) => {
  237. const newInputs = produce(inputs, (draft) => {
  238. const targetCondition = draft.break_conditions?.find(item => item.id === conditionId)
  239. if (targetCondition && targetCondition.sub_variable_condition) {
  240. const targetSubCondition = targetCondition.sub_variable_condition.conditions.find(item => item.id === subConditionId)
  241. if (targetSubCondition)
  242. Object.assign(targetSubCondition, newSubCondition)
  243. }
  244. })
  245. setInputs(newInputs)
  246. }, [inputs, setInputs])
  247. const handleToggleSubVariableConditionLogicalOperator = useCallback<HandleToggleSubVariableConditionLogicalOperator>((conditionId) => {
  248. const newInputs = produce(inputs, (draft) => {
  249. const targetCondition = draft.break_conditions?.find(item => item.id === conditionId)
  250. if (targetCondition && targetCondition.sub_variable_condition)
  251. targetCondition.sub_variable_condition.logical_operator = targetCondition.sub_variable_condition.logical_operator === LogicalOperator.and ? LogicalOperator.or : LogicalOperator.and
  252. })
  253. setInputs(newInputs)
  254. }, [inputs, setInputs])
  255. const handleUpdateLoopCount = useCallback((value: number) => {
  256. const newInputs = produce(inputs, (draft) => {
  257. draft.loop_count = value
  258. })
  259. setInputs(newInputs)
  260. }, [inputs, setInputs])
  261. return {
  262. readOnly,
  263. inputs,
  264. filterInputVar,
  265. childrenNodeVars,
  266. loopChildrenNodes,
  267. isShowSingleRun,
  268. showSingleRun,
  269. hideSingleRun,
  270. isShowLoopDetail,
  271. showLoopDetail,
  272. hideLoopDetail,
  273. backToSingleRun,
  274. runningStatus,
  275. handleRun,
  276. handleStop,
  277. runResult,
  278. inputVarValues,
  279. setInputVarValues,
  280. usedOutVars,
  281. loop,
  282. setLoop,
  283. loopInputKey,
  284. loopRunResult,
  285. handleAddCondition,
  286. handleRemoveCondition,
  287. handleUpdateCondition,
  288. handleToggleConditionLogicalOperator,
  289. handleAddSubVariableCondition,
  290. handleUpdateSubVariableCondition,
  291. handleRemoveSubVariableCondition,
  292. handleToggleSubVariableConditionLogicalOperator,
  293. handleUpdateLoopCount,
  294. changeErrorResponseMode,
  295. }
  296. }
  297. export default useConfig