mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add support for external TAI API calls
This commit is contained in:
@ -3,7 +3,8 @@ const port = 8000;
|
||||
const whitelist = ['127.0.0.1']; //Example for add several IP in whitelist: ['127.0.0.1', '192.168.0.10']
|
||||
const whitelistMode = false; //Disabling enabling the ip whitelist mode. true/false
|
||||
const autorun = true; //Autorun in the browser. true/false
|
||||
const enableExtensions = true; //Enables support for TavernAI-extras project
|
||||
|
||||
module.exports = {
|
||||
port, whitelist, whitelistMode, autorun
|
||||
port, whitelist, whitelistMode, autorun, enableExtensions,
|
||||
};
|
||||
|
@ -33,6 +33,8 @@
|
||||
|
||||
|
||||
<script type=module>
|
||||
// API OBJECT FOR EXTERNAL WIRING
|
||||
window['TavernAI'] = {};
|
||||
|
||||
import {encode, decode} from "../scripts/gpt-2-3-tokenizer/mod.js";
|
||||
$(document).ready(function(){
|
||||
@ -765,6 +767,14 @@
|
||||
const newMessage = $(`#chat [mesid="${count_view_mes}"]`);
|
||||
newMessage.data('isSystem', isSystem);
|
||||
|
||||
if (mes.extra?.image) {
|
||||
const image = document.createElement('img');
|
||||
image.src = mes.extra?.image;
|
||||
image.classList.add('img_extra');
|
||||
newMessage.find('.mes_text').css('font-size', '0px');
|
||||
newMessage.find('.mes_text').append(image);
|
||||
}
|
||||
|
||||
if (isSystem) {
|
||||
newMessage.find('.mes_edit').hide();
|
||||
}
|
||||
@ -3371,6 +3381,16 @@
|
||||
}
|
||||
});
|
||||
// end world info settings
|
||||
|
||||
if (data.enable_extensions) {
|
||||
const src = 'scripts/extensions.js';
|
||||
if ($(`script[src="${src}"]`).length === 0) {
|
||||
const script = document.createElement('script');
|
||||
script.type = 'text/javascript';
|
||||
script.src = src;
|
||||
$('body').append(script);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!is_checked_colab) isColab();
|
||||
|
||||
@ -4326,6 +4346,19 @@
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
window['TavernAI'].getContext = function() {
|
||||
return {
|
||||
chat: chat,
|
||||
characters: characters,
|
||||
name1: name1,
|
||||
name2: name2,
|
||||
characterId: this_chid,
|
||||
onlineStatus: online_status,
|
||||
addOneMessage: addOneMessage,
|
||||
generate: Generate,
|
||||
};
|
||||
};
|
||||
});
|
||||
</script>
|
||||
<title>Tavern.AI</title>
|
||||
@ -4921,6 +4954,5 @@
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<script src="scripts/extensions.js" type="module"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -1,11 +1,17 @@
|
||||
|
||||
const settings_html = `
|
||||
<div class="extensions_block">
|
||||
const extensions_urlKey = 'extensions_url';
|
||||
const extensions_autoConnectKey = 'extensions_autoconnect';
|
||||
let extensions = [];
|
||||
|
||||
(function () {
|
||||
const settings_html = `
|
||||
<div class="extensions_block">
|
||||
<hr>
|
||||
<h3>Extensions</h3>
|
||||
<input id="extensions_url" type="text" class="text_pole" />
|
||||
<div class="extensions_url_block">
|
||||
<input id="extensions_connect" type="submit" value="Connect" />
|
||||
<span class="expander"></span>
|
||||
<input id="extensions_autoconnect" type="checkbox"/><h4>Auto-connect</h4>
|
||||
</div>
|
||||
<div id="extensions_status">Not connected</div>
|
||||
@ -14,34 +20,34 @@ const settings_html = `
|
||||
<ul id="extensions_list">
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
</div>
|
||||
`;
|
||||
|
||||
const settings_style = `
|
||||
<style>
|
||||
#extensions_url {
|
||||
const settings_style = `
|
||||
<style>
|
||||
#extensions_url {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
|
||||
#extensions_loaded {
|
||||
#extensions_loaded {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
.extensions_block h3 {
|
||||
.extensions_block h3 {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
}
|
||||
|
||||
#extensions_status {
|
||||
#extensions_status {
|
||||
margin: 10px;
|
||||
opacity: 0.85;
|
||||
font-weight: 700;
|
||||
}
|
||||
}
|
||||
|
||||
.extensions_block input[type="submit"]:hover{
|
||||
.extensions_block input[type="submit"]:hover{
|
||||
background-color: green;
|
||||
}
|
||||
}
|
||||
|
||||
.extensions_block input[type="submit"] {
|
||||
.extensions_block input[type="submit"] {
|
||||
cursor: pointer;
|
||||
color: #fff;
|
||||
opacity: 0.7;
|
||||
@ -49,59 +55,61 @@ const settings_style = `
|
||||
font-size: 1rem;
|
||||
height: 2.5rem;
|
||||
transition: 0.3s;
|
||||
}
|
||||
}
|
||||
|
||||
.extensions_block input[type="checkbox"] {
|
||||
.extensions_block input[type="checkbox"] {
|
||||
margin-left: 10px;
|
||||
}
|
||||
}
|
||||
|
||||
.extensions_url_block {
|
||||
.extensions_url_block {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
margin: 10px;
|
||||
}
|
||||
width: 90%
|
||||
}
|
||||
|
||||
.extensions_url_block- h4 {
|
||||
.extensions_url_block- h4 {
|
||||
display: inline;
|
||||
}
|
||||
}
|
||||
|
||||
.extensions_block {
|
||||
.extensions_block {
|
||||
clear: both;
|
||||
padding: 0.05px; /* clear fix */
|
||||
}
|
||||
}
|
||||
|
||||
.success {
|
||||
.success {
|
||||
color: green;
|
||||
}
|
||||
}
|
||||
|
||||
.failure {
|
||||
.failure {
|
||||
color: red;
|
||||
}
|
||||
</style>
|
||||
`;
|
||||
}
|
||||
|
||||
const urlKey = 'extensions_url';
|
||||
const autoConnectKey = 'extensions_autoconnect';
|
||||
const defaultUrl = "http://localhost:5100";
|
||||
let connectedToApi = false;
|
||||
let extensions = [];
|
||||
.expander {
|
||||
flex-grow: 1;
|
||||
}
|
||||
</style>
|
||||
`;
|
||||
|
||||
async function connectClickHandler() {
|
||||
const defaultUrl = "http://localhost:5100";
|
||||
let connectedToApi = false;
|
||||
|
||||
async function connectClickHandler() {
|
||||
const baseUrl = $("#extensions_url").val();
|
||||
localStorage.setItem(urlKey, baseUrl);
|
||||
localStorage.setItem(extensions_urlKey, baseUrl);
|
||||
await connectToApi(baseUrl);
|
||||
}
|
||||
}
|
||||
|
||||
function autoConnectInputHandler() {
|
||||
function autoConnectInputHandler() {
|
||||
const value = $(this).prop('checked');
|
||||
localStorage.setItem(autoConnectKey, value.toString());
|
||||
localStorage.setItem(extensions_autoConnectKey, value.toString());
|
||||
|
||||
if (value && !connectedToApi) {
|
||||
$("#extensions_connect").trigger('click');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function connectToApi(baseUrl) {
|
||||
async function connectToApi(baseUrl) {
|
||||
const url = new URL(baseUrl);
|
||||
url.pathname = '/api/extensions';
|
||||
|
||||
@ -119,9 +127,9 @@ async function connectToApi(baseUrl) {
|
||||
catch {
|
||||
updateStatus(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function updateStatus(success) {
|
||||
function updateStatus(success) {
|
||||
connectedToApi = success;
|
||||
const _text = success ? 'Connected to API' : 'Could not connect to API';
|
||||
const _class = success ? 'success' : 'failure';
|
||||
@ -140,9 +148,9 @@ function updateStatus(success) {
|
||||
$('#extensions_loaded').hide(200);
|
||||
$('#extensions_list').empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function applyExtensions(baseUrl) {
|
||||
function applyExtensions(baseUrl) {
|
||||
const url = new URL(baseUrl);
|
||||
|
||||
if (!Array.isArray(extensions) || extensions.length === 0) {
|
||||
@ -150,19 +158,11 @@ function applyExtensions(baseUrl) {
|
||||
}
|
||||
|
||||
for (let extension of extensions) {
|
||||
if (extension.metadata.js) {
|
||||
url.pathname = `/api/script/${extension.name}`;
|
||||
const src = url.toString();
|
||||
|
||||
if ($(`script[src="${src}"]`).length === 0) {
|
||||
const script = document.createElement('script');
|
||||
script.type = 'module';
|
||||
script.src = src;
|
||||
$('body').append(script);
|
||||
}
|
||||
|
||||
addExtensionStyle(extension);
|
||||
addExtensionScript(extension);
|
||||
}
|
||||
|
||||
function addExtensionStyle(extension) {
|
||||
if (extension.metadata.css) {
|
||||
url.pathname = `/api/style/${extension.name}`;
|
||||
const href = url.toString();
|
||||
@ -176,15 +176,30 @@ function applyExtensions(baseUrl) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$(document).ready(async function () {
|
||||
const url = localStorage.getItem(urlKey) ?? defaultUrl;
|
||||
const autoConnect = Boolean(localStorage.getItem(autoConnectKey)) ?? false;
|
||||
function addExtensionScript(extension) {
|
||||
if (extension.metadata.js) {
|
||||
url.pathname = `/api/script/${extension.name}`;
|
||||
const src = url.toString();
|
||||
|
||||
if ($(`script[src="${src}"]`).length === 0) {
|
||||
const script = document.createElement('script');
|
||||
script.type = 'module';
|
||||
script.src = src;
|
||||
$('body').append(script);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$(document).ready(async function () {
|
||||
const url = localStorage.getItem(extensions_urlKey) ?? defaultUrl;
|
||||
const autoConnect = Boolean(localStorage.getItem(extensions_autoConnectKey)) ?? false;
|
||||
$('#rm_api_block').append(settings_html);
|
||||
$('head').append(settings_style);
|
||||
$("#extensions_url").val(url);
|
||||
$("#extensions_connect").on('click', connectClickHandler);
|
||||
$("#extensions_autoconnect").on('input', autoConnectInputHandler);
|
||||
$("#extensions_autoconnect").prop('checked', autoConnect).trigger('input');
|
||||
});
|
||||
});
|
||||
})();
|
@ -25,7 +25,7 @@ const server_port = config.port;
|
||||
const whitelist = config.whitelist;
|
||||
const whitelistMode = config.whitelistMode;
|
||||
const autorun = config.autorun;
|
||||
|
||||
const enableExtensions = config.enableExtensions;
|
||||
|
||||
|
||||
var Client = require('node-rest-client').Client;
|
||||
@ -804,7 +804,8 @@ app.post('/getsettings', jsonParser, (request, response) => { //Wintermute's cod
|
||||
koboldai_setting_names,
|
||||
world_names,
|
||||
novelai_settings,
|
||||
novelai_setting_names
|
||||
novelai_setting_names,
|
||||
enable_extensions: enableExtensions,
|
||||
});
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user