Throttle classification requests during streaming

This commit is contained in:
Cohee 2023-09-10 20:14:57 +03:00
parent c9d8d7ba64
commit 9a5e667674
1 changed files with 14 additions and 1 deletions

View File

@ -8,6 +8,7 @@ export { MODULE_NAME };
const MODULE_NAME = 'expressions'; const MODULE_NAME = 'expressions';
const UPDATE_INTERVAL = 2000; const UPDATE_INTERVAL = 2000;
const STREAMING_UPDATE_INTERVAL = 6000;
const FALLBACK_EXPRESSION = 'joy'; const FALLBACK_EXPRESSION = 'joy';
const DEFAULT_EXPRESSIONS = [ const DEFAULT_EXPRESSIONS = [
"talkinghead", "talkinghead",
@ -46,6 +47,7 @@ let lastCharacter = undefined;
let lastMessage = null; let lastMessage = null;
let spriteCache = {}; let spriteCache = {};
let inApiCall = false; let inApiCall = false;
let lastServerResponseTime = 0;
function isVisualNovelMode() { function isVisualNovelMode() {
return Boolean(!isMobile() && power_user.waifuMode && getContext().groupId); return Boolean(!isMobile() && power_user.waifuMode && getContext().groupId);
@ -566,6 +568,17 @@ async function moduleWorker() {
return; return;
} }
// Throttle classification requests during streaming
if (context.streamingProcessor && !context.streamingProcessor.isFinished) {
const now = Date.now();
const timeSinceLastServerResponse = now - lastServerResponseTime;
if (timeSinceLastServerResponse < STREAMING_UPDATE_INTERVAL) {
console.log('Streaming in progress: throttling expression update. Next update at ' + new Date(lastServerResponseTime + STREAMING_UPDATE_INTERVAL));
return;
}
}
try { try {
inApiCall = true; inApiCall = true;
let expression = await getExpressionLabel(currentLastMessage.mes); let expression = await getExpressionLabel(currentLastMessage.mes);
@ -583,7 +596,6 @@ async function moduleWorker() {
} }
await sendExpressionCall(spriteFolderName, expression, force, vnMode); await sendExpressionCall(spriteFolderName, expression, force, vnMode);
} }
catch (error) { catch (error) {
console.log(error); console.log(error);
@ -592,6 +604,7 @@ async function moduleWorker() {
inApiCall = false; inApiCall = false;
lastCharacter = context.groupId || context.characterId; lastCharacter = context.groupId || context.characterId;
lastMessage = currentLastMessage.mes; lastMessage = currentLastMessage.mes;
lastServerResponseTime = Date.now();
} }
} }