use-nodes-sync-draft.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { useCallback } from 'react'
  2. import produce from 'immer'
  3. import { useStoreApi } from 'reactflow'
  4. import { useParams } from 'next/navigation'
  5. import {
  6. useStore,
  7. useWorkflowStore,
  8. } from '../store'
  9. import { BlockEnum } from '../types'
  10. import { useWorkflowUpdate } from '../hooks'
  11. import { useNodesReadOnly } from './use-workflow'
  12. import { syncWorkflowDraft } from '@/service/workflow'
  13. import { useFeaturesStore } from '@/app/components/base/features/hooks'
  14. import { API_PREFIX } from '@/config'
  15. export const useNodesSyncDraft = () => {
  16. const store = useStoreApi()
  17. const workflowStore = useWorkflowStore()
  18. const featuresStore = useFeaturesStore()
  19. const { getNodesReadOnly } = useNodesReadOnly()
  20. const { handleRefreshWorkflowDraft } = useWorkflowUpdate()
  21. const debouncedSyncWorkflowDraft = useStore(s => s.debouncedSyncWorkflowDraft)
  22. const params = useParams()
  23. const getPostParams = useCallback(() => {
  24. const {
  25. getNodes,
  26. edges,
  27. transform,
  28. } = store.getState()
  29. const [x, y, zoom] = transform
  30. const {
  31. appId,
  32. conversationVariables,
  33. environmentVariables,
  34. syncWorkflowDraftHash,
  35. } = workflowStore.getState()
  36. if (appId) {
  37. const nodes = getNodes()
  38. const hasStartNode = nodes.find(node => node.data.type === BlockEnum.Start)
  39. if (!hasStartNode)
  40. return
  41. const features = featuresStore!.getState().features
  42. const producedNodes = produce(nodes, (draft) => {
  43. draft.forEach((node) => {
  44. Object.keys(node.data).forEach((key) => {
  45. if (key.startsWith('_'))
  46. delete node.data[key]
  47. })
  48. })
  49. })
  50. const producedEdges = produce(edges, (draft) => {
  51. draft.forEach((edge) => {
  52. Object.keys(edge.data).forEach((key) => {
  53. if (key.startsWith('_'))
  54. delete edge.data[key]
  55. })
  56. })
  57. })
  58. return {
  59. url: `/apps/${appId}/workflows/draft`,
  60. params: {
  61. graph: {
  62. nodes: producedNodes,
  63. edges: producedEdges,
  64. viewport: {
  65. x,
  66. y,
  67. zoom,
  68. },
  69. },
  70. features: {
  71. opening_statement: features.opening?.opening_statement || '',
  72. suggested_questions: features.opening?.suggested_questions || [],
  73. suggested_questions_after_answer: features.suggested,
  74. text_to_speech: features.text2speech,
  75. speech_to_text: features.speech2text,
  76. retriever_resource: features.citation,
  77. sensitive_word_avoidance: features.moderation,
  78. file_upload: features.file,
  79. },
  80. environment_variables: environmentVariables,
  81. conversation_variables: conversationVariables,
  82. hash: syncWorkflowDraftHash,
  83. },
  84. }
  85. }
  86. }, [store, featuresStore, workflowStore])
  87. const syncWorkflowDraftWhenPageClose = useCallback(() => {
  88. if (getNodesReadOnly())
  89. return
  90. const postParams = getPostParams()
  91. if (postParams) {
  92. navigator.sendBeacon(
  93. `${API_PREFIX}/apps/${params.appId}/workflows/draft?_token=${localStorage.getItem('console_token')}`,
  94. JSON.stringify(postParams.params),
  95. )
  96. }
  97. }, [getPostParams, params.appId, getNodesReadOnly])
  98. const doSyncWorkflowDraft = useCallback(async (notRefreshWhenSyncError?: boolean) => {
  99. if (getNodesReadOnly())
  100. return
  101. const postParams = getPostParams()
  102. if (postParams) {
  103. const {
  104. setSyncWorkflowDraftHash,
  105. setDraftUpdatedAt,
  106. } = workflowStore.getState()
  107. try {
  108. const res = await syncWorkflowDraft(postParams)
  109. setSyncWorkflowDraftHash(res.hash)
  110. setDraftUpdatedAt(res.updated_at)
  111. }
  112. catch (error: any) {
  113. if (error && error.json && !error.bodyUsed) {
  114. error.json().then((err: any) => {
  115. if (err.code === 'draft_workflow_not_sync' && !notRefreshWhenSyncError)
  116. handleRefreshWorkflowDraft()
  117. })
  118. }
  119. }
  120. }
  121. }, [workflowStore, getPostParams, getNodesReadOnly, handleRefreshWorkflowDraft])
  122. const handleSyncWorkflowDraft = useCallback((sync?: boolean, notRefreshWhenSyncError?: boolean) => {
  123. if (getNodesReadOnly())
  124. return
  125. if (sync)
  126. doSyncWorkflowDraft(notRefreshWhenSyncError)
  127. else
  128. debouncedSyncWorkflowDraft(doSyncWorkflowDraft)
  129. }, [debouncedSyncWorkflowDraft, doSyncWorkflowDraft, getNodesReadOnly])
  130. return {
  131. doSyncWorkflowDraft,
  132. handleSyncWorkflowDraft,
  133. syncWorkflowDraftWhenPageClose,
  134. }
  135. }