Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

use-config.ts 13KB

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