#1090 Save Kobold/ooba servers history

This commit is contained in:
Cohee 2023-09-08 22:44:06 +03:00
parent 786b87952e
commit ea01247bcf
6 changed files with 104 additions and 18 deletions

View File

@ -83,6 +83,7 @@
<script type="module" src="scripts/preset-manager.js"></script>
<script type="module" src="scripts/filters.js"></script>
<script type="module" src="scripts/personas.js"></script>
<script type="module" src="scripts/server-history.js"></script>
<title>SillyTavern</title>
</head>
@ -1766,8 +1767,8 @@
<div id="kobold_api_block">
<h4 data-i18n="API url">API url</h4>
<small data-i18n="Example: http://127.0.0.1:5000/api ">Example: http://127.0.0.1:5000/api </small>
<input id="api_url_text" name="api_url" class="text_pole" placeholder="http://127.0.0.1:5000/api" maxlength="500" value="" autocomplete="off">
<div id="api_button" class="menu_button" type="submit" data-i18n="Connect">Connect</div>
<input id="api_url_text" name="api_url" class="text_pole" placeholder="http://127.0.0.1:5000/api" maxlength="500" value="" autocomplete="off" data-server-history="kobold">
<div id="api_button" class="menu_button" type="submit" data-i18n="Connect" data-server-connect="kobold">Connect</div>
<div id="api_loading" class="api-load-icon fa-solid fa-hourglass fa-spin"></div>
</div>
<div id="online_status2">
@ -1857,15 +1858,15 @@
<div class="flex1">
<h4 data-i18n="Blocking API url">Blocking API url</h4>
<small data-i18n="Example: http://127.0.0.1:5000/api ">Example: http://127.0.0.1:5000/api </small>
<input id="textgenerationwebui_api_url_text" name="textgenerationwebui_api_url" class="text_pole wide100p" maxlength="500" value="" autocomplete="off">
<input id="textgenerationwebui_api_url_text" name="textgenerationwebui_api_url" class="text_pole wide100p" maxlength="500" value="" autocomplete="off" data-server-history="ooba_blocking">
</div>
<div class="flex1">
<h4 data-i18n="Streaming API url">Streaming API url</h4>
<small data-i18n="Example: ws://127.0.0.1:5005/api/v1/stream">Example: ws://127.0.0.1:5005/api/v1/stream </small>
<input id="streaming_url_textgenerationwebui" type="text" class="text_pole wide100p" maxlength="500" value="" autocomplete="off">
<input id="streaming_url_textgenerationwebui" type="text" class="text_pole wide100p" maxlength="500" value="" autocomplete="off" data-server-history="ooba_streaming">
</div>
</div>
<div id="api_button_textgenerationwebui" class="menu_button" type="submit" data-i18n="Connect">Connect</div>
<div id="api_button_textgenerationwebui" class="menu_button" type="submit" data-i18n="Connect" data-server-connect="ooba_blocking,ooba_streaming">Connect</div>
<div id="api_loading_textgenerationwebui" class="api-load-icon fa-solid fa-hourglass fa-spin"></div>
</div>
</form>

View File

@ -7535,7 +7535,6 @@ jQuery(async function () {
///////////////////////////////////////////////////////////////////////////////////
$("#api_button").click(function (e) {
e.stopPropagation();
if ($("#api_url_text").val() != "") {
let value = formatKoboldUrl(String($("#api_url_text").val()).trim());
@ -7568,7 +7567,6 @@ jQuery(async function () {
});
$("#api_button_textgenerationwebui").click(async function (e) {
e.stopPropagation();
const url_source = api_use_mancer_webui ? "#mancer_api_url_text" : "#textgenerationwebui_api_url_text";
if ($(url_source).val() != "") {
let value = formatTextGenURL(String($(url_source).val()).trim(), api_use_mancer_webui);

View File

@ -31,7 +31,7 @@ import {
SECRET_KEYS,
secret_state,
} from "./secrets.js";
import { debounce, delay, getStringHash, waitUntilCondition } from "./utils.js";
import { debounce, delay, getStringHash, isUrlOrAPIKey, waitUntilCondition } from "./utils.js";
import { chat_completion_sources, oai_settings } from "./openai.js";
import { getTokenCount } from "./tokenizers.js";
@ -403,15 +403,6 @@ function RA_autoconnect(PrevApi) {
}
}
function isUrlOrAPIKey(string) {
try {
new URL(string);
return true;
} catch (_) {
// return pattern.test(string);
}
}
function OpenNavPanels() {
const deviceInfo = getDeviceInfo();
if (deviceInfo && deviceInfo.device.type === 'desktop') {

View File

@ -201,6 +201,7 @@ let power_user = {
fuzzy_search: false,
encode_tags: false,
lazy_load: 0,
servers: [],
};
let themes = [];

View File

@ -0,0 +1,86 @@
import { saveSettingsDebounced } from "../script.js";
import { power_user } from "./power-user.js";
import { isUrlOrAPIKey } from "./utils.js";
/**
* @param {{ term: string; }} request
* @param {function} resolve
* @param {string} serverLabel
*/
function findServers(request, resolve, serverLabel) {
if (!power_user.servers) {
power_user.servers = [];
}
const needle = request.term.toLowerCase();
const result = power_user.servers.filter(x => x.label == serverLabel).sort((a, b) => b.lastConnection - a.lastConnection).map(x => x.url).slice(0, 5);
const hasExactMatch = result.findIndex(x => x.toLowerCase() == needle) !== -1;
if (request.term && !hasExactMatch) {
result.unshift(request.term);
}
resolve(result);
}
function selectServer(event, ui, serverLabel) {
// unfocus the input
$(event.target).val(ui.item.value).trigger('blur');
$('[data-server-connect]').each(function () {
const serverLabels = String($(this).data('server-connect')).split(',');
if (serverLabels.includes(serverLabel)) {
$(this).trigger('click');
}
});
}
function createServerAutocomplete() {
const inputElement = $(this);
const serverLabel = inputElement.data('server-history');
inputElement
.autocomplete({
source: (i, o) => findServers(i, o, serverLabel),
select: (e, u) => selectServer(e, u, serverLabel),
minLength: 0,
})
.focus(onInputFocus); // <== show tag list on click
}
function onInputFocus() {
$(this).autocomplete('search', $(this).val());
}
function onServerConnectClick() {
const serverLabels = String($(this).data('server-connect')).split(',');
serverLabels.forEach(serverLabel => {
if (!power_user.servers) {
power_user.servers = [];
}
const value = String($(`[data-server-history="${serverLabel}"]`).val()).toLowerCase().trim();
// Don't save empty values or invalid URLs
if (!value || !isUrlOrAPIKey(value)) {
return;
}
const server = power_user.servers.find(x => x.url === value && x.label === serverLabel);
if (!server) {
power_user.servers.push({ label: serverLabel, url: value, lastConnection: Date.now() });
} else {
server.lastConnection = Date.now();
}
saveSettingsDebounced();
});
}
jQuery(function () {
$('[data-server-history]').each(createServerAutocomplete);
$(document).on('click', '[data-server-connect]', onServerConnectClick);
});

View File

@ -18,6 +18,15 @@ export function escapeHtml(str) {
return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}
export function isUrlOrAPIKey(value) {
try {
new URL(value);
return true;
} catch (_) {
return false;
}
}
/**
* Determines if a value is unique in an array.
* @param {any} value Current value.