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-nodes-sync-draft.ts 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { useCallback } from 'react'
  2. import produce from 'immer'
  3. import { useStoreApi } from 'reactflow'
  4. import {
  5. useWorkflowStore,
  6. } from '@/app/components/workflow/store'
  7. import {
  8. useNodesReadOnly,
  9. } from '@/app/components/workflow/hooks/use-workflow'
  10. import { API_PREFIX } from '@/config'
  11. import { syncWorkflowDraft } from '@/service/workflow'
  12. import { usePipelineRefreshDraft } from '.'
  13. import { CUSTOM_DATA_SOURCE_EMPTY_NODE } from '@/app/components/workflow/nodes/data-source-empty/constants'
  14. export const useNodesSyncDraft = () => {
  15. const store = useStoreApi()
  16. const workflowStore = useWorkflowStore()
  17. const { getNodesReadOnly } = useNodesReadOnly()
  18. const { handleRefreshWorkflowDraft } = usePipelineRefreshDraft()
  19. const getPostParams = useCallback(() => {
  20. const {
  21. getNodes,
  22. edges,
  23. transform,
  24. } = store.getState()
  25. const nodesOriginal = getNodes()
  26. const nodes = nodesOriginal.filter(node => node.type !== CUSTOM_DATA_SOURCE_EMPTY_NODE)
  27. const [x, y, zoom] = transform
  28. const {
  29. pipelineId,
  30. environmentVariables,
  31. syncWorkflowDraftHash,
  32. ragPipelineVariables,
  33. } = workflowStore.getState()
  34. if (pipelineId && !!nodes.length) {
  35. const producedNodes = produce(nodes, (draft) => {
  36. draft.forEach((node) => {
  37. Object.keys(node.data).forEach((key) => {
  38. if (key.startsWith('_'))
  39. delete node.data[key]
  40. })
  41. })
  42. })
  43. const producedEdges = produce(edges, (draft) => {
  44. draft.forEach((edge) => {
  45. Object.keys(edge.data).forEach((key) => {
  46. if (key.startsWith('_'))
  47. delete edge.data[key]
  48. })
  49. })
  50. })
  51. return {
  52. url: `/rag/pipelines/${pipelineId}/workflows/draft`,
  53. params: {
  54. graph: {
  55. nodes: producedNodes,
  56. edges: producedEdges,
  57. viewport: {
  58. x,
  59. y,
  60. zoom,
  61. },
  62. },
  63. environment_variables: environmentVariables,
  64. rag_pipeline_variables: ragPipelineVariables,
  65. hash: syncWorkflowDraftHash,
  66. },
  67. }
  68. }
  69. }, [store, workflowStore])
  70. const syncWorkflowDraftWhenPageClose = useCallback(() => {
  71. if (getNodesReadOnly())
  72. return
  73. const postParams = getPostParams()
  74. if (postParams) {
  75. navigator.sendBeacon(
  76. `${API_PREFIX}${postParams.url}?_token=${localStorage.getItem('console_token')}`,
  77. JSON.stringify(postParams.params),
  78. )
  79. }
  80. }, [getPostParams, getNodesReadOnly])
  81. const doSyncWorkflowDraft = useCallback(async (
  82. notRefreshWhenSyncError?: boolean,
  83. callback?: {
  84. onSuccess?: () => void
  85. onError?: () => void
  86. onSettled?: () => void
  87. },
  88. ) => {
  89. if (getNodesReadOnly())
  90. return
  91. const postParams = getPostParams()
  92. if (postParams) {
  93. const {
  94. setSyncWorkflowDraftHash,
  95. setDraftUpdatedAt,
  96. } = workflowStore.getState()
  97. try {
  98. const res = await syncWorkflowDraft(postParams)
  99. setSyncWorkflowDraftHash(res.hash)
  100. setDraftUpdatedAt(res.updated_at)
  101. callback?.onSuccess && callback.onSuccess()
  102. }
  103. catch (error: any) {
  104. if (error && error.json && !error.bodyUsed) {
  105. error.json().then((err: any) => {
  106. if (err.code === 'draft_workflow_not_sync' && !notRefreshWhenSyncError)
  107. handleRefreshWorkflowDraft()
  108. })
  109. }
  110. callback?.onError && callback.onError()
  111. }
  112. finally {
  113. callback?.onSettled && callback.onSettled()
  114. }
  115. }
  116. }, [getPostParams, getNodesReadOnly, workflowStore, handleRefreshWorkflowDraft])
  117. return {
  118. doSyncWorkflowDraft,
  119. syncWorkflowDraftWhenPageClose,
  120. }
  121. }