Add expressions plugin

This commit is contained in:
SillyLossy
2023-03-04 22:23:22 +02:00
parent ada45f2c70
commit ea44f763c2
4 changed files with 141 additions and 1 deletions

View File

@@ -0,0 +1,101 @@
const MODULE_NAME = 'expressions';
const SETTINGS_KEY = 'extensions_memory_settings';
const UPDATE_INTERVAL = 1000;
let lastCharacter = null;
let lastMessage = null;
let inApiCall = false;
const getContext = function () {
return window['TavernAI'].getContext();
}
const getApiUrl = function () {
return localStorage.getItem('extensions_url');
}
async function moduleWorker() {
function getLastCharacterMessage() {
const reversedChat = context.chat.slice().reverse();
for (let mes of reversedChat) {
if (mes.is_user || mes.is_system) {
continue;
}
return mes.mes;
}
return '';
}
const context = getContext();
// group chats and non-characters not supported
if (context.groupId || !context.characterId) {
removeExpression();
return;
}
// character changed
if (lastCharacter !== context.characterId) {
removeExpression();
}
// check if last message changed
const currentLastMessage = getLastCharacterMessage();
if (lastCharacter === context.characterId && lastMessage === currentLastMessage) {
return;
}
// API is busy
if (inApiCall) {
return;
}
try {
inApiCall = true;
const url = new URL(getApiUrl());
url.pathname = '/api/classify';
const apiResult = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: currentLastMessage })
});
if (apiResult.ok) {
const data = await apiResult.json();
const expression = data.classification[0].label;
setExpression(context.name2, expression);
}
}
catch (error) {
console.log(error);
}
finally {
inApiCall = false;
lastCharacter = context.characterId;
lastMessage = currentLastMessage;
}
}
function removeExpression() {
$('div.expression').css('background-image', 'unset');
}
function setExpression(character, expression) {
const imgUrl = `url('/characters/${character}/${expression}.png')`;
$('div.expression').css('background-image', imgUrl);
}
(function () {
function addExpressionImage() {
const html = `<div class="expression"></div>`
$('body').append(html);
}
addExpressionImage();
setInterval(moduleWorker, UPDATE_INTERVAL);
})();

View File

@@ -0,0 +1,8 @@
{
"display_name": "Character Expressions",
"requires": [
"classify"
],
"js": "index.js",
"css": "style.css"
}

View File

@@ -0,0 +1,19 @@
div.expression {
background-image: unset;
background-repeat: no-repeat;
background-size: contain;
width: 200px;
height: 300px;
position: fixed;
left: 0;
bottom: 0;
margin-left: 10px;
filter: drop-shadow(2px 2px 4px #cccccc84);
transition: 500ms;
}
@media screen and (max-width:1200px) {
div.expression {
display: none;
}
}

View File

@@ -135,7 +135,7 @@ async function moduleWorker() {
&& chat[chat.length - 1].extra.memory
&& lastMessageId === chat.length
&& getStringHash(chat[chat.length - 1].mes) !== lastMessageHash) {
chat[chat.length - 1].extra.memory = null;
delete chat[chat.length - 1].extra.memory;
}
try {
@@ -228,7 +228,19 @@ async function summarizeChat(context) {
}
function onMemoryRestoreClick() {
const context = getContext();
const content = $('#memory_contents').val();
const reversedChat = context.chat.slice().reverse();
for (let mes of reversedChat) {
if (mes.extra && mes.extra.memory == content) {
delete mes.extra.memory;
break;
}
}
const newContent = getLatestMemoryFromChat(context.chat);
setMemoryContext(newContent, false);
}
function onMemoryContentInput() {