getMessagesCallback: refactor to async

This commit is contained in:
Succubyss 2024-08-18 13:49:23 -05:00
parent cfb40cae0b
commit 5e354f22c5

View File

@ -1835,7 +1835,7 @@ async function popupCallback(args, value) {
return String(value); return String(value);
} }
function getMessagesCallback(args, value) { async function getMessagesCallback(args, value) {
const includeNames = !isFalseBoolean(args?.names); const includeNames = !isFalseBoolean(args?.names);
const includeHidden = isTrueBoolean(args?.hidden); const includeHidden = isTrueBoolean(args?.hidden);
const role = args?.role; const role = args?.role;
@ -1868,30 +1868,36 @@ function getMessagesCallback(args, value) {
throw new Error(`Invalid role provided. Expected one of: system, assistant, user. Got: ${role}`); throw new Error(`Invalid role provided. Expected one of: system, assistant, user. Got: ${role}`);
}; };
const messages = []; const processMessage = async (mesId) => {
const msg = chat[mesId];
for (let messageId = range.start; messageId <= range.end; messageId++) { if (!msg) {
const message = chat[messageId]; console.warn(`WARN: No message found with ID ${mesId}`);
if (!message) { return null;
console.warn(`WARN: No message found with ID ${messageId}`);
continue;
} }
if (role && !filterByRole(message)) { if (role && !filterByRole(msg)) {
console.debug(`/messages: Skipping message with ID ${messageId} due to role filter`); console.debug(`/messages: Skipping message with ID ${mesId} due to role filter`);
continue; return null;
} }
if (!includeHidden && message.is_system) { if (!includeHidden && msg.is_system) {
console.debug(`/messages: Skipping hidden message with ID ${messageId}`); console.debug(`/messages: Skipping hidden message with ID ${mesId}`);
continue; return null;
} }
if (includeNames) { return includeNames ? `${msg.name}: ${msg.mes}` : msg.mes;
messages.push(`${message.name}: ${message.mes}`); };
} else {
messages.push(message.mes); const messagePromises = new Array(range.end - range.start + 1);
}
for (let rInd = range.start; rInd <= range.end; ++rInd)
messagePromises[rInd - range.start] = processMessage(rInd);
const messages = await Promise.all(messagePromises);
for (let i = 0; i < messages.length; /**/ ) {
if (messages[i] !== null) ++i;
else messages.splice(i, 1);
} }
return messages.join('\n\n'); return messages.join('\n\n');