From 1278b5c309b30d14f743a4720d453cd76f8f1f64 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 3 Oct 2024 22:13:37 +0300 Subject: [PATCH] Fix character attachment content saving into settings.json --- .../scripts/extensions/attachments/index.js | 21 +++++++++++++++++++ public/scripts/extensions/vectors/index.js | 4 ++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/public/scripts/extensions/attachments/index.js b/public/scripts/extensions/attachments/index.js index 3ae36e46f..7db88dc4f 100644 --- a/public/scripts/extensions/attachments/index.js +++ b/public/scripts/extensions/attachments/index.js @@ -1,3 +1,4 @@ +import { event_types, eventSource, saveSettingsDebounced } from '../../../script.js'; import { deleteAttachment, getDataBankAttachments, getDataBankAttachmentsForSource, getFileAttachment, uploadFileAttachmentToServer } from '../../chats.js'; import { extension_settings, renderExtensionTemplateAsync } from '../../extensions.js'; import { SlashCommand } from '../../slash-commands/SlashCommand.js'; @@ -196,7 +197,27 @@ async function enableDataBankAttachment(args, value) { return ''; } +function cleanUpAttachments() { + let shouldSaveSettings = false; + if (extension_settings.character_attachments) { + Object.values(extension_settings.character_attachments).flat().filter(a => a.text).forEach(a => { + shouldSaveSettings = true; + delete a.text; + }); + } + if (Array.isArray(extension_settings.attachments)) { + extension_settings.attachments.filter(a => a.text).forEach(a => { + shouldSaveSettings = true; + delete a.text; + }); + } + if (shouldSaveSettings) { + saveSettingsDebounced(); + } +} + jQuery(async () => { + eventSource.on(event_types.APP_READY, cleanUpAttachments); const manageButton = await renderExtensionTemplateAsync('attachments', 'manage-button', {}); const attachButton = await renderExtensionTemplateAsync('attachments', 'attach-button', {}); $('#data_bank_wand_container').append(manageButton); diff --git a/public/scripts/extensions/vectors/index.js b/public/scripts/extensions/vectors/index.js index 3d1a7a539..c78004917 100644 --- a/public/scripts/extensions/vectors/index.js +++ b/public/scripts/extensions/vectors/index.js @@ -466,13 +466,13 @@ async function ingestDataBankAttachments(source) { } // Download and process the file - file.text = await getFileAttachment(file.url); + const fileText = await getFileAttachment(file.url); console.log(`Vectors: Retrieved file ${file.name} from Data Bank`); // Convert kilobytes to string length const thresholdLength = settings.size_threshold_db * 1024; // Use chunk size from settings if file is larger than threshold const chunkSize = file.size > thresholdLength ? settings.chunk_size_db : -1; - await vectorizeFile(file.text, file.name, collectionId, chunkSize, settings.overlap_percent_db); + await vectorizeFile(fileText, file.name, collectionId, chunkSize, settings.overlap_percent_db); } return dataBankCollectionIds;