WI-to-AN for real this time.

This commit is contained in:
RossAscends
2023-06-18 18:17:43 +09:00
parent d59024b4a5
commit 58a6ccd4a5
5 changed files with 35 additions and 13 deletions

View File

@ -2915,7 +2915,12 @@
</div>
<div>
<label><input type="radio" name="position" value="2">
<span data-i18n="Author's Note">Author's Note</span>
<span data-i18n="Author's Note">Author's Note Top</span>
</label>
</div>
<div>
<label><input type="radio" name="position" value="3">
<span data-i18n="Author's Note">Author's Note Bottom</span>
</label>
</div>

View File

@ -2012,10 +2012,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
let { worldInfoString, worldInfoBefore, worldInfoAfter } = getWorldInfoPrompt(chat2);
let allAnchors = getAllExtensionPrompts();
const afterScenarioAnchor = getExtensionPrompt(extension_prompt_types.AFTER_SCENARIO);
let zeroDepthAnchor = getExtensionPrompt(extension_prompt_types.IN_CHAT, 0, ' ');
let { worldInfoString, worldInfoBefore, worldInfoAfter } = getWorldInfoPrompt(chat2);
// Moved here to not overflow the Poe context with added prompt bits
if (main_api == 'poe') {

View File

@ -51,6 +51,7 @@ const extension_settings = {
note: {
default: '',
chara: [],
wiAddition: [],
},
caption: {},
expressions: {},

View File

@ -19,7 +19,7 @@ const DEFAULT_DEPTH = 4;
const DEFAULT_POSITION = 1;
const DEFAULT_INTERVAL = 1;
const metadata_keys = {
export const metadata_keys = {
prompt: 'note_prompt',
interval: 'note_interval',
depth: 'note_depth',
@ -283,7 +283,10 @@ function onChatChanged() {
$('#extension_floating_default_token_counter').text(tokenCounter3);
}
(function () {
//for some reason exporting metadata_keys for WI usage caused this to throw errors
//"accessing eventSource before initialization"
//putting it on a 1ms Timeout solved this.
setTimeout(function () {
function addExtensionsSettings() {
const settingsHtml = `
<div id="floatingPrompt" class="drawer-content flexGap5">
@ -399,4 +402,4 @@ function onChatChanged() {
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);
})();
}, 1);

View File

@ -1,6 +1,7 @@
import { saveSettings, callPopup, substituteParams, getTokenCount, getRequestHeaders } from "../script.js";
import { saveSettings, callPopup, substituteParams, getTokenCount, getRequestHeaders, chat_metadata } 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";
export {
world_info,
@ -35,7 +36,9 @@ const saveSettingsDebounced = debounce(() => saveSettings(), 1000);
const world_info_position = {
before: 0,
after: 1,
authorsnote: 2,
ANTop: 2,
ANBottom: 3,
};
function getWorldInfoPrompt(chat2) {
@ -592,8 +595,11 @@ function checkWorldInfo(chat) {
const newEntries = [...activatedNow]
.map((x) => world_info_data.entries[x])
.sort((a, b) => sortedEntries.indexOf(a) - sortedEntries.indexOf(b));
let ANInjectionTokens = 0;
for (const entry of newEntries) {
let ANWithWI;
let originalAN = context.extensionPrompts['2_floating_prompt'].value;
if (entry.position === world_info_position.after) {
worldInfoAfter = `${substituteParams(
entry.content
@ -602,14 +608,20 @@ function checkWorldInfo(chat) {
worldInfoBefore = `${substituteParams(
entry.content
)}\n${worldInfoBefore}`;
} else {
let originalAN = context.extensionPrompts['2_floating_prompt'].value;
let ANWithWI = originalAN + "\n" + entry.content;
context.extensionPrompts['2_floating_prompt'].value = ANWithWI;
//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) >= world_info_budget
(getTokenCount(worldInfoBefore + worldInfoAfter) + ANInjectionTokens) >= world_info_budget
) {
needsToScan = false;
break;