From 50a7d93ddc50dd3d1540f7fc97e3ce706f903f23 Mon Sep 17 00:00:00 2001 From: JzoNg Date: Sun, 12 Oct 2025 13:22:04 +0800 Subject: [PATCH] memory in embedded chatbot --- .../base/chat/embedded-chatbot/context.tsx | 13 ++++ .../base/chat/embedded-chatbot/hooks.tsx | 63 +++++++++++++++++++ .../base/chat/embedded-chatbot/index.tsx | 26 +++++++- 3 files changed, 101 insertions(+), 1 deletion(-) diff --git a/web/app/components/base/chat/embedded-chatbot/context.tsx b/web/app/components/base/chat/embedded-chatbot/context.tsx index f5a888a2ab..123688e181 100644 --- a/web/app/components/base/chat/embedded-chatbot/context.tsx +++ b/web/app/components/base/chat/embedded-chatbot/context.tsx @@ -6,6 +6,7 @@ import type { ChatConfig, ChatItem, Feedback, + Memory, } from '../types' import type { ThemeBuilder } from './theme/theme-context' import type { @@ -58,6 +59,12 @@ export type EmbeddedChatbotContextValue = { } showChatMemory?: boolean setShowChatMemory: (state: boolean) => void + memoryList: Memory[] + clearAllMemory: () => void + updateMemory: (memory: Memory, content: string) => void + resetDefault: (memory: Memory) => void + clearAllUpdateVersion: (memory: Memory) => void + switchMemoryVersion: (memory: Memory, version: string) => void } export const EmbeddedChatbotContext = createContext({ @@ -90,5 +97,11 @@ export const EmbeddedChatbotContext = createContext initUserVariables: {}, showChatMemory: false, setShowChatMemory: noop, + memoryList: [], + clearAllMemory: noop, + updateMemory: noop, + resetDefault: noop, + clearAllUpdateVersion: noop, + switchMemoryVersion: noop, }) export const useEmbeddedChatbotContext = () => useContext(EmbeddedChatbotContext) diff --git a/web/app/components/base/chat/embedded-chatbot/hooks.tsx b/web/app/components/base/chat/embedded-chatbot/hooks.tsx index b7820a7f2c..b786cb81e9 100644 --- a/web/app/components/base/chat/embedded-chatbot/hooks.tsx +++ b/web/app/components/base/chat/embedded-chatbot/hooks.tsx @@ -18,11 +18,14 @@ import { CONVERSATION_ID_INFO } from '../constants' import { buildChatItemTree, getProcessedInputsFromUrlParams, getProcessedSystemVariablesFromUrlParams, getProcessedUserVariablesFromUrlParams } from '../utils' import { getProcessedFilesFromResponse } from '../../file-uploader/utils' import { + deleteMemory, + editMemory, fetchAppInfo, fetchAppMeta, fetchAppParams, fetchChatList, fetchConversations, + fetchMemories, generationConversationName, updateFeedback, } from '@/service/share' @@ -38,6 +41,7 @@ import { addFileInfos, sortAgentSorts } from '@/app/components/tools/utils' import { noop } from 'lodash-es' import { useGetUserCanAccessApp } from '@/service/access-control' import { useGlobalPublicStore } from '@/context/global-public-context' +import type { Memory } from '@/app/components/base/chat/types' function getFormattedChatList(messages: any[]) { const newChatList: ChatItem[] = [] @@ -398,6 +402,59 @@ export const useEmbeddedChatbot = () => { }, [isInstalledApp, appId, t, notify]) const [showChatMemory, setShowChatMemory] = useState(false) + const [memoryList, setMemoryList] = useState([]) + + const getMemoryList = useCallback(async (currentConversationId: string) => { + const memories = await fetchMemories(currentConversationId, '', '', isInstalledApp, appId) + setMemoryList(memories) + }, [isInstalledApp, appId]) + + const clearAllMemory = useCallback(async () => { + await deleteMemory('', isInstalledApp, appId) + notify({ type: 'success', message: t('common.api.success') }) + getMemoryList(currentConversationId) + }, [currentConversationId, getMemoryList]) + + const resetDefault = useCallback(async (memory: Memory) => { + try { + await editMemory(memory.spec.id, memory.spec.template, isInstalledApp, appId) + getMemoryList(currentConversationId) + } + catch (error) { + console.error('Failed to reset memory:', error) + } + }, [currentConversationId, getMemoryList, isInstalledApp, appId]) + + const clearAllUpdateVersion = useCallback(async (memory: Memory) => { + await deleteMemory(memory.spec.id, isInstalledApp, appId) + notify({ type: 'success', message: t('common.api.success') }) + getMemoryList(currentConversationId) + }, [currentConversationId, getMemoryList]) + + const switchMemoryVersion = useCallback(async (memory: Memory, version: string) => { + const memories = await fetchMemories(currentConversationId, memory.spec.id, version, isInstalledApp, appId) + const newMemory = memories[0] + const newList = produce(memoryList, (draft) => { + const index = draft.findIndex(item => item.spec.id === memory.spec.id) + if (index !== -1) + draft[index] = newMemory + }) + setMemoryList(newList) + }, [memoryList, currentConversationId, isInstalledApp, appId]) + + const updateMemory = useCallback(async (memory: Memory, content: string) => { + try { + await editMemory(memory.spec.id, content, isInstalledApp, appId) + getMemoryList(currentConversationId) + } + catch (error) { + console.error('Failed to reset memory:', error) + } + }, [getMemoryList, currentConversationId, isInstalledApp, appId]) + + useEffect(() => { + getMemoryList(currentConversationId) + }, [currentConversationId, getMemoryList]) return { appInfoError, @@ -443,5 +500,11 @@ export const useEmbeddedChatbot = () => { initUserVariables, showChatMemory, setShowChatMemory, + memoryList, + clearAllMemory, + updateMemory, + resetDefault, + clearAllUpdateVersion, + switchMemoryVersion, } } diff --git a/web/app/components/base/chat/embedded-chatbot/index.tsx b/web/app/components/base/chat/embedded-chatbot/index.tsx index f6d0838b15..fa6af90e8e 100644 --- a/web/app/components/base/chat/embedded-chatbot/index.tsx +++ b/web/app/components/base/chat/embedded-chatbot/index.tsx @@ -33,6 +33,12 @@ const Chatbot = () => { themeBuilder, showChatMemory, setShowChatMemory, + memoryList, + clearAllMemory, + updateMemory, + resetDefault, + clearAllUpdateVersion, + switchMemoryVersion, } = useEmbeddedChatbotContext() const { t } = useTranslation() const systemFeatures = useGlobalPublicStore(s => s.systemFeatures) @@ -99,9 +105,15 @@ const Chatbot = () => { >
e.stopPropagation()}>
@@ -150,6 +162,12 @@ const EmbeddedChatbotWrapper = () => { initUserVariables, showChatMemory, setShowChatMemory, + memoryList, + clearAllMemory, + updateMemory, + resetDefault, + clearAllUpdateVersion, + switchMemoryVersion, } = useEmbeddedChatbot() return { initUserVariables, showChatMemory, setShowChatMemory, + memoryList, + clearAllMemory, + updateMemory, + resetDefault, + clearAllUpdateVersion, + switchMemoryVersion, }}>