From f477f4f79015788a8f96d56ff365a900d7e902fb Mon Sep 17 00:00:00 2001 From: Juanan Pereira Date: Mon, 13 Jan 2025 18:21:00 +0100 Subject: [PATCH] fix: fixess issue #7181 --- backend/open_webui/routers/files.py | 2 ++ src/lib/components/chat/MessageInput.svelte | 17 +++++++++--- src/lib/components/common/FileItem.svelte | 2 ++ .../workspace/Knowledge/KnowledgeBase.svelte | 26 +++++++++++-------- .../Knowledge/KnowledgeBase/Files.svelte | 2 +- 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/backend/open_webui/routers/files.py b/backend/open_webui/routers/files.py index 9e36d98b7..b648fccc2 100644 --- a/backend/open_webui/routers/files.py +++ b/backend/open_webui/routers/files.py @@ -345,6 +345,8 @@ async def get_file_content_by_id(id: str, user=Depends(get_verified_user)): async def delete_file_by_id(id: str, user=Depends(get_verified_user)): file = Files.get_file_by_id(id) if file and (file.user_id == user.id or user.role == "admin"): + # We should add Chroma cleanup here + result = Files.delete_file_by_id(id) if result: try: diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 694302c6d..5ec2a6d84 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -38,6 +38,7 @@ import { generateAutoCompletion } from '$lib/apis'; import { error, text } from '@sveltejs/kit'; import Image from '../common/Image.svelte'; + import { deleteFileById } from '$lib/apis/files'; const i18n = getContext('i18n'); @@ -615,9 +616,19 @@ loading={file.status === 'uploading'} dismissible={true} edit={true} - on:dismiss={() => { - files.splice(fileIdx, 1); - files = files; + on:dismiss={async () => { + try { + if (file.id) { + // This will handle both file deletion and Chroma cleanup + await deleteFileById(localStorage.token, file.id); + } + // Remove from UI state + files.splice(fileIdx, 1); + files = files; + } catch (e) { + console.error('Error deleting file:', e); + toast.error(e); + } }} on:click={() => { console.log(file); diff --git a/src/lib/components/common/FileItem.svelte b/src/lib/components/common/FileItem.svelte index 97e0b9137..dcd5c4dd7 100644 --- a/src/lib/components/common/FileItem.svelte +++ b/src/lib/components/common/FileItem.svelte @@ -25,6 +25,8 @@ export let type: string; export let size: number; + import { deleteFileById } from '$lib/apis/files'; + let showModal = false; diff --git a/src/lib/components/workspace/Knowledge/KnowledgeBase.svelte b/src/lib/components/workspace/Knowledge/KnowledgeBase.svelte index cabcff571..82b7e0e6e 100644 --- a/src/lib/components/workspace/Knowledge/KnowledgeBase.svelte +++ b/src/lib/components/workspace/Knowledge/KnowledgeBase.svelte @@ -11,7 +11,7 @@ import { page } from '$app/stores'; import { mobile, showSidebar, knowledge as _knowledge } from '$lib/stores'; - import { updateFileDataContentById, uploadFile } from '$lib/apis/files'; + import { updateFileDataContentById, uploadFile, deleteFileById } from '$lib/apis/files'; import { addFileToKnowledgeById, getKnowledgeById, @@ -372,17 +372,21 @@ }; const deleteFileHandler = async (fileId) => { - const updatedKnowledge = await removeFileFromKnowledgeById( - localStorage.token, - id, - fileId - ).catch((e) => { - toast.error(e); - }); + try { + console.log('Starting file deletion process for:', fileId); - if (updatedKnowledge) { - knowledge = updatedKnowledge; - toast.success($i18n.t('File removed successfully.')); + // Remove from knowledge base only + const updatedKnowledge = await removeFileFromKnowledgeById(localStorage.token, id, fileId); + + console.log('Knowledge base updated:', updatedKnowledge); + + if (updatedKnowledge) { + knowledge = updatedKnowledge; + toast.success($i18n.t('File removed successfully.')); + } + } catch (e) { + console.error('Error in deleteFileHandler:', e); + toast.error(e); } }; diff --git a/src/lib/components/workspace/Knowledge/KnowledgeBase/Files.svelte b/src/lib/components/workspace/Knowledge/KnowledgeBase/Files.svelte index bdc3371da..eed0a95c8 100644 --- a/src/lib/components/workspace/Knowledge/KnowledgeBase/Files.svelte +++ b/src/lib/components/workspace/Knowledge/KnowledgeBase/Files.svelte @@ -19,7 +19,7 @@ ? ' bg-gray-50 dark:bg-gray-850' : 'bg-transparent'} hover:bg-gray-50 dark:hover:bg-gray-850 transition" {small} - {file} + item={file} name={file?.name ?? file?.meta?.name} type="file" size={file?.size ?? file?.meta?.size ?? ''}