use-config.ts 11 KB

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