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:
@ -3339,4 +3339,4 @@
|
|||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
@ -153,6 +153,7 @@ import {
|
|||||||
import { EventEmitter } from './scripts/eventemitter.js';
|
import { EventEmitter } from './scripts/eventemitter.js';
|
||||||
import { context_settings, loadContextTemplatesFromSettings } from "./scripts/context-template.js";
|
import { context_settings, loadContextTemplatesFromSettings } from "./scripts/context-template.js";
|
||||||
import { dinkusExtension } from "./scripts/showdown-dinkus.js";
|
import { dinkusExtension } from "./scripts/showdown-dinkus.js";
|
||||||
|
import { setFloatingPrompt } from "./scripts/extensions/floating-prompt/index.js";
|
||||||
|
|
||||||
//exporting functions and vars for mods
|
//exporting functions and vars for mods
|
||||||
export {
|
export {
|
||||||
@ -2024,8 +2025,11 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Extension added strings
|
// 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);
|
let { worldInfoString, worldInfoBefore, worldInfoAfter } = await getWorldInfoPrompt(chat2);
|
||||||
|
// call combined AN into Generate
|
||||||
let allAnchors = getAllExtensionPrompts();
|
let allAnchors = getAllExtensionPrompts();
|
||||||
const afterScenarioAnchor = getExtensionPrompt(extension_prompt_types.AFTER_SCENARIO);
|
const afterScenarioAnchor = getExtensionPrompt(extension_prompt_types.AFTER_SCENARIO);
|
||||||
let zeroDepthAnchor = getExtensionPrompt(extension_prompt_types.IN_CHAT, 0, ' ');
|
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');
|
$('#dialogue_popup').addClass('large_dialogue_popup wide_dialogue_popup');
|
||||||
|
|
||||||
const croppedImage = await callPopup(getCropPopup(e.target.result), 'avatarToCrop');
|
const croppedImage = await callPopup(getCropPopup(e.target.result), 'avatarToCrop');
|
||||||
if (!croppedImage) {
|
if (!croppedImage) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$("#avatar_load_preview").attr("src", croppedImage || e.target.result);
|
$("#avatar_load_preview").attr("src", croppedImage || e.target.result);
|
||||||
|
|
||||||
@ -4994,7 +4998,7 @@ function callPopup(text, type, inputValue = '') {
|
|||||||
rotatable: false,
|
rotatable: false,
|
||||||
crop: function (event) {
|
crop: function (event) {
|
||||||
crop_data = event.detail;
|
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');
|
$('#dialogue_popup').addClass('large_dialogue_popup wide_dialogue_popup');
|
||||||
const confirmation = await callPopup(getCropPopup(dataUrl.target.result), 'avatarToCrop');
|
const confirmation = await callPopup(getCropPopup(dataUrl.target.result), 'avatarToCrop');
|
||||||
if (!confirmation) {
|
if (!confirmation) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let url = "/uploaduseravatar";
|
let url = "/uploaduseravatar";
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ const UPDATE_INTERVAL = 1000;
|
|||||||
const DEFAULT_DEPTH = 4;
|
const DEFAULT_DEPTH = 4;
|
||||||
const DEFAULT_POSITION = 1;
|
const DEFAULT_POSITION = 1;
|
||||||
const DEFAULT_INTERVAL = 1;
|
const DEFAULT_INTERVAL = 1;
|
||||||
|
export var shouldWIAddPrompt = false;
|
||||||
|
|
||||||
export const metadata_keys = {
|
export const metadata_keys = {
|
||||||
prompt: 'note_prompt',
|
prompt: 'note_prompt',
|
||||||
@ -72,6 +73,12 @@ function setNotePositionCommand(_, text) {
|
|||||||
toastr.info("Author's Note position updated");
|
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 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 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);
|
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() {
|
async function onExtensionFloatingPromptInput() {
|
||||||
chat_metadata[metadata_keys.prompt] = $(this).val();
|
chat_metadata[metadata_keys.prompt] = $(this).val();
|
||||||
setMainPromptTokenCounterDebounced(chat_metadata[metadata_keys.prompt]);
|
setMainPromptTokenCounterDebounced(chat_metadata[metadata_keys.prompt]);
|
||||||
saveMetadataDebounced();
|
updateSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onExtensionFloatingIntervalInput() {
|
async function onExtensionFloatingIntervalInput() {
|
||||||
chat_metadata[metadata_keys.interval] = Number($(this).val());
|
chat_metadata[metadata_keys.interval] = Number($(this).val());
|
||||||
saveMetadataDebounced();
|
updateSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onExtensionFloatingDepthInput() {
|
async function onExtensionFloatingDepthInput() {
|
||||||
@ -96,12 +103,12 @@ async function onExtensionFloatingDepthInput() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
chat_metadata[metadata_keys.depth] = value;
|
chat_metadata[metadata_keys.depth] = value;
|
||||||
saveMetadataDebounced();
|
updateSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onExtensionFloatingPositionInput(e) {
|
async function onExtensionFloatingPositionInput(e) {
|
||||||
chat_metadata[metadata_keys.position] = e.target.value;
|
chat_metadata[metadata_keys.position] = e.target.value;
|
||||||
saveMetadataDebounced();
|
updateSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
function onExtensionFloatingCharaPromptInput() {
|
function onExtensionFloatingCharaPromptInput() {
|
||||||
@ -147,7 +154,7 @@ function onExtensionFloatingCharaPromptInput() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
saveSettingsDebounced();
|
updateSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
function onExtensionFloatingCharaCheckboxChanged() {
|
function onExtensionFloatingCharaCheckboxChanged() {
|
||||||
@ -157,14 +164,14 @@ function onExtensionFloatingCharaCheckboxChanged() {
|
|||||||
if (charaNote) {
|
if (charaNote) {
|
||||||
charaNote.useChara = value;
|
charaNote.useChara = value;
|
||||||
|
|
||||||
saveSettingsDebounced();
|
updateSettings();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function onExtensionFloatingDefaultInput() {
|
function onExtensionFloatingDefaultInput() {
|
||||||
extension_settings.note.default = $(this).val();
|
extension_settings.note.default = $(this).val();
|
||||||
setDefaultPromptTokenCounterDebounced(extension_settings.note.default);
|
setDefaultPromptTokenCounterDebounced(extension_settings.note.default);
|
||||||
saveSettingsDebounced();
|
updateSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadSettings() {
|
function loadSettings() {
|
||||||
@ -190,18 +197,23 @@ function loadSettings() {
|
|||||||
$('#extension_floating_default').val(extension_settings.note.default);
|
$('#extension_floating_default').val(extension_settings.note.default);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function moduleWorker() {
|
export function setFloatingPrompt() {
|
||||||
const context = getContext();
|
const context = getContext();
|
||||||
|
console.log(context.characterId, context.groupId)
|
||||||
if (!context.groupId && context.characterId === undefined) {
|
if (!context.groupId && context.characterId === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
loadSettings();
|
|
||||||
|
|
||||||
// take the count of messages
|
// take the count of messages
|
||||||
let lastMessageNumber = Array.isArray(context.chat) && context.chat.length ? context.chat.filter(m => m.is_user).length : 0;
|
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
|
// interval 1 should be inserted no matter what
|
||||||
if (chat_metadata[metadata_keys.interval] === 1) {
|
if (chat_metadata[metadata_keys.interval] === 1) {
|
||||||
lastMessageNumber = 1;
|
lastMessageNumber = 1;
|
||||||
@ -217,6 +229,8 @@ async function moduleWorker() {
|
|||||||
? (lastMessageNumber % chat_metadata[metadata_keys.interval])
|
? (lastMessageNumber % chat_metadata[metadata_keys.interval])
|
||||||
: (chat_metadata[metadata_keys.interval] - lastMessageNumber);
|
: (chat_metadata[metadata_keys.interval] - lastMessageNumber);
|
||||||
const shouldAddPrompt = messagesTillInsertion == 0;
|
const shouldAddPrompt = messagesTillInsertion == 0;
|
||||||
|
shouldWIAddPrompt = shouldAddPrompt;
|
||||||
|
console.debug(shouldAddPrompt, messagesTillInsertion);
|
||||||
|
|
||||||
let prompt = shouldAddPrompt ? $('#extension_floating_prompt').val() : '';
|
let prompt = shouldAddPrompt ? $('#extension_floating_prompt').val() : '';
|
||||||
if (shouldAddPrompt && extension_settings.note.chara && getContext().characterId) {
|
if (shouldAddPrompt && extension_settings.note.chara && getContext().characterId) {
|
||||||
@ -227,7 +241,6 @@ async function moduleWorker() {
|
|||||||
prompt = charaNote.prompt;
|
prompt = charaNote.prompt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
context.setExtensionPrompt(MODULE_NAME, prompt, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
|
context.setExtensionPrompt(MODULE_NAME, prompt, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
|
||||||
$('#extension_floating_counter').text(shouldAddPrompt ? '0' : messagesTillInsertion);
|
$('#extension_floating_counter').text(shouldAddPrompt ? '0' : messagesTillInsertion);
|
||||||
}
|
}
|
||||||
@ -266,6 +279,8 @@ function onANMenuItemClick() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onChatChanged() {
|
function onChatChanged() {
|
||||||
|
loadSettings();
|
||||||
|
setFloatingPrompt();
|
||||||
const context = getContext();
|
const context = getContext();
|
||||||
|
|
||||||
// Disable the chara note if in a group
|
// Disable the chara note if in a group
|
||||||
@ -403,12 +418,9 @@ setTimeout(function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addExtensionsSettings();
|
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('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('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('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);
|
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);
|
eventSource.on(event_types.CHAT_CHANGED, onChatChanged);
|
||||||
window['AuthorNote_generateInterceptor'] = moduleWorker;
|
|
||||||
}, 1);
|
}, 1);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { saveSettings, callPopup, substituteParams, getTokenCount, getRequestHeaders, chat_metadata, this_chid, characters } from "../script.js";
|
import { saveSettings, callPopup, substituteParams, getTokenCount, getRequestHeaders, chat_metadata, this_chid, characters } from "../script.js";
|
||||||
import { download, debounce, delay, initScrollHeight, resetScrollHeight } from "./utils.js";
|
import { download, debounce, delay, initScrollHeight, resetScrollHeight } from "./utils.js";
|
||||||
import { getContext } from "./extensions.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 {
|
export {
|
||||||
world_info,
|
world_info,
|
||||||
@ -25,7 +25,7 @@ const world_info_insertion_strategy = {
|
|||||||
evenly: 0,
|
evenly: 0,
|
||||||
character_first: 1,
|
character_first: 1,
|
||||||
global_first: 2,
|
global_first: 2,
|
||||||
}
|
};
|
||||||
|
|
||||||
let world_info = null;
|
let world_info = null;
|
||||||
let world_names;
|
let world_names;
|
||||||
@ -590,18 +590,21 @@ async function getSortedEntries() {
|
|||||||
let entries;
|
let entries;
|
||||||
const sortFn = (a, b) => b.order - a.order;
|
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:
|
case world_info_insertion_strategy.evenly:
|
||||||
|
console.debug('WI using evenly')
|
||||||
entries = [...globalLore, ...characterLore].sort(sortFn);
|
entries = [...globalLore, ...characterLore].sort(sortFn);
|
||||||
break;
|
break;
|
||||||
case world_info_insertion_strategy.character_first:
|
case world_info_insertion_strategy.character_first:
|
||||||
|
console.debug('WI using char first')
|
||||||
entries = [...characterLore.sort(sortFn), ...globalLore.sort(sortFn)];
|
entries = [...characterLore.sort(sortFn), ...globalLore.sort(sortFn)];
|
||||||
break;
|
break;
|
||||||
case world_info_insertion_strategy.global_first:
|
case world_info_insertion_strategy.global_first:
|
||||||
|
console.debug('WI using global first')
|
||||||
entries = [...globalLore.sort(sortFn), ...characterLore.sort(sortFn)];
|
entries = [...globalLore.sort(sortFn), ...characterLore.sort(sortFn)];
|
||||||
break;
|
break;
|
||||||
default:
|
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);
|
entries = [...globalLore, ...characterLore].sort(sortFn);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -629,7 +632,7 @@ async function checkWorldInfo(chat) {
|
|||||||
const sortedEntries = await getSortedEntries();
|
const sortedEntries = await getSortedEntries();
|
||||||
|
|
||||||
if (sortedEntries.length === 0) {
|
if (sortedEntries.length === 0) {
|
||||||
return { worldInfoBefore, worldInfoAfter };
|
return { worldInfoBefore, worldInfoAfter };
|
||||||
}
|
}
|
||||||
|
|
||||||
while (needsToScan) {
|
while (needsToScan) {
|
||||||
@ -676,43 +679,43 @@ async function checkWorldInfo(chat) {
|
|||||||
needsToScan = world_info_recursive && activatedNow.size > 0;
|
needsToScan = world_info_recursive && activatedNow.size > 0;
|
||||||
const newEntries = [...activatedNow]
|
const newEntries = [...activatedNow]
|
||||||
.sort((a, b) => sortedEntries.indexOf(a) - sortedEntries.indexOf(b));
|
.sort((a, b) => sortedEntries.indexOf(a) - sortedEntries.indexOf(b));
|
||||||
let ANInjectionTokens = 0;
|
if (shouldWIAddPrompt) {
|
||||||
for (const entry of newEntries) {
|
let ANInjectionTokens = 0;
|
||||||
let ANWithWI;
|
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;
|
} else if (entry.position === world_info_position.ANBottom) {
|
||||||
if (entry.position === world_info_position.after) {
|
ANBottomInjection.push(entry.content);
|
||||||
worldInfoAfter = `${substituteParams(
|
ANInjectionTokens += getTokenCount(entry.content);
|
||||||
entry.content
|
} else if (entry.position === world_info_position.ANTop) {
|
||||||
)}\n${worldInfoAfter}`;
|
ANTopInjection.push(entry.content);
|
||||||
} else if (entry.position === world_info_position.before) {
|
ANInjectionTokens = getTokenCount(entry.content);
|
||||||
worldInfoBefore = `${substituteParams(
|
}
|
||||||
entry.content
|
|
||||||
)}\n${worldInfoBefore}`;
|
|
||||||
|
|
||||||
//WI must hijack AN to inject before the prompt is set.
|
if (
|
||||||
} else if (entry.position === world_info_position.ANBottom) {
|
(getTokenCount(worldInfoBefore + worldInfoAfter) + ANInjectionTokens) >= world_info_budget
|
||||||
ANWithWI = originalAN + "\n" + entry.content;
|
) {
|
||||||
ANInjectionTokens = ANInjectionTokens + getTokenCount(ANWithWI) - getTokenCount(originalAN);
|
needsToScan = false;
|
||||||
context.setExtensionPrompt('2_floating_prompt', ANWithWI, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
|
break;
|
||||||
} else if (entry.position === world_info_position.ANTop) {
|
}
|
||||||
ANWithWI = entry.content + "\n" + originalAN;
|
}
|
||||||
ANInjectionTokens = ANInjectionTokens + getTokenCount(ANWithWI) - getTokenCount(originalAN);
|
if (needsToScan) {
|
||||||
context.setExtensionPrompt('2_floating_prompt', ANWithWI, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
|
textToScan = (transformString(newEntries.map(x => x.content).join('\n')) + textToScan);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
const originalAN = context.extensionPrompts['2_floating_prompt'].value;
|
||||||
(getTokenCount(worldInfoBefore + worldInfoAfter) + ANInjectionTokens) >= world_info_budget
|
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]);
|
||||||
needsToScan = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (needsToScan) {
|
|
||||||
textToScan = (transformString(newEntries.map(x => x.content).join('\n')) + textToScan);
|
|
||||||
}
|
|
||||||
|
|
||||||
allActivatedEntries = new Set([...allActivatedEntries, ...activatedNow]);
|
allActivatedEntries = new Set([...allActivatedEntries, ...activatedNow]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user