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.

5.2 KB
Raw Blame
'use client'
import type { JsonObject } from '@dify/contracts/api/console/trial-apps/types.gen'
import type { FC } from 'react'
import type { Edge, Node } from '@/app/components/workflow/types'
import { cn } from '@langgenius/dify-ui/cn'
import { useQuery } from '@tanstack/react-query'
import * as React from 'react'
import { LoadingPlaceholder } from '@/app/components/base/loading-placeholder'
import { BlockEnum } from '@/app/components/workflow/types'
import WorkflowPreview from '@/app/components/workflow/workflow-preview'
import { consoleQuery } from '@/service/console'

type Props = {
  readonly appId: string
  readonly className?: string
}

const blockTypeMap: Record<string, BlockEnum> = {
  agent: BlockEnum.Agent,
  'agent-v2': BlockEnum.AgentV2,
  answer: BlockEnum.Answer,
  assigner: BlockEnum.Assigner,
  code: BlockEnum.Code,
  datasource: BlockEnum.DataSource,
  'datasource-empty': BlockEnum.DataSourceEmpty,
  'document-extractor': BlockEnum.DocExtractor,
  end: BlockEnum.End,
  'http-request': BlockEnum.HttpRequest,
  'human-input': BlockEnum.HumanInput,
  'if-else': BlockEnum.IfElse,
  iteration: BlockEnum.Iteration,
  'iteration-start': BlockEnum.IterationStart,
  'knowledge-index': BlockEnum.KnowledgeBase,
  'knowledge-retrieval': BlockEnum.KnowledgeRetrieval,
  'knowledge-retrieval-v2': BlockEnum.KnowledgeRetrievalV2,
  'list-operator': BlockEnum.ListFilter,
  llm: BlockEnum.LLM,
  loop: BlockEnum.Loop,
  'loop-end': BlockEnum.LoopEnd,
  'loop-start': BlockEnum.LoopStart,
  'parameter-extractor': BlockEnum.ParameterExtractor,
  'question-classifier': BlockEnum.QuestionClassifier,
  start: BlockEnum.Start,
  'start-placeholder': BlockEnum.StartPlaceholder,
  'template-transform': BlockEnum.TemplateTransform,
  tool: BlockEnum.Tool,
  'trigger-plugin': BlockEnum.TriggerPlugin,
  'trigger-schedule': BlockEnum.TriggerSchedule,
  'trigger-webhook': BlockEnum.TriggerWebhook,
  'variable-aggregator': BlockEnum.VariableAggregator,
  'variable-assigner': BlockEnum.VariableAssigner,
}

const isRecord = (value: unknown): value is Record<string, unknown> => {
  return typeof value === 'object' && value !== null && !Array.isArray(value)
}

const getString = (value: unknown) => {
  return typeof value === 'string' ? value : undefined
}

const getPosition = (value: unknown) => {
  if (!isRecord(value) || typeof value.x !== 'number' || typeof value.y !== 'number')
    return { x: 0, y: 0 }

  return {
    x: value.x,
    y: value.y,
  }
}

const getViewport = (value: unknown) => {
  if (
    !isRecord(value) ||
    typeof value.x !== 'number' ||
    typeof value.y !== 'number' ||
    typeof value.zoom !== 'number'
  ) {
    return { x: 0, y: 0, zoom: 1 }
  }

  return {
    x: value.x,
    y: value.y,
    zoom: value.zoom,
  }
}

const getBlockType = (value: unknown) => {
  if (typeof value !== 'string') return null

  return blockTypeMap[value] || null
}

const normalizeWorkflowPreviewGraph = (graph: JsonObject) => {
  const nodesData = Array.isArray(graph.nodes) ? graph.nodes : []
  const edgesData = Array.isArray(graph.edges) ? graph.edges : []

  const nodes: Node[] = nodesData.flatMap((node) => {
    if (!isRecord(node)) return []

    const id = getString(node.id)
    if (!id) return []

    const data = isRecord(node.data) ? node.data : {}
    const type = getBlockType(data.type) || BlockEnum.Start
    const title = getString(data.title) || ''

    return [
      {
        id,
        position: getPosition(node.position),
        ...(getString(node.type) ? { type: getString(node.type) } : {}),
        data: {
          ...data,
          desc: getString(data.desc) || '',
          title,
          type,
        },
      },
    ]
  })
  const edges: Edge[] = edgesData.flatMap((edge) => {
    if (!isRecord(edge)) return []

    const id = getString(edge.id)
    if (!id) return []

    const data = isRecord(edge.data) ? edge.data : {}
    const sourceType = getBlockType(data.sourceType) || BlockEnum.Start
    const targetType = getBlockType(data.targetType) || BlockEnum.Start

    return [
      {
        id,
        source: getString(edge.source) || '',
        target: getString(edge.target) || '',
        ...(getString(edge.type) ? { type: getString(edge.type) } : {}),
        ...(getString(edge.sourceHandle) ? { sourceHandle: getString(edge.sourceHandle) } : {}),
        ...(getString(edge.targetHandle) ? { targetHandle: getString(edge.targetHandle) } : {}),
        data: {
          ...data,
          sourceType,
          targetType,
        },
      },
    ]
  })

  return {
    nodes,
    edges,
    viewport: getViewport(graph.viewport),
  }
}

const FlowAppPreview: FC<Props> = ({ appId, className }) => {
  const { data, isLoading } = useQuery(
    consoleQuery.trialApps.byAppId.workflows.get.queryOptions({
      input: { params: { app_id: appId } },
    }),
  )

  if (isLoading) {
    return (
      <div className="flex h-full items-center justify-center">
        <LoadingPlaceholder />
      </div>
    )
  }
  if (!data) return null
  const previewGraph = normalizeWorkflowPreviewGraph(data.graph)
  return (
    <div className="size-full">
      <WorkflowPreview {...previewGraph} className={cn(className)} miniMapToRight />
    </div>
  )
}
export default React.memo(FlowAppPreview)