dify
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
export { default as IterationLogTrigger } from './iteration-log-trigger'
|
||||
export { default as IterationResultPanel } from './iteration-result-panel'
|
||||
@@ -0,0 +1,147 @@
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { RiArrowRightSLine } from '@remixicon/react'
|
||||
import Button from '@/app/components/base/button'
|
||||
import type {
|
||||
IterationDurationMap,
|
||||
NodeTracing,
|
||||
} from '@/types/workflow'
|
||||
import { NodeRunningStatus } from '@/app/components/workflow/types'
|
||||
import { Iteration } from '@/app/components/base/icons/src/vender/workflow'
|
||||
|
||||
type IterationLogTriggerProps = {
|
||||
nodeInfo: NodeTracing
|
||||
allExecutions?: NodeTracing[]
|
||||
onShowIterationResultList: (iterationResultList: NodeTracing[][], iterationResultDurationMap: IterationDurationMap) => void
|
||||
}
|
||||
const IterationLogTrigger = ({
|
||||
nodeInfo,
|
||||
allExecutions,
|
||||
onShowIterationResultList,
|
||||
}: IterationLogTriggerProps) => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const filterNodesForInstance = (key: string): NodeTracing[] => {
|
||||
if (!allExecutions) return []
|
||||
|
||||
const parallelNodes = allExecutions.filter(exec =>
|
||||
exec.execution_metadata?.parallel_mode_run_id === key,
|
||||
)
|
||||
if (parallelNodes.length > 0)
|
||||
return parallelNodes
|
||||
|
||||
const serialIndex = Number.parseInt(key, 10)
|
||||
if (!isNaN(serialIndex)) {
|
||||
const serialNodes = allExecutions.filter(exec =>
|
||||
exec.execution_metadata?.iteration_id === nodeInfo.node_id
|
||||
&& exec.execution_metadata?.iteration_index === serialIndex,
|
||||
)
|
||||
if (serialNodes.length > 0)
|
||||
return serialNodes
|
||||
}
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
const handleOnShowIterationDetail = (e: React.MouseEvent<HTMLButtonElement>) => {
|
||||
e.stopPropagation()
|
||||
e.nativeEvent.stopImmediatePropagation()
|
||||
|
||||
const iterationNodeMeta = nodeInfo.execution_metadata
|
||||
const iterDurationMap = nodeInfo?.iterDurationMap || iterationNodeMeta?.iteration_duration_map || {}
|
||||
|
||||
let structuredList: NodeTracing[][] = []
|
||||
if (iterationNodeMeta?.iteration_duration_map) {
|
||||
const instanceKeys = Object.keys(iterationNodeMeta.iteration_duration_map)
|
||||
structuredList = instanceKeys
|
||||
.map(key => filterNodesForInstance(key))
|
||||
.filter(branchNodes => branchNodes.length > 0)
|
||||
|
||||
// Also include failed iterations that might not be in duration map
|
||||
if (allExecutions && nodeInfo.details?.length) {
|
||||
const existingIterationIndices = new Set<number>()
|
||||
structuredList.forEach((iteration) => {
|
||||
iteration.forEach((node) => {
|
||||
if (node.execution_metadata?.iteration_index !== undefined)
|
||||
existingIterationIndices.add(node.execution_metadata.iteration_index)
|
||||
})
|
||||
})
|
||||
|
||||
// Find failed iterations that are not in the structured list
|
||||
nodeInfo.details.forEach((iteration, index) => {
|
||||
if (!existingIterationIndices.has(index) && iteration.some(node => node.status === NodeRunningStatus.Failed))
|
||||
structuredList.push(iteration)
|
||||
})
|
||||
|
||||
// Sort by iteration index to maintain order
|
||||
structuredList.sort((a, b) => {
|
||||
const aIndex = a[0]?.execution_metadata?.iteration_index ?? 0
|
||||
const bIndex = b[0]?.execution_metadata?.iteration_index ?? 0
|
||||
return aIndex - bIndex
|
||||
})
|
||||
}
|
||||
}
|
||||
else if (nodeInfo.details?.length) {
|
||||
structuredList = nodeInfo.details
|
||||
}
|
||||
|
||||
onShowIterationResultList(structuredList, iterDurationMap)
|
||||
}
|
||||
|
||||
let displayIterationCount = 0
|
||||
const iterMap = nodeInfo.execution_metadata?.iteration_duration_map
|
||||
if (iterMap)
|
||||
displayIterationCount = Object.keys(iterMap).length
|
||||
else if (nodeInfo.details?.length)
|
||||
displayIterationCount = nodeInfo.details.length
|
||||
else if (nodeInfo.metadata?.iterator_length)
|
||||
displayIterationCount = nodeInfo.metadata.iterator_length
|
||||
|
||||
const getErrorCount = (details: NodeTracing[][] | undefined, iterationNodeMeta?: any) => {
|
||||
if (!details || details.length === 0)
|
||||
return 0
|
||||
|
||||
// Use Set to track failed iteration indices to avoid duplicate counting
|
||||
const failedIterationIndices = new Set<number>()
|
||||
|
||||
// Collect failed iteration indices from details
|
||||
details.forEach((iteration, index) => {
|
||||
if (iteration.some(item => item.status === NodeRunningStatus.Failed)) {
|
||||
// Try to get iteration index from first node, fallback to array index
|
||||
const iterationIndex = iteration[0]?.execution_metadata?.iteration_index ?? index
|
||||
failedIterationIndices.add(iterationIndex)
|
||||
}
|
||||
})
|
||||
|
||||
// If allExecutions exists, check for additional failed iterations
|
||||
if (iterationNodeMeta?.iteration_duration_map && allExecutions) {
|
||||
// Find all failed iteration nodes
|
||||
allExecutions.forEach((exec) => {
|
||||
if (exec.execution_metadata?.iteration_id === nodeInfo.node_id
|
||||
&& exec.status === NodeRunningStatus.Failed
|
||||
&& exec.execution_metadata?.iteration_index !== undefined)
|
||||
failedIterationIndices.add(exec.execution_metadata.iteration_index)
|
||||
})
|
||||
}
|
||||
|
||||
return failedIterationIndices.size
|
||||
}
|
||||
const errorCount = getErrorCount(nodeInfo.details, nodeInfo.execution_metadata)
|
||||
|
||||
return (
|
||||
<Button
|
||||
className='flex w-full cursor-pointer items-center gap-2 self-stretch rounded-lg border-none bg-components-button-tertiary-bg-hover px-3 py-2 hover:bg-components-button-tertiary-bg-hover'
|
||||
onClick={handleOnShowIterationDetail}
|
||||
>
|
||||
<Iteration className='h-4 w-4 shrink-0 text-components-button-tertiary-text' />
|
||||
<div className='system-sm-medium flex-1 text-left text-components-button-tertiary-text'>{t('workflow.nodes.iteration.iteration', { count: displayIterationCount })}{errorCount > 0 && (
|
||||
<>
|
||||
{t('workflow.nodes.iteration.comma')}
|
||||
{t('workflow.nodes.iteration.error', { count: errorCount })}
|
||||
</>
|
||||
)}</div>
|
||||
<RiArrowRightSLine className='h-4 w-4 shrink-0 text-components-button-tertiary-text' />
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
||||
export default IterationLogTrigger
|
||||
@@ -0,0 +1,128 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import React, { useCallback, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import {
|
||||
RiArrowLeftLine,
|
||||
RiArrowRightSLine,
|
||||
RiErrorWarningLine,
|
||||
RiLoader2Line,
|
||||
} from '@remixicon/react'
|
||||
import { NodeRunningStatus } from '@/app/components/workflow/types'
|
||||
import TracingPanel from '@/app/components/workflow/run/tracing-panel'
|
||||
import { Iteration } from '@/app/components/base/icons/src/vender/workflow'
|
||||
import cn from '@/utils/classnames'
|
||||
import type { IterationDurationMap, NodeTracing } from '@/types/workflow'
|
||||
const i18nPrefix = 'workflow.singleRun'
|
||||
|
||||
type Props = {
|
||||
list: NodeTracing[][]
|
||||
onBack: () => void
|
||||
iterDurationMap?: IterationDurationMap
|
||||
}
|
||||
|
||||
const IterationResultPanel: FC<Props> = ({
|
||||
list,
|
||||
onBack,
|
||||
iterDurationMap,
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
const [expandedIterations, setExpandedIterations] = useState<Record<number, boolean>>({})
|
||||
|
||||
const toggleIteration = useCallback((index: number) => {
|
||||
setExpandedIterations(prev => ({
|
||||
...prev,
|
||||
[index]: !prev[index],
|
||||
}))
|
||||
}, [])
|
||||
const countIterDuration = (iteration: NodeTracing[], iterDurationMap: IterationDurationMap): string => {
|
||||
const IterRunIndex = iteration[0]?.execution_metadata?.iteration_index as number
|
||||
const iterRunId = iteration[0]?.execution_metadata?.parallel_mode_run_id
|
||||
const iterItem = iterDurationMap[iterRunId || IterRunIndex]
|
||||
const duration = iterItem
|
||||
return `${(duration && duration > 0.01) ? duration.toFixed(2) : 0.01}s`
|
||||
}
|
||||
const iterationStatusShow = (index: number, iteration: NodeTracing[], iterDurationMap?: IterationDurationMap) => {
|
||||
const hasFailed = iteration.some(item => item.status === NodeRunningStatus.Failed)
|
||||
const isRunning = iteration.some(item => item.status === NodeRunningStatus.Running)
|
||||
const hasDurationMap = iterDurationMap && Object.keys(iterDurationMap).length !== 0
|
||||
|
||||
if (hasFailed)
|
||||
return <RiErrorWarningLine className='h-4 w-4 text-text-destructive' />
|
||||
|
||||
if (isRunning)
|
||||
return <RiLoader2Line className='h-3.5 w-3.5 animate-spin text-primary-600' />
|
||||
|
||||
return (
|
||||
<>
|
||||
{hasDurationMap && (
|
||||
<div className='system-xs-regular text-text-tertiary'>
|
||||
{countIterDuration(iteration, iterDurationMap)}
|
||||
</div>
|
||||
)}
|
||||
<RiArrowRightSLine
|
||||
className={cn(
|
||||
'h-4 w-4 shrink-0 text-text-tertiary transition-transform duration-200',
|
||||
expandedIterations[index] && 'rotate-90',
|
||||
)}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='bg-components-panel-bg'>
|
||||
<div
|
||||
className='flex h-8 cursor-pointer items-center border-b-[0.5px] border-b-divider-regular px-4 text-text-accent-secondary'
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
e.nativeEvent.stopImmediatePropagation()
|
||||
onBack()
|
||||
}}
|
||||
>
|
||||
<RiArrowLeftLine className='mr-1 h-4 w-4' />
|
||||
<div className='system-sm-medium'>{t(`${i18nPrefix}.back`)}</div>
|
||||
</div>
|
||||
{/* List */}
|
||||
<div className='bg-components-panel-bg p-2'>
|
||||
{list.map((iteration, index) => (
|
||||
<div key={index} className={cn('mb-1 overflow-hidden rounded-xl border-none bg-background-section-burn')}>
|
||||
<div
|
||||
className={cn(
|
||||
'flex w-full cursor-pointer items-center justify-between px-3',
|
||||
expandedIterations[index] ? 'pb-2 pt-3' : 'py-3',
|
||||
'rounded-xl text-left',
|
||||
)}
|
||||
onClick={() => toggleIteration(index)}
|
||||
>
|
||||
<div className={cn('flex grow items-center gap-2')}>
|
||||
<div className='flex h-4 w-4 shrink-0 items-center justify-center rounded-[5px] border-divider-subtle bg-util-colors-cyan-cyan-500'>
|
||||
<Iteration className='h-3 w-3 text-text-primary-on-surface' />
|
||||
</div>
|
||||
<span className='system-sm-semibold-uppercase grow text-text-primary'>
|
||||
{t(`${i18nPrefix}.iteration`)} {index + 1}
|
||||
</span>
|
||||
{iterationStatusShow(index, iteration, iterDurationMap)}
|
||||
</div>
|
||||
</div>
|
||||
{expandedIterations[index] && <div
|
||||
className="h-px grow bg-divider-subtle"
|
||||
></div>}
|
||||
<div className={cn(
|
||||
'transition-all duration-200',
|
||||
expandedIterations[index]
|
||||
? 'opacity-100'
|
||||
: 'max-h-0 overflow-hidden opacity-0',
|
||||
)}>
|
||||
<TracingPanel
|
||||
list={iteration}
|
||||
className='bg-background-section-burn'
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
export default React.memo(IterationResultPanel)
|
||||
Reference in New Issue
Block a user