mirror of
https://github.com/langgenius/dify.git
synced 2026-03-27 17:19:55 +08:00
132 lines
4.2 KiB
TypeScript
132 lines
4.2 KiB
TypeScript
import type { InputVar, Node, ValueSelector, Variable } from '../../types'
|
|
import type { LoopNodeType } from './types'
|
|
import type { NodeTracing } from '@/types/workflow'
|
|
import { useCallback, useContext, useMemo } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { WorkflowContext } from '@/app/components/workflow/context'
|
|
import formatTracing from '@/app/components/workflow/run/utils/format-log'
|
|
import { ValueType } from '@/app/components/workflow/types'
|
|
import { useIsNodeInLoop, useWorkflow } from '../../hooks'
|
|
import {
|
|
buildUsedOutVars,
|
|
createInputVarValues,
|
|
dedupeInputVars,
|
|
getDependentVarsFromLoopPayload,
|
|
getVarSelectorsFromCondition,
|
|
} from './use-single-run-form-params.helpers'
|
|
|
|
type Params = {
|
|
id: string
|
|
payload: LoopNodeType
|
|
runInputData: Record<string, any>
|
|
runResult: NodeTracing
|
|
loopRunResult: NodeTracing[]
|
|
setRunInputData: (data: Record<string, any>) => void
|
|
toVarInputs: (variables: Variable[]) => InputVar[]
|
|
varSelectorsToVarInputs: (variables: ValueSelector[]) => InputVar[]
|
|
}
|
|
|
|
const useSingleRunFormParams = ({
|
|
id,
|
|
payload,
|
|
runInputData,
|
|
runResult,
|
|
loopRunResult,
|
|
setRunInputData,
|
|
toVarInputs,
|
|
varSelectorsToVarInputs,
|
|
}: Params) => {
|
|
const { t } = useTranslation()
|
|
|
|
const { isNodeInLoop } = useIsNodeInLoop(id)
|
|
|
|
const { getLoopNodeChildren, getBeforeNodesInSameBranch } = useWorkflow()
|
|
const workflowStore = useContext(WorkflowContext)
|
|
const parentAvailableNodes = workflowStore?.getState().parentAvailableNodes || []
|
|
const loopChildrenNodes = getLoopNodeChildren(id)
|
|
const beforeNodes = useMemo(() => {
|
|
const baseBeforeNodes = getBeforeNodesInSameBranch(id)
|
|
if (!parentAvailableNodes.length)
|
|
return baseBeforeNodes
|
|
const merged = new Map<string, Node>()
|
|
baseBeforeNodes.forEach((node) => {
|
|
merged.set(node.id, node)
|
|
})
|
|
parentAvailableNodes.forEach((node) => {
|
|
if (!merged.has(node.id))
|
|
merged.set(node.id, node)
|
|
})
|
|
return Array.from(merged.values())
|
|
}, [getBeforeNodesInSameBranch, id, parentAvailableNodes])
|
|
const canChooseVarNodes = useMemo(() => [...beforeNodes, ...loopChildrenNodes], [beforeNodes, loopChildrenNodes])
|
|
|
|
const { usedOutVars, allVarObject } = useMemo(() => buildUsedOutVars({
|
|
loopChildrenNodes,
|
|
currentNodeId: id,
|
|
canChooseVarNodes,
|
|
isNodeInLoop,
|
|
toVarInputs,
|
|
}), [loopChildrenNodes, id, canChooseVarNodes, isNodeInLoop, toVarInputs])
|
|
|
|
const nodeInfo = useMemo(() => {
|
|
const formattedNodeInfo = formatTracing(loopRunResult, t)[0]
|
|
|
|
if (runResult && formattedNodeInfo) {
|
|
return {
|
|
...formattedNodeInfo,
|
|
execution_metadata: {
|
|
...runResult.execution_metadata,
|
|
...formattedNodeInfo.execution_metadata,
|
|
},
|
|
}
|
|
}
|
|
|
|
return formattedNodeInfo
|
|
}, [runResult, loopRunResult, t])
|
|
|
|
const setInputVarValues = useCallback((newPayload: Record<string, any>) => {
|
|
setRunInputData(newPayload)
|
|
}, [setRunInputData])
|
|
|
|
const inputVarValues = useMemo(() => createInputVarValues(runInputData), [runInputData])
|
|
|
|
const forms = useMemo(() => {
|
|
const allInputs: ValueSelector[] = []
|
|
payload.break_conditions?.forEach((condition) => {
|
|
const vars = getVarSelectorsFromCondition(condition)
|
|
allInputs.push(...vars)
|
|
})
|
|
|
|
payload.loop_variables?.forEach((loopVariable) => {
|
|
if (loopVariable.value_type === ValueType.variable)
|
|
allInputs.push(loopVariable.value)
|
|
})
|
|
const inputVarsFromValue: InputVar[] = []
|
|
const varInputs = [...varSelectorsToVarInputs(allInputs), ...inputVarsFromValue]
|
|
const uniqueVarInputs = dedupeInputVars(varInputs)
|
|
return [
|
|
{
|
|
inputs: [...usedOutVars, ...uniqueVarInputs],
|
|
values: inputVarValues,
|
|
onChange: setInputVarValues,
|
|
},
|
|
]
|
|
}, [payload.break_conditions, payload.loop_variables, varSelectorsToVarInputs, usedOutVars, inputVarValues, setInputVarValues])
|
|
|
|
const getDependentVars = useCallback(() => getDependentVarsFromLoopPayload({
|
|
nodeId: id,
|
|
usedOutVars,
|
|
breakConditions: payload.break_conditions,
|
|
loopVariables: payload.loop_variables,
|
|
}), [id, usedOutVars, payload.break_conditions, payload.loop_variables])
|
|
|
|
return {
|
|
forms,
|
|
nodeInfo,
|
|
allVarObject,
|
|
getDependentVars,
|
|
}
|
|
}
|
|
|
|
export default useSingleRunFormParams
|