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

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