fix(workflow): Implement automatic variable addition from opening statement to start node (#9450)

This commit is contained in:
Kevin9703 2024-10-17 19:01:50 +08:00 committed by GitHub
parent a45f8969a0
commit e7aecb89dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 3 deletions

View File

@ -13,16 +13,19 @@ import TextToSpeech from './text-to-speech'
import SpeechToText from './speech-to-text' import SpeechToText from './speech-to-text'
import Citation from './citation' import Citation from './citation'
import Moderation from './moderation' import Moderation from './moderation'
import type { InputVar } from '@/app/components/workflow/types'
export type FeaturePanelProps = { export type FeaturePanelProps = {
onChange?: OnFeaturesChange onChange?: OnFeaturesChange
openingStatementProps: OpeningStatementProps openingStatementProps: OpeningStatementProps
disabled?: boolean disabled?: boolean
workflowVariables: InputVar[]
} }
const FeaturePanel = ({ const FeaturePanel = ({
onChange, onChange,
openingStatementProps, openingStatementProps,
disabled, disabled,
workflowVariables,
}: FeaturePanelProps) => { }: FeaturePanelProps) => {
const { t } = useTranslation() const { t } = useTranslation()
const features = useFeatures(s => s.features) const features = useFeatures(s => s.features)
@ -60,6 +63,7 @@ const FeaturePanel = ({
{...openingStatementProps} {...openingStatementProps}
onChange={onChange} onChange={onChange}
readonly={disabled} readonly={disabled}
workflowVariables={workflowVariables}
/> />
) )
} }

View File

@ -24,6 +24,7 @@ import ConfirmAddVar from '@/app/components/app/configuration/config-prompt/conf
import { getNewVar } from '@/utils/var' import { getNewVar } from '@/utils/var'
import { varHighlightHTML } from '@/app/components/app/configuration/base/var-highlight' import { varHighlightHTML } from '@/app/components/app/configuration/base/var-highlight'
import type { PromptVariable } from '@/models/debug' import type { PromptVariable } from '@/models/debug'
import type { InputVar } from '@/app/components/workflow/types'
const MAX_QUESTION_NUM = 5 const MAX_QUESTION_NUM = 5
@ -32,6 +33,7 @@ export type OpeningStatementProps = {
readonly?: boolean readonly?: boolean
promptVariables?: PromptVariable[] promptVariables?: PromptVariable[]
onAutoAddPromptVariable: (variable: PromptVariable[]) => void onAutoAddPromptVariable: (variable: PromptVariable[]) => void
workflowVariables?: InputVar[]
} }
// regex to match the {{}} and replace it with a span // regex to match the {{}} and replace it with a span
@ -42,6 +44,7 @@ const OpeningStatement: FC<OpeningStatementProps> = ({
readonly, readonly,
promptVariables = [], promptVariables = [],
onAutoAddPromptVariable, onAutoAddPromptVariable,
workflowVariables = [],
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
const featureStore = useFeaturesStore() const featureStore = useFeaturesStore()
@ -96,14 +99,18 @@ const OpeningStatement: FC<OpeningStatementProps> = ({
const handleConfirm = () => { const handleConfirm = () => {
const keys = getInputKeys(tempValue) const keys = getInputKeys(tempValue)
const promptKeys = promptVariables.map(item => item.key) const promptKeys = promptVariables.map(item => item.key)
const workflowVariableKeys = workflowVariables.map(item => item.variable)
let notIncludeKeys: string[] = [] let notIncludeKeys: string[] = []
if (promptKeys.length === 0) { if (promptKeys.length === 0 && workflowVariables.length === 0) {
if (keys.length > 0) if (keys.length > 0)
notIncludeKeys = keys notIncludeKeys = keys
} }
else { else {
notIncludeKeys = keys.filter(key => !promptKeys.includes(key)) if (workflowVariables.length > 0)
notIncludeKeys = keys.filter(key => !workflowVariableKeys.includes(key))
else notIncludeKeys = keys.filter(key => !promptKeys.includes(key))
} }
if (notIncludeKeys.length > 0) { if (notIncludeKeys.length > 0) {

View File

@ -4,16 +4,21 @@ import {
} from 'react' } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { RiCloseLine } from '@remixicon/react' import { RiCloseLine } from '@remixicon/react'
import { useNodes } from 'reactflow'
import { useStore } from './store' import { useStore } from './store'
import { import {
useIsChatMode, useIsChatMode,
useNodesReadOnly, useNodesReadOnly,
useNodesSyncDraft, useNodesSyncDraft,
} from './hooks' } from './hooks'
import { type CommonNodeType, type InputVar, InputVarType, type Node } from './types'
import useConfig from './nodes/start/use-config'
import type { StartNodeType } from './nodes/start/types'
import { import {
FeaturesChoose, FeaturesChoose,
FeaturesPanel, FeaturesPanel,
} from '@/app/components/base/features' } from '@/app/components/base/features'
import type { PromptVariable } from '@/models/debug'
const Features = () => { const Features = () => {
const { t } = useTranslation() const { t } = useTranslation()
@ -21,6 +26,24 @@ const Features = () => {
const setShowFeaturesPanel = useStore(s => s.setShowFeaturesPanel) const setShowFeaturesPanel = useStore(s => s.setShowFeaturesPanel)
const { nodesReadOnly } = useNodesReadOnly() const { nodesReadOnly } = useNodesReadOnly()
const { handleSyncWorkflowDraft } = useNodesSyncDraft() const { handleSyncWorkflowDraft } = useNodesSyncDraft()
const nodes = useNodes<CommonNodeType>()
const startNode = nodes.find(node => node.data.type === 'start')
const { id, data } = startNode as Node<StartNodeType>
const { handleAddVariable } = useConfig(id, data)
const handleAddOpeningStatementVariable = (variables: PromptVariable[]) => {
const newVariable = variables[0]
const startNodeVariable: InputVar = {
variable: newVariable.key,
label: newVariable.name,
type: InputVarType.textInput,
max_length: newVariable.max_length,
required: newVariable.required || false,
options: [],
}
handleAddVariable(startNodeVariable)
}
const handleFeaturesChange = useCallback(() => { const handleFeaturesChange = useCallback(() => {
handleSyncWorkflowDraft() handleSyncWorkflowDraft()
@ -55,8 +78,9 @@ const Features = () => {
disabled={nodesReadOnly} disabled={nodesReadOnly}
onChange={handleFeaturesChange} onChange={handleFeaturesChange}
openingStatementProps={{ openingStatementProps={{
onAutoAddPromptVariable: () => {}, onAutoAddPromptVariable: handleAddOpeningStatementVariable,
}} }}
workflowVariables={data.variables}
/> />
</div> </div>
</div> </div>