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.

7.4 KB
Raw Blame
'use client'
import type { FC } from 'react'
import type { ExternalTraceRun } from './external-trace-row'
import type { WorkflowRunInfo } from './workflow-tool-tracing-context'
import type { NodeTracing } from '@/types/workflow'
import { cn } from '@langgenius/dify-ui/cn'
import * as React from 'react'
import { use, useCallback, useState } from 'react'
import { useTranslation } from 'react-i18next'
import formatNodeList from '@/app/components/workflow/run/utils/format-log'
import { BlockEnum } from '../types'
import { getHoveredParallelId } from './get-hovered-parallel-id'
import { useLogs } from './hooks'
import NodePanel from './node'
import SpecialResultPanel from './special-result-panel'
import { getIterationDurationMap, getIterationResultList } from './utils/format-log/iteration'
import WorkflowToolTracing from './workflow-tool-tracing'
import { WorkflowToolTracingContext } from './workflow-tool-tracing-context'

type TracingPanelProps = {
  list: NodeTracing[]
  externalTraceRun?: ExternalTraceRun
  className?: string
  hideNodeInfo?: boolean
  hideNodeProcessDetail?: boolean
  workflowRun?: WorkflowRunInfo
}

function findExecution(nodes: NodeTracing[], id: string): NodeTracing | undefined {
  for (const node of nodes) {
    if (node.id === id) return node
    const child = node.parallelDetail?.children && findExecution(node.parallelDetail.children, id)
    if (child) return child
  }
}

const TracingPanel: FC<TracingPanelProps> = ({
  list,
  externalTraceRun,
  className,
  hideNodeInfo = false,
  hideNodeProcessDetail = false,
  workflowRun,
}) => {
  const parentTracing = use(WorkflowToolTracingContext)
  const currentWorkflowRun = workflowRun ?? parentTracing?.workflowRun
  const [workflowTool, setWorkflowTool] = useState<NodeTracing | null>(null)
  const { t } = useTranslation(['workflow'])
  const treeNodes = formatNodeList(list, t)
  const [containerId, setContainerId] = useState<string | null>(null)
  const container = containerId ? findExecution(treeNodes, containerId) : undefined
  const iteration = container?.node_type === BlockEnum.Iteration ? container : undefined
  const loop = container?.node_type === BlockEnum.Loop ? container : undefined
  const [collapsedNodes, setCollapsedNodes] = useState<Set<string>>(() => new Set())
  const [hoveredParallel, setHoveredParallel] = useState<string | null>(null)

  const toggleCollapse = (id: string) => {
    setCollapsedNodes((prev) => {
      const newSet = new Set(prev)
      if (newSet.has(id)) newSet.delete(id)
      else newSet.add(id)

      return newSet
    })
  }

  const handleParallelMouseEnter = useCallback((id: string) => {
    setHoveredParallel(id)
  }, [])

  const handleParallelMouseLeave = useCallback((e: React.MouseEvent) => {
    setHoveredParallel(getHoveredParallelId(e.relatedTarget))
  }, [])

  const logs = useLogs()

  const renderNode = (node: NodeTracing) => {
    const isParallelFirstNode = !!node.parallelDetail?.isParallelStartNode
    if (isParallelFirstNode) {
      const parallelDetail = node.parallelDetail!
      const isCollapsed = collapsedNodes.has(node.id)
      const isHovered = hoveredParallel === node.id
      return (
        <div
          key={node.id}
          className="relative mb-2 ml-4"
          data-parallel-id={node.id}
          onMouseEnter={() => handleParallelMouseEnter(node.id)}
          onMouseLeave={handleParallelMouseLeave}
        >
          <div className="mb-1 flex items-center">
            <button
              type="button"
              aria-controls={`${node.id}-children`}
              aria-expanded={!isCollapsed}
              aria-label={parallelDetail.parallelTitle}
              onClick={() => toggleCollapse(node.id)}
              className={cn(
                'mr-2 transition-colors',
                isHovered
                  ? 'rounded-sm border-components-button-primary-border bg-components-button-primary-bg text-text-primary-on-surface'
                  : 'text-text-secondary hover:text-text-primary',
              )}
            >
              {isHovered ? (
                <span aria-hidden className="i-ri-arrow-down-s-line size-3" />
              ) : (
                <span aria-hidden className="i-ri-menu-4-line size-3 text-text-tertiary" />
              )}
            </button>
            <div className="flex items-center system-xs-semibold-uppercase text-text-secondary">
              <span>{parallelDetail.parallelTitle}</span>
            </div>
            <div
              className="mx-2 h-px grow bg-divider-subtle"
              style={{
                background:
                  'linear-gradient(to right, rgba(16, 24, 40, 0.08), rgba(255, 255, 255, 0)',
              }}
            ></div>
          </div>
          <div id={`${node.id}-children`} hidden={isCollapsed} className="relative pl-2">
            <div
              className={cn(
                'absolute top-0 bottom-0 left-1.25 w-0.5',
                isHovered ? 'bg-text-accent-secondary' : 'bg-divider-subtle',
              )}
            ></div>
            {parallelDetail.children!.map(renderNode)}
          </div>
        </div>
      )
    } else {
      const isHovered = hoveredParallel === node.id
      return (
        <div key={node.id}>
          <div
            className={cn(
              '-mb-1.5 pl-4 system-2xs-medium-uppercase',
              isHovered ? 'text-text-tertiary' : 'text-text-quaternary',
            )}
          >
            {node?.parallelDetail?.branchTitle}
          </div>
          <NodePanel
            nodeInfo={node!}
            allExecutions={list}
            externalTraceRun={externalTraceRun}
            onShowIterationDetail={() => setContainerId(node.id)}
            onShowLoopDetail={() => setContainerId(node.id)}
            onShowRetryDetail={logs.handleShowRetryResultList}
            onShowAgentOrToolLog={logs.handleShowAgentOrToolLog}
            hideInfo={hideNodeInfo}
            hideProcessDetail={hideNodeProcessDetail}
          />
        </div>
      )
    }
  }

  const content =
    workflowTool && currentWorkflowRun ? (
      <WorkflowToolTracing
        key={workflowTool.id}
        node={workflowTool}
        workflowRun={currentWorkflowRun}
        onBack={() => setWorkflowTool(null)}
      />
    ) : container || logs.showSpecialResultPanel ? (
      <SpecialResultPanel
        {...logs}
        externalTraceRun={externalTraceRun}
        showIteratingDetail={!!iteration}
        setShowIteratingDetailFalse={() => setContainerId(null)}
        iterationResultList={iteration ? getIterationResultList(iteration, list) : undefined}
        iterationResultDurationMap={iteration ? getIterationDurationMap(iteration) : undefined}
        showLoopingDetail={!!loop}
        setShowLoopingDetailFalse={() => setContainerId(null)}
        loopResultList={loop?.details}
        loopResultDurationMap={loop?.loopDurationMap || loop?.execution_metadata?.loop_duration_map}
        loopResultVariableMap={loop?.execution_metadata?.loop_variable_map}
      />
    ) : (
      <div
        className={cn('py-2', className)}
        onClick={(e) => {
          e.stopPropagation()
          e.nativeEvent.stopImmediatePropagation()
        }}
      >
        {treeNodes.map(renderNode)}
      </div>
    )

  return (
    <WorkflowToolTracingContext
      value={
        currentWorkflowRun
          ? { workflowRun: currentWorkflowRun, onShowWorkflowTool: setWorkflowTool }
          : null
      }
    >
      {content}
    </WorkflowToolTracingContext>
  )
}

export default TracingPanel