mirror of
http://112.124.100.131/huang.ze/ebiz-dify-ai.git
synced 2025-12-23 01:36:55 +08:00
FEAT: NEW WORKFLOW ENGINE (#3160)
Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Yeuoly <admin@srmxy.cn> Co-authored-by: JzoNg <jzongcode@gmail.com> Co-authored-by: StyleZhang <jasonapring2015@outlook.com> Co-authored-by: jyong <jyong@dify.ai> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: jyong <718720800@qq.com>
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import React, { useCallback, useRef } from 'react'
|
||||
import { useBoolean, useHover } from 'ahooks'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import InputVarTypeIcon from '../../_base/components/input-var-type-icon'
|
||||
import type { InputVar, MoreInfo } from '@/app/components/workflow/types'
|
||||
import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development'
|
||||
import { Edit03 } from '@/app/components/base/icons/src/vender/solid/general'
|
||||
import { Trash03 } from '@/app/components/base/icons/src/vender/line/general'
|
||||
import ConfigVarModal from '@/app/components/app/configuration/config-var/config-modal'
|
||||
|
||||
type Props = {
|
||||
readonly: boolean
|
||||
payload: InputVar
|
||||
onChange?: (item: InputVar, moreInfo?: MoreInfo) => void
|
||||
onRemove?: () => void
|
||||
rightContent?: JSX.Element
|
||||
varKeys?: string[]
|
||||
}
|
||||
|
||||
const VarItem: FC<Props> = ({
|
||||
readonly,
|
||||
payload,
|
||||
onChange = () => { },
|
||||
onRemove = () => { },
|
||||
rightContent,
|
||||
varKeys = [],
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const ref = useRef(null)
|
||||
const isHovering = useHover(ref)
|
||||
const [isShowEditVarModal, {
|
||||
setTrue: showEditVarModal,
|
||||
setFalse: hideEditVarModal,
|
||||
}] = useBoolean(false)
|
||||
|
||||
const handlePayloadChange = useCallback((payload: InputVar, moreInfo?: MoreInfo) => {
|
||||
onChange(payload, moreInfo)
|
||||
hideEditVarModal()
|
||||
}, [onChange, hideEditVarModal])
|
||||
return (
|
||||
<div ref={ref} className='flex items-center h-8 justify-between px-2.5 bg-white rounded-lg border border-gray-200 shadow-xs cursor-pointer hover:shadow-md'>
|
||||
<div className='flex items-center space-x-1 grow w-0'>
|
||||
<Variable02 className='w-3.5 h-3.5 text-primary-500' />
|
||||
<div title={payload.variable} className='shrink-0 max-w-[130px] truncate text-[13px] font-medium text-gray-700'>{payload.variable}</div>
|
||||
{payload.label && (<><div className='shrink-0 text-xs font-medium text-gray-400'>·</div>
|
||||
<div title={payload.label as string} className='max-w-[130px] truncate text-[13px] font-medium text-gray-500'>{payload.label as string}</div>
|
||||
</>)}
|
||||
</div>
|
||||
<div className='shrink-0 ml-2 flex items-center'>
|
||||
{rightContent || (<>
|
||||
{(!isHovering || readonly)
|
||||
? (
|
||||
<>
|
||||
{payload.required && (
|
||||
<div className='mr-2 text-xs font-normal text-gray-500'>{t('workflow.nodes.start.required')}</div>
|
||||
)}
|
||||
<InputVarTypeIcon type={payload.type} className='w-3.5 h-3.5 text-gray-500' />
|
||||
</>
|
||||
)
|
||||
: (!readonly && (
|
||||
<>
|
||||
<div onClick={showEditVarModal} className='mr-1 p-1 rounded-md cursor-pointer hover:bg-black/5'>
|
||||
<Edit03 className='w-4 h-4 text-gray-500' />
|
||||
</div>
|
||||
<div onClick={onRemove} className='p-1 rounded-md cursor-pointer hover:bg-black/5'>
|
||||
<Trash03 className='w-4 h-4 text-gray-500' />
|
||||
</div>
|
||||
</>
|
||||
))}
|
||||
</>)}
|
||||
|
||||
</div>
|
||||
{
|
||||
isShowEditVarModal && (
|
||||
<ConfigVarModal
|
||||
isShow
|
||||
payload={payload}
|
||||
onClose={hideEditVarModal}
|
||||
onConfirm={handlePayloadChange}
|
||||
varKeys={varKeys}
|
||||
/>
|
||||
)
|
||||
}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
export default React.memo(VarItem)
|
||||
@@ -0,0 +1,70 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import React, { useCallback } from 'react'
|
||||
import produce from 'immer'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import VarItem from './var-item'
|
||||
import { ChangeType, type InputVar, type MoreInfo } from '@/app/components/workflow/types'
|
||||
type Props = {
|
||||
readonly: boolean
|
||||
list: InputVar[]
|
||||
onChange: (list: InputVar[], moreInfo?: { index: number; payload: MoreInfo }) => void
|
||||
}
|
||||
|
||||
const VarList: FC<Props> = ({
|
||||
readonly,
|
||||
list,
|
||||
onChange,
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const handleVarChange = useCallback((index: number) => {
|
||||
return (payload: InputVar, moreInfo?: MoreInfo) => {
|
||||
const newList = produce(list, (draft) => {
|
||||
draft[index] = payload
|
||||
})
|
||||
onChange(newList, moreInfo ? { index, payload: moreInfo } : undefined)
|
||||
}
|
||||
}, [list, onChange])
|
||||
|
||||
const handleVarRemove = useCallback((index: number) => {
|
||||
return () => {
|
||||
const newList = produce(list, (draft) => {
|
||||
draft.splice(index, 1)
|
||||
})
|
||||
onChange(newList, {
|
||||
index,
|
||||
payload: {
|
||||
type: ChangeType.remove,
|
||||
payload: {
|
||||
beforeKey: list[index].variable,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
}, [list, onChange])
|
||||
|
||||
if (list.length === 0) {
|
||||
return (
|
||||
<div className='flex rounded-md bg-gray-50 items-center h-[42px] justify-center leading-[18px] text-xs font-normal text-gray-500'>
|
||||
{t('workflow.nodes.start.noVarTip')}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='space-y-1'>
|
||||
{list.map((item, index) => (
|
||||
<VarItem
|
||||
key={index}
|
||||
readonly={readonly}
|
||||
payload={item}
|
||||
onChange={handleVarChange(index)}
|
||||
onRemove={handleVarRemove(index)}
|
||||
varKeys={list.map(item => item.variable)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
export default React.memo(VarList)
|
||||
23
web/app/components/workflow/nodes/start/default.ts
Normal file
23
web/app/components/workflow/nodes/start/default.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import type { NodeDefault } from '../../types'
|
||||
import type { StartNodeType } from './types'
|
||||
import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/constants'
|
||||
|
||||
const nodeDefault: NodeDefault<StartNodeType> = {
|
||||
defaultValue: {
|
||||
variables: [],
|
||||
},
|
||||
getAvailablePrevNodes() {
|
||||
return []
|
||||
},
|
||||
getAvailableNextNodes(isChatMode: boolean) {
|
||||
const nodes = isChatMode ? ALL_CHAT_AVAILABLE_BLOCKS : ALL_COMPLETION_AVAILABLE_BLOCKS
|
||||
return nodes
|
||||
},
|
||||
checkValid() {
|
||||
return {
|
||||
isValid: true,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
export default nodeDefault
|
||||
40
web/app/components/workflow/nodes/start/node.tsx
Normal file
40
web/app/components/workflow/nodes/start/node.tsx
Normal file
@@ -0,0 +1,40 @@
|
||||
import type { FC } from 'react'
|
||||
import React from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import InputVarTypeIcon from '../_base/components/input-var-type-icon'
|
||||
import type { StartNodeType } from './types'
|
||||
import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development'
|
||||
import type { NodeProps } from '@/app/components/workflow/types'
|
||||
const i18nPrefix = 'workflow.nodes.start'
|
||||
|
||||
const Node: FC<NodeProps<StartNodeType>> = ({
|
||||
data,
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
const { variables } = data
|
||||
|
||||
if (!variables.length)
|
||||
return null
|
||||
|
||||
return (
|
||||
<div className='mb-1 px-3 py-1'>
|
||||
<div className='space-y-0.5'>
|
||||
{variables.map(variable => (
|
||||
<div key={variable.variable} className='flex items-center h-6 justify-between bg-gray-100 rounded-md px-1 space-x-1 text-xs font-normal text-gray-700'>
|
||||
<div className='w-0 grow flex items-center space-x-1'>
|
||||
<Variable02 className='shrink-0 w-3.5 h-3.5 text-primary-500' />
|
||||
<span className='w-0 grow truncate text-xs font-normal text-gray-700'>{variable.variable}</span>
|
||||
</div>
|
||||
|
||||
<div className='ml-1 flex items-center space-x-1'>
|
||||
{variable.required && <span className='text-xs font-normal text-gray-500 uppercase'>{t(`${i18nPrefix}.required`)}</span>}
|
||||
<InputVarTypeIcon type={variable.type} className='w-3 h-3 text-gray-500' />
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(Node)
|
||||
109
web/app/components/workflow/nodes/start/panel.tsx
Normal file
109
web/app/components/workflow/nodes/start/panel.tsx
Normal file
@@ -0,0 +1,109 @@
|
||||
import type { FC } from 'react'
|
||||
import React from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import RemoveEffectVarConfirm from '../_base/components/remove-effect-var-confirm'
|
||||
import VarList from './components/var-list'
|
||||
import VarItem from './components/var-item'
|
||||
import useConfig from './use-config'
|
||||
import type { StartNodeType } from './types'
|
||||
import Split from '@/app/components/workflow/nodes/_base/components/split'
|
||||
import Field from '@/app/components/workflow/nodes/_base/components/field'
|
||||
import AddButton from '@/app/components/base/button/add-button'
|
||||
import ConfigVarModal from '@/app/components/app/configuration/config-var/config-modal'
|
||||
import type { InputVar, NodePanelProps } from '@/app/components/workflow/types'
|
||||
|
||||
const i18nPrefix = 'workflow.nodes.start'
|
||||
|
||||
const Panel: FC<NodePanelProps<StartNodeType>> = ({
|
||||
id,
|
||||
data,
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
const {
|
||||
readOnly,
|
||||
isChatMode,
|
||||
inputs,
|
||||
isShowAddVarModal,
|
||||
showAddVarModal,
|
||||
handleAddVariable,
|
||||
hideAddVarModal,
|
||||
handleVarListChange,
|
||||
isShowRemoveVarConfirm,
|
||||
hideRemoveVarConfirm,
|
||||
onRemoveVarConfirm,
|
||||
} = useConfig(id, data)
|
||||
|
||||
const handleAddVarConfirm = (payload: InputVar) => {
|
||||
handleAddVariable(payload)
|
||||
hideAddVarModal()
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='mt-2'>
|
||||
<div className='px-4 pb-2 space-y-4'>
|
||||
<Field
|
||||
title={t(`${i18nPrefix}.inputField`)}
|
||||
operations={
|
||||
!readOnly ? <AddButton onClick={showAddVarModal} /> : undefined
|
||||
}
|
||||
>
|
||||
<>
|
||||
<VarList
|
||||
readonly={readOnly}
|
||||
list={inputs.variables || []}
|
||||
onChange={handleVarListChange}
|
||||
/>
|
||||
|
||||
<div className='mt-1 space-y-1'>
|
||||
<Split className='my-2' />
|
||||
{
|
||||
isChatMode && (
|
||||
<VarItem
|
||||
readonly
|
||||
payload={{
|
||||
variable: 'sys.query',
|
||||
} as any}
|
||||
rightContent={
|
||||
<div className='text-xs font-normal text-gray-500'>
|
||||
String
|
||||
</div>
|
||||
}
|
||||
/>)
|
||||
}
|
||||
<VarItem
|
||||
readonly
|
||||
payload={{
|
||||
variable: 'sys.files',
|
||||
} as any}
|
||||
rightContent={
|
||||
<div className='text-xs font-normal text-gray-500'>
|
||||
Array[File]
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
|
||||
</>
|
||||
</Field>
|
||||
</div>
|
||||
|
||||
{isShowAddVarModal && (
|
||||
<ConfigVarModal
|
||||
isCreate
|
||||
isShow={isShowAddVarModal}
|
||||
onClose={hideAddVarModal}
|
||||
onConfirm={handleAddVarConfirm}
|
||||
varKeys={inputs.variables.map(v => v.variable)}
|
||||
/>
|
||||
)}
|
||||
|
||||
<RemoveEffectVarConfirm
|
||||
isShow={isShowRemoveVarConfirm}
|
||||
onCancel={hideRemoveVarConfirm}
|
||||
onConfirm={onRemoveVarConfirm}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(Panel)
|
||||
5
web/app/components/workflow/nodes/start/types.ts
Normal file
5
web/app/components/workflow/nodes/start/types.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { CommonNodeType, InputVar } from '@/app/components/workflow/types'
|
||||
|
||||
export type StartNodeType = CommonNodeType & {
|
||||
variables: InputVar[]
|
||||
}
|
||||
76
web/app/components/workflow/nodes/start/use-config.ts
Normal file
76
web/app/components/workflow/nodes/start/use-config.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { useCallback, useState } from 'react'
|
||||
import produce from 'immer'
|
||||
import { useBoolean } from 'ahooks'
|
||||
import type { StartNodeType } from './types'
|
||||
import { ChangeType } from '@/app/components/workflow/types'
|
||||
import type { InputVar, MoreInfo, ValueSelector } from '@/app/components/workflow/types'
|
||||
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
|
||||
import {
|
||||
useIsChatMode,
|
||||
useNodesReadOnly,
|
||||
useWorkflow,
|
||||
} from '@/app/components/workflow/hooks'
|
||||
|
||||
const useConfig = (id: string, payload: StartNodeType) => {
|
||||
const { nodesReadOnly: readOnly } = useNodesReadOnly()
|
||||
const { handleOutVarRenameChange, isVarUsedInNodes, removeUsedVarInNodes } = useWorkflow()
|
||||
const isChatMode = useIsChatMode()
|
||||
|
||||
const { inputs, setInputs } = useNodeCrud<StartNodeType>(id, payload)
|
||||
|
||||
const [isShowAddVarModal, {
|
||||
setTrue: showAddVarModal,
|
||||
setFalse: hideAddVarModal,
|
||||
}] = useBoolean(false)
|
||||
|
||||
const [isShowRemoveVarConfirm, {
|
||||
setTrue: showRemoveVarConfirm,
|
||||
setFalse: hideRemoveVarConfirm,
|
||||
}] = useBoolean(false)
|
||||
const [removedVar, setRemovedVar] = useState<ValueSelector>([])
|
||||
const handleVarListChange = useCallback((newList: InputVar[], moreInfo?: { index: number; payload: MoreInfo }) => {
|
||||
if (moreInfo?.payload?.type === ChangeType.remove) {
|
||||
if (isVarUsedInNodes([id, moreInfo?.payload?.payload?.beforeKey || ''])) {
|
||||
showRemoveVarConfirm()
|
||||
setRemovedVar([id, moreInfo?.payload?.payload?.beforeKey || ''])
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
const newInputs = produce(inputs, (draft: any) => {
|
||||
draft.variables = newList
|
||||
})
|
||||
setInputs(newInputs)
|
||||
if (moreInfo?.payload?.type === ChangeType.changeVarName) {
|
||||
const changedVar = newList[moreInfo.index]
|
||||
handleOutVarRenameChange(id, [id, inputs.variables[moreInfo.index].variable], [id, changedVar.variable])
|
||||
}
|
||||
}, [handleOutVarRenameChange, id, inputs, isVarUsedInNodes, setInputs, showRemoveVarConfirm])
|
||||
|
||||
const removeVarInNode = useCallback(() => {
|
||||
removeUsedVarInNodes(removedVar)
|
||||
hideRemoveVarConfirm()
|
||||
}, [hideRemoveVarConfirm, removeUsedVarInNodes, removedVar])
|
||||
|
||||
const handleAddVariable = useCallback((payload: InputVar) => {
|
||||
const newInputs = produce(inputs, (draft: StartNodeType) => {
|
||||
draft.variables.push(payload)
|
||||
})
|
||||
setInputs(newInputs)
|
||||
}, [inputs, setInputs])
|
||||
return {
|
||||
readOnly,
|
||||
isChatMode,
|
||||
inputs,
|
||||
isShowAddVarModal,
|
||||
showAddVarModal,
|
||||
hideAddVarModal,
|
||||
handleVarListChange,
|
||||
handleAddVariable,
|
||||
isShowRemoveVarConfirm,
|
||||
hideRemoveVarConfirm,
|
||||
onRemoveVarConfirm: removeVarInNode,
|
||||
}
|
||||
}
|
||||
|
||||
export default useConfig
|
||||
5
web/app/components/workflow/nodes/start/utils.ts
Normal file
5
web/app/components/workflow/nodes/start/utils.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { StartNodeType } from './types'
|
||||
|
||||
export const checkNodeValid = (payload: StartNodeType) => {
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user