Fix character attachment content saving into settings.json

1278b5c309b30d14f743a4720d453cd76f8f1f64

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +23 -2Ignore whitespace
public/scripts/extensions/attachments/index.js+21 -0
@@ -1,3 +1,4 @@
1import { event_types, eventSource, saveSettingsDebounced } from '../../../script.js';
1import { deleteAttachment, getDataBankAttachments, getDataBankAttachmentsForSource, getFileAttachment, uploadFileAttachmentToServer } from '../../chats.js';2import { deleteAttachment, getDataBankAttachments, getDataBankAttachmentsForSource, getFileAttachment, uploadFileAttachmentToServer } from '../../chats.js';
2import { extension_settings, renderExtensionTemplateAsync } from '../../extensions.js';3import { extension_settings, renderExtensionTemplateAsync } from '../../extensions.js';
3import { SlashCommand } from '../../slash-commands/SlashCommand.js';4import { SlashCommand } from '../../slash-commands/SlashCommand.js';
@@ -196,7 +197,27 @@ async function enableDataBankAttachment(args, value) {
196 return '';197 return '';
197}198}
198199
200function cleanUpAttachments() {
201 let shouldSaveSettings = false;
202 if (extension_settings.character_attachments) {
203 Object.values(extension_settings.character_attachments).flat().filter(a => a.text).forEach(a => {
204 shouldSaveSettings = true;
205 delete a.text;
206 });
207 }
208 if (Array.isArray(extension_settings.attachments)) {
209 extension_settings.attachments.filter(a => a.text).forEach(a => {
210 shouldSaveSettings = true;
211 delete a.text;
212 });
213 }
214 if (shouldSaveSettings) {
215 saveSettingsDebounced();
216 }
217}
218
199jQuery(async () => {219jQuery(async () => {
220 eventSource.on(event_types.APP_READY, cleanUpAttachments);
200 const manageButton = await renderExtensionTemplateAsync('attachments', 'manage-button', {});221 const manageButton = await renderExtensionTemplateAsync('attachments', 'manage-button', {});
201 const attachButton = await renderExtensionTemplateAsync('attachments', 'attach-button', {});222 const attachButton = await renderExtensionTemplateAsync('attachments', 'attach-button', {});
202 $('#data_bank_wand_container').append(manageButton);223 $('#data_bank_wand_container').append(manageButton);
public/scripts/extensions/vectors/index.js+2 -2
@@ -466,13 +466,13 @@ async function ingestDataBankAttachments(source) {
466 }466 }
467467
468 // Download and process the file468 // Download and process the file
469 file.text = await getFileAttachment(file.url);469 const fileText = await getFileAttachment(file.url);
470 console.log(`Vectors: Retrieved file ${file.name} from Data Bank`);470 console.log(`Vectors: Retrieved file ${file.name} from Data Bank`);
471 // Convert kilobytes to string length471 // Convert kilobytes to string length
472 const thresholdLength = settings.size_threshold_db * 1024;472 const thresholdLength = settings.size_threshold_db * 1024;
473 // Use chunk size from settings if file is larger than threshold473 // Use chunk size from settings if file is larger than threshold
474 const chunkSize = file.size > thresholdLength ? settings.chunk_size_db : -1;474 const chunkSize = file.size > thresholdLength ? settings.chunk_size_db : -1;
475 await vectorizeFile(file.text, file.name, collectionId, chunkSize, settings.overlap_percent_db);475 await vectorizeFile(fileText, file.name, collectionId, chunkSize, settings.overlap_percent_db);
476 }476 }
477477
478 return dataBankCollectionIds;478 return dataBankCollectionIds;