fixes #4324: injectedIndices becomes invalidated with a off-by-one in… (#4325) * fixes #4324: injectedIndices becomes invalidated with a off-by-one index shift when PHI/jailbreak message is added. Could lead to dropped injections/history. * Run formatter * Fix pre-allocation of injections in Text Completions prompt builder --------- Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>

e712ededc9245e84aa4e336e0f82121c092ff1e4

Aleks L.T <joink-github@toppe.no>

Signed
1 files changed, +4 -1Showing whitespace changes
public/script.js+4 -1
@@ -3796,7 +3796,10 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
3796 coreChat.splice(coreChat.length - 1, 0, { mes: jailbreak, is_user: true });3796 coreChat.splice(coreChat.length - 1, 0, { mes: jailbreak, is_user: true });
3797 }3797 }
3798 else {3798 else {
3799 // This operation will result in the injectedIndices indexes being off by one
3799 coreChat.push({ mes: jailbreak, is_user: true });3800 coreChat.push({ mes: jailbreak, is_user: true });
3801 // Add +1 to the elements to correct for the new PHI/Jailbreak message.
3802 injectedIndices.forEach((e, idx) => injectedIndices[idx] = e + 1);
3800 }3803 }
3801 }3804 }
3802 }3805 }
@@ -4757,7 +4760,7 @@ async function doChatInject(messages, isContinue) {
47574760
4758 if (roleMessages.length) {4761 if (roleMessages.length) {
4759 const depth = isContinue && i === 0 ? 1 : i;4762 const depth = isContinue && i === 0 ? 1 : i;
4760 const injectIdx = depth + totalInsertedMessages;4763 const injectIdx = Math.min(depth + totalInsertedMessages, messages.length);
4761 messages.splice(injectIdx, 0, ...roleMessages);4764 messages.splice(injectIdx, 0, ...roleMessages);
4762 totalInsertedMessages += roleMessages.length;4765 totalInsertedMessages += roleMessages.length;
4763 injectedIndices.push(...Array.from({ length: roleMessages.length }, (_, i) => injectIdx + i));4766 injectedIndices.push(...Array.from({ length: roleMessages.length }, (_, i) => injectIdx + i));