mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add function to write extension fields to character cards.
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
import { callPopup, eventSource, event_types, saveSettings, saveSettingsDebounced, getRequestHeaders, substituteParams, renderTemplate, animation_duration } from '../script.js';
|
||||
import { hideLoader, showLoader } from './loader.js';
|
||||
import { isSubsetOf } from './utils.js';
|
||||
import { isSubsetOf, setValueByPath } from './utils.js';
|
||||
export {
|
||||
getContext,
|
||||
getApiUrl,
|
||||
@ -883,6 +883,55 @@ async function runGenerationInterceptors(chat, contextSize) {
|
||||
return aborted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a field to the character's data extensions object.
|
||||
* @param {number} characterId Index in the character array
|
||||
* @param {string} key Field name
|
||||
* @param {any} value Field value
|
||||
* @returns {Promise<void>} When the field is written
|
||||
*/
|
||||
export async function writeExtensionField(characterId, key, value) {
|
||||
const context = getContext();
|
||||
const character = context.characters[characterId];
|
||||
if (!character) {
|
||||
console.warn('Character not found', characterId);
|
||||
return;
|
||||
}
|
||||
const path = `data.extensions.${key}`;
|
||||
setValueByPath(character, path, value);
|
||||
|
||||
// Process JSON data
|
||||
if (character.json_data) {
|
||||
const jsonData = JSON.parse(character.json_data);
|
||||
setValueByPath(jsonData, path, value);
|
||||
character.json_data = JSON.stringify(jsonData);
|
||||
|
||||
// Make sure the data doesn't get lost when saving the current character
|
||||
if (Number(characterId) === Number(context.characterId)) {
|
||||
$('#character_json_data').val(character.json_data);
|
||||
}
|
||||
}
|
||||
|
||||
// Save data to the server
|
||||
const saveDataRequest = {
|
||||
avatar: character.avatar,
|
||||
data: {
|
||||
extensions: {
|
||||
[key]: value,
|
||||
},
|
||||
},
|
||||
};
|
||||
const mergeResponse = await fetch('/api/characters/merge-attributes', {
|
||||
method: 'POST',
|
||||
headers: getRequestHeaders(),
|
||||
body: JSON.stringify(saveDataRequest),
|
||||
});
|
||||
|
||||
if (!mergeResponse.ok) {
|
||||
console.error('Failed to save extension field', mergeResponse.statusText);
|
||||
}
|
||||
}
|
||||
|
||||
jQuery(function () {
|
||||
addExtensionsButtonAndMenu();
|
||||
$('#extensionsMenuButton').css('display', 'flex');
|
||||
|
Reference in New Issue
Block a user