add memory variable

This commit is contained in:
zxhlyh
2025-10-16 18:30:15 +08:00
parent 1c3ff179f8
commit e90086c2d2
10 changed files with 154 additions and 20 deletions

View File

@ -5,8 +5,9 @@ import {
RiArrowDownSLine,
RiCheckLine,
} from '@remixicon/react'
import { useShallow } from 'zustand/react/shallow'
import {
useNodes,
useStore,
} from 'reactflow'
import {
PortalToFollowElem,
@ -14,9 +15,6 @@ import {
PortalToFollowElemTrigger,
} from '@/app/components/base/portal-to-follow-elem'
import BlockIcon from '@/app/components/workflow/block-icon'
import type {
CommonNodeType,
} from '@/app/components/workflow/types'
import { BlockEnum } from '@/app/components/workflow/types'
import cn from '@/utils/classnames'
@ -32,12 +30,13 @@ const NodeSelector: FC<Props> = ({
nodeType = BlockEnum.LLM,
}) => {
const [open, setOpen] = useState(false)
const nodes = useNodes<CommonNodeType>()
const filteredNodes = nodeType ? nodes.filter(node => node.data?.type === nodeType) : nodes
const filteredNodes = useStore(useShallow((s) => {
const nodes = [...s.nodeInternals.values()]
return nodes.filter(node => node.data?.type === nodeType)
}))
const currentNode = useMemo(() => filteredNodes.find(node => node.id === value), [filteredNodes, value])
return (
<PortalToFollowElem
open={open}

View File

@ -29,12 +29,14 @@ import { useDocLink } from '@/context/i18n'
import cn from '@/utils/classnames'
import useInspectVarsCrud from '../../hooks/use-inspect-vars-crud'
import { ChatVarType } from './type'
import { useMemoryVariable } from '@/app/components/workflow/hooks'
const ChatVariablePanel = () => {
const { t } = useTranslation()
const docLink = useDocLink()
const store = useStoreApi()
const workflowStore = useWorkflowStore()
const { handleAddMemoryVariable, handleUpdateMemoryVariable, handleDeleteMemoryVariable } = useMemoryVariable()
const setShowChatVariablePanel = useStore(s => s.setShowChatVariablePanel)
const varList = useStore(s => s.conversationVariables) as ConversationVariable[]
const memoryVariables = useStore(s => s.memoryVariables) as MemoryVariable[]
@ -90,8 +92,8 @@ const ChatVariablePanel = () => {
removeUsedVarInNodes(chatVar)
const varList = workflowStore.getState().conversationVariables
updateChatVarList(varList.filter(v => v.id !== chatVar.id))
const memoryList = workflowStore.getState().memoryVariables
setMemoryVariables(memoryList.filter(v => v.id !== chatVar.id))
if (chatVar.value_type === ChatVarType.Memory)
handleDeleteMemoryVariable(chatVar as MemoryVariable)
setCacheForDelete(undefined)
setShowRemoveConfirm(false)
handleVarChanged(chatVar.value_type === ChatVarType.Memory)
@ -110,8 +112,10 @@ const ChatVariablePanel = () => {
const handleSave = useCallback(async (chatVar: ConversationVariable | MemoryVariable) => {
if (chatVar.value_type === ChatVarType.Memory) {
const memoryVarList = workflowStore.getState().memoryVariables
setMemoryVariables([chatVar, ...memoryVarList])
if (!currentVar)
handleAddMemoryVariable(chatVar as MemoryVariable)
else
handleUpdateMemoryVariable(chatVar as MemoryVariable)
handleVarChanged(true)
return
}