mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
World Info: Add insertion order at depth (#1174)
Depth previously injected entries randomly. This is not ideal for world info architectures that rely on insertion order to function properly. Redo depth injection to have its own parameter and redo how it's handled in generate. Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
@ -4004,6 +4004,10 @@
|
|||||||
</small>
|
</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="world_entry_form_control wi-enter-footer-text flex-container flexNoGap ">
|
||||||
|
<label for="depth" data-i18n="Depth:">Depth:</label>
|
||||||
|
<input class="text_pole wideMax100px margin0" type="number" name="depth" placeholder="" min="0" max="10000" />
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="world_entry_form_control wi-enter-footer-text flex-container flexNoGap ">
|
<div class="world_entry_form_control wi-enter-footer-text flex-container flexNoGap ">
|
||||||
<label for="order" data-i18n="Order:">Order:</label>
|
<label for="order" data-i18n="Order:">Order:</label>
|
||||||
@ -4017,11 +4021,6 @@
|
|||||||
<input class="text_pole wideMax100px margin0" type="number" name="probability" placeholder="" min="0" max="100" />
|
<input class="text_pole wideMax100px margin0" type="number" name="probability" placeholder="" min="0" max="100" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="flex-container flexFlowColumn flexNoGap wi-enter-footer-text ">
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<input class="menu_button delete_entry_button" type="submit" data-i18n="Delete Entry" value="Delete Entry" />
|
<input class="menu_button delete_entry_button" type="submit" data-i18n="Delete Entry" value="Delete Entry" />
|
||||||
</div>
|
</div>
|
||||||
<div class="wide100p">
|
<div class="wide100p">
|
||||||
|
@ -2002,6 +2002,7 @@ function addPersonaDescriptionExtensionPrompt() {
|
|||||||
const ANWithDesc = power_user.persona_description_position === persona_description_positions.TOP_AN
|
const ANWithDesc = power_user.persona_description_position === persona_description_positions.TOP_AN
|
||||||
? `${power_user.persona_description}\n${originalAN}`
|
? `${power_user.persona_description}\n${originalAN}`
|
||||||
: `${originalAN}\n${power_user.persona_description}`;
|
: `${originalAN}\n${power_user.persona_description}`;
|
||||||
|
|
||||||
setExtensionPrompt(NOTE_MODULE_NAME, ANWithDesc, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
|
setExtensionPrompt(NOTE_MODULE_NAME, ANWithDesc, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2564,7 +2565,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
|||||||
// Set non-WI AN
|
// Set non-WI AN
|
||||||
setFloatingPrompt();
|
setFloatingPrompt();
|
||||||
// Add WI to prompt (and also inject WI to AN value via hijack)
|
// Add WI to prompt (and also inject WI to AN value via hijack)
|
||||||
let { worldInfoString, worldInfoBefore, worldInfoAfter } = await getWorldInfoPrompt(chat2, this_max_context);
|
let { worldInfoString, worldInfoBefore, worldInfoAfter, worldInfoDepth } = await getWorldInfoPrompt(chat2, this_max_context);
|
||||||
// Add persona description to prompt
|
// Add persona description to prompt
|
||||||
addPersonaDescriptionExtensionPrompt();
|
addPersonaDescriptionExtensionPrompt();
|
||||||
// Call combined AN into Generate
|
// Call combined AN into Generate
|
||||||
@ -2589,6 +2590,12 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
|||||||
|
|
||||||
const storyString = renderStoryString(storyStringParams);
|
const storyString = renderStoryString(storyStringParams);
|
||||||
|
|
||||||
|
// Add all depth WI entries to prompt
|
||||||
|
worldInfoDepth.forEach((e) => {
|
||||||
|
const joinedEntries = e.entries.join("\n");
|
||||||
|
setExtensionPrompt(`customDepthWI-${e.depth}`, joinedEntries, 1, e.depth)
|
||||||
|
});
|
||||||
|
|
||||||
if (main_api === 'openai') {
|
if (main_api === 'openai') {
|
||||||
message_already_generated = '';
|
message_already_generated = '';
|
||||||
setOpenAIMessages(coreChat);
|
setOpenAIMessages(coreChat);
|
||||||
|
@ -85,7 +85,12 @@ async function getWorldInfoPrompt(chat2, maxContext) {
|
|||||||
worldInfoAfter = activatedWorldInfo.worldInfoAfter;
|
worldInfoAfter = activatedWorldInfo.worldInfoAfter;
|
||||||
worldInfoString = worldInfoBefore + worldInfoAfter;
|
worldInfoString = worldInfoBefore + worldInfoAfter;
|
||||||
|
|
||||||
return { worldInfoString, worldInfoBefore, worldInfoAfter };
|
return {
|
||||||
|
worldInfoString,
|
||||||
|
worldInfoBefore,
|
||||||
|
worldInfoAfter,
|
||||||
|
worldInfoDepth: activatedWorldInfo.WIDepthEntries
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function setWorldInfoSettings(settings, data) {
|
function setWorldInfoSettings(settings, data) {
|
||||||
@ -675,6 +680,24 @@ function getWorldEntry(name, data, entry) {
|
|||||||
entry.probability = null;
|
entry.probability = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// depth
|
||||||
|
const depthInput = template.find('input[name="depth"]');
|
||||||
|
depthInput.data("uid", entry.uid);
|
||||||
|
depthInput.on("input", function() {
|
||||||
|
const uid = $(this).data("uid");
|
||||||
|
const value = Number($(this).val());
|
||||||
|
|
||||||
|
data.entries[uid].depth = !isNaN(value) ? value : 0;
|
||||||
|
setOriginalDataValue(data, uid, "depth", data.entries[uid].depth);
|
||||||
|
saveWorldInfo(name, data);
|
||||||
|
});
|
||||||
|
depthInput.val(entry.depth ?? 4).trigger("input");
|
||||||
|
|
||||||
|
// Hide by default unless depth is specified
|
||||||
|
if (entry.position === 4) {
|
||||||
|
depthInput.parent().hide();
|
||||||
|
}
|
||||||
|
|
||||||
const probabilityInput = template.find('input[name="probability"]');
|
const probabilityInput = template.find('input[name="probability"]');
|
||||||
probabilityInput.data("uid", entry.uid);
|
probabilityInput.data("uid", entry.uid);
|
||||||
probabilityInput.on("input", function () {
|
probabilityInput.on("input", function () {
|
||||||
@ -740,8 +763,10 @@ function getWorldEntry(name, data, entry) {
|
|||||||
const value = Number($(this).val());
|
const value = Number($(this).val());
|
||||||
data.entries[uid].position = !isNaN(value) ? value : 0;
|
data.entries[uid].position = !isNaN(value) ? value : 0;
|
||||||
if (value === 4) {
|
if (value === 4) {
|
||||||
template.find('label[for="order"').text('Depth:')
|
depthInput.parent().show();
|
||||||
} else { template.find('label[for="order"').text('Order:') }
|
} else {
|
||||||
|
depthInput.parent().hide();
|
||||||
|
}
|
||||||
// Spec v2 only supports before_char and after_char
|
// Spec v2 only supports before_char and after_char
|
||||||
setOriginalDataValue(data, uid, "position", data.entries[uid].position == 0 ? 'before_char' : 'after_char');
|
setOriginalDataValue(data, uid, "position", data.entries[uid].position == 0 ? 'before_char' : 'after_char');
|
||||||
// Write the original value as extensions field
|
// Write the original value as extensions field
|
||||||
@ -1233,6 +1258,7 @@ async function checkWorldInfo(chat, maxContext) {
|
|||||||
const WIAfterEntries = [];
|
const WIAfterEntries = [];
|
||||||
const ANTopEntries = [];
|
const ANTopEntries = [];
|
||||||
const ANBottomEntries = [];
|
const ANBottomEntries = [];
|
||||||
|
const WIDepthEntries = [];
|
||||||
|
|
||||||
// Appends from insertion order 999 to 1. Use unshift for this purpose
|
// Appends from insertion order 999 to 1. Use unshift for this purpose
|
||||||
[...allActivatedEntries].sort(sortFn).forEach((entry) => {
|
[...allActivatedEntries].sort(sortFn).forEach((entry) => {
|
||||||
@ -1250,12 +1276,15 @@ async function checkWorldInfo(chat, maxContext) {
|
|||||||
ANBottomEntries.unshift(entry.content);
|
ANBottomEntries.unshift(entry.content);
|
||||||
break;
|
break;
|
||||||
case world_info_position.atDepth:
|
case world_info_position.atDepth:
|
||||||
//inserted one by one, unrelated to any array of items
|
const existingDepthIndex = WIDepthEntries.findIndex((e) => e.depth === entry.depth ?? 4);
|
||||||
//must have a unique value for 'key' argument
|
if (existingDepthIndex !== -1) {
|
||||||
//uses the order input to specify depth
|
WIDepthEntries[existingDepthIndex].entries.unshift(entry.content);
|
||||||
var randomNumber = Math.floor(Math.random() * 99999) + 1;
|
} else {
|
||||||
context.setExtensionPrompt(`customDepthWI-${entry.keywords}-${entry.uid}-${randomNumber}`, entry.content, 1, entry.order);
|
WIDepthEntries.push({
|
||||||
break;
|
depth: entry.depth,
|
||||||
|
entries: [entry.content]
|
||||||
|
});
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -1270,7 +1299,7 @@ async function checkWorldInfo(chat, maxContext) {
|
|||||||
context.setExtensionPrompt(NOTE_MODULE_NAME, ANWithWI, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
|
context.setExtensionPrompt(NOTE_MODULE_NAME, ANWithWI, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { worldInfoBefore, worldInfoAfter };
|
return { worldInfoBefore, worldInfoAfter, WIDepthEntries };
|
||||||
}
|
}
|
||||||
|
|
||||||
function matchKeys(haystack, needle) {
|
function matchKeys(haystack, needle) {
|
||||||
|
Reference in New Issue
Block a user