Fix off-by-one cases in user prompt filler logic (#4648) * Fix off-by-one cases in user prompt filler logic Fixes #4645 * Skip inject pre-allocation in CC prompts * Fix filler insert on continuing on user message

a559f45ad4025263c083752cc5870541c6bc784c

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
2 files changed, +19 -5Showing whitespace changes
public/script.js+16 -5
@@ -177,6 +177,8 @@ import {
177177 renderPaginationDropdown,
178178 paginationDropdownChangeHandler,
179179 importFromExternalUrl,
180+ shiftUpByOne,
181+ shiftDownByOne,
180182} from './scripts/utils.js';
181183import { debounce_timeout, GENERATION_TYPE_TRIGGERS, IGNORE_SYMBOL, inject_ids } from './scripts/constants.js';
182184
@@ -3910,14 +3912,14 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
39103912 // This operation will result in the injectedIndices indexes being off by one
39113913 coreChat.push({ mes: jailbreak, is_user: true });
39123914 // Add +1 to the elements to correct for the new PHI/Jailbreak message.
3913- injectedIndices.forEach((e, idx) => injectedIndices[idx] = e + 1);
3915+ injectedIndices.forEach(shiftUpByOne);
39143916 }
39153917 }
39163918 }
39173919
39183920 let chat2 = [];
39193921 let continue_mag = '';
39203922 constlet userMessageIndices = [];
39213923 const lastUserMessageIndex = coreChat.findLastIndex(x => x.is_user);
39223924
39233925 for (let i = coreChat.length - 1, j = 0; i >= 0; i--, j++) {
@@ -4016,16 +4018,24 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
40164018 // Only add the chat in context if past the greeting message
40174019 if (isContinue && (chat2.length > 1 || main_api === 'openai')) {
40184020 cyclePrompt = chat2.shift();
4021+ // Adjust indices to account for the shift
4022+ injectedIndices = injectedIndices.map(shiftDownByOne).filter(x => x >= 0);
4023+ userMessageIndices = userMessageIndices.map(shiftDownByOne).filter(x => x >= 0);
40194024 }
40204025
40214026 // Collect enough messages to fill the context
40224027 let arrMes = new Array(chat2.length);
40234028 let tokenCount = await getMessagesTokenCount();
40244029 let lastAddedIndex = -10;
40254030
40264031 // Pre-allocate all injections first.
40274032 // If it doesn't fit - user shot himself in the foot
40284033 for (const index of injectedIndices) {
4034+ // not needed for OAI prompting
4035+ if (main_api == 'openai') {
4036+ break;
4037+ }
4038+
40294039 const item = chat2[index];
40304040
40314041 if (typeof item !== 'string') {
@@ -4768,7 +4778,7 @@ export function stopGeneration() {
47684778 * @returns {Promise<number[]>} Array of indices where the extension prompts were injected
47694779 */
47704780async function doChatInject(messages, isContinue) {
47714781 const injectedIndicesinjectedMessages = [];
47724782 let totalInsertedMessages = 0;
47734783 messages.reverse();
47744784
@@ -4808,10 +4818,11 @@ async function doChatInject(messages, isContinue) {
48084818 const injectIdx = Math.min(depth + totalInsertedMessages, messages.length);
48094819 messages.splice(injectIdx, 0, ...roleMessages);
48104820 totalInsertedMessages += roleMessages.length;
4811- injectedIndices.push(...Array.from({ length: roleMessages.length }, (_, i) => injectIdx + i));
4821+ injectedMessages.push(...roleMessages);
48124822 }
48134823 }
48144824
4825+ const injectedIndices = injectedMessages.map(msg => messages.indexOf(msg));
48154826 messages.reverse();
48164827 return injectedIndices;
48174828}
public/scripts/utils.js+3 -0
@@ -18,6 +18,9 @@ import { groups, selected_group } from './group-chats.js';
1818import { getCurrentLocale, t } from './i18n.js';
1919import { importWorldInfo } from './world-info.js';
2020
21+export const shiftUpByOne = (e, i, a) => a[i] = e + 1;
22+export const shiftDownByOne = (e, i, a) => a[i] = e - 1;
23+
2124/**
2225 * Pagination status string template.
2326 * @type {string}