mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Refactor API keys handling. Remove ST hosting from colab
This commit is contained in:
@@ -7,21 +7,14 @@ import {
|
||||
online_status,
|
||||
main_api,
|
||||
api_server,
|
||||
nai_settings,
|
||||
api_server_textgenerationwebui,
|
||||
is_send_press,
|
||||
getTokenCount,
|
||||
menu_type,
|
||||
selectRightMenuWithAnimation,
|
||||
select_selected_character,
|
||||
setCharacterId,
|
||||
|
||||
|
||||
} from "../script.js";
|
||||
|
||||
import {
|
||||
select_group_chats,
|
||||
} from "./group-chats.js";
|
||||
|
||||
import {
|
||||
power_user,
|
||||
@@ -30,8 +23,10 @@ import {
|
||||
|
||||
import { LoadLocal, SaveLocal, ClearLocal, CheckLocal, LoadLocalBool } from "./f-localStorage.js";
|
||||
import { selected_group, is_group_generating, getGroupAvatar, groups } from "./group-chats.js";
|
||||
import { oai_settings } from "./openai.js";
|
||||
import { poe_settings } from "./poe.js";
|
||||
import {
|
||||
SECRET_KEYS,
|
||||
secret_state,
|
||||
} from "./secrets.js";
|
||||
|
||||
var NavToggle = document.getElementById("nav-toggle");
|
||||
var RPanelPin = document.getElementById("rm_button_panel_pin");
|
||||
@@ -368,13 +363,11 @@ function RA_autoconnect(PrevApi) {
|
||||
case 'kobold':
|
||||
if (api_server && isUrlOrAPIKey(api_server)) {
|
||||
$("#api_button").click();
|
||||
|
||||
}
|
||||
break;
|
||||
case 'novel':
|
||||
if (nai_settings.api_key_novel) {
|
||||
if (secret_state[SECRET_KEYS.NOVEL]) {
|
||||
$("#api_button_novel").click();
|
||||
|
||||
}
|
||||
break;
|
||||
case 'textgenerationwebui':
|
||||
@@ -383,12 +376,12 @@ function RA_autoconnect(PrevApi) {
|
||||
}
|
||||
break;
|
||||
case 'openai':
|
||||
if (oai_settings.api_key_openai) {
|
||||
if (secret_state[SECRET_KEYS.OPENAI]) {
|
||||
$("#api_button_openai").click();
|
||||
}
|
||||
break;
|
||||
case 'poe':
|
||||
if (poe_settings.token) {
|
||||
if (secret_state[SECRET_KEYS.POE]) {
|
||||
$("#poe_connect").click();
|
||||
}
|
||||
break;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { saveSettingsDebounced, changeMainAPI, callPopup, setGenerationProgress, CLIENT_VERSION } from "../script.js";
|
||||
import { saveSettingsDebounced, changeMainAPI, callPopup, setGenerationProgress, CLIENT_VERSION, getRequestHeaders } from "../script.js";
|
||||
import { delay } from "./utils.js";
|
||||
|
||||
export {
|
||||
@@ -14,7 +14,6 @@ export {
|
||||
let models = [];
|
||||
|
||||
let horde_settings = {
|
||||
api_key: '0000000000',
|
||||
models: [],
|
||||
use_horde: false,
|
||||
auto_adjust_response_length: true,
|
||||
@@ -30,14 +29,6 @@ const getRequestArgs = () => ({
|
||||
"Client-Agent": CLIENT_VERSION,
|
||||
}
|
||||
});
|
||||
const postRequestArgs = () => ({
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"apikey": horde_settings.api_key,
|
||||
"Client-Agent": CLIENT_VERSION,
|
||||
}
|
||||
});
|
||||
|
||||
async function getWorkers() {
|
||||
const response = await fetch('https://horde.koboldai.net/api/v2/workers?type=text', getRequestArgs());
|
||||
@@ -107,8 +98,12 @@ async function generateHorde(prompt, params) {
|
||||
"models": horde_settings.models,
|
||||
};
|
||||
|
||||
const response = await fetch("https://horde.koboldai.net/api/v2/generate/text/async", {
|
||||
...postRequestArgs(),
|
||||
const response = await fetch("/generate_horde", {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
...getRequestHeaders(),
|
||||
"Client-Agent": CLIENT_VERSION,
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
@@ -176,12 +171,6 @@ async function getHordeModels() {
|
||||
if (horde_settings.models.length && models.filter(m => horde_settings.models.includes(m.name)).length === 0) {
|
||||
horde_settings.models = [];
|
||||
}
|
||||
|
||||
// if no models preselected - select a first one in dropdown
|
||||
/*if (Array.isArray(horde_settings.models) || horde_settings.models.length == 0) {
|
||||
$('#horde_model').first()
|
||||
horde_settings.models = [.find(":selected").val()];
|
||||
}*/
|
||||
}
|
||||
|
||||
function loadHordeSettings(settings) {
|
||||
@@ -190,7 +179,6 @@ function loadHordeSettings(settings) {
|
||||
}
|
||||
|
||||
$('#use_horde').prop("checked", horde_settings.use_horde).trigger('input');
|
||||
$('#horde_api_key').val(horde_settings.api_key);
|
||||
$('#horde_auto_adjust_response_length').prop("checked", horde_settings.auto_adjust_response_length);
|
||||
$('#horde_auto_adjust_context_length').prop("checked", horde_settings.auto_adjust_context_length);
|
||||
}
|
||||
@@ -219,11 +207,6 @@ jQuery(function () {
|
||||
saveSettingsDebounced();
|
||||
});
|
||||
|
||||
$("#horde_api_key").on("input", function () {
|
||||
horde_settings.api_key = $(this).val();
|
||||
saveSettingsDebounced();
|
||||
});
|
||||
|
||||
$("#horde_auto_adjust_response_length").on("input", function () {
|
||||
horde_settings.auto_adjust_response_length = !!$(this).prop("checked");
|
||||
saveSettingsDebounced();
|
||||
|
@@ -14,7 +14,6 @@ const nai_settings = {
|
||||
rep_pen_novel: 1,
|
||||
rep_pen_size_novel: 100,
|
||||
model_novel: "euterpe-v2",
|
||||
api_key_novel: "",
|
||||
preset_settings_novel: "Classic-Euterpe",
|
||||
};
|
||||
|
||||
@@ -44,12 +43,6 @@ function loadNovelPreset(preset) {
|
||||
}
|
||||
|
||||
function loadNovelSettings(settings) {
|
||||
//load Novel API KEY is exists
|
||||
if (settings.api_key_novel != undefined) {
|
||||
nai_settings.api_key_novel = settings.api_key_novel;
|
||||
$("#api_key_novel").val(nai_settings.api_key_novel);
|
||||
}
|
||||
|
||||
//load the rest of the Novel settings without any checks
|
||||
nai_settings.model_novel = settings.model_novel;
|
||||
$(`#model_novel_select option[value=${nai_settings.model_novel}]`).attr("selected", true);
|
||||
|
@@ -23,6 +23,11 @@ import { groups, selected_group } from "./group-chats.js";
|
||||
import {
|
||||
power_user,
|
||||
} from "./power-user.js";
|
||||
import {
|
||||
SECRET_KEYS,
|
||||
secret_state,
|
||||
writeSecret,
|
||||
} from "./secrets.js";
|
||||
|
||||
import {
|
||||
delay,
|
||||
@@ -76,7 +81,6 @@ const tokenCache = {};
|
||||
|
||||
const default_settings = {
|
||||
preset_settings_openai: 'Default',
|
||||
api_key_openai: '',
|
||||
temp_openai: 0.9,
|
||||
freq_pen_openai: 0.7,
|
||||
pres_pen_openai: 0.7,
|
||||
@@ -101,7 +105,6 @@ const default_settings = {
|
||||
|
||||
const oai_settings = {
|
||||
preset_settings_openai: 'Default',
|
||||
api_key_openai: '',
|
||||
temp_openai: 1.0,
|
||||
freq_pen_openai: 0,
|
||||
pres_pen_openai: 0,
|
||||
@@ -666,11 +669,6 @@ function countTokens(messages, full = false) {
|
||||
}
|
||||
|
||||
function loadOpenAISettings(data, settings) {
|
||||
if (settings.api_key_openai != undefined) {
|
||||
oai_settings.api_key_openai = settings.api_key_openai;
|
||||
$("#api_key_openai").val(oai_settings.api_key_openai);
|
||||
}
|
||||
|
||||
openai_setting_names = data.openai_setting_names;
|
||||
openai_settings = data.openai_settings;
|
||||
openai_settings = data.openai_settings;
|
||||
@@ -766,7 +764,6 @@ async function getStatusOpen() {
|
||||
if (is_get_status_openai) {
|
||||
|
||||
let data = {
|
||||
key: oai_settings.api_key_openai,
|
||||
reverse_proxy: oai_settings.reverse_proxy,
|
||||
};
|
||||
|
||||
@@ -873,13 +870,10 @@ async function saveOpenAIPreset(name, settings) {
|
||||
}
|
||||
|
||||
async function showApiKeyUsage() {
|
||||
const body = JSON.stringify({ key: oai_settings.api_key_openai });
|
||||
|
||||
try {
|
||||
const response = await fetch('/openai_usage', {
|
||||
method: 'POST',
|
||||
headers: getRequestHeaders(),
|
||||
body: body,
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
@@ -1168,15 +1162,23 @@ function onReverseProxyInput() {
|
||||
|
||||
async function onConnectButtonClick(e) {
|
||||
e.stopPropagation();
|
||||
if ($('#api_key_openai').val() != '') {
|
||||
$("#api_loading_openai").css("display", 'inline-block');
|
||||
$("#api_button_openai").css("display", 'none');
|
||||
oai_settings.api_key_openai = $('#api_key_openai').val().trim();
|
||||
saveSettingsDebounced();
|
||||
is_get_status_openai = true;
|
||||
is_api_button_press_openai = true;
|
||||
await getStatusOpen();
|
||||
const api_key_openai = $('#api_key_openai').val().trim();
|
||||
|
||||
if (api_key_openai.length) {
|
||||
await writeSecret(SECRET_KEYS.OPENAI, api_key_openai);
|
||||
}
|
||||
|
||||
if (!secret_state[SECRET_KEYS.OPENAI]) {
|
||||
console.log('No secret key saved for OpenAI');
|
||||
return;
|
||||
}
|
||||
|
||||
$("#api_loading_openai").css("display", 'inline-block');
|
||||
$("#api_button_openai").css("display", 'none');
|
||||
saveSettingsDebounced();
|
||||
is_get_status_openai = true;
|
||||
is_api_button_press_openai = true;
|
||||
await getStatusOpen();
|
||||
}
|
||||
|
||||
$(document).ready(function () {
|
||||
|
@@ -7,6 +7,11 @@ import {
|
||||
getTokenCount,
|
||||
getRequestHeaders,
|
||||
} from "../script.js";
|
||||
import {
|
||||
SECRET_KEYS,
|
||||
secret_state,
|
||||
writeSecret,
|
||||
} from "./secrets.js";
|
||||
|
||||
export {
|
||||
is_get_status_poe,
|
||||
@@ -38,7 +43,6 @@ const DEFAULT_CHARACTER_NUDGE_MESSAGE = "[Your the next response shall only be w
|
||||
const DEFAULT_IMPERSONATION_PROMPT = "[Write 1 reply only in internet RP style from the point of view of {{user}}, using the chat history so far as a guideline for the writing style of {{user}}. Don't write as {{char}} or system.]";
|
||||
|
||||
const poe_settings = {
|
||||
token: '',
|
||||
bot: 'a2',
|
||||
jailbreak_response: DEFAULT_JAILBREAK_RESPONSE,
|
||||
jailbreak_message: DEFAULT_JAILBREAK_MESSAGE,
|
||||
@@ -67,7 +71,6 @@ function loadPoeSettings(settings) {
|
||||
$('#poe_auto_jailbreak').prop('checked', poe_settings.auto_jailbreak);
|
||||
$('#poe_auto_purge').prop('checked', poe_settings.auto_purge);
|
||||
$('#poe_streaming').prop('checked', poe_settings.streaming);
|
||||
$('#poe_token').val(poe_settings.token ?? '');
|
||||
$('#poe_impersonation_prompt').val(poe_settings.impersonation_prompt);
|
||||
selectBot();
|
||||
}
|
||||
@@ -78,11 +81,6 @@ function selectBot() {
|
||||
}
|
||||
}
|
||||
|
||||
function onTokenInput() {
|
||||
poe_settings.token = $('#poe_token').val();
|
||||
saveSettingsDebounced();
|
||||
}
|
||||
|
||||
function onBotChange() {
|
||||
poe_settings.bot = $('#poe_bots').find(":selected").val();
|
||||
saveSettingsDebounced();
|
||||
@@ -147,7 +145,6 @@ async function generatePoe(type, finalPrompt, signal) {
|
||||
async function purgeConversation(count = -1) {
|
||||
const body = JSON.stringify({
|
||||
bot: poe_settings.bot,
|
||||
token: poe_settings.token,
|
||||
count,
|
||||
});
|
||||
|
||||
@@ -167,7 +164,6 @@ async function sendMessage(prompt, withStreaming, signal) {
|
||||
|
||||
const body = JSON.stringify({
|
||||
bot: poe_settings.bot,
|
||||
token: poe_settings.token,
|
||||
streaming: withStreaming && poe_settings.streaming,
|
||||
prompt,
|
||||
});
|
||||
@@ -213,7 +209,19 @@ async function sendMessage(prompt, withStreaming, signal) {
|
||||
}
|
||||
|
||||
async function onConnectClick() {
|
||||
if (!poe_settings.token || is_poe_button_press) {
|
||||
const api_key_poe = $('#poe_token').val().trim();
|
||||
|
||||
if (api_key_poe.length) {
|
||||
await writeSecret(SECRET_KEYS.POE, api_key_poe);
|
||||
}
|
||||
|
||||
if (!secret_state[SECRET_KEYS.POE]) {
|
||||
console.error('No secret key saved for Poe');
|
||||
return;
|
||||
}
|
||||
|
||||
if ( is_poe_button_press) {
|
||||
console.log('Poe API button is pressed');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -236,7 +244,7 @@ function setButtonState(value) {
|
||||
}
|
||||
|
||||
async function checkStatusPoe() {
|
||||
const body = JSON.stringify({ token: poe_settings.token });
|
||||
const body = JSON.stringify();
|
||||
const response = await fetch('/status_poe', {
|
||||
headers: getRequestHeaders(),
|
||||
body: body,
|
||||
@@ -336,7 +344,6 @@ function onMessageRestoreClick() {
|
||||
}
|
||||
|
||||
$('document').ready(function () {
|
||||
$('#poe_token').on('input', onTokenInput);
|
||||
$('#poe_bots').on('change', onBotChange);
|
||||
$('#poe_connect').on('click', onConnectClick);
|
||||
$('#poe_activation_response').on('input', onResponseInput);
|
||||
|
62
public/scripts/secrets.js
Normal file
62
public/scripts/secrets.js
Normal file
@@ -0,0 +1,62 @@
|
||||
import { getRequestHeaders } from "../script.js";
|
||||
|
||||
export const SECRET_KEYS = {
|
||||
HORDE: 'api_key_horde',
|
||||
OPENAI: 'api_key_openai',
|
||||
POE: 'api_key_poe',
|
||||
NOVEL: 'api_key_novel',
|
||||
}
|
||||
|
||||
const INPUT_MAP = {
|
||||
[SECRET_KEYS.HORDE]: '#horde_api_key',
|
||||
[SECRET_KEYS.OPENAI]: '#api_key_openai',
|
||||
[SECRET_KEYS.POE]: '#poe_token',
|
||||
[SECRET_KEYS.NOVEL]: '#api_key_novel',
|
||||
}
|
||||
|
||||
function updateSecretDisplay() {
|
||||
for (const [secret_key, input_selector] of Object.entries(INPUT_MAP)) {
|
||||
const validSecret = !!secret_state[secret_key];
|
||||
const placeholder = validSecret ? '✔️ Key saved' : '❌ Missing key';
|
||||
$(input_selector).attr('placeholder', placeholder).val('');
|
||||
}
|
||||
}
|
||||
|
||||
export let secret_state = {};
|
||||
|
||||
export async function writeSecret(key, value) {
|
||||
try {
|
||||
const response = await fetch('/writesecret', {
|
||||
method: 'POST',
|
||||
headers: getRequestHeaders(),
|
||||
body: JSON.stringify({ key, value }),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const text = await response.text();
|
||||
|
||||
if (text == 'ok') {
|
||||
secret_state[key] = true;
|
||||
updateSecretDisplay();
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
console.error('Could not write secret value: ', key);
|
||||
}
|
||||
}
|
||||
|
||||
export async function readSecretState() {
|
||||
try {
|
||||
const response = await fetch('/readsecretstate', {
|
||||
method: 'POST',
|
||||
headers: getRequestHeaders(),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
secret_state = await response.json();
|
||||
updateSecretDisplay();
|
||||
}
|
||||
} catch {
|
||||
console.error('Could not read secrets file');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user