Smudge groups depth prompts in Join mode.

This commit is contained in:
Cohee 2023-10-26 00:39:11 +03:00
parent 8dcfe57888
commit 5cdc3d1d18
3 changed files with 82 additions and 8 deletions

View File

@ -60,6 +60,7 @@ import {
getGroupBlock,
getGroupChatNames,
getGroupCharacterCards,
getGroupDepthPrompts,
} from "./scripts/group-chats.js";
import {
@ -599,7 +600,7 @@ function getCurrentChatId() {
}
const talkativeness_default = 0.5;
const depth_prompt_depth_default = 4;
export const depth_prompt_depth_default = 4;
const per_page_default = 50;
var is_advanced_char_open = false;
@ -2563,9 +2564,18 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
}
// Depth prompt (character-specific A/N)
const depthPromptText = baseChatReplace(characters[this_chid].data?.extensions?.depth_prompt?.prompt?.trim(), name1, name2) || '';
const depthPromptDepth = characters[this_chid].data?.extensions?.depth_prompt?.depth ?? depth_prompt_depth_default;
setExtensionPrompt('DEPTH_PROMPT', depthPromptText, extension_prompt_types.IN_CHAT, depthPromptDepth);
removeDepthPrompts();
const groupDepthPrompts = getGroupDepthPrompts(selected_group);
if (selected_group && Array.isArray(groupDepthPrompts) && groupDepthPrompts.length > 0) {
groupDepthPrompts.forEach((value, index) => {
setExtensionPrompt('DEPTH_PROMPT_' + index, value.text, extension_prompt_types.IN_CHAT, value.depth);
});
} else {
const depthPromptText = baseChatReplace(characters[this_chid].data?.extensions?.depth_prompt?.prompt?.trim(), name1, name2) || '';
const depthPromptDepth = characters[this_chid].data?.extensions?.depth_prompt?.depth ?? depth_prompt_depth_default;
setExtensionPrompt('DEPTH_PROMPT', depthPromptText, extension_prompt_types.IN_CHAT, depthPromptDepth);
}
// Parse example messages
if (!mesExamples.startsWith('<START>')) {
@ -5762,6 +5772,18 @@ export function setExtensionPrompt(key, value, position, depth) {
extension_prompts[key] = { value: String(value), position: Number(position), depth: Number(depth) };
}
/**
* Removes all char A/N prompt injections from the chat.
* To clean up when switching from groups to solo and vice versa.
*/
export function removeDepthPrompts() {
for (const key of Object.keys(extension_prompts)) {
if (key.startsWith('DEPTH_PROMPT')) {
delete extension_prompts[key];
}
}
}
/**
* Adds or updates the metadata for the currently active chat.
* @param {Object} newValues An object with collection of new values to be added into the metadata.

View File

@ -67,6 +67,7 @@ import {
isChatSaving,
setExternalAbortController,
baseChatReplace,
depth_prompt_depth_default,
} from "../script.js";
import { appendTagToList, createTagMapFromList, getTagsList, applyTagsOnCharacterSelect, tag_map, printTagFilters } from './tags.js';
import { FILTER_TYPES, FilterHelper } from './filters.js';
@ -199,6 +200,53 @@ export async function getGroupChat(groupId) {
eventSource.emit(event_types.CHAT_CHANGED, getCurrentChatId());
}
/**
* Gets depth prompts for group members.
* @param {string} groupId Group ID
* @returns {{depth: number, text: string}[]} Array of depth prompts
*/
export function getGroupDepthPrompts(groupId) {
if (!groupId) {
return [];
}
console.debug('getGroupDepthPrompts entered for group: ', groupId);
const group = groups.find(x => x.id === groupId);
if (!group || !Array.isArray(group.members) || !group.members.length) {
return [];
}
if (group.generation_mode === group_generation_mode.SWAP) {
return [];
}
const depthPrompts = [];
for (const member of group.members) {
if (group.disabled_members.includes(member)) {
console.debug(`Skipping disabled group member: ${member}`);
continue;
}
const character = characters.find(x => x.avatar === member);
if (!character) {
console.debug(`Skipping missing member: ${member}`);
continue;
}
const depthPromptText = baseChatReplace(character.data?.extensions?.depth_prompt?.prompt?.trim(), name1, character.name) || '';
const depthPromptDepth = character.data?.extensions?.depth_prompt?.depth ?? depth_prompt_depth_default;
if (depthPromptText) {
depthPrompts.push({ text: depthPromptText, depth: depthPromptDepth });
}
}
return depthPrompts;
}
/**
* Combines group members info a single string. Only for groups with generation mode set to APPEND.
* @param {string} groupId Group ID

View File

@ -1383,12 +1383,16 @@ async function checkWorldInfo(chat, maxContext) {
// Add the depth or AN if enabled
// Put this code here since otherwise, the chat reference is modified
if (extension_settings.note.allowWIScan) {
let depthPrompt = getExtensionPromptByName("DEPTH_PROMPT")
if (depthPrompt) {
textToScan = `${depthPrompt}\n${textToScan}`
for (const key of Object.keys(context.extensionPrompts)) {
if (key.startsWith('DEPTH_PROMPT')) {
const depthPrompt = getExtensionPromptByName(key)
if (depthPrompt) {
textToScan = `${depthPrompt}\n${textToScan}`
}
}
}
let anPrompt = getExtensionPromptByName(NOTE_MODULE_NAME);
const anPrompt = getExtensionPromptByName(NOTE_MODULE_NAME);
if (anPrompt) {
textToScan = `${anPrompt}\n${textToScan}`
}