feat: memory

This commit is contained in:
zxhlyh 2025-10-22 16:28:08 +08:00
parent 1cf7007fb4
commit 6b2d460023
8 changed files with 32 additions and 28 deletions

View File

@ -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<PromptEditorProps> = ({

View File

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

View File

@ -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) => {

View File

@ -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<Props> = ({

View File

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

View File

@ -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<Props> = ({

View File

@ -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 (
<div className='system-xs-regular mt-2 flex items-center justify-center rounded-[10px] bg-background-section p-3 text-text-tertiary'>
{t('workflow.nodes.common.memory.block.empty')}
@ -47,13 +48,13 @@ const BlockMemory = ({ payload }: BlockMemoryProps) => {
}
return (
<>
<div>
<div className='mt-2 space-y-1'>
{
memoryVariablesInUsed.map(memoryVariable => (
<div
key={memoryVariable.id}
className='group flex h-8 items-center space-x-1 rounded-lg border-[0.5px] border-components-panel-border-subtle bg-components-panel-on-panel-item-bg pl-2 pr-1 shadow-xs hover:border hover:border-state-destructive-solid hover:bg-state-destructive-hover'>
<div className='h-4 w-4'></div>
<MemoryIcon className='h-4 w-4 text-util-colors-teal-teal-700' />
<div
title={memoryVariable.name}
className='system-sm-medium grow truncate text-text-secondary'

View File

@ -18,12 +18,11 @@ import {
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
import { checkHasContextBlock, checkHasHistoryBlock, checkHasQueryBlock } from '@/app/components/base/prompt-editor/constants'
import useInspectVarsCrud from '@/app/components/workflow/hooks/use-inspect-vars-crud'
import { ChatVarType } from '@/app/components/workflow/panel/chat-variable-panel/type'
const useConfig = (id: string, payload: LLMNodeType) => {
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,