SillyTavern/public/scripts/extensions/vectors/index.js

714 lines
24 KiB
JavaScript
Raw Normal View History

2023-12-02 19:04:51 +01:00
import { eventSource, event_types, extension_prompt_types, getCurrentChatId, getRequestHeaders, is_send_press, saveSettingsDebounced, setExtensionPrompt, substituteParams } from '../../../script.js';
2024-01-24 15:51:57 +01:00
import { ModuleWorkerWrapper, extension_settings, getContext, modules, renderExtensionTemplate } from '../../extensions.js';
import { collapseNewlines } from '../../power-user.js';
2023-12-02 19:04:51 +01:00
import { SECRET_KEYS, secret_state } from '../../secrets.js';
import { debounce, getStringHash as calculateHash, waitUntilCondition, onlyUnique, splitRecursive } from '../../utils.js';
2023-09-07 23:28:06 +02:00
const MODULE_NAME = 'vectors';
export const EXTENSION_PROMPT_TAG = '3_vectors';
2023-09-07 23:28:06 +02:00
const settings = {
2023-11-29 23:01:59 +01:00
// For both
2023-09-17 13:09:24 +02:00
source: 'transformers',
2023-12-11 21:47:26 +01:00
include_wi: false,
2023-11-29 23:01:59 +01:00
// For chats
enabled_chats: false,
2023-12-02 19:04:51 +01:00
template: 'Past events: {{text}}',
2023-09-09 20:26:04 +02:00
depth: 2,
position: extension_prompt_types.IN_PROMPT,
protect: 5,
insert: 3,
query: 2,
message_chunk_size: 400,
2023-11-29 23:01:59 +01:00
// For files
enabled_files: false,
size_threshold: 10,
chunk_size: 5000,
chunk_count: 2,
2023-09-07 23:28:06 +02:00
};
const moduleWorker = new ModuleWorkerWrapper(synchronizeChat);
2023-09-08 14:25:10 +02:00
async function onVectorizeAllClick() {
try {
2023-11-29 23:01:59 +01:00
if (!settings.enabled_chats) {
2023-09-08 14:25:10 +02:00
return;
}
const chatId = getCurrentChatId();
if (!chatId) {
toastr.info('No chat selected', 'Vectorization aborted');
return;
}
2023-09-08 14:25:10 +02:00
const batchSize = 5;
const elapsedLog = [];
let finished = false;
$('#vectorize_progress').show();
$('#vectorize_progress_percent').text('0');
$('#vectorize_progress_eta').text('...');
while (!finished) {
if (is_send_press) {
toastr.info('Message generation is in progress.', 'Vectorization aborted');
throw new Error('Message generation is in progress.');
}
2023-09-08 14:25:10 +02:00
const startTime = Date.now();
const remaining = await synchronizeChat(batchSize);
const elapsed = Date.now() - startTime;
elapsedLog.push(elapsed);
finished = remaining <= 0;
const total = getContext().chat.length;
const processed = total - remaining;
const processedPercent = Math.round((processed / total) * 100); // percentage of the work done
const lastElapsed = elapsedLog.slice(-5); // last 5 elapsed times
const averageElapsed = lastElapsed.reduce((a, b) => a + b, 0) / lastElapsed.length; // average time needed to process one item
2023-09-08 14:25:10 +02:00
const pace = averageElapsed / batchSize; // time needed to process one item
const remainingTime = Math.round(pace * remaining / 1000);
$('#vectorize_progress_percent').text(processedPercent);
$('#vectorize_progress_eta').text(remainingTime);
if (chatId !== getCurrentChatId()) {
throw new Error('Chat changed');
}
}
} catch (error) {
console.error('Vectors: Failed to vectorize all', error);
} finally {
$('#vectorize_progress').hide();
}
}
let syncBlocked = false;
/**
* Splits messages into chunks before inserting them into the vector index.
* @param {object[]} items Array of vector items
* @returns {object[]} Array of vector items (possibly chunked)
*/
function splitByChunks(items) {
if (settings.message_chunk_size <= 0) {
return items;
}
const chunkedItems = [];
for (const item of items) {
const chunks = splitRecursive(item.text, settings.message_chunk_size);
for (const chunk of chunks) {
const chunkedItem = { ...item, text: chunk };
chunkedItems.push(chunkedItem);
}
}
return chunkedItems;
}
async function synchronizeChat(batchSize = 5) {
2023-11-29 23:01:59 +01:00
if (!settings.enabled_chats) {
return -1;
}
try {
await waitUntilCondition(() => !syncBlocked && !is_send_press, 1000);
} catch {
console.log('Vectors: Synchronization blocked by another process');
return -1;
}
2023-09-07 23:28:06 +02:00
try {
syncBlocked = true;
2023-09-07 23:28:06 +02:00
const context = getContext();
const chatId = getCurrentChatId();
if (!chatId || !Array.isArray(context.chat)) {
console.debug('Vectors: No chat selected');
return -1;
2023-09-07 23:28:06 +02:00
}
2023-11-29 23:01:59 +01:00
const hashedMessages = context.chat.filter(x => !x.is_system).map(x => ({ text: String(x.mes), hash: getStringHash(x.mes), index: context.chat.indexOf(x) }));
2023-09-07 23:28:06 +02:00
const hashesInCollection = await getSavedHashes(chatId);
const newVectorItems = hashedMessages.filter(x => !hashesInCollection.includes(x.hash));
const deletedHashes = hashesInCollection.filter(x => !hashedMessages.some(y => y.hash === x));
if (newVectorItems.length > 0) {
const chunkedBatch = splitByChunks(newVectorItems.slice(0, batchSize));
console.log(`Vectors: Found ${newVectorItems.length} new items. Processing ${batchSize}...`);
await insertVectorItems(chatId, chunkedBatch);
2023-09-07 23:28:06 +02:00
}
if (deletedHashes.length > 0) {
await deleteVectorItems(chatId, deletedHashes);
console.log(`Vectors: Deleted ${deletedHashes.length} old hashes`);
}
2023-09-08 12:57:27 +02:00
return newVectorItems.length - batchSize;
2023-09-07 23:28:06 +02:00
} catch (error) {
2024-01-24 15:51:57 +01:00
/**
* Gets the error message for a given cause
* @param {string} cause Error cause key
* @returns {string} Error message
*/
function getErrorMessage(cause) {
switch (cause) {
case 'api_key_missing':
return 'API key missing. Save it in the "API Connections" panel.';
case 'extras_module_missing':
return 'Extras API must provide an "embeddings" module.';
default:
return 'Check server console for more details';
}
}
2023-09-07 23:28:06 +02:00
console.error('Vectors: Failed to synchronize chat', error);
2024-01-24 15:51:57 +01:00
const message = getErrorMessage(error.cause);
toastr.error(message, 'Vectorization failed');
2023-09-09 20:36:04 +02:00
return -1;
} finally {
syncBlocked = false;
2023-09-07 23:28:06 +02:00
}
}
// Cache object for storing hash values
const hashCache = {};
/**
* Gets the hash value for a given string
* @param {string} str Input string
* @returns {number} Hash value
*/
function getStringHash(str) {
2023-09-08 12:57:27 +02:00
// Check if the hash is already in the cache
2023-12-02 16:21:57 +01:00
if (Object.hasOwn(hashCache, str)) {
2023-09-08 12:57:27 +02:00
return hashCache[str];
}
2023-09-07 23:28:06 +02:00
2023-09-08 12:57:27 +02:00
// Calculate the hash value
const hash = calculateHash(str);
2023-09-07 23:28:06 +02:00
2023-09-08 12:57:27 +02:00
// Store the hash in the cache
hashCache[str] = hash;
2023-09-07 23:28:06 +02:00
2023-09-08 12:57:27 +02:00
return hash;
2023-09-07 23:28:06 +02:00
}
2023-11-29 23:01:59 +01:00
/**
* Retrieves files from the chat and inserts them into the vector index.
* @param {object[]} chat Array of chat messages
* @returns {Promise<void>}
*/
async function processFiles(chat) {
try {
if (!settings.enabled_files) {
return;
}
for (const message of chat) {
// Message has no file
if (!message?.extra?.file) {
continue;
}
// Trim file inserted by the script
const fileText = String(message.mes)
.substring(0, message.extra.fileLength).trim()
.replace(/^```/, '').replace(/```$/, '').trim();
2023-11-29 23:01:59 +01:00
// Convert kilobytes to string length
const thresholdLength = settings.size_threshold * 1024;
// File is too small
if (fileText.length < thresholdLength) {
continue;
}
message.mes = message.mes.substring(message.extra.fileLength);
2023-11-29 23:01:59 +01:00
const fileName = message.extra.file.name;
const collectionId = `file_${getStringHash(fileName)}`;
const hashesInCollection = await getSavedHashes(collectionId);
// File is already in the collection
if (!hashesInCollection.length) {
await vectorizeFile(fileText, fileName, collectionId);
}
const queryText = getQueryText(chat);
const fileChunks = await retrieveFileChunks(queryText, collectionId);
// Wrap it back in a code block
message.mes = `\`\`\`\n${fileChunks}\n\`\`\`\n\n${message.mes}`;
2023-11-29 23:01:59 +01:00
}
} catch (error) {
console.error('Vectors: Failed to retrieve files', error);
}
}
/**
* Retrieves file chunks from the vector index and inserts them into the chat.
* @param {string} queryText Text to query
* @param {string} collectionId File collection ID
* @returns {Promise<string>} Retrieved file text
*/
async function retrieveFileChunks(queryText, collectionId) {
console.debug(`Vectors: Retrieving file chunks for collection ${collectionId}`, queryText);
const queryResults = await queryCollection(collectionId, queryText, settings.chunk_count);
console.debug(`Vectors: Retrieved ${queryResults.hashes.length} file chunks for collection ${collectionId}`, queryResults);
2024-01-26 03:18:15 +01:00
const metadata = queryResults.metadata.filter(x => x.text).sort((a, b) => a.index - b.index).map(x => x.text).filter(onlyUnique);
2023-11-29 23:01:59 +01:00
const fileText = metadata.join('\n');
return fileText;
}
/**
* Vectorizes a file and inserts it into the vector index.
* @param {string} fileText File text
* @param {string} fileName File name
* @param {string} collectionId File collection ID
*/
async function vectorizeFile(fileText, fileName, collectionId) {
try {
2023-12-02 19:04:51 +01:00
toastr.info('Vectorization may take some time, please wait...', `Ingesting file ${fileName}`);
2023-11-29 23:01:59 +01:00
const chunks = splitRecursive(fileText, settings.chunk_size);
console.debug(`Vectors: Split file ${fileName} into ${chunks.length} chunks`, chunks);
const items = chunks.map((chunk, index) => ({ hash: getStringHash(chunk), text: chunk, index: index }));
await insertVectorItems(collectionId, items);
console.log(`Vectors: Inserted ${chunks.length} vector items for file ${fileName} into ${collectionId}`);
} catch (error) {
console.error('Vectors: Failed to vectorize file', error);
}
}
2023-09-07 23:28:06 +02:00
/**
* Removes the most relevant messages from the chat and displays them in the extension prompt
2023-09-07 23:28:06 +02:00
* @param {object[]} chat Array of chat messages
*/
async function rearrangeChat(chat) {
try {
2023-09-08 12:57:27 +02:00
// Clear the extension prompt
2023-12-11 21:47:26 +01:00
setExtensionPrompt(EXTENSION_PROMPT_TAG, '', extension_prompt_types.IN_PROMPT, 0, settings.include_wi);
2023-09-08 12:57:27 +02:00
2023-11-29 23:01:59 +01:00
if (settings.enabled_files) {
await processFiles(chat);
}
if (!settings.enabled_chats) {
2023-09-07 23:28:06 +02:00
return;
}
const chatId = getCurrentChatId();
if (!chatId || !Array.isArray(chat)) {
console.debug('Vectors: No chat selected');
return;
}
2023-09-09 20:26:04 +02:00
if (chat.length < settings.protect) {
console.debug(`Vectors: Not enough messages to rearrange (less than ${settings.protect})`);
2023-09-07 23:28:06 +02:00
return;
}
const queryText = getQueryText(chat);
if (queryText.length === 0) {
console.debug('Vectors: No text to query');
return;
}
// Get the most relevant messages, excluding the last few
2024-02-24 14:56:47 +01:00
const queryResults = await queryCollection(chatId, queryText, settings.insert);
2023-11-29 23:01:59 +01:00
const queryHashes = queryResults.hashes.filter(onlyUnique);
2023-09-07 23:28:06 +02:00
const queriedMessages = [];
const insertedHashes = new Set();
2023-09-09 20:26:04 +02:00
const retainMessages = chat.slice(-settings.protect);
2023-09-07 23:28:06 +02:00
for (const message of chat) {
if (retainMessages.includes(message) || !message.mes) {
2023-09-07 23:28:06 +02:00
continue;
}
const hash = getStringHash(message.mes);
if (queryHashes.includes(hash) && !insertedHashes.has(hash)) {
2023-09-07 23:28:06 +02:00
queriedMessages.push(message);
insertedHashes.add(hash);
2023-09-07 23:28:06 +02:00
}
}
// Rearrange queried messages to match query order
// Order is reversed because more relevant are at the lower indices
queriedMessages.sort((a, b) => queryHashes.indexOf(getStringHash(b.mes)) - queryHashes.indexOf(getStringHash(a.mes)));
2023-09-07 23:28:06 +02:00
// Remove queried messages from the original chat array
for (const message of chat) {
if (queriedMessages.includes(message)) {
chat.splice(chat.indexOf(message), 1);
}
2023-09-07 23:28:06 +02:00
}
2023-09-08 12:57:27 +02:00
if (queriedMessages.length === 0) {
console.debug('Vectors: No relevant messages found');
return;
}
// Format queried messages into a single string
2023-09-09 20:26:04 +02:00
const insertedText = getPromptText(queriedMessages);
2023-12-11 21:47:26 +01:00
setExtensionPrompt(EXTENSION_PROMPT_TAG, insertedText, settings.position, settings.depth, settings.include_wi);
2023-09-07 23:28:06 +02:00
} catch (error) {
console.error('Vectors: Failed to rearrange chat', error);
}
}
2023-09-09 20:26:04 +02:00
/**
* @param {any[]} queriedMessages
* @returns {string}
*/
function getPromptText(queriedMessages) {
const queriedText = queriedMessages.map(x => collapseNewlines(`${x.name}: ${x.mes}`).trim()).join('\n\n');
console.log('Vectors: relevant past messages found.\n', queriedText);
return substituteParams(settings.template.replace(/{{text}}/i, queriedText));
}
2023-09-07 23:28:06 +02:00
window['vectors_rearrangeChat'] = rearrangeChat;
const onChatEvent = debounce(async () => await moduleWorker.update(), 500);
/**
* Gets the text to query from the chat
* @param {object[]} chat Chat messages
* @returns {string} Text to query
*/
2023-09-07 23:28:06 +02:00
function getQueryText(chat) {
let queryText = '';
let i = 0;
for (const message of chat.slice().reverse()) {
if (message.mes) {
queryText += message.mes + '\n';
i++;
}
2023-09-09 20:26:04 +02:00
if (i === settings.query) {
2023-09-07 23:28:06 +02:00
break;
}
}
return collapseNewlines(queryText).trim();
}
/**
* Gets the saved hashes for a collection
* @param {string} collectionId
* @returns {Promise<number[]>} Saved hashes
*/
async function getSavedHashes(collectionId) {
const response = await fetch('/api/vector/list', {
method: 'POST',
headers: getRequestHeaders(),
2023-09-08 12:57:27 +02:00
body: JSON.stringify({
collectionId: collectionId,
source: settings.source,
}),
2023-09-07 23:28:06 +02:00
});
if (!response.ok) {
throw new Error(`Failed to get saved hashes for collection ${collectionId}`);
}
const hashes = await response.json();
return hashes;
}
2024-01-24 15:51:57 +01:00
/**
* Add headers for the Extras API source.
* @param {object} headers Headers object
*/
function addExtrasHeaders(headers) {
console.log(`Vector source is extras, populating API URL: ${extension_settings.apiUrl}`);
Object.assign(headers, {
'X-Extras-Url': extension_settings.apiUrl,
'X-Extras-Key': extension_settings.apiKey,
});
}
2023-09-07 23:28:06 +02:00
/**
* Inserts vector items into a collection
* @param {string} collectionId - The collection to insert into
* @param {{ hash: number, text: string }[]} items - The items to insert
* @returns {Promise<void>}
*/
async function insertVectorItems(collectionId, items) {
if (settings.source === 'openai' && !secret_state[SECRET_KEYS.OPENAI] ||
settings.source === 'palm' && !secret_state[SECRET_KEYS.MAKERSUITE] ||
settings.source === 'mistral' && !secret_state[SECRET_KEYS.MISTRALAI]) {
throw new Error('Vectors: API key missing', { cause: 'api_key_missing' });
}
2024-01-24 15:51:57 +01:00
if (settings.source === 'extras' && !modules.includes('embeddings')) {
throw new Error('Vectors: Embeddings module missing', { cause: 'extras_module_missing' });
}
const headers = getRequestHeaders();
if (settings.source === 'extras') {
2024-01-24 15:51:57 +01:00
addExtrasHeaders(headers);
}
2023-09-07 23:28:06 +02:00
const response = await fetch('/api/vector/insert', {
method: 'POST',
headers: headers,
2023-09-08 12:57:27 +02:00
body: JSON.stringify({
collectionId: collectionId,
items: items,
source: settings.source,
}),
2023-09-07 23:28:06 +02:00
});
if (!response.ok) {
throw new Error(`Failed to insert vector items for collection ${collectionId}`);
}
}
/**
* Deletes vector items from a collection
* @param {string} collectionId - The collection to delete from
* @param {number[]} hashes - The hashes of the items to delete
* @returns {Promise<void>}
*/
async function deleteVectorItems(collectionId, hashes) {
const response = await fetch('/api/vector/delete', {
method: 'POST',
headers: getRequestHeaders(),
2023-09-08 12:57:27 +02:00
body: JSON.stringify({
collectionId: collectionId,
hashes: hashes,
source: settings.source,
}),
2023-09-07 23:28:06 +02:00
});
if (!response.ok) {
throw new Error(`Failed to delete vector items for collection ${collectionId}`);
}
}
/**
* @param {string} collectionId - The collection to query
* @param {string} searchText - The text to query
* @param {number} topK - The number of results to return
2023-11-29 23:01:59 +01:00
* @returns {Promise<{ hashes: number[], metadata: object[]}>} - Hashes of the results
2023-09-07 23:28:06 +02:00
*/
async function queryCollection(collectionId, searchText, topK) {
const headers = getRequestHeaders();
if (settings.source === 'extras') {
2024-01-24 15:51:57 +01:00
addExtrasHeaders(headers);
}
2023-09-07 23:28:06 +02:00
const response = await fetch('/api/vector/query', {
method: 'POST',
headers: headers,
2023-09-08 12:57:27 +02:00
body: JSON.stringify({
collectionId: collectionId,
searchText: searchText,
topK: topK,
source: settings.source,
}),
2023-09-07 23:28:06 +02:00
});
if (!response.ok) {
throw new Error(`Failed to query collection ${collectionId}`);
}
const results = await response.json();
return results;
}
2024-02-23 21:37:00 +01:00
/**
* Purges the vector index for a collection.
* @param {string} collectionId Collection ID to purge
* @returns <Promise<boolean>> True if deleted, false if not
*/
2023-09-09 21:15:47 +02:00
async function purgeVectorIndex(collectionId) {
try {
2023-11-29 23:01:59 +01:00
if (!settings.enabled_chats) {
2024-02-23 21:37:00 +01:00
return true;
2023-09-09 21:15:47 +02:00
}
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}`);
2024-02-23 21:37:00 +01:00
return true;
2023-09-09 21:15:47 +02:00
} catch (error) {
console.error('Vectors: Failed to purge', error);
2024-02-23 21:37:00 +01:00
return false;
2023-09-09 21:15:47 +02:00
}
}
2023-11-29 23:01:59 +01:00
function toggleSettings() {
$('#vectors_files_settings').toggle(!!settings.enabled_files);
$('#vectors_chats_settings').toggle(!!settings.enabled_chats);
}
async function onPurgeClick() {
const chatId = getCurrentChatId();
if (!chatId) {
toastr.info('No chat selected', 'Purge aborted');
return;
}
2024-02-23 21:37:00 +01:00
if (await purgeVectorIndex(chatId)) {
toastr.success('Vector index purged', 'Purge successful');
} else {
toastr.error('Failed to purge vector index', 'Purge failed');
}
}
async function onViewStatsClick() {
const chatId = getCurrentChatId();
if (!chatId) {
toastr.info('No chat selected');
return;
}
const hashesInCollection = await getSavedHashes(chatId);
const totalHashes = hashesInCollection.length;
const uniqueHashes = hashesInCollection.filter(onlyUnique).length;
toastr.info(`Total hashes: <b>${totalHashes}</b><br>
Unique hashes: <b>${uniqueHashes}</b><br><br>
I'll mark collected messages with a green circle.`,
`Stats for chat ${chatId}`,
{ timeOut: 10000, escapeHtml: false });
const chat = getContext().chat;
for (const message of chat) {
if (hashesInCollection.includes(getStringHash(message.mes))) {
const messageElement = $(`.mes[mesid="${chat.indexOf(message)}"]`);
messageElement.addClass('vectorized');
}
}
}
2023-09-07 23:28:06 +02:00
jQuery(async () => {
if (!extension_settings.vectors) {
extension_settings.vectors = settings;
}
2023-11-29 23:01:59 +01:00
// Migrate from old settings
if (settings['enabled']) {
settings.enabled_chats = true;
}
2023-09-07 23:28:06 +02:00
Object.assign(settings, extension_settings.vectors);
2023-09-17 13:09:24 +02:00
// Migrate from TensorFlow to Transformers
settings.source = settings.source !== 'local' ? settings.source : 'transformers';
2023-09-07 23:28:06 +02:00
$('#extensions_settings2').append(renderExtensionTemplate(MODULE_NAME, 'settings'));
2023-11-29 23:01:59 +01:00
$('#vectors_enabled_chats').prop('checked', settings.enabled_chats).on('input', () => {
settings.enabled_chats = $('#vectors_enabled_chats').prop('checked');
2023-09-07 23:28:06 +02:00
Object.assign(extension_settings.vectors, settings);
saveSettingsDebounced();
2023-11-29 23:01:59 +01:00
toggleSettings();
});
$('#vectors_enabled_files').prop('checked', settings.enabled_files).on('input', () => {
settings.enabled_files = $('#vectors_enabled_files').prop('checked');
Object.assign(extension_settings.vectors, settings);
saveSettingsDebounced();
toggleSettings();
2023-09-07 23:28:06 +02:00
});
2023-09-08 12:57:27 +02:00
$('#vectors_source').val(settings.source).on('change', () => {
settings.source = String($('#vectors_source').val());
Object.assign(extension_settings.vectors, settings);
saveSettingsDebounced();
});
2023-09-09 20:26:04 +02:00
$('#vectors_template').val(settings.template).on('input', () => {
settings.template = String($('#vectors_template').val());
Object.assign(extension_settings.vectors, settings);
saveSettingsDebounced();
});
$('#vectors_depth').val(settings.depth).on('input', () => {
settings.depth = Number($('#vectors_depth').val());
Object.assign(extension_settings.vectors, settings);
saveSettingsDebounced();
});
$('#vectors_protect').val(settings.protect).on('input', () => {
settings.protect = Number($('#vectors_protect').val());
Object.assign(extension_settings.vectors, settings);
saveSettingsDebounced();
});
$('#vectors_insert').val(settings.insert).on('input', () => {
settings.insert = Number($('#vectors_insert').val());
Object.assign(extension_settings.vectors, settings);
saveSettingsDebounced();
});
$('#vectors_query').val(settings.query).on('input', () => {
settings.query = Number($('#vectors_query').val());
Object.assign(extension_settings.vectors, settings);
saveSettingsDebounced();
});
$(`input[name="vectors_position"][value="${settings.position}"]`).prop('checked', true);
$('input[name="vectors_position"]').on('change', () => {
settings.position = Number($('input[name="vectors_position"]:checked').val());
Object.assign(extension_settings.vectors, settings);
saveSettingsDebounced();
});
2023-09-08 14:25:10 +02:00
$('#vectors_vectorize_all').on('click', onVectorizeAllClick);
$('#vectors_purge').on('click', onPurgeClick);
$('#vectors_view_stats').on('click', onViewStatsClick);
2023-09-07 23:28:06 +02:00
2023-11-29 23:01:59 +01:00
$('#vectors_size_threshold').val(settings.size_threshold).on('input', () => {
settings.size_threshold = Number($('#vectors_size_threshold').val());
Object.assign(extension_settings.vectors, settings);
saveSettingsDebounced();
});
$('#vectors_chunk_size').val(settings.chunk_size).on('input', () => {
settings.chunk_size = Number($('#vectors_chunk_size').val());
Object.assign(extension_settings.vectors, settings);
saveSettingsDebounced();
});
$('#vectors_chunk_count').val(settings.chunk_count).on('input', () => {
settings.chunk_count = Number($('#vectors_chunk_count').val());
Object.assign(extension_settings.vectors, settings);
saveSettingsDebounced();
});
2023-12-11 21:47:26 +01:00
$('#vectors_include_wi').prop('checked', settings.include_wi).on('input', () => {
settings.include_wi = !!$('#vectors_include_wi').prop('checked');
Object.assign(extension_settings.vectors, settings);
saveSettingsDebounced();
});
$('#vectors_message_chunk_size').val(settings.message_chunk_size).on('input', () => {
settings.message_chunk_size = Number($('#vectors_message_chunk_size').val());
Object.assign(extension_settings.vectors, settings);
saveSettingsDebounced();
});
2023-11-29 23:01:59 +01:00
toggleSettings();
2023-09-07 23:28:06 +02:00
eventSource.on(event_types.MESSAGE_DELETED, onChatEvent);
eventSource.on(event_types.MESSAGE_EDITED, onChatEvent);
eventSource.on(event_types.MESSAGE_SENT, onChatEvent);
eventSource.on(event_types.MESSAGE_RECEIVED, onChatEvent);
eventSource.on(event_types.MESSAGE_SWIPED, onChatEvent);
2023-09-09 21:15:47 +02:00
eventSource.on(event_types.CHAT_DELETED, purgeVectorIndex);
eventSource.on(event_types.GROUP_CHAT_DELETED, purgeVectorIndex);
2023-09-07 23:28:06 +02:00
});