Merge branch 'staging' into feat/ext-manager-toolbar

572b60d0c13dbf3ba4a2ab47c0a80f6905d3c8d1

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

12 files changed, +290 -89Showing whitespace changes
.github/workflows/issues-updates-on-merge.yml+3 -3
@@ -36,10 +36,10 @@ jobs:
3636 for ISSUE in $(echo $issues | jq -r '.[]'); do
3737 if [ "${{ github.ref }}" == "refs/heads/staging" ]; then
3838 LABEL="βœ… Done (staging)"
3939 gh issue edit $ISSUE -R ${{ github.repository }} --add-label "$LABEL" --remove-label "πŸ§‘β€πŸ’» In Progress"
4040 elif [ "${{ github.ref }}" == "refs/heads/release" ]; then
4141 LABEL="βœ… Done"
4242 gh issue edit $ISSUE -R ${{ github.repository }} --add-label "$LABEL" --remove-label "πŸ§‘β€πŸ’» In Progress"
4343 fi
4444 echo "Added label '$LABEL' to(and removed 'πŸ§‘β€πŸ’» In Progress' if present) in issue #$ISSUE"
4545 done
.github/workflows/pr-auto-manager.yml+2 -2
@@ -262,6 +262,6 @@ jobs:
262262 GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
263263 run: |
264264 for ISSUE in $(echo $final_issues | jq -r '.[]'); do
265265 gh issue edit $ISSUE -R ${{ github.repository }} --add-label "βœ… Done (staging)" --remove-label "πŸ§‘β€πŸ’» In Progress"
266266 echo "Added label 'βœ… Done (staging)' to(and removed 'πŸ§‘β€πŸ’» In Progress' if present) in issue #$ISSUE"
267267 done
public/index.html+1 -0
@@ -5998,6 +5998,7 @@
59985998 </div>
59995999 </div>
60006000 </div>
6001+ <i class="menu_button move_entry_button fa-solid fa-right-left" title="Move Entry to Another Lorebook" data-i18n="[title]Move Entry to Another Lorebook"></i>
60016002 <i class="menu_button duplicate_entry_button fa-solid fa-paste" title="Duplicate world info entry" data-i18n="[title]Duplicate world info entry" type="submit" value=""></i>
60026003 <i class="menu_button delete_entry_button fa-solid fa-trash-can" title="Delete world info entry" data-i18n="[title]Delete world info entry" type="submit" value=""></i>
60036004 </div>
public/scripts/PromptManager.js+2 -30
@@ -1,6 +1,6 @@
11'use strict';
22
33import { DOMPurify, Popper } from '../lib.js';
44
55import { event_types, eventSource, is_send_press, main_api, substituteParams } from '../script.js';
66import { is_group_generating } from './group-chats.js';
@@ -1440,36 +1440,8 @@ class PromptManager {
14401440 footerDiv.querySelector('select').selectedIndex = selectedPromptIndex;
14411441
14421442 // Add prompt export dialogue and options
1443-
1444- const exportForCharacter = await renderTemplateAsync('promptManagerExportForCharacter');
1445- const exportPopup = await renderTemplateAsync('promptManagerExportPopup', { isGlobalStrategy: 'global' === this.configuration.promptOrder.strategy, exportForCharacter });
1446- rangeBlockDiv.insertAdjacentHTML('beforeend', exportPopup);
1447-
1448- // Destroy previous popper instance if it exists
1449- if (this.exportPopper) {
1450- this.exportPopper.destroy();
1451- }
1452-
1453- this.exportPopper = Popper.createPopper(
1454- document.getElementById('prompt-manager-export'),
1455- document.getElementById('prompt-manager-export-format-popup'),
1456- { placement: 'bottom' },
1457- );
1458-
1459- const showExportSelection = () => {
1460- const popup = document.getElementById('prompt-manager-export-format-popup');
1461- const show = popup.hasAttribute('data-show');
1462-
1463- if (show) popup.removeAttribute('data-show');
1464- else popup.setAttribute('data-show', '');
1465-
1466- this.exportPopper.update();
1467- };
1468-
14691443 footerDiv.querySelector('#prompt-manager-import').addEventListener('click', this.handleImport);
14701444 footerDiv.querySelector('#prompt-manager-export').addEventListener('click', showExportSelectionthis.handleFullExport);
1471- rangeBlockDiv.querySelector('.export-promptmanager-prompts-full').addEventListener('click', this.handleFullExport);
1472- rangeBlockDiv.querySelector('.export-promptmanager-prompts-character')?.addEventListener('click', this.handleCharacterExport);
14731445 }
14741446 }
14751447
public/scripts/custom-request.js+67 -8
@@ -2,7 +2,7 @@ import { getPresetManager } from './preset-manager.js';
22import { extractMessageFromData, getGenerateUrl, getRequestHeaders } from '../script.js';
33import { getTextGenServer } from './textgen-settings.js';
44import { extractReasoningFromData } from './reasoning.js';
55import { formatInstructModeChat, formatInstructModePrompt, getInstructStoppingSequences, names_behavior_types } from './instruct-mode.js';
66import { getStreamingReply, tryParseStreamingError } from './openai.js';
77import EventSourceStream from './sse-stream.js';
88
@@ -190,6 +190,7 @@ export class TextCompletionService {
190190 * @param {Object} options - Configuration options
191191 * @param {string?} [options.presetName] - Name of the preset to use for generation settings
192192 * @param {string?} [options.instructName] - Name of instruct preset for message formatting
193+ * @param {Partial<InstructSettings>?} [options.instructSettings] - Override instruct settings
193194 * @param {boolean} extractData - Whether to extract structured data from response
194195 * @param {AbortSignal?} [signal]
195196 * @returns {Promise<ExtractedData | (() => AsyncGenerator<StreamResponse>)>} If not streaming, returns extracted data; if streaming, returns a function that creates an AsyncGenerator
@@ -222,15 +223,20 @@ export class TextCompletionService {
222223 }
223224 }
224225
226+
227+ /** @type {InstructSettings | undefined} */
228+ let instructPreset;
225229 // Handle instruct formatting if requested
226230 if (Array.isArray(prompt) && instructName) {
227231 const instructPresetManager = getPresetManager('instruct');
228232 let instructPreset = instructPresetManager?.getCompletionPresetByName(instructName);
229233 if (instructPreset) {
230234 // Clone the preset to avoid modifying the original
231235 instructPreset = structuredClone(instructPreset);
232- instructPreset.macro = false;
233236 instructPreset.names_behavior = names_behavior_types.NONE;
237+ if (options.instructSettings) {
238+ Object.assign(instructPreset, options.instructSettings);
239+ }
234240
235241 // Format messages using instruct formatting
236242 const formattedMessages = [];
@@ -266,10 +272,9 @@ export class TextCompletionService {
266272 formattedMessages.push(messageContent);
267273 }
268274 requestData.prompt = formattedMessages.join('');
269- if (instructPreset.output_suffix) {
275+ const stoppingStrings = getInstructStoppingSequences({ customInstruct: instructPreset, useStopStrings: false });
270276 requestData.stop = [instructPreset.output_suffix]stoppingStrings;
271277 requestData.stopping_strings = [instructPreset.output_suffix]stoppingStrings;
272- }
273278 } else {
274279 console.warn(`Instruct preset "${instructName}" not found, using basic formatting`);
275280 requestData.prompt = prompt.map(x => x.content).join('\n\n');
@@ -283,7 +288,61 @@ export class TextCompletionService {
283288 // @ts-ignore
284289 const data = this.createRequestData(requestData);
285290
286291 returnconst response = await this.sendRequest(data, extractData, signal);
292+ // Remove stopping strings from the end
293+ if (!data.stream && extractData) {
294+ /** @type {ExtractedData} */
295+ // @ts-ignore
296+ const extractedData = response;
297+
298+ let message = extractedData.content;
299+
300+ message = message.replace(/[^\S\r\n]+$/gm, '');
301+
302+ if (requestData.stopping_strings) {
303+ for (const stoppingString of requestData.stopping_strings) {
304+ if (stoppingString.length) {
305+ for (let j = stoppingString.length; j > 0; j--) {
306+ if (message.slice(-j) === stoppingString.slice(0, j)) {
307+ message = message.slice(0, -j);
308+ break;
309+ }
310+ }
311+ }
312+ }
313+ }
314+
315+ if (instructPreset) {
316+ [
317+ instructPreset.stop_sequence,
318+ instructPreset.input_sequence,
319+ ].forEach(sequence => {
320+ if (sequence?.trim()) {
321+ const index = message.indexOf(sequence);
322+ if (index !== -1) {
323+ message = message.substring(0, index);
324+ }
325+ }
326+ });
327+
328+ [
329+ instructPreset.output_sequence,
330+ instructPreset.last_output_sequence,
331+ ].forEach(sequences => {
332+ if (sequences) {
333+ sequences.split('\n')
334+ .filter(line => line.trim() !== '')
335+ .forEach(line => {
336+ message = message.replaceAll(line, '');
337+ });
338+ }
339+ });
340+ }
341+
342+ extractedData.content = message;
343+ }
344+
345+ return response;
287346 }
288347
289348 /**
public/scripts/extensions/shared.js+10 -2
@@ -285,6 +285,7 @@ export class ConnectionManagerRequestService {
285285 extractData: true,
286286 includePreset: true,
287287 includeInstruct: true,
288+ instructSettings: {},
288289 };
289290
290291 static getAllowedTypes() {
@@ -298,11 +299,17 @@ export class ConnectionManagerRequestService {
298299 * @param {string} profileId
299300 * @param {string | (import('../custom-request.js').ChatCompletionMessage & {ignoreInstruct?: boolean})[]} prompt
300301 * @param {number} maxTokens
301- * @param {{stream?: boolean, signal?: AbortSignal, extractData?: boolean, includePreset?: boolean, includeInstruct?: boolean}} custom - default values are true
302+ * @param {Object} custom
303+ * @param {boolean?} [custom.stream=false]
304+ * @param {AbortSignal?} [custom.signal]
305+ * @param {boolean?} [custom.extractData=true]
306+ * @param {boolean?} [custom.includePreset=true]
307+ * @param {boolean?} [custom.includeInstruct=true]
308+ * @param {Partial<InstructSettings>?} [custom.instructSettings] Override instruct settings
302309 * @returns {Promise<import('../custom-request.js').ExtractedData | (() => AsyncGenerator<import('../custom-request.js').StreamResponse>)>} If not streaming, returns extracted data; if streaming, returns a function that creates an AsyncGenerator
303310 */
304311 static async sendRequest(profileId, prompt, maxTokens, custom = this.defaultSendRequestParams) {
305312 const { stream, signal, extractData, includePreset, includeInstruct, instructSettings } = { ...this.defaultSendRequestParams, ...custom };
306313
307314 const context = SillyTavern.getContext();
308315 if (context.extensionSettings.disabledExtensions.includes('connection-manager')) {
@@ -346,6 +353,7 @@ export class ConnectionManagerRequestService {
346353 }, {
347354 instructName: includeInstruct ? profile.instruct : undefined,
348355 presetName: includePreset ? profile.preset : undefined,
356+ instructSettings: includeInstruct ? instructSettings : undefined,
349357 }, extractData, signal);
350358 }
351359 default: {
public/scripts/instruct-mode.js+18 -12
@@ -243,9 +243,14 @@ export function autoSelectInstructPreset(modelId) {
243243
244244/**
245245 * Converts instruct mode sequences to an array of stopping strings.
246+ * @param {Object} options
247+ * @param {InstructSettings?} [options.customInstruct=null] - Custom instruct settings.
248+ * @param {boolean?} [options.useStopStrings] - Decides whether to use "Chat Start" and "Example Separator"
246249 * @returns {string[]} Array of instruct mode stopping strings.
247250 */
248-export function getInstructStoppingSequences() {
251+export function getInstructStoppingSequences({ customInstruct = null, useStopStrings = null } = {}) {
252+ const instruct = structuredClone(customInstruct ?? power_user.instruct);
253+
249254 /**
250255 * Adds instruct mode sequence to the result array.
251256 * @param {string} sequence Sequence string.
@@ -254,7 +259,7 @@ export function getInstructStoppingSequences() {
254259 function addInstructSequence(sequence) {
255260 // Cohee: oobabooga's textgen always appends newline before the sequence as a stopping string
256261 // But it's a problem for Metharme which doesn't use newlines to separate them.
257262 const wrap = (s) => power_user.instruct.wrap ? '\n' + s : s;
258263 // Sequence must be a non-empty string
259264 if (typeof sequence === 'string' && sequence.length > 0) {
260265 // If sequence is just a whitespace or newline - we don't want to make it a stopping string
@@ -262,7 +267,7 @@ export function getInstructStoppingSequences() {
262267 if (sequence.trim().length > 0) {
263268 const wrappedSequence = wrap(sequence);
264269 // Need to respect "insert macro" setting
265270 const stopString = power_user.instruct.macro ? substituteParams(wrappedSequence) : wrappedSequence;
266271 result.push(stopString);
267272 }
268273 }
@@ -270,14 +275,15 @@ export function getInstructStoppingSequences() {
270275
271276 const result = [];
272277
273- if (power_user.instruct.enabled) {
278+ // Since preset's don't have "enabled", we assume it's always enabled
274- const stop_sequence = power_user.instruct.stop_sequence || '';
279+ if (customInstruct ?? instruct.enabled) {
275280 const input_sequencestop_sequence = power_user.instruct.input_sequence?.replace(/{{name}}/gi, name1)stop_sequence || '';
276281 const output_sequenceinput_sequence = power_user.instruct.output_sequenceinput_sequence?.replace(/{{name}}/gi, name2name1) || '';
277282 const first_output_sequenceoutput_sequence = power_user.instruct.first_output_sequenceoutput_sequence?.replace(/{{name}}/gi, name2) || '';
278283 const last_output_sequencefirst_output_sequence = power_user.instruct.last_output_sequencefirst_output_sequence?.replace(/{{name}}/gi, name2) || '';
279284 const system_sequencelast_output_sequence = power_user.instruct.system_sequencelast_output_sequence?.replace(/{{name}}/gi, 'System'name2) || '';
280285 const last_system_sequencesystem_sequence = power_user.instruct.last_system_sequencesystem_sequence?.replace(/{{name}}/gi, 'System') || '';
286+ const last_system_sequence = instruct.last_system_sequence?.replace(/{{name}}/gi, 'System') || '';
281287
282288 const combined_sequence = [
283289 stop_sequence,
@@ -292,7 +298,7 @@ export function getInstructStoppingSequences() {
292298 combined_sequence.split('\n').filter((line, index, self) => self.indexOf(line) === index).forEach(addInstructSequence);
293299 }
294300
295301 if (useStopStrings ?? power_user.context.use_stop_strings) {
296302 if (power_user.context.chat_start) {
297303 result.push(`\n${substituteParams(power_user.context.chat_start)}`);
298304 }
public/scripts/personas.js+8 -13
@@ -111,6 +111,7 @@ export function setUserAvatar(imgfile, { toastPersonaNameChange = true, navigate
111111 reloadUserAvatar();
112112 updatePersonaUIStates({ navigateToCurrent: navigateToCurrent });
113113 selectCurrentPersona({ toastPersonaNameChange: toastPersonaNameChange });
114+ retriggerFirstMessageOnEmptyChat();
114115 saveSettingsDebounced();
115116 $('.zoomed_avatar[forchar]').remove();
116117}
@@ -465,7 +466,7 @@ export function initPersona(avatarId, personaName, personaDescription) {
465466 * @returns {Promise<boolean>} A promise that resolves to true if the character was converted, false otherwise.
466467 */
467468export async function convertCharacterToPersona(characterId = null) {
468469 if (null === characterId) characterId = Number(this_chid);
469470
470471 const avatarUrl = characters[characterId]?.avatar;
471472 if (!avatarUrl) {
@@ -1243,7 +1244,7 @@ function getPersonaStates(avatarId) {
12431244 /** @type {PersonaConnection[]} */
12441245 const connections = power_user.persona_descriptions[avatarId]?.connections;
12451246 const hasCharLock = !!connections?.some(c =>
12461247 (!selected_group && c.type === 'character' && c.id === characters[Number(this_chid)]?.avatar)
12471248 || (selected_group && c.type === 'group' && c.id === selected_group));
12481249
12491250 return {
@@ -1481,7 +1482,7 @@ async function loadPersonaForCurrentChat({ doRender = false } = {}) {
14811482 * @returns {string[]} - An array of persona keys that are connected to the given character key
14821483 */
14831484export function getConnectedPersonas(characterKey = undefined) {
14841485 characterKey ??= selected_group || characters[Number(this_chid)]?.avatar;
14851486 const connectedPersonas = Object.entries(power_user.persona_descriptions)
14861487 .filter(([_, desc]) => desc.connections?.some(conn => conn.type === 'character' && conn.id === characterKey))
14871488 .map(([key, _]) => key);
@@ -1513,7 +1514,7 @@ export async function showCharConnections() {
15131514 console.log(`Unlocking persona ${personaId} from current character ${name2}`);
15141515 power_user.persona_descriptions[personaId].connections = connections.filter(c => {
15151516 if (menu_type == 'group_edit' && c.type == 'group' && c.id == selected_group) return false;
15161517 else if (c.type == 'character' && c.id == characters[Number(this_chid)]?.avatar) return false;
15171518 return true;
15181519 });
15191520 saveSettingsDebounced();
@@ -1545,8 +1546,8 @@ export async function showCharConnections() {
15451546export function getCurrentConnectionObj() {
15461547 if (selected_group)
15471548 return { type: 'group', id: selected_group };
15481549 if (characters[Number(this_chid)]?.avatar)
15491550 return { type: 'character', id: characters[Number(this_chid)]?.avatar };
15501551 return null;
15511552}
15521553
@@ -1664,7 +1665,7 @@ async function syncUserNameToPersona() {
16641665 * Only works if only the first message is present, and not in group mode.
16651666 */
16661667export function retriggerFirstMessageOnEmptyChat() {
16671668 if (Number(this_chid) >= 0 && !selected_group && chat.length === 1) {
16681669 $('#firstmessage_textarea').trigger('input');
16691670 }
16701671}
@@ -1782,7 +1783,6 @@ function setNameCallback({ mode = 'all' }, name) {
17821783 if (!persona) persona = Object.entries(power_user.personas).find(([_, personaName]) => personaName.toLowerCase() === name.toLowerCase())?.[1];
17831784 if (persona) {
17841785 autoSelectPersona(persona);
1785- retriggerFirstMessageOnEmptyChat();
17861786 return '';
17871787 } else if (mode === 'lookup') {
17881788 toastr.warning(`Persona ${name} not found`);
@@ -1793,7 +1793,6 @@ function setNameCallback({ mode = 'all' }, name) {
17931793 if (['temp', 'all'].includes(mode)) {
17941794 // Otherwise, set just the name
17951795 setUserName(name); //this prevented quickReply usage
1796- retriggerFirstMessageOnEmptyChat();
17971796 }
17981797
17991798 return '';
@@ -1944,9 +1943,6 @@ export async function initPersonas() {
19441943 $(document).on('click', '#user_avatar_block .avatar-container', function () {
19451944 const imgfile = $(this).attr('data-avatar-id');
19461945 setUserAvatar(imgfile);
1947-
1948- // force firstMes {{user}} update on persona switch
1949- retriggerFirstMessageOnEmptyChat();
19501946 });
19511947
19521948 $('#persona_rename_button').on('click', () => renamePersona(user_avatar));
@@ -1979,4 +1975,3 @@ export async function initPersonas() {
19791975 eventSource.on(event_types.CHAT_CHANGED, loadPersonaForCurrentChat);
19801976 switchPersonaGridView();
19811977}
1982-
public/scripts/templates/promptManagerExportForCharacter.html+0 -4
@@ -1,4 +0,0 @@
1-<div class="row">
2- <a class="export-promptmanager-prompts-character list-group-item" data-i18n="Export for character">Export for character</a>
3- <span class="tooltip fa-solid fa-info-circle" data-i18n="[title]Export prompts for this character, including their order." title="Export prompts for this character, including their order."></span>
4-</div>
public/scripts/templates/promptManagerExportPopup.html+0 -12
@@ -1,12 +0,0 @@
1-<div id="prompt-manager-export-format-popup" class="list-group">
2- <div class="prompt-manager-export-format-popup-flex">
3- <div class="row">
4- <a class="export-promptmanager-prompts-full list-group-item" data-i18n="Export all">Export all</a>
5- <span class="tooltip fa-solid fa-info-circle" data-i18n="[title]Export all your prompts to a file" title="Export all your prompts to a file"></span>
6- </div>
7- {{#if isGlobalStrategy}}
8- {{else}}
9- {{{exportForCharacter}}}
10- {{/if}}
11- </div>
12-</div>
public/scripts/templates/worldInfoKeywordHeaders.html+1 -1
@@ -1,4 +1,4 @@
11<div id="WIEntryHeaderTitlesPC" class="flex-container wide100p spaceBetween justifyCenter textAlignCenter" style="padding:0 47.5em0em;">
22 <small class="flex1" data-i18n="Title/Memo">Title/Memo</small>
33 <small style="width: calc(3.5em + 10px)" data-i18n="Strategy">Strategy</small>
44 <small style="width: calc(3.5em + 20px)" data-i18n="Position">Position</small>
public/scripts/world-info.js+178 -2
@@ -2208,7 +2208,7 @@ function verifyWorldInfoSearchSortRule() {
22082208 * Use `originalWIDataKeyMap` to find the correct value to be set.
22092209 *
22102210 * @param {object} data - The data object containing the original data entries.
22112211 * @param {stringnumber} uid - The unique identifier of the data entry.
22122212 * @param {string} key - The key of the value to be set.
22132213 * @param {any} value - The value to be set.
22142214 */
@@ -2232,7 +2232,9 @@ export function setWIOriginalDataValue(data, uid, key, value) {
22322232 */
22332233export function deleteWIOriginalDataValue(data, uid) {
22342234 if (data.originalData && Array.isArray(data.originalData.entries)) {
2235- const originalIndex = data.originalData.entries.findIndex(x => x.uid === uid);
2235+ // Non-strict equality is used here to allow for both string and number comparisons
2236+ // @eslint-disable-next-line eqeqeq
2237+ const originalIndex = data.originalData.entries.findIndex(x => x.uid == uid);
22362238
22372239 if (originalIndex >= 0) {
22382240 data.originalData.entries.splice(originalIndex, 1);
@@ -3143,6 +3145,84 @@ export async function getWorldEntry(name, data, entry) {
31433145 updateEditor(navigation_option.previous);
31443146 });
31453147
3148+ // move button
3149+ const moveButton = template.find('.move_entry_button');
3150+ moveButton.attr('data-uid', entry.uid);
3151+ moveButton.attr('data-current-world', name);
3152+ moveButton.on('click', async function (e) {
3153+ e.stopPropagation();
3154+ const sourceUid = $(this).attr('data-uid');
3155+ const sourceWorld = $(this).attr('data-current-world');
3156+ const sourceWorldInfo = await loadWorldInfo(sourceWorld);
3157+ if (!sourceWorldInfo) {
3158+ return;
3159+ }
3160+ const sourceName = sourceWorldInfo.entries[sourceUid]?.comment;
3161+ if (sourceName === undefined) {
3162+ return;
3163+ }
3164+
3165+ const select = document.createElement('select');
3166+ select.id = 'move_entry_target_select';
3167+ select.classList.add('text_pole', 'wide100p', 'marginTop10');
3168+
3169+ const defaultOption = document.createElement('option');
3170+ defaultOption.value = '';
3171+ defaultOption.textContent = `-- ${t`Select Target Lorebook`} --`;
3172+ select.appendChild(defaultOption);
3173+
3174+ let selectableWorldCount = 0;
3175+ world_names.forEach(worldName => {
3176+ if (worldName !== sourceWorld) { // Exclude current world
3177+ const option = document.createElement('option');
3178+ option.value = world_names.indexOf(worldName).toString();
3179+ option.textContent = worldName;
3180+ select.appendChild(option);
3181+ selectableWorldCount++;
3182+ }
3183+ });
3184+
3185+ if (selectableWorldCount === 0) {
3186+ toastr.warning(t`There are no other lorebooks to move to.`);
3187+ return;
3188+ }
3189+
3190+ // Create wrapper div
3191+ const wrapper = document.createElement('div');
3192+ wrapper.textContent = t`Move "${sourceName}" to:`;
3193+
3194+ // Create container and append elements
3195+ const container = document.createElement('div');
3196+ container.appendChild(wrapper);
3197+ container.appendChild(select);
3198+
3199+ let selectedWorldIndex = -1;
3200+ select.addEventListener('change', function() {
3201+ selectedWorldIndex = this.value === '' ? -1 : Number(this.value);
3202+ });
3203+
3204+ const popupConfirm = await callGenericPopup(container, POPUP_TYPE.CONFIRM, '', {
3205+ okButton: t`Move`,
3206+ cancelButton: t`Cancel`,
3207+ });
3208+ if (!popupConfirm) {
3209+ return;
3210+ }
3211+
3212+ if (selectedWorldIndex === -1) {
3213+ return;
3214+ }
3215+
3216+ const selectedValue = world_names[selectedWorldIndex];
3217+
3218+ if (!selectedValue) {
3219+ toastr.warning(t`Please select a target lorebook.`);
3220+ return;
3221+ }
3222+
3223+ await moveWorldInfoEntry(sourceWorld, selectedValue, sourceUid);
3224+ });
3225+
31463226 // scan depth
31473227 const scanDepthInput = template.find('input[name="scanDepth"]');
31483228 scanDepthInput.data('uid', entry.uid);
@@ -5271,3 +5351,99 @@ jQuery(() => {
52715351 });
52725352 });
52735353});
5354+
5355+/**
5356+ * Moves a World Info entry from a source lorebook to a target lorebook.
5357+ *
5358+ * @param {string} sourceName - The name of the source lorebook file.
5359+ * @param {string} targetName - The name of the target lorebook file.
5360+ * @param {string|number} uid - The UID of the entry to move from the source lorebook.
5361+ * @returns {Promise<boolean>} True if the move was successful, false otherwise.
5362+ */
5363+export async function moveWorldInfoEntry(sourceName, targetName, uid) {
5364+ if (sourceName === targetName) {
5365+ return false;
5366+ }
5367+
5368+ if (!world_names.includes(sourceName)) {
5369+ toastr.error(t`Source lorebook '${sourceName}' not found.`);
5370+ console.error(`[WI Move] Source lorebook '${sourceName}' does not exist.`);
5371+ return false;
5372+ }
5373+
5374+ if (!world_names.includes(targetName)) {
5375+ toastr.error(t`Target lorebook '${targetName}' not found.`);
5376+ console.error(`[WI Move] Target lorebook '${targetName}' does not exist.`);
5377+ return false;
5378+ }
5379+
5380+ const entryUidString = String(uid);
5381+
5382+ try {
5383+ const sourceData = await loadWorldInfo(sourceName);
5384+ const targetData = await loadWorldInfo(targetName);
5385+
5386+ if (!sourceData || !sourceData.entries) {
5387+ toastr.error(t`Failed to load data for source lorebook '${sourceName}'.`);
5388+ console.error(`[WI Move] Could not load source data for '${sourceName}'.`);
5389+ return false;
5390+ }
5391+ if (!targetData || !targetData.entries) {
5392+ toastr.error(t`Failed to load data for target lorebook '${targetName}'.`);
5393+ console.error(`[WI Move] Could not load target data for '${targetName}'.`);
5394+ return false;
5395+ }
5396+
5397+ if (!sourceData.entries[entryUidString]) {
5398+ toastr.error(t`Entry not found in source lorebook '${sourceName}'.`);
5399+ console.error(`[WI Move] Entry UID ${entryUidString} not found in '${sourceName}'.`);
5400+ return false;
5401+ }
5402+
5403+ const entryToMove = structuredClone(sourceData.entries[entryUidString]);
5404+
5405+
5406+ const newUid = getFreeWorldEntryUid(targetData);
5407+ if (newUid === null) {
5408+ console.error(`[WI Move] Failed to get a free UID in '${targetName}'.`);
5409+ return false;
5410+ }
5411+
5412+ entryToMove.uid = newUid;
5413+ // Place the entry at the end of the target lorebook
5414+ const maxDisplayIndex = Object.values(targetData.entries).reduce((max, entry) => Math.max(max, entry.displayIndex ?? -1), -1);
5415+ entryToMove.displayIndex = maxDisplayIndex + 1;
5416+
5417+ targetData.entries[newUid] = entryToMove;
5418+
5419+ delete sourceData.entries[entryUidString];
5420+ // Remove from originalData if it exists
5421+ deleteWIOriginalDataValue(sourceData, entryUidString);
5422+ // TODO: setWIOriginalDataValue
5423+ console.debug(`[WI Move] Removed entry UID ${entryUidString} from source '${sourceName}'.`);
5424+
5425+
5426+ await saveWorldInfo(targetName, targetData, true);
5427+ console.debug(`[WI Move] Saved target lorebook '${targetName}'.`);
5428+ await saveWorldInfo(sourceName, sourceData, true);
5429+ console.debug(`[WI Move] Saved source lorebook '${sourceName}'.`);
5430+
5431+
5432+ console.log(`[WI Move] ${entryToMove.comment} moved successfully to '${targetName}'.`);
5433+
5434+ // Check if the currently viewed book in the editor is the source or target and reload it
5435+ const currentEditorBookIndex = Number($('#world_editor_select').val());
5436+ if (!isNaN(currentEditorBookIndex)) {
5437+ const currentEditorBookName = world_names[currentEditorBookIndex];
5438+ if (currentEditorBookName === sourceName || currentEditorBookName === targetName) {
5439+ reloadEditor(currentEditorBookName);
5440+ }
5441+ }
5442+
5443+ return true;
5444+ } catch (error) {
5445+ toastr.error(t`An unexpected error occurred while moving the entry: ${error.message}`);
5446+ console.error('[WI Move] Unexpected error:', error);
5447+ return false;
5448+ }
5449+}