mirror of https://github.com/langgenius/dify.git
memory in embedded chatbot
This commit is contained in:
parent
61e4bc6b17
commit
50a7d93ddc
|
|
@ -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<EmbeddedChatbotContextValue>({
|
||||
|
|
@ -90,5 +97,11 @@ export const EmbeddedChatbotContext = createContext<EmbeddedChatbotContextValue>
|
|||
initUserVariables: {},
|
||||
showChatMemory: false,
|
||||
setShowChatMemory: noop,
|
||||
memoryList: [],
|
||||
clearAllMemory: noop,
|
||||
updateMemory: noop,
|
||||
resetDefault: noop,
|
||||
clearAllUpdateVersion: noop,
|
||||
switchMemoryVersion: noop,
|
||||
})
|
||||
export const useEmbeddedChatbotContext = () => useContext(EmbeddedChatbotContext)
|
||||
|
|
|
|||
|
|
@ -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<Memory[]>([])
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 = () => {
|
|||
>
|
||||
<div className='flex h-full w-[360px] rounded-xl shadow-lg' onClick={e => e.stopPropagation()}>
|
||||
<MemoryPanel
|
||||
showChatMemory={showChatMemory}
|
||||
isMobile={isMobile}
|
||||
showChatMemory={showChatMemory}
|
||||
setShowChatMemory={setShowChatMemory}
|
||||
memoryList={memoryList}
|
||||
clearAllMemory={clearAllMemory}
|
||||
updateMemory={updateMemory}
|
||||
resetDefault={resetDefault}
|
||||
clearAllUpdateVersion={clearAllUpdateVersion}
|
||||
switchMemoryVersion={switchMemoryVersion}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -150,6 +162,12 @@ const EmbeddedChatbotWrapper = () => {
|
|||
initUserVariables,
|
||||
showChatMemory,
|
||||
setShowChatMemory,
|
||||
memoryList,
|
||||
clearAllMemory,
|
||||
updateMemory,
|
||||
resetDefault,
|
||||
clearAllUpdateVersion,
|
||||
switchMemoryVersion,
|
||||
} = useEmbeddedChatbot()
|
||||
|
||||
return <EmbeddedChatbotContext.Provider value={{
|
||||
|
|
@ -189,6 +207,12 @@ const EmbeddedChatbotWrapper = () => {
|
|||
initUserVariables,
|
||||
showChatMemory,
|
||||
setShowChatMemory,
|
||||
memoryList,
|
||||
clearAllMemory,
|
||||
updateMemory,
|
||||
resetDefault,
|
||||
clearAllUpdateVersion,
|
||||
switchMemoryVersion,
|
||||
}}>
|
||||
<Chatbot />
|
||||
</EmbeddedChatbotContext.Provider>
|
||||
|
|
|
|||
Loading…
Reference in New Issue