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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { useCallback } from 'react'
  2. import produce from 'immer'
  3. import { useStoreApi } from 'reactflow'
  4. import { useParams } from 'next/navigation'
  5. import {
  6. useWorkflowStore,
  7. } from '@/app/components/workflow/store'
  8. import { BlockEnum } from '@/app/components/workflow/types'
  9. import { useWorkflowUpdate } from '@/app/components/workflow/hooks'
  10. import {
  11. useNodesReadOnly,
  12. } from '@/app/components/workflow/hooks/use-workflow'
  13. import { syncWorkflowDraft } from '@/service/workflow'
  14. import { useFeaturesStore } from '@/app/components/base/features/hooks'
  15. import { API_PREFIX } from '@/config'
  16. export const useNodesSyncDraft = () => {
  17. const store = useStoreApi()
  18. const workflowStore = useWorkflowStore()
  19. const featuresStore = useFeaturesStore()
  20. const { getNodesReadOnly } = useNodesReadOnly()
  21. const { handleRefreshWorkflowDraft } = useWorkflowUpdate()
  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?.enabled ? (features.opening?.opening_statement || '') : '',
  72. suggested_questions: features.opening?.enabled ? (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 (
  99. notRefreshWhenSyncError?: boolean,
  100. callback?: {
  101. onSuccess?: () => void
  102. onError?: () => void
  103. onSettled?: () => void
  104. },
  105. ) => {
  106. if (getNodesReadOnly())
  107. return
  108. const postParams = getPostParams()
  109. if (postParams) {
  110. const {
  111. setSyncWorkflowDraftHash,
  112. setDraftUpdatedAt,
  113. } = workflowStore.getState()
  114. try {
  115. const res = await syncWorkflowDraft(postParams)
  116. setSyncWorkflowDraftHash(res.hash)
  117. setDraftUpdatedAt(res.updated_at)
  118. callback?.onSuccess && callback.onSuccess()
  119. }
  120. catch (error: any) {
  121. if (error && error.json && !error.bodyUsed) {
  122. error.json().then((err: any) => {
  123. if (err.code === 'draft_workflow_not_sync' && !notRefreshWhenSyncError)
  124. handleRefreshWorkflowDraft()
  125. })
  126. }
  127. callback?.onError && callback.onError()
  128. }
  129. finally {
  130. callback?.onSettled && callback.onSettled()
  131. }
  132. }
  133. }, [workflowStore, getPostParams, getNodesReadOnly, handleRefreshWorkflowDraft])
  134. return {
  135. doSyncWorkflowDraft,
  136. syncWorkflowDraftWhenPageClose,
  137. }
  138. }