import { memo, useCallback, useState, } from 'react' import { useContext } from 'use-context-selector' import { useStoreApi, } from 'reactflow' import { RiBookOpenLine, RiCloseLine } from '@remixicon/react' import { useTranslation } from 'react-i18next' import { useStore } from '@/app/components/workflow/store' import ActionButton, { ActionButtonState } from '@/app/components/base/action-button' import { BubbleX, LongArrowLeft, LongArrowRight } from '@/app/components/base/icons/src/vender/line/others' import BlockIcon from '@/app/components/workflow/block-icon' import VariableModalTrigger from '@/app/components/workflow/panel/chat-variable-panel/components/variable-modal-trigger' import VariableItem from '@/app/components/workflow/panel/chat-variable-panel/components/variable-item' import RemoveEffectVarConfirm from '@/app/components/workflow/nodes/_base/components/remove-effect-var-confirm' import type { ConversationVariable, } from '@/app/components/workflow/types' import { findUsedVarNodes, updateNodeVars } from '@/app/components/workflow/nodes/_base/components/variable/utils' import { useNodesSyncDraft } from '@/app/components/workflow/hooks/use-nodes-sync-draft' import { BlockEnum } from '@/app/components/workflow/types' import I18n from '@/context/i18n' import { LanguagesSupported } from '@/i18n/language' import cn from '@/utils/classnames' const ChatVariablePanel = () => { const { t } = useTranslation() const { locale } = useContext(I18n) const store = useStoreApi() const setShowChatVariablePanel = useStore(s => s.setShowChatVariablePanel) const varList = useStore(s => s.conversationVariables) as ConversationVariable[] const updateChatVarList = useStore(s => s.setConversationVariables) const { doSyncWorkflowDraft } = useNodesSyncDraft() const [showTip, setShowTip] = useState(true) const [showVariableModal, setShowVariableModal] = useState(false) const [currentVar, setCurrentVar] = useState() const [showRemoveVarConfirm, setShowRemoveConfirm] = useState(false) const [cacheForDelete, setCacheForDelete] = useState() const getEffectedNodes = useCallback((chatVar: ConversationVariable) => { const { getNodes } = store.getState() const allNodes = getNodes() return findUsedVarNodes( ['conversation', chatVar.name], allNodes, ) }, [store]) const removeUsedVarInNodes = useCallback((chatVar: ConversationVariable) => { const { getNodes, setNodes } = store.getState() const effectedNodes = getEffectedNodes(chatVar) const newNodes = getNodes().map((node) => { if (effectedNodes.find(n => n.id === node.id)) return updateNodeVars(node, ['conversation', chatVar.name], []) return node }) setNodes(newNodes) }, [getEffectedNodes, store]) const handleEdit = (chatVar: ConversationVariable) => { setCurrentVar(chatVar) setShowVariableModal(true) } const handleDelete = useCallback((chatVar: ConversationVariable) => { removeUsedVarInNodes(chatVar) updateChatVarList(varList.filter(v => v.id !== chatVar.id)) setCacheForDelete(undefined) setShowRemoveConfirm(false) doSyncWorkflowDraft() }, [doSyncWorkflowDraft, removeUsedVarInNodes, updateChatVarList, varList]) const deleteCheck = useCallback((chatVar: ConversationVariable) => { const effectedNodes = getEffectedNodes(chatVar) if (effectedNodes.length > 0) { setCacheForDelete(chatVar) setShowRemoveConfirm(true) } else { handleDelete(chatVar) } }, [getEffectedNodes, handleDelete]) const handleSave = useCallback(async (chatVar: ConversationVariable) => { // add chatVar if (!currentVar) { const newList = [chatVar, ...varList] updateChatVarList(newList) doSyncWorkflowDraft() return } // edit chatVar const newList = varList.map(v => v.id === currentVar.id ? chatVar : v) updateChatVarList(newList) // side effects of rename env if (currentVar.name !== chatVar.name) { const { getNodes, setNodes } = store.getState() const effectedNodes = getEffectedNodes(currentVar) const newNodes = getNodes().map((node) => { if (effectedNodes.find(n => n.id === node.id)) return updateNodeVars(node, ['conversation', currentVar.name], ['conversation', chatVar.name]) return node }) setNodes(newNodes) } doSyncWorkflowDraft() }, [currentVar, doSyncWorkflowDraft, getEffectedNodes, store, updateChatVarList, varList]) return (
{t('workflow.chatVariable.panelTitle')}
setShowTip(!showTip)}>
setShowChatVariablePanel(false)} >
{showTip && (
TIPS
{t('workflow.chatVariable.panelDescription')} {t('workflow.chatVariable.docLink')}
conversation_var
String
WRITE
{t('workflow.blocks.assigner')}
READ
{t('workflow.blocks.llm')}
)}
setCurrentVar(undefined)} />
{varList.map(chatVar => ( ))}
setShowRemoveConfirm(false)} onConfirm={() => cacheForDelete && handleDelete(cacheForDelete)} />
) } export default memo(ChatVariablePanel)