feat: question classify init

This commit is contained in:
Joel
2024-03-15 20:50:55 +08:00
parent e3c65c072c
commit 777cca1a09
11 changed files with 115 additions and 38 deletions

View File

@ -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

View File

@ -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,