Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.7 KB
Raw Blame
import type { AppMode } from '@dify/contracts/api/console/apps/types.gen'
import type { Node, WorkflowDataUpdater } from '@/app/components/workflow/types'
import { useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import { START_INITIAL_POSITION } from '@/app/components/workflow/constants'
import startPlaceholderDefault from '@/app/components/workflow/nodes/start-placeholder/default'
import { BlockEnum } from '@/app/components/workflow/types'
import { generateNewNode } from '@/app/components/workflow/utils'
import { normalizeWorkflowNodes } from '@/app/components/workflow/utils/normalize-workflow-nodes'

type HydrateWorkflowDraftGraphOptions = {
  localStartPlaceholderNodes?: Node[]
}

const hasWorkflowEntryNode = (nodes: Node[] = []): boolean => {
  return nodes.some(
    (node) =>
      node?.data?.type === BlockEnum.Start ||
      node?.data?.type === BlockEnum.TriggerSchedule ||
      node?.data?.type === BlockEnum.TriggerWebhook ||
      node?.data?.type === BlockEnum.TriggerPlugin,
  )
}

const hasStartPlaceholderNode = (nodes: Node[] = []): boolean => {
  return nodes.some((node) => node?.data?.type === BlockEnum.StartPlaceholder)
}

export const useWorkflowDraftGraphForCanvas = (appMode?: AppMode) => {
  const { t } = useTranslation(['workflow'] as const)

  const getNodesWithLocalStartPlaceholder = useCallback(
    (nodes: Node[] = [], localStartPlaceholderNodes?: Node[]) => {
      if (appMode !== 'workflow' || hasWorkflowEntryNode(nodes) || hasStartPlaceholderNode(nodes))
        return nodes

      if (localStartPlaceholderNodes?.length) return [...localStartPlaceholderNodes, ...nodes]

      const { newNode: startPlaceholderNode } = generateNewNode({
        data: {
          ...startPlaceholderDefault.defaultValue,
          selected: true,
          type: startPlaceholderDefault.metaData.type,
          title: t(($) => $[`blocks.${startPlaceholderDefault.metaData.type}`], { ns: 'workflow' }),
          desc: '',
        },
        position: START_INITIAL_POSITION,
      })

      return [startPlaceholderNode, ...nodes]
    },
    [appMode, t],
  )

  const getWorkflowDraftGraphForCanvas = useCallback(
    (
      graph?: Partial<WorkflowDataUpdater>,
      options?: HydrateWorkflowDraftGraphOptions,
    ): WorkflowDataUpdater => {
      const nodes = getNodesWithLocalStartPlaceholder(
        normalizeWorkflowNodes(graph?.nodes || [], appMode),
        options?.localStartPlaceholderNodes,
      )

      return {
        nodes,
        edges: graph?.edges || [],
        viewport: graph?.viewport || { x: 0, y: 0, zoom: 1 },
      }
    },
    [appMode, getNodesWithLocalStartPlaceholder],
  )

  return {
    getWorkflowDraftGraphForCanvas,
  }
}