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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. type Params = {
  10. flowId: string
  11. conversationVarsUrl: string
  12. systemVarsUrl: string
  13. }
  14. export const useSetWorkflowVarsWithValue = ({
  15. flowId,
  16. conversationVarsUrl,
  17. systemVarsUrl,
  18. }: Params) => {
  19. const workflowStore = useWorkflowStore()
  20. const store = useStoreApi()
  21. const invalidateConversationVarValues = useInvalidateConversationVarValues(conversationVarsUrl)
  22. const invalidateSysVarValues = useInvalidateSysVarValues(systemVarsUrl)
  23. const { handleCancelAllNodeSuccessStatus } = useNodesInteractionsWithoutSync()
  24. const setInspectVarsToStore = useCallback((inspectVars: VarInInspect[]) => {
  25. const { setNodesWithInspectVars } = workflowStore.getState()
  26. const { getNodes } = store.getState()
  27. const nodeArr = getNodes()
  28. const nodesKeyValue: Record<string, Node> = {}
  29. nodeArr.forEach((node) => {
  30. nodesKeyValue[node.id] = node
  31. })
  32. const withValueNodeIds: Record<string, boolean> = {}
  33. inspectVars.forEach((varItem) => {
  34. const nodeId = varItem.selector[0]
  35. const node = nodesKeyValue[nodeId]
  36. if (!node)
  37. return
  38. withValueNodeIds[nodeId] = true
  39. })
  40. const withValueNodes = Object.keys(withValueNodeIds).map((nodeId) => {
  41. return nodesKeyValue[nodeId]
  42. })
  43. const res: NodeWithVar[] = withValueNodes.map((node) => {
  44. const nodeId = node.id
  45. const varsUnderTheNode = inspectVars.filter((varItem) => {
  46. return varItem.selector[0] === nodeId
  47. })
  48. const nodeWithVar = {
  49. nodeId,
  50. nodePayload: node.data,
  51. nodeType: node.data.type,
  52. title: node.data.title,
  53. vars: varsUnderTheNode,
  54. isSingRunRunning: false,
  55. isValueFetched: false,
  56. }
  57. return nodeWithVar
  58. })
  59. setNodesWithInspectVars(res)
  60. }, [workflowStore, store])
  61. const fetchInspectVars = useCallback(async () => {
  62. invalidateConversationVarValues()
  63. invalidateSysVarValues()
  64. const data = await fetchAllInspectVars(flowId)
  65. setInspectVarsToStore(data)
  66. handleCancelAllNodeSuccessStatus() // to make sure clear node output show the unset status
  67. }, [invalidateConversationVarValues, invalidateSysVarValues, flowId, setInspectVarsToStore, handleCancelAllNodeSuccessStatus])
  68. return {
  69. fetchInspectVars,
  70. }
  71. }