Prompt Manager: Make main/PHI/aux injectable (#4829)

0b5b3f089ebdf5965c4295bc02b2b9065fca7a03

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

Signed
2 files changed, +35 -55Ignore whitespace
public/scripts/PromptManager.js+1 -9
@@ -579,10 +579,6 @@ class PromptManager {
579579 const sourceName = this.promptSources[promptId];
580580 entrySource.textContent = sourceName;
581581 }
582-
583- if (!this.systemPrompts.includes(promptId)) {
584- injectionPositionField.removeAttribute('disabled');
585- }
586582 };
587583
588584 // Append prompt to selected character
@@ -1399,10 +1395,6 @@ class PromptManager {
13991395 entrySource.textContent = sourceName;
14001396 }
14011397
1402- if (this.systemPrompts.includes(prompt.identifier)) {
1403- injectionPositionField.setAttribute('disabled', 'disabled');
1404- }
1405-
14061398 const resetPromptButton = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_reset');
14071399 if (true === prompt.system_prompt) {
14081400 resetPromptButton.style.display = 'block';
@@ -1757,7 +1749,7 @@ class PromptManager {
17571749 ${isInjectionPrompt ? '<span class="fa-fw fa-solid fa-syringe" title="In-Chat Injection"></span>' : ''}
17581750 ${this.isPromptInspectionAllowed(prompt) ? `<a title="${encodedName}" class="prompt-manager-inspect-action">${encodedName}</a>` : `<span title="${encodedName}">${encodedName}</span>`}
17591751 ${roleIcon ? `<span data-role="${escapeHtml(prompt.role)}" class="fa-xs fa-solid ${roleIcon}" title="${roleTitle}"></span>` : ''}
17601752 ${isInjectionPrompt ? `<small class="prompt-manager-injection-depth">@ ${escapeHtml(prompt.injection_depth.toString())}</small>` : ''}
17611753 ${isOverriddenPrompt ? '<small class="fa-solid fa-address-card prompt-manager-overridden" title="Pulled from a character card"></small>' : ''}
17621754 </span>
17631755 <span>
public/scripts/openai.js+34 -46
@@ -1117,60 +1117,48 @@ async function populateChatCompletion(prompts, chatCompletion, { bias, quietProm
11171117 // Bias
11181118 if (bias && bias.trim().length) await addToChatCompletion('bias');
11191119
1120- // Tavern Extras - Summary
1120+ const injectToMain = async (/** @type {Prompt} */ prompt, /** @type {string|number} */ position) => {
11211121 if (promptschatCompletion.has('summarymain')) {
11221122 const summarymessage = promptsawait Message.getfromPromptAsync('summary'prompt);
1123-
1123+ chatCompletion.insert(message, 'main', position);
11241124 if} (summary.position)else {
1125- const message = await Message.fromPromptAsync(summary);
1125+ // Convert the relative prompt to an injection and place it relative to main prompt
1126- chatCompletion.insert(message, 'main', summary.position);
1126+ // Keeping prompts in the same order bucket will squash them together during in-chat injection
1127- }
1127+ const indexOfMain = absolutePrompts.findIndex(p => p.identifier === 'main');
1128- }
1128+ if (indexOfMain >= 0) {
1129-
1129+ const main = absolutePrompts[indexOfMain];
1130- // Authors Note
1130+ const promptCopy = new Prompt(prompt);
1131- if (prompts.has('authorsNote')) {
1131+ promptCopy.role = main.role;
1132- const authorsNote = prompts.get('authorsNote');
1132+ promptCopy.injection_position = main.injection_position;
1133-
1133+ promptCopy.injection_depth = main.injection_depth;
1134- if (authorsNote.position) {
1134+ promptCopy.injection_order = main.injection_order;
1135- const message = await Message.fromPromptAsync(authorsNote);
1135+ const newIndex = position === 'end' ? indexOfMain + 1 : indexOfMain;
11361136 chatCompletion absolutePrompts.insertsplice(messagenewIndex, 'main'0, authorsNote.positionpromptCopy);
11371137 }
1138- }
1139-
1140- // Vectors Memory
1141- if (prompts.has('vectorsMemory')) {
1142- const vectorsMemory = prompts.get('vectorsMemory');
1143-
1144- if (vectorsMemory.position) {
1145- const message = await Message.fromPromptAsync(vectorsMemory);
1146- chatCompletion.insert(message, 'main', vectorsMemory.position);
1147- }
1148- }
1149-
1150- // Vectors Data Bank
1151- if (prompts.has('vectorsDataBank')) {
1152- const vectorsDataBank = prompts.get('vectorsDataBank');
1153-
1154- if (vectorsDataBank.position) {
1155- const message = await Message.fromPromptAsync(vectorsDataBank);
1156- chatCompletion.insert(message, 'main', vectorsDataBank.position);
11571138 }
11581139 };
11591140
1160- // Smart Context (ChromaDB)
1141+ const knownPrompts = [
1161- if (prompts.has('smartContext')) {
1142+ 'summary',
1162- const smartContext = prompts.get('smartContext');
1143+ 'authorsNote',
1144+ 'vectorsMemory',
1145+ 'vectorsDataBank',
1146+ 'smartContext',
1147+ ];
11631148
1164- if (smartContext.position) {
1149+ // Known relative extension prompts
1165- const message = await Message.fromPromptAsync(smartContext);
1150+ for (const key of knownPrompts) {
1166- chatCompletion.insert(message, 'main', smartContext.position);
1151+ if (prompts.has(key)) {
1152+ const prompt = prompts.get(key);
1153+ if (prompt.position) {
1154+ await injectToMain(prompt, prompt.position);
1155+ }
11671156 }
11681157 }
11691158
11701159 // Other relative extension prompts
11711160 for (const prompt of prompts.collection.filter(p => p.extension && p.position)) {
1172- const message = await Message.fromPromptAsync(prompt);
1161+ await injectToMain(prompt, prompt.position);
1173- chatCompletion.insert(message, 'main', prompt.position);
11741162 }
11751163
11761164 // Pre-allocation of tokens for tool data