您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

use-pipeline-config.ts 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { useCallback } from 'react'
  2. import {
  3. useStore,
  4. useWorkflowStore,
  5. } from '@/app/components/workflow/store'
  6. import { useWorkflowConfig } from '@/service/use-workflow'
  7. import type { FetchWorkflowDraftResponse } from '@/types/workflow'
  8. import { useDataSourceList } from '@/service/use-pipeline'
  9. import type { DataSourceItem } from '@/app/components/workflow/block-selector/types'
  10. import { basePath } from '@/utils/var'
  11. import type { FileUploadConfigResponse } from '@/models/common'
  12. export const usePipelineConfig = () => {
  13. const pipelineId = useStore(s => s.pipelineId)
  14. const workflowStore = useWorkflowStore()
  15. const handleUpdateWorkflowConfig = useCallback((config: Record<string, any>) => {
  16. const { setWorkflowConfig } = workflowStore.getState()
  17. setWorkflowConfig(config)
  18. }, [workflowStore])
  19. useWorkflowConfig(
  20. pipelineId ? `/rag/pipelines/${pipelineId}/workflows/draft/config` : '',
  21. handleUpdateWorkflowConfig,
  22. )
  23. const handleUpdateNodesDefaultConfigs = useCallback((nodesDefaultConfigs: Record<string, any> | Record<string, any>[]) => {
  24. const { setNodesDefaultConfigs } = workflowStore.getState()
  25. let res: Record<string, any> = {}
  26. if (Array.isArray(nodesDefaultConfigs)) {
  27. nodesDefaultConfigs.forEach((item) => {
  28. res[item.type] = item.config
  29. })
  30. }
  31. else {
  32. res = nodesDefaultConfigs as Record<string, any>
  33. }
  34. setNodesDefaultConfigs!(res)
  35. }, [workflowStore])
  36. useWorkflowConfig(
  37. pipelineId ? `/rag/pipelines/${pipelineId}/workflows/default-workflow-block-configs` : '',
  38. handleUpdateNodesDefaultConfigs,
  39. )
  40. const handleUpdatePublishedAt = useCallback((publishedWorkflow: FetchWorkflowDraftResponse) => {
  41. const { setPublishedAt } = workflowStore.getState()
  42. setPublishedAt(publishedWorkflow?.created_at)
  43. }, [workflowStore])
  44. useWorkflowConfig(
  45. pipelineId ? `/rag/pipelines/${pipelineId}/workflows/publish` : '',
  46. handleUpdatePublishedAt,
  47. )
  48. const handleUpdateDataSourceList = useCallback((dataSourceList: DataSourceItem[]) => {
  49. dataSourceList.forEach((item) => {
  50. const icon = item.declaration.identity.icon
  51. if (typeof icon == 'string' && !icon.includes(basePath))
  52. item.declaration.identity.icon = `${basePath}${icon}`
  53. })
  54. const { setDataSourceList } = workflowStore.getState()
  55. setDataSourceList!(dataSourceList)
  56. }, [workflowStore])
  57. const handleUpdateWorkflowFileUploadConfig = useCallback((config: FileUploadConfigResponse) => {
  58. const { setFileUploadConfig } = workflowStore.getState()
  59. setFileUploadConfig(config)
  60. }, [workflowStore])
  61. useWorkflowConfig('/files/upload', handleUpdateWorkflowFileUploadConfig)
  62. useDataSourceList(!!pipelineId, handleUpdateDataSourceList)
  63. }