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.4KB

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