diff --git a/public/script.js b/public/script.js index 49f265b5a..3b17f29f2 100644 --- a/public/script.js +++ b/public/script.js @@ -448,6 +448,7 @@ export const event_types = { CHARACTER_DELETED: 'characterDeleted', CHARACTER_DUPLICATED: 'character_duplicated', SMOOTH_STREAM_TOKEN_RECEIVED: 'smooth_stream_token_received', + FILE_ATTACHMENT_DELETED: 'file_attachment_deleted', }; export const eventSource = new EventEmitter(); diff --git a/public/scripts/chats.js b/public/scripts/chats.js index 018379bc9..4adf43d5a 100644 --- a/public/scripts/chats.js +++ b/public/scripts/chats.js @@ -558,6 +558,7 @@ async function deleteFileFromServer(url) { throw new Error(error); } + await eventSource.emit(event_types.FILE_ATTACHMENT_DELETED, url); return true; } catch (error) { toastr.error(String(error), 'Could not delete file'); diff --git a/public/scripts/extensions/vectors/index.js b/public/scripts/extensions/vectors/index.js index 3f48f9270..377878d8b 100644 --- a/public/scripts/extensions/vectors/index.js +++ b/public/scripts/extensions/vectors/index.js @@ -353,7 +353,8 @@ async function processFiles(chat) { message.mes = message.mes.substring(message.extra.fileLength); const fileName = message.extra.file.name; - const collectionId = `file_${getStringHash(fileName)}`; + const fileUrl = message.extra.file.url; + const collectionId = `file_${getStringHash(fileUrl)}`; const hashesInCollection = await getSavedHashes(collectionId); // File is already in the collection @@ -748,6 +749,37 @@ async function queryMultipleCollections(collectionIds, searchText, topK) { return await response.json(); } +/** + * Purges the vector index for a file. + * @param {string} fileUrl File URL to purge + */ +async function purgeFileVectorIndex(fileUrl) { + try { + if (!settings.enabled_files) { + return; + } + + console.log(`Vectors: Purging file vector index for ${fileUrl}`); + const collectionId = `file_${getStringHash(fileUrl)}`; + + const response = await fetch('/api/vector/purge', { + method: 'POST', + headers: getRequestHeaders(), + body: JSON.stringify({ + collectionId: collectionId, + }), + }); + + if (!response.ok) { + throw new Error(`Could not delete vector index for collection ${collectionId}`); + } + + console.log(`Vectors: Purged vector index for collection ${collectionId}`); + } catch (error) { + console.error('Vectors: Failed to purge file', error); + } +} + /** * Purges the vector index for a collection. * @param {string} collectionId Collection ID to purge @@ -1025,4 +1057,5 @@ jQuery(async () => { eventSource.on(event_types.MESSAGE_SWIPED, onChatEvent); eventSource.on(event_types.CHAT_DELETED, purgeVectorIndex); eventSource.on(event_types.GROUP_CHAT_DELETED, purgeVectorIndex); + eventSource.on(event_types.FILE_ATTACHMENT_DELETED, purgeFileVectorIndex); });