Merge pull request #589 from BlipRanger/feature/tags

Tag, Author, Author Notes Import Support
This commit is contained in:
Cohee
2023-06-29 12:24:18 +03:00
committed by GitHub
5 changed files with 67 additions and 2 deletions

View File

@@ -2271,6 +2271,9 @@
<label for="show_card_avatar_urls"><input id="show_card_avatar_urls" type="checkbox" />
<span data-i18n="Show avatar filenames">Show avatar filenames</span>
</label>
<label for="import_card_tags"><input id="import_card_tags" type="checkbox" />
<span data-i18n="Import Card Tags">Import Card Tags</span>
</label>
<div class="inline-drawer wide100p flexFlowColumn">
<div class="inline-drawer-toggle inline-drawer-header">

View File

@@ -136,7 +136,7 @@ import {
getCharaFilename,
} from "./scripts/utils.js";
import { extension_settings, loadExtensionSettings, runGenerationInterceptors, saveMetadataDebounced } from "./scripts/extensions.js";
import { extension_settings, getContext, loadExtensionSettings, runGenerationInterceptors, saveMetadataDebounced } from "./scripts/extensions.js";
import { executeSlashCommands, getSlashCommandsHelp, registerSlashCommand } from "./scripts/slash-commands.js";
import {
tag_map,
@@ -147,6 +147,7 @@ import {
appendTagToList,
createTagMapFromList,
renameTagKey,
importTags,
tag_filter_types,
} from "./scripts/tags.js";
import {
@@ -6335,6 +6336,12 @@ function importCharacter(file) {
await getCharacters();
select_rm_info(`char_import`, data.file_name, oldSelectedChar);
if(power_user.import_card_tags){
let currentContext = getContext();
let avatarFileName = `${data.file_name}.png`;
let importedCharacter = currentContext.characters.find(character => character.avatar === avatarFileName);
await importTags(importedCharacter);
}
$("#rm_info_block").transition({ opacity: 1, duration: 1000 });
}
},

View File

@@ -580,6 +580,7 @@ function loadPowerUserSettings(settings, data) {
$(`#tokenizer option[value="${power_user.tokenizer}"]`).attr('selected', true);
$(`#pygmalion_formatting option[value=${power_user.pygmalion_formatting}]`).attr("selected", true);
$(`#send_on_enter option[value=${power_user.send_on_enter}]`).attr("selected", true);
$("#import_card_tags").prop("checked", power_user.import_card_tags);
$("#collapse-newlines-checkbox").prop("checked", power_user.collapse_newlines);
$("#pin-examples-checkbox").prop("checked", power_user.pin_examples);
$("#disable-description-formatting-checkbox").prop("checked", power_user.disable_description_formatting);
@@ -1308,6 +1309,11 @@ $(document).ready(() => {
saveSettingsDebounced();
});
$("#import_card_tags").on('input', function () {
power_user.import_card_tags = !!$(this).prop('checked');
saveSettingsDebounced();
});
$("#render_formulas").on("input", function () {
power_user.render_formulas = !!$(this).prop('checked');
reloadMarkdownProcessor(power_user.render_formulas);

View File

@@ -5,6 +5,7 @@ import {
callPopup,
menu_type,
updateVisibleDivs,
getCharacters,
} from "../script.js";
import { selected_group } from "./group-chats.js";
@@ -19,6 +20,7 @@ export {
appendTagToList,
createTagMapFromList,
renameTagKey,
importTags,
};
const random_id = () => Math.round(Date.now() * Math.random()).toString();
@@ -218,6 +220,52 @@ function selectTag(event, ui, listSelector) {
return false;
}
function getExistingTags(new_tags) {
let existing_tags = [];
for (let tag of new_tags) {
if (tags.find(t => t.name === tag)) {
existing_tags.push(tag);
}
}
return existing_tags
}
async function importTags(imported_char) {
let imported_tags = imported_char.tags.filter(t => t !== "ROOT" && t !== "TAVERN");
let existingTags = await getExistingTags(imported_tags);
let newTags = imported_tags.filter(t => !existingTags.includes(t));
let selected_tags = "";
if(newTags.length === 0) {
await callPopup(`<h3>Importing Tags</h3><p>${existingTags.length} exisiting tags have been found.</p>`, 'text');
} else {
selected_tags = await callPopup(`<h3>Importing Tags</h3><p>${existingTags.length} exisiting tags have been found.</p><p>The following ${newTags.length} new tags will be imported.</p>`, 'input', newTags.join(', '));
}
selected_tags = existingTags.concat(selected_tags.split(', '));
selected_tags = selected_tags.filter(t => t !== "");
//Anti-troll measure
if (selected_tags.length > 15) {
selected_tags = selected_tags.slice(0, 15);
}
for (let tagName of selected_tags) {
let tag = tags.find(t => t.name === tagName);
if (!tag) {
tag = createNewTag(tagName);
}
addTagToMap(tag.id);
tag_map[imported_char.avatar].push(tag.id);
};
saveSettingsDebounced();
printTags();
getCharacters();
// need to return false to keep the input clear
return false;
}
function createNewTag(tagName) {
const tag = {
id: random_id(),

View File

@@ -783,6 +783,7 @@ function readFromV2(char) {
mes_example: 'mes_example',
talkativeness: 'extensions.talkativeness',
fav: 'extensions.fav',
tags: 'tags',
};
_.forEach(fieldMappings, (v2Path, charField) => {
@@ -808,7 +809,7 @@ function readFromV2(char) {
return;
}
}
if (!_.isUndefined(char[charField]) && !_.isUndefined(v2Value) && char[charField] !== v2Value) {
if (!_.isUndefined(char[charField]) && !_.isUndefined(v2Value) && String(char[charField]) !== String(v2Value)) {
console.debug(`Spec v2 data mismatch with Spec v1 for field: ${charField}`, char[charField], v2Value);
}
char[charField] = v2Value;