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-fetch-workflow-inspect-vars.ts 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { useCallback } from 'react'
  2. import type { NodeWithVar, VarInInspect } from '@/types/workflow'
  3. import { useWorkflowStore } from '@/app/components/workflow/store'
  4. import { useStoreApi } from 'reactflow'
  5. import type { Node } from '@/app/components/workflow/types'
  6. import { fetchAllInspectVars } from '@/service/workflow'
  7. import { useInvalidateConversationVarValues, useInvalidateSysVarValues } from '@/service/use-workflow'
  8. import { useNodesInteractionsWithoutSync } from '@/app/components/workflow/hooks/use-nodes-interactions-without-sync'
  9. import type { FlowType } from '@/types/common'
  10. type Params = {
  11. flowType: FlowType
  12. flowId: string
  13. }
  14. export const useSetWorkflowVarsWithValue = ({
  15. flowType,
  16. flowId,
  17. }: Params) => {
  18. const workflowStore = useWorkflowStore()
  19. const store = useStoreApi()
  20. const invalidateConversationVarValues = useInvalidateConversationVarValues(flowType, flowId)
  21. const invalidateSysVarValues = useInvalidateSysVarValues(flowType, flowId)
  22. const { handleCancelAllNodeSuccessStatus } = useNodesInteractionsWithoutSync()
  23. const setInspectVarsToStore = useCallback((inspectVars: VarInInspect[]) => {
  24. const { setNodesWithInspectVars } = workflowStore.getState()
  25. const { getNodes } = store.getState()
  26. const nodeArr = getNodes()
  27. const nodesKeyValue: Record<string, Node> = {}
  28. nodeArr.forEach((node) => {
  29. nodesKeyValue[node.id] = node
  30. })
  31. const withValueNodeIds: Record<string, boolean> = {}
  32. inspectVars.forEach((varItem) => {
  33. const nodeId = varItem.selector[0]
  34. const node = nodesKeyValue[nodeId]
  35. if (!node)
  36. return
  37. withValueNodeIds[nodeId] = true
  38. })
  39. const withValueNodes = Object.keys(withValueNodeIds).map((nodeId) => {
  40. return nodesKeyValue[nodeId]
  41. })
  42. const res: NodeWithVar[] = withValueNodes.map((node) => {
  43. const nodeId = node.id
  44. const varsUnderTheNode = inspectVars.filter((varItem) => {
  45. return varItem.selector[0] === nodeId
  46. })
  47. const nodeWithVar = {
  48. nodeId,
  49. nodePayload: node.data,
  50. nodeType: node.data.type,
  51. title: node.data.title,
  52. vars: varsUnderTheNode,
  53. isSingRunRunning: false,
  54. isValueFetched: false,
  55. }
  56. return nodeWithVar
  57. })
  58. setNodesWithInspectVars(res)
  59. }, [workflowStore, store])
  60. const fetchInspectVars = useCallback(async () => {
  61. invalidateConversationVarValues()
  62. invalidateSysVarValues()
  63. const data = await fetchAllInspectVars(flowType, flowId)
  64. setInspectVarsToStore(data)
  65. handleCancelAllNodeSuccessStatus() // to make sure clear node output show the unset status
  66. }, [invalidateConversationVarValues, invalidateSysVarValues, flowType, flowId, setInspectVarsToStore, handleCancelAllNodeSuccessStatus])
  67. return {
  68. fetchInspectVars,
  69. }
  70. }