You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-inspect-vars-crud-common.ts 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import { fetchNodeInspectVars } from '@/service/workflow'
  2. import { useWorkflowStore } from '@/app/components/workflow/store'
  3. import type { ValueSelector } from '@/app/components/workflow/types'
  4. import type { VarInInspect } from '@/types/workflow'
  5. import { VarInInspectType } from '@/types/workflow'
  6. import { useCallback } from 'react'
  7. import { isConversationVar, isENV, isSystemVar } from '@/app/components/workflow/nodes/_base/components/variable/utils'
  8. import produce from 'immer'
  9. import type { Node } from '@/app/components/workflow/types'
  10. import { useNodesInteractionsWithoutSync } from '@/app/components/workflow/hooks/use-nodes-interactions-without-sync'
  11. import { useEdgesInteractionsWithoutSync } from '@/app/components/workflow/hooks/use-edges-interactions-without-sync'
  12. import type { FlowType } from '@/types/common'
  13. import useFLow from '@/service/use-flow'
  14. type Params = {
  15. flowId: string
  16. flowType: FlowType
  17. }
  18. export const useInspectVarsCrudCommon = ({
  19. flowId,
  20. flowType,
  21. }: Params) => {
  22. const workflowStore = useWorkflowStore()
  23. const {
  24. useInvalidateConversationVarValues,
  25. useInvalidateSysVarValues,
  26. useResetConversationVar,
  27. useResetToLastRunValue,
  28. useDeleteAllInspectorVars,
  29. useDeleteNodeInspectorVars,
  30. useDeleteInspectVar,
  31. useEditInspectorVar,
  32. } = useFLow({ flowType })
  33. const invalidateConversationVarValues = useInvalidateConversationVarValues(flowId)
  34. const { mutateAsync: doResetConversationVar } = useResetConversationVar(flowId)
  35. const { mutateAsync: doResetToLastRunValue } = useResetToLastRunValue(flowId)
  36. const invalidateSysVarValues = useInvalidateSysVarValues(flowId)
  37. const { mutateAsync: doDeleteAllInspectorVars } = useDeleteAllInspectorVars(flowId)
  38. const { mutate: doDeleteNodeInspectorVars } = useDeleteNodeInspectorVars(flowId)
  39. const { mutate: doDeleteInspectVar } = useDeleteInspectVar(flowId)
  40. const { mutateAsync: doEditInspectorVar } = useEditInspectorVar(flowId)
  41. const { handleCancelNodeSuccessStatus } = useNodesInteractionsWithoutSync()
  42. const { handleEdgeCancelRunningStatus } = useEdgesInteractionsWithoutSync()
  43. const getNodeInspectVars = useCallback((nodeId: string) => {
  44. const { nodesWithInspectVars } = workflowStore.getState()
  45. const node = nodesWithInspectVars.find(node => node.nodeId === nodeId)
  46. return node
  47. }, [workflowStore])
  48. const getVarId = useCallback((nodeId: string, varName: string) => {
  49. const node = getNodeInspectVars(nodeId)
  50. if (!node)
  51. return undefined
  52. const varId = node.vars.find((varItem) => {
  53. return varItem.selector[1] === varName
  54. })?.id
  55. return varId
  56. }, [getNodeInspectVars])
  57. const getInspectVar = useCallback((nodeId: string, name: string): VarInInspect | undefined => {
  58. const node = getNodeInspectVars(nodeId)
  59. if (!node)
  60. return undefined
  61. const variable = node.vars.find((varItem) => {
  62. return varItem.name === name
  63. })
  64. return variable
  65. }, [getNodeInspectVars])
  66. const hasSetInspectVar = useCallback((nodeId: string, name: string, sysVars: VarInInspect[], conversationVars: VarInInspect[]) => {
  67. const isEnv = isENV([nodeId])
  68. if (isEnv) // always have value
  69. return true
  70. const isSys = isSystemVar([nodeId])
  71. if (isSys)
  72. return sysVars.some(varItem => varItem.selector?.[1]?.replace('sys.', '') === name)
  73. const isChatVar = isConversationVar([nodeId])
  74. if (isChatVar)
  75. return conversationVars.some(varItem => varItem.selector?.[1] === name)
  76. return getInspectVar(nodeId, name) !== undefined
  77. }, [getInspectVar])
  78. const hasNodeInspectVars = useCallback((nodeId: string) => {
  79. return !!getNodeInspectVars(nodeId)
  80. }, [getNodeInspectVars])
  81. const fetchInspectVarValue = useCallback(async (selector: ValueSelector) => {
  82. const {
  83. setNodeInspectVars,
  84. } = workflowStore.getState()
  85. const nodeId = selector[0]
  86. const isSystemVar = nodeId === 'sys'
  87. const isConversationVar = nodeId === 'conversation'
  88. if (isSystemVar) {
  89. invalidateSysVarValues()
  90. return
  91. }
  92. if (isConversationVar) {
  93. invalidateConversationVarValues()
  94. return
  95. }
  96. const vars = await fetchNodeInspectVars(flowType, flowId, nodeId)
  97. setNodeInspectVars(nodeId, vars)
  98. }, [workflowStore, flowType, flowId, invalidateSysVarValues, invalidateConversationVarValues])
  99. // after last run would call this
  100. const appendNodeInspectVars = useCallback((nodeId: string, payload: VarInInspect[], allNodes: Node[]) => {
  101. const {
  102. nodesWithInspectVars,
  103. setNodesWithInspectVars,
  104. } = workflowStore.getState()
  105. const nodes = produce(nodesWithInspectVars, (draft) => {
  106. const nodeInfo = allNodes.find(node => node.id === nodeId)
  107. if (nodeInfo) {
  108. const index = draft.findIndex(node => node.nodeId === nodeId)
  109. if (index === -1) {
  110. draft.unshift({
  111. nodeId,
  112. nodeType: nodeInfo.data.type,
  113. title: nodeInfo.data.title,
  114. vars: payload,
  115. nodePayload: nodeInfo.data,
  116. })
  117. }
  118. else {
  119. draft[index].vars = payload
  120. // put the node to the topAdd commentMore actions
  121. draft.unshift(draft.splice(index, 1)[0])
  122. }
  123. }
  124. })
  125. setNodesWithInspectVars(nodes)
  126. handleCancelNodeSuccessStatus(nodeId)
  127. }, [workflowStore, handleCancelNodeSuccessStatus])
  128. const hasNodeInspectVar = useCallback((nodeId: string, varId: string) => {
  129. const { nodesWithInspectVars } = workflowStore.getState()
  130. const targetNode = nodesWithInspectVars.find(item => item.nodeId === nodeId)
  131. if(!targetNode || !targetNode.vars)
  132. return false
  133. return targetNode.vars.some(item => item.id === varId)
  134. }, [workflowStore])
  135. const deleteInspectVar = useCallback(async (nodeId: string, varId: string) => {
  136. const { deleteInspectVar } = workflowStore.getState()
  137. if(hasNodeInspectVar(nodeId, varId)) {
  138. await doDeleteInspectVar(varId)
  139. deleteInspectVar(nodeId, varId)
  140. }
  141. }, [doDeleteInspectVar, workflowStore, hasNodeInspectVar])
  142. const resetConversationVar = useCallback(async (varId: string) => {
  143. await doResetConversationVar(varId)
  144. invalidateConversationVarValues()
  145. }, [doResetConversationVar, invalidateConversationVarValues])
  146. const deleteNodeInspectorVars = useCallback(async (nodeId: string) => {
  147. const { deleteNodeInspectVars } = workflowStore.getState()
  148. if (hasNodeInspectVars(nodeId)) {
  149. await doDeleteNodeInspectorVars(nodeId)
  150. deleteNodeInspectVars(nodeId)
  151. }
  152. }, [doDeleteNodeInspectorVars, workflowStore, hasNodeInspectVars])
  153. const deleteAllInspectorVars = useCallback(async () => {
  154. const { deleteAllInspectVars } = workflowStore.getState()
  155. await doDeleteAllInspectorVars()
  156. await invalidateConversationVarValues()
  157. await invalidateSysVarValues()
  158. deleteAllInspectVars()
  159. handleEdgeCancelRunningStatus()
  160. }, [doDeleteAllInspectorVars, invalidateConversationVarValues, invalidateSysVarValues, workflowStore, handleEdgeCancelRunningStatus])
  161. const editInspectVarValue = useCallback(async (nodeId: string, varId: string, value: any) => {
  162. const { setInspectVarValue } = workflowStore.getState()
  163. await doEditInspectorVar({
  164. varId,
  165. value,
  166. })
  167. setInspectVarValue(nodeId, varId, value)
  168. if (nodeId === VarInInspectType.conversation)
  169. invalidateConversationVarValues()
  170. if (nodeId === VarInInspectType.system)
  171. invalidateSysVarValues()
  172. }, [doEditInspectorVar, invalidateConversationVarValues, invalidateSysVarValues, workflowStore])
  173. const renameInspectVarName = useCallback(async (nodeId: string, oldName: string, newName: string) => {
  174. const { renameInspectVarName } = workflowStore.getState()
  175. const varId = getVarId(nodeId, oldName)
  176. if (!varId)
  177. return
  178. const newSelector = [nodeId, newName]
  179. await doEditInspectorVar({
  180. varId,
  181. name: newName,
  182. })
  183. renameInspectVarName(nodeId, varId, newSelector)
  184. }, [doEditInspectorVar, getVarId, workflowStore])
  185. const isInspectVarEdited = useCallback((nodeId: string, name: string) => {
  186. const inspectVar = getInspectVar(nodeId, name)
  187. if (!inspectVar)
  188. return false
  189. return inspectVar.edited
  190. }, [getInspectVar])
  191. const resetToLastRunVar = useCallback(async (nodeId: string, varId: string) => {
  192. const { resetToLastRunVar } = workflowStore.getState()
  193. const isSysVar = nodeId === 'sys'
  194. const data = await doResetToLastRunValue(varId)
  195. if(isSysVar)
  196. invalidateSysVarValues()
  197. else
  198. resetToLastRunVar(nodeId, varId, data.value)
  199. }, [doResetToLastRunValue, invalidateSysVarValues, workflowStore])
  200. return {
  201. hasNodeInspectVars,
  202. hasSetInspectVar,
  203. fetchInspectVarValue,
  204. editInspectVarValue,
  205. renameInspectVarName,
  206. appendNodeInspectVars,
  207. deleteInspectVar,
  208. deleteNodeInspectorVars,
  209. deleteAllInspectorVars,
  210. isInspectVarEdited,
  211. resetToLastRunVar,
  212. invalidateSysVarValues,
  213. resetConversationVar,
  214. invalidateConversationVarValues,
  215. }
  216. }