Add role selection to Character's Note depth prompt

This commit is contained in:
Cohee 2024-03-30 23:12:01 +02:00
parent 50670c1e6a
commit 153f75cf1a
4 changed files with 55 additions and 7 deletions

View File

@ -4505,7 +4505,7 @@
Character's Note
</span>
</h4>
<textarea id="depth_prompt_prompt" name="depth_prompt_prompt" class="text_pole" rows="2" maxlength="50000" autocomplete="off" form="form_create" placeholder="(Text to be inserted in-chat @ designated depth)"></textarea>
<textarea id="depth_prompt_prompt" name="depth_prompt_prompt" class="text_pole" rows="5" maxlength="50000" autocomplete="off" form="form_create" placeholder="(Text to be inserted in-chat @ designated depth and role)"></textarea>
</div>
<div>
<h4>
@ -4513,7 +4513,17 @@
@ Depth
</span>
</h4>
<input id="depth_prompt_depth" name="depth_prompt_depth" class="text_pole widthUnset m-t-0" type="number" min="0" max="999" value="4" form="form_create" />
<input id="depth_prompt_depth" name="depth_prompt_depth" class="text_pole textarea_compact m-t-0" type="number" min="0" max="999" value="4" form="form_create" />
<h4>
<span data-i18n="Role">
Role
</span>
</h4>
<select id="depth_prompt_role" name="depth_prompt_role" form="form_create" class="text_pole textarea_compact m-t-0">
<option value="system" data-i18n="System">System</option>
<option value="user" data-i18n="User">User</option>
<option value="assistant" data-i18n="Assistant">Assistant</option>
</select>
<div class="extension_token_counter">
Tokens: <span data-token-counter="depth_prompt_prompt" data-token-permanent="true">counting...</span>
</div>

View File

@ -752,6 +752,7 @@ function getCurrentChatId() {
const talkativeness_default = 0.5;
export const depth_prompt_depth_default = 4;
export const depth_prompt_role_default = 'system';
const per_page_default = 50;
var is_advanced_char_open = false;
@ -778,6 +779,7 @@ let create_save = {
alternate_greetings: [],
depth_prompt_prompt: '',
depth_prompt_depth: depth_prompt_depth_default,
depth_prompt_role: depth_prompt_role_default,
extensions: {},
};
@ -3114,12 +3116,14 @@ async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, qu
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, extension_settings.note.allowWIScan);
const role = getExtensionPromptRoleByName(value.role);
setExtensionPrompt('DEPTH_PROMPT_' + index, value.text, extension_prompt_types.IN_CHAT, value.depth, extension_settings.note.allowWIScan, role);
});
} 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, extension_settings.note.allowWIScan);
const depthPromptRole = getExtensionPromptRoleByName(characters[this_chid].data?.extensions?.depth_prompt?.role ?? depth_prompt_role_default);
setExtensionPrompt('DEPTH_PROMPT', depthPromptText, extension_prompt_types.IN_CHAT, depthPromptDepth, extension_settings.note.allowWIScan, depthPromptRole);
}
// Parse example messages
@ -6697,6 +6701,7 @@ export function select_selected_character(chid) {
$('#scenario_pole').val(characters[chid].scenario);
$('#depth_prompt_prompt').val(characters[chid].data?.extensions?.depth_prompt?.prompt ?? '');
$('#depth_prompt_depth').val(characters[chid].data?.extensions?.depth_prompt?.depth ?? depth_prompt_depth_default);
$('#depth_prompt_role').val(characters[chid].data?.extensions?.depth_prompt?.role ?? depth_prompt_role_default);
$('#talkativeness_slider').val(characters[chid].talkativeness || talkativeness_default);
$('#mes_example_textarea').val(characters[chid].mes_example);
$('#selected_chat_pole').val(characters[chid].chat);
@ -6767,6 +6772,7 @@ function select_rm_create() {
$('#scenario_pole').val(create_save.scenario);
$('#depth_prompt_prompt').val(create_save.depth_prompt_prompt);
$('#depth_prompt_depth').val(create_save.depth_prompt_depth);
$('#depth_prompt_role').val(create_save.depth_prompt_role);
$('#mes_example_textarea').val(create_save.mes_example);
$('#character_json_data').val('');
$('#avatar_div').css('display', 'flex');
@ -6810,6 +6816,30 @@ export function setExtensionPrompt(key, value, position, depth, scan = false, ro
};
}
/**
* Gets a enum value of the extension prompt role by its name.
* @param {string} roleName The name of the extension prompt role.
* @returns {number} The role id of the extension prompt.
*/
export function getExtensionPromptRoleByName(roleName) {
// If the role is already a valid number, return it
if (typeof roleName === 'number' && Object.values(extension_prompt_roles).includes(roleName)) {
return roleName;
}
switch (roleName) {
case 'system':
return extension_prompt_roles.SYSTEM;
case 'user':
return extension_prompt_roles.USER;
case 'assistant':
return extension_prompt_roles.ASSISTANT;
}
// Skill issue?
return extension_prompt_roles.SYSTEM;
}
/**
* Removes all char A/N prompt injections from the chat.
* To clean up when switching from groups to solo and vice versa.
@ -7425,6 +7455,7 @@ async function createOrEditCharacter(e) {
{ id: '#scenario_pole', callback: value => create_save.scenario = value },
{ id: '#depth_prompt_prompt', callback: value => create_save.depth_prompt_prompt = value },
{ id: '#depth_prompt_depth', callback: value => create_save.depth_prompt_depth = value, defaultValue: depth_prompt_depth_default },
{ id: '#depth_prompt_role', callback: value => create_save.depth_prompt_role = value, defaultValue: depth_prompt_role_default },
{ id: '#mes_example_textarea', callback: value => create_save.mes_example = value },
{ id: '#character_json_data', callback: () => { } },
{ id: '#alternate_greetings_template', callback: value => create_save.alternate_greetings = value, defaultValue: [] },
@ -8794,6 +8825,7 @@ jQuery(async function () {
'#talkativeness_slider': function () { create_save.talkativeness = Number($('#talkativeness_slider').val()); },
'#depth_prompt_prompt': function () { create_save.depth_prompt_prompt = String($('#depth_prompt_prompt').val()); },
'#depth_prompt_depth': function () { create_save.depth_prompt_depth = Number($('#depth_prompt_depth').val()); },
'#depth_prompt_role': function () { create_save.depth_prompt_role = String($('#depth_prompt_role').val()); },
};
Object.keys(elementsToUpdate).forEach(function (id) {

View File

@ -68,6 +68,7 @@ import {
depth_prompt_depth_default,
loadItemizedPrompts,
animation_duration,
depth_prompt_role_default,
} from '../script.js';
import { printTagList, createTagMapFromList, applyTagsOnCharacterSelect, tag_map } from './tags.js';
import { FILTER_TYPES, FilterHelper } from './filters.js';
@ -284,7 +285,7 @@ export function findGroupMemberId(arg) {
* Gets depth prompts for group members.
* @param {string} groupId Group ID
* @param {number} characterId Current Character ID
* @returns {{depth: number, text: string}[]} Array of depth prompts
* @returns {{depth: number, text: string, role: string}[]} Array of depth prompts
*/
export function getGroupDepthPrompts(groupId, characterId) {
if (!groupId) {
@ -320,9 +321,10 @@ export function getGroupDepthPrompts(groupId, characterId) {
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;
const depthPromptRole = character.data?.extensions?.depth_prompt?.role ?? depth_prompt_role_default;
if (depthPromptText) {
depthPrompts.push({ text: depthPromptText, depth: depthPromptDepth });
depthPrompts.push({ text: depthPromptText, depth: depthPromptDepth, role: depthPromptRole });
}
}

View File

@ -210,7 +210,8 @@ function convertToV2(char) {
creator: char.creator,
tags: char.tags,
depth_prompt_prompt: char.depth_prompt_prompt,
depth_prompt_response: char.depth_prompt_response,
depth_prompt_depth: char.depth_prompt_depth,
depth_prompt_role: char.depth_prompt_role,
});
result.chat = char.chat ?? humanizedISO8601DateTime();
@ -331,9 +332,12 @@ function charaFormatData(data) {
// Spec extension: depth prompt
const depth_default = 4;
const role_default = 'system';
const depth_value = !isNaN(Number(data.depth_prompt_depth)) ? Number(data.depth_prompt_depth) : depth_default;
const role_value = data.depth_prompt_role ?? role_default;
_.set(char, 'data.extensions.depth_prompt.prompt', data.depth_prompt_prompt ?? '');
_.set(char, 'data.extensions.depth_prompt.depth', depth_value);
_.set(char, 'data.extensions.depth_prompt.role', role_value);
//_.set(char, 'data.extensions.create_date', humanizedISO8601DateTime());
//_.set(char, 'data.extensions.avatar', 'none');
//_.set(char, 'data.extensions.chat', data.ch_name + ' - ' + humanizedISO8601DateTime());