mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Prevent sync and generation at the same time
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
import { eventSource, event_types, extension_prompt_types, getCurrentChatId, getRequestHeaders, saveSettingsDebounced, setExtensionPrompt, substituteParams } from "../../../script.js";
|
import { eventSource, event_types, extension_prompt_types, getCurrentChatId, getRequestHeaders, is_send_press, saveSettingsDebounced, setExtensionPrompt, substituteParams } from "../../../script.js";
|
||||||
import { ModuleWorkerWrapper, extension_settings, getContext, renderExtensionTemplate } from "../../extensions.js";
|
import { ModuleWorkerWrapper, extension_settings, getContext, renderExtensionTemplate } from "../../extensions.js";
|
||||||
import { collapseNewlines, power_user, ui_mode } from "../../power-user.js";
|
import { collapseNewlines, power_user, ui_mode } from "../../power-user.js";
|
||||||
import { debounce, getStringHash as calculateHash } from "../../utils.js";
|
import { debounce, getStringHash as calculateHash, waitUntilCondition } from "../../utils.js";
|
||||||
|
|
||||||
const MODULE_NAME = 'vectors';
|
const MODULE_NAME = 'vectors';
|
||||||
|
|
||||||
@ -35,6 +35,11 @@ async function onVectorizeAllClick() {
|
|||||||
$('#vectorize_progress_eta').text('...');
|
$('#vectorize_progress_eta').text('...');
|
||||||
|
|
||||||
while (!finished) {
|
while (!finished) {
|
||||||
|
if (is_send_press) {
|
||||||
|
toastr.info('Message generation is in progress.', 'Vectorization aborted');
|
||||||
|
throw new Error('Message generation is in progress.');
|
||||||
|
}
|
||||||
|
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
const remaining = await synchronizeChat(batchSize);
|
const remaining = await synchronizeChat(batchSize);
|
||||||
const elapsed = Date.now() - startTime;
|
const elapsed = Date.now() - startTime;
|
||||||
@ -44,7 +49,8 @@ async function onVectorizeAllClick() {
|
|||||||
const total = getContext().chat.length;
|
const total = getContext().chat.length;
|
||||||
const processed = total - remaining;
|
const processed = total - remaining;
|
||||||
const processedPercent = Math.round((processed / total) * 100); // percentage of the work done
|
const processedPercent = Math.round((processed / total) * 100); // percentage of the work done
|
||||||
const averageElapsed = elapsedLog.slice(-5).reduce((a, b) => a + b, 0) / elapsedLog.length; // average time needed to process one item
|
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
|
||||||
const pace = averageElapsed / batchSize; // time needed to process one item
|
const pace = averageElapsed / batchSize; // time needed to process one item
|
||||||
const remainingTime = Math.round(pace * remaining / 1000);
|
const remainingTime = Math.round(pace * remaining / 1000);
|
||||||
|
|
||||||
@ -62,12 +68,22 @@ async function onVectorizeAllClick() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let syncBlocked = false;
|
||||||
|
|
||||||
async function synchronizeChat(batchSize = 5) {
|
async function synchronizeChat(batchSize = 5) {
|
||||||
|
try {
|
||||||
|
await waitUntilCondition(() => !syncBlocked, 500);
|
||||||
|
} catch {
|
||||||
|
console.log('Vectors: Synchronization blocked by another process');
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!settings.enabled) {
|
if (!settings.enabled) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
syncBlocked = true;
|
||||||
const context = getContext();
|
const context = getContext();
|
||||||
const chatId = getCurrentChatId();
|
const chatId = getCurrentChatId();
|
||||||
|
|
||||||
@ -83,8 +99,8 @@ async function synchronizeChat(batchSize = 5) {
|
|||||||
const deletedHashes = hashesInCollection.filter(x => !hashedMessages.some(y => y.hash === x));
|
const deletedHashes = hashesInCollection.filter(x => !hashedMessages.some(y => y.hash === x));
|
||||||
|
|
||||||
if (newVectorItems.length > 0) {
|
if (newVectorItems.length > 0) {
|
||||||
|
console.log(`Vectors: Found ${newVectorItems.length} new items. Processing ${batchSize}...`);
|
||||||
await insertVectorItems(chatId, newVectorItems.slice(0, batchSize));
|
await insertVectorItems(chatId, newVectorItems.slice(0, batchSize));
|
||||||
console.log(`Vectors: Inserted ${newVectorItems.length} new items`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (deletedHashes.length > 0) {
|
if (deletedHashes.length > 0) {
|
||||||
@ -97,6 +113,8 @@ async function synchronizeChat(batchSize = 5) {
|
|||||||
toastr.error('Check server console for more details', 'Vectorization failed');
|
toastr.error('Check server console for more details', 'Vectorization failed');
|
||||||
console.error('Vectors: Failed to synchronize chat', error);
|
console.error('Vectors: Failed to synchronize chat', error);
|
||||||
return -1;
|
return -1;
|
||||||
|
} finally {
|
||||||
|
syncBlocked = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
<label for="vectors_template">
|
<label for="vectors_template">
|
||||||
Insertion template:
|
Insertion template:
|
||||||
</label>
|
</label>
|
||||||
<textarea id="vectors_template" class="text_pole textarea_compact" rows="2" placeholder="Use {{text}} macro to specify the position of retrieved text."></textarea>
|
<textarea id="vectors_template" class="text_pole textarea_compact autoSetHeight" rows="2" placeholder="Use {{text}} macro to specify the position of retrieved text."></textarea>
|
||||||
<label for="vectors_position">Injection position:</label>
|
<label for="vectors_position">Injection position:</label>
|
||||||
<div class="radio_group">
|
<div class="radio_group">
|
||||||
<label>
|
<label>
|
||||||
|
Reference in New Issue
Block a user