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
Signed| @@ -177,6 +177,8 @@ import { | ||
| 177 | 177 | renderPaginationDropdown, |
| 178 | 178 | paginationDropdownChangeHandler, |
| 179 | 179 | importFromExternalUrl, |
| 180 | + shiftUpByOne, | |
| 181 | + shiftDownByOne, | |
| 180 | 182 | } from './scripts/utils.js'; |
| 181 | 183 | import { debounce_timeout, GENERATION_TYPE_TRIGGERS, IGNORE_SYMBOL, inject_ids } from './scripts/constants.js'; |
| 182 | 184 | |
| @@ -3910,14 +3912,14 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | ||
| 3910 | 3912 | // This operation will result in the injectedIndices indexes being off by one |
| 3911 | 3913 | coreChat.push({ mes: jailbreak, is_user: true }); |
| 3912 | 3914 | // 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); | |
| 3914 | 3916 | } |
| 3915 | 3917 | } |
| 3916 | 3918 | } |
| 3917 | 3919 | |
| 3918 | 3920 | let chat2 = []; |
| 3919 | 3921 | let continue_mag = ''; |
| 3920 | 3922 | constlet userMessageIndices = []; |
| 3921 | 3923 | const lastUserMessageIndex = coreChat.findLastIndex(x => x.is_user); |
| 3922 | 3924 | |
| 3923 | 3925 | 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 | ||
| 4016 | 4018 | // Only add the chat in context if past the greeting message |
| 4017 | 4019 | if (isContinue && (chat2.length > 1 || main_api === 'openai')) { |
| 4018 | 4020 | 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); | |
| 4019 | 4024 | } |
| 4020 | 4025 | |
| 4021 | 4026 | // Collect enough messages to fill the context |
| 4022 | 4027 | let arrMes = new Array(chat2.length); |
| 4023 | 4028 | let tokenCount = await getMessagesTokenCount(); |
| 4024 | 4029 | let lastAddedIndex = -10; |
| 4025 | 4030 | |
| 4026 | 4031 | // Pre-allocate all injections first. |
| 4027 | 4032 | // If it doesn't fit - user shot himself in the foot |
| 4028 | 4033 | for (const index of injectedIndices) { |
| 4034 | + // not needed for OAI prompting | |
| 4035 | + if (main_api == 'openai') { | |
| 4036 | + break; | |
| 4037 | + } | |
| 4038 | + | |
| 4029 | 4039 | const item = chat2[index]; |
| 4030 | 4040 | |
| 4031 | 4041 | if (typeof item !== 'string') { |
| @@ -4768,7 +4778,7 @@ export function stopGeneration() { | ||
| 4768 | 4778 | * @returns {Promise<number[]>} Array of indices where the extension prompts were injected |
| 4769 | 4779 | */ |
| 4770 | 4780 | async function doChatInject(messages, isContinue) { |
| 4771 | 4781 | const injectedIndicesinjectedMessages = []; |
| 4772 | 4782 | let totalInsertedMessages = 0; |
| 4773 | 4783 | messages.reverse(); |
| 4774 | 4784 | |
| @@ -4808,10 +4818,11 @@ async function doChatInject(messages, isContinue) { | ||
| 4808 | 4818 | const injectIdx = Math.min(depth + totalInsertedMessages, messages.length); |
| 4809 | 4819 | messages.splice(injectIdx, 0, ...roleMessages); |
| 4810 | 4820 | totalInsertedMessages += roleMessages.length; |
| 4811 | - injectedIndices.push(...Array.from({ length: roleMessages.length }, (_, i) => injectIdx + i)); | |
| 4821 | + injectedMessages.push(...roleMessages); | |
| 4812 | 4822 | } |
| 4813 | 4823 | } |
| 4814 | 4824 | |
| 4825 | + const injectedIndices = injectedMessages.map(msg => messages.indexOf(msg)); | |
| 4815 | 4826 | messages.reverse(); |
| 4816 | 4827 | return injectedIndices; |
| 4817 | 4828 | } |
| @@ -18,6 +18,9 @@ import { groups, selected_group } from './group-chats.js'; | ||
| 18 | 18 | import { getCurrentLocale, t } from './i18n.js'; |
| 19 | 19 | import { importWorldInfo } from './world-info.js'; |
| 20 | 20 | |
| 21 | +export const shiftUpByOne = (e, i, a) => a[i] = e + 1; | |
| 22 | +export const shiftDownByOne = (e, i, a) => a[i] = e - 1; | |
| 23 | + | |
| 21 | 24 | /** |
| 22 | 25 | * Pagination status string template. |
| 23 | 26 | * @type {string} |