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-DSL.ts 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {
  2. useCallback,
  3. useState,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import {
  7. DSL_EXPORT_CHECK,
  8. } from '@/app/components/workflow/constants'
  9. import { useNodesSyncDraft } from './use-nodes-sync-draft'
  10. import { useEventEmitterContextContext } from '@/context/event-emitter'
  11. import { fetchWorkflowDraft } from '@/service/workflow'
  12. import { useToastContext } from '@/app/components/base/toast'
  13. import { useWorkflowStore } from '@/app/components/workflow/store'
  14. import { useExportPipelineDSL } from '@/service/use-pipeline'
  15. export const useDSL = () => {
  16. const { t } = useTranslation()
  17. const { notify } = useToastContext()
  18. const { eventEmitter } = useEventEmitterContextContext()
  19. const [exporting, setExporting] = useState(false)
  20. const { doSyncWorkflowDraft } = useNodesSyncDraft()
  21. const workflowStore = useWorkflowStore()
  22. const { mutateAsync: exportPipelineConfig } = useExportPipelineDSL()
  23. const handleExportDSL = useCallback(async (include = false) => {
  24. const { pipelineId, knowledgeName } = workflowStore.getState()
  25. if (!pipelineId)
  26. return
  27. if (exporting)
  28. return
  29. try {
  30. setExporting(true)
  31. await doSyncWorkflowDraft()
  32. const { data } = await exportPipelineConfig({
  33. pipelineId,
  34. include,
  35. })
  36. const a = document.createElement('a')
  37. const file = new Blob([data], { type: 'application/yaml' })
  38. const url = URL.createObjectURL(file)
  39. a.href = url
  40. a.download = `${knowledgeName}.pipeline`
  41. a.click()
  42. URL.revokeObjectURL(url)
  43. }
  44. catch {
  45. notify({ type: 'error', message: t('app.exportFailed') })
  46. }
  47. finally {
  48. setExporting(false)
  49. }
  50. }, [notify, t, doSyncWorkflowDraft, exporting, exportPipelineConfig, workflowStore])
  51. const exportCheck = useCallback(async () => {
  52. const { pipelineId } = workflowStore.getState()
  53. if (!pipelineId)
  54. return
  55. try {
  56. const workflowDraft = await fetchWorkflowDraft(`/rag/pipelines/${pipelineId}/workflows/draft`)
  57. const list = (workflowDraft.environment_variables || []).filter(env => env.value_type === 'secret')
  58. if (list.length === 0) {
  59. handleExportDSL()
  60. return
  61. }
  62. eventEmitter?.emit({
  63. type: DSL_EXPORT_CHECK,
  64. payload: {
  65. data: list,
  66. },
  67. } as any)
  68. }
  69. catch {
  70. notify({ type: 'error', message: t('app.exportFailed') })
  71. }
  72. }, [eventEmitter, handleExportDSL, notify, t, workflowStore])
  73. return {
  74. exportCheck,
  75. handleExportDSL,
  76. }
  77. }