mirror of https://github.com/langgenius/dify.git
feat: question classify init
This commit is contained in:
parent
e3c65c072c
commit
777cca1a09
|
|
@ -41,6 +41,7 @@ const Panel: FC<NodePanelProps<CodeNodeType>> = ({
|
|||
handleCodeLanguageChange,
|
||||
handleVarsChange,
|
||||
handleAddOutputVariable,
|
||||
filterVar,
|
||||
// single run
|
||||
isShowSingleRun,
|
||||
hideSingleRun,
|
||||
|
|
@ -67,6 +68,7 @@ const Panel: FC<NodePanelProps<CodeNodeType>> = ({
|
|||
nodeId={id}
|
||||
list={inputs.variables}
|
||||
onChange={handleVarListChange}
|
||||
filterVar={filterVar}
|
||||
/>
|
||||
</Field>
|
||||
<Split />
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ import { useCallback } from 'react'
|
|||
import produce from 'immer'
|
||||
import useVarList from '../_base/hooks/use-var-list'
|
||||
import useOutputVarList from '../_base/hooks/use-output-var-list'
|
||||
import { VarType } from '../../types'
|
||||
import type { Var } from '../../types'
|
||||
import type { CodeLanguage, CodeNodeType } from './types'
|
||||
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
|
||||
import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run'
|
||||
|
|
@ -32,6 +34,10 @@ const useConfig = (id: string, payload: CodeNodeType) => {
|
|||
setInputs,
|
||||
})
|
||||
|
||||
const filterVar = useCallback((varPayload: Var) => {
|
||||
return [VarType.string, VarType.number, VarType.object, VarType.array, VarType.arrayNumber, VarType.arrayString, VarType.arrayObject].includes(varPayload.type)
|
||||
}, [])
|
||||
|
||||
// single run
|
||||
const {
|
||||
isShowSingleRun,
|
||||
|
|
@ -72,6 +78,7 @@ const useConfig = (id: string, payload: CodeNodeType) => {
|
|||
handleCodeChange,
|
||||
handleCodeLanguageChange,
|
||||
handleVarsChange,
|
||||
filterVar,
|
||||
handleAddOutputVariable,
|
||||
// single run
|
||||
isShowSingleRun,
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import VarReferencePicker from '../../_base/components/variable/var-reference-pi
|
|||
import { isComparisonOperatorNeedTranslate } from '../utils'
|
||||
import type { Condition } from '@/app/components/workflow/nodes/if-else/types'
|
||||
import { ComparisonOperator, LogicalOperator } from '@/app/components/workflow/nodes/if-else/types'
|
||||
import type { ValueSelector } from '@/app/components/workflow/types'
|
||||
import type { ValueSelector, Var } from '@/app/components/workflow/types'
|
||||
import { Trash03 } from '@/app/components/base/icons/src/vender/line/general'
|
||||
import { RefreshCw05 } from '@/app/components/base/icons/src/vender/line/arrows'
|
||||
import Selector from '@/app/components/workflow/nodes/_base/components/selector'
|
||||
|
|
@ -73,6 +73,7 @@ type ItemProps = {
|
|||
isShowLogicalOperator?: boolean
|
||||
logicalOperator: LogicalOperator
|
||||
onLogicalOperatorToggle: () => void
|
||||
filterVar: (varPayload: Var) => boolean
|
||||
}
|
||||
|
||||
const Item: FC<ItemProps> = ({
|
||||
|
|
@ -85,14 +86,15 @@ const Item: FC<ItemProps> = ({
|
|||
isShowLogicalOperator,
|
||||
logicalOperator,
|
||||
onLogicalOperatorToggle,
|
||||
filterVar,
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
const isValueReadOnly = [ComparisonOperator.empty, ComparisonOperator.notEmpty, ComparisonOperator.isNull, ComparisonOperator.isNotNull].includes(payload.comparison_operator)
|
||||
|
||||
const handleVarReferenceChange = useCallback((value: ValueSelector) => {
|
||||
const handleVarReferenceChange = useCallback((value: ValueSelector | string) => {
|
||||
onChange({
|
||||
...payload,
|
||||
variable_selector: value,
|
||||
variable_selector: value as ValueSelector,
|
||||
})
|
||||
// TODO: handle value type change will effect the comparisonOperators
|
||||
}, [onChange, payload])
|
||||
|
|
@ -140,6 +142,7 @@ const Item: FC<ItemProps> = ({
|
|||
className='grow'
|
||||
value={payload.variable_selector}
|
||||
onChange={handleVarReferenceChange}
|
||||
filterVar={filterVar}
|
||||
/>
|
||||
|
||||
<Selector
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import type { FC } from 'react'
|
|||
import React, { useCallback } from 'react'
|
||||
import produce from 'immer'
|
||||
import cn from 'classnames'
|
||||
import type { Var } from '../../../types'
|
||||
import Item from './condition-item'
|
||||
import type { Condition, LogicalOperator } from '@/app/components/workflow/nodes/if-else/types'
|
||||
|
||||
|
|
@ -14,6 +15,7 @@ type Props = {
|
|||
onChange: (newList: Condition[]) => void
|
||||
logicalOperator: LogicalOperator
|
||||
onLogicalOperatorToggle: () => void
|
||||
filterVar: (varPayload: Var) => boolean
|
||||
}
|
||||
|
||||
const ConditionList: FC<Props> = ({
|
||||
|
|
@ -24,6 +26,7 @@ const ConditionList: FC<Props> = ({
|
|||
onChange,
|
||||
logicalOperator,
|
||||
onLogicalOperatorToggle,
|
||||
filterVar,
|
||||
}) => {
|
||||
const handleItemChange = useCallback((index: number) => {
|
||||
return (newItem: Condition) => {
|
||||
|
|
@ -59,6 +62,7 @@ const ConditionList: FC<Props> = ({
|
|||
onRemove={handleItemRemove(0)}
|
||||
logicalOperator={logicalOperator}
|
||||
onLogicalOperatorToggle={onLogicalOperatorToggle}
|
||||
filterVar={filterVar}
|
||||
/>
|
||||
{
|
||||
list.length > 1 && (
|
||||
|
|
@ -74,6 +78,7 @@ const ConditionList: FC<Props> = ({
|
|||
isShowLogicalOperator
|
||||
logicalOperator={logicalOperator}
|
||||
onLogicalOperatorToggle={onLogicalOperatorToggle}
|
||||
filterVar={filterVar}
|
||||
/>
|
||||
)))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ const Panel: FC<NodePanelProps<IfElseNodeType>> = ({
|
|||
handleConditionsChange,
|
||||
handleAddCondition,
|
||||
handleLogicalOperatorToggle,
|
||||
filterVar,
|
||||
} = useConfig(id, data)
|
||||
return (
|
||||
<div className='mt-2'>
|
||||
|
|
@ -38,6 +39,7 @@ const Panel: FC<NodePanelProps<IfElseNodeType>> = ({
|
|||
onChange={handleConditionsChange}
|
||||
logicalOperator={inputs.logical_operator}
|
||||
onLogicalOperatorToggle={handleLogicalOperatorToggle}
|
||||
filterVar={filterVar}
|
||||
/>
|
||||
<AddButton
|
||||
className='mt-3'
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
import { useCallback } from 'react'
|
||||
import produce from 'immer'
|
||||
import type { Var } from '../../types'
|
||||
import { VarType } from '../../types'
|
||||
import { ComparisonOperator, LogicalOperator } from './types'
|
||||
import type { Condition, IfElseNodeType } from './types'
|
||||
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
|
||||
|
|
@ -12,7 +14,7 @@ const useConfig = (id: string, payload: IfElseNodeType) => {
|
|||
draft.conditions = newConditions
|
||||
})
|
||||
setInputs(newInputs)
|
||||
}, [])
|
||||
}, [inputs, setInputs])
|
||||
|
||||
const handleAddCondition = useCallback(() => {
|
||||
const newInputs = produce(inputs, (draft) => {
|
||||
|
|
@ -24,20 +26,25 @@ const useConfig = (id: string, payload: IfElseNodeType) => {
|
|||
})
|
||||
})
|
||||
setInputs(newInputs)
|
||||
}, [inputs])
|
||||
}, [inputs, setInputs])
|
||||
|
||||
const handleLogicalOperatorToggle = useCallback(() => {
|
||||
const newInputs = produce(inputs, (draft) => {
|
||||
draft.logical_operator = draft.logical_operator === LogicalOperator.and ? LogicalOperator.or : LogicalOperator.and
|
||||
})
|
||||
setInputs(newInputs)
|
||||
}, [inputs])
|
||||
}, [inputs, setInputs])
|
||||
|
||||
const filterVar = useCallback((varPayload: Var) => {
|
||||
return varPayload.type !== VarType.arrayFile
|
||||
}, [])
|
||||
|
||||
return {
|
||||
inputs,
|
||||
handleConditionsChange,
|
||||
handleAddCondition,
|
||||
handleLogicalOperatorToggle,
|
||||
filterVar,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-s
|
|||
|
||||
const useConfig = (id: string, payload: KnowledgeRetrievalNodeType) => {
|
||||
const isChatMode = useIsChatMode()
|
||||
console.log()
|
||||
const { getBeforeNodesInSameBranch } = useWorkflow()
|
||||
const startNode = getBeforeNodesInSameBranch(id).find(node => node.data.type === BlockEnum.Start)
|
||||
const startNodeId = startNode?.id
|
||||
|
|
@ -55,14 +56,16 @@ const useConfig = (id: string, payload: KnowledgeRetrievalNodeType) => {
|
|||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
if (inputs._isReady) {
|
||||
if (isChatMode && inputs.query_variable_selector.length === 0 && startNodeId) {
|
||||
handleQueryVarChange(
|
||||
[startNodeId, 'sys.query'],
|
||||
)
|
||||
}
|
||||
}
|
||||
}, [inputs._isReady])
|
||||
let query_variable_selector: ValueSelector = []
|
||||
if (isChatMode && inputs.query_variable_selector.length === 0 && startNodeId)
|
||||
query_variable_selector = [startNodeId, 'sys.query']
|
||||
|
||||
setInputs({
|
||||
...inputs,
|
||||
query_variable_selector,
|
||||
})
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [])
|
||||
|
||||
const handleOnDatasetsChange = useCallback((newDatasets: DataSet[]) => {
|
||||
const newInputs = produce(inputs, (draft) => {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import type { FC } from 'react'
|
||||
import React, { useEffect } from 'react'
|
||||
import React from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import VarReferencePicker from '../_base/components/variable/var-reference-picker'
|
||||
import useConfig from './use-config'
|
||||
|
|
@ -11,7 +11,6 @@ import ModelParameterModal from '@/app/components/header/account-setting/model-p
|
|||
import { InputVarType, type NodePanelProps } from '@/app/components/workflow/types'
|
||||
import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form'
|
||||
import ResultPanel from '@/app/components/workflow/run/result-panel'
|
||||
import { useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
|
||||
|
||||
const i18nPrefix = 'workflow.nodes.questionClassifiers'
|
||||
|
||||
|
|
@ -21,10 +20,6 @@ const Panel: FC<NodePanelProps<QuestionClassifierNodeType>> = ({
|
|||
}) => {
|
||||
const { t } = useTranslation()
|
||||
const readOnly = false
|
||||
const {
|
||||
currentProvider,
|
||||
currentModel,
|
||||
} = useModelListAndDefaultModelAndCurrentProviderAndModel(1)
|
||||
|
||||
const {
|
||||
inputs,
|
||||
|
|
@ -42,20 +37,11 @@ const Panel: FC<NodePanelProps<QuestionClassifierNodeType>> = ({
|
|||
query,
|
||||
setQuery,
|
||||
runResult,
|
||||
filterVar,
|
||||
} = useConfig(id, data)
|
||||
|
||||
const model = inputs.model
|
||||
|
||||
useEffect(() => {
|
||||
if (currentProvider?.provider && currentModel?.model && !model.provider) {
|
||||
handleModelChanged({
|
||||
provider: currentProvider?.provider,
|
||||
modelId: currentModel?.model,
|
||||
mode: currentModel?.model_properties?.mode as string,
|
||||
})
|
||||
}
|
||||
}, [model.provider, currentProvider, currentModel, handleModelChanged])
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className='mt-2 px-4 space-y-4'>
|
||||
|
|
@ -68,6 +54,7 @@ const Panel: FC<NodePanelProps<QuestionClassifierNodeType>> = ({
|
|||
nodeId={id}
|
||||
value={inputs.query_variable_selector}
|
||||
onChange={handleQueryVarChange}
|
||||
filterVar={filterVar}
|
||||
/>
|
||||
</Field>
|
||||
<Field
|
||||
|
|
|
|||
|
|
@ -1,22 +1,52 @@
|
|||
import { useCallback } from 'react'
|
||||
import { useCallback, useEffect, useRef } from 'react'
|
||||
import produce from 'immer'
|
||||
import type { Memory, ValueSelector } from '../../types'
|
||||
import { BlockEnum, VarType } from '../../types'
|
||||
import type { Memory, ValueSelector, Var } from '../../types'
|
||||
import { useIsChatMode, useWorkflow } from '../../hooks'
|
||||
import { useStore } from '../../store'
|
||||
import type { QuestionClassifierNodeType } from './types'
|
||||
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
|
||||
import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run'
|
||||
import { useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
|
||||
|
||||
const useConfig = (id: string, payload: QuestionClassifierNodeType) => {
|
||||
const isChatMode = useIsChatMode()
|
||||
const defaultConfig = useStore(s => s.nodesDefaultConfigs)[payload.type]
|
||||
const { getBeforeNodesInSameBranch } = useWorkflow()
|
||||
const startNode = getBeforeNodesInSameBranch(id).find(node => node.data.type === BlockEnum.Start)
|
||||
const startNodeId = startNode?.id
|
||||
const { inputs, setInputs } = useNodeCrud<QuestionClassifierNodeType>(id, payload)
|
||||
const inputRef = useRef(inputs)
|
||||
useEffect(() => {
|
||||
inputRef.current = inputs
|
||||
}, [inputs])
|
||||
|
||||
// model
|
||||
const {
|
||||
currentProvider,
|
||||
currentModel,
|
||||
} = useModelListAndDefaultModelAndCurrentProviderAndModel(1)
|
||||
|
||||
const model = inputs.model
|
||||
|
||||
const handleModelChanged = useCallback((model: { provider: string; modelId: string; mode?: string }) => {
|
||||
const newInputs = produce(inputs, (draft) => {
|
||||
const newInputs = produce(inputRef.current, (draft) => {
|
||||
draft.model.provider = model.provider
|
||||
draft.model.name = model.modelId
|
||||
draft.model.mode = model.mode!
|
||||
})
|
||||
setInputs(newInputs)
|
||||
}, [inputs, setInputs])
|
||||
}, [setInputs])
|
||||
|
||||
useEffect(() => {
|
||||
if (currentProvider?.provider && currentModel?.model && !model.provider) {
|
||||
handleModelChanged({
|
||||
provider: currentProvider?.provider,
|
||||
modelId: currentModel?.model,
|
||||
mode: currentModel?.model_properties?.mode as string,
|
||||
})
|
||||
}
|
||||
}, [model.provider, currentProvider, currentModel, handleModelChanged])
|
||||
|
||||
const handleCompletionParamsChange = useCallback((newParams: Record<string, any>) => {
|
||||
const newInputs = produce(inputs, (draft) => {
|
||||
|
|
@ -25,13 +55,30 @@ const useConfig = (id: string, payload: QuestionClassifierNodeType) => {
|
|||
setInputs(newInputs)
|
||||
}, [inputs, setInputs])
|
||||
|
||||
const handleQueryVarChange = useCallback((newVar: ValueSelector) => {
|
||||
const handleQueryVarChange = useCallback((newVar: ValueSelector | string) => {
|
||||
const newInputs = produce(inputs, (draft) => {
|
||||
draft.query_variable_selector = newVar
|
||||
draft.query_variable_selector = newVar as ValueSelector
|
||||
})
|
||||
setInputs(newInputs)
|
||||
// console.log(newInputs.query_variable_selector)
|
||||
}, [inputs, setInputs])
|
||||
|
||||
useEffect(() => {
|
||||
const isReady = defaultConfig && Object.keys(defaultConfig).length > 0
|
||||
if (isReady) {
|
||||
let query_variable_selector: ValueSelector = []
|
||||
if (isChatMode && inputs.query_variable_selector.length === 0 && startNodeId)
|
||||
query_variable_selector = [startNodeId, 'sys.query']
|
||||
setInputs({
|
||||
...inputs,
|
||||
...defaultConfig,
|
||||
query_variable_selector: inputs.query_variable_selector.length > 0 ? inputs.query_variable_selector : query_variable_selector,
|
||||
})
|
||||
console.log(query_variable_selector)
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [defaultConfig])
|
||||
|
||||
const handleClassesChange = useCallback((newClasses: any) => {
|
||||
const newInputs = produce(inputs, (draft) => {
|
||||
draft.classes = newClasses
|
||||
|
|
@ -80,11 +127,16 @@ const useConfig = (id: string, payload: QuestionClassifierNodeType) => {
|
|||
})
|
||||
}, [runInputData, setRunInputData])
|
||||
|
||||
const filterVar = useCallback((varPayload: Var) => {
|
||||
return varPayload.type === VarType.string
|
||||
}, [])
|
||||
|
||||
return {
|
||||
inputs,
|
||||
handleModelChanged,
|
||||
handleCompletionParamsChange,
|
||||
handleQueryVarChange,
|
||||
filterVar,
|
||||
handleTopicsChange: handleClassesChange,
|
||||
handleInstructionChange,
|
||||
handleMemoryChange,
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ const Panel: FC<NodePanelProps<TemplateTransformNodeType>> = ({
|
|||
handleVarListChange,
|
||||
handleAddVariable,
|
||||
handleCodeChange,
|
||||
filterVar,
|
||||
// single run
|
||||
isShowSingleRun,
|
||||
hideSingleRun,
|
||||
|
|
@ -56,6 +57,7 @@ const Panel: FC<NodePanelProps<TemplateTransformNodeType>> = ({
|
|||
readonly={readOnly}
|
||||
list={inputs.variables}
|
||||
onChange={handleVarListChange}
|
||||
filterVar={filterVar}
|
||||
/>
|
||||
</Field>
|
||||
<Split />
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import { useCallback } from 'react'
|
||||
import produce from 'immer'
|
||||
import useVarList from '../_base/hooks/use-var-list'
|
||||
import type { Var } from '../../types'
|
||||
import { VarType } from '../../types'
|
||||
import type { TemplateTransformNodeType } from './types'
|
||||
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
|
||||
import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run'
|
||||
|
|
@ -17,7 +19,7 @@ const useConfig = (id: string, payload: TemplateTransformNodeType) => {
|
|||
draft.template = template
|
||||
})
|
||||
setInputs(newInputs)
|
||||
}, [setInputs])
|
||||
}, [inputs, setInputs])
|
||||
|
||||
// single run
|
||||
const {
|
||||
|
|
@ -48,13 +50,18 @@ const useConfig = (id: string, payload: TemplateTransformNodeType) => {
|
|||
|
||||
const setInputVarValues = useCallback((newPayload: Record<string, any>) => {
|
||||
setRunInputData(newPayload)
|
||||
}, [runInputData, setRunInputData])
|
||||
}, [setRunInputData])
|
||||
|
||||
const filterVar = useCallback((varPayload: Var) => {
|
||||
return [VarType.string, VarType.number, VarType.object, VarType.array, VarType.arrayNumber, VarType.arrayString, VarType.arrayObject].includes(varPayload.type)
|
||||
}, [])
|
||||
|
||||
return {
|
||||
inputs,
|
||||
handleVarListChange,
|
||||
handleAddVariable,
|
||||
handleCodeChange,
|
||||
filterVar,
|
||||
// single run
|
||||
isShowSingleRun,
|
||||
hideSingleRun,
|
||||
|
|
|
|||
Loading…
Reference in New Issue