@ -3,7 +3,6 @@ import {
useRef ,
useRef ,
} from 'react'
} from 'react'
import produce from 'immer'
import produce from 'immer'
import { useBoolean } from 'ahooks'
import { v4 as uuid4 } from 'uuid'
import { v4 as uuid4 } from 'uuid'
import {
import {
useIsChatMode ,
useIsChatMode ,
@ -12,10 +11,9 @@ import {
useWorkflow ,
useWorkflow ,
} from '../../hooks'
} from '../../hooks'
import { ValueType , VarType } from '../../types'
import { ValueType , VarType } from '../../types'
import type { ErrorHandleMode , Va lueSelector, Va r } from '../../types'
import type { ErrorHandleMode , Va r } from '../../types'
import useNodeCrud from '../_base/hooks/use-node-crud'
import useNodeCrud from '../_base/hooks/use-node-crud'
import { getNodeInfoById , getNodeUsedVarPassToServerKey , getNodeUsedVars , isSystemVar , toNodeOutputVars } from '../_base/components/variable/utils'
import { toNodeOutputVars } from '../_base/components/variable/utils'
import useOneStepRun from '../_base/hooks/use-one-step-run'
import { getOperators } from './utils'
import { getOperators } from './utils'
import { LogicalOperator } from './types'
import { LogicalOperator } from './types'
import type { HandleAddCondition , HandleAddSubVariableCondition , HandleRemoveCondition , HandleToggleConditionLogicalOperator , HandleToggleSubVariableConditionLogicalOperator , HandleUpdateCondition , HandleUpdateSubVariableCondition , LoopNodeType } from './types'
import type { HandleAddCondition , HandleAddSubVariableCondition , HandleRemoveCondition , HandleToggleConditionLogicalOperator , HandleToggleSubVariableConditionLogicalOperator , HandleUpdateCondition , HandleUpdateSubVariableCondition , LoopNodeType } from './types'
@ -47,137 +45,12 @@ const useConfig = (id: string, payload: LoopNodeType) => {
const canChooseVarNodes = [ . . . beforeNodes , . . . loopChildrenNodes ]
const canChooseVarNodes = [ . . . beforeNodes , . . . loopChildrenNodes ]
const childrenNodeVars = toNodeOutputVars ( loopChildrenNodes , isChatMode , undefined , [ ] , conversationVariables )
const childrenNodeVars = toNodeOutputVars ( loopChildrenNodes , isChatMode , undefined , [ ] , conversationVariables )
// single run
const loopInputKey = ` ${ id } .input_selector `
const {
showSingleRun ,
hideSingleRun ,
toVarInputs ,
handleRun : doHandleRun ,
runInputData ,
setRunInputData ,
runResult ,
loopRunResult ,
} = useOneStepRun < LoopNodeType > ( {
id ,
data : inputs ,
loopInputKey ,
defaultRunInputData : {
[ loopInputKey ] : [ '' ] ,
} ,
} )
const [ isShowLoopDetail , {
setTrue : doShowLoopDetail ,
setFalse : doHideLoopDetail ,
} ] = useBoolean ( false )
const hideLoopDetail = useCallback ( ( ) = > {
hideSingleRun ( )
doHideLoopDetail ( )
} , [ doHideLoopDetail , hideSingleRun ] )
const showLoopDetail = useCallback ( ( ) = > {
doShowLoopDetail ( )
} , [ doShowLoopDetail ] )
const backToSingleRun = useCallback ( ( ) = > {
hideLoopDetail ( )
showSingleRun ( )
} , [ hideLoopDetail , showSingleRun ] )
const {
const {
getIsVarFileAttribute ,
getIsVarFileAttribute ,
} = useIsVarFileAttribute ( {
} = useIsVarFileAttribute ( {
nodeId : id ,
nodeId : id ,
} )
} )
const { usedOutVars , allVarObject } = ( ( ) = > {
const vars : ValueSelector [ ] = [ ]
const varObjs : Record < string , boolean > = { }
const allVarObject : Record < string , {
inSingleRunPassedKey : string
} > = { }
loopChildrenNodes . forEach ( ( node ) = > {
const nodeVars = getNodeUsedVars ( node ) . filter ( item = > item && item . length > 0 )
nodeVars . forEach ( ( varSelector ) = > {
if ( varSelector [ 0 ] === id ) { // skip Loop node itself variable: item, index
return
}
const isInLoop = isNodeInLoop ( varSelector [ 0 ] )
if ( isInLoop ) // not pass loop inner variable
return
const varSectorStr = varSelector . join ( '.' )
if ( ! varObjs [ varSectorStr ] ) {
varObjs [ varSectorStr ] = true
vars . push ( varSelector )
}
let passToServerKeys = getNodeUsedVarPassToServerKey ( node , varSelector )
if ( typeof passToServerKeys === 'string' )
passToServerKeys = [ passToServerKeys ]
passToServerKeys . forEach ( ( key : string , index : number ) = > {
allVarObject [ [ varSectorStr , node . id , index ] . join ( DELIMITER ) ] = {
inSingleRunPassedKey : key ,
}
} )
} )
} )
const res = toVarInputs ( vars . map ( ( item ) = > {
const varInfo = getNodeInfoById ( canChooseVarNodes , item [ 0 ] )
return {
label : {
nodeType : varInfo?.data.type ,
nodeName : varInfo?.data.title || canChooseVarNodes [ 0 ] ? . data . title , // default start node title
variable : isSystemVar ( item ) ? item . join ( '.' ) : item [ item . length - 1 ] ,
} ,
variable : ` ${ item . join ( '.' ) } ` ,
value_selector : item ,
}
} ) )
return {
usedOutVars : res ,
allVarObject ,
}
} ) ( )
const handleRun = useCallback ( ( data : Record < string , any > ) = > {
const formattedData : Record < string , any > = { }
Object . keys ( allVarObject ) . forEach ( ( key ) = > {
const [ varSectorStr , nodeId ] = key . split ( DELIMITER )
formattedData [ ` ${ nodeId } . ${ allVarObject [ key ] . inSingleRunPassedKey } ` ] = data [ varSectorStr ]
} )
formattedData [ loopInputKey ] = data [ loopInputKey ]
doHandleRun ( formattedData )
} , [ allVarObject , doHandleRun , loopInputKey ] )
const inputVarValues = ( ( ) = > {
const vars : Record < string , any > = { }
Object . keys ( runInputData )
. filter ( key = > ! [ loopInputKey ] . includes ( key ) )
. forEach ( ( key ) = > {
vars [ key ] = runInputData [ key ]
} )
return vars
} ) ( )
const setInputVarValues = useCallback ( ( newPayload : Record < string , any > ) = > {
const newVars = {
. . . newPayload ,
[ loopInputKey ] : runInputData [ loopInputKey ] ,
}
setRunInputData ( newVars )
} , [ loopInputKey , runInputData , setRunInputData ] )
const loop = runInputData [ loopInputKey ]
const setLoop = useCallback ( ( newLoop : string [ ] ) = > {
setRunInputData ( {
. . . runInputData ,
[ loopInputKey ] : newLoop ,
} )
} , [ loopInputKey , runInputData , setRunInputData ] )
const changeErrorResponseMode = useCallback ( ( item : { value : unknown } ) = > {
const changeErrorResponseMode = useCallback ( ( item : { value : unknown } ) = > {
const newInputs = produce ( inputs , ( draft ) = > {
const newInputs = produce ( inputs , ( draft ) = > {
draft . error_handle_mode = item . value as ErrorHandleMode
draft . error_handle_mode = item . value as ErrorHandleMode
@ -339,19 +212,6 @@ const useConfig = (id: string, payload: LoopNodeType) => {
filterInputVar ,
filterInputVar ,
childrenNodeVars ,
childrenNodeVars ,
loopChildrenNodes ,
loopChildrenNodes ,
showSingleRun ,
isShowLoopDetail ,
showLoopDetail ,
hideLoopDetail ,
backToSingleRun ,
runResult ,
inputVarValues ,
setInputVarValues ,
usedOutVars ,
loop ,
setLoop ,
loopInputKey ,
loopRunResult ,
handleAddCondition ,
handleAddCondition ,
handleRemoveCondition ,
handleRemoveCondition ,
handleUpdateCondition ,
handleUpdateCondition ,