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 -1Ignore whitespace
public/script.js+4 -1
@@ -3796,7 +3796,10 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
37963796 coreChat.splice(coreChat.length - 1, 0, { mes: jailbreak, is_user: true });
37973797 }
37983798 else {
3799+ // This operation will result in the injectedIndices indexes being off by one
37993800 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);
38003803 }
38013804 }
38023805 }
@@ -4757,7 +4760,7 @@ async function doChatInject(messages, isContinue) {
47574760
47584761 if (roleMessages.length) {
47594762 const depth = isContinue && i === 0 ? 1 : i;
47604763 const injectIdx = Math.min(depth + totalInsertedMessages, messages.length);
47614764 messages.splice(injectIdx, 0, ...roleMessages);
47624765 totalInsertedMessages += roleMessages.length;
47634766 injectedIndices.push(...Array.from({ length: roleMessages.length }, (_, i) => injectIdx + i));