From 6b2d460023e285572ba3e17a7dda3054690f75d7 Mon Sep 17 00:00:00 2001 From: zxhlyh Date: Wed, 22 Oct 2025 16:28:08 +0800 Subject: [PATCH] feat: memory --- web/app/components/base/prompt-editor/index.tsx | 6 +++--- .../plugins/memory-popup-plugin/index.tsx | 6 +++--- .../workflow/hooks/use-memory-variable.ts | 10 ++++++++-- .../nodes/_base/components/prompt/editor.tsx | 6 +++--- .../nodes/llm/components/config-prompt-item.tsx | 6 +++--- .../workflow/nodes/llm/components/config-prompt.tsx | 6 +++--- .../llm/components/memory-system/block-memory.tsx | 7 ++++--- web/app/components/workflow/nodes/llm/use-config.ts | 13 +++++-------- 8 files changed, 32 insertions(+), 28 deletions(-) diff --git a/web/app/components/base/prompt-editor/index.tsx b/web/app/components/base/prompt-editor/index.tsx index 7a6a844702..c5dd9720e5 100644 --- a/web/app/components/base/prompt-editor/index.tsx +++ b/web/app/components/base/prompt-editor/index.tsx @@ -81,7 +81,7 @@ import { } from './constants' import { useEventEmitterContextContext } from '@/context/event-emitter' import type { - ConversationVariable, + MemoryVariable, } from '@/app/components/workflow/types' import cn from '@/utils/classnames' @@ -109,8 +109,8 @@ export type PromptEditorProps = { lastRunBlock?: LastRunBlockType isSupportFileVar?: boolean isMemorySupported?: boolean - memoryVarInNode?: ConversationVariable[] - memoryVarInApp?: ConversationVariable[] + memoryVarInNode?: MemoryVariable[] + memoryVarInApp?: MemoryVariable[] } const PromptEditor: FC = ({ diff --git a/web/app/components/base/prompt-editor/plugins/memory-popup-plugin/index.tsx b/web/app/components/base/prompt-editor/plugins/memory-popup-plugin/index.tsx index f825349434..67f146420e 100644 --- a/web/app/components/base/prompt-editor/plugins/memory-popup-plugin/index.tsx +++ b/web/app/components/base/prompt-editor/plugins/memory-popup-plugin/index.tsx @@ -29,7 +29,7 @@ import { MEMORY_POPUP_SHOW_BY_EVENT_EMITTER, MEMORY_VAR_CREATED_BY_MODAL_BY_EVEN import Divider from '@/app/components/base/divider' import VariableIcon from '@/app/components/workflow/nodes/_base/components/variable/variable-label/base/variable-icon' import type { - ConversationVariable, + MemoryVariable, } from '@/app/components/workflow/types' import { INSERT_WORKFLOW_VARIABLE_BLOCK_COMMAND } from '../workflow-variable-block' @@ -39,8 +39,8 @@ export type MemoryPopupProps = { className?: string container?: Element | null instanceId?: string - memoryVarInNode: ConversationVariable[] - memoryVarInApp: ConversationVariable[] + memoryVarInNode: MemoryVariable[] + memoryVarInApp: MemoryVariable[] } export default function MemoryPopupPlugin({ diff --git a/web/app/components/workflow/hooks/use-memory-variable.ts b/web/app/components/workflow/hooks/use-memory-variable.ts index 2c8e030bc8..5b4b4d523f 100644 --- a/web/app/components/workflow/hooks/use-memory-variable.ts +++ b/web/app/components/workflow/hooks/use-memory-variable.ts @@ -58,10 +58,16 @@ export const useMemoryVariable = () => { const oldMemoryVariable = memoryVariables.find(v => v.id === memoryVariable.id) setMemoryVariables(memoryVariables.map(v => v.id === memoryVariable.id ? memoryVariable : v)) - if (oldMemoryVariable && !oldMemoryVariable?.node && memoryVariable.node) + if (oldMemoryVariable && !oldMemoryVariable?.node && memoryVariable.node) { handleAddMemoryVariableToNode(memoryVariable.node, memoryVariable.id) - else if (oldMemoryVariable && oldMemoryVariable.node && !memoryVariable.node) + } + else if (oldMemoryVariable && oldMemoryVariable.node && !memoryVariable.node) { handleDeleteMemoryVariableFromNode(oldMemoryVariable.node, memoryVariable.id) + } + else if (oldMemoryVariable && oldMemoryVariable.node && memoryVariable.node && memoryVariable.node !== oldMemoryVariable.node) { + handleDeleteMemoryVariableFromNode(oldMemoryVariable.node, memoryVariable.id) + handleAddMemoryVariableToNode(memoryVariable.node, memoryVariable.id) + } }, [setMemoryVariables, workflowStore, handleAddMemoryVariableToNode, handleDeleteMemoryVariableFromNode]) const handleDeleteMemoryVariable = useCallback((memoryVariable: MemoryVariable) => { diff --git a/web/app/components/workflow/nodes/_base/components/prompt/editor.tsx b/web/app/components/workflow/nodes/_base/components/prompt/editor.tsx index 3bee8c7b5f..c59a6928d9 100644 --- a/web/app/components/workflow/nodes/_base/components/prompt/editor.tsx +++ b/web/app/components/workflow/nodes/_base/components/prompt/editor.tsx @@ -39,7 +39,7 @@ import { useWorkflowVariableType } from '@/app/components/workflow/hooks' import AddMemoryButton from './add-memory-button' import { MEMORY_POPUP_SHOW_BY_EVENT_EMITTER } from './type' import type { - ConversationVariable, + MemoryVariable, } from '@/app/components/workflow/types' import MemoryCreateButton from '@/app/components/workflow/nodes/llm/components/memory-system/memory-create-button' @@ -86,8 +86,8 @@ type Props = { titleClassName?: string required?: boolean isMemorySupported?: boolean - memoryVarInNode?: ConversationVariable[] - memoryVarInApp?: ConversationVariable[] + memoryVarInNode?: MemoryVariable[] + memoryVarInApp?: MemoryVariable[] } const Editor: FC = ({ diff --git a/web/app/components/workflow/nodes/llm/components/config-prompt-item.tsx b/web/app/components/workflow/nodes/llm/components/config-prompt-item.tsx index 45786fb984..e3c018a558 100644 --- a/web/app/components/workflow/nodes/llm/components/config-prompt-item.tsx +++ b/web/app/components/workflow/nodes/llm/components/config-prompt-item.tsx @@ -10,7 +10,7 @@ import TypeSelector from '@/app/components/workflow/nodes/_base/components/selec import Tooltip from '@/app/components/base/tooltip' import { PromptRole } from '@/models/debug' import type { - ConversationVariable, + MemoryVariable, } from '@/app/components/workflow/types' const i18nPrefix = 'workflow.nodes.llm' @@ -42,8 +42,8 @@ type Props = { varList: Variable[] handleAddVariable: (payload: any) => void modelConfig?: ModelConfig - memoryVarInNode?: ConversationVariable[] - memoryVarInApp?: ConversationVariable[] + memoryVarInNode?: MemoryVariable[] + memoryVarInApp?: MemoryVariable[] } const roleOptions = [ diff --git a/web/app/components/workflow/nodes/llm/components/config-prompt.tsx b/web/app/components/workflow/nodes/llm/components/config-prompt.tsx index a48f5c18e6..c789e626c1 100644 --- a/web/app/components/workflow/nodes/llm/components/config-prompt.tsx +++ b/web/app/components/workflow/nodes/llm/components/config-prompt.tsx @@ -15,7 +15,7 @@ import Editor from '@/app/components/workflow/nodes/_base/components/prompt/edit import AddButton from '@/app/components/workflow/nodes/_base/components/add-button' import { DragHandle } from '@/app/components/base/icons/src/vender/line/others' import type { - ConversationVariable, + MemoryVariable, } from '@/app/components/workflow/types' const i18nPrefix = 'workflow.nodes.llm' @@ -38,8 +38,8 @@ type Props = { handleAddVariable: (payload: any) => void modelConfig: ModelConfig memoryVarSortFn?: (a: string, b: string) => number - memoryVarInNode?: ConversationVariable[] - memoryVarInApp?: ConversationVariable[] + memoryVarInNode?: MemoryVariable[] + memoryVarInApp?: MemoryVariable[] } const ConfigPrompt: FC = ({ diff --git a/web/app/components/workflow/nodes/llm/components/memory-system/block-memory.tsx b/web/app/components/workflow/nodes/llm/components/memory-system/block-memory.tsx index c6f01521e8..8f50a683ce 100644 --- a/web/app/components/workflow/nodes/llm/components/memory-system/block-memory.tsx +++ b/web/app/components/workflow/nodes/llm/components/memory-system/block-memory.tsx @@ -12,6 +12,7 @@ import Badge from '@/app/components/base/badge' import ActionButton from '@/app/components/base/action-button' import { useMemoryVariables } from './hooks/use-memory-variables' import Confirm from '@/app/components/base/confirm' +import { Memory as MemoryIcon } from '@/app/components/base/icons/src/vender/line/others' type BlockMemoryProps = { payload: Memory @@ -38,7 +39,7 @@ const BlockMemory = ({ payload }: BlockMemoryProps) => { }) } - if (!block_id?.length) { + if (!memoryVariablesInUsed?.length) { return (
{t('workflow.nodes.common.memory.block.empty')} @@ -47,13 +48,13 @@ const BlockMemory = ({ payload }: BlockMemoryProps) => { } return ( <> -
+
{ memoryVariablesInUsed.map(memoryVariable => (
-
+
{ const { nodesReadOnly: readOnly } = useNodesReadOnly() const isChatMode = useIsChatMode() - const conversationVariables = useStore(s => s.conversationVariables) + const memoryVariables = useStore(s => s.memoryVariables) const defaultConfig = useStore(s => s.nodesDefaultConfigs)?.[payload.type] const [defaultRolePrefix, setDefaultRolePrefix] = useState<{ user: string; assistant: string }>({ user: '', assistant: '' }) @@ -346,17 +345,15 @@ const useConfig = (id: string, payload: LLMNodeType) => { const memoryVarInNode = useMemo(() => { const idsInNode = inputs.memory?.block_id || [] - return conversationVariables - .filter(varItem => varItem.value_type === ChatVarType.Memory) + return memoryVariables .filter(varItem => idsInNode.includes(varItem.id)) - }, [inputs.memory?.block_id, conversationVariables]) + }, [inputs.memory?.block_id, memoryVariables]) const memoryVarInApp = useMemo(() => { const idsInApp = inputs.memory?.block_id || [] - return conversationVariables - .filter(varItem => varItem.value_type === ChatVarType.Memory) + return memoryVariables .filter(varItem => !idsInApp.includes(varItem.id)) - }, [inputs.memory?.block_id, conversationVariables]) + }, [inputs.memory?.block_id, memoryVariables]) return { readOnly,