chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
+69
@@ -0,0 +1,69 @@
|
||||
import type { LoopDurationMap, LoopVariableMap } from '@/types/workflow'
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { createNodeTracing as createTrace } from '../../../__tests__/fixtures'
|
||||
import { BlockEnum } from '../../../types'
|
||||
import LoopLogTrigger from '../loop-log-trigger'
|
||||
|
||||
const createNodeTracing = (overrides: Parameters<typeof createTrace>[0] = {}) =>
|
||||
createTrace({
|
||||
node_id: 'loop-node',
|
||||
node_type: BlockEnum.Loop,
|
||||
title: 'Loop',
|
||||
status: 'succeeded',
|
||||
execution_metadata: { total_tokens: 0, total_price: 0, currency: 'USD' },
|
||||
...overrides,
|
||||
})
|
||||
|
||||
describe('LoopLogTrigger', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
// Loop triggers should summarize count/error status and forward structured details.
|
||||
describe('Structured Detail Handling', () => {
|
||||
it('should pass existing loop details, durations, and variables to the callback', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onShowLoopResultList =
|
||||
vi.fn<NonNullable<React.ComponentProps<typeof LoopLogTrigger>['onShowLoopResultList']>>()
|
||||
const onParentClick = vi.fn()
|
||||
const detailList = [
|
||||
[createNodeTracing({ id: 'loop-1-step-1', status: 'succeeded' })],
|
||||
[createNodeTracing({ id: 'loop-2-step-1', status: 'failed' })],
|
||||
]
|
||||
const loopDurationMap: LoopDurationMap = { 0: 1.2, 1: 2.5 }
|
||||
const loopVariableMap: LoopVariableMap = { 1: { item: 'alpha' } }
|
||||
|
||||
render(
|
||||
<div onClick={onParentClick}>
|
||||
<LoopLogTrigger
|
||||
nodeInfo={createNodeTracing({
|
||||
details: detailList,
|
||||
loopDurationMap,
|
||||
execution_metadata: {
|
||||
total_tokens: 0,
|
||||
total_price: 0,
|
||||
currency: 'USD',
|
||||
loop_duration_map: loopDurationMap,
|
||||
loop_variable_map: loopVariableMap,
|
||||
},
|
||||
})}
|
||||
onShowLoopResultList={onShowLoopResultList}
|
||||
/>
|
||||
</div>,
|
||||
)
|
||||
|
||||
expect(screen.getByText(/workflowLogic\.nodes\.loop\.loop/))!.toBeInTheDocument()
|
||||
expect(screen.getByText(/workflowLogic\.nodes\.loop\.error/))!.toBeInTheDocument()
|
||||
|
||||
await user.click(screen.getByRole('button'))
|
||||
|
||||
expect(onParentClick).not.toHaveBeenCalled()
|
||||
expect(onShowLoopResultList).toHaveBeenCalledWith(
|
||||
detailList,
|
||||
loopDurationMap,
|
||||
loopVariableMap,
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
import { screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { NodeRunningStatus } from '@/app/components/workflow/types'
|
||||
import { renderWithConsoleQuery as render } from '@/test/console/query-data'
|
||||
import { createNodeTracing } from '../../../__tests__/fixtures'
|
||||
import LoopResultPanel from '../loop-result-panel'
|
||||
|
||||
it('expands and collapses loop details with the keyboard and returns to the summary', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onBack = vi.fn()
|
||||
render(
|
||||
<LoopResultPanel
|
||||
list={[
|
||||
[
|
||||
createNodeTracing({
|
||||
title: 'Summarize item',
|
||||
status: NodeRunningStatus.Succeeded,
|
||||
execution_metadata: { total_tokens: 0, total_price: 0, currency: 'USD', loop_index: 0 },
|
||||
}),
|
||||
],
|
||||
]}
|
||||
onBack={onBack}
|
||||
loopDurationMap={{ 0: 1.2 }}
|
||||
loopVariableMap={{ 0: { item: 'alpha' } }}
|
||||
/>,
|
||||
)
|
||||
const header = screen.getByRole('button', { name: /workflow.singleRun.loop 1/ })
|
||||
expect(header).toHaveTextContent('1.20s')
|
||||
expect(header).toHaveAttribute('aria-expanded', 'false')
|
||||
expect(screen.queryByRole('textbox')).not.toBeInTheDocument()
|
||||
header.focus()
|
||||
await user.keyboard('{Enter}')
|
||||
expect(header).toHaveAttribute('aria-expanded', 'true')
|
||||
expect(screen.getByRole('textbox')).toHaveAttribute('readonly')
|
||||
expect(JSON.parse((screen.getByRole('textbox') as HTMLTextAreaElement).value)).toEqual({
|
||||
item: 'alpha',
|
||||
})
|
||||
expect(screen.getByText('Summarize item')).toBeInTheDocument()
|
||||
await user.keyboard(' ')
|
||||
expect(header).toHaveAttribute('aria-expanded', 'false')
|
||||
expect(screen.queryByRole('textbox')).not.toBeInTheDocument()
|
||||
await user.click(screen.getByRole('button', { name: 'workflow.singleRun.back' }))
|
||||
expect(onBack).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it.each([
|
||||
{ metadata: { loop_index: 2 }, variables: { 2: { item: 'alpha' } }, expected: 'alpha' },
|
||||
{
|
||||
metadata: { loop_index: 2, parallel_mode_run_id: 'parallel-1' },
|
||||
variables: { 2: { item: 'wrong' }, 'parallel-1': { item: 'beta' } },
|
||||
expected: 'beta',
|
||||
},
|
||||
])(
|
||||
'resolves compacted loop rows using execution metadata $metadata',
|
||||
async ({ metadata, variables, expected }) => {
|
||||
const user = userEvent.setup()
|
||||
render(
|
||||
<LoopResultPanel
|
||||
list={[
|
||||
[
|
||||
createNodeTracing({
|
||||
execution_metadata: { total_tokens: 0, total_price: 0, currency: 'USD', ...metadata },
|
||||
}),
|
||||
],
|
||||
]}
|
||||
onBack={vi.fn()}
|
||||
loopVariableMap={variables}
|
||||
/>,
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: /workflow.singleRun.loop 1/ }))
|
||||
expect(screen.getByRole('textbox')).toHaveAttribute('readonly')
|
||||
expect(JSON.parse((screen.getByRole('textbox') as HTMLTextAreaElement).value)).toEqual({
|
||||
item: expected,
|
||||
})
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,2 @@
|
||||
export { default as LoopLogTrigger } from './loop-log-trigger'
|
||||
export { default as LoopResultPanel } from './loop-result-panel'
|
||||
@@ -0,0 +1,65 @@
|
||||
import type { LoopDurationMap, LoopVariableMap, NodeTracing } from '@/types/workflow'
|
||||
import { Button } from '@langgenius/dify-ui/button'
|
||||
import { RiArrowRightSLine } from '@remixicon/react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
type LoopLogTriggerProps = {
|
||||
nodeInfo: NodeTracing
|
||||
onShowLoopResultList: (
|
||||
loopResultList: NodeTracing[][],
|
||||
loopResultDurationMap: LoopDurationMap,
|
||||
loopVariableMap: LoopVariableMap,
|
||||
) => void
|
||||
}
|
||||
const LoopLogTrigger = ({ nodeInfo, onShowLoopResultList }: LoopLogTriggerProps) => {
|
||||
const { t } = useTranslation(['workflow', 'workflowLogic'])
|
||||
|
||||
const handleOnShowLoopDetail = (e: React.MouseEvent<HTMLButtonElement>) => {
|
||||
e.stopPropagation()
|
||||
e.nativeEvent.stopImmediatePropagation()
|
||||
const loopNodeMeta = nodeInfo.execution_metadata
|
||||
const loopDurMap = nodeInfo?.loopDurationMap || loopNodeMeta?.loop_duration_map || {}
|
||||
const loopVarMap = loopNodeMeta?.loop_variable_map || {}
|
||||
|
||||
onShowLoopResultList(nodeInfo.details ?? [], loopDurMap, loopVarMap)
|
||||
}
|
||||
|
||||
let displayLoopCount = 0
|
||||
const loopMap = nodeInfo.execution_metadata?.loop_duration_map
|
||||
if (loopMap) displayLoopCount = Object.keys(loopMap).length
|
||||
else if (nodeInfo.details?.length) displayLoopCount = nodeInfo.details.length
|
||||
else if (nodeInfo.metadata?.loop_length) displayLoopCount = nodeInfo.metadata.loop_length
|
||||
|
||||
const getErrorCount = (details: NodeTracing[][] | undefined) => {
|
||||
if (!details || details.length === 0) return 0
|
||||
return details.reduce((acc, loop) => {
|
||||
if (loop.some((item) => item.status === 'failed')) acc++
|
||||
return acc
|
||||
}, 0)
|
||||
}
|
||||
const errorCount = getErrorCount(nodeInfo.details)
|
||||
|
||||
return (
|
||||
<Button
|
||||
className="flex w-full cursor-pointer items-center self-stretch rounded-lg bg-components-button-tertiary-bg-hover px-3 py-2 inset-ring-0 hover:bg-components-button-tertiary-bg-hover"
|
||||
onClick={handleOnShowLoopDetail}
|
||||
>
|
||||
<span
|
||||
aria-hidden
|
||||
className="i-custom-vender-workflow-loop size-4 shrink-0 text-components-button-tertiary-text"
|
||||
/>
|
||||
<div className="flex-1 text-left system-sm-medium text-components-button-tertiary-text">
|
||||
{t(($) => $['nodes.loop.loop'], { ns: 'workflowLogic', count: displayLoopCount })}
|
||||
{errorCount > 0 && (
|
||||
<>
|
||||
{t(($) => $['nodes.loop.comma'], { ns: 'workflowLogic' })}
|
||||
{t(($) => $['nodes.loop.error'], { ns: 'workflowLogic', count: errorCount })}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
<RiArrowRightSLine className="size-4 shrink-0 text-components-button-tertiary-text" />
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
||||
export default LoopLogTrigger
|
||||
@@ -0,0 +1,183 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import type { ExternalTraceRun } from '../external-trace-row'
|
||||
import type { LoopDurationMap, LoopVariableMap, NodeTracing } from '@/types/workflow'
|
||||
import { cn } from '@langgenius/dify-ui/cn'
|
||||
import {
|
||||
RiArrowLeftLine,
|
||||
RiArrowRightSLine,
|
||||
RiErrorWarningLine,
|
||||
RiLoader2Line,
|
||||
} from '@remixicon/react'
|
||||
import * as React from 'react'
|
||||
import { useCallback, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
|
||||
import { CodeLanguage } from '@/app/components/workflow/nodes/code/types'
|
||||
import TracingPanel from '@/app/components/workflow/run/tracing-panel'
|
||||
import { NodeRunningStatus } from '@/app/components/workflow/types'
|
||||
|
||||
const i18nPrefix = 'singleRun'
|
||||
|
||||
const getLoopRunKey = (loop: NodeTracing[], fallbackIndex: number) => {
|
||||
const executionMetadata = loop[0]?.execution_metadata
|
||||
|
||||
if (executionMetadata?.parallel_mode_run_id !== undefined)
|
||||
return executionMetadata.parallel_mode_run_id
|
||||
|
||||
if (executionMetadata?.loop_index !== undefined) return String(executionMetadata.loop_index)
|
||||
|
||||
return String(fallbackIndex)
|
||||
}
|
||||
|
||||
type Props = {
|
||||
readonly list: NodeTracing[][]
|
||||
readonly onBack: () => void
|
||||
readonly loopDurationMap?: LoopDurationMap
|
||||
readonly loopVariableMap?: LoopVariableMap
|
||||
readonly externalTraceRun?: ExternalTraceRun
|
||||
}
|
||||
|
||||
const LoopResultPanel: FC<Props> = ({
|
||||
list,
|
||||
onBack,
|
||||
loopDurationMap,
|
||||
loopVariableMap,
|
||||
externalTraceRun,
|
||||
}) => {
|
||||
const { t } = useTranslation(['workflow', 'workflowLogic'])
|
||||
const [expandedLoops, setExpandedLoops] = useState<Record<number, boolean>>({})
|
||||
|
||||
const toggleLoop = useCallback((index: number) => {
|
||||
setExpandedLoops((prev) => ({
|
||||
...prev,
|
||||
[index]: !prev[index],
|
||||
}))
|
||||
}, [])
|
||||
|
||||
const countLoopDuration = (
|
||||
loop: NodeTracing[],
|
||||
index: number,
|
||||
loopDurationMap: LoopDurationMap,
|
||||
): string => {
|
||||
const loopItem = loopDurationMap[getLoopRunKey(loop, index)]
|
||||
const duration = loopItem
|
||||
return `${duration && duration > 0.01 ? duration.toFixed(2) : 0.01}s`
|
||||
}
|
||||
|
||||
const loopStatusShow = (
|
||||
index: number,
|
||||
loop: NodeTracing[],
|
||||
loopDurationMap?: LoopDurationMap,
|
||||
) => {
|
||||
const hasFailed = loop.some((item) => item.status === NodeRunningStatus.Failed)
|
||||
const isRunning = loop.some((item) => item.status === NodeRunningStatus.Running)
|
||||
const hasDurationMap = loopDurationMap && Object.keys(loopDurationMap).length !== 0
|
||||
|
||||
if (hasFailed) return <RiErrorWarningLine className="size-4 text-text-destructive" />
|
||||
|
||||
if (isRunning) return <RiLoader2Line className="size-3.5 animate-spin text-primary-600" />
|
||||
|
||||
return (
|
||||
<>
|
||||
{hasDurationMap && (
|
||||
<div className="system-xs-regular text-text-tertiary">
|
||||
{countLoopDuration(loop, index, loopDurationMap)}
|
||||
</div>
|
||||
)}
|
||||
<RiArrowRightSLine
|
||||
className={cn(
|
||||
'size-4 shrink-0 text-text-tertiary transition-transform duration-200',
|
||||
expandedLoops[index] && 'rotate-90',
|
||||
)}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="bg-components-panel-bg">
|
||||
<button
|
||||
type="button"
|
||||
className="flex h-8 w-full 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 size-4" />
|
||||
<div className="system-sm-medium">
|
||||
{t(($) => $[`${i18nPrefix}.back`], { ns: 'workflow' })}
|
||||
</div>
|
||||
</button>
|
||||
{/* List */}
|
||||
<div className="bg-components-panel-bg p-2">
|
||||
{list.map((loop, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className={cn('mb-1 overflow-hidden rounded-xl border-none bg-background-section-burn')}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
aria-expanded={!!expandedLoops[index]}
|
||||
className={cn(
|
||||
'flex w-full cursor-pointer items-center justify-between px-3',
|
||||
expandedLoops[index] ? 'pt-3 pb-2' : 'py-3',
|
||||
'rounded-xl text-left',
|
||||
)}
|
||||
onClick={() => toggleLoop(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">
|
||||
<span
|
||||
aria-hidden
|
||||
className="i-custom-vender-workflow-loop size-3 text-text-primary-on-surface"
|
||||
/>
|
||||
</div>
|
||||
<span className="grow system-sm-semibold-uppercase text-text-primary">
|
||||
{t(($) => $[`${i18nPrefix}.loop`], { ns: 'workflow' })} {index + 1}
|
||||
</span>
|
||||
{loopStatusShow(index, loop, loopDurationMap)}
|
||||
</div>
|
||||
</button>
|
||||
{expandedLoops[index] && <div className="h-px grow bg-divider-subtle"></div>}
|
||||
<div
|
||||
inert={!expandedLoops[index]}
|
||||
aria-hidden={!expandedLoops[index]}
|
||||
className={cn(
|
||||
'transition-all duration-200',
|
||||
expandedLoops[index] ? 'opacity-100' : 'max-h-0 overflow-hidden opacity-0',
|
||||
)}
|
||||
>
|
||||
{loopVariableMap?.[getLoopRunKey(loop, index)] && (
|
||||
<div className="p-2 pb-0">
|
||||
<CodeEditor
|
||||
readOnly
|
||||
title={
|
||||
<div>
|
||||
{t(($) => $['nodes.loop.loopVariables'], {
|
||||
ns: 'workflowLogic',
|
||||
}).toLocaleUpperCase()}
|
||||
</div>
|
||||
}
|
||||
language={CodeLanguage.json}
|
||||
height={112}
|
||||
value={loopVariableMap[getLoopRunKey(loop, index)]}
|
||||
isJSONStringifyBeauty
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<TracingPanel
|
||||
list={loop}
|
||||
className="bg-background-section-burn"
|
||||
externalTraceRun={externalTraceRun}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
export default React.memo(LoopResultPanel)
|
||||
Reference in new issue
Block a user