mirror of
https://github.com/SillyTavern/SillyTavern-Extras.git
synced 2026-03-10 05:50:10 +00:00
Add expressions plugin
This commit is contained in:
101
extensions/expressions/index.js
Normal file
101
extensions/expressions/index.js
Normal 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);
|
||||
})();
|
||||
8
extensions/expressions/manifest.json
Normal file
8
extensions/expressions/manifest.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"display_name": "Character Expressions",
|
||||
"requires": [
|
||||
"classify"
|
||||
],
|
||||
"js": "index.js",
|
||||
"css": "style.css"
|
||||
}
|
||||
19
extensions/expressions/style.css
Normal file
19
extensions/expressions/style.css
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user