mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
fix WI2AN insertions
This commit is contained in:
@ -153,6 +153,7 @@ import {
|
||||
import { EventEmitter } from './scripts/eventemitter.js';
|
||||
import { context_settings, loadContextTemplatesFromSettings } from "./scripts/context-template.js";
|
||||
import { dinkusExtension } from "./scripts/showdown-dinkus.js";
|
||||
import { setFloatingPrompt } from "./scripts/extensions/floating-prompt/index.js";
|
||||
|
||||
//exporting functions and vars for mods
|
||||
export {
|
||||
@ -2024,8 +2025,11 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
||||
}
|
||||
|
||||
// Extension added strings
|
||||
//WI moved to top in order to allow it to hijack AN if necessary
|
||||
//set non-WI AN
|
||||
setFloatingPrompt();
|
||||
//add WI to prompt (and also inject WI to AN value via hijack)
|
||||
let { worldInfoString, worldInfoBefore, worldInfoAfter } = await getWorldInfoPrompt(chat2);
|
||||
// call combined AN into Generate
|
||||
let allAnchors = getAllExtensionPrompts();
|
||||
const afterScenarioAnchor = getExtensionPrompt(extension_prompt_types.AFTER_SCENARIO);
|
||||
let zeroDepthAnchor = getExtensionPrompt(extension_prompt_types.IN_CHAT, 0, ' ');
|
||||
@ -3652,9 +3656,9 @@ async function read_avatar_load(input) {
|
||||
$('#dialogue_popup').addClass('large_dialogue_popup wide_dialogue_popup');
|
||||
|
||||
const croppedImage = await callPopup(getCropPopup(e.target.result), 'avatarToCrop');
|
||||
if (!croppedImage) {
|
||||
return;
|
||||
}
|
||||
if (!croppedImage) {
|
||||
return;
|
||||
}
|
||||
|
||||
$("#avatar_load_preview").attr("src", croppedImage || e.target.result);
|
||||
|
||||
@ -4994,7 +4998,7 @@ function callPopup(text, type, inputValue = '') {
|
||||
rotatable: false,
|
||||
crop: function (event) {
|
||||
crop_data = event.detail;
|
||||
crop_data.want_resize = !power_user.never_resize_avatars
|
||||
crop_data.want_resize = !power_user.never_resize_avatars
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -6154,9 +6158,9 @@ $(document).ready(function () {
|
||||
|
||||
$('#dialogue_popup').addClass('large_dialogue_popup wide_dialogue_popup');
|
||||
const confirmation = await callPopup(getCropPopup(dataUrl.target.result), 'avatarToCrop');
|
||||
if (!confirmation) {
|
||||
return;
|
||||
}
|
||||
if (!confirmation) {
|
||||
return;
|
||||
}
|
||||
|
||||
let url = "/uploaduseravatar";
|
||||
|
||||
|
@ -18,6 +18,7 @@ const UPDATE_INTERVAL = 1000;
|
||||
const DEFAULT_DEPTH = 4;
|
||||
const DEFAULT_POSITION = 1;
|
||||
const DEFAULT_INTERVAL = 1;
|
||||
export var shouldWIAddPrompt = false;
|
||||
|
||||
export const metadata_keys = {
|
||||
prompt: 'note_prompt',
|
||||
@ -72,6 +73,12 @@ function setNotePositionCommand(_, text) {
|
||||
toastr.info("Author's Note position updated");
|
||||
}
|
||||
|
||||
function updateSettings() {
|
||||
saveSettingsDebounced();
|
||||
loadSettings();
|
||||
setFloatingPrompt();
|
||||
}
|
||||
|
||||
const setMainPromptTokenCounterDebounced = debounce((value) => $('#extension_floating_prompt_token_counter').text(getTokenCount(value)), 1000);
|
||||
const setCharaPromptTokenCounterDebounced = debounce((value) => $('#extension_floating_chara_token_counter').text(getTokenCount(value)), 1000);
|
||||
const setDefaultPromptTokenCounterDebounced = debounce((value) => $('#extension_floating_default_token_counter').text(getTokenCount(value)), 1000);
|
||||
@ -79,12 +86,12 @@ const setDefaultPromptTokenCounterDebounced = debounce((value) => $('#extension_
|
||||
async function onExtensionFloatingPromptInput() {
|
||||
chat_metadata[metadata_keys.prompt] = $(this).val();
|
||||
setMainPromptTokenCounterDebounced(chat_metadata[metadata_keys.prompt]);
|
||||
saveMetadataDebounced();
|
||||
updateSettings();
|
||||
}
|
||||
|
||||
async function onExtensionFloatingIntervalInput() {
|
||||
chat_metadata[metadata_keys.interval] = Number($(this).val());
|
||||
saveMetadataDebounced();
|
||||
updateSettings();
|
||||
}
|
||||
|
||||
async function onExtensionFloatingDepthInput() {
|
||||
@ -96,12 +103,12 @@ async function onExtensionFloatingDepthInput() {
|
||||
}
|
||||
|
||||
chat_metadata[metadata_keys.depth] = value;
|
||||
saveMetadataDebounced();
|
||||
updateSettings();
|
||||
}
|
||||
|
||||
async function onExtensionFloatingPositionInput(e) {
|
||||
chat_metadata[metadata_keys.position] = e.target.value;
|
||||
saveMetadataDebounced();
|
||||
updateSettings();
|
||||
}
|
||||
|
||||
function onExtensionFloatingCharaPromptInput() {
|
||||
@ -147,7 +154,7 @@ function onExtensionFloatingCharaPromptInput() {
|
||||
return;
|
||||
}
|
||||
|
||||
saveSettingsDebounced();
|
||||
updateSettings();
|
||||
}
|
||||
|
||||
function onExtensionFloatingCharaCheckboxChanged() {
|
||||
@ -157,14 +164,14 @@ function onExtensionFloatingCharaCheckboxChanged() {
|
||||
if (charaNote) {
|
||||
charaNote.useChara = value;
|
||||
|
||||
saveSettingsDebounced();
|
||||
updateSettings();
|
||||
}
|
||||
}
|
||||
|
||||
function onExtensionFloatingDefaultInput() {
|
||||
extension_settings.note.default = $(this).val();
|
||||
setDefaultPromptTokenCounterDebounced(extension_settings.note.default);
|
||||
saveSettingsDebounced();
|
||||
updateSettings();
|
||||
}
|
||||
|
||||
function loadSettings() {
|
||||
@ -190,18 +197,23 @@ function loadSettings() {
|
||||
$('#extension_floating_default').val(extension_settings.note.default);
|
||||
}
|
||||
|
||||
async function moduleWorker() {
|
||||
export function setFloatingPrompt() {
|
||||
const context = getContext();
|
||||
|
||||
console.log(context.characterId, context.groupId)
|
||||
if (!context.groupId && context.characterId === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
loadSettings();
|
||||
|
||||
// take the count of messages
|
||||
let lastMessageNumber = Array.isArray(context.chat) && context.chat.length ? context.chat.filter(m => m.is_user).length : 0;
|
||||
|
||||
console.debug(`
|
||||
setFloatingPrompt entered
|
||||
------
|
||||
lastMessageNumber = ${lastMessageNumber}
|
||||
metadata_keys.interval = ${chat_metadata[metadata_keys.interval]}
|
||||
`)
|
||||
|
||||
// interval 1 should be inserted no matter what
|
||||
if (chat_metadata[metadata_keys.interval] === 1) {
|
||||
lastMessageNumber = 1;
|
||||
@ -217,6 +229,8 @@ async function moduleWorker() {
|
||||
? (lastMessageNumber % chat_metadata[metadata_keys.interval])
|
||||
: (chat_metadata[metadata_keys.interval] - lastMessageNumber);
|
||||
const shouldAddPrompt = messagesTillInsertion == 0;
|
||||
shouldWIAddPrompt = shouldAddPrompt;
|
||||
console.debug(shouldAddPrompt, messagesTillInsertion);
|
||||
|
||||
let prompt = shouldAddPrompt ? $('#extension_floating_prompt').val() : '';
|
||||
if (shouldAddPrompt && extension_settings.note.chara && getContext().characterId) {
|
||||
@ -227,7 +241,6 @@ async function moduleWorker() {
|
||||
prompt = charaNote.prompt;
|
||||
}
|
||||
}
|
||||
|
||||
context.setExtensionPrompt(MODULE_NAME, prompt, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
|
||||
$('#extension_floating_counter').text(shouldAddPrompt ? '0' : messagesTillInsertion);
|
||||
}
|
||||
@ -266,6 +279,8 @@ function onANMenuItemClick() {
|
||||
}
|
||||
|
||||
function onChatChanged() {
|
||||
loadSettings();
|
||||
setFloatingPrompt();
|
||||
const context = getContext();
|
||||
|
||||
// Disable the chara note if in a group
|
||||
@ -403,12 +418,9 @@ setTimeout(function () {
|
||||
}
|
||||
|
||||
addExtensionsSettings();
|
||||
const wrapper = new ModuleWorkerWrapper(moduleWorker);
|
||||
setInterval(wrapper.update.bind(wrapper), UPDATE_INTERVAL);
|
||||
registerSlashCommand('note', setNoteTextCommand, [], "<span class='monospace'>(text)</span> – sets an author's note for the currently selected chat", true, true);
|
||||
registerSlashCommand('depth', setNoteDepthCommand, [], "<span class='monospace'>(number)</span> – sets an author's note depth for in-chat positioning", true, true);
|
||||
registerSlashCommand('freq', setNoteIntervalCommand, ['interval'], "<span class='monospace'>(number)</span> – sets an author's note insertion frequency", true, true);
|
||||
registerSlashCommand('pos', setNotePositionCommand, ['position'], "(<span class='monospace'>chat</span> or <span class='monospace'>scenario</span>) – sets an author's note position", true, true);
|
||||
eventSource.on(event_types.CHAT_CHANGED, onChatChanged);
|
||||
window['AuthorNote_generateInterceptor'] = moduleWorker;
|
||||
}, 1);
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { saveSettings, callPopup, substituteParams, getTokenCount, getRequestHeaders, chat_metadata, this_chid, characters } from "../script.js";
|
||||
import { download, debounce, delay, initScrollHeight, resetScrollHeight } from "./utils.js";
|
||||
import { getContext } from "./extensions.js";
|
||||
import { metadata_keys } from "./extensions/floating-prompt/index.js";
|
||||
import { metadata_keys, shouldWIAddPrompt } from "./extensions/floating-prompt/index.js";
|
||||
|
||||
export {
|
||||
world_info,
|
||||
@ -25,7 +25,7 @@ const world_info_insertion_strategy = {
|
||||
evenly: 0,
|
||||
character_first: 1,
|
||||
global_first: 2,
|
||||
}
|
||||
};
|
||||
|
||||
let world_info = null;
|
||||
let world_names;
|
||||
@ -590,18 +590,21 @@ async function getSortedEntries() {
|
||||
let entries;
|
||||
const sortFn = (a, b) => b.order - a.order;
|
||||
|
||||
switch (world_info_character_strategy) {
|
||||
switch (Number(world_info_character_strategy)) {
|
||||
case world_info_insertion_strategy.evenly:
|
||||
console.debug('WI using evenly')
|
||||
entries = [...globalLore, ...characterLore].sort(sortFn);
|
||||
break;
|
||||
case world_info_insertion_strategy.character_first:
|
||||
console.debug('WI using char first')
|
||||
entries = [...characterLore.sort(sortFn), ...globalLore.sort(sortFn)];
|
||||
break;
|
||||
case world_info_insertion_strategy.global_first:
|
||||
console.debug('WI using global first')
|
||||
entries = [...globalLore.sort(sortFn), ...characterLore.sort(sortFn)];
|
||||
break;
|
||||
default:
|
||||
console.error(`Unknown insertion strategy: ${world_info_character_strategy}`);
|
||||
console.error("Unknown WI insertion strategy: ", world_info_character_strategy, "defaulting to evenly");
|
||||
entries = [...globalLore, ...characterLore].sort(sortFn);
|
||||
break;
|
||||
}
|
||||
@ -629,7 +632,7 @@ async function checkWorldInfo(chat) {
|
||||
const sortedEntries = await getSortedEntries();
|
||||
|
||||
if (sortedEntries.length === 0) {
|
||||
return { worldInfoBefore, worldInfoAfter };
|
||||
return { worldInfoBefore, worldInfoAfter };
|
||||
}
|
||||
|
||||
while (needsToScan) {
|
||||
@ -676,43 +679,43 @@ async function checkWorldInfo(chat) {
|
||||
needsToScan = world_info_recursive && activatedNow.size > 0;
|
||||
const newEntries = [...activatedNow]
|
||||
.sort((a, b) => sortedEntries.indexOf(a) - sortedEntries.indexOf(b));
|
||||
let ANInjectionTokens = 0;
|
||||
for (const entry of newEntries) {
|
||||
let ANWithWI;
|
||||
if (shouldWIAddPrompt) {
|
||||
let ANInjectionTokens = 0;
|
||||
let ANTopInjection = [];
|
||||
let ANBottomInjection = [];
|
||||
for (const entry of newEntries) {
|
||||
if (entry.position === world_info_position.after) {
|
||||
worldInfoAfter = `${substituteParams(
|
||||
entry.content
|
||||
)}\n${worldInfoAfter}`;
|
||||
} else if (entry.position === world_info_position.before) {
|
||||
worldInfoBefore = `${substituteParams(
|
||||
entry.content
|
||||
)}\n${worldInfoBefore}`;
|
||||
|
||||
let originalAN = context.extensionPrompts['2_floating_prompt'].value;
|
||||
if (entry.position === world_info_position.after) {
|
||||
worldInfoAfter = `${substituteParams(
|
||||
entry.content
|
||||
)}\n${worldInfoAfter}`;
|
||||
} else if (entry.position === world_info_position.before) {
|
||||
worldInfoBefore = `${substituteParams(
|
||||
entry.content
|
||||
)}\n${worldInfoBefore}`;
|
||||
} else if (entry.position === world_info_position.ANBottom) {
|
||||
ANBottomInjection.push(entry.content);
|
||||
ANInjectionTokens += getTokenCount(entry.content);
|
||||
} else if (entry.position === world_info_position.ANTop) {
|
||||
ANTopInjection.push(entry.content);
|
||||
ANInjectionTokens = getTokenCount(entry.content);
|
||||
}
|
||||
|
||||
//WI must hijack AN to inject before the prompt is set.
|
||||
} else if (entry.position === world_info_position.ANBottom) {
|
||||
ANWithWI = originalAN + "\n" + entry.content;
|
||||
ANInjectionTokens = ANInjectionTokens + getTokenCount(ANWithWI) - getTokenCount(originalAN);
|
||||
context.setExtensionPrompt('2_floating_prompt', ANWithWI, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
|
||||
} else if (entry.position === world_info_position.ANTop) {
|
||||
ANWithWI = entry.content + "\n" + originalAN;
|
||||
ANInjectionTokens = ANInjectionTokens + getTokenCount(ANWithWI) - getTokenCount(originalAN);
|
||||
context.setExtensionPrompt('2_floating_prompt', ANWithWI, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
|
||||
if (
|
||||
(getTokenCount(worldInfoBefore + worldInfoAfter) + ANInjectionTokens) >= world_info_budget
|
||||
) {
|
||||
needsToScan = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (needsToScan) {
|
||||
textToScan = (transformString(newEntries.map(x => x.content).join('\n')) + textToScan);
|
||||
}
|
||||
|
||||
if (
|
||||
(getTokenCount(worldInfoBefore + worldInfoAfter) + ANInjectionTokens) >= world_info_budget
|
||||
) {
|
||||
needsToScan = false;
|
||||
break;
|
||||
}
|
||||
const originalAN = context.extensionPrompts['2_floating_prompt'].value;
|
||||
const ANWithWI = `\n${ANTopInjection.join("\n")} \n${originalAN} \n${ANBottomInjection.reverse().join("\n")}`
|
||||
context.setExtensionPrompt('2_floating_prompt', ANWithWI, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
|
||||
}
|
||||
|
||||
if (needsToScan) {
|
||||
textToScan = (transformString(newEntries.map(x => x.content).join('\n')) + textToScan);
|
||||
}
|
||||
|
||||
allActivatedEntries = new Set([...allActivatedEntries, ...activatedNow]);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user