fix: remove files and memory when user clean thread (#2524)
Signed-off-by: James <james@jan.ai> Co-authored-by: James <james@jan.ai>
This commit is contained in:
parent
54ba41000a
commit
1f8dc893ba
@ -1,7 +1,11 @@
|
|||||||
|
import { useCallback } from 'react'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ChatCompletionRole,
|
ChatCompletionRole,
|
||||||
ExtensionTypeEnum,
|
ExtensionTypeEnum,
|
||||||
ConversationalExtension,
|
ConversationalExtension,
|
||||||
|
fs,
|
||||||
|
joinPath,
|
||||||
} from '@janhq/core'
|
} from '@janhq/core'
|
||||||
|
|
||||||
import { useAtom, useAtomValue, useSetAtom } from 'jotai'
|
import { useAtom, useAtomValue, useSetAtom } from 'jotai'
|
||||||
@ -12,10 +16,11 @@ import { toaster } from '@/containers/Toast'
|
|||||||
|
|
||||||
import { extensionManager } from '@/extension/ExtensionManager'
|
import { extensionManager } from '@/extension/ExtensionManager'
|
||||||
|
|
||||||
|
import { janDataFolderPathAtom } from '@/helpers/atoms/AppConfig.atom'
|
||||||
import {
|
import {
|
||||||
|
chatMessages,
|
||||||
cleanChatMessageAtom as cleanChatMessagesAtom,
|
cleanChatMessageAtom as cleanChatMessagesAtom,
|
||||||
deleteChatMessageAtom as deleteChatMessagesAtom,
|
deleteChatMessageAtom as deleteChatMessagesAtom,
|
||||||
getCurrentChatMessagesAtom,
|
|
||||||
} from '@/helpers/atoms/ChatMessage.atom'
|
} from '@/helpers/atoms/ChatMessage.atom'
|
||||||
import {
|
import {
|
||||||
threadsAtom,
|
threadsAtom,
|
||||||
@ -26,27 +31,45 @@ import {
|
|||||||
|
|
||||||
export default function useDeleteThread() {
|
export default function useDeleteThread() {
|
||||||
const [threads, setThreads] = useAtom(threadsAtom)
|
const [threads, setThreads] = useAtom(threadsAtom)
|
||||||
const setCurrentPrompt = useSetAtom(currentPromptAtom)
|
const messages = useAtomValue(chatMessages)
|
||||||
const messages = useAtomValue(getCurrentChatMessagesAtom)
|
const janDataFolderPath = useAtomValue(janDataFolderPathAtom)
|
||||||
|
|
||||||
|
const setCurrentPrompt = useSetAtom(currentPromptAtom)
|
||||||
const setActiveThreadId = useSetAtom(setActiveThreadIdAtom)
|
const setActiveThreadId = useSetAtom(setActiveThreadIdAtom)
|
||||||
const deleteMessages = useSetAtom(deleteChatMessagesAtom)
|
const deleteMessages = useSetAtom(deleteChatMessagesAtom)
|
||||||
const cleanMessages = useSetAtom(cleanChatMessagesAtom)
|
const cleanMessages = useSetAtom(cleanChatMessagesAtom)
|
||||||
|
|
||||||
const deleteThreadState = useSetAtom(deleteThreadStateAtom)
|
const deleteThreadState = useSetAtom(deleteThreadStateAtom)
|
||||||
const updateThreadLastMessage = useSetAtom(updateThreadStateLastMessageAtom)
|
const updateThreadLastMessage = useSetAtom(updateThreadStateLastMessageAtom)
|
||||||
|
|
||||||
const cleanThread = async (threadId: string) => {
|
const cleanThread = useCallback(
|
||||||
if (threadId) {
|
async (threadId: string) => {
|
||||||
const thread = threads.filter((c) => c.id === threadId)[0]
|
|
||||||
cleanMessages(threadId)
|
cleanMessages(threadId)
|
||||||
|
const thread = threads.find((c) => c.id === threadId)
|
||||||
|
if (!thread) return
|
||||||
|
|
||||||
|
const updatedMessages = (messages[threadId] ?? []).filter(
|
||||||
|
(msg) => msg.role === ChatCompletionRole.System
|
||||||
|
)
|
||||||
|
|
||||||
|
// remove files
|
||||||
|
try {
|
||||||
|
const threadFolderPath = await joinPath([
|
||||||
|
janDataFolderPath,
|
||||||
|
'threads',
|
||||||
|
threadId,
|
||||||
|
])
|
||||||
|
const threadFilesPath = await joinPath([threadFolderPath, 'files'])
|
||||||
|
const threadMemoryPath = await joinPath([threadFolderPath, 'memory'])
|
||||||
|
await fs.rm(threadFilesPath)
|
||||||
|
await fs.rm(threadMemoryPath)
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('Error deleting thread files', err)
|
||||||
|
}
|
||||||
|
|
||||||
if (thread) {
|
|
||||||
await extensionManager
|
await extensionManager
|
||||||
.get<ConversationalExtension>(ExtensionTypeEnum.Conversational)
|
.get<ConversationalExtension>(ExtensionTypeEnum.Conversational)
|
||||||
?.writeMessages(
|
?.writeMessages(threadId, updatedMessages)
|
||||||
threadId,
|
|
||||||
messages.filter((msg) => msg.role === ChatCompletionRole.System)
|
|
||||||
)
|
|
||||||
|
|
||||||
thread.metadata = {
|
thread.metadata = {
|
||||||
...thread.metadata,
|
...thread.metadata,
|
||||||
@ -56,9 +79,15 @@ export default function useDeleteThread() {
|
|||||||
.get<ConversationalExtension>(ExtensionTypeEnum.Conversational)
|
.get<ConversationalExtension>(ExtensionTypeEnum.Conversational)
|
||||||
?.saveThread(thread)
|
?.saveThread(thread)
|
||||||
updateThreadLastMessage(threadId, undefined)
|
updateThreadLastMessage(threadId, undefined)
|
||||||
}
|
},
|
||||||
}
|
[
|
||||||
}
|
janDataFolderPath,
|
||||||
|
threads,
|
||||||
|
messages,
|
||||||
|
cleanMessages,
|
||||||
|
updateThreadLastMessage,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
const deleteThread = async (threadId: string) => {
|
const deleteThread = async (threadId: string) => {
|
||||||
if (!threadId) {
|
if (!threadId) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user